Release coccinelle-0.1
[bpt/coccinelle.git] / demos / demo_rule9 / sym53c8xx.c
1 /******************************************************************************
2 ** High Performance device driver for the Symbios 53C896 controller.
3 **
4 ** Copyright (C) 1998-2001 Gerard Roudier <groudier@free.fr>
5 **
6 ** This driver also supports all the Symbios 53C8XX controller family,
7 ** except 53C810 revisions < 16, 53C825 revisions < 16 and all
8 ** revisions of 53C815 controllers.
9 **
10 ** This driver is based on the Linux port of the FreeBSD ncr driver.
11 **
12 ** Copyright (C) 1994 Wolfgang Stanglmeier
13 **
14 **-----------------------------------------------------------------------------
15 **
16 ** This program is free software; you can redistribute it and/or modify
17 ** it under the terms of the GNU General Public License as published by
18 ** the Free Software Foundation; either version 2 of the License, or
19 ** (at your option) any later version.
20 **
21 ** This program is distributed in the hope that it will be useful,
22 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ** GNU General Public License for more details.
25 **
26 ** You should have received a copy of the GNU General Public License
27 ** along with this program; if not, write to the Free Software
28 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 **
30 **-----------------------------------------------------------------------------
31 **
32 ** The Linux port of the FreeBSD ncr driver has been achieved in
33 ** november 1995 by:
34 **
35 ** Gerard Roudier <groudier@free.fr>
36 **
37 ** Being given that this driver originates from the FreeBSD version, and
38 ** in order to keep synergy on both, any suggested enhancements and corrections
39 ** received on Linux are automatically a potential candidate for the FreeBSD
40 ** version.
41 **
42 ** The original driver has been written for 386bsd and FreeBSD by
43 ** Wolfgang Stanglmeier <wolf@cologne.de>
44 ** Stefan Esser <se@mi.Uni-Koeln.de>
45 **
46 **-----------------------------------------------------------------------------
47 **
48 ** Major contributions:
49 ** --------------------
50 **
51 ** NVRAM detection and reading.
52 ** Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
53 **
54 *******************************************************************************
55 */
56
57 /*
58 ** Supported SCSI features:
59 ** Synchronous data transfers
60 ** Wide16 SCSI BUS
61 ** Disconnection/Reselection
62 ** Tagged command queuing
63 ** SCSI Parity checking
64 **
65 ** Supported NCR/SYMBIOS chips:
66 ** 53C810A (8 bits, Fast 10, no rom BIOS)
67 ** 53C825A (Wide, Fast 10, on-board rom BIOS)
68 ** 53C860 (8 bits, Fast 20, no rom BIOS)
69 ** 53C875 (Wide, Fast 20, on-board rom BIOS)
70 ** 53C876 (Wide, Fast 20 Dual, on-board rom BIOS)
71 ** 53C895 (Wide, Fast 40, on-board rom BIOS)
72 ** 53C895A (Wide, Fast 40, on-board rom BIOS)
73 ** 53C896 (Wide, Fast 40 Dual, on-board rom BIOS)
74 ** 53C897 (Wide, Fast 40 Dual, on-board rom BIOS)
75 ** 53C1510D (Wide, Fast 40 Dual, on-board rom BIOS)
76 ** 53C1010 (Wide, Fast 80 Dual, on-board rom BIOS)
77 ** 53C1010_66(Wide, Fast 80 Dual, on-board rom BIOS, 33/66MHz PCI)
78 **
79 ** Other features:
80 ** Memory mapped IO
81 ** Module
82 ** Shared IRQ
83 */
84
85 /*
86 ** Name and version of the driver
87 */
88 #define SCSI_NCR_DRIVER_NAME "sym53c8xx-1.7.3c-20010512"
89
90 #define SCSI_NCR_DEBUG_FLAGS (0)
91
92 #define NAME53C "sym53c"
93 #define NAME53C8XX "sym53c8xx"
94
95 /*==========================================================
96 **
97 ** Include files
98 **
99 **==========================================================
100 */
101
102 #include <linux/version.h>
103
104 #include <linux/module.h>
105
106 #include <asm/dma.h>
107 #include <asm/io.h>
108 #include <asm/system.h>
109 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,17)
110 #include <linux/spinlock.h>
111 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,93)
112 #include <asm/spinlock.h>
113 #endif
114 #include <linux/delay.h>
115 #include <linux/interrupt.h>
116 #include <linux/signal.h>
117 #include <linux/errno.h>
118 #include <linux/pci.h>
119 #include <linux/string.h>
120 #include <linux/mm.h>
121 #include <linux/ioport.h>
122 #include <linux/time.h>
123 #include <linux/timer.h>
124 #include <linux/stat.h>
125
126 #include <linux/blk.h>
127
128 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,35)
129 #include <linux/init.h>
130 #endif
131
132 #ifndef __init
133 #define __init
134 #endif
135 #ifndef __initdata
136 #define __initdata
137 #endif
138
139 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,1,92)
140 #include <linux/bios32.h>
141 #endif
142
143 #include "scsi.h"
144 #include "hosts.h"
145
146 #include <linux/types.h>
147
148 /*
149 ** Define BITS_PER_LONG for earlier linux versions.
150 */
151 #ifndef BITS_PER_LONG
152 #if (~0UL) == 0xffffffffUL
153 #define BITS_PER_LONG 32
154 #else
155 #define BITS_PER_LONG 64
156 #endif
157 #endif
158
159 /*
160 ** Define the BSD style u_int32 and u_int64 type.
161 ** Are in fact u_int32_t and u_int64_t :-)
162 */
163 typedef u32 u_int32;
164 typedef u64 u_int64;
165
166 #include "sym53c8xx.h"
167
168 /*
169 ** Donnot compile integrity checking code for Linux-2.3.0
170 ** and above since SCSI data structures are not ready yet.
171 */
172 /* #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0) */
173 #if 0
174 #define SCSI_NCR_INTEGRITY_CHECKING
175 #endif
176
177 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
178 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
179
180 /*
181 ** Hmmm... What complex some PCI-HOST bridges actually are,
182 ** despite the fact that the PCI specifications are looking
183 ** so smart and simple! ;-)
184 */
185 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,47)
186 #define SCSI_NCR_DYNAMIC_DMA_MAPPING
187 #endif
188
189 /*==========================================================
190 **
191 ** A la VMS/CAM-3 queue management.
192 ** Implemented from linux list management.
193 **
194 **==========================================================
195 */
196
197 typedef struct xpt_quehead {
198 struct xpt_quehead *flink; /* Forward pointer */
199 struct xpt_quehead *blink; /* Backward pointer */
200 } XPT_QUEHEAD;
201
202 #define xpt_que_init(ptr) do { \
203 (ptr)->flink = (ptr); (ptr)->blink = (ptr); \
204 } while (0)
205
206 static inline void __xpt_que_add(struct xpt_quehead * new,
207 struct xpt_quehead * blink,
208 struct xpt_quehead * flink)
209 {
210 flink->blink = new;
211 new->flink = flink;
212 new->blink = blink;
213 blink->flink = new;
214 }
215
216 static inline void __xpt_que_del(struct xpt_quehead * blink,
217 struct xpt_quehead * flink)
218 {
219 flink->blink = blink;
220 blink->flink = flink;
221 }
222
223 static inline int xpt_que_empty(struct xpt_quehead *head)
224 {
225 return head->flink == head;
226 }
227
228 static inline void xpt_que_splice(struct xpt_quehead *list,
229 struct xpt_quehead *head)
230 {
231 struct xpt_quehead *first = list->flink;
232
233 if (first != list) {
234 struct xpt_quehead *last = list->blink;
235 struct xpt_quehead *at = head->flink;
236
237 first->blink = head;
238 head->flink = first;
239
240 last->flink = at;
241 at->blink = last;
242 }
243 }
244
245 #define xpt_que_entry(ptr, type, member) \
246 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
247
248
249 #define xpt_insque(new, pos) __xpt_que_add(new, pos, (pos)->flink)
250
251 #define xpt_remque(el) __xpt_que_del((el)->blink, (el)->flink)
252
253 #define xpt_insque_head(new, head) __xpt_que_add(new, head, (head)->flink)
254
255 static inline struct xpt_quehead *xpt_remque_head(struct xpt_quehead *head)
256 {
257 struct xpt_quehead *elem = head->flink;
258
259 if (elem != head)
260 __xpt_que_del(head, elem->flink);
261 else
262 elem = 0;
263 return elem;
264 }
265
266 #define xpt_insque_tail(new, head) __xpt_que_add(new, (head)->blink, head)
267
268 static inline struct xpt_quehead *xpt_remque_tail(struct xpt_quehead *head)
269 {
270 struct xpt_quehead *elem = head->blink;
271
272 if (elem != head)
273 __xpt_que_del(elem->blink, head);
274 else
275 elem = 0;
276 return elem;
277 }
278
279 /*==========================================================
280 **
281 ** Configuration and Debugging
282 **
283 **==========================================================
284 */
285
286 /*
287 ** SCSI address of this device.
288 ** The boot routines should have set it.
289 ** If not, use this.
290 */
291
292 #ifndef SCSI_NCR_MYADDR
293 #define SCSI_NCR_MYADDR (7)
294 #endif
295
296 /*
297 ** The maximum number of tags per logic unit.
298 ** Used only for devices that support tags.
299 */
300
301 #ifndef SCSI_NCR_MAX_TAGS
302 #define SCSI_NCR_MAX_TAGS (8)
303 #endif
304
305 /*
306 ** TAGS are actually unlimited (256 tags/lun).
307 ** But Linux only supports 255. :)
308 */
309 #if SCSI_NCR_MAX_TAGS > 255
310 #define MAX_TAGS 255
311 #else
312 #define MAX_TAGS SCSI_NCR_MAX_TAGS
313 #endif
314
315 /*
316 ** Since the ncr chips only have a 8 bit ALU, we try to be clever
317 ** about offset calculation in the TASK TABLE per LUN that is an
318 ** array of DWORDS = 4 bytes.
319 */
320 #if MAX_TAGS > (512/4)
321 #define MAX_TASKS (1024/4)
322 #elif MAX_TAGS > (256/4)
323 #define MAX_TASKS (512/4)
324 #else
325 #define MAX_TASKS (256/4)
326 #endif
327
328 /*
329 ** This one means 'NO TAG for this job'
330 */
331 #define NO_TAG (256)
332
333 /*
334 ** Number of targets supported by the driver.
335 ** n permits target numbers 0..n-1.
336 ** Default is 16, meaning targets #0..#15.
337 ** #7 .. is myself.
338 */
339
340 #ifdef SCSI_NCR_MAX_TARGET
341 #define MAX_TARGET (SCSI_NCR_MAX_TARGET)
342 #else
343 #define MAX_TARGET (16)
344 #endif
345
346 /*
347 ** Number of logic units supported by the driver.
348 ** n enables logic unit numbers 0..n-1.
349 ** The common SCSI devices require only
350 ** one lun, so take 1 as the default.
351 */
352
353 #ifdef SCSI_NCR_MAX_LUN
354 #define MAX_LUN 64
355 #else
356 #define MAX_LUN (1)
357 #endif
358
359 /*
360 ** Asynchronous pre-scaler (ns). Shall be 40 for
361 ** the SCSI timings to be compliant.
362 */
363
364 #ifndef SCSI_NCR_MIN_ASYNC
365 #define SCSI_NCR_MIN_ASYNC (40)
366 #endif
367
368 /*
369 ** The maximum number of jobs scheduled for starting.
370 ** We allocate 4 entries more than the value we announce
371 ** to the SCSI upper layer. Guess why ! :-)
372 */
373
374 #ifdef SCSI_NCR_CAN_QUEUE
375 #define MAX_START (SCSI_NCR_CAN_QUEUE + 4)
376 #else
377 #define MAX_START (MAX_TARGET + 7 * MAX_TAGS)
378 #endif
379
380 /*
381 ** We donnot want to allocate more than 1 PAGE for the
382 ** the start queue and the done queue. We hard-code entry
383 ** size to 8 in order to let cpp do the checking.
384 ** Allows 512-4=508 pending IOs for i386 but Linux seems for
385 ** now not able to provide the driver with this amount of IOs.
386 */
387 #if MAX_START > PAGE_SIZE/8
388 #undef MAX_START
389 #define MAX_START (PAGE_SIZE/8)
390 #endif
391
392 /*
393 ** The maximum number of segments a transfer is split into.
394 ** We support up to 127 segments for both read and write.
395 */
396
397 #define MAX_SCATTER (SCSI_NCR_MAX_SCATTER)
398 #define SCR_SG_SIZE (2)
399
400 /*
401 ** other
402 */
403
404 #define NCR_SNOOP_TIMEOUT (1000000)
405
406 /*==========================================================
407 **
408 ** Miscallaneous BSDish defines.
409 **
410 **==========================================================
411 */
412
413 #define u_char unsigned char
414 #define u_short unsigned short
415 #define u_int unsigned int
416 #define u_long unsigned long
417
418 #ifndef bcopy
419 #define bcopy(s, d, n) memcpy((d), (s), (n))
420 #endif
421
422 #ifndef bzero
423 #define bzero(d, n) memset((d), 0, (n))
424 #endif
425
426 #ifndef offsetof
427 #define offsetof(t, m) ((size_t) (&((t *)0)->m))
428 #endif
429
430 /*
431 ** Simple Wrapper to kernel PCI bus interface.
432 **
433 ** This wrapper allows to get rid of old kernel PCI interface
434 ** and still allows to preserve linux-2.0 compatibilty.
435 ** In fact, it is mostly an incomplete emulation of the new
436 ** PCI code for pre-2.2 kernels. When kernel-2.0 support
437 ** will be dropped, we will just have to remove most of this
438 ** code.
439 */
440
441 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
442
443 typedef struct pci_dev *pcidev_t;
444 #define PCIDEV_NULL (0)
445 #define PciBusNumber(d) (d)->bus->number
446 #define PciDeviceFn(d) (d)->devfn
447 #define PciVendorId(d) (d)->vendor
448 #define PciDeviceId(d) (d)->device
449 #define PciIrqLine(d) (d)->irq
450
451 static u_long __init
452 pci_get_base_cookie(struct pci_dev *pdev, int index)
453 {
454 u_long base;
455
456 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,12)
457 base = pdev->resource[index].start;
458 #else
459 base = pdev->base_address[index];
460 #if BITS_PER_LONG > 32
461 if ((base & 0x7) == 0x4)
462 *base |= (((u_long)pdev->base_address[++index]) << 32);
463 #endif
464 #endif
465 return (base & ~0x7ul);
466 }
467
468 static int __init
469 pci_get_base_address(struct pci_dev *pdev, int index, u_long *base)
470 {
471 u32 tmp;
472 #define PCI_BAR_OFFSET(index) (PCI_BASE_ADDRESS_0 + (index<<2))
473
474 pci_read_config_dword(pdev, PCI_BAR_OFFSET(index), &tmp);
475 *base = tmp;
476 ++index;
477 if ((tmp & 0x7) == 0x4) {
478 #if BITS_PER_LONG > 32
479 pci_read_config_dword(pdev, PCI_BAR_OFFSET(index), &tmp);
480 *base |= (((u_long)tmp) << 32);
481 #endif
482 ++index;
483 }
484 return index;
485 #undef PCI_BAR_OFFSET
486 }
487
488 #else /* Incomplete emulation of current PCI code for pre-2.2 kernels */
489
490 typedef unsigned int pcidev_t;
491 #define PCIDEV_NULL (~0u)
492 #define PciBusNumber(d) ((d)>>8)
493 #define PciDeviceFn(d) ((d)&0xff)
494 #define __PciDev(busn, devfn) (((busn)<<8)+(devfn))
495
496 #define pci_present pcibios_present
497
498 #define pci_read_config_byte(d, w, v) \
499 pcibios_read_config_byte(PciBusNumber(d), PciDeviceFn(d), w, v)
500 #define pci_read_config_word(d, w, v) \
501 pcibios_read_config_word(PciBusNumber(d), PciDeviceFn(d), w, v)
502 #define pci_read_config_dword(d, w, v) \
503 pcibios_read_config_dword(PciBusNumber(d), PciDeviceFn(d), w, v)
504
505 #define pci_write_config_byte(d, w, v) \
506 pcibios_write_config_byte(PciBusNumber(d), PciDeviceFn(d), w, v)
507 #define pci_write_config_word(d, w, v) \
508 pcibios_write_config_word(PciBusNumber(d), PciDeviceFn(d), w, v)
509 #define pci_write_config_dword(d, w, v) \
510 pcibios_write_config_dword(PciBusNumber(d), PciDeviceFn(d), w, v)
511
512 static pcidev_t __init
513 pci_find_device(unsigned int vendor, unsigned int device, pcidev_t prev)
514 {
515 static unsigned short pci_index;
516 int retv;
517 unsigned char bus_number, device_fn;
518
519 if (prev == PCIDEV_NULL)
520 pci_index = 0;
521 else
522 ++pci_index;
523 retv = pcibios_find_device (vendor, device, pci_index,
524 &bus_number, &device_fn);
525 return retv ? PCIDEV_NULL : __PciDev(bus_number, device_fn);
526 }
527
528 static u_short __init PciVendorId(pcidev_t dev)
529 {
530 u_short vendor_id;
531 pci_read_config_word(dev, PCI_VENDOR_ID, &vendor_id);
532 return vendor_id;
533 }
534
535 static u_short __init PciDeviceId(pcidev_t dev)
536 {
537 u_short device_id;
538 pci_read_config_word(dev, PCI_DEVICE_ID, &device_id);
539 return device_id;
540 }
541
542 static u_int __init PciIrqLine(pcidev_t dev)
543 {
544 u_char irq;
545 pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
546 return irq;
547 }
548
549 static int __init
550 pci_get_base_address(pcidev_t dev, int offset, u_long *base)
551 {
552 u_int32 tmp;
553
554 pci_read_config_dword(dev, PCI_BASE_ADDRESS_0 + offset, &tmp);
555 *base = tmp;
556 offset += sizeof(u_int32);
557 if ((tmp & 0x7) == 0x4) {
558 #if BITS_PER_LONG > 32
559 pci_read_config_dword(dev, PCI_BASE_ADDRESS_0 + offset, &tmp);
560 *base |= (((u_long)tmp) << 32);
561 #endif
562 offset += sizeof(u_int32);
563 }
564 return offset;
565 }
566 static u_long __init
567 pci_get_base_cookie(struct pci_dev *pdev, int offset)
568 {
569 u_long base;
570
571 (void) pci_get_base_address(dev, offset, &base);
572
573 return base;
574 }
575
576 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0) */
577
578 /* Does not make sense in earlier kernels */
579 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
580 #define pci_enable_device(pdev) (0)
581 #endif
582 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,4)
583 #define scsi_set_pci_device(inst, pdev) (0)
584 #endif
585
586 /*==========================================================
587 **
588 ** Debugging tags
589 **
590 **==========================================================
591 */
592
593 #define DEBUG_ALLOC (0x0001)
594 #define DEBUG_PHASE (0x0002)
595 #define DEBUG_QUEUE (0x0008)
596 #define DEBUG_RESULT (0x0010)
597 #define DEBUG_POINTER (0x0020)
598 #define DEBUG_SCRIPT (0x0040)
599 #define DEBUG_TINY (0x0080)
600 #define DEBUG_TIMING (0x0100)
601 #define DEBUG_NEGO (0x0200)
602 #define DEBUG_TAGS (0x0400)
603 #define DEBUG_IC (0x0800)
604
605 /*
606 ** Enable/Disable debug messages.
607 ** Can be changed at runtime too.
608 */
609
610 #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
611 static int ncr_debug = SCSI_NCR_DEBUG_FLAGS;
612 #define DEBUG_FLAGS ncr_debug
613 #else
614 #define DEBUG_FLAGS SCSI_NCR_DEBUG_FLAGS
615 #endif
616
617 /*
618 ** SMP threading.
619 **
620 ** Assuming that SMP systems are generally high end systems and may
621 ** use several SCSI adapters, we are using one lock per controller
622 ** instead of some global one. For the moment (linux-2.1.95), driver's
623 ** entry points are called with the 'io_request_lock' lock held, so:
624 ** - We are uselessly loosing a couple of micro-seconds to lock the
625 ** controller data structure.
626 ** - But the driver is not broken by design for SMP and so can be
627 ** more resistant to bugs or bad changes in the IO sub-system code.
628 ** - A small advantage could be that the interrupt code is grained as
629 ** wished (e.g.: threaded by controller).
630 */
631
632 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,93)
633
634 spinlock_t sym53c8xx_lock = SPIN_LOCK_UNLOCKED;
635 #define NCR_LOCK_DRIVER(flags) spin_lock_irqsave(&sym53c8xx_lock, flags)
636 #define NCR_UNLOCK_DRIVER(flags) spin_unlock_irqrestore(&sym53c8xx_lock,flags)
637
638 #define NCR_INIT_LOCK_NCB(np) spin_lock_init(&np->smp_lock);
639 #define NCR_LOCK_NCB(np, flags) spin_lock_irqsave(&np->smp_lock, flags)
640 #define NCR_UNLOCK_NCB(np, flags) spin_unlock_irqrestore(&np->smp_lock, flags)
641
642 #define NCR_LOCK_SCSI_DONE(host, flags) \
643 spin_lock_irqsave(((host)->host_lock), flags)
644 #define NCR_UNLOCK_SCSI_DONE(host, flags) \
645 spin_unlock_irqrestore(((host)->host_lock), flags)
646
647 #else
648
649 #define NCR_LOCK_DRIVER(flags) do { save_flags(flags); cli(); } while (0)
650 #define NCR_UNLOCK_DRIVER(flags) do { restore_flags(flags); } while (0)
651
652 #define NCR_INIT_LOCK_NCB(np) do { } while (0)
653 #define NCR_LOCK_NCB(np, flags) do { save_flags(flags); cli(); } while (0)
654 #define NCR_UNLOCK_NCB(np, flags) do { restore_flags(flags); } while (0)
655
656 #define NCR_LOCK_SCSI_DONE(host, flags) do {;} while (0)
657 #define NCR_UNLOCK_SCSI_DONE(host, flags) do {;} while (0)
658
659 #endif
660
661 /*
662 ** Memory mapped IO
663 **
664 ** Since linux-2.1, we must use ioremap() to map the io memory space.
665 ** iounmap() to unmap it. That allows portability.
666 ** Linux 1.3.X and 2.0.X allow to remap physical pages addresses greater
667 ** than the highest physical memory address to kernel virtual pages with
668 ** vremap() / vfree(). That was not portable but worked with i386
669 ** architecture.
670 */
671
672 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
673 #define ioremap vremap
674 #define iounmap vfree
675 #endif
676
677 #ifdef __sparc__
678 # include <asm/irq.h>
679 # define memcpy_to_pci(a, b, c) memcpy_toio((a), (b), (c))
680 #elif defined(__alpha__)
681 # define memcpy_to_pci(a, b, c) memcpy_toio((a), (b), (c))
682 #else /* others */
683 # define memcpy_to_pci(a, b, c) memcpy_toio((a), (b), (c))
684 #endif
685
686 #ifndef SCSI_NCR_PCI_MEM_NOT_SUPPORTED
687 static u_long __init remap_pci_mem(u_long base, u_long size)
688 {
689 u_long page_base = ((u_long) base) & PAGE_MASK;
690 u_long page_offs = ((u_long) base) - page_base;
691 u_long page_remapped = (u_long) ioremap(page_base, page_offs+size);
692
693 return page_remapped? (page_remapped + page_offs) : 0UL;
694 }
695
696 static void __init unmap_pci_mem(u_long vaddr, u_long size)
697 {
698 if (vaddr)
699 iounmap((void *) (vaddr & PAGE_MASK));
700 }
701
702 #endif /* not def SCSI_NCR_PCI_MEM_NOT_SUPPORTED */
703
704 /*
705 ** Insert a delay in micro-seconds and milli-seconds.
706 ** -------------------------------------------------
707 ** Under Linux, udelay() is restricted to delay < 1 milli-second.
708 ** In fact, it generally works for up to 1 second delay.
709 ** Since 2.1.105, the mdelay() function is provided for delays
710 ** in milli-seconds.
711 ** Under 2.0 kernels, udelay() is an inline function that is very
712 ** inaccurate on Pentium processors.
713 */
714
715 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,105)
716 #define UDELAY udelay
717 #define MDELAY mdelay
718 #else
719 static void UDELAY(long us) { udelay(us); }
720 static void MDELAY(long ms) { while (ms--) UDELAY(1000); }
721 #endif
722
723 /*
724 ** Simple power of two buddy-like allocator
725 ** ----------------------------------------
726 ** This simple code is not intended to be fast, but to provide
727 ** power of 2 aligned memory allocations.
728 ** Since the SCRIPTS processor only supplies 8 bit arithmetic,
729 ** this allocator allows simple and fast address calculations
730 ** from the SCRIPTS code. In addition, cache line alignment
731 ** is guaranteed for power of 2 cache line size.
732 ** Enhanced in linux-2.3.44 to provide a memory pool per pcidev
733 ** to support dynamic dma mapping. (I would have preferred a
734 ** real bus astraction, btw).
735 */
736
737 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
738 #define __GetFreePages(flags, order) __get_free_pages(flags, order)
739 #else
740 #define __GetFreePages(flags, order) __get_free_pages(flags, order, 0)
741 #endif
742
743 #define MEMO_SHIFT 4 /* 16 bytes minimum memory chunk */
744 #if PAGE_SIZE >= 8192
745 #define MEMO_PAGE_ORDER 0 /* 1 PAGE maximum */
746 #else
747 #define MEMO_PAGE_ORDER 1 /* 2 PAGES maximum */
748 #endif
749 #define MEMO_FREE_UNUSED /* Free unused pages immediately */
750 #define MEMO_WARN 1
751 #define MEMO_GFP_FLAGS GFP_ATOMIC
752 #define MEMO_CLUSTER_SHIFT (PAGE_SHIFT+MEMO_PAGE_ORDER)
753 #define MEMO_CLUSTER_SIZE (1UL << MEMO_CLUSTER_SHIFT)
754 #define MEMO_CLUSTER_MASK (MEMO_CLUSTER_SIZE-1)
755
756 typedef u_long m_addr_t; /* Enough bits to bit-hack addresses */
757 typedef pcidev_t m_bush_t; /* Something that addresses DMAable */
758
759 typedef struct m_link { /* Link between free memory chunks */
760 struct m_link *next;
761 } m_link_s;
762
763 #ifdef SCSI_NCR_DYNAMIC_DMA_MAPPING
764 typedef struct m_vtob { /* Virtual to Bus address translation */
765 struct m_vtob *next;
766 m_addr_t vaddr;
767 m_addr_t baddr;
768 } m_vtob_s;
769 #define VTOB_HASH_SHIFT 5
770 #define VTOB_HASH_SIZE (1UL << VTOB_HASH_SHIFT)
771 #define VTOB_HASH_MASK (VTOB_HASH_SIZE-1)
772 #define VTOB_HASH_CODE(m) \
773 ((((m_addr_t) (m)) >> MEMO_CLUSTER_SHIFT) & VTOB_HASH_MASK)
774 #endif
775
776 typedef struct m_pool { /* Memory pool of a given kind */
777 #ifdef SCSI_NCR_DYNAMIC_DMA_MAPPING
778 m_bush_t bush;
779 m_addr_t (*getp)(struct m_pool *);
780 void (*freep)(struct m_pool *, m_addr_t);
781 #define M_GETP() mp->getp(mp)
782 #define M_FREEP(p) mp->freep(mp, p)
783 #define GetPages() __GetFreePages(MEMO_GFP_FLAGS, MEMO_PAGE_ORDER)
784 #define FreePages(p) free_pages(p, MEMO_PAGE_ORDER)
785 int nump;
786 m_vtob_s *(vtob[VTOB_HASH_SIZE]);
787 struct m_pool *next;
788 #else
789 #define M_GETP() __GetFreePages(MEMO_GFP_FLAGS, MEMO_PAGE_ORDER)
790 #define M_FREEP(p) free_pages(p, MEMO_PAGE_ORDER)
791 #endif /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
792 struct m_link h[PAGE_SHIFT-MEMO_SHIFT+MEMO_PAGE_ORDER+1];
793 } m_pool_s;
794
795 static void *___m_alloc(m_pool_s *mp, int size)
796 {
797 int i = 0;
798 int s = (1 << MEMO_SHIFT);
799 int j;
800 m_addr_t a;
801 m_link_s *h = mp->h;
802
803 if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
804 return 0;
805
806 while (size > s) {
807 s <<= 1;
808 ++i;
809 }
810
811 j = i;
812 while (!h[j].next) {
813 if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
814 h[j].next = (m_link_s *) M_GETP();
815 if (h[j].next)
816 h[j].next->next = 0;
817 break;
818 }
819 ++j;
820 s <<= 1;
821 }
822 a = (m_addr_t) h[j].next;
823 if (a) {
824 h[j].next = h[j].next->next;
825 while (j > i) {
826 j -= 1;
827 s >>= 1;
828 h[j].next = (m_link_s *) (a+s);
829 h[j].next->next = 0;
830 }
831 }
832 #ifdef DEBUG
833 printk("___m_alloc(%d) = %p\n", size, (void *) a);
834 #endif
835 return (void *) a;
836 }
837
838 static void ___m_free(m_pool_s *mp, void *ptr, int size)
839 {
840 int i = 0;
841 int s = (1 << MEMO_SHIFT);
842 m_link_s *q;
843 m_addr_t a, b;
844 m_link_s *h = mp->h;
845
846 #ifdef DEBUG
847 printk("___m_free(%p, %d)\n", ptr, size);
848 #endif
849
850 if (size > (PAGE_SIZE << MEMO_PAGE_ORDER))
851 return;
852
853 while (size > s) {
854 s <<= 1;
855 ++i;
856 }
857
858 a = (m_addr_t) ptr;
859
860 while (1) {
861 #ifdef MEMO_FREE_UNUSED
862 if (s == (PAGE_SIZE << MEMO_PAGE_ORDER)) {
863 M_FREEP(a);
864 break;
865 }
866 #endif
867 b = a ^ s;
868 q = &h[i];
869 while (q->next && q->next != (m_link_s *) b) {
870 q = q->next;
871 }
872 if (!q->next) {
873 ((m_link_s *) a)->next = h[i].next;
874 h[i].next = (m_link_s *) a;
875 break;
876 }
877 q->next = q->next->next;
878 a = a & b;
879 s <<= 1;
880 ++i;
881 }
882 }
883
884 static void *__m_calloc2(m_pool_s *mp, int size, char *name, int uflags)
885 {
886 void *p;
887
888 p = ___m_alloc(mp, size);
889
890 if (DEBUG_FLAGS & DEBUG_ALLOC)
891 printk ("new %-10s[%4d] @%p.\n", name, size, p);
892
893 if (p)
894 bzero(p, size);
895 else if (uflags & MEMO_WARN)
896 printk (NAME53C8XX ": failed to allocate %s[%d]\n", name, size);
897
898 return p;
899 }
900
901 #define __m_calloc(mp, s, n) __m_calloc2(mp, s, n, MEMO_WARN)
902
903 static void __m_free(m_pool_s *mp, void *ptr, int size, char *name)
904 {
905 if (DEBUG_FLAGS & DEBUG_ALLOC)
906 printk ("freeing %-10s[%4d] @%p.\n", name, size, ptr);
907
908 ___m_free(mp, ptr, size);
909
910 }
911
912 /*
913 * With pci bus iommu support, we use a default pool of unmapped memory
914 * for memory we donnot need to DMA from/to and one pool per pcidev for
915 * memory accessed by the PCI chip. `mp0' is the default not DMAable pool.
916 */
917
918 #ifndef SCSI_NCR_DYNAMIC_DMA_MAPPING
919
920 static m_pool_s mp0;
921
922 #else
923
924 static m_addr_t ___mp0_getp(m_pool_s *mp)
925 {
926 m_addr_t m = GetPages();
927 if (m)
928 ++mp->nump;
929 return m;
930 }
931
932 static void ___mp0_freep(m_pool_s *mp, m_addr_t m)
933 {
934 FreePages(m);
935 --mp->nump;
936 }
937
938 static m_pool_s mp0 = {0, ___mp0_getp, ___mp0_freep};
939
940 #endif /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
941
942 static void *m_calloc(int size, char *name)
943 {
944 u_long flags;
945 void *m;
946 NCR_LOCK_DRIVER(flags);
947 m = __m_calloc(&mp0, size, name);
948 NCR_UNLOCK_DRIVER(flags);
949 return m;
950 }
951
952 static void m_free(void *ptr, int size, char *name)
953 {
954 u_long flags;
955 NCR_LOCK_DRIVER(flags);
956 __m_free(&mp0, ptr, size, name);
957 NCR_UNLOCK_DRIVER(flags);
958 }
959
960 /*
961 * DMAable pools.
962 */
963
964 #ifndef SCSI_NCR_DYNAMIC_DMA_MAPPING
965
966 /* Without pci bus iommu support, all the memory is assumed DMAable */
967
968 #define __m_calloc_dma(b, s, n) m_calloc(s, n)
969 #define __m_free_dma(b, p, s, n) m_free(p, s, n)
970 #define __vtobus(b, p) virt_to_bus(p)
971
972 #else
973
974 /*
975 * With pci bus iommu support, we maintain one pool per pcidev and a
976 * hashed reverse table for virtual to bus physical address translations.
977 */
978 static m_addr_t ___dma_getp(m_pool_s *mp)
979 {
980 m_addr_t vp;
981 m_vtob_s *vbp;
982
983 vbp = __m_calloc(&mp0, sizeof(*vbp), "VTOB");
984 if (vbp) {
985 dma_addr_t daddr;
986 vp = (m_addr_t) pci_alloc_consistent(mp->bush,
987 PAGE_SIZE<<MEMO_PAGE_ORDER,
988 &daddr);
989 if (vp) {
990 int hc = VTOB_HASH_CODE(vp);
991 vbp->vaddr = vp;
992 vbp->baddr = daddr;
993 vbp->next = mp->vtob[hc];
994 mp->vtob[hc] = vbp;
995 ++mp->nump;
996 return vp;
997 }
998 else
999 __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
1000 }
1001 return 0;
1002 }
1003
1004 static void ___dma_freep(m_pool_s *mp, m_addr_t m)
1005 {
1006 m_vtob_s **vbpp, *vbp;
1007 int hc = VTOB_HASH_CODE(m);
1008
1009 vbpp = &mp->vtob[hc];
1010 while (*vbpp && (*vbpp)->vaddr != m)
1011 vbpp = &(*vbpp)->next;
1012 if (*vbpp) {
1013 vbp = *vbpp;
1014 *vbpp = (*vbpp)->next;
1015 pci_free_consistent(mp->bush, PAGE_SIZE<<MEMO_PAGE_ORDER,
1016 (void *)vbp->vaddr, (dma_addr_t)vbp->baddr);
1017 __m_free(&mp0, vbp, sizeof(*vbp), "VTOB");
1018 --mp->nump;
1019 }
1020 }
1021
1022 static inline m_pool_s *___get_dma_pool(m_bush_t bush)
1023 {
1024 m_pool_s *mp;
1025 for (mp = mp0.next; mp && mp->bush != bush; mp = mp->next);
1026 return mp;
1027 }
1028
1029 static m_pool_s *___cre_dma_pool(m_bush_t bush)
1030 {
1031 m_pool_s *mp;
1032 mp = __m_calloc(&mp0, sizeof(*mp), "MPOOL");
1033 if (mp) {
1034 bzero(mp, sizeof(*mp));
1035 mp->bush = bush;
1036 mp->getp = ___dma_getp;
1037 mp->freep = ___dma_freep;
1038 mp->next = mp0.next;
1039 mp0.next = mp;
1040 }
1041 return mp;
1042 }
1043
1044 static void ___del_dma_pool(m_pool_s *p)
1045 {
1046 struct m_pool **pp = &mp0.next;
1047
1048 while (*pp && *pp != p)
1049 pp = &(*pp)->next;
1050 if (*pp) {
1051 *pp = (*pp)->next;
1052 __m_free(&mp0, p, sizeof(*p), "MPOOL");
1053 }
1054 }
1055
1056 static void *__m_calloc_dma(m_bush_t bush, int size, char *name)
1057 {
1058 u_long flags;
1059 struct m_pool *mp;
1060 void *m = 0;
1061
1062 NCR_LOCK_DRIVER(flags);
1063 mp = ___get_dma_pool(bush);
1064 if (!mp)
1065 mp = ___cre_dma_pool(bush);
1066 if (mp)
1067 m = __m_calloc(mp, size, name);
1068 if (mp && !mp->nump)
1069 ___del_dma_pool(mp);
1070 NCR_UNLOCK_DRIVER(flags);
1071
1072 return m;
1073 }
1074
1075 static void __m_free_dma(m_bush_t bush, void *m, int size, char *name)
1076 {
1077 u_long flags;
1078 struct m_pool *mp;
1079
1080 NCR_LOCK_DRIVER(flags);
1081 mp = ___get_dma_pool(bush);
1082 if (mp)
1083 __m_free(mp, m, size, name);
1084 if (mp && !mp->nump)
1085 ___del_dma_pool(mp);
1086 NCR_UNLOCK_DRIVER(flags);
1087 }
1088
1089 static m_addr_t __vtobus(m_bush_t bush, void *m)
1090 {
1091 u_long flags;
1092 m_pool_s *mp;
1093 int hc = VTOB_HASH_CODE(m);
1094 m_vtob_s *vp = 0;
1095 m_addr_t a = ((m_addr_t) m) & ~MEMO_CLUSTER_MASK;
1096
1097 NCR_LOCK_DRIVER(flags);
1098 mp = ___get_dma_pool(bush);
1099 if (mp) {
1100 vp = mp->vtob[hc];
1101 while (vp && (m_addr_t) vp->vaddr != a)
1102 vp = vp->next;
1103 }
1104 NCR_UNLOCK_DRIVER(flags);
1105 return vp ? vp->baddr + (((m_addr_t) m) - a) : 0;
1106 }
1107
1108 #endif /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
1109
1110 #define _m_calloc_dma(np, s, n) __m_calloc_dma(np->pdev, s, n)
1111 #define _m_free_dma(np, p, s, n) __m_free_dma(np->pdev, p, s, n)
1112 #define m_calloc_dma(s, n) _m_calloc_dma(np, s, n)
1113 #define m_free_dma(p, s, n) _m_free_dma(np, p, s, n)
1114 #define _vtobus(np, p) __vtobus(np->pdev, p)
1115 #define vtobus(p) _vtobus(np, p)
1116
1117 /*
1118 * Deal with DMA mapping/unmapping.
1119 */
1120
1121 #ifndef SCSI_NCR_DYNAMIC_DMA_MAPPING
1122
1123 /* Linux versions prior to pci bus iommu kernel interface */
1124
1125 #define __unmap_scsi_data(pdev, cmd) do {; } while (0)
1126 #define __map_scsi_single_data(pdev, cmd) (__vtobus(pdev,(cmd)->request_buffer))
1127 #define __map_scsi_sg_data(pdev, cmd) ((cmd)->use_sg)
1128 #define __sync_scsi_data(pdev, cmd) do {; } while (0)
1129
1130 #define scsi_sg_dma_address(sc) vtobus((sc)->address)
1131 #define scsi_sg_dma_len(sc) ((sc)->length)
1132
1133 #else
1134
1135 /* Linux version with pci bus iommu kernel interface */
1136
1137 /* To keep track of the dma mapping (sg/single) that has been set */
1138 #define __data_mapped(cmd) (cmd)->SCp.phase
1139 #define __data_mapping(cmd) (cmd)->SCp.dma_handle
1140
1141 static void __unmap_scsi_data(pcidev_t pdev, Scsi_Cmnd *cmd)
1142 {
1143 int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
1144
1145 switch(__data_mapped(cmd)) {
1146 case 2:
1147 pci_unmap_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
1148 break;
1149 case 1:
1150 pci_unmap_page(pdev, __data_mapping(cmd),
1151 cmd->request_bufflen, dma_dir);
1152 break;
1153 }
1154 __data_mapped(cmd) = 0;
1155 }
1156
1157 static dma_addr_t __map_scsi_single_data(pcidev_t pdev, Scsi_Cmnd *cmd)
1158 {
1159 dma_addr_t mapping;
1160 int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
1161
1162 if (cmd->request_bufflen == 0)
1163 return 0;
1164
1165 mapping = pci_map_page(pdev,
1166 virt_to_page(cmd->request_buffer),
1167 ((unsigned long)cmd->request_buffer &
1168 ~PAGE_MASK),
1169 cmd->request_bufflen, dma_dir);
1170 __data_mapped(cmd) = 1;
1171 __data_mapping(cmd) = mapping;
1172
1173 return mapping;
1174 }
1175
1176 static int __map_scsi_sg_data(pcidev_t pdev, Scsi_Cmnd *cmd)
1177 {
1178 int use_sg;
1179 int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
1180
1181 if (cmd->use_sg == 0)
1182 return 0;
1183
1184 use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
1185 __data_mapped(cmd) = 2;
1186 __data_mapping(cmd) = use_sg;
1187
1188 return use_sg;
1189 }
1190
1191 static void __sync_scsi_data(pcidev_t pdev, Scsi_Cmnd *cmd)
1192 {
1193 int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
1194
1195 switch(__data_mapped(cmd)) {
1196 case 2:
1197 pci_dma_sync_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
1198 break;
1199 case 1:
1200 pci_dma_sync_single(pdev, __data_mapping(cmd),
1201 cmd->request_bufflen, dma_dir);
1202 break;
1203 }
1204 }
1205
1206 #define scsi_sg_dma_address(sc) sg_dma_address(sc)
1207 #define scsi_sg_dma_len(sc) sg_dma_len(sc)
1208
1209 #endif /* SCSI_NCR_DYNAMIC_DMA_MAPPING */
1210
1211 #define unmap_scsi_data(np, cmd) __unmap_scsi_data(np->pdev, cmd)
1212 #define map_scsi_single_data(np, cmd) __map_scsi_single_data(np->pdev, cmd)
1213 #define map_scsi_sg_data(np, cmd) __map_scsi_sg_data(np->pdev, cmd)
1214 #define sync_scsi_data(np, cmd) __sync_scsi_data(np->pdev, cmd)
1215
1216
1217 /*
1218 * Print out some buffer.
1219 */
1220 static void ncr_print_hex(u_char *p, int n)
1221 {
1222 while (n-- > 0)
1223 printk (" %x", *p++);
1224 }
1225
1226 static void ncr_printl_hex(char *label, u_char *p, int n)
1227 {
1228 printk("%s", label);
1229 ncr_print_hex(p, n);
1230 printk (".\n");
1231 }
1232
1233 /*
1234 ** Transfer direction
1235 **
1236 ** Until some linux kernel version near 2.3.40, low-level scsi
1237 ** drivers were not told about data transfer direction.
1238 ** We check the existence of this feature that has been expected
1239 ** for a _long_ time by all SCSI driver developers by just
1240 ** testing against the definition of SCSI_DATA_UNKNOWN. Indeed
1241 ** this is a hack, but testing against a kernel version would
1242 ** have been a shame. ;-)
1243 */
1244 #ifdef SCSI_DATA_UNKNOWN
1245
1246 #define scsi_data_direction(cmd) (cmd->sc_data_direction)
1247
1248 #else
1249
1250 #define SCSI_DATA_UNKNOWN 0
1251 #define SCSI_DATA_WRITE 1
1252 #define SCSI_DATA_READ 2
1253 #define SCSI_DATA_NONE 3
1254
1255 static __inline__ int scsi_data_direction(Scsi_Cmnd *cmd)
1256 {
1257 int direction;
1258
1259 switch((int) cmd->cmnd[0]) {
1260 case 0x08: /* READ(6) 08 */
1261 case 0x28: /* READ(10) 28 */
1262 case 0xA8: /* READ(12) A8 */
1263 direction = SCSI_DATA_READ;
1264 break;
1265 case 0x0A: /* WRITE(6) 0A */
1266 case 0x2A: /* WRITE(10) 2A */
1267 case 0xAA: /* WRITE(12) AA */
1268 direction = SCSI_DATA_WRITE;
1269 break;
1270 default:
1271 direction = SCSI_DATA_UNKNOWN;
1272 break;
1273 }
1274
1275 return direction;
1276 }
1277
1278 #endif /* SCSI_DATA_UNKNOWN */
1279
1280
1281 /*
1282 ** /proc directory entry and proc_info function
1283 */
1284 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,27)
1285 static struct proc_dir_entry proc_scsi_sym53c8xx = {
1286 PROC_SCSI_SYM53C8XX, 9, NAME53C8XX,
1287 S_IFDIR | S_IRUGO | S_IXUGO, 2
1288 };
1289 #endif
1290 #ifdef SCSI_NCR_PROC_INFO_SUPPORT
1291 static int sym53c8xx_proc_info(char *buffer, char **start, off_t offset,
1292 int length, int hostno, int func);
1293 #endif
1294
1295 /*
1296 ** Driver setup.
1297 **
1298 ** This structure is initialized from linux config options.
1299 ** It can be overridden at boot-up by the boot command line.
1300 */
1301 static struct ncr_driver_setup
1302 driver_setup = SCSI_NCR_DRIVER_SETUP;
1303
1304 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
1305 static struct ncr_driver_setup
1306 driver_safe_setup __initdata = SCSI_NCR_DRIVER_SAFE_SETUP;
1307 # ifdef MODULE
1308 char *sym53c8xx = 0; /* command line passed by insmod */
1309 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,30)
1310 MODULE_PARM(sym53c8xx, "s");
1311 # endif
1312 # endif
1313 #endif
1314
1315 /*
1316 ** Other Linux definitions
1317 */
1318 #define SetScsiResult(cmd, h_sts, s_sts) \
1319 cmd->result = (((h_sts) << 16) + ((s_sts) & 0x7f))
1320
1321 /* We may have to remind our amnesiac SCSI layer of the reason of the abort */
1322 #if 0
1323 #define SetScsiAbortResult(cmd) \
1324 SetScsiResult( \
1325 cmd, \
1326 (cmd)->abort_reason == DID_TIME_OUT ? DID_TIME_OUT : DID_ABORT, \
1327 0xff)
1328 #else
1329 #define SetScsiAbortResult(cmd) SetScsiResult(cmd, DID_ABORT, 0xff)
1330 #endif
1331
1332 static void sym53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs);
1333 static void sym53c8xx_timeout(unsigned long np);
1334
1335 #define initverbose (driver_setup.verbose)
1336 #define bootverbose (np->verbose)
1337
1338 #ifdef SCSI_NCR_NVRAM_SUPPORT
1339 static u_char Tekram_sync[16] __initdata =
1340 {25,31,37,43, 50,62,75,125, 12,15,18,21, 6,7,9,10};
1341 #endif /* SCSI_NCR_NVRAM_SUPPORT */
1342
1343 /*
1344 ** Structures used by sym53c8xx_detect/sym53c8xx_pci_init to
1345 ** transmit device configuration to the ncr_attach() function.
1346 */
1347 typedef struct {
1348 int bus;
1349 u_char device_fn;
1350 u_long base;
1351 u_long base_2;
1352 u_long io_port;
1353 u_long base_c;
1354 u_long base_2_c;
1355 int irq;
1356 /* port and reg fields to use INB, OUTB macros */
1357 u_long base_io;
1358 volatile struct ncr_reg *reg;
1359 } ncr_slot;
1360
1361 typedef struct {
1362 int type;
1363 #define SCSI_NCR_SYMBIOS_NVRAM (1)
1364 #define SCSI_NCR_TEKRAM_NVRAM (2)
1365 #ifdef SCSI_NCR_NVRAM_SUPPORT
1366 union {
1367 Symbios_nvram Symbios;
1368 Tekram_nvram Tekram;
1369 } data;
1370 #endif
1371 } ncr_nvram;
1372
1373 /*
1374 ** Structure used by sym53c8xx_detect/sym53c8xx_pci_init
1375 ** to save data on each detected board for ncr_attach().
1376 */
1377 typedef struct {
1378 pcidev_t pdev;
1379 ncr_slot slot;
1380 ncr_chip chip;
1381 ncr_nvram *nvram;
1382 u_char host_id;
1383 #ifdef SCSI_NCR_PQS_PDS_SUPPORT
1384 u_char pqs_pds;
1385 #endif
1386 int attach_done;
1387 } ncr_device;
1388
1389 /*==========================================================
1390 **
1391 ** assert ()
1392 **
1393 **==========================================================
1394 **
1395 ** modified copy from 386bsd:/usr/include/sys/assert.h
1396 **
1397 **----------------------------------------------------------
1398 */
1399
1400 #define assert(expression) { \
1401 if (!(expression)) { \
1402 (void)panic( \
1403 "assertion \"%s\" failed: file \"%s\", line %d\n", \
1404 #expression, \
1405 __FILE__, __LINE__); \
1406 } \
1407 }
1408
1409 /*==========================================================
1410 **
1411 ** Command control block states.
1412 **
1413 **==========================================================
1414 */
1415
1416 #define HS_IDLE (0)
1417 #define HS_BUSY (1)
1418 #define HS_NEGOTIATE (2) /* sync/wide data transfer*/
1419 #define HS_DISCONNECT (3) /* Disconnected by target */
1420
1421 #define HS_DONEMASK (0x80)
1422 #define HS_COMPLETE (4|HS_DONEMASK)
1423 #define HS_SEL_TIMEOUT (5|HS_DONEMASK) /* Selection timeout */
1424 #define HS_RESET (6|HS_DONEMASK) /* SCSI reset */
1425 #define HS_ABORTED (7|HS_DONEMASK) /* Transfer aborted */
1426 #define HS_TIMEOUT (8|HS_DONEMASK) /* Software timeout */
1427 #define HS_FAIL (9|HS_DONEMASK) /* SCSI or PCI bus errors */
1428 #define HS_UNEXPECTED (10|HS_DONEMASK)/* Unexpected disconnect */
1429
1430 #define DSA_INVALID 0xffffffff
1431
1432 /*==========================================================
1433 **
1434 ** Software Interrupt Codes
1435 **
1436 **==========================================================
1437 */
1438
1439 #define SIR_BAD_STATUS (1)
1440 #define SIR_SEL_ATN_NO_MSG_OUT (2)
1441 #define SIR_MSG_RECEIVED (3)
1442 #define SIR_MSG_WEIRD (4)
1443 #define SIR_NEGO_FAILED (5)
1444 #define SIR_NEGO_PROTO (6)
1445 #define SIR_SCRIPT_STOPPED (7)
1446 #define SIR_REJECT_TO_SEND (8)
1447 #define SIR_SWIDE_OVERRUN (9)
1448 #define SIR_SODL_UNDERRUN (10)
1449 #define SIR_RESEL_NO_MSG_IN (11)
1450 #define SIR_RESEL_NO_IDENTIFY (12)
1451 #define SIR_RESEL_BAD_LUN (13)
1452 #define SIR_TARGET_SELECTED (14)
1453 #define SIR_RESEL_BAD_I_T_L (15)
1454 #define SIR_RESEL_BAD_I_T_L_Q (16)
1455 #define SIR_ABORT_SENT (17)
1456 #define SIR_RESEL_ABORTED (18)
1457 #define SIR_MSG_OUT_DONE (19)
1458 #define SIR_AUTO_SENSE_DONE (20)
1459 #define SIR_DUMMY_INTERRUPT (21)
1460 #define SIR_DATA_OVERRUN (22)
1461 #define SIR_BAD_PHASE (23)
1462 #define SIR_MAX (23)
1463
1464 /*==========================================================
1465 **
1466 ** Extended error bits.
1467 ** xerr_status field of struct ccb.
1468 **
1469 **==========================================================
1470 */
1471
1472 #define XE_EXTRA_DATA (1) /* unexpected data phase */
1473 #define XE_BAD_PHASE (2) /* illegal phase (4/5) */
1474 #define XE_PARITY_ERR (4) /* unrecovered SCSI parity error */
1475 #define XE_SODL_UNRUN (1<<3)
1476 #define XE_SWIDE_OVRUN (1<<4)
1477
1478 /*==========================================================
1479 **
1480 ** Negotiation status.
1481 ** nego_status field of struct ccb.
1482 **
1483 **==========================================================
1484 */
1485
1486 #define NS_NOCHANGE (0)
1487 #define NS_SYNC (1)
1488 #define NS_WIDE (2)
1489 #define NS_PPR (4)
1490
1491 /*==========================================================
1492 **
1493 ** "Special features" of targets.
1494 ** quirks field of struct tcb.
1495 ** actualquirks field of struct ccb.
1496 **
1497 **==========================================================
1498 */
1499
1500 #define QUIRK_AUTOSAVE (0x01)
1501
1502 /*==========================================================
1503 **
1504 ** Capability bits in Inquire response byte 7.
1505 **
1506 **==========================================================
1507 */
1508
1509 #define INQ7_QUEUE (0x02)
1510 #define INQ7_SYNC (0x10)
1511 #define INQ7_WIDE16 (0x20)
1512
1513 /*==========================================================
1514 **
1515 ** A CCB hashed table is used to retrieve CCB address
1516 ** from DSA value.
1517 **
1518 **==========================================================
1519 */
1520
1521 #define CCB_HASH_SHIFT 8
1522 #define CCB_HASH_SIZE (1UL << CCB_HASH_SHIFT)
1523 #define CCB_HASH_MASK (CCB_HASH_SIZE-1)
1524 #define CCB_HASH_CODE(dsa) (((dsa) >> 11) & CCB_HASH_MASK)
1525
1526 /*==========================================================
1527 **
1528 ** Declaration of structs.
1529 **
1530 **==========================================================
1531 */
1532
1533 struct tcb;
1534 struct lcb;
1535 struct ccb;
1536 struct ncb;
1537 struct script;
1538
1539 typedef struct ncb * ncb_p;
1540 typedef struct tcb * tcb_p;
1541 typedef struct lcb * lcb_p;
1542 typedef struct ccb * ccb_p;
1543
1544 struct link {
1545 ncrcmd l_cmd;
1546 ncrcmd l_paddr;
1547 };
1548
1549 struct usrcmd {
1550 u_long target;
1551 u_long lun;
1552 u_long data;
1553 u_long cmd;
1554 };
1555
1556 #define UC_SETSYNC 10
1557 #define UC_SETTAGS 11
1558 #define UC_SETDEBUG 12
1559 #define UC_SETORDER 13
1560 #define UC_SETWIDE 14
1561 #define UC_SETFLAG 15
1562 #define UC_SETVERBOSE 17
1563 #define UC_RESETDEV 18
1564 #define UC_CLEARDEV 19
1565
1566 #define UF_TRACE (0x01)
1567 #define UF_NODISC (0x02)
1568 #define UF_NOSCAN (0x04)
1569
1570 /*========================================================================
1571 **
1572 ** Declaration of structs: target control block
1573 **
1574 **========================================================================
1575 */
1576 struct tcb {
1577 /*----------------------------------------------------------------
1578 ** LUN tables.
1579 ** An array of bus addresses is used on reselection by
1580 ** the SCRIPT.
1581 **----------------------------------------------------------------
1582 */
1583 u_int32 *luntbl; /* lcbs bus address table */
1584 u_int32 b_luntbl; /* bus address of this table */
1585 u_int32 b_lun0; /* bus address of lun0 */
1586 lcb_p l0p; /* lcb of LUN #0 (normal case) */
1587 #if MAX_LUN > 1
1588 lcb_p *lmp; /* Other lcb's [1..MAX_LUN] */
1589 #endif
1590 /*----------------------------------------------------------------
1591 ** Target capabilities.
1592 **----------------------------------------------------------------
1593 */
1594 u_char inq_done; /* Target capabilities received */
1595 u_char inq_byte7; /* Contains these capabilities */
1596
1597 /*----------------------------------------------------------------
1598 ** Some flags.
1599 **----------------------------------------------------------------
1600 */
1601 u_char to_reset; /* This target is to be reset */
1602
1603 /*----------------------------------------------------------------
1604 ** Pointer to the ccb used for negotiation.
1605 ** Prevent from starting a negotiation for all queued commands
1606 ** when tagged command queuing is enabled.
1607 **----------------------------------------------------------------
1608 */
1609 ccb_p nego_cp;
1610
1611 /*----------------------------------------------------------------
1612 ** negotiation of wide and synch transfer and device quirks.
1613 ** sval, wval and uval are read from SCRIPTS and so have alignment
1614 ** constraints.
1615 **----------------------------------------------------------------
1616 */
1617 /*0*/ u_char uval;
1618 /*1*/ u_char sval;
1619 /*2*/ u_char filler2;
1620 /*3*/ u_char wval;
1621 u_short period;
1622 u_char minsync;
1623 u_char maxoffs;
1624 u_char quirks;
1625 u_char widedone;
1626
1627 #ifdef SCSI_NCR_INTEGRITY_CHECKING
1628 u_char ic_min_sync;
1629 u_char ic_max_width;
1630 u_char ic_done;
1631 #endif
1632 u_char ic_maximums_set;
1633 u_char ppr_negotiation;
1634
1635 /*----------------------------------------------------------------
1636 ** User settable limits and options.
1637 ** These limits are read from the NVRAM if present.
1638 **----------------------------------------------------------------
1639 */
1640 u_char usrsync;
1641 u_char usrwide;
1642 u_short usrtags;
1643 u_char usrflag;
1644 };
1645
1646 /*========================================================================
1647 **
1648 ** Declaration of structs: lun control block
1649 **
1650 **========================================================================
1651 */
1652 struct lcb {
1653 /*----------------------------------------------------------------
1654 ** On reselection, SCRIPTS use this value as a JUMP address
1655 ** after the IDENTIFY has been successfully received.
1656 ** This field is set to 'resel_tag' if TCQ is enabled and
1657 ** to 'resel_notag' if TCQ is disabled.
1658 ** (Must be at zero due to bad lun handling on reselection)
1659 **----------------------------------------------------------------
1660 */
1661 /*0*/ u_int32 resel_task;
1662
1663 /*----------------------------------------------------------------
1664 ** Task table used by the script processor to retrieve the
1665 ** task corresponding to a reselected nexus. The TAG is used
1666 ** as offset to determine the corresponding entry.
1667 ** Each entry contains the associated CCB bus address.
1668 **----------------------------------------------------------------
1669 */
1670 u_int32 tasktbl_0; /* Used if TCQ not enabled */
1671 u_int32 *tasktbl;
1672 u_int32 b_tasktbl;
1673
1674 /*----------------------------------------------------------------
1675 ** CCB queue management.
1676 **----------------------------------------------------------------
1677 */
1678 XPT_QUEHEAD busy_ccbq; /* Queue of busy CCBs */
1679 XPT_QUEHEAD wait_ccbq; /* Queue of waiting for IO CCBs */
1680 u_short busyccbs; /* CCBs busy for this lun */
1681 u_short queuedccbs; /* CCBs queued to the controller*/
1682 u_short queuedepth; /* Queue depth for this lun */
1683 u_short scdev_depth; /* SCSI device queue depth */
1684 u_short maxnxs; /* Max possible nexuses */
1685
1686 /*----------------------------------------------------------------
1687 ** Control of tagged command queuing.
1688 ** Tags allocation is performed using a circular buffer.
1689 ** This avoids using a loop for tag allocation.
1690 **----------------------------------------------------------------
1691 */
1692 u_short ia_tag; /* Tag allocation index */
1693 u_short if_tag; /* Tag release index */
1694 u_char *cb_tags; /* Circular tags buffer */
1695 u_char inq_byte7; /* Store unit CmdQ capability */
1696 u_char usetags; /* Command queuing is active */
1697 u_char to_clear; /* User wants to clear all tasks*/
1698 u_short maxtags; /* Max NR of tags asked by user */
1699 u_short numtags; /* Current number of tags */
1700
1701 /*----------------------------------------------------------------
1702 ** QUEUE FULL and ORDERED tag control.
1703 **----------------------------------------------------------------
1704 */
1705 u_short num_good; /* Nr of GOOD since QUEUE FULL */
1706 u_short tags_sum[2]; /* Tags sum counters */
1707 u_char tags_si; /* Current index to tags sum */
1708 u_long tags_stime; /* Last time we switch tags_sum */
1709 };
1710
1711 /*========================================================================
1712 **
1713 ** Declaration of structs: actions for a task.
1714 **
1715 **========================================================================
1716 **
1717 ** It is part of the CCB and is called by the scripts processor to
1718 ** start or restart the data structure (nexus).
1719 **
1720 **------------------------------------------------------------------------
1721 */
1722 struct action {
1723 u_int32 start;
1724 u_int32 restart;
1725 };
1726
1727 /*========================================================================
1728 **
1729 ** Declaration of structs: Phase mismatch context.
1730 **
1731 **========================================================================
1732 **
1733 ** It is part of the CCB and is used as parameters for the DATA
1734 ** pointer. We need two contexts to handle correctly the SAVED
1735 ** DATA POINTER.
1736 **
1737 **------------------------------------------------------------------------
1738 */
1739 struct pm_ctx {
1740 struct scr_tblmove sg; /* Updated interrupted SG block */
1741 u_int32 ret; /* SCRIPT return address */
1742 };
1743
1744 /*========================================================================
1745 **
1746 ** Declaration of structs: global HEADER.
1747 **
1748 **========================================================================
1749 **
1750 ** In earlier driver versions, this substructure was copied from the
1751 ** ccb to a global address after selection (or reselection) and copied
1752 ** back before disconnect. Since we are now using LOAD/STORE DSA
1753 ** RELATIVE instructions, the script is able to access directly these
1754 ** fields, and so, this header is no more copied.
1755 **
1756 **------------------------------------------------------------------------
1757 */
1758
1759 struct head {
1760 /*----------------------------------------------------------------
1761 ** Start and restart SCRIPTS addresses (must be at 0).
1762 **----------------------------------------------------------------
1763 */
1764 struct action go;
1765
1766 /*----------------------------------------------------------------
1767 ** Saved data pointer.
1768 ** Points to the position in the script responsible for the
1769 ** actual transfer of data.
1770 ** It's written after reception of a SAVE_DATA_POINTER message.
1771 ** The goalpointer points after the last transfer command.
1772 **----------------------------------------------------------------
1773 */
1774 u_int32 savep;
1775 u_int32 lastp;
1776 u_int32 goalp;
1777
1778 /*----------------------------------------------------------------
1779 ** Alternate data pointer.
1780 ** They are copied back to savep/lastp/goalp by the SCRIPTS
1781 ** when the direction is unknown and the device claims data out.
1782 **----------------------------------------------------------------
1783 */
1784 u_int32 wlastp;
1785 u_int32 wgoalp;
1786
1787 /*----------------------------------------------------------------
1788 ** Status fields.
1789 **----------------------------------------------------------------
1790 */
1791 u_char status[4]; /* host status */
1792 };
1793
1794 /*
1795 ** LUN control block lookup.
1796 ** We use a direct pointer for LUN #0, and a table of pointers
1797 ** which is only allocated for devices that support LUN(s) > 0.
1798 */
1799 #if MAX_LUN <= 1
1800 #define ncr_lp(np, tp, lun) (!lun) ? (tp)->l0p : 0
1801 #else
1802 #define ncr_lp(np, tp, lun) \
1803 (!lun) ? (tp)->l0p : (tp)->lmp ? (tp)->lmp[(lun)] : 0
1804 #endif
1805
1806 /*
1807 ** The status bytes are used by the host and the script processor.
1808 **
1809 ** The four bytes (status[4]) are copied to the scratchb register
1810 ** (declared as scr0..scr3 in ncr_reg.h) just after the select/reselect,
1811 ** and copied back just after disconnecting.
1812 ** Inside the script the XX_REG are used.
1813 */
1814
1815 /*
1816 ** Last four bytes (script)
1817 */
1818 #define QU_REG scr0
1819 #define HS_REG scr1
1820 #define HS_PRT nc_scr1
1821 #define SS_REG scr2
1822 #define SS_PRT nc_scr2
1823 #define HF_REG scr3
1824 #define HF_PRT nc_scr3
1825
1826 /*
1827 ** Last four bytes (host)
1828 */
1829 #define actualquirks phys.header.status[0]
1830 #define host_status phys.header.status[1]
1831 #define scsi_status phys.header.status[2]
1832 #define host_flags phys.header.status[3]
1833
1834 /*
1835 ** Host flags
1836 */
1837 #define HF_IN_PM0 1u
1838 #define HF_IN_PM1 (1u<<1)
1839 #define HF_ACT_PM (1u<<2)
1840 #define HF_DP_SAVED (1u<<3)
1841 #define HF_AUTO_SENSE (1u<<4)
1842 #define HF_DATA_IN (1u<<5)
1843 #define HF_PM_TO_C (1u<<6)
1844 #define HF_EXT_ERR (1u<<7)
1845
1846 #ifdef SCSI_NCR_IARB_SUPPORT
1847 #define HF_HINT_IARB (1u<<7)
1848 #endif
1849
1850 /*
1851 ** This one is stolen from QU_REG.:)
1852 */
1853 #define HF_DATA_ST (1u<<7)
1854
1855 /*==========================================================
1856 **
1857 ** Declaration of structs: Data structure block
1858 **
1859 **==========================================================
1860 **
1861 ** During execution of a ccb by the script processor,
1862 ** the DSA (data structure address) register points
1863 ** to this substructure of the ccb.
1864 ** This substructure contains the header with
1865 ** the script-processor-changable data and
1866 ** data blocks for the indirect move commands.
1867 **
1868 **----------------------------------------------------------
1869 */
1870
1871 struct dsb {
1872
1873 /*
1874 ** Header.
1875 */
1876
1877 struct head header;
1878
1879 /*
1880 ** Table data for Script
1881 */
1882
1883 struct scr_tblsel select;
1884 struct scr_tblmove smsg ;
1885 struct scr_tblmove smsg_ext ;
1886 struct scr_tblmove cmd ;
1887 struct scr_tblmove sense ;
1888 struct scr_tblmove wresid;
1889 struct scr_tblmove data [MAX_SCATTER];
1890
1891 /*
1892 ** Phase mismatch contexts.
1893 ** We need two to handle correctly the
1894 ** SAVED DATA POINTER.
1895 */
1896
1897 struct pm_ctx pm0;
1898 struct pm_ctx pm1;
1899 };
1900
1901
1902 /*========================================================================
1903 **
1904 ** Declaration of structs: Command control block.
1905 **
1906 **========================================================================
1907 */
1908 struct ccb {
1909 /*----------------------------------------------------------------
1910 ** This is the data structure which is pointed by the DSA
1911 ** register when it is executed by the script processor.
1912 ** It must be the first entry.
1913 **----------------------------------------------------------------
1914 */
1915 struct dsb phys;
1916
1917 /*----------------------------------------------------------------
1918 ** The general SCSI driver provides a
1919 ** pointer to a control block.
1920 **----------------------------------------------------------------
1921 */
1922 Scsi_Cmnd *cmd; /* SCSI command */
1923 u_char cdb_buf[16]; /* Copy of CDB */
1924 u_char sense_buf[64];
1925 int data_len; /* Total data length */
1926 int segments; /* Number of SG segments */
1927
1928 /*----------------------------------------------------------------
1929 ** Message areas.
1930 ** We prepare a message to be sent after selection.
1931 ** We may use a second one if the command is rescheduled
1932 ** due to CHECK_CONDITION or QUEUE FULL status.
1933 ** Contents are IDENTIFY and SIMPLE_TAG.
1934 ** While negotiating sync or wide transfer,
1935 ** a SDTR or WDTR message is appended.
1936 **----------------------------------------------------------------
1937 */
1938 u_char scsi_smsg [12];
1939 u_char scsi_smsg2[12];
1940
1941 /*----------------------------------------------------------------
1942 ** Miscellaneous status'.
1943 **----------------------------------------------------------------
1944 */
1945 u_char nego_status; /* Negotiation status */
1946 u_char xerr_status; /* Extended error flags */
1947 u_int32 extra_bytes; /* Extraneous bytes transferred */
1948
1949 /*----------------------------------------------------------------
1950 ** Saved info for auto-sense
1951 **----------------------------------------------------------------
1952 */
1953 u_char sv_scsi_status;
1954 u_char sv_xerr_status;
1955
1956 /*----------------------------------------------------------------
1957 ** Other fields.
1958 **----------------------------------------------------------------
1959 */
1960 u_long p_ccb; /* BUS address of this CCB */
1961 u_char sensecmd[6]; /* Sense command */
1962 u_char to_abort; /* This CCB is to be aborted */
1963 u_short tag; /* Tag for this transfer */
1964 /* NO_TAG means no tag */
1965 u_char tags_si; /* Lun tags sum index (0,1) */
1966
1967 u_char target;
1968 u_char lun;
1969 u_short queued;
1970 ccb_p link_ccb; /* Host adapter CCB chain */
1971 ccb_p link_ccbh; /* Host adapter CCB hash chain */
1972 XPT_QUEHEAD link_ccbq; /* Link to unit CCB queue */
1973 u_int32 startp; /* Initial data pointer */
1974 u_int32 lastp0; /* Initial 'lastp' */
1975 int ext_sg; /* Extreme data pointer, used */
1976 int ext_ofs; /* to calculate the residual. */
1977 int resid;
1978 };
1979
1980 #define CCB_PHYS(cp,lbl) (cp->p_ccb + offsetof(struct ccb, lbl))
1981
1982
1983 /*========================================================================
1984 **
1985 ** Declaration of structs: NCR device descriptor
1986 **
1987 **========================================================================
1988 */
1989 struct ncb {
1990 /*----------------------------------------------------------------
1991 ** Idle task and invalid task actions and their bus
1992 ** addresses.
1993 **----------------------------------------------------------------
1994 */
1995 struct action idletask;
1996 struct action notask;
1997 struct action bad_i_t_l;
1998 struct action bad_i_t_l_q;
1999 u_long p_idletask;
2000 u_long p_notask;
2001 u_long p_bad_i_t_l;
2002 u_long p_bad_i_t_l_q;
2003
2004 /*----------------------------------------------------------------
2005 ** Dummy lun table to protect us against target returning bad
2006 ** lun number on reselection.
2007 **----------------------------------------------------------------
2008 */
2009 u_int32 *badluntbl; /* Table physical address */
2010 u_int32 resel_badlun; /* SCRIPT handler BUS address */
2011
2012 /*----------------------------------------------------------------
2013 ** Bit 32-63 of the on-chip RAM bus address in LE format.
2014 ** The START_RAM64 script loads the MMRS and MMWS from this
2015 ** field.
2016 **----------------------------------------------------------------
2017 */
2018 u_int32 scr_ram_seg;
2019
2020 /*----------------------------------------------------------------
2021 ** CCBs management queues.
2022 **----------------------------------------------------------------
2023 */
2024 Scsi_Cmnd *waiting_list; /* Commands waiting for a CCB */
2025 /* when lcb is not allocated. */
2026 Scsi_Cmnd *done_list; /* Commands waiting for done() */
2027 /* callback to be invoked. */
2028 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,93)
2029 spinlock_t smp_lock; /* Lock for SMP threading */
2030 #endif
2031
2032 /*----------------------------------------------------------------
2033 ** Chip and controller indentification.
2034 **----------------------------------------------------------------
2035 */
2036 int unit; /* Unit number */
2037 char chip_name[8]; /* Chip name */
2038 char inst_name[16]; /* ncb instance name */
2039
2040 /*----------------------------------------------------------------
2041 ** Initial value of some IO register bits.
2042 ** These values are assumed to have been set by BIOS, and may
2043 ** be used for probing adapter implementation differences.
2044 **----------------------------------------------------------------
2045 */
2046 u_char sv_scntl0, sv_scntl3, sv_dmode, sv_dcntl, sv_ctest3, sv_ctest4,
2047 sv_ctest5, sv_gpcntl, sv_stest2, sv_stest4, sv_stest1, sv_scntl4;
2048
2049 /*----------------------------------------------------------------
2050 ** Actual initial value of IO register bits used by the
2051 ** driver. They are loaded at initialisation according to
2052 ** features that are to be enabled.
2053 **----------------------------------------------------------------
2054 */
2055 u_char rv_scntl0, rv_scntl3, rv_dmode, rv_dcntl, rv_ctest3, rv_ctest4,
2056 rv_ctest5, rv_stest2, rv_ccntl0, rv_ccntl1, rv_scntl4;
2057
2058 /*----------------------------------------------------------------
2059 ** Target data.
2060 ** Target control block bus address array used by the SCRIPT
2061 ** on reselection.
2062 **----------------------------------------------------------------
2063 */
2064 struct tcb target[MAX_TARGET];
2065 u_int32 *targtbl;
2066
2067 /*----------------------------------------------------------------
2068 ** Virtual and physical bus addresses of the chip.
2069 **----------------------------------------------------------------
2070 */
2071 #ifndef SCSI_NCR_PCI_MEM_NOT_SUPPORTED
2072 u_long base_va; /* MMIO base virtual address */
2073 u_long base2_va; /* On-chip RAM virtual address */
2074 #endif
2075 u_long base_ba; /* MMIO base bus address */
2076 u_long base_io; /* IO space base address */
2077 u_long base_ws; /* (MM)IO window size */
2078 u_long base2_ba; /* On-chip RAM bus address */
2079 u_long base2_ws; /* On-chip RAM window size */
2080 u_int irq; /* IRQ number */
2081 volatile /* Pointer to volatile for */
2082 struct ncr_reg *reg; /* memory mapped IO. */
2083
2084 /*----------------------------------------------------------------
2085 ** SCRIPTS virtual and physical bus addresses.
2086 ** 'script' is loaded in the on-chip RAM if present.
2087 ** 'scripth' stays in main memory for all chips except the
2088 ** 53C895A and 53C896 that provide 8K on-chip RAM.
2089 **----------------------------------------------------------------
2090 */
2091 struct script *script0; /* Copies of script and scripth */
2092 struct scripth *scripth0; /* relocated for this ncb. */
2093 u_long p_script; /* Actual script and scripth */
2094 u_long p_scripth; /* bus addresses. */
2095 u_long p_scripth0;
2096
2097 /*----------------------------------------------------------------
2098 ** General controller parameters and configuration.
2099 **----------------------------------------------------------------
2100 */
2101 pcidev_t pdev;
2102 u_short device_id; /* PCI device id */
2103 u_char revision_id; /* PCI device revision id */
2104 u_char bus; /* PCI BUS number */
2105 u_char device_fn; /* PCI BUS device and function */
2106 u_char myaddr; /* SCSI id of the adapter */
2107 u_char maxburst; /* log base 2 of dwords burst */
2108 u_char maxwide; /* Maximum transfer width */
2109 u_char minsync; /* Minimum sync period factor */
2110 u_char maxsync; /* Maximum sync period factor */
2111 u_char maxoffs; /* Max scsi offset */
2112 u_char maxoffs_st; /* Max scsi offset in ST mode */
2113 u_char multiplier; /* Clock multiplier (1,2,4) */
2114 u_char clock_divn; /* Number of clock divisors */
2115 u_long clock_khz; /* SCSI clock frequency in KHz */
2116 u_int features; /* Chip features map */
2117
2118 /*----------------------------------------------------------------
2119 ** Range for the PCI clock frequency measurement result
2120 ** that ensures the algorithm used by the driver can be
2121 ** trusted for the SCSI clock frequency measurement.
2122 ** (Assuming a PCI clock frequency of 33 MHz).
2123 **----------------------------------------------------------------
2124 */
2125 u_int pciclock_min;
2126 u_int pciclock_max;
2127
2128 /*----------------------------------------------------------------
2129 ** Start queue management.
2130 ** It is filled up by the host processor and accessed by the
2131 ** SCRIPTS processor in order to start SCSI commands.
2132 **----------------------------------------------------------------
2133 */
2134 u_long p_squeue; /* Start queue BUS address */
2135 u_int32 *squeue; /* Start queue virtual address */
2136 u_short squeueput; /* Next free slot of the queue */
2137 u_short actccbs; /* Number of allocated CCBs */
2138 u_short queuedepth; /* Start queue depth */
2139
2140 /*----------------------------------------------------------------
2141 ** Command completion queue.
2142 ** It is the same size as the start queue to avoid overflow.
2143 **----------------------------------------------------------------
2144 */
2145 u_short dqueueget; /* Next position to scan */
2146 u_int32 *dqueue; /* Completion (done) queue */
2147
2148 /*----------------------------------------------------------------
2149 ** Timeout handler.
2150 **----------------------------------------------------------------
2151 */
2152 struct timer_list timer; /* Timer handler link header */
2153 u_long lasttime;
2154 u_long settle_time; /* Resetting the SCSI BUS */
2155
2156 /*----------------------------------------------------------------
2157 ** Debugging and profiling.
2158 **----------------------------------------------------------------
2159 */
2160 struct ncr_reg regdump; /* Register dump */
2161 u_long regtime; /* Time it has been done */
2162
2163 /*----------------------------------------------------------------
2164 ** Miscellaneous buffers accessed by the scripts-processor.
2165 ** They shall be DWORD aligned, because they may be read or
2166 ** written with a script command.
2167 **----------------------------------------------------------------
2168 */
2169 u_char msgout[12]; /* Buffer for MESSAGE OUT */
2170 u_char msgin [12]; /* Buffer for MESSAGE IN */
2171 u_int32 lastmsg; /* Last SCSI message sent */
2172 u_char scratch; /* Scratch for SCSI receive */
2173
2174 /*----------------------------------------------------------------
2175 ** Miscellaneous configuration and status parameters.
2176 **----------------------------------------------------------------
2177 */
2178 u_char scsi_mode; /* Current SCSI BUS mode */
2179 u_char order; /* Tag order to use */
2180 u_char verbose; /* Verbosity for this controller*/
2181 u_int32 ncr_cache; /* Used for cache test at init. */
2182 u_long p_ncb; /* BUS address of this NCB */
2183
2184 /*----------------------------------------------------------------
2185 ** CCB lists and queue.
2186 **----------------------------------------------------------------
2187 */
2188 ccb_p ccbh[CCB_HASH_SIZE]; /* CCB hashed by DSA value */
2189 struct ccb *ccbc; /* CCB chain */
2190 XPT_QUEHEAD free_ccbq; /* Queue of available CCBs */
2191
2192 /*----------------------------------------------------------------
2193 ** IMMEDIATE ARBITRATION (IARB) control.
2194 ** We keep track in 'last_cp' of the last CCB that has been
2195 ** queued to the SCRIPTS processor and clear 'last_cp' when
2196 ** this CCB completes. If last_cp is not zero at the moment
2197 ** we queue a new CCB, we set a flag in 'last_cp' that is
2198 ** used by the SCRIPTS as a hint for setting IARB.
2199 ** We donnot set more than 'iarb_max' consecutive hints for
2200 ** IARB in order to leave devices a chance to reselect.
2201 ** By the way, any non zero value of 'iarb_max' is unfair. :)
2202 **----------------------------------------------------------------
2203 */
2204 #ifdef SCSI_NCR_IARB_SUPPORT
2205 struct ccb *last_cp; /* Last queud CCB used for IARB */
2206 u_short iarb_max; /* Max. # consecutive IARB hints*/
2207 u_short iarb_count; /* Actual # of these hints */
2208 #endif
2209
2210 /*----------------------------------------------------------------
2211 ** We need the LCB in order to handle disconnections and
2212 ** to count active CCBs for task management. So, we use
2213 ** a unique CCB for LUNs we donnot have the LCB yet.
2214 ** This queue normally should have at most 1 element.
2215 **----------------------------------------------------------------
2216 */
2217 XPT_QUEHEAD b0_ccbq;
2218
2219 /*----------------------------------------------------------------
2220 ** We use a different scatter function for 896 rev 1.
2221 **----------------------------------------------------------------
2222 */
2223 int (*scatter) (ncb_p, ccb_p, Scsi_Cmnd *);
2224
2225 /*----------------------------------------------------------------
2226 ** Command abort handling.
2227 ** We need to synchronize tightly with the SCRIPTS
2228 ** processor in order to handle things correctly.
2229 **----------------------------------------------------------------
2230 */
2231 u_char abrt_msg[4]; /* Message to send buffer */
2232 struct scr_tblmove abrt_tbl; /* Table for the MOV of it */
2233 struct scr_tblsel abrt_sel; /* Sync params for selection */
2234 u_char istat_sem; /* Tells the chip to stop (SEM) */
2235
2236 /*----------------------------------------------------------------
2237 ** Fields that should be removed or changed.
2238 **----------------------------------------------------------------
2239 */
2240 struct usrcmd user; /* Command from user */
2241 volatile u_char release_stage; /* Synchronisation stage on release */
2242
2243 /*----------------------------------------------------------------
2244 ** Fields that are used (primarily) for integrity check
2245 **----------------------------------------------------------------
2246 */
2247 unsigned char check_integrity; /* Enable midlayer integ. check on
2248 * bus scan. */
2249 #ifdef SCSI_NCR_INTEGRITY_CHECKING
2250 unsigned char check_integ_par; /* Set if par or Init. Det. error
2251 * used only during integ check */
2252 #endif
2253 };
2254
2255 #define NCB_PHYS(np, lbl) (np->p_ncb + offsetof(struct ncb, lbl))
2256 #define NCB_SCRIPT_PHYS(np,lbl) (np->p_script + offsetof (struct script, lbl))
2257 #define NCB_SCRIPTH_PHYS(np,lbl) (np->p_scripth + offsetof (struct scripth,lbl))
2258 #define NCB_SCRIPTH0_PHYS(np,lbl) (np->p_scripth0+offsetof (struct scripth,lbl))
2259
2260 /*==========================================================
2261 **
2262 **
2263 ** Script for NCR-Processor.
2264 **
2265 ** Use ncr_script_fill() to create the variable parts.
2266 ** Use ncr_script_copy_and_bind() to make a copy and
2267 ** bind to physical addresses.
2268 **
2269 **
2270 **==========================================================
2271 **
2272 ** We have to know the offsets of all labels before
2273 ** we reach them (for forward jumps).
2274 ** Therefore we declare a struct here.
2275 ** If you make changes inside the script,
2276 ** DONT FORGET TO CHANGE THE LENGTHS HERE!
2277 **
2278 **----------------------------------------------------------
2279 */
2280
2281 /*
2282 ** Script fragments which are loaded into the on-chip RAM
2283 ** of 825A, 875, 876, 895, 895A and 896 chips.
2284 */
2285 struct script {
2286 ncrcmd start [ 14];
2287 ncrcmd getjob_begin [ 4];
2288 ncrcmd getjob_end [ 4];
2289 ncrcmd select [ 8];
2290 ncrcmd wf_sel_done [ 2];
2291 ncrcmd send_ident [ 2];
2292 #ifdef SCSI_NCR_IARB_SUPPORT
2293 ncrcmd select2 [ 8];
2294 #else
2295 ncrcmd select2 [ 2];
2296 #endif
2297 ncrcmd command [ 2];
2298 ncrcmd dispatch [ 28];
2299 ncrcmd sel_no_cmd [ 10];
2300 ncrcmd init [ 6];
2301 ncrcmd clrack [ 4];
2302 ncrcmd disp_status [ 4];
2303 ncrcmd datai_done [ 26];
2304 ncrcmd datao_done [ 12];
2305 ncrcmd ign_i_w_r_msg [ 4];
2306 ncrcmd datai_phase [ 2];
2307 ncrcmd datao_phase [ 4];
2308 ncrcmd msg_in [ 2];
2309 ncrcmd msg_in2 [ 10];
2310 #ifdef SCSI_NCR_IARB_SUPPORT
2311 ncrcmd status [ 14];
2312 #else
2313 ncrcmd status [ 10];
2314 #endif
2315 ncrcmd complete [ 8];
2316 #ifdef SCSI_NCR_PCIQ_MAY_REORDER_WRITES
2317 ncrcmd complete2 [ 12];
2318 #else
2319 ncrcmd complete2 [ 10];
2320 #endif
2321 #ifdef SCSI_NCR_PCIQ_SYNC_ON_INTR
2322 ncrcmd done [ 18];
2323 #else
2324 ncrcmd done [ 14];
2325 #endif
2326 ncrcmd done_end [ 2];
2327 ncrcmd save_dp [ 8];
2328 ncrcmd restore_dp [ 4];
2329 ncrcmd disconnect [ 20];
2330 #ifdef SCSI_NCR_IARB_SUPPORT
2331 ncrcmd idle [ 4];
2332 #else
2333 ncrcmd idle [ 2];
2334 #endif
2335 #ifdef SCSI_NCR_IARB_SUPPORT
2336 ncrcmd ungetjob [ 6];
2337 #else
2338 ncrcmd ungetjob [ 4];
2339 #endif
2340 ncrcmd reselect [ 4];
2341 ncrcmd reselected [ 20];
2342 ncrcmd resel_scntl4 [ 30];
2343 #if MAX_TASKS*4 > 512
2344 ncrcmd resel_tag [ 18];
2345 #elif MAX_TASKS*4 > 256
2346 ncrcmd resel_tag [ 12];
2347 #else
2348 ncrcmd resel_tag [ 8];
2349 #endif
2350 ncrcmd resel_go [ 6];
2351 ncrcmd resel_notag [ 2];
2352 ncrcmd resel_dsa [ 8];
2353 ncrcmd data_in [MAX_SCATTER * SCR_SG_SIZE];
2354 ncrcmd data_in2 [ 4];
2355 ncrcmd data_out [MAX_SCATTER * SCR_SG_SIZE];
2356 ncrcmd data_out2 [ 4];
2357 ncrcmd pm0_data [ 12];
2358 ncrcmd pm0_data_out [ 6];
2359 ncrcmd pm0_data_end [ 6];
2360 ncrcmd pm1_data [ 12];
2361 ncrcmd pm1_data_out [ 6];
2362 ncrcmd pm1_data_end [ 6];
2363 };
2364
2365 /*
2366 ** Script fragments which stay in main memory for all chips
2367 ** except for the 895A and 896 that support 8K on-chip RAM.
2368 */
2369 struct scripth {
2370 ncrcmd start64 [ 2];
2371 ncrcmd no_data [ 2];
2372 ncrcmd sel_for_abort [ 18];
2373 ncrcmd sel_for_abort_1 [ 2];
2374 ncrcmd select_no_atn [ 8];
2375 ncrcmd wf_sel_done_no_atn [ 4];
2376
2377 ncrcmd msg_in_etc [ 14];
2378 ncrcmd msg_received [ 4];
2379 ncrcmd msg_weird_seen [ 4];
2380 ncrcmd msg_extended [ 20];
2381 ncrcmd msg_bad [ 6];
2382 ncrcmd msg_weird [ 4];
2383 ncrcmd msg_weird1 [ 8];
2384
2385 ncrcmd wdtr_resp [ 6];
2386 ncrcmd send_wdtr [ 4];
2387 ncrcmd sdtr_resp [ 6];
2388 ncrcmd send_sdtr [ 4];
2389 ncrcmd ppr_resp [ 6];
2390 ncrcmd send_ppr [ 4];
2391 ncrcmd nego_bad_phase [ 4];
2392 ncrcmd msg_out [ 4];
2393 ncrcmd msg_out_done [ 4];
2394 ncrcmd data_ovrun [ 2];
2395 ncrcmd data_ovrun1 [ 22];
2396 ncrcmd data_ovrun2 [ 8];
2397 ncrcmd abort_resel [ 16];
2398 ncrcmd resend_ident [ 4];
2399 ncrcmd ident_break [ 4];
2400 ncrcmd ident_break_atn [ 4];
2401 ncrcmd sdata_in [ 6];
2402 ncrcmd data_io [ 2];
2403 ncrcmd data_io_com [ 8];
2404 ncrcmd data_io_out [ 12];
2405 ncrcmd resel_bad_lun [ 4];
2406 ncrcmd bad_i_t_l [ 4];
2407 ncrcmd bad_i_t_l_q [ 4];
2408 ncrcmd bad_status [ 6];
2409 ncrcmd tweak_pmj [ 12];
2410 ncrcmd pm_handle [ 20];
2411 ncrcmd pm_handle1 [ 4];
2412 ncrcmd pm_save [ 4];
2413 ncrcmd pm0_save [ 14];
2414 ncrcmd pm1_save [ 14];
2415
2416 /* WSR handling */
2417 #ifdef SYM_DEBUG_PM_WITH_WSR
2418 ncrcmd pm_wsr_handle [ 44];
2419 #else
2420 ncrcmd pm_wsr_handle [ 42];
2421 #endif
2422 ncrcmd wsr_ma_helper [ 4];
2423
2424 /* Data area */
2425 ncrcmd zero [ 1];
2426 ncrcmd scratch [ 1];
2427 ncrcmd scratch1 [ 1];
2428 ncrcmd pm0_data_addr [ 1];
2429 ncrcmd pm1_data_addr [ 1];
2430 ncrcmd saved_dsa [ 1];
2431 ncrcmd saved_drs [ 1];
2432 ncrcmd done_pos [ 1];
2433 ncrcmd startpos [ 1];
2434 ncrcmd targtbl [ 1];
2435 /* End of data area */
2436
2437 #ifdef SCSI_NCR_PCI_MEM_NOT_SUPPORTED
2438 ncrcmd start_ram [ 1];
2439 ncrcmd script0_ba [ 4];
2440 ncrcmd start_ram64 [ 3];
2441 ncrcmd script0_ba64 [ 3];
2442 ncrcmd scripth0_ba64 [ 6];
2443 ncrcmd ram_seg64 [ 1];
2444 #endif
2445 ncrcmd snooptest [ 6];
2446 ncrcmd snoopend [ 2];
2447 };
2448
2449 /*==========================================================
2450 **
2451 **
2452 ** Function headers.
2453 **
2454 **
2455 **==========================================================
2456 */
2457
2458 static ccb_p ncr_alloc_ccb (ncb_p np);
2459 static void ncr_complete (ncb_p np, ccb_p cp);
2460 static void ncr_exception (ncb_p np);
2461 static void ncr_free_ccb (ncb_p np, ccb_p cp);
2462 static ccb_p ncr_ccb_from_dsa(ncb_p np, u_long dsa);
2463 static void ncr_init_tcb (ncb_p np, u_char tn);
2464 static lcb_p ncr_alloc_lcb (ncb_p np, u_char tn, u_char ln);
2465 static lcb_p ncr_setup_lcb (ncb_p np, u_char tn, u_char ln,
2466 u_char *inq_data);
2467 static void ncr_getclock (ncb_p np, int mult);
2468 static u_int ncr_getpciclock (ncb_p np);
2469 static void ncr_selectclock (ncb_p np, u_char scntl3);
2470 static ccb_p ncr_get_ccb (ncb_p np, u_char tn, u_char ln);
2471 static void ncr_init (ncb_p np, int reset, char * msg, u_long code);
2472 static void ncr_int_sbmc (ncb_p np);
2473 static void ncr_int_par (ncb_p np, u_short sist);
2474 static void ncr_int_ma (ncb_p np);
2475 static void ncr_int_sir (ncb_p np);
2476 static void ncr_int_sto (ncb_p np);
2477 static void ncr_int_udc (ncb_p np);
2478 static void ncr_negotiate (ncb_p np, tcb_p tp);
2479 static int ncr_prepare_nego(ncb_p np, ccb_p cp, u_char *msgptr);
2480 #ifdef SCSI_NCR_INTEGRITY_CHECKING
2481 static int ncr_ic_nego(ncb_p np, ccb_p cp, Scsi_Cmnd *cmd, u_char *msgptr);
2482 #endif
2483 static void ncr_script_copy_and_bind
2484 (ncb_p np, ncrcmd *src, ncrcmd *dst, int len);
2485 static void ncr_script_fill (struct script * scr, struct scripth * scripth);
2486 static int ncr_scatter_896R1 (ncb_p np, ccb_p cp, Scsi_Cmnd *cmd);
2487 static int ncr_scatter (ncb_p np, ccb_p cp, Scsi_Cmnd *cmd);
2488 static void ncr_getsync (ncb_p np, u_char sfac, u_char *fakp, u_char *scntl3p);
2489 static void ncr_get_xfer_info(ncb_p np, tcb_p tp, u_char *factor, u_char *offset, u_char *width);
2490 static void ncr_setsync (ncb_p np, ccb_p cp, u_char scntl3, u_char sxfer, u_char scntl4);
2491 static void ncr_set_sync_wide_status (ncb_p np, u_char target);
2492 static void ncr_setup_tags (ncb_p np, u_char tn, u_char ln);
2493 static void ncr_setwide (ncb_p np, ccb_p cp, u_char wide, u_char ack);
2494 static void ncr_setsyncwide (ncb_p np, ccb_p cp, u_char scntl3, u_char sxfer, u_char scntl4, u_char wide);
2495 static int ncr_show_msg (u_char * msg);
2496 static void ncr_print_msg (ccb_p cp, char *label, u_char * msg);
2497 static int ncr_snooptest (ncb_p np);
2498 static void ncr_timeout (ncb_p np);
2499 static void ncr_wakeup (ncb_p np, u_long code);
2500 static int ncr_wakeup_done (ncb_p np);
2501 static void ncr_start_next_ccb (ncb_p np, lcb_p lp, int maxn);
2502 static void ncr_put_start_queue(ncb_p np, ccb_p cp);
2503 static void ncr_chip_reset (ncb_p np);
2504 static void ncr_soft_reset (ncb_p np);
2505 static void ncr_start_reset (ncb_p np);
2506 static int ncr_reset_scsi_bus (ncb_p np, int enab_int, int settle_delay);
2507 static int ncr_compute_residual (ncb_p np, ccb_p cp);
2508
2509 #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
2510 static void ncr_usercmd (ncb_p np);
2511 #endif
2512
2513 static int ncr_attach (Scsi_Host_Template *tpnt, int unit, ncr_device *device);
2514 static void ncr_free_resources(ncb_p np);
2515
2516 static void insert_into_waiting_list(ncb_p np, Scsi_Cmnd *cmd);
2517 static Scsi_Cmnd *retrieve_from_waiting_list(int to_remove, ncb_p np, Scsi_Cmnd *cmd);
2518 static void process_waiting_list(ncb_p np, int sts);
2519
2520 #define remove_from_waiting_list(np, cmd) \
2521 retrieve_from_waiting_list(1, (np), (cmd))
2522 #define requeue_waiting_list(np) process_waiting_list((np), DID_OK)
2523 #define reset_waiting_list(np) process_waiting_list((np), DID_RESET)
2524
2525 #ifdef SCSI_NCR_NVRAM_SUPPORT
2526 static void ncr_get_nvram (ncr_device *devp, ncr_nvram *nvp);
2527 static int sym_read_Tekram_nvram (ncr_slot *np, u_short device_id,
2528 Tekram_nvram *nvram);
2529 static int sym_read_Symbios_nvram (ncr_slot *np, Symbios_nvram *nvram);
2530 #endif
2531
2532 /*==========================================================
2533 **
2534 **
2535 ** Global static data.
2536 **
2537 **
2538 **==========================================================
2539 */
2540
2541 static inline char *ncr_name (ncb_p np)
2542 {
2543 return np->inst_name;
2544 }
2545
2546
2547 /*==========================================================
2548 **
2549 **
2550 ** Scripts for NCR-Processor.
2551 **
2552 ** Use ncr_script_bind for binding to physical addresses.
2553 **
2554 **
2555 **==========================================================
2556 **
2557 ** NADDR generates a reference to a field of the controller data.
2558 ** PADDR generates a reference to another part of the script.
2559 ** RADDR generates a reference to a script processor register.
2560 ** FADDR generates a reference to a script processor register
2561 ** with offset.
2562 **
2563 **----------------------------------------------------------
2564 */
2565
2566 #define RELOC_SOFTC 0x40000000
2567 #define RELOC_LABEL 0x50000000
2568 #define RELOC_REGISTER 0x60000000
2569 #if 0
2570 #define RELOC_KVAR 0x70000000
2571 #endif
2572 #define RELOC_LABELH 0x80000000
2573 #define RELOC_MASK 0xf0000000
2574
2575 #define NADDR(label) (RELOC_SOFTC | offsetof(struct ncb, label))
2576 #define PADDR(label) (RELOC_LABEL | offsetof(struct script, label))
2577 #define PADDRH(label) (RELOC_LABELH | offsetof(struct scripth, label))
2578 #define RADDR(label) (RELOC_REGISTER | REG(label))
2579 #define FADDR(label,ofs)(RELOC_REGISTER | ((REG(label))+(ofs)))
2580 #define KVAR(which) (RELOC_KVAR | (which))
2581
2582 #define SCR_DATA_ZERO 0xf00ff00f
2583
2584 #ifdef RELOC_KVAR
2585 #define SCRIPT_KVAR_JIFFIES (0)
2586 #define SCRIPT_KVAR_FIRST SCRIPT_KVAR_JIFFIES
2587 #define SCRIPT_KVAR_LAST SCRIPT_KVAR_JIFFIES
2588 /*
2589 * Kernel variables referenced in the scripts.
2590 * THESE MUST ALL BE ALIGNED TO A 4-BYTE BOUNDARY.
2591 */
2592 static void *script_kvars[] __initdata =
2593 { (void *)&jiffies };
2594 #endif
2595
2596 static struct script script0 __initdata = {
2597 /*--------------------------< START >-----------------------*/ {
2598 /*
2599 ** This NOP will be patched with LED ON
2600 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
2601 */
2602 SCR_NO_OP,
2603 0,
2604 /*
2605 ** Clear SIGP.
2606 */
2607 SCR_FROM_REG (ctest2),
2608 0,
2609
2610 /*
2611 ** Stop here if the C code wants to perform
2612 ** some error recovery procedure manually.
2613 ** (Indicate this by setting SEM in ISTAT)
2614 */
2615 SCR_FROM_REG (istat),
2616 0,
2617 /*
2618 ** Report to the C code the next position in
2619 ** the start queue the SCRIPTS will schedule.
2620 ** The C code must not change SCRATCHA.
2621 */
2622 SCR_LOAD_ABS (scratcha, 4),
2623 PADDRH (startpos),
2624 SCR_INT ^ IFTRUE (MASK (SEM, SEM)),
2625 SIR_SCRIPT_STOPPED,
2626
2627 /*
2628 ** Start the next job.
2629 **
2630 ** @DSA = start point for this job.
2631 ** SCRATCHA = address of this job in the start queue.
2632 **
2633 ** We will restore startpos with SCRATCHA if we fails the
2634 ** arbitration or if it is the idle job.
2635 **
2636 ** The below GETJOB_BEGIN to GETJOB_END section of SCRIPTS
2637 ** is a critical path. If it is partially executed, it then
2638 ** may happen that the job address is not yet in the DSA
2639 ** and the next queue position points to the next JOB.
2640 */
2641 SCR_LOAD_ABS (dsa, 4),
2642 PADDRH (startpos),
2643 SCR_LOAD_REL (temp, 4),
2644 4,
2645 }/*-------------------------< GETJOB_BEGIN >------------------*/,{
2646 SCR_STORE_ABS (temp, 4),
2647 PADDRH (startpos),
2648 SCR_LOAD_REL (dsa, 4),
2649 0,
2650 }/*-------------------------< GETJOB_END >--------------------*/,{
2651 SCR_LOAD_REL (temp, 4),
2652 0,
2653 SCR_RETURN,
2654 0,
2655
2656 }/*-------------------------< SELECT >----------------------*/,{
2657 /*
2658 ** DSA contains the address of a scheduled
2659 ** data structure.
2660 **
2661 ** SCRATCHA contains the address of the start queue
2662 ** entry which points to the next job.
2663 **
2664 ** Set Initiator mode.
2665 **
2666 ** (Target mode is left as an exercise for the reader)
2667 */
2668
2669 SCR_CLR (SCR_TRG),
2670 0,
2671 /*
2672 ** And try to select this target.
2673 */
2674 SCR_SEL_TBL_ATN ^ offsetof (struct dsb, select),
2675 PADDR (ungetjob),
2676 /*
2677 ** Now there are 4 possibilities:
2678 **
2679 ** (1) The ncr loses arbitration.
2680 ** This is ok, because it will try again,
2681 ** when the bus becomes idle.
2682 ** (But beware of the timeout function!)
2683 **
2684 ** (2) The ncr is reselected.
2685 ** Then the script processor takes the jump
2686 ** to the RESELECT label.
2687 **
2688 ** (3) The ncr wins arbitration.
2689 ** Then it will execute SCRIPTS instruction until
2690 ** the next instruction that checks SCSI phase.
2691 ** Then will stop and wait for selection to be
2692 ** complete or selection time-out to occur.
2693 **
2694 ** After having won arbitration, the ncr SCRIPTS
2695 ** processor is able to execute instructions while
2696 ** the SCSI core is performing SCSI selection. But
2697 ** some script instruction that is not waiting for
2698 ** a valid phase (or selection timeout) to occur
2699 ** breaks the selection procedure, by probably
2700 ** affecting timing requirements.
2701 ** So we have to wait immediately for the next phase
2702 ** or the selection to complete or time-out.
2703 */
2704
2705 /*
2706 ** load the savep (saved pointer) into
2707 ** the actual data pointer.
2708 */
2709 SCR_LOAD_REL (temp, 4),
2710 offsetof (struct ccb, phys.header.savep),
2711 /*
2712 ** Initialize the status registers
2713 */
2714 SCR_LOAD_REL (scr0, 4),
2715 offsetof (struct ccb, phys.header.status),
2716
2717 }/*-------------------------< WF_SEL_DONE >----------------------*/,{
2718 SCR_INT ^ IFFALSE (WHEN (SCR_MSG_OUT)),
2719 SIR_SEL_ATN_NO_MSG_OUT,
2720 }/*-------------------------< SEND_IDENT >----------------------*/,{
2721 /*
2722 ** Selection complete.
2723 ** Send the IDENTIFY and SIMPLE_TAG messages
2724 ** (and the M_X_SYNC_REQ / M_X_WIDE_REQ message)
2725 */
2726 SCR_MOVE_TBL ^ SCR_MSG_OUT,
2727 offsetof (struct dsb, smsg),
2728 }/*-------------------------< SELECT2 >----------------------*/,{
2729 #ifdef SCSI_NCR_IARB_SUPPORT
2730 /*
2731 ** Set IMMEDIATE ARBITRATION if we have been given
2732 ** a hint to do so. (Some job to do after this one).
2733 */
2734 SCR_FROM_REG (HF_REG),
2735 0,
2736 SCR_JUMPR ^ IFFALSE (MASK (HF_HINT_IARB, HF_HINT_IARB)),
2737 8,
2738 SCR_REG_REG (scntl1, SCR_OR, IARB),
2739 0,
2740 #endif
2741 /*
2742 ** Anticipate the COMMAND phase.
2743 ** This is the PHASE we expect at this point.
2744 */
2745 SCR_JUMP ^ IFFALSE (WHEN (SCR_COMMAND)),
2746 PADDR (sel_no_cmd),
2747
2748 }/*-------------------------< COMMAND >--------------------*/,{
2749 /*
2750 ** ... and send the command
2751 */
2752 SCR_MOVE_TBL ^ SCR_COMMAND,
2753 offsetof (struct dsb, cmd),
2754
2755 }/*-----------------------< DISPATCH >----------------------*/,{
2756 /*
2757 ** MSG_IN is the only phase that shall be
2758 ** entered at least once for each (re)selection.
2759 ** So we test it first.
2760 */
2761 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN)),
2762 PADDR (msg_in),
2763 SCR_JUMP ^ IFTRUE (IF (SCR_DATA_OUT)),
2764 PADDR (datao_phase),
2765 SCR_JUMP ^ IFTRUE (IF (SCR_DATA_IN)),
2766 PADDR (datai_phase),
2767 SCR_JUMP ^ IFTRUE (IF (SCR_STATUS)),
2768 PADDR (status),
2769 SCR_JUMP ^ IFTRUE (IF (SCR_COMMAND)),
2770 PADDR (command),
2771 SCR_JUMP ^ IFTRUE (IF (SCR_MSG_OUT)),
2772 PADDRH (msg_out),
2773 /*
2774 * Discard as many illegal phases as
2775 * required and tell the C code about.
2776 */
2777 SCR_JUMPR ^ IFFALSE (WHEN (SCR_ILG_OUT)),
2778 16,
2779 SCR_MOVE_ABS (1) ^ SCR_ILG_OUT,
2780 NADDR (scratch),
2781 SCR_JUMPR ^ IFTRUE (WHEN (SCR_ILG_OUT)),
2782 -16,
2783 SCR_JUMPR ^ IFFALSE (WHEN (SCR_ILG_IN)),
2784 16,
2785 SCR_MOVE_ABS (1) ^ SCR_ILG_IN,
2786 NADDR (scratch),
2787 SCR_JUMPR ^ IFTRUE (WHEN (SCR_ILG_IN)),
2788 -16,
2789 SCR_INT,
2790 SIR_BAD_PHASE,
2791 SCR_JUMP,
2792 PADDR (dispatch),
2793 }/*---------------------< SEL_NO_CMD >----------------------*/,{
2794 /*
2795 ** The target does not switch to command
2796 ** phase after IDENTIFY has been sent.
2797 **
2798 ** If it stays in MSG OUT phase send it
2799 ** the IDENTIFY again.
2800 */
2801 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
2802 PADDRH (resend_ident),
2803 /*
2804 ** If target does not switch to MSG IN phase
2805 ** and we sent a negotiation, assert the
2806 ** failure immediately.
2807 */
2808 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN)),
2809 PADDR (dispatch),
2810 SCR_FROM_REG (HS_REG),
2811 0,
2812 SCR_INT ^ IFTRUE (DATA (HS_NEGOTIATE)),
2813 SIR_NEGO_FAILED,
2814 /*
2815 ** Jump to dispatcher.
2816 */
2817 SCR_JUMP,
2818 PADDR (dispatch),
2819
2820 }/*-------------------------< INIT >------------------------*/,{
2821 /*
2822 ** Wait for the SCSI RESET signal to be
2823 ** inactive before restarting operations,
2824 ** since the chip may hang on SEL_ATN
2825 ** if SCSI RESET is active.
2826 */
2827 SCR_FROM_REG (sstat0),
2828 0,
2829 SCR_JUMPR ^ IFTRUE (MASK (IRST, IRST)),
2830 -16,
2831 SCR_JUMP,
2832 PADDR (start),
2833 }/*-------------------------< CLRACK >----------------------*/,{
2834 /*
2835 ** Terminate possible pending message phase.
2836 */
2837 SCR_CLR (SCR_ACK),
2838 0,
2839 SCR_JUMP,
2840 PADDR (dispatch),
2841
2842 }/*-------------------------< DISP_STATUS >----------------------*/,{
2843 /*
2844 ** Anticipate STATUS phase.
2845 **
2846 ** Does spare 3 SCRIPTS instructions when we have
2847 ** completed the INPUT of the data.
2848 */
2849 SCR_JUMP ^ IFTRUE (WHEN (SCR_STATUS)),
2850 PADDR (status),
2851 SCR_JUMP,
2852 PADDR (dispatch),
2853
2854 }/*-------------------------< DATAI_DONE >-------------------*/,{
2855 /*
2856 * If the device wants us to send more data,
2857 * we must count the extra bytes.
2858 */
2859 SCR_JUMP ^ IFTRUE (WHEN (SCR_DATA_IN)),
2860 PADDRH (data_ovrun),
2861 /*
2862 ** If the SWIDE is not full, jump to dispatcher.
2863 ** We anticipate a STATUS phase.
2864 ** If we get later an IGNORE WIDE RESIDUE, we
2865 ** will alias it as a MODIFY DP (-1).
2866 */
2867 SCR_FROM_REG (scntl2),
2868 0,
2869 SCR_JUMP ^ IFFALSE (MASK (WSR, WSR)),
2870 PADDR (disp_status),
2871 /*
2872 ** The SWIDE is full.
2873 ** Clear this condition.
2874 */
2875 SCR_REG_REG (scntl2, SCR_OR, WSR),
2876 0,
2877 /*
2878 * We are expecting an IGNORE RESIDUE message
2879 * from the device, otherwise we are in data
2880 * overrun condition. Check against MSG_IN phase.
2881 */
2882 SCR_INT ^ IFFALSE (WHEN (SCR_MSG_IN)),
2883 SIR_SWIDE_OVERRUN,
2884 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
2885 PADDR (disp_status),
2886 /*
2887 * We are in MSG_IN phase,
2888 * Read the first byte of the message.
2889 * If it is not an IGNORE RESIDUE message,
2890 * signal overrun and jump to message
2891 * processing.
2892 */
2893 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2894 NADDR (msgin[0]),
2895 SCR_INT ^ IFFALSE (DATA (M_IGN_RESIDUE)),
2896 SIR_SWIDE_OVERRUN,
2897 SCR_JUMP ^ IFFALSE (DATA (M_IGN_RESIDUE)),
2898 PADDR (msg_in2),
2899
2900 /*
2901 * We got the message we expected.
2902 * Read the 2nd byte, and jump to dispatcher.
2903 */
2904 SCR_CLR (SCR_ACK),
2905 0,
2906 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2907 NADDR (msgin[1]),
2908 SCR_CLR (SCR_ACK),
2909 0,
2910 SCR_JUMP,
2911 PADDR (disp_status),
2912
2913 }/*-------------------------< DATAO_DONE >-------------------*/,{
2914 /*
2915 * If the device wants us to send more data,
2916 * we must count the extra bytes.
2917 */
2918 SCR_JUMP ^ IFTRUE (WHEN (SCR_DATA_OUT)),
2919 PADDRH (data_ovrun),
2920 /*
2921 ** If the SODL is not full jump to dispatcher.
2922 ** We anticipate a MSG IN phase or a STATUS phase.
2923 */
2924 SCR_FROM_REG (scntl2),
2925 0,
2926 SCR_JUMP ^ IFFALSE (MASK (WSS, WSS)),
2927 PADDR (disp_status),
2928 /*
2929 ** The SODL is full, clear this condition.
2930 */
2931 SCR_REG_REG (scntl2, SCR_OR, WSS),
2932 0,
2933 /*
2934 ** And signal a DATA UNDERRUN condition
2935 ** to the C code.
2936 */
2937 SCR_INT,
2938 SIR_SODL_UNDERRUN,
2939 SCR_JUMP,
2940 PADDR (dispatch),
2941
2942 }/*-------------------------< IGN_I_W_R_MSG >--------------*/,{
2943 /*
2944 ** We jump here from the phase mismatch interrupt,
2945 ** When we have a SWIDE and the device has presented
2946 ** a IGNORE WIDE RESIDUE message on the BUS.
2947 ** We just have to throw away this message and then
2948 ** to jump to dispatcher.
2949 */
2950 SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
2951 NADDR (scratch),
2952 /*
2953 ** Clear ACK and jump to dispatcher.
2954 */
2955 SCR_JUMP,
2956 PADDR (clrack),
2957
2958 }/*-------------------------< DATAI_PHASE >------------------*/,{
2959 SCR_RETURN,
2960 0,
2961 }/*-------------------------< DATAO_PHASE >------------------*/,{
2962 /*
2963 ** Patch for 53c1010_66 only - to allow A0 part
2964 ** to operate properly in a 33MHz PCI bus.
2965 **
2966 ** SCR_REG_REG(scntl4, SCR_OR, 0x0c),
2967 ** 0,
2968 */
2969 SCR_NO_OP,
2970 0,
2971 SCR_RETURN,
2972 0,
2973 }/*-------------------------< MSG_IN >--------------------*/,{
2974 /*
2975 ** Get the first byte of the message.
2976 **
2977 ** The script processor doesn't negate the
2978 ** ACK signal after this transfer.
2979 */
2980 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
2981 NADDR (msgin[0]),
2982 }/*-------------------------< MSG_IN2 >--------------------*/,{
2983 /*
2984 ** Check first against 1 byte messages
2985 ** that we handle from SCRIPTS.
2986 */
2987 SCR_JUMP ^ IFTRUE (DATA (M_COMPLETE)),
2988 PADDR (complete),
2989 SCR_JUMP ^ IFTRUE (DATA (M_DISCONNECT)),
2990 PADDR (disconnect),
2991 SCR_JUMP ^ IFTRUE (DATA (M_SAVE_DP)),
2992 PADDR (save_dp),
2993 SCR_JUMP ^ IFTRUE (DATA (M_RESTORE_DP)),
2994 PADDR (restore_dp),
2995 /*
2996 ** We handle all other messages from the
2997 ** C code, so no need to waste on-chip RAM
2998 ** for those ones.
2999 */
3000 SCR_JUMP,
3001 PADDRH (msg_in_etc),
3002
3003 }/*-------------------------< STATUS >--------------------*/,{
3004 /*
3005 ** get the status
3006 */
3007 SCR_MOVE_ABS (1) ^ SCR_STATUS,
3008 NADDR (scratch),
3009 #ifdef SCSI_NCR_IARB_SUPPORT
3010 /*
3011 ** If STATUS is not GOOD, clear IMMEDIATE ARBITRATION,
3012 ** since we may have to tamper the start queue from
3013 ** the C code.
3014 */
3015 SCR_JUMPR ^ IFTRUE (DATA (S_GOOD)),
3016 8,
3017 SCR_REG_REG (scntl1, SCR_AND, ~IARB),
3018 0,
3019 #endif
3020 /*
3021 ** save status to scsi_status.
3022 ** mark as complete.
3023 */
3024 SCR_TO_REG (SS_REG),
3025 0,
3026 SCR_LOAD_REG (HS_REG, HS_COMPLETE),
3027 0,
3028 /*
3029 ** Anticipate the MESSAGE PHASE for
3030 ** the TASK COMPLETE message.
3031 */
3032 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_IN)),
3033 PADDR (msg_in),
3034 SCR_JUMP,
3035 PADDR (dispatch),
3036
3037 }/*-------------------------< COMPLETE >-----------------*/,{
3038 /*
3039 ** Complete message.
3040 **
3041 ** Copy the data pointer to LASTP in header.
3042 */
3043 SCR_STORE_REL (temp, 4),
3044 offsetof (struct ccb, phys.header.lastp),
3045 /*
3046 ** When we terminate the cycle by clearing ACK,
3047 ** the target may disconnect immediately.
3048 **
3049 ** We don't want to be told of an
3050 ** "unexpected disconnect",
3051 ** so we disable this feature.
3052 */
3053 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
3054 0,
3055 /*
3056 ** Terminate cycle ...
3057 */
3058 SCR_CLR (SCR_ACK|SCR_ATN),
3059 0,
3060 /*
3061 ** ... and wait for the disconnect.
3062 */
3063 SCR_WAIT_DISC,
3064 0,
3065 }/*-------------------------< COMPLETE2 >-----------------*/,{
3066 /*
3067 ** Save host status to header.
3068 */
3069 SCR_STORE_REL (scr0, 4),
3070 offsetof (struct ccb, phys.header.status),
3071
3072 #ifdef SCSI_NCR_PCIQ_MAY_REORDER_WRITES
3073 /*
3074 ** Some bridges may reorder DMA writes to memory.
3075 ** We donnot want the CPU to deal with completions
3076 ** without all the posted write having been flushed
3077 ** to memory. This DUMMY READ should flush posted
3078 ** buffers prior to the CPU having to deal with
3079 ** completions.
3080 */
3081 SCR_LOAD_REL (scr0, 4), /* DUMMY READ */
3082 offsetof (struct ccb, phys.header.status),
3083 #endif
3084 /*
3085 ** If command resulted in not GOOD status,
3086 ** call the C code if needed.
3087 */
3088 SCR_FROM_REG (SS_REG),
3089 0,
3090 SCR_CALL ^ IFFALSE (DATA (S_GOOD)),
3091 PADDRH (bad_status),
3092
3093 /*
3094 ** If we performed an auto-sense, call
3095 ** the C code to synchronyze task aborts
3096 ** with UNIT ATTENTION conditions.
3097 */
3098 SCR_FROM_REG (HF_REG),
3099 0,
3100 SCR_INT ^ IFTRUE (MASK (HF_AUTO_SENSE, HF_AUTO_SENSE)),
3101 SIR_AUTO_SENSE_DONE,
3102
3103 }/*------------------------< DONE >-----------------*/,{
3104 #ifdef SCSI_NCR_PCIQ_SYNC_ON_INTR
3105 /*
3106 ** It seems that some bridges flush everything
3107 ** when the INTR line is raised. For these ones,
3108 ** we can just ensure that the INTR line will be
3109 ** raised before each completion. So, if it happens
3110 ** that we have been faster that the CPU, we just
3111 ** have to synchronize with it. A dummy programmed
3112 ** interrupt will do the trick.
3113 ** Note that we overlap at most 1 IO with the CPU
3114 ** in this situation and that the IRQ line must not
3115 ** be shared.
3116 */
3117 SCR_FROM_REG (istat),
3118 0,
3119 SCR_INT ^ IFTRUE (MASK (INTF, INTF)),
3120 SIR_DUMMY_INTERRUPT,
3121 #endif
3122 /*
3123 ** Copy the DSA to the DONE QUEUE and
3124 ** signal completion to the host.
3125 ** If we are interrupted between DONE
3126 ** and DONE_END, we must reset, otherwise
3127 ** the completed CCB will be lost.
3128 */
3129 SCR_STORE_ABS (dsa, 4),
3130 PADDRH (saved_dsa),
3131 SCR_LOAD_ABS (dsa, 4),
3132 PADDRH (done_pos),
3133 SCR_LOAD_ABS (scratcha, 4),
3134 PADDRH (saved_dsa),
3135 SCR_STORE_REL (scratcha, 4),
3136 0,
3137 /*
3138 ** The instruction below reads the DONE QUEUE next
3139 ** free position from memory.
3140 ** In addition it ensures that all PCI posted writes
3141 ** are flushed and so the DSA value of the done
3142 ** CCB is visible by the CPU before INTFLY is raised.
3143 */
3144 SCR_LOAD_REL (temp, 4),
3145 4,
3146 SCR_INT_FLY,
3147 0,
3148 SCR_STORE_ABS (temp, 4),
3149 PADDRH (done_pos),
3150 }/*------------------------< DONE_END >-----------------*/,{
3151 SCR_JUMP,
3152 PADDR (start),
3153
3154 }/*-------------------------< SAVE_DP >------------------*/,{
3155 /*
3156 ** Clear ACK immediately.
3157 ** No need to delay it.
3158 */
3159 SCR_CLR (SCR_ACK),
3160 0,
3161 /*
3162 ** Keep track we received a SAVE DP, so
3163 ** we will switch to the other PM context
3164 ** on the next PM since the DP may point
3165 ** to the current PM context.
3166 */
3167 SCR_REG_REG (HF_REG, SCR_OR, HF_DP_SAVED),
3168 0,
3169 /*
3170 ** SAVE_DP message:
3171 ** Copy the data pointer to SAVEP in header.
3172 */
3173 SCR_STORE_REL (temp, 4),
3174 offsetof (struct ccb, phys.header.savep),
3175 SCR_JUMP,
3176 PADDR (dispatch),
3177 }/*-------------------------< RESTORE_DP >---------------*/,{
3178 /*
3179 ** RESTORE_DP message:
3180 ** Copy SAVEP in header to actual data pointer.
3181 */
3182 SCR_LOAD_REL (temp, 4),
3183 offsetof (struct ccb, phys.header.savep),
3184 SCR_JUMP,
3185 PADDR (clrack),
3186
3187 }/*-------------------------< DISCONNECT >---------------*/,{
3188 /*
3189 ** DISCONNECTing ...
3190 **
3191 ** disable the "unexpected disconnect" feature,
3192 ** and remove the ACK signal.
3193 */
3194 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
3195 0,
3196 SCR_CLR (SCR_ACK|SCR_ATN),
3197 0,
3198 /*
3199 ** Wait for the disconnect.
3200 */
3201 SCR_WAIT_DISC,
3202 0,
3203 /*
3204 ** Status is: DISCONNECTED.
3205 */
3206 SCR_LOAD_REG (HS_REG, HS_DISCONNECT),
3207 0,
3208 /*
3209 ** Save host status to header.
3210 */
3211 SCR_STORE_REL (scr0, 4),
3212 offsetof (struct ccb, phys.header.status),
3213 /*
3214 ** If QUIRK_AUTOSAVE is set,
3215 ** do an "save pointer" operation.
3216 */
3217 SCR_FROM_REG (QU_REG),
3218 0,
3219 SCR_JUMP ^ IFFALSE (MASK (QUIRK_AUTOSAVE, QUIRK_AUTOSAVE)),
3220 PADDR (start),
3221 /*
3222 ** like SAVE_DP message:
3223 ** Remember we saved the data pointer.
3224 ** Copy data pointer to SAVEP in header.
3225 */
3226 SCR_REG_REG (HF_REG, SCR_OR, HF_DP_SAVED),
3227 0,
3228 SCR_STORE_REL (temp, 4),
3229 offsetof (struct ccb, phys.header.savep),
3230 SCR_JUMP,
3231 PADDR (start),
3232
3233 }/*-------------------------< IDLE >------------------------*/,{
3234 /*
3235 ** Nothing to do?
3236 ** Wait for reselect.
3237 ** This NOP will be patched with LED OFF
3238 ** SCR_REG_REG (gpreg, SCR_OR, 0x01)
3239 */
3240 SCR_NO_OP,
3241 0,
3242 #ifdef SCSI_NCR_IARB_SUPPORT
3243 SCR_JUMPR,
3244 8,
3245 #endif
3246 }/*-------------------------< UNGETJOB >-----------------*/,{
3247 #ifdef SCSI_NCR_IARB_SUPPORT
3248 /*
3249 ** Set IMMEDIATE ARBITRATION, for the next time.
3250 ** This will give us better chance to win arbitration
3251 ** for the job we just wanted to do.
3252 */
3253 SCR_REG_REG (scntl1, SCR_OR, IARB),
3254 0,
3255 #endif
3256 /*
3257 ** We are not able to restart the SCRIPTS if we are
3258 ** interrupted and these instruction haven't been
3259 ** all executed. BTW, this is very unlikely to
3260 ** happen, but we check that from the C code.
3261 */
3262 SCR_LOAD_REG (dsa, 0xff),
3263 0,
3264 SCR_STORE_ABS (scratcha, 4),
3265 PADDRH (startpos),
3266 }/*-------------------------< RESELECT >--------------------*/,{
3267 /*
3268 ** make the host status invalid.
3269 */
3270 SCR_CLR (SCR_TRG),
3271 0,
3272 /*
3273 ** Sleep waiting for a reselection.
3274 ** If SIGP is set, special treatment.
3275 **
3276 ** Zu allem bereit ..
3277 */
3278 SCR_WAIT_RESEL,
3279 PADDR(start),
3280 }/*-------------------------< RESELECTED >------------------*/,{
3281 /*
3282 ** This NOP will be patched with LED ON
3283 ** SCR_REG_REG (gpreg, SCR_AND, 0xfe)
3284 */
3285 SCR_NO_OP,
3286 0,
3287 /*
3288 ** load the target id into the sdid
3289 */
3290 SCR_REG_SFBR (ssid, SCR_AND, 0x8F),
3291 0,
3292 SCR_TO_REG (sdid),
3293 0,
3294 /*
3295 ** load the target control block address
3296 */
3297 SCR_LOAD_ABS (dsa, 4),
3298 PADDRH (targtbl),
3299 SCR_SFBR_REG (dsa, SCR_SHL, 0),
3300 0,
3301 SCR_REG_REG (dsa, SCR_SHL, 0),
3302 0,
3303 SCR_REG_REG (dsa, SCR_AND, 0x3c),
3304 0,
3305 SCR_LOAD_REL (dsa, 4),
3306 0,
3307 /*
3308 ** Load the synchronous transfer registers.
3309 */
3310 SCR_LOAD_REL (scntl3, 1),
3311 offsetof(struct tcb, wval),
3312 SCR_LOAD_REL (sxfer, 1),
3313 offsetof(struct tcb, sval),
3314 }/*-------------------------< RESEL_SCNTL4 >------------------*/,{
3315 /*
3316 ** Write with uval value. Patch if device
3317 ** does not support Ultra3.
3318 **
3319 ** SCR_LOAD_REL (scntl4, 1),
3320 ** offsetof(struct tcb, uval),
3321 */
3322
3323 SCR_NO_OP,
3324 0,
3325 /*
3326 * We expect MESSAGE IN phase.
3327 * If not, get help from the C code.
3328 */
3329 SCR_INT ^ IFFALSE (WHEN (SCR_MSG_IN)),
3330 SIR_RESEL_NO_MSG_IN,
3331 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3332 NADDR (msgin),
3333
3334 /*
3335 * If IDENTIFY LUN #0, use a faster path
3336 * to find the LCB structure.
3337 */
3338 SCR_JUMPR ^ IFTRUE (MASK (0x80, 0xbf)),
3339 56,
3340 /*
3341 * If message isn't an IDENTIFY,
3342 * tell the C code about.
3343 */
3344 SCR_INT ^ IFFALSE (MASK (0x80, 0x80)),
3345 SIR_RESEL_NO_IDENTIFY,
3346 /*
3347 * It is an IDENTIFY message,
3348 * Load the LUN control block address.
3349 */
3350 SCR_LOAD_REL (dsa, 4),
3351 offsetof(struct tcb, b_luntbl),
3352 SCR_SFBR_REG (dsa, SCR_SHL, 0),
3353 0,
3354 SCR_REG_REG (dsa, SCR_SHL, 0),
3355 0,
3356 SCR_REG_REG (dsa, SCR_AND, 0xfc),
3357 0,
3358 SCR_LOAD_REL (dsa, 4),
3359 0,
3360 SCR_JUMPR,
3361 8,
3362 /*
3363 ** LUN 0 special case (but usual one :))
3364 */
3365 SCR_LOAD_REL (dsa, 4),
3366 offsetof(struct tcb, b_lun0),
3367
3368 /*
3369 ** Load the reselect task action for this LUN.
3370 ** Load the tasks DSA array for this LUN.
3371 ** Call the action.
3372 */
3373 SCR_LOAD_REL (temp, 4),
3374 offsetof(struct lcb, resel_task),
3375 SCR_LOAD_REL (dsa, 4),
3376 offsetof(struct lcb, b_tasktbl),
3377 SCR_RETURN,
3378 0,
3379 }/*-------------------------< RESEL_TAG >-------------------*/,{
3380 /*
3381 ** ACK the IDENTIFY or TAG previously received
3382 */
3383
3384 SCR_CLR (SCR_ACK),
3385 0,
3386 /*
3387 ** Read IDENTIFY + SIMPLE + TAG using a single MOVE.
3388 ** Agressive optimization, is'nt it?
3389 ** No need to test the SIMPLE TAG message, since the
3390 ** driver only supports conformant devices for tags. ;-)
3391 */
3392 SCR_MOVE_ABS (2) ^ SCR_MSG_IN,
3393 NADDR (msgin),
3394 /*
3395 ** Read the TAG from the SIDL.
3396 ** Still an aggressive optimization. ;-)
3397 ** Compute the CCB indirect jump address which
3398 ** is (#TAG*2 & 0xfc) due to tag numbering using
3399 ** 1,3,5..MAXTAGS*2+1 actual values.
3400 */
3401 SCR_REG_SFBR (sidl, SCR_SHL, 0),
3402 0,
3403 #if MAX_TASKS*4 > 512
3404 SCR_JUMPR ^ IFFALSE (CARRYSET),
3405 8,
3406 SCR_REG_REG (dsa1, SCR_OR, 2),
3407 0,
3408 SCR_REG_REG (sfbr, SCR_SHL, 0),
3409 0,
3410 SCR_JUMPR ^ IFFALSE (CARRYSET),
3411 8,
3412 SCR_REG_REG (dsa1, SCR_OR, 1),
3413 0,
3414 #elif MAX_TASKS*4 > 256
3415 SCR_JUMPR ^ IFFALSE (CARRYSET),
3416 8,
3417 SCR_REG_REG (dsa1, SCR_OR, 1),
3418 0,
3419 #endif
3420 /*
3421 ** Retrieve the DSA of this task.
3422 ** JUMP indirectly to the restart point of the CCB.
3423 */
3424 SCR_SFBR_REG (dsa, SCR_AND, 0xfc),
3425 0,
3426 }/*-------------------------< RESEL_GO >-------------------*/,{
3427 SCR_LOAD_REL (dsa, 4),
3428 0,
3429 SCR_LOAD_REL (temp, 4),
3430 offsetof(struct ccb, phys.header.go.restart),
3431 SCR_RETURN,
3432 0,
3433 /* In normal situations we branch to RESEL_DSA */
3434 }/*-------------------------< RESEL_NOTAG >-------------------*/,{
3435 /*
3436 ** JUMP indirectly to the restart point of the CCB.
3437 */
3438 SCR_JUMP,
3439 PADDR (resel_go),
3440
3441 }/*-------------------------< RESEL_DSA >-------------------*/,{
3442 /*
3443 ** Ack the IDENTIFY or TAG previously received.
3444 */
3445 SCR_CLR (SCR_ACK),
3446 0,
3447 /*
3448 ** load the savep (saved pointer) into
3449 ** the actual data pointer.
3450 */
3451 SCR_LOAD_REL (temp, 4),
3452 offsetof (struct ccb, phys.header.savep),
3453 /*
3454 ** Initialize the status registers
3455 */
3456 SCR_LOAD_REL (scr0, 4),
3457 offsetof (struct ccb, phys.header.status),
3458 /*
3459 ** Jump to dispatcher.
3460 */
3461 SCR_JUMP,
3462 PADDR (dispatch),
3463
3464 }/*-------------------------< DATA_IN >--------------------*/,{
3465 /*
3466 ** Because the size depends on the
3467 ** #define MAX_SCATTER parameter,
3468 ** it is filled in at runtime.
3469 **
3470 ** ##===========< i=0; i<MAX_SCATTER >=========
3471 ** || SCR_CHMOV_TBL ^ SCR_DATA_IN,
3472 ** || offsetof (struct dsb, data[ i]),
3473 ** ##==========================================
3474 **
3475 **---------------------------------------------------------
3476 */
3477 0
3478 }/*-------------------------< DATA_IN2 >-------------------*/,{
3479 SCR_CALL,
3480 PADDR (datai_done),
3481 SCR_JUMP,
3482 PADDRH (data_ovrun),
3483 }/*-------------------------< DATA_OUT >--------------------*/,{
3484 /*
3485 ** Because the size depends on the
3486 ** #define MAX_SCATTER parameter,
3487 ** it is filled in at runtime.
3488 **
3489 ** ##===========< i=0; i<MAX_SCATTER >=========
3490 ** || SCR_CHMOV_TBL ^ SCR_DATA_OUT,
3491 ** || offsetof (struct dsb, data[ i]),
3492 ** ##==========================================
3493 **
3494 **---------------------------------------------------------
3495 */
3496 0
3497 }/*-------------------------< DATA_OUT2 >-------------------*/,{
3498 SCR_CALL,
3499 PADDR (datao_done),
3500 SCR_JUMP,
3501 PADDRH (data_ovrun),
3502
3503 }/*-------------------------< PM0_DATA >--------------------*/,{
3504 /*
3505 ** Read our host flags to SFBR, so we will be able
3506 ** to check against the data direction we expect.
3507 */
3508 SCR_FROM_REG (HF_REG),
3509 0,
3510 /*
3511 ** Check against actual DATA PHASE.
3512 */
3513 SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
3514 PADDR (pm0_data_out),
3515 /*
3516 ** Actual phase is DATA IN.
3517 ** Check against expected direction.
3518 */
3519 SCR_JUMP ^ IFFALSE (MASK (HF_DATA_IN, HF_DATA_IN)),
3520 PADDRH (data_ovrun),
3521 /*
3522 ** Keep track we are moving data from the
3523 ** PM0 DATA mini-script.
3524 */
3525 SCR_REG_REG (HF_REG, SCR_OR, HF_IN_PM0),
3526 0,
3527 /*
3528 ** Move the data to memory.
3529 */
3530 SCR_CHMOV_TBL ^ SCR_DATA_IN,
3531 offsetof (struct ccb, phys.pm0.sg),
3532 SCR_JUMP,
3533 PADDR (pm0_data_end),
3534 }/*-------------------------< PM0_DATA_OUT >----------------*/,{
3535 /*
3536 ** Actual phase is DATA OUT.
3537 ** Check against expected direction.
3538 */
3539 SCR_JUMP ^ IFTRUE (MASK (HF_DATA_IN, HF_DATA_IN)),
3540 PADDRH (data_ovrun),
3541 /*
3542 ** Keep track we are moving data from the
3543 ** PM0 DATA mini-script.
3544 */
3545 SCR_REG_REG (HF_REG, SCR_OR, HF_IN_PM0),
3546 0,
3547 /*
3548 ** Move the data from memory.
3549 */
3550 SCR_CHMOV_TBL ^ SCR_DATA_OUT,
3551 offsetof (struct ccb, phys.pm0.sg),
3552 }/*-------------------------< PM0_DATA_END >----------------*/,{
3553 /*
3554 ** Clear the flag that told we were moving
3555 ** data from the PM0 DATA mini-script.
3556 */
3557 SCR_REG_REG (HF_REG, SCR_AND, (~HF_IN_PM0)),
3558 0,
3559 /*
3560 ** Return to the previous DATA script which
3561 ** is guaranteed by design (if no bug) to be
3562 ** the main DATA script for this transfer.
3563 */
3564 SCR_LOAD_REL (temp, 4),
3565 offsetof (struct ccb, phys.pm0.ret),
3566 SCR_RETURN,
3567 0,
3568 }/*-------------------------< PM1_DATA >--------------------*/,{
3569 /*
3570 ** Read our host flags to SFBR, so we will be able
3571 ** to check against the data direction we expect.
3572 */
3573 SCR_FROM_REG (HF_REG),
3574 0,
3575 /*
3576 ** Check against actual DATA PHASE.
3577 */
3578 SCR_JUMP ^ IFFALSE (WHEN (SCR_DATA_IN)),
3579 PADDR (pm1_data_out),
3580 /*
3581 ** Actual phase is DATA IN.
3582 ** Check against expected direction.
3583 */
3584 SCR_JUMP ^ IFFALSE (MASK (HF_DATA_IN, HF_DATA_IN)),
3585 PADDRH (data_ovrun),
3586 /*
3587 ** Keep track we are moving data from the
3588 ** PM1 DATA mini-script.
3589 */
3590 SCR_REG_REG (HF_REG, SCR_OR, HF_IN_PM1),
3591 0,
3592 /*
3593 ** Move the data to memory.
3594 */
3595 SCR_CHMOV_TBL ^ SCR_DATA_IN,
3596 offsetof (struct ccb, phys.pm1.sg),
3597 SCR_JUMP,
3598 PADDR (pm1_data_end),
3599 }/*-------------------------< PM1_DATA_OUT >----------------*/,{
3600 /*
3601 ** Actual phase is DATA OUT.
3602 ** Check against expected direction.
3603 */
3604 SCR_JUMP ^ IFTRUE (MASK (HF_DATA_IN, HF_DATA_IN)),
3605 PADDRH (data_ovrun),
3606 /*
3607 ** Keep track we are moving data from the
3608 ** PM1 DATA mini-script.
3609 */
3610 SCR_REG_REG (HF_REG, SCR_OR, HF_IN_PM1),
3611 0,
3612 /*
3613 ** Move the data from memory.
3614 */
3615 SCR_CHMOV_TBL ^ SCR_DATA_OUT,
3616 offsetof (struct ccb, phys.pm1.sg),
3617 }/*-------------------------< PM1_DATA_END >----------------*/,{
3618 /*
3619 ** Clear the flag that told we were moving
3620 ** data from the PM1 DATA mini-script.
3621 */
3622 SCR_REG_REG (HF_REG, SCR_AND, (~HF_IN_PM1)),
3623 0,
3624 /*
3625 ** Return to the previous DATA script which
3626 ** is guaranteed by design (if no bug) to be
3627 ** the main DATA script for this transfer.
3628 */
3629 SCR_LOAD_REL (temp, 4),
3630 offsetof (struct ccb, phys.pm1.ret),
3631 SCR_RETURN,
3632 0,
3633 }/*---------------------------------------------------------*/
3634 };
3635
3636
3637 static struct scripth scripth0 __initdata = {
3638 /*------------------------< START64 >-----------------------*/{
3639 /*
3640 ** SCRIPT entry point for the 895A and the 896.
3641 ** For now, there is no specific stuff for that
3642 ** chip at this point, but this may come.
3643 */
3644 SCR_JUMP,
3645 PADDR (init),
3646 }/*-------------------------< NO_DATA >-------------------*/,{
3647 SCR_JUMP,
3648 PADDRH (data_ovrun),
3649 }/*-----------------------< SEL_FOR_ABORT >------------------*/,{
3650 /*
3651 ** We are jumped here by the C code, if we have
3652 ** some target to reset or some disconnected
3653 ** job to abort. Since error recovery is a serious
3654 ** busyness, we will really reset the SCSI BUS, if
3655 ** case of a SCSI interrupt occurring in this path.
3656 */
3657
3658 /*
3659 ** Set initiator mode.
3660 */
3661 SCR_CLR (SCR_TRG),
3662 0,
3663 /*
3664 ** And try to select this target.
3665 */
3666 SCR_SEL_TBL_ATN ^ offsetof (struct ncb, abrt_sel),
3667 PADDR (reselect),
3668
3669 /*
3670 ** Wait for the selection to complete or
3671 ** the selection to time out.
3672 */
3673 SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_OUT)),
3674 -8,
3675 /*
3676 ** Call the C code.
3677 */
3678 SCR_INT,
3679 SIR_TARGET_SELECTED,
3680 /*
3681 ** The C code should let us continue here.
3682 ** Send the 'kiss of death' message.
3683 ** We expect an immediate disconnect once
3684 ** the target has eaten the message.
3685 */
3686 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
3687 0,
3688 SCR_MOVE_TBL ^ SCR_MSG_OUT,
3689 offsetof (struct ncb, abrt_tbl),
3690 SCR_CLR (SCR_ACK|SCR_ATN),
3691 0,
3692 SCR_WAIT_DISC,
3693 0,
3694 /*
3695 ** Tell the C code that we are done.
3696 */
3697 SCR_INT,
3698 SIR_ABORT_SENT,
3699 }/*-----------------------< SEL_FOR_ABORT_1 >--------------*/,{
3700 /*
3701 ** Jump at scheduler.
3702 */
3703 SCR_JUMP,
3704 PADDR (start),
3705
3706 }/*------------------------< SELECT_NO_ATN >-----------------*/,{
3707 /*
3708 ** Set Initiator mode.
3709 ** And try to select this target without ATN.
3710 */
3711
3712 SCR_CLR (SCR_TRG),
3713 0,
3714 SCR_SEL_TBL ^ offsetof (struct dsb, select),
3715 PADDR (ungetjob),
3716 /*
3717 ** load the savep (saved pointer) into
3718 ** the actual data pointer.
3719 */
3720 SCR_LOAD_REL (temp, 4),
3721 offsetof (struct ccb, phys.header.savep),
3722 /*
3723 ** Initialize the status registers
3724 */
3725 SCR_LOAD_REL (scr0, 4),
3726 offsetof (struct ccb, phys.header.status),
3727
3728 }/*------------------------< WF_SEL_DONE_NO_ATN >-----------------*/,{
3729 /*
3730 ** Wait immediately for the next phase or
3731 ** the selection to complete or time-out.
3732 */
3733 SCR_JUMPR ^ IFFALSE (WHEN (SCR_MSG_OUT)),
3734 0,
3735 SCR_JUMP,
3736 PADDR (select2),
3737
3738 }/*-------------------------< MSG_IN_ETC >--------------------*/,{
3739 /*
3740 ** If it is an EXTENDED (variable size message)
3741 ** Handle it.
3742 */
3743 SCR_JUMP ^ IFTRUE (DATA (M_EXTENDED)),
3744 PADDRH (msg_extended),
3745 /*
3746 ** Let the C code handle any other
3747 ** 1 byte message.
3748 */
3749 SCR_JUMP ^ IFTRUE (MASK (0x00, 0xf0)),
3750 PADDRH (msg_received),
3751 SCR_JUMP ^ IFTRUE (MASK (0x10, 0xf0)),
3752 PADDRH (msg_received),
3753 /*
3754 ** We donnot handle 2 bytes messages from SCRIPTS.
3755 ** So, let the C code deal with these ones too.
3756 */
3757 SCR_JUMP ^ IFFALSE (MASK (0x20, 0xf0)),
3758 PADDRH (msg_weird_seen),
3759 SCR_CLR (SCR_ACK),
3760 0,
3761 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3762 NADDR (msgin[1]),
3763 SCR_JUMP,
3764 PADDRH (msg_received),
3765
3766 }/*-------------------------< MSG_RECEIVED >--------------------*/,{
3767 SCR_LOAD_REL (scratcha, 4), /* DUMMY READ */
3768 0,
3769 SCR_INT,
3770 SIR_MSG_RECEIVED,
3771
3772 }/*-------------------------< MSG_WEIRD_SEEN >------------------*/,{
3773 SCR_LOAD_REL (scratcha, 4), /* DUMMY READ */
3774 0,
3775 SCR_INT,
3776 SIR_MSG_WEIRD,
3777
3778 }/*-------------------------< MSG_EXTENDED >--------------------*/,{
3779 /*
3780 ** Clear ACK and get the next byte
3781 ** assumed to be the message length.
3782 */
3783 SCR_CLR (SCR_ACK),
3784 0,
3785 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3786 NADDR (msgin[1]),
3787 /*
3788 ** Try to catch some unlikely situations as 0 length
3789 ** or too large the length.
3790 */
3791 SCR_JUMP ^ IFTRUE (DATA (0)),
3792 PADDRH (msg_weird_seen),
3793 SCR_TO_REG (scratcha),
3794 0,
3795 SCR_REG_REG (sfbr, SCR_ADD, (256-8)),
3796 0,
3797 SCR_JUMP ^ IFTRUE (CARRYSET),
3798 PADDRH (msg_weird_seen),
3799 /*
3800 ** We donnot handle extended messages from SCRIPTS.
3801 ** Read the amount of data correponding to the
3802 ** message length and call the C code.
3803 */
3804 SCR_STORE_REL (scratcha, 1),
3805 offsetof (struct dsb, smsg_ext.size),
3806 SCR_CLR (SCR_ACK),
3807 0,
3808 SCR_MOVE_TBL ^ SCR_MSG_IN,
3809 offsetof (struct dsb, smsg_ext),
3810 SCR_JUMP,
3811 PADDRH (msg_received),
3812
3813 }/*-------------------------< MSG_BAD >------------------*/,{
3814 /*
3815 ** unimplemented message - reject it.
3816 */
3817 SCR_INT,
3818 SIR_REJECT_TO_SEND,
3819 SCR_SET (SCR_ATN),
3820 0,
3821 SCR_JUMP,
3822 PADDR (clrack),
3823
3824 }/*-------------------------< MSG_WEIRD >--------------------*/,{
3825 /*
3826 ** weird message received
3827 ** ignore all MSG IN phases and reject it.
3828 */
3829 SCR_INT,
3830 SIR_REJECT_TO_SEND,
3831 SCR_SET (SCR_ATN),
3832 0,
3833 }/*-------------------------< MSG_WEIRD1 >--------------------*/,{
3834 SCR_CLR (SCR_ACK),
3835 0,
3836 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_IN)),
3837 PADDR (dispatch),
3838 SCR_MOVE_ABS (1) ^ SCR_MSG_IN,
3839 NADDR (scratch),
3840 SCR_JUMP,
3841 PADDRH (msg_weird1),
3842 }/*-------------------------< WDTR_RESP >----------------*/,{
3843 /*
3844 ** let the target fetch our answer.
3845 */
3846 SCR_SET (SCR_ATN),
3847 0,
3848 SCR_CLR (SCR_ACK),
3849 0,
3850 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
3851 PADDRH (nego_bad_phase),
3852
3853 }/*-------------------------< SEND_WDTR >----------------*/,{
3854 /*
3855 ** Send the M_X_WIDE_REQ
3856 */
3857 SCR_MOVE_ABS (4) ^ SCR_MSG_OUT,
3858 NADDR (msgout),
3859 SCR_JUMP,
3860 PADDRH (msg_out_done),
3861
3862 }/*-------------------------< SDTR_RESP >-------------*/,{
3863 /*
3864 ** let the target fetch our answer.
3865 */
3866 SCR_SET (SCR_ATN),
3867 0,
3868 SCR_CLR (SCR_ACK),
3869 0,
3870 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
3871 PADDRH (nego_bad_phase),
3872
3873 }/*-------------------------< SEND_SDTR >-------------*/,{
3874 /*
3875 ** Send the M_X_SYNC_REQ
3876 */
3877 SCR_MOVE_ABS (5) ^ SCR_MSG_OUT,
3878 NADDR (msgout),
3879 SCR_JUMP,
3880 PADDRH (msg_out_done),
3881
3882 }/*-------------------------< PPR_RESP >-------------*/,{
3883 /*
3884 ** let the target fetch our answer.
3885 */
3886 SCR_SET (SCR_ATN),
3887 0,
3888 SCR_CLR (SCR_ACK),
3889 0,
3890 SCR_JUMP ^ IFFALSE (WHEN (SCR_MSG_OUT)),
3891 PADDRH (nego_bad_phase),
3892
3893 }/*-------------------------< SEND_PPR >-------------*/,{
3894 /*
3895 ** Send the M_X_PPR_REQ
3896 */
3897 SCR_MOVE_ABS (8) ^ SCR_MSG_OUT,
3898 NADDR (msgout),
3899 SCR_JUMP,
3900 PADDRH (msg_out_done),
3901
3902 }/*-------------------------< NEGO_BAD_PHASE >------------*/,{
3903 SCR_INT,
3904 SIR_NEGO_PROTO,
3905 SCR_JUMP,
3906 PADDR (dispatch),
3907
3908 }/*-------------------------< MSG_OUT >-------------------*/,{
3909 /*
3910 ** The target requests a message.
3911 */
3912 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
3913 NADDR (msgout),
3914 /*
3915 ** ... wait for the next phase
3916 ** if it's a message out, send it again, ...
3917 */
3918 SCR_JUMP ^ IFTRUE (WHEN (SCR_MSG_OUT)),
3919 PADDRH (msg_out),
3920 }/*-------------------------< MSG_OUT_DONE >--------------*/,{
3921 /*
3922 ** ... else clear the message ...
3923 */
3924 SCR_INT,
3925 SIR_MSG_OUT_DONE,
3926 /*
3927 ** ... and process the next phase
3928 */
3929 SCR_JUMP,
3930 PADDR (dispatch),
3931
3932 }/*-------------------------< DATA_OVRUN >-----------------------*/,{
3933 /*
3934 * Use scratcha to count the extra bytes.
3935 */
3936 SCR_LOAD_ABS (scratcha, 4),
3937 PADDRH (zero),
3938 }/*-------------------------< DATA_OVRUN1 >----------------------*/,{
3939 /*
3940 * The target may want to transfer too much data.
3941 *
3942 * If phase is DATA OUT write 1 byte and count it.
3943 */
3944 SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_OUT)),
3945 16,
3946 SCR_CHMOV_ABS (1) ^ SCR_DATA_OUT,
3947 NADDR (scratch),
3948 SCR_JUMP,
3949 PADDRH (data_ovrun2),
3950 /*
3951 * If WSR is set, clear this condition, and
3952 * count this byte.
3953 */
3954 SCR_FROM_REG (scntl2),
3955 0,
3956 SCR_JUMPR ^ IFFALSE (MASK (WSR, WSR)),
3957 16,
3958 SCR_REG_REG (scntl2, SCR_OR, WSR),
3959 0,
3960 SCR_JUMP,
3961 PADDRH (data_ovrun2),
3962 /*
3963 * Finally check against DATA IN phase.
3964 * Signal data overrun to the C code
3965 * and jump to dispatcher if not so.
3966 * Read 1 byte otherwise and count it.
3967 */
3968 SCR_JUMPR ^ IFTRUE (WHEN (SCR_DATA_IN)),
3969 16,
3970 SCR_INT,
3971 SIR_DATA_OVERRUN,
3972 SCR_JUMP,
3973 PADDR (dispatch),
3974 SCR_CHMOV_ABS (1) ^ SCR_DATA_IN,
3975 NADDR (scratch),
3976 }/*-------------------------< DATA_OVRUN2 >----------------------*/,{
3977 /*
3978 * Count this byte.
3979 * This will allow to return a negative
3980 * residual to user.
3981 */
3982 SCR_REG_REG (scratcha, SCR_ADD, 0x01),
3983 0,
3984 SCR_REG_REG (scratcha1, SCR_ADDC, 0),
3985 0,
3986 SCR_REG_REG (scratcha2, SCR_ADDC, 0),
3987 0,
3988 /*
3989 * .. and repeat as required.
3990 */
3991 SCR_JUMP,
3992 PADDRH (data_ovrun1),
3993
3994 }/*-------------------------< ABORT_RESEL >----------------*/,{
3995 SCR_SET (SCR_ATN),
3996 0,
3997 SCR_CLR (SCR_ACK),
3998 0,
3999 /*
4000 ** send the abort/abortag/reset message
4001 ** we expect an immediate disconnect
4002 */
4003 SCR_REG_REG (scntl2, SCR_AND, 0x7f),
4004 0,
4005 SCR_MOVE_ABS (1) ^ SCR_MSG_OUT,
4006 NADDR (msgout),
4007 SCR_CLR (SCR_ACK|SCR_ATN),
4008 0,
4009 SCR_WAIT_DISC,
4010 0,
4011 SCR_INT,
4012 SIR_RESEL_ABORTED,
4013 SCR_JUMP,
4014 PADDR (start),
4015 }/*-------------------------< RESEND_IDENT >-------------------*/,{
4016 /*
4017 ** The target stays in MSG OUT phase after having acked
4018 ** Identify [+ Tag [+ Extended message ]]. Targets shall
4019 ** behave this way on parity error.
4020 ** We must send it again all the messages.
4021 */
4022 SCR_SET (SCR_ATN), /* Shall be asserted 2 deskew delays before the */
4023 0, /* 1rst ACK = 90 ns. Hope the NCR is'nt too fast */
4024 SCR_JUMP,
4025 PADDR (send_ident),
4026 }/*-------------------------< IDENT_BREAK >-------------------*/,{
4027 SCR_CLR (SCR_ATN),
4028 0,
4029 SCR_JUMP,
4030 PADDR (select2),
4031 }/*-------------------------< IDENT_BREAK_ATN >----------------*/,{
4032 SCR_SET (SCR_ATN),
4033 0,
4034 SCR_JUMP,
4035 PADDR (select2),
4036 }/*-------------------------< SDATA_IN >-------------------*/,{
4037 SCR_CHMOV_TBL ^ SCR_DATA_IN,
4038 offsetof (struct dsb, sense),
4039 SCR_CALL,
4040 PADDR (datai_done),
4041 SCR_JUMP,
4042 PADDRH (data_ovrun),
4043 }/*-------------------------< DATA_IO >--------------------*/,{
4044 /*
4045 ** We jump here if the data direction was unknown at the
4046 ** time we had to queue the command to the scripts processor.
4047 ** Pointers had been set as follow in this situation:
4048 ** savep --> DATA_IO
4049 ** lastp --> start pointer when DATA_IN
4050 ** goalp --> goal pointer when DATA_IN
4051 ** wlastp --> start pointer when DATA_OUT
4052 ** wgoalp --> goal pointer when DATA_OUT
4053 ** This script sets savep/lastp/goalp according to the
4054 ** direction chosen by the target.
4055 */
4056 SCR_JUMP ^ IFTRUE (WHEN (SCR_DATA_OUT)),
4057 PADDRH(data_io_out),
4058 }/*-------------------------< DATA_IO_COM >-----------------*/,{
4059 /*
4060 ** Direction is DATA IN.
4061 ** Warning: we jump here, even when phase is DATA OUT.
4062 */
4063 SCR_LOAD_REL (scratcha, 4),
4064 offsetof (struct ccb, phys.header.lastp),
4065 SCR_STORE_REL (scratcha, 4),
4066 offsetof (struct ccb, phys.header.savep),
4067
4068 /*
4069 ** Jump to the SCRIPTS according to actual direction.
4070 */
4071 SCR_LOAD_REL (temp, 4),
4072 offsetof (struct ccb, phys.header.savep),
4073 SCR_RETURN,
4074 0,
4075 }/*-------------------------< DATA_IO_OUT >-----------------*/,{
4076 /*
4077 ** Direction is DATA OUT.
4078 */
4079 SCR_REG_REG (HF_REG, SCR_AND, (~HF_DATA_IN)),
4080 0,
4081 SCR_LOAD_REL (scratcha, 4),
4082 offsetof (struct ccb, phys.header.wlastp),
4083 SCR_STORE_REL (scratcha, 4),
4084 offsetof (struct ccb, phys.header.lastp),
4085 SCR_LOAD_REL (scratcha, 4),
4086 offsetof (struct ccb, phys.header.wgoalp),
4087 SCR_STORE_REL (scratcha, 4),
4088 offsetof (struct ccb, phys.header.goalp),
4089 SCR_JUMP,
4090 PADDRH(data_io_com),
4091
4092 }/*-------------------------< RESEL_BAD_LUN >---------------*/,{
4093 /*
4094 ** Message is an IDENTIFY, but lun is unknown.
4095 ** Signal problem to C code for logging the event.
4096 ** Send a M_ABORT to clear all pending tasks.
4097 */
4098 SCR_INT,
4099 SIR_RESEL_BAD_LUN,
4100 SCR_JUMP,
4101 PADDRH (abort_resel),
4102 }/*-------------------------< BAD_I_T_L >------------------*/,{
4103 /*
4104 ** We donnot have a task for that I_T_L.
4105 ** Signal problem to C code for logging the event.
4106 ** Send a M_ABORT message.
4107 */
4108 SCR_INT,
4109 SIR_RESEL_BAD_I_T_L,
4110 SCR_JUMP,
4111 PADDRH (abort_resel),
4112 }/*-------------------------< BAD_I_T_L_Q >----------------*/,{
4113 /*
4114 ** We donnot have a task that matches the tag.
4115 ** Signal problem to C code for logging the event.
4116 ** Send a M_ABORTTAG message.
4117 */
4118 SCR_INT,
4119 SIR_RESEL_BAD_I_T_L_Q,
4120 SCR_JUMP,
4121 PADDRH (abort_resel),
4122 }/*-------------------------< BAD_STATUS >-----------------*/,{
4123 /*
4124 ** Anything different from INTERMEDIATE
4125 ** CONDITION MET should be a bad SCSI status,
4126 ** given that GOOD status has already been tested.
4127 ** Call the C code.
4128 */
4129 SCR_LOAD_ABS (scratcha, 4),
4130 PADDRH (startpos),
4131 SCR_INT ^ IFFALSE (DATA (S_COND_MET)),
4132 SIR_BAD_STATUS,
4133 SCR_RETURN,
4134 0,
4135
4136 }/*-------------------------< TWEAK_PMJ >------------------*/,{
4137 /*
4138 ** Disable PM handling from SCRIPTS for the data phase
4139 ** and so force PM to be handled from C code if HF_PM_TO_C
4140 ** flag is set.
4141 */
4142 SCR_FROM_REG(HF_REG),
4143 0,
4144 SCR_JUMPR ^ IFTRUE (MASK (HF_PM_TO_C, HF_PM_TO_C)),
4145 16,
4146 SCR_REG_REG (ccntl0, SCR_OR, ENPMJ),
4147 0,
4148 SCR_RETURN,
4149 0,
4150 SCR_REG_REG (ccntl0, SCR_AND, (~ENPMJ)),
4151 0,
4152 SCR_RETURN,
4153 0,
4154
4155 }/*-------------------------< PM_HANDLE >------------------*/,{
4156 /*
4157 ** Phase mismatch handling.
4158 **
4159 ** Since we have to deal with 2 SCSI data pointers
4160 ** (current and saved), we need at least 2 contexts.
4161 ** Each context (pm0 and pm1) has a saved area, a
4162 ** SAVE mini-script and a DATA phase mini-script.
4163 */
4164 /*
4165 ** Get the PM handling flags.
4166 */
4167 SCR_FROM_REG (HF_REG),
4168 0,
4169 /*
4170 ** If no flags (1rst PM for example), avoid
4171 ** all the below heavy flags testing.
4172 ** This makes the normal case a bit faster.
4173 */
4174 SCR_JUMP ^ IFTRUE (MASK (0, (HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED))),
4175 PADDRH (pm_handle1),
4176 /*
4177 ** If we received a SAVE DP, switch to the
4178 ** other PM context since the savep may point
4179 ** to the current PM context.
4180 */
4181 SCR_JUMPR ^ IFFALSE (MASK (HF_DP_SAVED, HF_DP_SAVED)),
4182 8,
4183 SCR_REG_REG (sfbr, SCR_XOR, HF_ACT_PM),
4184 0,
4185 /*
4186 ** If we have been interrupt in a PM DATA mini-script,
4187 ** we take the return address from the corresponding
4188 ** saved area.
4189 ** This ensure the return address always points to the
4190 ** main DATA script for this transfer.
4191 */
4192 SCR_JUMP ^ IFTRUE (MASK (0, (HF_IN_PM0 | HF_IN_PM1))),
4193 PADDRH (pm_handle1),
4194 SCR_JUMPR ^ IFFALSE (MASK (HF_IN_PM0, HF_IN_PM0)),
4195 16,
4196 SCR_LOAD_REL (ia, 4),
4197 offsetof(struct ccb, phys.pm0.ret),
4198 SCR_JUMP,
4199 PADDRH (pm_save),
4200 SCR_LOAD_REL (ia, 4),
4201 offsetof(struct ccb, phys.pm1.ret),
4202 SCR_JUMP,
4203 PADDRH (pm_save),
4204 }/*-------------------------< PM_HANDLE1 >-----------------*/,{
4205 /*
4206 ** Normal case.
4207 ** Update the return address so that it
4208 ** will point after the interrupted MOVE.
4209 */
4210 SCR_REG_REG (ia, SCR_ADD, 8),
4211 0,
4212 SCR_REG_REG (ia1, SCR_ADDC, 0),
4213 0,
4214 }/*-------------------------< PM_SAVE >--------------------*/,{
4215 /*
4216 ** Clear all the flags that told us if we were
4217 ** interrupted in a PM DATA mini-script and/or
4218 ** we received a SAVE DP.
4219 */
4220 SCR_SFBR_REG (HF_REG, SCR_AND, (~(HF_IN_PM0|HF_IN_PM1|HF_DP_SAVED))),
4221 0,
4222 /*
4223 ** Choose the current PM context.
4224 */
4225 SCR_JUMP ^ IFTRUE (MASK (HF_ACT_PM, HF_ACT_PM)),
4226 PADDRH (pm1_save),
4227 }/*-------------------------< PM0_SAVE >-------------------*/,{
4228 SCR_STORE_REL (ia, 4),
4229 offsetof(struct ccb, phys.pm0.ret),
4230 /*
4231 ** If WSR bit is set, either UA and RBC may
4232 ** have to be changed whatever the device wants
4233 ** to ignore this residue ot not.
4234 */
4235 SCR_FROM_REG (scntl2),
4236 0,
4237 SCR_CALL ^ IFTRUE (MASK (WSR, WSR)),
4238 PADDRH (pm_wsr_handle),
4239 /*
4240 ** Save the remaining byte count, the updated
4241 ** address and the return address.
4242 */
4243 SCR_STORE_REL (rbc, 4),
4244 offsetof(struct ccb, phys.pm0.sg.size),
4245 SCR_STORE_REL (ua, 4),
4246 offsetof(struct ccb, phys.pm0.sg.addr),
4247 /*
4248 ** Set the current pointer at the PM0 DATA mini-script.
4249 */
4250 SCR_LOAD_ABS (temp, 4),
4251 PADDRH (pm0_data_addr),
4252 SCR_JUMP,
4253 PADDR (dispatch),
4254 }/*-------------------------< PM1_SAVE >-------------------*/,{
4255 SCR_STORE_REL (ia, 4),
4256 offsetof(struct ccb, phys.pm1.ret),
4257 /*
4258 ** If WSR bit is set, either UA and RBC may
4259 ** have been changed whatever the device wants
4260 ** to ignore this residue or not.
4261 */
4262 SCR_FROM_REG (scntl2),
4263 0,
4264 SCR_CALL ^ IFTRUE (MASK (WSR, WSR)),
4265 PADDRH (pm_wsr_handle),
4266 /*
4267 ** Save the remaining byte count, the updated
4268 ** address and the return address.
4269 */
4270 SCR_STORE_REL (rbc, 4),
4271 offsetof(struct ccb, phys.pm1.sg.size),
4272 SCR_STORE_REL (ua, 4),
4273 offsetof(struct ccb, phys.pm1.sg.addr),
4274 /*
4275 ** Set the current pointer at the PM1 DATA mini-script.
4276 */
4277 SCR_LOAD_ABS (temp, 4),
4278 PADDRH (pm1_data_addr),
4279 SCR_JUMP,
4280 PADDR (dispatch),
4281 }/*--------------------------< PM_WSR_HANDLE >-----------------------*/,{
4282 /*
4283 * Phase mismatch handling from SCRIPT with WSR set.
4284 * Such a condition can occur if the chip wants to
4285 * execute a CHMOV(size > 1) when the WSR bit is
4286 * set and the target changes PHASE.
4287 */
4288 #ifdef SYM_DEBUG_PM_WITH_WSR
4289 /*
4290 * Some debugging may still be needed.:)
4291 */
4292 SCR_INT,
4293 SIR_PM_WITH_WSR,
4294 #endif
4295 /*
4296 * We must move the residual byte to memory.
4297 *
4298 * UA contains bit 0..31 of the address to
4299 * move the residual byte.
4300 * Move it to the table indirect.
4301 */
4302 SCR_STORE_REL (ua, 4),
4303 offsetof (struct ccb, phys.wresid.addr),
4304 /*
4305 * Increment UA (move address to next position).
4306 */
4307 SCR_REG_REG (ua, SCR_ADD, 1),
4308 0,
4309 SCR_REG_REG (ua1, SCR_ADDC, 0),
4310 0,
4311 SCR_REG_REG (ua2, SCR_ADDC, 0),
4312 0,
4313 SCR_REG_REG (ua3, SCR_ADDC, 0),
4314 0,
4315 /*
4316 * Compute SCRATCHA as:
4317 * - size to transfer = 1 byte.
4318 * - bit 24..31 = high address bit [32...39].
4319 */
4320 SCR_LOAD_ABS (scratcha, 4),
4321 PADDRH (zero),
4322 SCR_REG_REG (scratcha, SCR_OR, 1),
4323 0,
4324 SCR_FROM_REG (rbc3),
4325 0,
4326 SCR_TO_REG (scratcha3),
4327 0,
4328 /*
4329 * Move this value to the table indirect.
4330 */
4331 SCR_STORE_REL (scratcha, 4),
4332 offsetof (struct ccb, phys.wresid.size),
4333 /*
4334 * Wait for a valid phase.
4335 * While testing with bogus QUANTUM drives, the C1010
4336 * sometimes raised a spurious phase mismatch with
4337 * WSR and the CHMOV(1) triggered another PM.
4338 * Waiting explicitely for the PHASE seemed to avoid
4339 * the nested phase mismatch. Btw, this didn't happen
4340 * using my IBM drives.
4341 */
4342 SCR_JUMPR ^ IFFALSE (WHEN (SCR_DATA_IN)),
4343 0,
4344 /*
4345 * Perform the move of the residual byte.
4346 */
4347 SCR_CHMOV_TBL ^ SCR_DATA_IN,
4348 offsetof (struct ccb, phys.wresid),
4349 /*
4350 * We can now handle the phase mismatch with UA fixed.
4351 * RBC[0..23]=0 is a special case that does not require
4352 * a PM context. The C code also checks against this.
4353 */
4354 SCR_FROM_REG (rbc),
4355 0,
4356 SCR_RETURN ^ IFFALSE (DATA (0)),
4357 0,
4358 SCR_FROM_REG (rbc1),
4359 0,
4360 SCR_RETURN ^ IFFALSE (DATA (0)),
4361 0,
4362 SCR_FROM_REG (rbc2),
4363 0,
4364 SCR_RETURN ^ IFFALSE (DATA (0)),
4365 0,
4366 /*
4367 * RBC[0..23]=0.
4368 * Not only we donnot need a PM context, but this would
4369 * lead to a bogus CHMOV(0). This condition means that
4370 * the residual was the last byte to move from this CHMOV.
4371 * So, we just have to move the current data script pointer
4372 * (i.e. TEMP) to the SCRIPTS address following the
4373 * interrupted CHMOV and jump to dispatcher.
4374 */
4375 SCR_STORE_ABS (ia, 4),
4376 PADDRH (scratch),
4377 SCR_LOAD_ABS (temp, 4),
4378 PADDRH (scratch),
4379 SCR_JUMP,
4380 PADDR (dispatch),
4381 }/*--------------------------< WSR_MA_HELPER >-----------------------*/,{
4382 /*
4383 * Helper for the C code when WSR bit is set.
4384 * Perform the move of the residual byte.
4385 */
4386 SCR_CHMOV_TBL ^ SCR_DATA_IN,
4387 offsetof (struct ccb, phys.wresid),
4388 SCR_JUMP,
4389 PADDR (dispatch),
4390 }/*-------------------------< ZERO >------------------------*/,{
4391 SCR_DATA_ZERO,
4392 }/*-------------------------< SCRATCH >---------------------*/,{
4393 SCR_DATA_ZERO,
4394 }/*-------------------------< SCRATCH1 >--------------------*/,{
4395 SCR_DATA_ZERO,
4396 }/*-------------------------< PM0_DATA_ADDR >---------------*/,{
4397 SCR_DATA_ZERO,
4398 }/*-------------------------< PM1_DATA_ADDR >---------------*/,{
4399 SCR_DATA_ZERO,
4400 }/*-------------------------< SAVED_DSA >-------------------*/,{
4401 SCR_DATA_ZERO,
4402 }/*-------------------------< SAVED_DRS >-------------------*/,{
4403 SCR_DATA_ZERO,
4404 }/*-------------------------< DONE_POS >--------------------*/,{
4405 SCR_DATA_ZERO,
4406 }/*-------------------------< STARTPOS >--------------------*/,{
4407 SCR_DATA_ZERO,
4408 }/*-------------------------< TARGTBL >---------------------*/,{
4409 SCR_DATA_ZERO,
4410
4411
4412 /*
4413 ** We may use MEMORY MOVE instructions to load the on chip-RAM,
4414 ** if it happens that mapping PCI memory is not possible.
4415 ** But writing the RAM from the CPU is the preferred method,
4416 ** since PCI 2.2 seems to disallow PCI self-mastering.
4417 */
4418
4419 #ifdef SCSI_NCR_PCI_MEM_NOT_SUPPORTED
4420
4421 }/*-------------------------< START_RAM >-------------------*/,{
4422 /*
4423 ** Load the script into on-chip RAM,
4424 ** and jump to start point.
4425 */
4426 SCR_COPY (sizeof (struct script)),
4427 }/*-------------------------< SCRIPT0_BA >--------------------*/,{
4428 0,
4429 PADDR (start),
4430 SCR_JUMP,
4431 PADDR (init),
4432
4433 }/*-------------------------< START_RAM64 >--------------------*/,{
4434 /*
4435 ** Load the RAM and start for 64 bit PCI (895A,896).
4436 ** Both scripts (script and scripth) are loaded into
4437 ** the RAM which is 8K (4K for 825A/875/895).
4438 ** We also need to load some 32-63 bit segments
4439 ** address of the SCRIPTS processor.
4440 ** LOAD/STORE ABSOLUTE always refers to on-chip RAM
4441 ** in our implementation. The main memory is
4442 ** accessed using LOAD/STORE DSA RELATIVE.
4443 */
4444 SCR_LOAD_REL (mmws, 4),
4445 offsetof (struct ncb, scr_ram_seg),
4446 SCR_COPY (sizeof(struct script)),
4447 }/*-------------------------< SCRIPT0_BA64 >--------------------*/,{
4448 0,
4449 PADDR (start),
4450 SCR_COPY (sizeof(struct scripth)),
4451 }/*-------------------------< SCRIPTH0_BA64 >--------------------*/,{
4452 0,
4453 PADDRH (start64),
4454 SCR_LOAD_REL (mmrs, 4),
4455 offsetof (struct ncb, scr_ram_seg),
4456 SCR_JUMP64,
4457 PADDRH (start64),
4458 }/*-------------------------< RAM_SEG64 >--------------------*/,{
4459 0,
4460
4461 #endif /* SCSI_NCR_PCI_MEM_NOT_SUPPORTED */
4462
4463 }/*-------------------------< SNOOPTEST >-------------------*/,{
4464 /*
4465 ** Read the variable.
4466 */
4467 SCR_LOAD_REL (scratcha, 4),
4468 offsetof(struct ncb, ncr_cache),
4469 SCR_STORE_REL (temp, 4),
4470 offsetof(struct ncb, ncr_cache),
4471 SCR_LOAD_REL (temp, 4),
4472 offsetof(struct ncb, ncr_cache),
4473 }/*-------------------------< SNOOPEND >-------------------*/,{
4474 /*
4475 ** And stop.
4476 */
4477 SCR_INT,
4478 99,
4479 }/*--------------------------------------------------------*/
4480 };
4481
4482 /*==========================================================
4483 **
4484 **
4485 ** Fill in #define dependent parts of the script
4486 **
4487 **
4488 **==========================================================
4489 */
4490
4491 void __init ncr_script_fill (struct script * scr, struct scripth * scrh)
4492 {
4493 int i;
4494 ncrcmd *p;
4495
4496 p = scr->data_in;
4497 for (i=0; i<MAX_SCATTER; i++) {
4498 *p++ =SCR_CHMOV_TBL ^ SCR_DATA_IN;
4499 *p++ =offsetof (struct dsb, data[i]);
4500 };
4501
4502 assert ((u_long)p == (u_long)&scr->data_in + sizeof (scr->data_in));
4503
4504 p = scr->data_out;
4505
4506 for (i=0; i<MAX_SCATTER; i++) {
4507 *p++ =SCR_CHMOV_TBL ^ SCR_DATA_OUT;
4508 *p++ =offsetof (struct dsb, data[i]);
4509 };
4510
4511 assert ((u_long)p == (u_long)&scr->data_out + sizeof (scr->data_out));
4512 }
4513
4514 /*==========================================================
4515 **
4516 **
4517 ** Copy and rebind a script.
4518 **
4519 **
4520 **==========================================================
4521 */
4522
4523 static void __init
4524 ncr_script_copy_and_bind (ncb_p np,ncrcmd *src,ncrcmd *dst,int len)
4525 {
4526 ncrcmd opcode, new, old, tmp1, tmp2;
4527 ncrcmd *start, *end;
4528 int relocs;
4529 int opchanged = 0;
4530
4531 start = src;
4532 end = src + len/4;
4533
4534 while (src < end) {
4535
4536 opcode = *src++;
4537 *dst++ = cpu_to_scr(opcode);
4538
4539 /*
4540 ** If we forget to change the length
4541 ** in struct script, a field will be
4542 ** padded with 0. This is an illegal
4543 ** command.
4544 */
4545
4546 if (opcode == 0) {
4547 printk (KERN_INFO "%s: ERROR0 IN SCRIPT at %d.\n",
4548 ncr_name(np), (int) (src-start-1));
4549 MDELAY (10000);
4550 continue;
4551 };
4552
4553 /*
4554 ** We use the bogus value 0xf00ff00f ;-)
4555 ** to reserve data area in SCRIPTS.
4556 */
4557 if (opcode == SCR_DATA_ZERO) {
4558 dst[-1] = 0;
4559 continue;
4560 }
4561
4562 if (DEBUG_FLAGS & DEBUG_SCRIPT)
4563 printk (KERN_INFO "%p: <%x>\n",
4564 (src-1), (unsigned)opcode);
4565
4566 /*
4567 ** We don't have to decode ALL commands
4568 */
4569 switch (opcode >> 28) {
4570
4571 case 0xf:
4572 /*
4573 ** LOAD / STORE DSA relative, don't relocate.
4574 */
4575 relocs = 0;
4576 break;
4577 case 0xe:
4578 /*
4579 ** LOAD / STORE absolute.
4580 */
4581 relocs = 1;
4582 break;
4583 case 0xc:
4584 /*
4585 ** COPY has TWO arguments.
4586 */
4587 relocs = 2;
4588 tmp1 = src[0];
4589 tmp2 = src[1];
4590 #ifdef RELOC_KVAR
4591 if ((tmp1 & RELOC_MASK) == RELOC_KVAR)
4592 tmp1 = 0;
4593 if ((tmp2 & RELOC_MASK) == RELOC_KVAR)
4594 tmp2 = 0;
4595 #endif
4596 if ((tmp1 ^ tmp2) & 3) {
4597 printk (KERN_ERR"%s: ERROR1 IN SCRIPT at %d.\n",
4598 ncr_name(np), (int) (src-start-1));
4599 MDELAY (1000);
4600 }
4601 /*
4602 ** If PREFETCH feature not enabled, remove
4603 ** the NO FLUSH bit if present.
4604 */
4605 if ((opcode & SCR_NO_FLUSH) &&
4606 !(np->features & FE_PFEN)) {
4607 dst[-1] = cpu_to_scr(opcode & ~SCR_NO_FLUSH);
4608 ++opchanged;
4609 }
4610 break;
4611
4612 case 0x0:
4613 /*
4614 ** MOVE/CHMOV (absolute address)
4615 */
4616 if (!(np->features & FE_WIDE))
4617 dst[-1] = cpu_to_scr(opcode | OPC_MOVE);
4618 relocs = 1;
4619 break;
4620
4621 case 0x1:
4622 /*
4623 ** MOVE/CHMOV (table indirect)
4624 */
4625 if (!(np->features & FE_WIDE))
4626 dst[-1] = cpu_to_scr(opcode | OPC_MOVE);
4627 relocs = 0;
4628 break;
4629
4630 case 0x8:
4631 /*
4632 ** JUMP / CALL
4633 ** don't relocate if relative :-)
4634 */
4635 if (opcode & 0x00800000)
4636 relocs = 0;
4637 else if ((opcode & 0xf8400000) == 0x80400000)/*JUMP64*/
4638 relocs = 2;
4639 else
4640 relocs = 1;
4641 break;
4642
4643 case 0x4:
4644 case 0x5:
4645 case 0x6:
4646 case 0x7:
4647 relocs = 1;
4648 break;
4649
4650 default:
4651 relocs = 0;
4652 break;
4653 };
4654
4655 if (!relocs) {
4656 *dst++ = cpu_to_scr(*src++);
4657 continue;
4658 }
4659 while (relocs--) {
4660 old = *src++;
4661
4662 switch (old & RELOC_MASK) {
4663 case RELOC_REGISTER:
4664 new = (old & ~RELOC_MASK) + np->base_ba;
4665 break;
4666 case RELOC_LABEL:
4667 new = (old & ~RELOC_MASK) + np->p_script;
4668 break;
4669 case RELOC_LABELH:
4670 new = (old & ~RELOC_MASK) + np->p_scripth;
4671 break;
4672 case RELOC_SOFTC:
4673 new = (old & ~RELOC_MASK) + np->p_ncb;
4674 break;
4675 #ifdef RELOC_KVAR
4676 case RELOC_KVAR:
4677 new=0;
4678 if (((old & ~RELOC_MASK) < SCRIPT_KVAR_FIRST) ||
4679 ((old & ~RELOC_MASK) > SCRIPT_KVAR_LAST))
4680 panic("ncr KVAR out of range");
4681 new = vtobus(script_kvars[old & ~RELOC_MASK]);
4682 #endif
4683 break;
4684 case 0:
4685 /* Don't relocate a 0 address. */
4686 if (old == 0) {
4687 new = old;
4688 break;
4689 }
4690 /* fall through */
4691 default:
4692 new = 0; /* For 'cc' not to complain */
4693 panic("ncr_script_copy_and_bind: "
4694 "weird relocation %x\n", old);
4695 break;
4696 }
4697
4698 *dst++ = cpu_to_scr(new);
4699 }
4700 };
4701 }
4702
4703 /*==========================================================
4704 **
4705 **
4706 ** Auto configuration: attach and init a host adapter.
4707 **
4708 **
4709 **==========================================================
4710 */
4711
4712 /*
4713 ** Linux host data structure.
4714 */
4715
4716 struct host_data {
4717 struct ncb *ncb;
4718 };
4719
4720 /*
4721 ** Print something which allows to retrieve the controler type, unit,
4722 ** target, lun concerned by a kernel message.
4723 */
4724
4725 static void PRINT_TARGET(ncb_p np, int target)
4726 {
4727 printk(KERN_INFO "%s-<%d,*>: ", ncr_name(np), target);
4728 }
4729
4730 static void PRINT_LUN(ncb_p np, int target, int lun)
4731 {
4732 printk(KERN_INFO "%s-<%d,%d>: ", ncr_name(np), target, lun);
4733 }
4734
4735 static void PRINT_ADDR(Scsi_Cmnd *cmd)
4736 {
4737 struct host_data *host_data = (struct host_data *) cmd->device->host->hostdata;
4738 PRINT_LUN(host_data->ncb, cmd->device->id, cmd->device->lun);
4739 }
4740
4741 /*==========================================================
4742 **
4743 ** NCR chip clock divisor table.
4744 ** Divisors are multiplied by 10,000,000 in order to make
4745 ** calculations more simple.
4746 **
4747 **==========================================================
4748 */
4749
4750 #define _5M 5000000
4751 static u_long div_10M[] =
4752 {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
4753
4754
4755 /*===============================================================
4756 **
4757 ** Prepare io register values used by ncr_init() according
4758 ** to selected and supported features.
4759 **
4760 ** NCR/SYMBIOS chips allow burst lengths of 2, 4, 8, 16, 32, 64,
4761 ** 128 transfers. All chips support at least 16 transfers bursts.
4762 ** The 825A, 875 and 895 chips support bursts of up to 128
4763 ** transfers and the 895A and 896 support bursts of up to 64
4764 ** transfers. All other chips support up to 16 transfers bursts.
4765 **
4766 ** For PCI 32 bit data transfers each transfer is a DWORD (4 bytes).
4767 ** It is a QUADWORD (8 bytes) for PCI 64 bit data transfers.
4768 ** Only the 896 is able to perform 64 bit data transfers.
4769 **
4770 ** We use log base 2 (burst length) as internal code, with
4771 ** value 0 meaning "burst disabled".
4772 **
4773 **===============================================================
4774 */
4775
4776 /*
4777 * Burst length from burst code.
4778 */
4779 #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
4780
4781 /*
4782 * Burst code from io register bits.
4783 */
4784 #define burst_code(dmode, ctest4, ctest5) \
4785 (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
4786
4787 /*
4788 * Set initial io register bits from burst code.
4789 */
4790 static inline void ncr_init_burst(ncb_p np, u_char bc)
4791 {
4792 np->rv_ctest4 &= ~0x80;
4793 np->rv_dmode &= ~(0x3 << 6);
4794 np->rv_ctest5 &= ~0x4;
4795
4796 if (!bc) {
4797 np->rv_ctest4 |= 0x80;
4798 }
4799 else {
4800 --bc;
4801 np->rv_dmode |= ((bc & 0x3) << 6);
4802 np->rv_ctest5 |= (bc & 0x4);
4803 }
4804 }
4805
4806 #ifdef SCSI_NCR_NVRAM_SUPPORT
4807
4808 /*
4809 ** Get target set-up from Symbios format NVRAM.
4810 */
4811
4812 static void __init
4813 ncr_Symbios_setup_target(ncb_p np, int target, Symbios_nvram *nvram)
4814 {
4815 tcb_p tp = &np->target[target];
4816 Symbios_target *tn = &nvram->target[target];
4817
4818 tp->usrsync = tn->sync_period ? (tn->sync_period + 3) / 4 : 255;
4819 tp->usrwide = tn->bus_width == 0x10 ? 1 : 0;
4820 tp->usrtags =
4821 (tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? MAX_TAGS : 0;
4822
4823 if (!(tn->flags & SYMBIOS_DISCONNECT_ENABLE))
4824 tp->usrflag |= UF_NODISC;
4825 if (!(tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME))
4826 tp->usrflag |= UF_NOSCAN;
4827 }
4828
4829 /*
4830 ** Get target set-up from Tekram format NVRAM.
4831 */
4832
4833 static void __init
4834 ncr_Tekram_setup_target(ncb_p np, int target, Tekram_nvram *nvram)
4835 {
4836 tcb_p tp = &np->target[target];
4837 struct Tekram_target *tn = &nvram->target[target];
4838 int i;
4839
4840 if (tn->flags & TEKRAM_SYNC_NEGO) {
4841 i = tn->sync_index & 0xf;
4842 tp->usrsync = Tekram_sync[i];
4843 }
4844
4845 tp->usrwide = (tn->flags & TEKRAM_WIDE_NEGO) ? 1 : 0;
4846
4847 if (tn->flags & TEKRAM_TAGGED_COMMANDS) {
4848 tp->usrtags = 2 << nvram->max_tags_index;
4849 }
4850
4851 if (!(tn->flags & TEKRAM_DISCONNECT_ENABLE))
4852 tp->usrflag = UF_NODISC;
4853
4854 /* If any device does not support parity, we will not use this option */
4855 if (!(tn->flags & TEKRAM_PARITY_CHECK))
4856 np->rv_scntl0 &= ~0x0a; /* SCSI parity checking disabled */
4857 }
4858 #endif /* SCSI_NCR_NVRAM_SUPPORT */
4859
4860 /*
4861 ** Save initial settings of some IO registers.
4862 ** Assumed to have been set by BIOS.
4863 */
4864 static void __init ncr_save_initial_setting(ncb_p np)
4865 {
4866 np->sv_scntl0 = INB(nc_scntl0) & 0x0a;
4867 np->sv_dmode = INB(nc_dmode) & 0xce;
4868 np->sv_dcntl = INB(nc_dcntl) & 0xa8;
4869 np->sv_ctest3 = INB(nc_ctest3) & 0x01;
4870 np->sv_ctest4 = INB(nc_ctest4) & 0x80;
4871 np->sv_gpcntl = INB(nc_gpcntl);
4872 np->sv_stest2 = INB(nc_stest2) & 0x20;
4873 np->sv_stest4 = INB(nc_stest4);
4874 np->sv_stest1 = INB(nc_stest1);
4875
4876 np->sv_scntl3 = INB(nc_scntl3) & 0x07;
4877
4878 if ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
4879 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66) ){
4880 /*
4881 ** C1010 always uses large fifo, bit 5 rsvd
4882 ** scntl4 used ONLY with C1010
4883 */
4884 np->sv_ctest5 = INB(nc_ctest5) & 0x04 ;
4885 np->sv_scntl4 = INB(nc_scntl4);
4886 }
4887 else {
4888 np->sv_ctest5 = INB(nc_ctest5) & 0x24 ;
4889 np->sv_scntl4 = 0;
4890 }
4891 }
4892
4893 /*
4894 ** Prepare io register values used by ncr_init()
4895 ** according to selected and supported features.
4896 */
4897 static int __init ncr_prepare_setting(ncb_p np, ncr_nvram *nvram)
4898 {
4899 u_char burst_max;
4900 u_long period;
4901 int i;
4902
4903 #ifdef CONFIG_PARISC
4904 char scsi_mode = -1;
4905 struct hardware_path hwpath;
4906 #endif
4907
4908 /*
4909 ** Wide ?
4910 */
4911
4912 np->maxwide = (np->features & FE_WIDE)? 1 : 0;
4913
4914 /*
4915 * Guess the frequency of the chip's clock.
4916 */
4917 if (np->features & (FE_ULTRA3 | FE_ULTRA2))
4918 np->clock_khz = 160000;
4919 else if (np->features & FE_ULTRA)
4920 np->clock_khz = 80000;
4921 else
4922 np->clock_khz = 40000;
4923
4924 /*
4925 * Get the clock multiplier factor.
4926 */
4927 if (np->features & FE_QUAD)
4928 np->multiplier = 4;
4929 else if (np->features & FE_DBLR)
4930 np->multiplier = 2;
4931 else
4932 np->multiplier = 1;
4933
4934 /*
4935 * Measure SCSI clock frequency for chips
4936 * it may vary from assumed one.
4937 */
4938 if (np->features & FE_VARCLK)
4939 ncr_getclock(np, np->multiplier);
4940
4941 /*
4942 * Divisor to be used for async (timer pre-scaler).
4943 *
4944 * Note: For C1010 the async divisor is 2(8) if he
4945 * quadrupler is disabled (enabled).
4946 */
4947
4948 if ( (np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
4949 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66)) {
4950
4951 np->rv_scntl3 = 0;
4952 }
4953 else
4954 {
4955 i = np->clock_divn - 1;
4956 while (--i >= 0) {
4957 if (10ul * SCSI_NCR_MIN_ASYNC * np->clock_khz
4958 > div_10M[i]) {
4959 ++i;
4960 break;
4961 }
4962 }
4963 np->rv_scntl3 = i+1;
4964 }
4965
4966
4967 /*
4968 * Save the ultra3 register for the C1010/C1010_66
4969 */
4970
4971 np->rv_scntl4 = np->sv_scntl4;
4972
4973 /*
4974 * Minimum synchronous period factor supported by the chip.
4975 * Btw, 'period' is in tenths of nanoseconds.
4976 */
4977
4978 period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
4979
4980 #ifdef CONFIG_PARISC
4981 /* Host firmware (PDC) keeps a table for crippling SCSI capabilities.
4982 * Many newer machines export one channel of 53c896 chip
4983 * as SE, 50-pin HD. Also used for Multi-initiator SCSI clusters
4984 * to set the SCSI Initiator ID.
4985 */
4986 get_pci_node_path(np->pdev, &hwpath);
4987 if (pdc_get_initiator(&hwpath, &np->myaddr, &period, &np->maxwide, &scsi_mode))
4988 {
4989 if (np->maxwide)
4990 np->features |= FE_WIDE;
4991 if (scsi_mode >= 0) {
4992 /* C3000 PDC reports period/mode */
4993 driver_setup.diff_support = 0;
4994 switch(scsi_mode) {
4995 case 0: np->scsi_mode = SMODE_SE; break;
4996 case 1: np->scsi_mode = SMODE_HVD; break;
4997 case 2: np->scsi_mode = SMODE_LVD; break;
4998 default: break;
4999 }
5000 }
5001 }
5002 #endif
5003
5004 if (period <= 250) np->minsync = 10;
5005 else if (period <= 303) np->minsync = 11;
5006 else if (period <= 500) np->minsync = 12;
5007 else np->minsync = (period + 40 - 1) / 40;
5008
5009 /*
5010 * Fix up. If sync. factor is 10 (160000Khz clock) and chip
5011 * supports ultra3, then min. sync. period 12.5ns and the factor is 9
5012 * Also keep track of the maximum offset in ST mode which may differ
5013 * from the maximum offset in DT mode. For now hardcoded to 31.
5014 */
5015
5016 if (np->features & FE_ULTRA3) {
5017 if (np->minsync == 10)
5018 np->minsync = 9;
5019 np->maxoffs_st = 31;
5020 }
5021 else
5022 np->maxoffs_st = np->maxoffs;
5023
5024 /*
5025 * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
5026 *
5027 * Transfer period minimums: SCSI-1 200 (50); Fast 100 (25)
5028 * Ultra 50 (12); Ultra2 (6); Ultra3 (3)
5029 */
5030
5031 if (np->minsync < 25 && !(np->features & (FE_ULTRA|FE_ULTRA2|FE_ULTRA3)))
5032 np->minsync = 25;
5033 else if (np->minsync < 12 && (np->features & FE_ULTRA))
5034 np->minsync = 12;
5035 else if (np->minsync < 10 && (np->features & FE_ULTRA2))
5036 np->minsync = 10;
5037 else if (np->minsync < 9 && (np->features & FE_ULTRA3))
5038 np->minsync = 9;
5039
5040 /*
5041 * Maximum synchronous period factor supported by the chip.
5042 */
5043
5044 period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
5045 np->maxsync = period > 2540 ? 254 : period / 10;
5046
5047 /*
5048 ** 64 bit (53C895A or 53C896) ?
5049 */
5050 if (np->features & FE_DAC) {
5051 if (np->features & FE_DAC_IN_USE)
5052 np->rv_ccntl1 |= (XTIMOD | EXTIBMV);
5053 else
5054 np->rv_ccntl1 |= (DDAC);
5055 }
5056
5057 /*
5058 ** Phase mismatch handled by SCRIPTS (53C895A, 53C896 or C1010) ?
5059 */
5060 if (np->features & FE_NOPM)
5061 np->rv_ccntl0 |= (ENPMJ);
5062
5063 /*
5064 ** Prepare initial value of other IO registers
5065 */
5066 #if defined SCSI_NCR_TRUST_BIOS_SETTING
5067 np->rv_scntl0 = np->sv_scntl0;
5068 np->rv_dmode = np->sv_dmode;
5069 np->rv_dcntl = np->sv_dcntl;
5070 np->rv_ctest3 = np->sv_ctest3;
5071 np->rv_ctest4 = np->sv_ctest4;
5072 np->rv_ctest5 = np->sv_ctest5;
5073 burst_max = burst_code(np->sv_dmode, np->sv_ctest4, np->sv_ctest5);
5074 #else
5075
5076 /*
5077 ** Select burst length (dwords)
5078 */
5079 burst_max = driver_setup.burst_max;
5080 if (burst_max == 255)
5081 burst_max = burst_code(np->sv_dmode, np->sv_ctest4, np->sv_ctest5);
5082 if (burst_max > 7)
5083 burst_max = 7;
5084 if (burst_max > np->maxburst)
5085 burst_max = np->maxburst;
5086
5087 /*
5088 ** DEL 352 - 53C810 Rev x11 - Part Number 609-0392140 - ITEM 2.
5089 ** This chip and the 860 Rev 1 may wrongly use PCI cache line
5090 ** based transactions on LOAD/STORE instructions. So we have
5091 ** to prevent these chips from using such PCI transactions in
5092 ** this driver. The generic sym53c8xx driver that does not use
5093 ** LOAD/STORE instructions does not need this work-around.
5094 */
5095 if ((np->device_id == PCI_DEVICE_ID_NCR_53C810 &&
5096 np->revision_id >= 0x10 && np->revision_id <= 0x11) ||
5097 (np->device_id == PCI_DEVICE_ID_NCR_53C860 &&
5098 np->revision_id <= 0x1))
5099 np->features &= ~(FE_WRIE|FE_ERL|FE_ERMP);
5100
5101 /*
5102 ** DEL ? - 53C1010 Rev 1 - Part Number 609-0393638
5103 ** 64-bit Slave Cycles must be disabled.
5104 */
5105 if ( ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) && (np->revision_id < 0x02) )
5106 || (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66 ) )
5107 np->rv_ccntl1 |= 0x10;
5108
5109 /*
5110 ** Select all supported special features.
5111 ** If we are using on-board RAM for scripts, prefetch (PFEN)
5112 ** does not help, but burst op fetch (BOF) does.
5113 ** Disabling PFEN makes sure BOF will be used.
5114 */
5115 if (np->features & FE_ERL)
5116 np->rv_dmode |= ERL; /* Enable Read Line */
5117 if (np->features & FE_BOF)
5118 np->rv_dmode |= BOF; /* Burst Opcode Fetch */
5119 if (np->features & FE_ERMP)
5120 np->rv_dmode |= ERMP; /* Enable Read Multiple */
5121 #if 1
5122 if ((np->features & FE_PFEN) && !np->base2_ba)
5123 #else
5124 if (np->features & FE_PFEN)
5125 #endif
5126 np->rv_dcntl |= PFEN; /* Prefetch Enable */
5127 if (np->features & FE_CLSE)
5128 np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
5129 if (np->features & FE_WRIE)
5130 np->rv_ctest3 |= WRIE; /* Write and Invalidate */
5131
5132
5133 if ( (np->device_id != PCI_DEVICE_ID_LSI_53C1010) &&
5134 (np->device_id != PCI_DEVICE_ID_LSI_53C1010_66) &&
5135 (np->features & FE_DFS))
5136 np->rv_ctest5 |= DFS; /* Dma Fifo Size */
5137 /* C1010/C1010_66 always large fifo */
5138
5139 /*
5140 ** Select some other
5141 */
5142 if (driver_setup.master_parity)
5143 np->rv_ctest4 |= MPEE; /* Master parity checking */
5144 if (driver_setup.scsi_parity)
5145 np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
5146
5147 #ifdef SCSI_NCR_NVRAM_SUPPORT
5148 /*
5149 ** Get parity checking, host ID and verbose mode from NVRAM
5150 **/
5151 if (nvram) {
5152 switch(nvram->type) {
5153 case SCSI_NCR_TEKRAM_NVRAM:
5154 np->myaddr = nvram->data.Tekram.host_id & 0x0f;
5155 break;
5156 case SCSI_NCR_SYMBIOS_NVRAM:
5157 if (!(nvram->data.Symbios.flags & SYMBIOS_PARITY_ENABLE))
5158 np->rv_scntl0 &= ~0x0a;
5159 np->myaddr = nvram->data.Symbios.host_id & 0x0f;
5160 if (nvram->data.Symbios.flags & SYMBIOS_VERBOSE_MSGS)
5161 np->verbose += 1;
5162 break;
5163 }
5164 }
5165 #endif
5166 /*
5167 ** Get SCSI addr of host adapter (set by bios?).
5168 */
5169 if (np->myaddr == 255) {
5170 np->myaddr = INB(nc_scid) & 0x07;
5171 if (!np->myaddr)
5172 np->myaddr = SCSI_NCR_MYADDR;
5173 }
5174
5175 #endif /* SCSI_NCR_TRUST_BIOS_SETTING */
5176
5177 /*
5178 * Prepare initial io register bits for burst length
5179 */
5180 ncr_init_burst(np, burst_max);
5181
5182 /*
5183 ** Set SCSI BUS mode.
5184 **
5185 ** - ULTRA2 chips (895/895A/896)
5186 ** and ULTRA 3 chips (1010) report the current
5187 ** BUS mode through the STEST4 IO register.
5188 ** - For previous generation chips (825/825A/875),
5189 ** user has to tell us how to check against HVD,
5190 ** since a 100% safe algorithm is not possible.
5191 */
5192 np->scsi_mode = SMODE_SE;
5193 if (np->features & (FE_ULTRA2 | FE_ULTRA3))
5194 np->scsi_mode = (np->sv_stest4 & SMODE);
5195 else if (np->features & FE_DIFF) {
5196 switch(driver_setup.diff_support) {
5197 case 4: /* Trust previous settings if present, then GPIO3 */
5198 if (np->sv_scntl3) {
5199 if (np->sv_stest2 & 0x20)
5200 np->scsi_mode = SMODE_HVD;
5201 break;
5202 }
5203 case 3: /* SYMBIOS controllers report HVD through GPIO3 */
5204 if (nvram && nvram->type != SCSI_NCR_SYMBIOS_NVRAM)
5205 break;
5206 if (INB(nc_gpreg) & 0x08)
5207 break;
5208 case 2: /* Set HVD unconditionally */
5209 np->scsi_mode = SMODE_HVD;
5210 case 1: /* Trust previous settings for HVD */
5211 if (np->sv_stest2 & 0x20)
5212 np->scsi_mode = SMODE_HVD;
5213 break;
5214 default:/* Don't care about HVD */
5215 break;
5216 }
5217 }
5218 if (np->scsi_mode == SMODE_HVD)
5219 np->rv_stest2 |= 0x20;
5220
5221 /*
5222 ** Set LED support from SCRIPTS.
5223 ** Ignore this feature for boards known to use a
5224 ** specific GPIO wiring and for the 895A or 896
5225 ** that drive the LED directly.
5226 ** Also probe initial setting of GPIO0 as output.
5227 */
5228 if ((driver_setup.led_pin ||
5229 (nvram && nvram->type == SCSI_NCR_SYMBIOS_NVRAM)) &&
5230 !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
5231 np->features |= FE_LED0;
5232
5233 /*
5234 ** Set irq mode.
5235 */
5236 switch(driver_setup.irqm & 3) {
5237 case 2:
5238 np->rv_dcntl |= IRQM;
5239 break;
5240 case 1:
5241 np->rv_dcntl |= (np->sv_dcntl & IRQM);
5242 break;
5243 default:
5244 break;
5245 }
5246
5247 /*
5248 ** Configure targets according to driver setup.
5249 ** If NVRAM present get targets setup from NVRAM.
5250 ** Allow to override sync, wide and NOSCAN from
5251 ** boot command line.
5252 */
5253 for (i = 0 ; i < MAX_TARGET ; i++) {
5254 tcb_p tp = &np->target[i];
5255
5256 tp->usrsync = 255;
5257 #ifdef SCSI_NCR_NVRAM_SUPPORT
5258 if (nvram) {
5259 switch(nvram->type) {
5260 case SCSI_NCR_TEKRAM_NVRAM:
5261 ncr_Tekram_setup_target(np, i, &nvram->data.Tekram);
5262 break;
5263 case SCSI_NCR_SYMBIOS_NVRAM:
5264 ncr_Symbios_setup_target(np, i, &nvram->data.Symbios);
5265 break;
5266 }
5267 if (driver_setup.use_nvram & 0x2)
5268 tp->usrsync = driver_setup.default_sync;
5269 if (driver_setup.use_nvram & 0x4)
5270 tp->usrwide = driver_setup.max_wide;
5271 if (driver_setup.use_nvram & 0x8)
5272 tp->usrflag &= ~UF_NOSCAN;
5273 }
5274 else {
5275 #else
5276 if (1) {
5277 #endif
5278 tp->usrsync = driver_setup.default_sync;
5279 tp->usrwide = driver_setup.max_wide;
5280 tp->usrtags = MAX_TAGS;
5281 if (!driver_setup.disconnection)
5282 np->target[i].usrflag = UF_NODISC;
5283 }
5284 }
5285
5286 /*
5287 ** Announce all that stuff to user.
5288 */
5289
5290 i = nvram ? nvram->type : 0;
5291 printk(KERN_INFO "%s: %sID %d, Fast-%d%s%s\n", ncr_name(np),
5292 i == SCSI_NCR_SYMBIOS_NVRAM ? "Symbios format NVRAM, " :
5293 (i == SCSI_NCR_TEKRAM_NVRAM ? "Tekram format NVRAM, " : ""),
5294 np->myaddr,
5295 np->minsync < 10 ? 80 :
5296 (np->minsync < 12 ? 40 : (np->minsync < 25 ? 20 : 10) ),
5297 (np->rv_scntl0 & 0xa) ? ", Parity Checking" : ", NO Parity",
5298 (np->rv_stest2 & 0x20) ? ", Differential" : "");
5299
5300 if (bootverbose > 1) {
5301 printk (KERN_INFO "%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
5302 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
5303 ncr_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
5304 np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
5305
5306 printk (KERN_INFO "%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
5307 "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
5308 ncr_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
5309 np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
5310 }
5311
5312 if (bootverbose && np->base2_ba)
5313 printk (KERN_INFO "%s: on-chip RAM at 0x%lx\n",
5314 ncr_name(np), np->base2_ba);
5315
5316 return 0;
5317 }
5318
5319
5320 #ifdef SCSI_NCR_DEBUG_NVRAM
5321
5322 void __init ncr_display_Symbios_nvram(ncb_p np, Symbios_nvram *nvram)
5323 {
5324 int i;
5325
5326 /* display Symbios nvram host data */
5327 printk(KERN_DEBUG "%s: HOST ID=%d%s%s%s%s%s\n",
5328 ncr_name(np), nvram->host_id & 0x0f,
5329 (nvram->flags & SYMBIOS_SCAM_ENABLE) ? " SCAM" :"",
5330 (nvram->flags & SYMBIOS_PARITY_ENABLE) ? " PARITY" :"",
5331 (nvram->flags & SYMBIOS_VERBOSE_MSGS) ? " VERBOSE" :"",
5332 (nvram->flags & SYMBIOS_CHS_MAPPING) ? " CHS_ALT" :"",
5333 (nvram->flags1 & SYMBIOS_SCAN_HI_LO) ? " HI_LO" :"");
5334
5335 /* display Symbios nvram drive data */
5336 for (i = 0 ; i < 15 ; i++) {
5337 struct Symbios_target *tn = &nvram->target[i];
5338 printk(KERN_DEBUG "%s-%d:%s%s%s%s WIDTH=%d SYNC=%d TMO=%d\n",
5339 ncr_name(np), i,
5340 (tn->flags & SYMBIOS_DISCONNECT_ENABLE) ? " DISC" : "",
5341 (tn->flags & SYMBIOS_SCAN_AT_BOOT_TIME) ? " SCAN_BOOT" : "",
5342 (tn->flags & SYMBIOS_SCAN_LUNS) ? " SCAN_LUNS" : "",
5343 (tn->flags & SYMBIOS_QUEUE_TAGS_ENABLED)? " TCQ" : "",
5344 tn->bus_width,
5345 tn->sync_period / 4,
5346 tn->timeout);
5347 }
5348 }
5349
5350 static u_char Tekram_boot_delay[7] __initdata = {3, 5, 10, 20, 30, 60, 120};
5351
5352 void __init ncr_display_Tekram_nvram(ncb_p np, Tekram_nvram *nvram)
5353 {
5354 int i, tags, boot_delay;
5355 char *rem;
5356
5357 /* display Tekram nvram host data */
5358 tags = 2 << nvram->max_tags_index;
5359 boot_delay = 0;
5360 if (nvram->boot_delay_index < 6)
5361 boot_delay = Tekram_boot_delay[nvram->boot_delay_index];
5362 switch((nvram->flags & TEKRAM_REMOVABLE_FLAGS) >> 6) {
5363 default:
5364 case 0: rem = ""; break;
5365 case 1: rem = " REMOVABLE=boot device"; break;
5366 case 2: rem = " REMOVABLE=all"; break;
5367 }
5368
5369 printk(KERN_DEBUG
5370 "%s: HOST ID=%d%s%s%s%s%s%s%s%s%s BOOT DELAY=%d tags=%d\n",
5371 ncr_name(np), nvram->host_id & 0x0f,
5372 (nvram->flags1 & SYMBIOS_SCAM_ENABLE) ? " SCAM" :"",
5373 (nvram->flags & TEKRAM_MORE_THAN_2_DRIVES) ? " >2DRIVES" :"",
5374 (nvram->flags & TEKRAM_DRIVES_SUP_1GB) ? " >1GB" :"",
5375 (nvram->flags & TEKRAM_RESET_ON_POWER_ON) ? " RESET" :"",
5376 (nvram->flags & TEKRAM_ACTIVE_NEGATION) ? " ACT_NEG" :"",
5377 (nvram->flags & TEKRAM_IMMEDIATE_SEEK) ? " IMM_SEEK" :"",
5378 (nvram->flags & TEKRAM_SCAN_LUNS) ? " SCAN_LUNS" :"",
5379 (nvram->flags1 & TEKRAM_F2_F6_ENABLED) ? " F2_F6" :"",
5380 rem, boot_delay, tags);
5381
5382 /* display Tekram nvram drive data */
5383 for (i = 0; i <= 15; i++) {
5384 int sync, j;
5385 struct Tekram_target *tn = &nvram->target[i];
5386 j = tn->sync_index & 0xf;
5387 sync = Tekram_sync[j];
5388 printk(KERN_DEBUG "%s-%d:%s%s%s%s%s%s PERIOD=%d\n",
5389 ncr_name(np), i,
5390 (tn->flags & TEKRAM_PARITY_CHECK) ? " PARITY" : "",
5391 (tn->flags & TEKRAM_SYNC_NEGO) ? " SYNC" : "",
5392 (tn->flags & TEKRAM_DISCONNECT_ENABLE) ? " DISC" : "",
5393 (tn->flags & TEKRAM_START_CMD) ? " START" : "",
5394 (tn->flags & TEKRAM_TAGGED_COMMANDS) ? " TCQ" : "",
5395 (tn->flags & TEKRAM_WIDE_NEGO) ? " WIDE" : "",
5396 sync);
5397 }
5398 }
5399 #endif /* SCSI_NCR_DEBUG_NVRAM */
5400
5401 /*
5402 ** Host attach and initialisations.
5403 **
5404 ** Allocate host data and ncb structure.
5405 ** Request IO region and remap MMIO region.
5406 ** Do chip initialization.
5407 ** If all is OK, install interrupt handling and
5408 ** start the timer daemon.
5409 */
5410
5411 static int __init
5412 ncr_attach (Scsi_Host_Template *tpnt, int unit, ncr_device *device)
5413 {
5414 struct host_data *host_data;
5415 ncb_p np = 0;
5416 struct Scsi_Host *instance = 0;
5417 u_long flags = 0;
5418 ncr_nvram *nvram = device->nvram;
5419 int i;
5420
5421 printk(KERN_INFO NAME53C "%s-%d: rev 0x%x on pci bus %d device %d function %d "
5422 #ifdef __sparc__
5423 "irq %s\n",
5424 #else
5425 "irq %d\n",
5426 #endif
5427 device->chip.name, unit, device->chip.revision_id,
5428 device->slot.bus, (device->slot.device_fn & 0xf8) >> 3,
5429 device->slot.device_fn & 7,
5430 #ifdef __sparc__
5431 __irq_itoa(device->slot.irq));
5432 #else
5433 device->slot.irq);
5434 #endif
5435
5436 /*
5437 ** Allocate host_data structure
5438 */
5439 if (!(instance = scsi_register(tpnt, sizeof(*host_data))))
5440 goto attach_error;
5441 host_data = (struct host_data *) instance->hostdata;
5442
5443 /*
5444 ** Allocate the host control block.
5445 */
5446 np = __m_calloc_dma(device->pdev, sizeof(struct ncb), "NCB");
5447 if (!np)
5448 goto attach_error;
5449 NCR_INIT_LOCK_NCB(np);
5450 np->pdev = device->pdev;
5451 np->p_ncb = vtobus(np);
5452 host_data->ncb = np;
5453
5454 /*
5455 ** Store input informations in the host data structure.
5456 */
5457 strncpy(np->chip_name, device->chip.name, sizeof(np->chip_name) - 1);
5458 np->unit = unit;
5459 np->verbose = driver_setup.verbose;
5460 sprintf(np->inst_name, NAME53C "%s-%d", np->chip_name, np->unit);
5461 np->device_id = device->chip.device_id;
5462 np->revision_id = device->chip.revision_id;
5463 np->bus = device->slot.bus;
5464 np->device_fn = device->slot.device_fn;
5465 np->features = device->chip.features;
5466 np->clock_divn = device->chip.nr_divisor;
5467 np->maxoffs = device->chip.offset_max;
5468 np->maxburst = device->chip.burst_max;
5469 np->myaddr = device->host_id;
5470
5471 /*
5472 ** Allocate the start queue.
5473 */
5474 np->squeue = (ncrcmd *)
5475 m_calloc_dma(sizeof(ncrcmd)*(MAX_START*2), "SQUEUE");
5476 if (!np->squeue)
5477 goto attach_error;
5478 np->p_squeue = vtobus(np->squeue);
5479
5480 /*
5481 ** Allocate the done queue.
5482 */
5483 np->dqueue = (ncrcmd *)
5484 m_calloc_dma(sizeof(ncrcmd)*(MAX_START*2), "DQUEUE");
5485 if (!np->dqueue)
5486 goto attach_error;
5487
5488 /*
5489 ** Allocate the target bus address array.
5490 */
5491 np->targtbl = (u_int32 *) m_calloc_dma(256, "TARGTBL");
5492 if (!np->targtbl)
5493 goto attach_error;
5494
5495 /*
5496 ** Allocate SCRIPTS areas
5497 */
5498 np->script0 = (struct script *)
5499 m_calloc_dma(sizeof(struct script), "SCRIPT");
5500 if (!np->script0)
5501 goto attach_error;
5502 np->scripth0 = (struct scripth *)
5503 m_calloc_dma(sizeof(struct scripth), "SCRIPTH");
5504 if (!np->scripth0)
5505 goto attach_error;
5506
5507 /*
5508 ** Initialyze the CCB free queue and,
5509 ** allocate some CCB. We need at least ONE.
5510 */
5511 xpt_que_init(&np->free_ccbq);
5512 xpt_que_init(&np->b0_ccbq);
5513 if (!ncr_alloc_ccb(np))
5514 goto attach_error;
5515
5516 /*
5517 ** Initialize timer structure
5518 **
5519 */
5520 init_timer(&np->timer);
5521 np->timer.data = (unsigned long) np;
5522 np->timer.function = sym53c8xx_timeout;
5523
5524 /*
5525 ** Try to map the controller chip to
5526 ** virtual and physical memory.
5527 */
5528
5529 np->base_ba = device->slot.base;
5530 np->base_ws = (np->features & FE_IO256)? 256 : 128;
5531 np->base2_ba = (np->features & FE_RAM)? device->slot.base_2 : 0;
5532
5533 #ifndef SCSI_NCR_IOMAPPED
5534 np->base_va = remap_pci_mem(device->slot.base_c, np->base_ws);
5535 if (!np->base_va) {
5536 printk(KERN_ERR "%s: can't map PCI MMIO region\n",ncr_name(np));
5537 goto attach_error;
5538 }
5539 else if (bootverbose > 1)
5540 printk(KERN_INFO "%s: using memory mapped IO\n", ncr_name(np));
5541
5542 /*
5543 ** Make the controller's registers available.
5544 ** Now the INB INW INL OUTB OUTW OUTL macros
5545 ** can be used safely.
5546 */
5547
5548 np->reg = (struct ncr_reg *) np->base_va;
5549
5550 #endif /* !defined SCSI_NCR_IOMAPPED */
5551
5552 /*
5553 ** If on-chip RAM is used, make sure SCRIPTS isn't too large.
5554 */
5555 if (np->base2_ba && sizeof(struct script) > 4096) {
5556 printk(KERN_ERR "%s: script too large.\n", ncr_name(np));
5557 goto attach_error;
5558 }
5559
5560 /*
5561 ** Try to map the controller chip into iospace.
5562 */
5563
5564 if (device->slot.io_port) {
5565 request_region(device->slot.io_port, np->base_ws, NAME53C8XX);
5566 np->base_io = device->slot.io_port;
5567 }
5568
5569 #ifdef SCSI_NCR_NVRAM_SUPPORT
5570 if (nvram) {
5571 switch(nvram->type) {
5572 case SCSI_NCR_SYMBIOS_NVRAM:
5573 #ifdef SCSI_NCR_DEBUG_NVRAM
5574 ncr_display_Symbios_nvram(np, &nvram->data.Symbios);
5575 #endif
5576 break;
5577 case SCSI_NCR_TEKRAM_NVRAM:
5578 #ifdef SCSI_NCR_DEBUG_NVRAM
5579 ncr_display_Tekram_nvram(np, &nvram->data.Tekram);
5580 #endif
5581 break;
5582 default:
5583 nvram = 0;
5584 #ifdef SCSI_NCR_DEBUG_NVRAM
5585 printk(KERN_DEBUG "%s: NVRAM: None or invalid data.\n", ncr_name(np));
5586 #endif
5587 }
5588 }
5589 #endif
5590
5591 /*
5592 ** Save setting of some IO registers, so we will
5593 ** be able to probe specific implementations.
5594 */
5595 ncr_save_initial_setting (np);
5596
5597 /*
5598 ** Reset the chip now, since it has been reported
5599 ** that SCSI clock calibration may not work properly
5600 ** if the chip is currently active.
5601 */
5602 ncr_chip_reset (np);
5603
5604 /*
5605 ** Do chip dependent initialization.
5606 */
5607 (void) ncr_prepare_setting(np, nvram);
5608
5609 /*
5610 ** Check the PCI clock frequency if needed.
5611 **
5612 ** Must be done after ncr_prepare_setting since it destroys
5613 ** STEST1 that is used to probe for the clock multiplier.
5614 **
5615 ** The range is currently [22688 - 45375 Khz], given
5616 ** the values used by ncr_getclock().
5617 ** This calibration of the frequecy measurement
5618 ** algorithm against the PCI clock frequency is only
5619 ** performed if the driver has had to measure the SCSI
5620 ** clock due to other heuristics not having been enough
5621 ** to deduce the SCSI clock frequency.
5622 **
5623 ** When the chip has been initialized correctly by the
5624 ** SCSI BIOS, the driver deduces the presence of the
5625 ** clock multiplier and the value of the SCSI clock from
5626 ** initial values of IO registers, and therefore no
5627 ** clock measurement is performed.
5628 ** Normally the driver should never have to measure any
5629 ** clock, unless the controller may use a 80 MHz clock
5630 ** or has a clock multiplier and any of the following
5631 ** condition is met:
5632 **
5633 ** - No SCSI BIOS is present.
5634 ** - SCSI BIOS did'nt enable the multiplier for some reason.
5635 ** - User has disabled the controller from the SCSI BIOS.
5636 ** - User booted the O/S from another O/S that did'nt enable
5637 ** the multiplier for some reason.
5638 **
5639 ** As a result, the driver may only have to measure some
5640 ** frequency in very unusual situations.
5641 **
5642 ** For this reality test against the PCI clock to really
5643 ** protect against flaws in the udelay() calibration or
5644 ** driver problem that affect the clock measurement
5645 ** algorithm, the actual PCI clock frequency must be 33 MHz.
5646 */
5647 i = np->pciclock_max ? ncr_getpciclock(np) : 0;
5648 if (i && (i < np->pciclock_min || i > np->pciclock_max)) {
5649 printk(KERN_ERR "%s: PCI clock (%u KHz) is out of range "
5650 "[%u KHz - %u KHz].\n",
5651 ncr_name(np), i, np->pciclock_min, np->pciclock_max);
5652 goto attach_error;
5653 }
5654
5655 /*
5656 ** Patch script to physical addresses
5657 */
5658 ncr_script_fill (&script0, &scripth0);
5659
5660 np->p_script = vtobus(np->script0);
5661 np->p_scripth = vtobus(np->scripth0);
5662 np->p_scripth0 = np->p_scripth;
5663
5664 if (np->base2_ba) {
5665 np->p_script = np->base2_ba;
5666 if (np->features & FE_RAM8K) {
5667 np->base2_ws = 8192;
5668 np->p_scripth = np->p_script + 4096;
5669 #if BITS_PER_LONG > 32
5670 np->scr_ram_seg = cpu_to_scr(np->base2_ba >> 32);
5671 #endif
5672 }
5673 else
5674 np->base2_ws = 4096;
5675 #ifndef SCSI_NCR_PCI_MEM_NOT_SUPPORTED
5676 np->base2_va =
5677 remap_pci_mem(device->slot.base_2_c, np->base2_ws);
5678 if (!np->base2_va) {
5679 printk(KERN_ERR "%s: can't map PCI MEMORY region\n",
5680 ncr_name(np));
5681 goto attach_error;
5682 }
5683 #endif
5684 }
5685
5686 ncr_script_copy_and_bind (np, (ncrcmd *) &script0, (ncrcmd *) np->script0, sizeof(struct script));
5687 ncr_script_copy_and_bind (np, (ncrcmd *) &scripth0, (ncrcmd *) np->scripth0, sizeof(struct scripth));
5688
5689 /*
5690 ** Patch some variables in SCRIPTS
5691 */
5692 np->scripth0->pm0_data_addr[0] =
5693 cpu_to_scr(NCB_SCRIPT_PHYS(np, pm0_data));
5694 np->scripth0->pm1_data_addr[0] =
5695 cpu_to_scr(NCB_SCRIPT_PHYS(np, pm1_data));
5696
5697 /*
5698 ** Patch if not Ultra 3 - Do not write to scntl4
5699 */
5700 if (np->features & FE_ULTRA3) {
5701 np->script0->resel_scntl4[0] = cpu_to_scr(SCR_LOAD_REL (scntl4, 1));
5702 np->script0->resel_scntl4[1] = cpu_to_scr(offsetof(struct tcb, uval));
5703 }
5704
5705
5706 #ifdef SCSI_NCR_PCI_MEM_NOT_SUPPORTED
5707 np->scripth0->script0_ba[0] = cpu_to_scr(vtobus(np->script0));
5708 np->scripth0->script0_ba64[0] = cpu_to_scr(vtobus(np->script0));
5709 np->scripth0->scripth0_ba64[0] = cpu_to_scr(vtobus(np->scripth0));
5710 np->scripth0->ram_seg64[0] = np->scr_ram_seg;
5711 #endif
5712 /*
5713 ** Prepare the idle and invalid task actions.
5714 */
5715 np->idletask.start = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
5716 np->idletask.restart = cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_i_t_l));
5717 np->p_idletask = NCB_PHYS(np, idletask);
5718
5719 np->notask.start = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
5720 np->notask.restart = cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_i_t_l));
5721 np->p_notask = NCB_PHYS(np, notask);
5722
5723 np->bad_i_t_l.start = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
5724 np->bad_i_t_l.restart = cpu_to_scr(NCB_SCRIPTH_PHYS (np, bad_i_t_l));
5725 np->p_bad_i_t_l = NCB_PHYS(np, bad_i_t_l);
5726
5727 np->bad_i_t_l_q.start = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
5728 np->bad_i_t_l_q.restart = cpu_to_scr(NCB_SCRIPTH_PHYS (np,bad_i_t_l_q));
5729 np->p_bad_i_t_l_q = NCB_PHYS(np, bad_i_t_l_q);
5730
5731 /*
5732 ** Allocate and prepare the bad lun table.
5733 */
5734 np->badluntbl = m_calloc_dma(256, "BADLUNTBL");
5735 if (!np->badluntbl)
5736 goto attach_error;
5737
5738 assert (offsetof(struct lcb, resel_task) == 0);
5739 np->resel_badlun = cpu_to_scr(NCB_SCRIPTH_PHYS(np, resel_bad_lun));
5740
5741 for (i = 0 ; i < 64 ; i++)
5742 np->badluntbl[i] = cpu_to_scr(NCB_PHYS(np, resel_badlun));
5743
5744 /*
5745 ** Prepare the target bus address array.
5746 */
5747 np->scripth0->targtbl[0] = cpu_to_scr(vtobus(np->targtbl));
5748 for (i = 0 ; i < MAX_TARGET ; i++) {
5749 np->targtbl[i] = cpu_to_scr(NCB_PHYS(np, target[i]));
5750 np->target[i].b_luntbl = cpu_to_scr(vtobus(np->badluntbl));
5751 np->target[i].b_lun0 = cpu_to_scr(NCB_PHYS(np, resel_badlun));
5752 }
5753
5754 /*
5755 ** Patch the script for LED support.
5756 */
5757
5758 if (np->features & FE_LED0) {
5759 np->script0->idle[0] =
5760 cpu_to_scr(SCR_REG_REG(gpreg, SCR_OR, 0x01));
5761 np->script0->reselected[0] =
5762 cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
5763 np->script0->start[0] =
5764 cpu_to_scr(SCR_REG_REG(gpreg, SCR_AND, 0xfe));
5765 }
5766
5767 /*
5768 ** Patch the script to provide an extra clock cycle on
5769 ** data out phase - 53C1010_66MHz part only.
5770 ** (Fixed in rev. 1 of the chip)
5771 */
5772 if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66 &&
5773 np->revision_id < 1){
5774 np->script0->datao_phase[0] =
5775 cpu_to_scr(SCR_REG_REG(scntl4, SCR_OR, 0x0c));
5776 }
5777
5778 #ifdef SCSI_NCR_IARB_SUPPORT
5779 /*
5780 ** If user does not want to use IMMEDIATE ARBITRATION
5781 ** when we are reselected while attempting to arbitrate,
5782 ** patch the SCRIPTS accordingly with a SCRIPT NO_OP.
5783 */
5784 if (!(driver_setup.iarb & 1))
5785 np->script0->ungetjob[0] = cpu_to_scr(SCR_NO_OP);
5786 /*
5787 ** If user wants IARB to be set when we win arbitration
5788 ** and have other jobs, compute the max number of consecutive
5789 ** settings of IARB hint before we leave devices a chance to
5790 ** arbitrate for reselection.
5791 */
5792 np->iarb_max = (driver_setup.iarb >> 4);
5793 #endif
5794
5795 /*
5796 ** DEL 472 - 53C896 Rev 1 - Part Number 609-0393055 - ITEM 5.
5797 */
5798 if (np->device_id == PCI_DEVICE_ID_NCR_53C896 &&
5799 np->revision_id <= 0x1 && (np->features & FE_NOPM)) {
5800 np->scatter = ncr_scatter_896R1;
5801 np->script0->datai_phase[0] = cpu_to_scr(SCR_JUMP);
5802 np->script0->datai_phase[1] =
5803 cpu_to_scr(NCB_SCRIPTH_PHYS (np, tweak_pmj));
5804 np->script0->datao_phase[0] = cpu_to_scr(SCR_JUMP);
5805 np->script0->datao_phase[1] =
5806 cpu_to_scr(NCB_SCRIPTH_PHYS (np, tweak_pmj));
5807 }
5808 else
5809 #ifdef DEBUG_896R1
5810 np->scatter = ncr_scatter_896R1;
5811 #else
5812 np->scatter = ncr_scatter;
5813 #endif
5814
5815 /*
5816 ** Reset chip.
5817 ** We should use ncr_soft_reset(), but we donnot want to do
5818 ** so, since we may not be safe if ABRT interrupt occurs due
5819 ** to the BIOS or previous O/S having enable this interrupt.
5820 **
5821 ** For C1010 need to set ABRT bit prior to SRST if SCRIPTs
5822 ** are running. Not true in this case.
5823 */
5824 ncr_chip_reset(np);
5825
5826 /*
5827 ** Now check the cache handling of the pci chipset.
5828 */
5829
5830 if (ncr_snooptest (np)) {
5831 printk (KERN_ERR "CACHE INCORRECTLY CONFIGURED.\n");
5832 goto attach_error;
5833 };
5834
5835 /*
5836 ** Install the interrupt handler.
5837 ** If we synchonize the C code with SCRIPTS on interrupt,
5838 ** we donnot want to share the INTR line at all.
5839 */
5840 if (request_irq(device->slot.irq, sym53c8xx_intr,
5841 #ifdef SCSI_NCR_PCIQ_SYNC_ON_INTR
5842 ((driver_setup.irqm & 0x20) ? 0 : SA_INTERRUPT),
5843 #else
5844 ((driver_setup.irqm & 0x10) ? 0 : SA_SHIRQ) |
5845 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,2,0)
5846 ((driver_setup.irqm & 0x20) ? 0 : SA_INTERRUPT),
5847 #else
5848 0,
5849 #endif
5850 #endif
5851 NAME53C8XX, np)) {
5852 printk(KERN_ERR "%s: request irq %d failure\n",
5853 ncr_name(np), device->slot.irq);
5854 goto attach_error;
5855 }
5856 np->irq = device->slot.irq;
5857
5858 /*
5859 ** After SCSI devices have been opened, we cannot
5860 ** reset the bus safely, so we do it here.
5861 ** Interrupt handler does the real work.
5862 ** Process the reset exception,
5863 ** if interrupts are not enabled yet.
5864 ** Then enable disconnects.
5865 */
5866 NCR_LOCK_NCB(np, flags);
5867 if (ncr_reset_scsi_bus(np, 0, driver_setup.settle_delay) != 0) {
5868 printk(KERN_ERR "%s: FATAL ERROR: CHECK SCSI BUS - CABLES, TERMINATION, DEVICE POWER etc.!\n", ncr_name(np));
5869
5870 NCR_UNLOCK_NCB(np, flags);
5871 goto attach_error;
5872 }
5873 ncr_exception (np);
5874
5875 /*
5876 ** The middle-level SCSI driver does not
5877 ** wait for devices to settle.
5878 ** Wait synchronously if more than 2 seconds.
5879 */
5880 if (driver_setup.settle_delay > 2) {
5881 printk(KERN_INFO "%s: waiting %d seconds for scsi devices to settle...\n",
5882 ncr_name(np), driver_setup.settle_delay);
5883 MDELAY (1000 * driver_setup.settle_delay);
5884 }
5885
5886 /*
5887 ** start the timeout daemon
5888 */
5889 np->lasttime=0;
5890 ncr_timeout (np);
5891
5892 /*
5893 ** use SIMPLE TAG messages by default
5894 */
5895 #ifdef SCSI_NCR_ALWAYS_SIMPLE_TAG
5896 np->order = M_SIMPLE_TAG;
5897 #endif
5898
5899 /*
5900 ** Done.
5901 ** Fill Linux host instance structure
5902 ** and return success.
5903 */
5904 instance->max_channel = 0;
5905 instance->this_id = np->myaddr;
5906 instance->max_id = np->maxwide ? 16 : 8;
5907 instance->max_lun = MAX_LUN;
5908 #ifndef SCSI_NCR_IOMAPPED
5909 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,29)
5910 instance->base = (unsigned long) np->reg;
5911 #else
5912 instance->base = (char *) np->reg;
5913 #endif
5914 #endif
5915 instance->irq = np->irq;
5916 instance->unique_id = np->base_io;
5917 instance->io_port = np->base_io;
5918 instance->n_io_port = np->base_ws;
5919 instance->dma_channel = 0;
5920 instance->cmd_per_lun = MAX_TAGS;
5921 instance->can_queue = (MAX_START-4);
5922 scsi_set_device(instance, &device->pdev->dev);
5923
5924 np->check_integrity = 0;
5925
5926 #ifdef SCSI_NCR_INTEGRITY_CHECKING
5927 instance->check_integrity = 0;
5928
5929 #ifdef SCSI_NCR_ENABLE_INTEGRITY_CHECK
5930 if ( !(driver_setup.bus_check & 0x04) ) {
5931 np->check_integrity = 1;
5932 instance->check_integrity = 1;
5933 }
5934 #endif
5935 #endif
5936
5937 NCR_UNLOCK_NCB(np, flags);
5938
5939 /*
5940 ** Now let the generic SCSI driver
5941 ** look for the SCSI devices on the bus ..
5942 */
5943 return 0;
5944
5945 attach_error:
5946 if (!instance) return -1;
5947 printk(KERN_INFO "%s: giving up ...\n", ncr_name(np));
5948 if (np)
5949 ncr_free_resources(np);
5950 scsi_unregister(instance);
5951
5952 return -1;
5953 }
5954
5955
5956 /*
5957 ** Free controller resources.
5958 */
5959 static void ncr_free_resources(ncb_p np)
5960 {
5961 ccb_p cp;
5962 tcb_p tp;
5963 lcb_p lp;
5964 int target, lun;
5965
5966 if (np->irq)
5967 free_irq(np->irq, np);
5968 if (np->base_io)
5969 release_region(np->base_io, np->base_ws);
5970 #ifndef SCSI_NCR_PCI_MEM_NOT_SUPPORTED
5971 if (np->base_va)
5972 unmap_pci_mem(np->base_va, np->base_ws);
5973 if (np->base2_va)
5974 unmap_pci_mem(np->base2_va, np->base2_ws);
5975 #endif
5976 if (np->scripth0)
5977 m_free_dma(np->scripth0, sizeof(struct scripth), "SCRIPTH");
5978 if (np->script0)
5979 m_free_dma(np->script0, sizeof(struct script), "SCRIPT");
5980 if (np->squeue)
5981 m_free_dma(np->squeue, sizeof(ncrcmd)*(MAX_START*2), "SQUEUE");
5982 if (np->dqueue)
5983 m_free_dma(np->dqueue, sizeof(ncrcmd)*(MAX_START*2),"DQUEUE");
5984
5985 while ((cp = np->ccbc) != NULL) {
5986 np->ccbc = cp->link_ccb;
5987 m_free_dma(cp, sizeof(*cp), "CCB");
5988 }
5989
5990 if (np->badluntbl)
5991 m_free_dma(np->badluntbl, 256,"BADLUNTBL");
5992
5993 for (target = 0; target < MAX_TARGET ; target++) {
5994 tp = &np->target[target];
5995 for (lun = 0 ; lun < MAX_LUN ; lun++) {
5996 lp = ncr_lp(np, tp, lun);
5997 if (!lp)
5998 continue;
5999 if (lp->tasktbl != &lp->tasktbl_0)
6000 m_free_dma(lp->tasktbl, MAX_TASKS*4, "TASKTBL");
6001 if (lp->cb_tags)
6002 m_free(lp->cb_tags, MAX_TAGS, "CB_TAGS");
6003 m_free_dma(lp, sizeof(*lp), "LCB");
6004 }
6005 #if MAX_LUN > 1
6006 if (tp->lmp)
6007 m_free(tp->lmp, MAX_LUN * sizeof(lcb_p), "LMP");
6008 if (tp->luntbl)
6009 m_free_dma(tp->luntbl, 256, "LUNTBL");
6010 #endif
6011 }
6012
6013 if (np->targtbl)
6014 m_free_dma(np->targtbl, 256, "TARGTBL");
6015
6016 m_free_dma(np, sizeof(*np), "NCB");
6017 }
6018
6019
6020 /*==========================================================
6021 **
6022 **
6023 ** Done SCSI commands list management.
6024 **
6025 ** We donnot enter the scsi_done() callback immediately
6026 ** after a command has been seen as completed but we
6027 ** insert it into a list which is flushed outside any kind
6028 ** of driver critical section.
6029 ** This allows to do minimal stuff under interrupt and
6030 ** inside critical sections and to also avoid locking up
6031 ** on recursive calls to driver entry points under SMP.
6032 ** In fact, the only kernel point which is entered by the
6033 ** driver with a driver lock set is get_free_pages(GFP_ATOMIC...)
6034 ** that shall not reenter the driver under any circumstance.
6035 **
6036 **==========================================================
6037 */
6038 static inline void ncr_queue_done_cmd(ncb_p np, Scsi_Cmnd *cmd)
6039 {
6040 unmap_scsi_data(np, cmd);
6041 cmd->host_scribble = (char *) np->done_list;
6042 np->done_list = cmd;
6043 }
6044
6045 static inline void ncr_flush_done_cmds(Scsi_Cmnd *lcmd)
6046 {
6047 Scsi_Cmnd *cmd;
6048
6049 while (lcmd) {
6050 cmd = lcmd;
6051 lcmd = (Scsi_Cmnd *) cmd->host_scribble;
6052 cmd->scsi_done(cmd);
6053 }
6054 }
6055
6056 /*==========================================================
6057 **
6058 **
6059 ** Prepare the next negotiation message for integrity check,
6060 ** if needed.
6061 **
6062 ** Fill in the part of message buffer that contains the
6063 ** negotiation and the nego_status field of the CCB.
6064 ** Returns the size of the message in bytes.
6065 **
6066 ** If tp->ppr_negotiation is 1 and a M_REJECT occurs, then
6067 ** we disable ppr_negotiation. If the first ppr_negotiation is
6068 ** successful, set this flag to 2.
6069 **
6070 **==========================================================
6071 */
6072 #ifdef SCSI_NCR_INTEGRITY_CHECKING
6073 static int ncr_ic_nego(ncb_p np, ccb_p cp, Scsi_Cmnd *cmd, u_char *msgptr)
6074 {
6075 tcb_p tp = &np->target[cp->target];
6076 int msglen = 0;
6077 int nego = 0;
6078 u_char new_width, new_offset, new_period;
6079 u_char no_increase;
6080
6081 if (tp->ppr_negotiation == 1) /* PPR message successful */
6082 tp->ppr_negotiation = 2;
6083
6084 if (tp->inq_done) {
6085
6086 if (!tp->ic_maximums_set) {
6087 tp->ic_maximums_set = 1;
6088
6089 /*
6090 * Check against target, host and user limits
6091 */
6092 if ( (tp->inq_byte7 & INQ7_WIDE16) &&
6093 np->maxwide && tp->usrwide)
6094 tp->ic_max_width = 1;
6095 else
6096 tp->ic_max_width = 0;
6097
6098
6099 if ((tp->inq_byte7 & INQ7_SYNC) && tp->maxoffs)
6100 tp->ic_min_sync = (tp->minsync < np->minsync) ?
6101 np->minsync : tp->minsync;
6102 else
6103 tp->ic_min_sync = 255;
6104
6105 tp->period = 1;
6106 tp->widedone = 1;
6107
6108 /*
6109 * Enable PPR negotiation - only if Ultra3 support
6110 * is accessible.
6111 */
6112
6113 #if 0
6114 if (tp->ic_max_width && (tp->ic_min_sync != 255 ))
6115 tp->ppr_negotiation = 1;
6116 #endif
6117 tp->ppr_negotiation = 0;
6118 if (np->features & FE_ULTRA3) {
6119 if (tp->ic_max_width && (tp->ic_min_sync == 0x09))
6120 tp->ppr_negotiation = 1;
6121 }
6122
6123 if (!tp->ppr_negotiation)
6124 cmd->ic_nego &= ~NS_PPR;
6125 }
6126
6127 if (DEBUG_FLAGS & DEBUG_IC) {
6128 printk("%s: cmd->ic_nego %d, 1st byte 0x%2X\n",
6129 ncr_name(np), cmd->ic_nego, cmd->cmnd[0]);
6130 }
6131
6132 /* Previous command recorded a parity or an initiator
6133 * detected error condition. Force bus to narrow for this
6134 * target. Clear flag. Negotation on request sense.
6135 * Note: kernel forces 2 bus resets :o( but clears itself out.
6136 * Minor bug? in scsi_obsolete.c (ugly)
6137 */
6138 if (np->check_integ_par) {
6139 printk("%s: Parity Error. Target set to narrow.\n",
6140 ncr_name(np));
6141 tp->ic_max_width = 0;
6142 tp->widedone = tp->period = 0;
6143 }
6144
6145 /* Initializing:
6146 * If ic_nego == NS_PPR, we are in the initial test for
6147 * PPR messaging support. If driver flag is clear, then
6148 * either we don't support PPR nego (narrow or async device)
6149 * or this is the second TUR and we have had a M. REJECT
6150 * or unexpected disconnect on the first PPR negotiation.
6151 * Do not negotiate, reset nego flags (in case a reset has
6152 * occurred), clear ic_nego and return.
6153 * General case: Kernel will clear flag on a fallback.
6154 * Do only SDTR or WDTR in the future.
6155 */
6156 if (!tp->ppr_negotiation && (cmd->ic_nego == NS_PPR )) {
6157 tp->ppr_negotiation = 0;
6158 cmd->ic_nego &= ~NS_PPR;
6159 tp->widedone = tp->period = 1;
6160 return msglen;
6161 }
6162 else if (( tp->ppr_negotiation && !(cmd->ic_nego & NS_PPR )) ||
6163 (!tp->ppr_negotiation && (cmd->ic_nego & NS_PPR )) ) {
6164 tp->ppr_negotiation = 0;
6165 cmd->ic_nego &= ~NS_PPR;
6166 }
6167
6168 /*
6169 * Always check the PPR nego. flag bit if ppr_negotiation
6170 * is set. If the ic_nego PPR bit is clear,
6171 * there must have been a fallback. Do only
6172 * WDTR / SDTR in the future.
6173 */
6174 if ((tp->ppr_negotiation) && (!(cmd->ic_nego & NS_PPR)))
6175 tp->ppr_negotiation = 0;
6176
6177 /* In case of a bus reset, ncr_negotiate will reset
6178 * the flags tp->widedone and tp->period to 0, forcing
6179 * a new negotiation. Do WDTR then SDTR. If PPR, do both.
6180 * Do NOT increase the period. It is possible for the Scsi_Cmnd
6181 * flags to be set to increase the period when a bus reset
6182 * occurs - we don't want to change anything.
6183 */
6184
6185 no_increase = 0;
6186
6187 if (tp->ppr_negotiation && (!tp->widedone) && (!tp->period) ) {
6188 cmd->ic_nego = NS_PPR;
6189 tp->widedone = tp->period = 1;
6190 no_increase = 1;
6191 }
6192 else if (!tp->widedone) {
6193 cmd->ic_nego = NS_WIDE;
6194 tp->widedone = 1;
6195 no_increase = 1;
6196 }
6197 else if (!tp->period) {
6198 cmd->ic_nego = NS_SYNC;
6199 tp->period = 1;
6200 no_increase = 1;
6201 }
6202
6203 new_width = cmd->ic_nego_width & tp->ic_max_width;
6204
6205 switch (cmd->ic_nego_sync) {
6206 case 2: /* increase the period */
6207 if (!no_increase) {
6208 if (tp->ic_min_sync <= 0x09)
6209 tp->ic_min_sync = 0x0A;
6210 else if (tp->ic_min_sync <= 0x0A)
6211 tp->ic_min_sync = 0x0C;
6212 else if (tp->ic_min_sync <= 0x0C)
6213 tp->ic_min_sync = 0x19;
6214 else if (tp->ic_min_sync <= 0x19)
6215 tp->ic_min_sync *= 2;
6216 else {
6217 tp->ic_min_sync = 255;
6218 cmd->ic_nego_sync = 0;
6219 tp->maxoffs = 0;
6220 }
6221 }
6222 new_period = tp->maxoffs?tp->ic_min_sync:0;
6223 new_offset = tp->maxoffs;
6224 break;
6225
6226 case 1: /* nego. to maximum */
6227 new_period = tp->maxoffs?tp->ic_min_sync:0;
6228 new_offset = tp->maxoffs;
6229 break;
6230
6231 case 0: /* nego to async */
6232 default:
6233 new_period = 0;
6234 new_offset = 0;
6235 break;
6236 };
6237
6238
6239 nego = NS_NOCHANGE;
6240 if (tp->ppr_negotiation) {
6241 u_char options_byte = 0;
6242
6243 /*
6244 ** Must make sure data is consistent.
6245 ** If period is 9 and sync, must be wide and DT bit set.
6246 ** else period must be larger. If the width is 0,
6247 ** reset bus to wide but increase the period to 0x0A.
6248 ** Note: The strange else clause is due to the integrity check.
6249 ** If fails at 0x09, wide, the I.C. code will redo at the same
6250 ** speed but a narrow bus. The driver must take care of slowing
6251 ** the bus speed down.
6252 **
6253 ** The maximum offset in ST mode is 31, in DT mode 62 (1010/1010_66 only)
6254 */
6255 if ( (new_period==0x09) && new_offset) {
6256 if (new_width)
6257 options_byte = 0x02;
6258 else {
6259 tp->ic_min_sync = 0x0A;
6260 new_period = 0x0A;
6261 cmd->ic_nego_width = 1;
6262 new_width = 1;
6263 }
6264 }
6265 if (!options_byte && new_offset > np->maxoffs_st)
6266 new_offset = np->maxoffs_st;
6267
6268 nego = NS_PPR;
6269
6270 msgptr[msglen++] = M_EXTENDED;
6271 msgptr[msglen++] = 6;
6272 msgptr[msglen++] = M_X_PPR_REQ;
6273 msgptr[msglen++] = new_period;
6274 msgptr[msglen++] = 0;
6275 msgptr[msglen++] = new_offset;
6276 msgptr[msglen++] = new_width;
6277 msgptr[msglen++] = options_byte;
6278
6279 }
6280 else {
6281 switch (cmd->ic_nego & ~NS_PPR) {
6282 case NS_WIDE:
6283 /*
6284 ** WDTR negotiation on if device supports
6285 ** wide or if wide device forced narrow
6286 ** due to a parity error.
6287 */
6288
6289 cmd->ic_nego_width &= tp->ic_max_width;
6290
6291 if (tp->ic_max_width | np->check_integ_par) {
6292 nego = NS_WIDE;
6293 msgptr[msglen++] = M_EXTENDED;
6294 msgptr[msglen++] = 2;
6295 msgptr[msglen++] = M_X_WIDE_REQ;
6296 msgptr[msglen++] = new_width;
6297 }
6298 break;
6299
6300 case NS_SYNC:
6301 /*
6302 ** negotiate synchronous transfers
6303 ** Target must support sync transfers.
6304 ** Min. period = 0x0A, maximum offset of 31=0x1f.
6305 */
6306
6307 if (tp->inq_byte7 & INQ7_SYNC) {
6308
6309 if (new_offset && (new_period < 0x0A)) {
6310 tp->ic_min_sync = 0x0A;
6311 new_period = 0x0A;
6312 }
6313 if (new_offset > np->maxoffs_st)
6314 new_offset = np->maxoffs_st;
6315 nego = NS_SYNC;
6316 msgptr[msglen++] = M_EXTENDED;
6317 msgptr[msglen++] = 3;
6318 msgptr[msglen++] = M_X_SYNC_REQ;
6319 msgptr[msglen++] = new_period;
6320 msgptr[msglen++] = new_offset;
6321 }
6322 else
6323 cmd->ic_nego_sync = 0;
6324 break;
6325
6326 case NS_NOCHANGE:
6327 break;
6328 }
6329 }
6330
6331 };
6332
6333 cp->nego_status = nego;
6334 np->check_integ_par = 0;
6335
6336 if (nego) {
6337 tp->nego_cp = cp;
6338 if (DEBUG_FLAGS & DEBUG_NEGO) {
6339 ncr_print_msg(cp, nego == NS_WIDE ?
6340 "wide/narrow msgout":
6341 (nego == NS_SYNC ? "sync/async msgout" : "ppr msgout"),
6342 msgptr);
6343 };
6344 };
6345
6346 return msglen;
6347 }
6348 #endif /* SCSI_NCR_INTEGRITY_CHECKING */
6349
6350 /*==========================================================
6351 **
6352 **
6353 ** Prepare the next negotiation message if needed.
6354 **
6355 ** Fill in the part of message buffer that contains the
6356 ** negotiation and the nego_status field of the CCB.
6357 ** Returns the size of the message in bytes.
6358 **
6359 **
6360 **==========================================================
6361 */
6362
6363
6364 static int ncr_prepare_nego(ncb_p np, ccb_p cp, u_char *msgptr)
6365 {
6366 tcb_p tp = &np->target[cp->target];
6367 int msglen = 0;
6368 int nego = 0;
6369 u_char width, offset, factor, last_byte;
6370
6371 if (!np->check_integrity) {
6372 /* If integrity checking disabled, enable PPR messaging
6373 * if device supports wide, sync and ultra 3
6374 */
6375 if (tp->ppr_negotiation == 1) /* PPR message successful */
6376 tp->ppr_negotiation = 2;
6377
6378 if ((tp->inq_done) && (!tp->ic_maximums_set)) {
6379 tp->ic_maximums_set = 1;
6380
6381 /*
6382 * Issue PPR only if board is capable
6383 * and set-up for Ultra3 transfers.
6384 */
6385 tp->ppr_negotiation = 0;
6386 if ( (np->features & FE_ULTRA3) &&
6387 (tp->usrwide) && (tp->maxoffs) &&
6388 (tp->minsync == 0x09) )
6389 tp->ppr_negotiation = 1;
6390 }
6391 }
6392
6393 if (tp->inq_done) {
6394 /*
6395 * Get the current width, offset and period
6396 */
6397 ncr_get_xfer_info( np, tp, &factor,
6398 &offset, &width);
6399
6400 /*
6401 ** negotiate wide transfers ?
6402 */
6403
6404 if (!tp->widedone) {
6405 if (tp->inq_byte7 & INQ7_WIDE16) {
6406 if (tp->ppr_negotiation)
6407 nego = NS_PPR;
6408 else
6409 nego = NS_WIDE;
6410
6411 width = tp->usrwide;
6412 #ifdef SCSI_NCR_INTEGRITY_CHECKING
6413 if (tp->ic_done)
6414 width &= tp->ic_max_width;
6415 #endif
6416 } else
6417 tp->widedone=1;
6418
6419 };
6420
6421 /*
6422 ** negotiate synchronous transfers?
6423 */
6424
6425 if ((nego != NS_WIDE) && !tp->period) {
6426 if (tp->inq_byte7 & INQ7_SYNC) {
6427 if (tp->ppr_negotiation)
6428 nego = NS_PPR;
6429 else
6430 nego = NS_SYNC;
6431
6432 /* Check for async flag */
6433 if (tp->maxoffs == 0) {
6434 offset = 0;
6435 factor = 0;
6436 }
6437 else {
6438 offset = tp->maxoffs;
6439 factor = tp->minsync;
6440 #ifdef SCSI_NCR_INTEGRITY_CHECKING
6441 if ((tp->ic_done) &&
6442 (factor < tp->ic_min_sync))
6443 factor = tp->ic_min_sync;
6444 #endif
6445 }
6446
6447 } else {
6448 offset = 0;
6449 factor = 0;
6450 tp->period =0xffff;
6451 PRINT_TARGET(np, cp->target);
6452 printk ("target did not report SYNC.\n");
6453 };
6454 };
6455 };
6456
6457 switch (nego) {
6458 case NS_PPR:
6459 /*
6460 ** Must make sure data is consistent.
6461 ** If period is 9 and sync, must be wide and DT bit set
6462 ** else period must be larger.
6463 ** Maximum offset is 31=0x1f is ST mode, 62 if DT mode
6464 */
6465 last_byte = 0;
6466 if ( (factor==9) && offset) {
6467 if (!width) {
6468 factor = 0x0A;
6469 }
6470 else
6471 last_byte = 0x02;
6472 }
6473 if (!last_byte && offset > np->maxoffs_st)
6474 offset = np->maxoffs_st;
6475
6476 msgptr[msglen++] = M_EXTENDED;
6477 msgptr[msglen++] = 6;
6478 msgptr[msglen++] = M_X_PPR_REQ;
6479 msgptr[msglen++] = factor;
6480 msgptr[msglen++] = 0;
6481 msgptr[msglen++] = offset;
6482 msgptr[msglen++] = width;
6483 msgptr[msglen++] = last_byte;
6484 break;
6485 case NS_SYNC:
6486 /*
6487 ** Never negotiate faster than Ultra 2 (25ns periods)
6488 */
6489 if (offset && (factor < 0x0A)) {
6490 factor = 0x0A;
6491 tp->minsync = 0x0A;
6492 }
6493 if (offset > np->maxoffs_st)
6494 offset = np->maxoffs_st;
6495
6496 msgptr[msglen++] = M_EXTENDED;
6497 msgptr[msglen++] = 3;
6498 msgptr[msglen++] = M_X_SYNC_REQ;
6499 msgptr[msglen++] = factor;
6500 msgptr[msglen++] = offset;
6501 break;
6502 case NS_WIDE:
6503 msgptr[msglen++] = M_EXTENDED;
6504 msgptr[msglen++] = 2;
6505 msgptr[msglen++] = M_X_WIDE_REQ;
6506 msgptr[msglen++] = width;
6507 break;
6508 };
6509
6510 cp->nego_status = nego;
6511
6512 if (nego) {
6513 tp->nego_cp = cp;
6514 if (DEBUG_FLAGS & DEBUG_NEGO) {
6515 ncr_print_msg(cp, nego == NS_WIDE ?
6516 "wide msgout":
6517 (nego == NS_SYNC ? "sync msgout" : "ppr msgout"),
6518 msgptr);
6519 };
6520 };
6521
6522 return msglen;
6523 }
6524
6525 /*==========================================================
6526 **
6527 **
6528 ** Start execution of a SCSI command.
6529 ** This is called from the generic SCSI driver.
6530 **
6531 **
6532 **==========================================================
6533 */
6534 static int ncr_queue_command (ncb_p np, Scsi_Cmnd *cmd)
6535 {
6536 /* Scsi_Device *device = cmd->device; */
6537 tcb_p tp = &np->target[cmd->device->id];
6538 lcb_p lp = ncr_lp(np, tp, cmd->device->lun);
6539 ccb_p cp;
6540
6541 u_char idmsg, *msgptr;
6542 u_int msglen;
6543 int direction;
6544 u_int32 lastp, goalp;
6545
6546 /*---------------------------------------------
6547 **
6548 ** Some shortcuts ...
6549 **
6550 **---------------------------------------------
6551 */
6552 if ((cmd->device->id == np->myaddr ) ||
6553 (cmd->device->id >= MAX_TARGET) ||
6554 (cmd->device->lun >= MAX_LUN )) {
6555 return(DID_BAD_TARGET);
6556 }
6557
6558 /*---------------------------------------------
6559 **
6560 ** Complete the 1st TEST UNIT READY command
6561 ** with error condition if the device is
6562 ** flagged NOSCAN, in order to speed up
6563 ** the boot.
6564 **
6565 **---------------------------------------------
6566 */
6567 if ((cmd->cmnd[0] == 0 || cmd->cmnd[0] == 0x12) &&
6568 (tp->usrflag & UF_NOSCAN)) {
6569 tp->usrflag &= ~UF_NOSCAN;
6570 return DID_BAD_TARGET;
6571 }
6572
6573 if (DEBUG_FLAGS & DEBUG_TINY) {
6574 PRINT_ADDR(cmd);
6575 printk ("CMD=%x ", cmd->cmnd[0]);
6576 }
6577
6578 /*---------------------------------------------------
6579 **
6580 ** Assign a ccb / bind cmd.
6581 ** If resetting, shorten settle_time if necessary
6582 ** in order to avoid spurious timeouts.
6583 ** If resetting or no free ccb,
6584 ** insert cmd into the waiting list.
6585 **
6586 **----------------------------------------------------
6587 */
6588 if (np->settle_time && cmd->timeout_per_command >= HZ) {
6589 u_long tlimit = ktime_get(cmd->timeout_per_command - HZ);
6590 if (ktime_dif(np->settle_time, tlimit) > 0)
6591 np->settle_time = tlimit;
6592 }
6593
6594 if (np->settle_time || !(cp=ncr_get_ccb (np, cmd->device->id, cmd->device->lun))) {
6595 insert_into_waiting_list(np, cmd);
6596 return(DID_OK);
6597 }
6598 cp->cmd = cmd;
6599
6600 /*---------------------------------------------------
6601 **
6602 ** Enable tagged queue if asked by scsi ioctl
6603 **
6604 **----------------------------------------------------
6605 */
6606 #if 0 /* This stuff was only useful for linux-1.2.13 */
6607 if (lp && !lp->numtags && cmd->device && cmd->device->tagged_queue) {
6608 lp->numtags = tp->usrtags;
6609 ncr_setup_tags (np, cp->target, cp->lun);
6610 }
6611 #endif
6612
6613 /*----------------------------------------------------
6614 **
6615 ** Build the identify / tag / sdtr message
6616 **
6617 **----------------------------------------------------
6618 */
6619
6620 idmsg = M_IDENTIFY | cp->lun;
6621
6622 if (cp ->tag != NO_TAG || (lp && !(tp->usrflag & UF_NODISC)))
6623 idmsg |= 0x40;
6624
6625 msgptr = cp->scsi_smsg;
6626 msglen = 0;
6627 msgptr[msglen++] = idmsg;
6628
6629 if (cp->tag != NO_TAG) {
6630 char order = np->order;
6631
6632 /*
6633 ** Force ordered tag if necessary to avoid timeouts
6634 ** and to preserve interactivity.
6635 */
6636 if (lp && ktime_exp(lp->tags_stime)) {
6637 lp->tags_si = !(lp->tags_si);
6638 if (lp->tags_sum[lp->tags_si]) {
6639 order = M_ORDERED_TAG;
6640 if ((DEBUG_FLAGS & DEBUG_TAGS)||bootverbose>0){
6641 PRINT_ADDR(cmd);
6642 printk("ordered tag forced.\n");
6643 }
6644 }
6645 lp->tags_stime = ktime_get(3*HZ);
6646 }
6647
6648 if (order == 0) {
6649 /*
6650 ** Ordered write ops, unordered read ops.
6651 */
6652 switch (cmd->cmnd[0]) {
6653 case 0x08: /* READ_SMALL (6) */
6654 case 0x28: /* READ_BIG (10) */
6655 case 0xa8: /* READ_HUGE (12) */
6656 order = M_SIMPLE_TAG;
6657 break;
6658 default:
6659 order = M_ORDERED_TAG;
6660 }
6661 }
6662 msgptr[msglen++] = order;
6663 /*
6664 ** For less than 128 tags, actual tags are numbered
6665 ** 1,3,5,..2*MAXTAGS+1,since we may have to deal
6666 ** with devices that have problems with #TAG 0 or too
6667 ** great #TAG numbers. For more tags (up to 256),
6668 ** we use directly our tag number.
6669 */
6670 #if MAX_TASKS > (512/4)
6671 msgptr[msglen++] = cp->tag;
6672 #else
6673 msgptr[msglen++] = (cp->tag << 1) + 1;
6674 #endif
6675 }
6676
6677 cp->host_flags = 0;
6678
6679 /*----------------------------------------------------
6680 **
6681 ** Build the data descriptors
6682 **
6683 **----------------------------------------------------
6684 */
6685
6686 direction = scsi_data_direction(cmd);
6687 if (direction != SCSI_DATA_NONE) {
6688 cp->segments = np->scatter (np, cp, cp->cmd);
6689 if (cp->segments < 0) {
6690 ncr_free_ccb(np, cp);
6691 return(DID_ERROR);
6692 }
6693 }
6694 else {
6695 cp->data_len = 0;
6696 cp->segments = 0;
6697 }
6698
6699 /*---------------------------------------------------
6700 **
6701 ** negotiation required?
6702 **
6703 ** (nego_status is filled by ncr_prepare_nego())
6704 **
6705 **---------------------------------------------------
6706 */
6707
6708 cp->nego_status = 0;
6709
6710 #ifdef SCSI_NCR_INTEGRITY_CHECKING
6711 if ((np->check_integrity && tp->ic_done) || !np->check_integrity) {
6712 if ((!tp->widedone || !tp->period) && !tp->nego_cp && lp) {
6713 msglen += ncr_prepare_nego (np, cp, msgptr + msglen);
6714 }
6715 }
6716 else if (np->check_integrity && (cmd->ic_in_progress)) {
6717 msglen += ncr_ic_nego (np, cp, cmd, msgptr + msglen);
6718 }
6719 else if (np->check_integrity && cmd->ic_complete) {
6720 u_long current_period;
6721 u_char current_offset, current_width, current_factor;
6722
6723 ncr_get_xfer_info (np, tp, &current_factor,
6724 &current_offset, &current_width);
6725
6726 tp->ic_max_width = current_width;
6727 tp->ic_min_sync = current_factor;
6728
6729 if (current_factor == 9) current_period = 125;
6730 else if (current_factor == 10) current_period = 250;
6731 else if (current_factor == 11) current_period = 303;
6732 else if (current_factor == 12) current_period = 500;
6733 else current_period = current_factor * 40;
6734
6735 /*
6736 * Negotiation for this target is complete. Update flags.
6737 */
6738 tp->period = current_period;
6739 tp->widedone = 1;
6740 tp->ic_done = 1;
6741
6742 printk("%s: Integrity Check Complete: \n", ncr_name(np));
6743
6744 printk("%s: %s %s SCSI", ncr_name(np),
6745 current_offset?"SYNC":"ASYNC",
6746 tp->ic_max_width?"WIDE":"NARROW");
6747 if (current_offset) {
6748 u_long mbs = 10000 * (tp->ic_max_width + 1);
6749
6750 printk(" %d.%d MB/s",
6751 (int) (mbs / current_period), (int) (mbs % current_period));
6752
6753 printk(" (%d ns, %d offset)\n",
6754 (int) current_period/10, current_offset);
6755 }
6756 else
6757 printk(" %d MB/s. \n ", (tp->ic_max_width+1)*5);
6758 }
6759 #else
6760 if ((!tp->widedone || !tp->period) && !tp->nego_cp && lp) {
6761 msglen += ncr_prepare_nego (np, cp, msgptr + msglen);
6762 }
6763 #endif /* SCSI_NCR_INTEGRITY_CHECKING */
6764
6765
6766 /*----------------------------------------------------
6767 **
6768 ** Determine xfer direction.
6769 **
6770 **----------------------------------------------------
6771 */
6772 if (!cp->data_len)
6773 direction = SCSI_DATA_NONE;
6774
6775 /*
6776 ** If data direction is UNKNOWN, speculate DATA_READ
6777 ** but prepare alternate pointers for WRITE in case
6778 ** of our speculation will be just wrong.
6779 ** SCRIPTS will swap values if needed.
6780 */
6781 switch(direction) {
6782 case SCSI_DATA_UNKNOWN:
6783 case SCSI_DATA_WRITE:
6784 goalp = NCB_SCRIPT_PHYS (np, data_out2) + 8;
6785 lastp = goalp - 8 - (cp->segments * (SCR_SG_SIZE*4));
6786 if (direction != SCSI_DATA_UNKNOWN)
6787 break;
6788 cp->phys.header.wgoalp = cpu_to_scr(goalp);
6789 cp->phys.header.wlastp = cpu_to_scr(lastp);
6790 /* fall through */
6791 case SCSI_DATA_READ:
6792 cp->host_flags |= HF_DATA_IN;
6793 goalp = NCB_SCRIPT_PHYS (np, data_in2) + 8;
6794 lastp = goalp - 8 - (cp->segments * (SCR_SG_SIZE*4));
6795 break;
6796 default:
6797 case SCSI_DATA_NONE:
6798 lastp = goalp = NCB_SCRIPTH_PHYS (np, no_data);
6799 break;
6800 }
6801
6802 /*
6803 ** Set all pointers values needed by SCRIPTS.
6804 ** If direction is unknown, start at data_io.
6805 */
6806 cp->phys.header.lastp = cpu_to_scr(lastp);
6807 cp->phys.header.goalp = cpu_to_scr(goalp);
6808
6809 if (direction == SCSI_DATA_UNKNOWN)
6810 cp->phys.header.savep =
6811 cpu_to_scr(NCB_SCRIPTH_PHYS (np, data_io));
6812 else
6813 cp->phys.header.savep= cpu_to_scr(lastp);
6814
6815 /*
6816 ** Save the initial data pointer in order to be able
6817 ** to redo the command.
6818 ** We also have to save the initial lastp, since it
6819 ** will be changed to DATA_IO if we don't know the data
6820 ** direction and the device completes the command with
6821 ** QUEUE FULL status (without entering the data phase).
6822 */
6823 cp->startp = cp->phys.header.savep;
6824 cp->lastp0 = cp->phys.header.lastp;
6825
6826 /*----------------------------------------------------
6827 **
6828 ** fill in ccb
6829 **
6830 **----------------------------------------------------
6831 **
6832 **
6833 ** physical -> virtual backlink
6834 ** Generic SCSI command
6835 */
6836
6837 /*
6838 ** Startqueue
6839 */
6840 cp->phys.header.go.start = cpu_to_scr(NCB_SCRIPT_PHYS (np,select));
6841 cp->phys.header.go.restart = cpu_to_scr(NCB_SCRIPT_PHYS (np,resel_dsa));
6842 /*
6843 ** select
6844 */
6845 cp->phys.select.sel_id = cp->target;
6846 cp->phys.select.sel_scntl3 = tp->wval;
6847 cp->phys.select.sel_sxfer = tp->sval;
6848 cp->phys.select.sel_scntl4 = tp->uval;
6849 /*
6850 ** message
6851 */
6852 cp->phys.smsg.addr = cpu_to_scr(CCB_PHYS (cp, scsi_smsg));
6853 cp->phys.smsg.size = cpu_to_scr(msglen);
6854
6855 /*
6856 ** command
6857 */
6858 memcpy(cp->cdb_buf, cmd->cmnd, MIN(cmd->cmd_len, sizeof(cp->cdb_buf)));
6859 cp->phys.cmd.addr = cpu_to_scr(CCB_PHYS (cp, cdb_buf[0]));
6860 cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
6861
6862 /*
6863 ** status
6864 */
6865 cp->actualquirks = tp->quirks;
6866 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
6867 cp->scsi_status = S_ILLEGAL;
6868 cp->xerr_status = 0;
6869 cp->extra_bytes = 0;
6870
6871 /*
6872 ** extreme data pointer.
6873 ** shall be positive, so -1 is lower than lowest.:)
6874 */
6875 cp->ext_sg = -1;
6876 cp->ext_ofs = 0;
6877
6878 /*----------------------------------------------------
6879 **
6880 ** Critical region: start this job.
6881 **
6882 **----------------------------------------------------
6883 */
6884
6885 /*
6886 ** activate this job.
6887 */
6888
6889 /*
6890 ** insert next CCBs into start queue.
6891 ** 2 max at a time is enough to flush the CCB wait queue.
6892 */
6893 if (lp)
6894 ncr_start_next_ccb(np, lp, 2);
6895 else
6896 ncr_put_start_queue(np, cp);
6897
6898 /*
6899 ** Command is successfully queued.
6900 */
6901
6902 return(DID_OK);
6903 }
6904
6905
6906 /*==========================================================
6907 **
6908 **
6909 ** Insert a CCB into the start queue and wake up the
6910 ** SCRIPTS processor.
6911 **
6912 **
6913 **==========================================================
6914 */
6915
6916 static void ncr_start_next_ccb(ncb_p np, lcb_p lp, int maxn)
6917 {
6918 XPT_QUEHEAD *qp;
6919 ccb_p cp;
6920
6921 while (maxn-- && lp->queuedccbs < lp->queuedepth) {
6922 qp = xpt_remque_head(&lp->wait_ccbq);
6923 if (!qp)
6924 break;
6925 ++lp->queuedccbs;
6926 cp = xpt_que_entry(qp, struct ccb, link_ccbq);
6927 xpt_insque_tail(qp, &lp->busy_ccbq);
6928 lp->tasktbl[cp->tag == NO_TAG ? 0 : cp->tag] =
6929 cpu_to_scr(cp->p_ccb);
6930 ncr_put_start_queue(np, cp);
6931 }
6932 }
6933
6934 static void ncr_put_start_queue(ncb_p np, ccb_p cp)
6935 {
6936 u_short qidx;
6937
6938 #ifdef SCSI_NCR_IARB_SUPPORT
6939 /*
6940 ** If the previously queued CCB is not yet done,
6941 ** set the IARB hint. The SCRIPTS will go with IARB
6942 ** for this job when starting the previous one.
6943 ** We leave devices a chance to win arbitration by
6944 ** not using more than 'iarb_max' consecutive
6945 ** immediate arbitrations.
6946 */
6947 if (np->last_cp && np->iarb_count < np->iarb_max) {
6948 np->last_cp->host_flags |= HF_HINT_IARB;
6949 ++np->iarb_count;
6950 }
6951 else
6952 np->iarb_count = 0;
6953 np->last_cp = cp;
6954 #endif
6955
6956 /*
6957 ** insert into start queue.
6958 */
6959 qidx = np->squeueput + 2;
6960 if (qidx >= MAX_START*2) qidx = 0;
6961
6962 np->squeue [qidx] = cpu_to_scr(np->p_idletask);
6963 MEMORY_BARRIER();
6964 np->squeue [np->squeueput] = cpu_to_scr(cp->p_ccb);
6965
6966 np->squeueput = qidx;
6967 cp->queued = 1;
6968
6969 if (DEBUG_FLAGS & DEBUG_QUEUE)
6970 printk ("%s: queuepos=%d.\n", ncr_name (np), np->squeueput);
6971
6972 /*
6973 ** Script processor may be waiting for reselect.
6974 ** Wake it up.
6975 */
6976 MEMORY_BARRIER();
6977 OUTB (nc_istat, SIGP|np->istat_sem);
6978 }
6979
6980
6981 /*==========================================================
6982 **
6983 ** Soft reset the chip.
6984 **
6985 ** Some 896 and 876 chip revisions may hang-up if we set
6986 ** the SRST (soft reset) bit at the wrong time when SCRIPTS
6987 ** are running.
6988 ** So, we need to abort the current operation prior to
6989 ** soft resetting the chip.
6990 **
6991 **==========================================================
6992 */
6993
6994 static void ncr_chip_reset (ncb_p np)
6995 {
6996 OUTB (nc_istat, SRST);
6997 UDELAY (10);
6998 OUTB (nc_istat, 0);
6999 }
7000
7001 static void ncr_soft_reset(ncb_p np)
7002 {
7003 u_char istat;
7004 int i;
7005
7006 if (!(np->features & FE_ISTAT1) || !(INB (nc_istat1) & SRUN))
7007 goto do_chip_reset;
7008
7009 OUTB (nc_istat, CABRT);
7010 for (i = 100000 ; i ; --i) {
7011 istat = INB (nc_istat);
7012 if (istat & SIP) {
7013 INW (nc_sist);
7014 }
7015 else if (istat & DIP) {
7016 if (INB (nc_dstat) & ABRT)
7017 break;
7018 }
7019 UDELAY(5);
7020 }
7021 OUTB (nc_istat, 0);
7022 if (!i)
7023 printk("%s: unable to abort current chip operation, "
7024 "ISTAT=0x%02x.\n", ncr_name(np), istat);
7025 do_chip_reset:
7026 ncr_chip_reset(np);
7027 }
7028
7029 /*==========================================================
7030 **
7031 **
7032 ** Start reset process.
7033 ** The interrupt handler will reinitialize the chip.
7034 ** The timeout handler will wait for settle_time before
7035 ** clearing it and so resuming command processing.
7036 **
7037 **
7038 **==========================================================
7039 */
7040 static void ncr_start_reset(ncb_p np)
7041 {
7042 (void) ncr_reset_scsi_bus(np, 1, driver_setup.settle_delay);
7043 }
7044
7045 static int ncr_reset_scsi_bus(ncb_p np, int enab_int, int settle_delay)
7046 {
7047 u_int32 term;
7048 int retv = 0;
7049
7050 np->settle_time = ktime_get(settle_delay * HZ);
7051
7052 if (bootverbose > 1)
7053 printk("%s: resetting, "
7054 "command processing suspended for %d seconds\n",
7055 ncr_name(np), settle_delay);
7056
7057 ncr_soft_reset(np); /* Soft reset the chip */
7058 UDELAY (2000); /* The 895/6 need time for the bus mode to settle */
7059 if (enab_int)
7060 OUTW (nc_sien, RST);
7061 /*
7062 ** Enable Tolerant, reset IRQD if present and
7063 ** properly set IRQ mode, prior to resetting the bus.
7064 */
7065 OUTB (nc_stest3, TE);
7066 OUTB (nc_dcntl, (np->rv_dcntl & IRQM));
7067 OUTB (nc_scntl1, CRST);
7068 UDELAY (200);
7069
7070 if (!driver_setup.bus_check)
7071 goto out;
7072 /*
7073 ** Check for no terminators or SCSI bus shorts to ground.
7074 ** Read SCSI data bus, data parity bits and control signals.
7075 ** We are expecting RESET to be TRUE and other signals to be
7076 ** FALSE.
7077 */
7078 term = INB(nc_sstat0);
7079 term = ((term & 2) << 7) + ((term & 1) << 17); /* rst sdp0 */
7080 term |= ((INB(nc_sstat2) & 0x01) << 26) | /* sdp1 */
7081 ((INW(nc_sbdl) & 0xff) << 9) | /* d7-0 */
7082 ((INW(nc_sbdl) & 0xff00) << 10) | /* d15-8 */
7083 INB(nc_sbcl); /* req ack bsy sel atn msg cd io */
7084
7085 if (!(np->features & FE_WIDE))
7086 term &= 0x3ffff;
7087
7088 if (term != (2<<7)) {
7089 printk("%s: suspicious SCSI data while resetting the BUS.\n",
7090 ncr_name(np));
7091 printk("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
7092 "0x%lx, expecting 0x%lx\n",
7093 ncr_name(np),
7094 (np->features & FE_WIDE) ? "dp1,d15-8," : "",
7095 (u_long)term, (u_long)(2<<7));
7096 if (driver_setup.bus_check == 1)
7097 retv = 1;
7098 }
7099 out:
7100 OUTB (nc_scntl1, 0);
7101 return retv;
7102 }
7103
7104 /*==========================================================
7105 **
7106 **
7107 ** Reset the SCSI BUS.
7108 ** This is called from the generic SCSI driver.
7109 **
7110 **
7111 **==========================================================
7112 */
7113 static int ncr_reset_bus (ncb_p np, Scsi_Cmnd *cmd, int sync_reset)
7114 {
7115 /* Scsi_Device *device = cmd->device; */
7116 ccb_p cp;
7117 int found;
7118
7119 /*
7120 * Return immediately if reset is in progress.
7121 */
7122 if (np->settle_time) {
7123 return SCSI_RESET_PUNT;
7124 }
7125 /*
7126 * Start the reset process.
7127 * The script processor is then assumed to be stopped.
7128 * Commands will now be queued in the waiting list until a settle
7129 * delay of 2 seconds will be completed.
7130 */
7131 ncr_start_reset(np);
7132 /*
7133 * First, look in the wakeup list
7134 */
7135 for (found=0, cp=np->ccbc; cp; cp=cp->link_ccb) {
7136 /*
7137 ** look for the ccb of this command.
7138 */
7139 if (cp->host_status == HS_IDLE) continue;
7140 if (cp->cmd == cmd) {
7141 found = 1;
7142 break;
7143 }
7144 }
7145 /*
7146 * Then, look in the waiting list
7147 */
7148 if (!found && retrieve_from_waiting_list(0, np, cmd))
7149 found = 1;
7150 /*
7151 * Wake-up all awaiting commands with DID_RESET.
7152 */
7153 reset_waiting_list(np);
7154 /*
7155 * Wake-up all pending commands with HS_RESET -> DID_RESET.
7156 */
7157 ncr_wakeup(np, HS_RESET);
7158 /*
7159 * If the involved command was not in a driver queue, and the
7160 * scsi driver told us reset is synchronous, and the command is not
7161 * currently in the waiting list, complete it with DID_RESET status,
7162 * in order to keep it alive.
7163 */
7164 if (!found && sync_reset && !retrieve_from_waiting_list(0, np, cmd)) {
7165 SetScsiResult(cmd, DID_RESET, 0);
7166 ncr_queue_done_cmd(np, cmd);
7167 }
7168
7169 return SCSI_RESET_SUCCESS;
7170 }
7171
7172 /*==========================================================
7173 **
7174 **
7175 ** Abort an SCSI command.
7176 ** This is called from the generic SCSI driver.
7177 **
7178 **
7179 **==========================================================
7180 */
7181 static int ncr_abort_command (ncb_p np, Scsi_Cmnd *cmd)
7182 {
7183 /* Scsi_Device *device = cmd->device; */
7184 ccb_p cp;
7185
7186 /*
7187 * First, look for the scsi command in the waiting list
7188 */
7189 if (remove_from_waiting_list(np, cmd)) {
7190 SetScsiAbortResult(cmd);
7191 ncr_queue_done_cmd(np, cmd);
7192 return SCSI_ABORT_SUCCESS;
7193 }
7194
7195 /*
7196 * Then, look in the wakeup list
7197 */
7198 for (cp=np->ccbc; cp; cp=cp->link_ccb) {
7199 /*
7200 ** look for the ccb of this command.
7201 */
7202 if (cp->host_status == HS_IDLE) continue;
7203 if (cp->cmd == cmd)
7204 break;
7205 }
7206
7207 if (!cp) {
7208 return SCSI_ABORT_NOT_RUNNING;
7209 }
7210
7211 /*
7212 ** Keep track we have to abort this job.
7213 */
7214 cp->to_abort = 1;
7215
7216 /*
7217 ** Tell the SCRIPTS processor to stop
7218 ** and synchronize with us.
7219 */
7220 np->istat_sem = SEM;
7221
7222 /*
7223 ** If there are no requests, the script
7224 ** processor will sleep on SEL_WAIT_RESEL.
7225 ** Let's wake it up, since it may have to work.
7226 */
7227 OUTB (nc_istat, SIGP|SEM);
7228
7229 /*
7230 ** Tell user we are working for him.
7231 */
7232 return SCSI_ABORT_PENDING;
7233 }
7234
7235 /*==========================================================
7236 **
7237 ** Linux release module stuff.
7238 **
7239 ** Called before unloading the module
7240 ** Detach the host.
7241 ** We have to free resources and halt the NCR chip
7242 **
7243 **==========================================================
7244 */
7245
7246 static int ncr_detach(ncb_p np)
7247 {
7248 int i;
7249
7250 printk("%s: detaching ...\n", ncr_name(np));
7251
7252 /*
7253 ** Stop the ncr_timeout process
7254 ** Set release_stage to 1 and wait that ncr_timeout() set it to 2.
7255 */
7256 np->release_stage = 1;
7257 for (i = 50 ; i && np->release_stage != 2 ; i--) MDELAY (100);
7258 if (np->release_stage != 2)
7259 printk("%s: the timer seems to be already stopped\n",
7260 ncr_name(np));
7261 else np->release_stage = 2;
7262
7263 /*
7264 ** Reset NCR chip.
7265 ** We should use ncr_soft_reset(), but we donnot want to do
7266 ** so, since we may not be safe if interrupts occur.
7267 */
7268
7269 printk("%s: resetting chip\n", ncr_name(np));
7270 ncr_chip_reset(np);
7271
7272 /*
7273 ** Restore bios setting for automatic clock detection.
7274 */
7275 OUTB(nc_dmode, np->sv_dmode);
7276 OUTB(nc_dcntl, np->sv_dcntl);
7277 OUTB(nc_ctest3, np->sv_ctest3);
7278 OUTB(nc_ctest4, np->sv_ctest4);
7279 OUTB(nc_ctest5, np->sv_ctest5);
7280 OUTB(nc_gpcntl, np->sv_gpcntl);
7281 OUTB(nc_stest2, np->sv_stest2);
7282
7283 ncr_selectclock(np, np->sv_scntl3);
7284 /*
7285 ** Free host resources
7286 */
7287 ncr_free_resources(np);
7288
7289 return 1;
7290 }
7291
7292 /*==========================================================
7293 **
7294 **
7295 ** Complete execution of a SCSI command.
7296 ** Signal completion to the generic SCSI driver.
7297 **
7298 **
7299 **==========================================================
7300 */
7301
7302 void ncr_complete (ncb_p np, ccb_p cp)
7303 {
7304 Scsi_Cmnd *cmd;
7305 tcb_p tp;
7306 lcb_p lp;
7307
7308 /*
7309 ** Sanity check
7310 */
7311 if (!cp || !cp->cmd)
7312 return;
7313
7314 /*
7315 ** Print some debugging info.
7316 */
7317
7318 if (DEBUG_FLAGS & DEBUG_TINY)
7319 printk ("CCB=%lx STAT=%x/%x\n", (unsigned long)cp,
7320 cp->host_status,cp->scsi_status);
7321
7322 /*
7323 ** Get command, target and lun pointers.
7324 */
7325
7326 cmd = cp->cmd;
7327 cp->cmd = NULL;
7328 tp = &np->target[cp->target];
7329 lp = ncr_lp(np, tp, cp->lun);
7330
7331 /*
7332 ** We donnot queue more than 1 ccb per target
7333 ** with negotiation at any time. If this ccb was
7334 ** used for negotiation, clear this info in the tcb.
7335 */
7336
7337 if (cp == tp->nego_cp)
7338 tp->nego_cp = 0;
7339
7340 #ifdef SCSI_NCR_IARB_SUPPORT
7341 /*
7342 ** We just complete the last queued CCB.
7343 ** Clear this info that is no more relevant.
7344 */
7345 if (cp == np->last_cp)
7346 np->last_cp = 0;
7347 #endif
7348
7349 /*
7350 ** If auto-sense performed, change scsi status,
7351 ** Otherwise, compute the residual.
7352 */
7353 if (cp->host_flags & HF_AUTO_SENSE) {
7354 cp->scsi_status = cp->sv_scsi_status;
7355 cp->xerr_status = cp->sv_xerr_status;
7356 }
7357 else {
7358 cp->resid = 0;
7359 if (cp->xerr_status ||
7360 cp->phys.header.lastp != cp->phys.header.goalp)
7361 cp->resid = ncr_compute_residual(np, cp);
7362 }
7363
7364 /*
7365 ** Check for extended errors.
7366 */
7367
7368 if (cp->xerr_status) {
7369 if (cp->xerr_status & XE_PARITY_ERR) {
7370 PRINT_ADDR(cmd);
7371 printk ("unrecovered SCSI parity error.\n");
7372 }
7373 if (cp->xerr_status & XE_EXTRA_DATA) {
7374 PRINT_ADDR(cmd);
7375 printk ("extraneous data discarded.\n");
7376 }
7377 if (cp->xerr_status & XE_BAD_PHASE) {
7378 PRINT_ADDR(cmd);
7379 printk ("illegal scsi phase (4/5).\n");
7380 }
7381 if (cp->xerr_status & XE_SODL_UNRUN) {
7382 PRINT_ADDR(cmd);
7383 printk ("ODD transfer in DATA OUT phase.\n");
7384 }
7385 if (cp->xerr_status & XE_SWIDE_OVRUN){
7386 PRINT_ADDR(cmd);
7387 printk ("ODD transfer in DATA IN phase.\n");
7388 }
7389
7390 if (cp->host_status==HS_COMPLETE)
7391 cp->host_status = HS_FAIL;
7392 }
7393
7394 /*
7395 ** Print out any error for debugging purpose.
7396 */
7397 if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
7398 if (cp->host_status!=HS_COMPLETE || cp->scsi_status!=S_GOOD ||
7399 cp->resid) {
7400 PRINT_ADDR(cmd);
7401 printk ("ERROR: cmd=%x host_status=%x scsi_status=%x "
7402 "data_len=%d residual=%d\n",
7403 cmd->cmnd[0], cp->host_status, cp->scsi_status,
7404 cp->data_len, cp->resid);
7405 }
7406 }
7407
7408 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,99)
7409 /*
7410 ** Move residual byte count to user structure.
7411 */
7412 cmd->resid = cp->resid;
7413 #endif
7414 /*
7415 ** Check the status.
7416 */
7417 if ( (cp->host_status == HS_COMPLETE)
7418 && (cp->scsi_status == S_GOOD ||
7419 cp->scsi_status == S_COND_MET)) {
7420 /*
7421 ** All went well (GOOD status).
7422 ** CONDITION MET status is returned on
7423 ** `Pre-Fetch' or `Search data' success.
7424 */
7425 SetScsiResult(cmd, DID_OK, cp->scsi_status);
7426
7427 /*
7428 ** Allocate the lcb if not yet.
7429 */
7430 if (!lp)
7431 ncr_alloc_lcb (np, cp->target, cp->lun);
7432
7433 /*
7434 ** On standard INQUIRY response (EVPD and CmDt
7435 ** not set), setup logical unit according to
7436 ** announced capabilities (we need the 1rst 8 bytes).
7437 */
7438 if (cmd->cmnd[0] == 0x12 && !(cmd->cmnd[1] & 0x3) &&
7439 cmd->request_bufflen - cp->resid > 7 && !cmd->use_sg) {
7440 sync_scsi_data(np, cmd); /* SYNC the data */
7441 ncr_setup_lcb (np, cp->target, cp->lun,
7442 (char *) cmd->request_buffer);
7443 }
7444
7445 /*
7446 ** If tags was reduced due to queue full,
7447 ** increase tags if 1000 good status received.
7448 */
7449 if (lp && lp->usetags && lp->numtags < lp->maxtags) {
7450 ++lp->num_good;
7451 if (lp->num_good >= 1000) {
7452 lp->num_good = 0;
7453 ++lp->numtags;
7454 ncr_setup_tags (np, cp->target, cp->lun);
7455 }
7456 }
7457 } else if ((cp->host_status == HS_COMPLETE)
7458 && (cp->scsi_status == S_CHECK_COND)) {
7459 /*
7460 ** Check condition code
7461 */
7462 SetScsiResult(cmd, DID_OK, S_CHECK_COND);
7463
7464 if (DEBUG_FLAGS & (DEBUG_RESULT|DEBUG_TINY)) {
7465 PRINT_ADDR(cmd);
7466 ncr_printl_hex("sense data:", cmd->sense_buffer, 14);
7467 }
7468 } else if ((cp->host_status == HS_COMPLETE)
7469 && (cp->scsi_status == S_CONFLICT)) {
7470 /*
7471 ** Reservation Conflict condition code
7472 */
7473 SetScsiResult(cmd, DID_OK, S_CONFLICT);
7474
7475 } else if ((cp->host_status == HS_COMPLETE)
7476 && (cp->scsi_status == S_BUSY ||
7477 cp->scsi_status == S_QUEUE_FULL)) {
7478
7479 /*
7480 ** Target is busy.
7481 */
7482 SetScsiResult(cmd, DID_OK, cp->scsi_status);
7483
7484 } else if ((cp->host_status == HS_SEL_TIMEOUT)
7485 || (cp->host_status == HS_TIMEOUT)) {
7486
7487 /*
7488 ** No response
7489 */
7490 SetScsiResult(cmd, DID_TIME_OUT, cp->scsi_status);
7491
7492 } else if (cp->host_status == HS_RESET) {
7493
7494 /*
7495 ** SCSI bus reset
7496 */
7497 SetScsiResult(cmd, DID_RESET, cp->scsi_status);
7498
7499 } else if (cp->host_status == HS_ABORTED) {
7500
7501 /*
7502 ** Transfer aborted
7503 */
7504 SetScsiAbortResult(cmd);
7505
7506 } else {
7507 int did_status;
7508
7509 /*
7510 ** Other protocol messes
7511 */
7512 PRINT_ADDR(cmd);
7513 printk ("COMMAND FAILED (%x %x) @%p.\n",
7514 cp->host_status, cp->scsi_status, cp);
7515
7516 did_status = DID_ERROR;
7517 if (cp->xerr_status & XE_PARITY_ERR)
7518 did_status = DID_PARITY;
7519
7520 SetScsiResult(cmd, did_status, cp->scsi_status);
7521 }
7522
7523 /*
7524 ** trace output
7525 */
7526
7527 if (tp->usrflag & UF_TRACE) {
7528 PRINT_ADDR(cmd);
7529 printk (" CMD:");
7530 ncr_print_hex(cmd->cmnd, cmd->cmd_len);
7531
7532 if (cp->host_status==HS_COMPLETE) {
7533 switch (cp->scsi_status) {
7534 case S_GOOD:
7535 printk (" GOOD");
7536 break;
7537 case S_CHECK_COND:
7538 printk (" SENSE:");
7539 ncr_print_hex(cmd->sense_buffer, 14);
7540 break;
7541 default:
7542 printk (" STAT: %x\n", cp->scsi_status);
7543 break;
7544 }
7545 } else printk (" HOSTERROR: %x", cp->host_status);
7546 printk ("\n");
7547 }
7548
7549 /*
7550 ** Free this ccb
7551 */
7552 ncr_free_ccb (np, cp);
7553
7554 /*
7555 ** requeue awaiting scsi commands for this lun.
7556 */
7557 if (lp && lp->queuedccbs < lp->queuedepth &&
7558 !xpt_que_empty(&lp->wait_ccbq))
7559 ncr_start_next_ccb(np, lp, 2);
7560
7561 /*
7562 ** requeue awaiting scsi commands for this controller.
7563 */
7564 if (np->waiting_list)
7565 requeue_waiting_list(np);
7566
7567 /*
7568 ** signal completion to generic driver.
7569 */
7570 ncr_queue_done_cmd(np, cmd);
7571 }
7572
7573 /*==========================================================
7574 **
7575 **
7576 ** Signal all (or one) control block done.
7577 **
7578 **
7579 **==========================================================
7580 */
7581
7582 /*
7583 ** The NCR has completed CCBs.
7584 ** Look at the DONE QUEUE.
7585 **
7586 ** On architectures that may reorder LOAD/STORE operations,
7587 ** a memory barrier may be needed after the reading of the
7588 ** so-called `flag' and prior to dealing with the data.
7589 */
7590 int ncr_wakeup_done (ncb_p np)
7591 {
7592 ccb_p cp;
7593 int i, n;
7594 u_long dsa;
7595
7596 n = 0;
7597 i = np->dqueueget;
7598 while (1) {
7599 dsa = scr_to_cpu(np->dqueue[i]);
7600 if (!dsa)
7601 break;
7602 np->dqueue[i] = 0;
7603 if ((i = i+2) >= MAX_START*2)
7604 i = 0;
7605
7606 cp = ncr_ccb_from_dsa(np, dsa);
7607 if (cp) {
7608 MEMORY_BARRIER();
7609 ncr_complete (np, cp);
7610 ++n;
7611 }
7612 else
7613 printk (KERN_ERR "%s: bad DSA (%lx) in done queue.\n",
7614 ncr_name(np), dsa);
7615 }
7616 np->dqueueget = i;
7617
7618 return n;
7619 }
7620
7621 /*
7622 ** Complete all active CCBs.
7623 */
7624 void ncr_wakeup (ncb_p np, u_long code)
7625 {
7626 ccb_p cp = np->ccbc;
7627
7628 while (cp) {
7629 if (cp->host_status != HS_IDLE) {
7630 cp->host_status = code;
7631 ncr_complete (np, cp);
7632 }
7633 cp = cp->link_ccb;
7634 }
7635 }
7636
7637 /*==========================================================
7638 **
7639 **
7640 ** Start NCR chip.
7641 **
7642 **
7643 **==========================================================
7644 */
7645
7646 void ncr_init (ncb_p np, int reset, char * msg, u_long code)
7647 {
7648 int i;
7649 u_long phys;
7650
7651 /*
7652 ** Reset chip if asked, otherwise just clear fifos.
7653 */
7654
7655 if (reset)
7656 ncr_soft_reset(np);
7657 else {
7658 OUTB (nc_stest3, TE|CSF);
7659 OUTONB (nc_ctest3, CLF);
7660 }
7661
7662 /*
7663 ** Message.
7664 */
7665
7666 if (msg) printk (KERN_INFO "%s: restart (%s).\n", ncr_name (np), msg);
7667
7668 /*
7669 ** Clear Start Queue
7670 */
7671 phys = np->p_squeue;
7672 np->queuedepth = MAX_START - 1; /* 1 entry needed as end marker */
7673 for (i = 0; i < MAX_START*2; i += 2) {
7674 np->squeue[i] = cpu_to_scr(np->p_idletask);
7675 np->squeue[i+1] = cpu_to_scr(phys + (i+2)*4);
7676 }
7677 np->squeue[MAX_START*2-1] = cpu_to_scr(phys);
7678
7679
7680 /*
7681 ** Start at first entry.
7682 */
7683 np->squeueput = 0;
7684 np->scripth0->startpos[0] = cpu_to_scr(phys);
7685
7686 /*
7687 ** Clear Done Queue
7688 */
7689 phys = vtobus(np->dqueue);
7690 for (i = 0; i < MAX_START*2; i += 2) {
7691 np->dqueue[i] = 0;
7692 np->dqueue[i+1] = cpu_to_scr(phys + (i+2)*4);
7693 }
7694 np->dqueue[MAX_START*2-1] = cpu_to_scr(phys);
7695
7696 /*
7697 ** Start at first entry.
7698 */
7699 np->scripth0->done_pos[0] = cpu_to_scr(phys);
7700 np->dqueueget = 0;
7701
7702 /*
7703 ** Wakeup all pending jobs.
7704 */
7705 ncr_wakeup (np, code);
7706
7707 /*
7708 ** Init chip.
7709 */
7710
7711 OUTB (nc_istat, 0x00 ); /* Remove Reset, abort */
7712 UDELAY (2000); /* The 895 needs time for the bus mode to settle */
7713
7714 OUTB (nc_scntl0, np->rv_scntl0 | 0xc0);
7715 /* full arb., ena parity, par->ATN */
7716 OUTB (nc_scntl1, 0x00); /* odd parity, and remove CRST!! */
7717
7718 ncr_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
7719
7720 OUTB (nc_scid , RRE|np->myaddr); /* Adapter SCSI address */
7721 OUTW (nc_respid, 1ul<<np->myaddr); /* Id to respond to */
7722 OUTB (nc_istat , SIGP ); /* Signal Process */
7723 OUTB (nc_dmode , np->rv_dmode); /* Burst length, dma mode */
7724 OUTB (nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */
7725
7726 OUTB (nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */
7727 OUTB (nc_ctest3, np->rv_ctest3); /* Write and invalidate */
7728 OUTB (nc_ctest4, np->rv_ctest4); /* Master parity checking */
7729
7730 if ((np->device_id != PCI_DEVICE_ID_LSI_53C1010) &&
7731 (np->device_id != PCI_DEVICE_ID_LSI_53C1010_66)){
7732 OUTB (nc_stest2, EXT|np->rv_stest2);
7733 /* Extended Sreq/Sack filtering, not supported in C1010/C1010_66 */
7734 }
7735 OUTB (nc_stest3, TE); /* TolerANT enable */
7736 OUTB (nc_stime0, 0x0c); /* HTH disabled STO 0.25 sec */
7737
7738 /*
7739 ** DEL 441 - 53C876 Rev 5 - Part Number 609-0392787/2788 - ITEM 2.
7740 ** Disable overlapped arbitration for all dual-function
7741 ** devices, regardless revision id.
7742 ** We may consider it is a post-chip-design feature. ;-)
7743 **
7744 ** Errata applies to all 896 and 1010 parts.
7745 */
7746 if (np->device_id == PCI_DEVICE_ID_NCR_53C875)
7747 OUTB (nc_ctest0, (1<<5));
7748 else if (np->device_id == PCI_DEVICE_ID_NCR_53C896 ||
7749 np->device_id == PCI_DEVICE_ID_LSI_53C1010 ||
7750 np->device_id == PCI_DEVICE_ID_LSI_53C1010_66 )
7751 np->rv_ccntl0 |= DPR;
7752
7753 /*
7754 ** C1010_66MHz rev 0 part requies AIPCNTL1 bit 3 to be set.
7755 */
7756 if (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66)
7757 OUTB(nc_aipcntl1, (1<<3));
7758
7759 /*
7760 ** Write CCNTL0/CCNTL1 for chips capable of 64 bit addressing
7761 ** and/or hardware phase mismatch, since only such chips
7762 ** seem to support those IO registers.
7763 */
7764 if (np->features & (FE_DAC | FE_NOPM)) {
7765 OUTB (nc_ccntl0, np->rv_ccntl0);
7766 OUTB (nc_ccntl1, np->rv_ccntl1);
7767 }
7768
7769 /*
7770 ** If phase mismatch handled by scripts (53C895A or 53C896
7771 ** or 53C1010 or 53C1010_66), set PM jump addresses.
7772 */
7773
7774 if (np->features & FE_NOPM) {
7775 printk(KERN_INFO "%s: handling phase mismatch from SCRIPTS.\n",
7776 ncr_name(np));
7777 OUTL (nc_pmjad1, NCB_SCRIPTH_PHYS (np, pm_handle));
7778 OUTL (nc_pmjad2, NCB_SCRIPTH_PHYS (np, pm_handle));
7779 }
7780
7781 /*
7782 ** Enable GPIO0 pin for writing if LED support from SCRIPTS.
7783 ** Also set GPIO5 and clear GPIO6 if hardware LED control.
7784 */
7785
7786 if (np->features & FE_LED0)
7787 OUTB(nc_gpcntl, INB(nc_gpcntl) & ~0x01);
7788 else if (np->features & FE_LEDC)
7789 OUTB(nc_gpcntl, (INB(nc_gpcntl) & ~0x41) | 0x20);
7790
7791
7792 /*
7793 ** enable ints
7794 */
7795
7796 OUTW (nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
7797 OUTB (nc_dien , MDPE|BF|SSI|SIR|IID);
7798
7799 /*
7800 ** For 895/895A/896/c1010
7801 ** Enable SBMC interrupt and save current SCSI bus mode.
7802 */
7803 if ( (np->features & FE_ULTRA2) || (np->features & FE_ULTRA3) ) {
7804 OUTONW (nc_sien, SBMC);
7805 np->scsi_mode = INB (nc_stest4) & SMODE;
7806 }
7807
7808 /*
7809 ** Fill in target structure.
7810 ** Reinitialize usrsync.
7811 ** Reinitialize usrwide.
7812 ** Prepare sync negotiation according to actual SCSI bus mode.
7813 */
7814
7815 for (i=0;i<MAX_TARGET;i++) {
7816 tcb_p tp = &np->target[i];
7817
7818 tp->to_reset = 0;
7819
7820 tp->sval = 0;
7821 tp->wval = np->rv_scntl3;
7822 tp->uval = np->rv_scntl4;
7823
7824 if (tp->usrsync != 255) {
7825 if (tp->usrsync <= np->maxsync) {
7826 if (tp->usrsync < np->minsync) {
7827 tp->usrsync = np->minsync;
7828 }
7829 }
7830 else
7831 tp->usrsync = 255;
7832 };
7833
7834 if (tp->usrwide > np->maxwide)
7835 tp->usrwide = np->maxwide;
7836
7837 ncr_negotiate (np, tp);
7838 }
7839
7840 /*
7841 ** Download SCSI SCRIPTS to on-chip RAM if present,
7842 ** and start script processor.
7843 ** We do the download preferently from the CPU.
7844 ** For platforms that may not support PCI memory mapping,
7845 ** we use a simple SCRIPTS that performs MEMORY MOVEs.
7846 */
7847 if (np->base2_ba) {
7848 if (bootverbose)
7849 printk ("%s: Downloading SCSI SCRIPTS.\n",
7850 ncr_name(np));
7851 #ifdef SCSI_NCR_PCI_MEM_NOT_SUPPORTED
7852 if (np->base2_ws == 8192)
7853 phys = NCB_SCRIPTH0_PHYS (np, start_ram64);
7854 else
7855 phys = NCB_SCRIPTH_PHYS (np, start_ram);
7856 #else
7857 if (np->base2_ws == 8192) {
7858 memcpy_to_pci(np->base2_va + 4096,
7859 np->scripth0, sizeof(struct scripth));
7860 OUTL (nc_mmws, np->scr_ram_seg);
7861 OUTL (nc_mmrs, np->scr_ram_seg);
7862 OUTL (nc_sfs, np->scr_ram_seg);
7863 phys = NCB_SCRIPTH_PHYS (np, start64);
7864 }
7865 else
7866 phys = NCB_SCRIPT_PHYS (np, init);
7867 memcpy_to_pci(np->base2_va, np->script0, sizeof(struct script));
7868 #endif /* SCSI_NCR_PCI_MEM_NOT_SUPPORTED */
7869 }
7870 else
7871 phys = NCB_SCRIPT_PHYS (np, init);
7872
7873 np->istat_sem = 0;
7874
7875 OUTL (nc_dsa, np->p_ncb);
7876 OUTL_DSP (phys);
7877 }
7878
7879 /*==========================================================
7880 **
7881 ** Prepare the negotiation values for wide and
7882 ** synchronous transfers.
7883 **
7884 **==========================================================
7885 */
7886
7887 static void ncr_negotiate (struct ncb* np, struct tcb* tp)
7888 {
7889 /*
7890 ** minsync unit is 4ns !
7891 */
7892
7893 u_long minsync = tp->usrsync;
7894
7895 /*
7896 ** SCSI bus mode limit
7897 */
7898
7899 if (np->scsi_mode && np->scsi_mode == SMODE_SE) {
7900 if (minsync < 12) minsync = 12;
7901 }
7902
7903 /*
7904 ** our limit ..
7905 */
7906
7907 if (minsync < np->minsync)
7908 minsync = np->minsync;
7909
7910 /*
7911 ** divider limit
7912 */
7913
7914 if (minsync > np->maxsync)
7915 minsync = 255;
7916
7917 tp->minsync = minsync;
7918 tp->maxoffs = (minsync<255 ? np->maxoffs : 0);
7919
7920 /*
7921 ** period=0: has to negotiate sync transfer
7922 */
7923
7924 tp->period=0;
7925
7926 /*
7927 ** widedone=0: has to negotiate wide transfer
7928 */
7929 tp->widedone=0;
7930 }
7931
7932 /*==========================================================
7933 **
7934 ** Get clock factor and sync divisor for a given
7935 ** synchronous factor period.
7936 ** Returns the clock factor (in sxfer) and scntl3
7937 ** synchronous divisor field.
7938 **
7939 **==========================================================
7940 */
7941
7942 static void ncr_getsync(ncb_p np, u_char sfac, u_char *fakp, u_char *scntl3p)
7943 {
7944 u_long clk = np->clock_khz; /* SCSI clock frequency in kHz */
7945 int div = np->clock_divn; /* Number of divisors supported */
7946 u_long fak; /* Sync factor in sxfer */
7947 u_long per; /* Period in tenths of ns */
7948 u_long kpc; /* (per * clk) */
7949
7950 /*
7951 ** Compute the synchronous period in tenths of nano-seconds
7952 ** from sfac.
7953 **
7954 ** Note, if sfac == 9, DT is being used. Double the period of 125
7955 ** to 250.
7956 */
7957 if (sfac <= 10) per = 250;
7958 else if (sfac == 11) per = 303;
7959 else if (sfac == 12) per = 500;
7960 else per = 40 * sfac;
7961
7962 /*
7963 ** Look for the greatest clock divisor that allows an
7964 ** input speed faster than the period.
7965 */
7966 kpc = per * clk;
7967 while (--div >= 0)
7968 if (kpc >= (div_10M[div] << 2)) break;
7969
7970 /*
7971 ** Calculate the lowest clock factor that allows an output
7972 ** speed not faster than the period.
7973 */
7974 fak = (kpc - 1) / div_10M[div] + 1;
7975
7976 #if 0 /* This optimization does not seem very useful */
7977
7978 per = (fak * div_10M[div]) / clk;
7979
7980 /*
7981 ** Why not to try the immediate lower divisor and to choose
7982 ** the one that allows the fastest output speed ?
7983 ** We don't want input speed too much greater than output speed.
7984 */
7985 if (div >= 1 && fak < 8) {
7986 u_long fak2, per2;
7987 fak2 = (kpc - 1) / div_10M[div-1] + 1;
7988 per2 = (fak2 * div_10M[div-1]) / clk;
7989 if (per2 < per && fak2 <= 8) {
7990 fak = fak2;
7991 per = per2;
7992 --div;
7993 }
7994 }
7995 #endif
7996
7997 if (fak < 4) fak = 4; /* Should never happen, too bad ... */
7998
7999 /*
8000 ** Compute and return sync parameters for the ncr
8001 */
8002 *fakp = fak - 4;
8003
8004 /*
8005 ** If sfac < 25, and 8xx parts, desire that the chip operate at
8006 ** least at Ultra speeds. Must set bit 7 of scntl3.
8007 ** For C1010, do not set this bit. If operating at Ultra3 speeds,
8008 ** set the U3EN bit instead.
8009 */
8010 if ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
8011 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66)) {
8012 *scntl3p = (div+1) << 4;
8013 *fakp = 0;
8014 }
8015 else {
8016 *scntl3p = ((div+1) << 4) + (sfac < 25 ? 0x80 : 0);
8017 *fakp = fak - 4;
8018 }
8019 }
8020
8021 /*==========================================================
8022 **
8023 ** Utility routine to return the current bus width
8024 ** synchronous period and offset.
8025 ** Utilizes target sval, wval and uval
8026 **
8027 **==========================================================
8028 */
8029 static void ncr_get_xfer_info(ncb_p np, tcb_p tp, u_char *factor,
8030 u_char *offset, u_char *width)
8031 {
8032
8033 u_char idiv;
8034 u_long period;
8035
8036 *width = (tp->wval & EWS) ? 1 : 0;
8037
8038 if ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
8039 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66))
8040 *offset = (tp->sval & 0x3f);
8041 else
8042 *offset = (tp->sval & 0x1f);
8043
8044 /*
8045 * Midlayer signal to the driver that all of the scsi commands
8046 * for the integrity check have completed. Save the negotiated
8047 * parameters (extracted from sval, wval and uval).
8048 * See ncr_setsync for alg. details.
8049 */
8050
8051 idiv = (tp->wval>>4) & 0x07;
8052
8053 if ( *offset && idiv ) {
8054 if ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
8055 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66)){
8056 if (tp->uval & 0x80)
8057 period = (2*div_10M[idiv-1])/np->clock_khz;
8058 else
8059 period = (4*div_10M[idiv-1])/np->clock_khz;
8060 }
8061 else
8062 period = (((tp->sval>>5)+4)*div_10M[idiv-1])/np->clock_khz;
8063 }
8064 else
8065 period = 0xffff;
8066
8067 if (period <= 125) *factor = 9;
8068 else if (period <= 250) *factor = 10;
8069 else if (period <= 303) *factor = 11;
8070 else if (period <= 500) *factor = 12;
8071 else *factor = (period + 40 - 1) / 40;
8072
8073 }
8074
8075
8076 /*==========================================================
8077 **
8078 ** Set actual values, sync status and patch all ccbs of
8079 ** a target according to new sync/wide agreement.
8080 **
8081 **==========================================================
8082 */
8083
8084 static void ncr_set_sync_wide_status (ncb_p np, u_char target)
8085 {
8086 ccb_p cp = np->ccbc;
8087 tcb_p tp = &np->target[target];
8088
8089 /*
8090 ** set actual value and sync_status
8091 **
8092 ** TEMP register contains current scripts address
8093 ** which is data type/direction/dependent.
8094 */
8095 OUTB (nc_sxfer, tp->sval);
8096 OUTB (nc_scntl3, tp->wval);
8097 if ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
8098 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66))
8099 OUTB (nc_scntl4, tp->uval);
8100
8101 /*
8102 ** patch ALL ccbs of this target.
8103 */
8104 for (cp = np->ccbc; cp; cp = cp->link_ccb) {
8105 if (cp->host_status == HS_IDLE)
8106 continue;
8107 if (cp->target != target)
8108 continue;
8109 cp->phys.select.sel_scntl3 = tp->wval;
8110 cp->phys.select.sel_sxfer = tp->sval;
8111 if ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
8112 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66))
8113 cp->phys.select.sel_scntl4 = tp->uval;
8114 };
8115 }
8116
8117 /*==========================================================
8118 **
8119 ** Switch sync mode for current job and it's target
8120 **
8121 **==========================================================
8122 */
8123
8124 static void ncr_setsync (ncb_p np, ccb_p cp, u_char scntl3, u_char sxfer,
8125 u_char scntl4)
8126 {
8127 tcb_p tp;
8128 u_char target = INB (nc_sdid) & 0x0f;
8129 u_char idiv;
8130 u_char offset;
8131
8132 assert (cp);
8133 if (!cp) return;
8134
8135 assert (target == (cp->target & 0xf));
8136
8137 tp = &np->target[target];
8138
8139 if ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
8140 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66)) {
8141 offset = sxfer & 0x3f; /* bits 5-0 */
8142 scntl3 = (scntl3 & 0xf0) | (tp->wval & EWS);
8143 scntl4 = (scntl4 & 0x80);
8144 }
8145 else {
8146 offset = sxfer & 0x1f; /* bits 4-0 */
8147 if (!scntl3 || !offset)
8148 scntl3 = np->rv_scntl3;
8149
8150 scntl3 = (scntl3 & 0xf0) | (tp->wval & EWS) |
8151 (np->rv_scntl3 & 0x07);
8152 }
8153
8154
8155 /*
8156 ** Deduce the value of controller sync period from scntl3.
8157 ** period is in tenths of nano-seconds.
8158 */
8159
8160 idiv = ((scntl3 >> 4) & 0x7);
8161 if ( offset && idiv) {
8162 if ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
8163 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66)) {
8164 /* Note: If extra data hold clocks are used,
8165 * the formulas below must be modified.
8166 * When scntl4 == 0, ST mode.
8167 */
8168 if (scntl4 & 0x80)
8169 tp->period = (2*div_10M[idiv-1])/np->clock_khz;
8170 else
8171 tp->period = (4*div_10M[idiv-1])/np->clock_khz;
8172 }
8173 else
8174 tp->period = (((sxfer>>5)+4)*div_10M[idiv-1])/np->clock_khz;
8175 }
8176 else
8177 tp->period = 0xffff;
8178
8179
8180 /*
8181 ** Stop there if sync parameters are unchanged
8182 */
8183 if (tp->sval == sxfer && tp->wval == scntl3 && tp->uval == scntl4) return;
8184 tp->sval = sxfer;
8185 tp->wval = scntl3;
8186 tp->uval = scntl4;
8187
8188 /*
8189 ** Bells and whistles ;-)
8190 ** Donnot announce negotiations due to auto-sense,
8191 ** unless user really want us to be verbose. :)
8192 */
8193 if ( bootverbose < 2 && (cp->host_flags & HF_AUTO_SENSE))
8194 goto next;
8195 PRINT_TARGET(np, target);
8196 if (offset) {
8197 unsigned f10 = 100000 << (tp->widedone ? tp->widedone -1 : 0);
8198 unsigned mb10 = (f10 + tp->period/2) / tp->period;
8199 char *scsi;
8200
8201 /*
8202 ** Disable extended Sreq/Sack filtering
8203 */
8204 if ((tp->period <= 2000) &&
8205 (np->device_id != PCI_DEVICE_ID_LSI_53C1010) &&
8206 (np->device_id != PCI_DEVICE_ID_LSI_53C1010_66))
8207 OUTOFFB (nc_stest2, EXT);
8208
8209 /*
8210 ** Bells and whistles ;-)
8211 */
8212 if (tp->period < 250) scsi = "FAST-80";
8213 else if (tp->period < 500) scsi = "FAST-40";
8214 else if (tp->period < 1000) scsi = "FAST-20";
8215 else if (tp->period < 2000) scsi = "FAST-10";
8216 else scsi = "FAST-5";
8217
8218 printk ("%s %sSCSI %d.%d MB/s (%d.%d ns, offset %d)\n", scsi,
8219 tp->widedone > 1 ? "WIDE " : "",
8220 mb10 / 10, mb10 % 10, tp->period / 10, tp->period % 10,
8221 offset);
8222 } else
8223 printk ("%sasynchronous.\n", tp->widedone > 1 ? "wide " : "");
8224 next:
8225 /*
8226 ** set actual value and sync_status
8227 ** patch ALL ccbs of this target.
8228 */
8229 ncr_set_sync_wide_status(np, target);
8230 }
8231
8232
8233 /*==========================================================
8234 **
8235 ** Switch wide mode for current job and it's target
8236 ** SCSI specs say: a SCSI device that accepts a WDTR
8237 ** message shall reset the synchronous agreement to
8238 ** asynchronous mode.
8239 **
8240 **==========================================================
8241 */
8242
8243 static void ncr_setwide (ncb_p np, ccb_p cp, u_char wide, u_char ack)
8244 {
8245 u_short target = INB (nc_sdid) & 0x0f;
8246 tcb_p tp;
8247 u_char scntl3;
8248 u_char sxfer;
8249
8250 assert (cp);
8251 if (!cp) return;
8252
8253 assert (target == (cp->target & 0xf));
8254
8255 tp = &np->target[target];
8256 tp->widedone = wide+1;
8257 scntl3 = (tp->wval & (~EWS)) | (wide ? EWS : 0);
8258
8259 sxfer = ack ? 0 : tp->sval;
8260
8261 /*
8262 ** Stop there if sync/wide parameters are unchanged
8263 */
8264 if (tp->sval == sxfer && tp->wval == scntl3) return;
8265 tp->sval = sxfer;
8266 tp->wval = scntl3;
8267
8268 /*
8269 ** Bells and whistles ;-)
8270 */
8271 if (bootverbose >= 2) {
8272 PRINT_TARGET(np, target);
8273 if (scntl3 & EWS)
8274 printk ("WIDE SCSI (16 bit) enabled.\n");
8275 else
8276 printk ("WIDE SCSI disabled.\n");
8277 }
8278
8279 /*
8280 ** set actual value and sync_status
8281 ** patch ALL ccbs of this target.
8282 */
8283 ncr_set_sync_wide_status(np, target);
8284 }
8285
8286
8287 /*==========================================================
8288 **
8289 ** Switch sync/wide mode for current job and it's target
8290 ** PPR negotiations only
8291 **
8292 **==========================================================
8293 */
8294
8295 static void ncr_setsyncwide (ncb_p np, ccb_p cp, u_char scntl3, u_char sxfer,
8296 u_char scntl4, u_char wide)
8297 {
8298 tcb_p tp;
8299 u_char target = INB (nc_sdid) & 0x0f;
8300 u_char idiv;
8301 u_char offset;
8302
8303 assert (cp);
8304 if (!cp) return;
8305
8306 assert (target == (cp->target & 0xf));
8307
8308 tp = &np->target[target];
8309 tp->widedone = wide+1;
8310
8311 if ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
8312 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66)) {
8313 offset = sxfer & 0x3f; /* bits 5-0 */
8314 scntl3 = (scntl3 & 0xf0) | (wide ? EWS : 0);
8315 scntl4 = (scntl4 & 0x80);
8316 }
8317 else {
8318 offset = sxfer & 0x1f; /* bits 4-0 */
8319 if (!scntl3 || !offset)
8320 scntl3 = np->rv_scntl3;
8321
8322 scntl3 = (scntl3 & 0xf0) | (wide ? EWS : 0) |
8323 (np->rv_scntl3 & 0x07);
8324 }
8325
8326
8327 /*
8328 ** Deduce the value of controller sync period from scntl3.
8329 ** period is in tenths of nano-seconds.
8330 */
8331
8332 idiv = ((scntl3 >> 4) & 0x7);
8333 if ( offset && idiv) {
8334 if ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
8335 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66)) {
8336 /* Note: If extra data hold clocks are used,
8337 * the formulas below must be modified.
8338 * When scntl4 == 0, ST mode.
8339 */
8340 if (scntl4 & 0x80)
8341 tp->period = (2*div_10M[idiv-1])/np->clock_khz;
8342 else
8343 tp->period = (4*div_10M[idiv-1])/np->clock_khz;
8344 }
8345 else
8346 tp->period = (((sxfer>>5)+4)*div_10M[idiv-1])/np->clock_khz;
8347 }
8348 else
8349 tp->period = 0xffff;
8350
8351
8352 /*
8353 ** Stop there if sync parameters are unchanged
8354 */
8355 if (tp->sval == sxfer && tp->wval == scntl3 && tp->uval == scntl4) return;
8356 tp->sval = sxfer;
8357 tp->wval = scntl3;
8358 tp->uval = scntl4;
8359
8360 /*
8361 ** Bells and whistles ;-)
8362 ** Donnot announce negotiations due to auto-sense,
8363 ** unless user really want us to be verbose. :)
8364 */
8365 if ( bootverbose < 2 && (cp->host_flags & HF_AUTO_SENSE))
8366 goto next;
8367 PRINT_TARGET(np, target);
8368 if (offset) {
8369 unsigned f10 = 100000 << (tp->widedone ? tp->widedone -1 : 0);
8370 unsigned mb10 = (f10 + tp->period/2) / tp->period;
8371 char *scsi;
8372
8373 /*
8374 ** Disable extended Sreq/Sack filtering
8375 */
8376 if ((tp->period <= 2000) &&
8377 (np->device_id != PCI_DEVICE_ID_LSI_53C1010) &&
8378 (np->device_id != PCI_DEVICE_ID_LSI_53C1010_66))
8379 OUTOFFB (nc_stest2, EXT);
8380
8381 /*
8382 ** Bells and whistles ;-)
8383 */
8384 if (tp->period < 250) scsi = "FAST-80";
8385 else if (tp->period < 500) scsi = "FAST-40";
8386 else if (tp->period < 1000) scsi = "FAST-20";
8387 else if (tp->period < 2000) scsi = "FAST-10";
8388 else scsi = "FAST-5";
8389
8390 printk ("%s %sSCSI %d.%d MB/s (%d.%d ns, offset %d)\n", scsi,
8391 tp->widedone > 1 ? "WIDE " : "",
8392 mb10 / 10, mb10 % 10, tp->period / 10, tp->period % 10,
8393 offset);
8394 } else
8395 printk ("%sasynchronous.\n", tp->widedone > 1 ? "wide " : "");
8396 next:
8397 /*
8398 ** set actual value and sync_status
8399 ** patch ALL ccbs of this target.
8400 */
8401 ncr_set_sync_wide_status(np, target);
8402 }
8403
8404
8405
8406
8407 /*==========================================================
8408 **
8409 ** Switch tagged mode for a target.
8410 **
8411 **==========================================================
8412 */
8413
8414 static void ncr_setup_tags (ncb_p np, u_char tn, u_char ln)
8415 {
8416 tcb_p tp = &np->target[tn];
8417 lcb_p lp = ncr_lp(np, tp, ln);
8418 u_short reqtags, maxdepth;
8419
8420 /*
8421 ** Just in case ...
8422 */
8423 if ((!tp) || (!lp))
8424 return;
8425
8426 /*
8427 ** If SCSI device queue depth is not yet set, leave here.
8428 */
8429 if (!lp->scdev_depth)
8430 return;
8431
8432 /*
8433 ** Donnot allow more tags than the SCSI driver can queue
8434 ** for this device.
8435 ** Donnot allow more tags than we can handle.
8436 */
8437 maxdepth = lp->scdev_depth;
8438 if (maxdepth > lp->maxnxs) maxdepth = lp->maxnxs;
8439 if (lp->maxtags > maxdepth) lp->maxtags = maxdepth;
8440 if (lp->numtags > maxdepth) lp->numtags = maxdepth;
8441
8442 /*
8443 ** only devices conformant to ANSI Version >= 2
8444 ** only devices capable of tagged commands
8445 ** only if enabled by user ..
8446 */
8447 if ((lp->inq_byte7 & INQ7_QUEUE) && lp->numtags > 1) {
8448 reqtags = lp->numtags;
8449 } else {
8450 reqtags = 1;
8451 };
8452
8453 /*
8454 ** Update max number of tags
8455 */
8456 lp->numtags = reqtags;
8457 if (lp->numtags > lp->maxtags)
8458 lp->maxtags = lp->numtags;
8459
8460 /*
8461 ** If we want to switch tag mode, we must wait
8462 ** for no CCB to be active.
8463 */
8464 if (reqtags > 1 && lp->usetags) { /* Stay in tagged mode */
8465 if (lp->queuedepth == reqtags) /* Already announced */
8466 return;
8467 lp->queuedepth = reqtags;
8468 }
8469 else if (reqtags <= 1 && !lp->usetags) { /* Stay in untagged mode */
8470 lp->queuedepth = reqtags;
8471 return;
8472 }
8473 else { /* Want to switch tag mode */
8474 if (lp->busyccbs) /* If not yet safe, return */
8475 return;
8476 lp->queuedepth = reqtags;
8477 lp->usetags = reqtags > 1 ? 1 : 0;
8478 }
8479
8480 /*
8481 ** Patch the lun mini-script, according to tag mode.
8482 */
8483 lp->resel_task = lp->usetags?
8484 cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_tag)) :
8485 cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_notag));
8486
8487 /*
8488 ** Announce change to user.
8489 */
8490 if (bootverbose) {
8491 PRINT_LUN(np, tn, ln);
8492 if (lp->usetags)
8493 printk("tagged command queue depth set to %d\n", reqtags);
8494 else
8495 printk("tagged command queueing disabled\n");
8496 }
8497 }
8498
8499 /*----------------------------------------------------
8500 **
8501 ** handle user commands
8502 **
8503 **----------------------------------------------------
8504 */
8505
8506 #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
8507
8508 static void ncr_usercmd (ncb_p np)
8509 {
8510 u_char t;
8511 tcb_p tp;
8512 int ln;
8513 u_long size;
8514
8515 switch (np->user.cmd) {
8516 case 0: return;
8517
8518 case UC_SETDEBUG:
8519 #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
8520 ncr_debug = np->user.data;
8521 #endif
8522 break;
8523
8524 case UC_SETORDER:
8525 np->order = np->user.data;
8526 break;
8527
8528 case UC_SETVERBOSE:
8529 np->verbose = np->user.data;
8530 break;
8531
8532 default:
8533 /*
8534 ** We assume that other commands apply to targets.
8535 ** This should always be the case and avoid the below
8536 ** 4 lines to be repeated 5 times.
8537 */
8538 for (t = 0; t < MAX_TARGET; t++) {
8539 if (!((np->user.target >> t) & 1))
8540 continue;
8541 tp = &np->target[t];
8542
8543 switch (np->user.cmd) {
8544
8545 case UC_SETSYNC:
8546 tp->usrsync = np->user.data;
8547 ncr_negotiate (np, tp);
8548 break;
8549
8550 case UC_SETWIDE:
8551 size = np->user.data;
8552 if (size > np->maxwide)
8553 size=np->maxwide;
8554 tp->usrwide = size;
8555 ncr_negotiate (np, tp);
8556 break;
8557
8558 case UC_SETTAGS:
8559 tp->usrtags = np->user.data;
8560 for (ln = 0; ln < MAX_LUN; ln++) {
8561 lcb_p lp;
8562 lp = ncr_lp(np, tp, ln);
8563 if (!lp)
8564 continue;
8565 lp->numtags = np->user.data;
8566 lp->maxtags = lp->numtags;
8567 ncr_setup_tags (np, t, ln);
8568 }
8569 break;
8570
8571 case UC_RESETDEV:
8572 tp->to_reset = 1;
8573 np->istat_sem = SEM;
8574 OUTB (nc_istat, SIGP|SEM);
8575 break;
8576
8577 case UC_CLEARDEV:
8578 for (ln = 0; ln < MAX_LUN; ln++) {
8579 lcb_p lp;
8580 lp = ncr_lp(np, tp, ln);
8581 if (lp)
8582 lp->to_clear = 1;
8583 }
8584 np->istat_sem = SEM;
8585 OUTB (nc_istat, SIGP|SEM);
8586 break;
8587
8588 case UC_SETFLAG:
8589 tp->usrflag = np->user.data;
8590 break;
8591 }
8592 }
8593 break;
8594 }
8595 np->user.cmd=0;
8596 }
8597 #endif
8598
8599 /*==========================================================
8600 **
8601 **
8602 ** ncr timeout handler.
8603 **
8604 **
8605 **==========================================================
8606 **
8607 ** Misused to keep the driver running when
8608 ** interrupts are not configured correctly.
8609 **
8610 **----------------------------------------------------------
8611 */
8612
8613 static void ncr_timeout (ncb_p np)
8614 {
8615 u_long thistime = ktime_get(0);
8616
8617 /*
8618 ** If release process in progress, let's go
8619 ** Set the release stage from 1 to 2 to synchronize
8620 ** with the release process.
8621 */
8622
8623 if (np->release_stage) {
8624 if (np->release_stage == 1) np->release_stage = 2;
8625 return;
8626 }
8627
8628 #ifdef SCSI_NCR_PCIQ_BROKEN_INTR
8629 np->timer.expires = ktime_get((HZ+9)/10);
8630 #else
8631 np->timer.expires = ktime_get(SCSI_NCR_TIMER_INTERVAL);
8632 #endif
8633 add_timer(&np->timer);
8634
8635 /*
8636 ** If we are resetting the ncr, wait for settle_time before
8637 ** clearing it. Then command processing will be resumed.
8638 */
8639 if (np->settle_time) {
8640 if (np->settle_time <= thistime) {
8641 if (bootverbose > 1)
8642 printk("%s: command processing resumed\n", ncr_name(np));
8643 np->settle_time = 0;
8644 requeue_waiting_list(np);
8645 }
8646 return;
8647 }
8648
8649 /*
8650 ** Nothing to do for now, but that may come.
8651 */
8652 if (np->lasttime + 4*HZ < thistime) {
8653 np->lasttime = thistime;
8654 }
8655
8656 #ifdef SCSI_NCR_PCIQ_MAY_MISS_COMPLETIONS
8657 /*
8658 ** Some way-broken PCI bridges may lead to
8659 ** completions being lost when the clearing
8660 ** of the INTFLY flag by the CPU occurs
8661 ** concurrently with the chip raising this flag.
8662 ** If this ever happen, lost completions will
8663 ** be reaped here.
8664 */
8665 ncr_wakeup_done(np);
8666 #endif
8667
8668 #ifdef SCSI_NCR_PCIQ_BROKEN_INTR
8669 if (INB(nc_istat) & (INTF|SIP|DIP)) {
8670
8671 /*
8672 ** Process pending interrupts.
8673 */
8674 if (DEBUG_FLAGS & DEBUG_TINY) printk ("{");
8675 ncr_exception (np);
8676 if (DEBUG_FLAGS & DEBUG_TINY) printk ("}");
8677 }
8678 #endif /* SCSI_NCR_PCIQ_BROKEN_INTR */
8679 }
8680
8681 /*==========================================================
8682 **
8683 ** log message for real hard errors
8684 **
8685 ** "ncr0 targ 0?: ERROR (ds:si) (so-si-sd) (sxfer/scntl3) @ name (dsp:dbc)."
8686 ** " reg: r0 r1 r2 r3 r4 r5 r6 ..... rf."
8687 **
8688 ** exception register:
8689 ** ds: dstat
8690 ** si: sist
8691 **
8692 ** SCSI bus lines:
8693 ** so: control lines as driver by NCR.
8694 ** si: control lines as seen by NCR.
8695 ** sd: scsi data lines as seen by NCR.
8696 **
8697 ** wide/fastmode:
8698 ** sxfer: (see the manual)
8699 ** scntl3: (see the manual)
8700 **
8701 ** current script command:
8702 ** dsp: script address (relative to start of script).
8703 ** dbc: first word of script command.
8704 **
8705 ** First 24 register of the chip:
8706 ** r0..rf
8707 **
8708 **==========================================================
8709 */
8710
8711 static void ncr_log_hard_error(ncb_p np, u_short sist, u_char dstat)
8712 {
8713 u_int32 dsp;
8714 int script_ofs;
8715 int script_size;
8716 char *script_name;
8717 u_char *script_base;
8718 int i;
8719
8720 dsp = INL (nc_dsp);
8721
8722 if (dsp > np->p_script && dsp <= np->p_script + sizeof(struct script)) {
8723 script_ofs = dsp - np->p_script;
8724 script_size = sizeof(struct script);
8725 script_base = (u_char *) np->script0;
8726 script_name = "script";
8727 }
8728 else if (np->p_scripth < dsp &&
8729 dsp <= np->p_scripth + sizeof(struct scripth)) {
8730 script_ofs = dsp - np->p_scripth;
8731 script_size = sizeof(struct scripth);
8732 script_base = (u_char *) np->scripth0;
8733 script_name = "scripth";
8734 } else {
8735 script_ofs = dsp;
8736 script_size = 0;
8737 script_base = 0;
8738 script_name = "mem";
8739 }
8740
8741 printk ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x) @ (%s %x:%08x).\n",
8742 ncr_name (np), (unsigned)INB (nc_sdid)&0x0f, dstat, sist,
8743 (unsigned)INB (nc_socl), (unsigned)INB (nc_sbcl), (unsigned)INB (nc_sbdl),
8744 (unsigned)INB (nc_sxfer),(unsigned)INB (nc_scntl3), script_name, script_ofs,
8745 (unsigned)INL (nc_dbc));
8746
8747 if (((script_ofs & 3) == 0) &&
8748 (unsigned)script_ofs < script_size) {
8749 printk ("%s: script cmd = %08x\n", ncr_name(np),
8750 scr_to_cpu((int) *(ncrcmd *)(script_base + script_ofs)));
8751 }
8752
8753 printk ("%s: regdump:", ncr_name(np));
8754 for (i=0; i<24;i++)
8755 printk (" %02x", (unsigned)INB_OFF(i));
8756 printk (".\n");
8757 }
8758
8759 /*============================================================
8760 **
8761 ** ncr chip exception handler.
8762 **
8763 **============================================================
8764 **
8765 ** In normal situations, interrupt conditions occur one at
8766 ** a time. But when something bad happens on the SCSI BUS,
8767 ** the chip may raise several interrupt flags before
8768 ** stopping and interrupting the CPU. The additionnal
8769 ** interrupt flags are stacked in some extra registers
8770 ** after the SIP and/or DIP flag has been raised in the
8771 ** ISTAT. After the CPU has read the interrupt condition
8772 ** flag from SIST or DSTAT, the chip unstacks the other
8773 ** interrupt flags and sets the corresponding bits in
8774 ** SIST or DSTAT. Since the chip starts stacking once the
8775 ** SIP or DIP flag is set, there is a small window of time
8776 ** where the stacking does not occur.
8777 **
8778 ** Typically, multiple interrupt conditions may happen in
8779 ** the following situations:
8780 **
8781 ** - SCSI parity error + Phase mismatch (PAR|MA)
8782 ** When an parity error is detected in input phase
8783 ** and the device switches to msg-in phase inside a
8784 ** block MOV.
8785 ** - SCSI parity error + Unexpected disconnect (PAR|UDC)
8786 ** When a stupid device does not want to handle the
8787 ** recovery of an SCSI parity error.
8788 ** - Some combinations of STO, PAR, UDC, ...
8789 ** When using non compliant SCSI stuff, when user is
8790 ** doing non compliant hot tampering on the BUS, when
8791 ** something really bad happens to a device, etc ...
8792 **
8793 ** The heuristic suggested by SYMBIOS to handle
8794 ** multiple interrupts is to try unstacking all
8795 ** interrupts conditions and to handle them on some
8796 ** priority based on error severity.
8797 ** This will work when the unstacking has been
8798 ** successful, but we cannot be 100 % sure of that,
8799 ** since the CPU may have been faster to unstack than
8800 ** the chip is able to stack. Hmmm ... But it seems that
8801 ** such a situation is very unlikely to happen.
8802 **
8803 ** If this happen, for example STO catched by the CPU
8804 ** then UDC happenning before the CPU have restarted
8805 ** the SCRIPTS, the driver may wrongly complete the
8806 ** same command on UDC, since the SCRIPTS didn't restart
8807 ** and the DSA still points to the same command.
8808 ** We avoid this situation by setting the DSA to an
8809 ** invalid value when the CCB is completed and before
8810 ** restarting the SCRIPTS.
8811 **
8812 ** Another issue is that we need some section of our
8813 ** recovery procedures to be somehow uninterruptible and
8814 ** that the SCRIPTS processor does not provides such a
8815 ** feature. For this reason, we handle recovery preferently
8816 ** from the C code and check against some SCRIPTS
8817 ** critical sections from the C code.
8818 **
8819 ** Hopefully, the interrupt handling of the driver is now
8820 ** able to resist to weird BUS error conditions, but donnot
8821 ** ask me for any guarantee that it will never fail. :-)
8822 ** Use at your own decision and risk.
8823 **
8824 **============================================================
8825 */
8826
8827 void ncr_exception (ncb_p np)
8828 {
8829 u_char istat, istatc;
8830 u_char dstat;
8831 u_short sist;
8832 int i;
8833
8834 /*
8835 ** interrupt on the fly ?
8836 **
8837 ** A `dummy read' is needed to ensure that the
8838 ** clear of the INTF flag reaches the device
8839 ** before the scanning of the DONE queue.
8840 */
8841 istat = INB (nc_istat);
8842 if (istat & INTF) {
8843 OUTB (nc_istat, (istat & SIGP) | INTF | np->istat_sem);
8844 istat = INB (nc_istat); /* DUMMY READ */
8845 if (DEBUG_FLAGS & DEBUG_TINY) printk ("F ");
8846 (void)ncr_wakeup_done (np);
8847 };
8848
8849 if (!(istat & (SIP|DIP)))
8850 return;
8851
8852 #if 0 /* We should never get this one */
8853 if (istat & CABRT)
8854 OUTB (nc_istat, CABRT);
8855 #endif
8856
8857 /*
8858 ** Steinbach's Guideline for Systems Programming:
8859 ** Never test for an error condition you don't know how to handle.
8860 */
8861
8862 /*========================================================
8863 ** PAR and MA interrupts may occur at the same time,
8864 ** and we need to know of both in order to handle
8865 ** this situation properly. We try to unstack SCSI
8866 ** interrupts for that reason. BTW, I dislike a LOT
8867 ** such a loop inside the interrupt routine.
8868 ** Even if DMA interrupt stacking is very unlikely to
8869 ** happen, we also try unstacking these ones, since
8870 ** this has no performance impact.
8871 **=========================================================
8872 */
8873 sist = 0;
8874 dstat = 0;
8875 istatc = istat;
8876 do {
8877 if (istatc & SIP)
8878 sist |= INW (nc_sist);
8879 if (istatc & DIP)
8880 dstat |= INB (nc_dstat);
8881 istatc = INB (nc_istat);
8882 istat |= istatc;
8883 } while (istatc & (SIP|DIP));
8884
8885 if (DEBUG_FLAGS & DEBUG_TINY)
8886 printk ("<%d|%x:%x|%x:%x>",
8887 (int)INB(nc_scr0),
8888 dstat,sist,
8889 (unsigned)INL(nc_dsp),
8890 (unsigned)INL(nc_dbc));
8891
8892 /*
8893 ** On paper, a memory barrier may be needed here.
8894 ** And since we are paranoid ... :)
8895 */
8896 MEMORY_BARRIER();
8897
8898 /*========================================================
8899 ** First, interrupts we want to service cleanly.
8900 **
8901 ** Phase mismatch (MA) is the most frequent interrupt
8902 ** for chip earlier than the 896 and so we have to service
8903 ** it as quickly as possible.
8904 ** A SCSI parity error (PAR) may be combined with a phase
8905 ** mismatch condition (MA).
8906 ** Programmed interrupts (SIR) are used to call the C code
8907 ** from SCRIPTS.
8908 ** The single step interrupt (SSI) is not used in this
8909 ** driver.
8910 **=========================================================
8911 */
8912
8913 if (!(sist & (STO|GEN|HTH|SGE|UDC|SBMC|RST)) &&
8914 !(dstat & (MDPE|BF|ABRT|IID))) {
8915 if (sist & PAR) ncr_int_par (np, sist);
8916 else if (sist & MA) ncr_int_ma (np);
8917 else if (dstat & SIR) ncr_int_sir (np);
8918 else if (dstat & SSI) OUTONB_STD ();
8919 else goto unknown_int;
8920 return;
8921 };
8922
8923 /*========================================================
8924 ** Now, interrupts that donnot happen in normal
8925 ** situations and that we may need to recover from.
8926 **
8927 ** On SCSI RESET (RST), we reset everything.
8928 ** On SCSI BUS MODE CHANGE (SBMC), we complete all
8929 ** active CCBs with RESET status, prepare all devices
8930 ** for negotiating again and restart the SCRIPTS.
8931 ** On STO and UDC, we complete the CCB with the corres-
8932 ** ponding status and restart the SCRIPTS.
8933 **=========================================================
8934 */
8935
8936 if (sist & RST) {
8937 ncr_init (np, 1, bootverbose ? "scsi reset" : NULL, HS_RESET);
8938 return;
8939 };
8940
8941 OUTB (nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
8942 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
8943
8944 if (!(sist & (GEN|HTH|SGE)) &&
8945 !(dstat & (MDPE|BF|ABRT|IID))) {
8946 if (sist & SBMC) ncr_int_sbmc (np);
8947 else if (sist & STO) ncr_int_sto (np);
8948 else if (sist & UDC) ncr_int_udc (np);
8949 else goto unknown_int;
8950 return;
8951 };
8952
8953 /*=========================================================
8954 ** Now, interrupts we are not able to recover cleanly.
8955 **
8956 ** Do the register dump.
8957 ** Log message for hard errors.
8958 ** Reset everything.
8959 **=========================================================
8960 */
8961 if (ktime_exp(np->regtime)) {
8962 np->regtime = ktime_get(10*HZ);
8963 for (i = 0; i<sizeof(np->regdump); i++)
8964 ((char*)&np->regdump)[i] = INB_OFF(i);
8965 np->regdump.nc_dstat = dstat;
8966 np->regdump.nc_sist = sist;
8967 };
8968
8969 ncr_log_hard_error(np, sist, dstat);
8970
8971 if ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
8972 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66)) {
8973 u_char ctest4_o, ctest4_m;
8974 u_char shadow;
8975
8976 /*
8977 * Get shadow register data
8978 * Write 1 to ctest4
8979 */
8980 ctest4_o = INB(nc_ctest4);
8981
8982 OUTB(nc_ctest4, ctest4_o | 0x10);
8983
8984 ctest4_m = INB(nc_ctest4);
8985 shadow = INW_OFF(0x42);
8986
8987 OUTB(nc_ctest4, ctest4_o);
8988
8989 printk("%s: ctest4/sist original 0x%x/0x%X mod: 0x%X/0x%x\n",
8990 ncr_name(np), ctest4_o, sist, ctest4_m, shadow);
8991 }
8992
8993 if ((sist & (GEN|HTH|SGE)) ||
8994 (dstat & (MDPE|BF|ABRT|IID))) {
8995 ncr_start_reset(np);
8996 return;
8997 };
8998
8999 unknown_int:
9000 /*=========================================================
9001 ** We just miss the cause of the interrupt. :(
9002 ** Print a message. The timeout will do the real work.
9003 **=========================================================
9004 */
9005 printk( "%s: unknown interrupt(s) ignored, "
9006 "ISTAT=0x%x DSTAT=0x%x SIST=0x%x\n",
9007 ncr_name(np), istat, dstat, sist);
9008 }
9009
9010
9011 /*==========================================================
9012 **
9013 ** generic recovery from scsi interrupt
9014 **
9015 **==========================================================
9016 **
9017 ** The doc says that when the chip gets an SCSI interrupt,
9018 ** it tries to stop in an orderly fashion, by completing
9019 ** an instruction fetch that had started or by flushing
9020 ** the DMA fifo for a write to memory that was executing.
9021 ** Such a fashion is not enough to know if the instruction
9022 ** that was just before the current DSP value has been
9023 ** executed or not.
9024 **
9025 ** There are 3 small SCRIPTS sections that deal with the
9026 ** start queue and the done queue that may break any
9027 ** assomption from the C code if we are interrupted
9028 ** inside, so we reset if it happens. Btw, since these
9029 ** SCRIPTS sections are executed while the SCRIPTS hasn't
9030 ** started SCSI operations, it is very unlikely to happen.
9031 **
9032 ** All the driver data structures are supposed to be
9033 ** allocated from the same 4 GB memory window, so there
9034 ** is a 1 to 1 relationship between DSA and driver data
9035 ** structures. Since we are careful :) to invalidate the
9036 ** DSA when we complete a command or when the SCRIPTS
9037 ** pushes a DSA into a queue, we can trust it when it
9038 ** points to a CCB.
9039 **
9040 **----------------------------------------------------------
9041 */
9042 static void ncr_recover_scsi_int (ncb_p np, u_char hsts)
9043 {
9044 u_int32 dsp = INL (nc_dsp);
9045 u_int32 dsa = INL (nc_dsa);
9046 ccb_p cp = ncr_ccb_from_dsa(np, dsa);
9047
9048 /*
9049 ** If we haven't been interrupted inside the SCRIPTS
9050 ** critical pathes, we can safely restart the SCRIPTS
9051 ** and trust the DSA value if it matches a CCB.
9052 */
9053 if ((!(dsp > NCB_SCRIPT_PHYS (np, getjob_begin) &&
9054 dsp < NCB_SCRIPT_PHYS (np, getjob_end) + 1)) &&
9055 (!(dsp > NCB_SCRIPT_PHYS (np, ungetjob) &&
9056 dsp < NCB_SCRIPT_PHYS (np, reselect) + 1)) &&
9057 (!(dsp > NCB_SCRIPTH_PHYS (np, sel_for_abort) &&
9058 dsp < NCB_SCRIPTH_PHYS (np, sel_for_abort_1) + 1)) &&
9059 (!(dsp > NCB_SCRIPT_PHYS (np, done) &&
9060 dsp < NCB_SCRIPT_PHYS (np, done_end) + 1))) {
9061 if (cp) {
9062 cp->host_status = hsts;
9063 ncr_complete (np, cp);
9064 }
9065 OUTL (nc_dsa, DSA_INVALID);
9066 OUTB (nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
9067 OUTB (nc_stest3, TE|CSF); /* clear scsi fifo */
9068 OUTL_DSP (NCB_SCRIPT_PHYS (np, start));
9069 }
9070 else
9071 goto reset_all;
9072
9073 return;
9074
9075 reset_all:
9076 ncr_start_reset(np);
9077 }
9078
9079 /*==========================================================
9080 **
9081 ** ncr chip exception handler for selection timeout
9082 **
9083 **==========================================================
9084 **
9085 ** There seems to be a bug in the 53c810.
9086 ** Although a STO-Interrupt is pending,
9087 ** it continues executing script commands.
9088 ** But it will fail and interrupt (IID) on
9089 ** the next instruction where it's looking
9090 ** for a valid phase.
9091 **
9092 **----------------------------------------------------------
9093 */
9094
9095 void ncr_int_sto (ncb_p np)
9096 {
9097 u_int32 dsp = INL (nc_dsp);
9098
9099 if (DEBUG_FLAGS & DEBUG_TINY) printk ("T");
9100
9101 if (dsp == NCB_SCRIPT_PHYS (np, wf_sel_done) + 8 ||
9102 !(driver_setup.recovery & 1))
9103 ncr_recover_scsi_int(np, HS_SEL_TIMEOUT);
9104 else
9105 ncr_start_reset(np);
9106 }
9107
9108 /*==========================================================
9109 **
9110 ** ncr chip exception handler for unexpected disconnect
9111 **
9112 **==========================================================
9113 **
9114 **----------------------------------------------------------
9115 */
9116 void ncr_int_udc (ncb_p np)
9117 {
9118 u_int32 dsa = INL (nc_dsa);
9119 ccb_p cp = ncr_ccb_from_dsa(np, dsa);
9120
9121 /*
9122 * Fix Up. Some disks respond to a PPR negotiation with
9123 * a bus free instead of a message reject.
9124 * Disable ppr negotiation if this is first time
9125 * tried ppr negotiation.
9126 */
9127 if (cp) {
9128 tcb_p tp = &np->target[cp->target];
9129 if (tp->ppr_negotiation == 1)
9130 tp->ppr_negotiation = 0;
9131 }
9132
9133 printk ("%s: unexpected disconnect\n", ncr_name(np));
9134 ncr_recover_scsi_int(np, HS_UNEXPECTED);
9135 }
9136
9137 /*==========================================================
9138 **
9139 ** ncr chip exception handler for SCSI bus mode change
9140 **
9141 **==========================================================
9142 **
9143 ** spi2-r12 11.2.3 says a transceiver mode change must
9144 ** generate a reset event and a device that detects a reset
9145 ** event shall initiate a hard reset. It says also that a
9146 ** device that detects a mode change shall set data transfer
9147 ** mode to eight bit asynchronous, etc...
9148 ** So, just resetting should be enough.
9149 **
9150 **
9151 **----------------------------------------------------------
9152 */
9153
9154 static void ncr_int_sbmc (ncb_p np)
9155 {
9156 u_char scsi_mode = INB (nc_stest4) & SMODE;
9157
9158 printk("%s: SCSI bus mode change from %x to %x.\n",
9159 ncr_name(np), np->scsi_mode, scsi_mode);
9160
9161 np->scsi_mode = scsi_mode;
9162
9163
9164 /*
9165 ** Suspend command processing for 1 second and
9166 ** reinitialize all except the chip.
9167 */
9168 np->settle_time = ktime_get(1*HZ);
9169 ncr_init (np, 0, bootverbose ? "scsi mode change" : NULL, HS_RESET);
9170 }
9171
9172 /*==========================================================
9173 **
9174 ** ncr chip exception handler for SCSI parity error.
9175 **
9176 **==========================================================
9177 **
9178 ** When the chip detects a SCSI parity error and is
9179 ** currently executing a (CH)MOV instruction, it does
9180 ** not interrupt immediately, but tries to finish the
9181 ** transfer of the current scatter entry before
9182 ** interrupting. The following situations may occur:
9183 **
9184 ** - The complete scatter entry has been transferred
9185 ** without the device having changed phase.
9186 ** The chip will then interrupt with the DSP pointing
9187 ** to the instruction that follows the MOV.
9188 **
9189 ** - A phase mismatch occurs before the MOV finished
9190 ** and phase errors are to be handled by the C code.
9191 ** The chip will then interrupt with both PAR and MA
9192 ** conditions set.
9193 **
9194 ** - A phase mismatch occurs before the MOV finished and
9195 ** phase errors are to be handled by SCRIPTS (895A or 896).
9196 ** The chip will load the DSP with the phase mismatch
9197 ** JUMP address and interrupt the host processor.
9198 **
9199 **----------------------------------------------------------
9200 */
9201
9202 static void ncr_int_par (ncb_p np, u_short sist)
9203 {
9204 u_char hsts = INB (HS_PRT);
9205 u_int32 dsp = INL (nc_dsp);
9206 u_int32 dbc = INL (nc_dbc);
9207 u_int32 dsa = INL (nc_dsa);
9208 u_char sbcl = INB (nc_sbcl);
9209 u_char cmd = dbc >> 24;
9210 int phase = cmd & 7;
9211 ccb_p cp = ncr_ccb_from_dsa(np, dsa);
9212
9213 printk("%s: SCSI parity error detected: SCR1=%d DBC=%x SBCL=%x\n",
9214 ncr_name(np), hsts, dbc, sbcl);
9215
9216 /*
9217 ** Check that the chip is connected to the SCSI BUS.
9218 */
9219 if (!(INB (nc_scntl1) & ISCON)) {
9220 if (!(driver_setup.recovery & 1)) {
9221 ncr_recover_scsi_int(np, HS_FAIL);
9222 return;
9223 }
9224 goto reset_all;
9225 }
9226
9227 /*
9228 ** If the nexus is not clearly identified, reset the bus.
9229 ** We will try to do better later.
9230 */
9231 if (!cp)
9232 goto reset_all;
9233
9234 /*
9235 ** Check instruction was a MOV, direction was INPUT and
9236 ** ATN is asserted.
9237 */
9238 if ((cmd & 0xc0) || !(phase & 1) || !(sbcl & 0x8))
9239 goto reset_all;
9240
9241 /*
9242 ** Keep track of the parity error.
9243 */
9244 OUTONB (HF_PRT, HF_EXT_ERR);
9245 cp->xerr_status |= XE_PARITY_ERR;
9246
9247 /*
9248 ** Prepare the message to send to the device.
9249 */
9250 np->msgout[0] = (phase == 7) ? M_PARITY : M_ID_ERROR;
9251
9252 #ifdef SCSI_NCR_INTEGRITY_CHECKING
9253 /*
9254 ** Save error message. For integrity check use only.
9255 */
9256 if (np->check_integrity)
9257 np->check_integ_par = np->msgout[0];
9258 #endif
9259
9260 /*
9261 ** If the old phase was DATA IN or DT DATA IN phase,
9262 ** we have to deal with the 3 situations described above.
9263 ** For other input phases (MSG IN and STATUS), the device
9264 ** must resend the whole thing that failed parity checking
9265 ** or signal error. So, jumping to dispatcher should be OK.
9266 */
9267 if ((phase == 1) || (phase == 5)) {
9268 /* Phase mismatch handled by SCRIPTS */
9269 if (dsp == NCB_SCRIPTH_PHYS (np, pm_handle))
9270 OUTL_DSP (dsp);
9271 /* Phase mismatch handled by the C code */
9272 else if (sist & MA)
9273 ncr_int_ma (np);
9274 /* No phase mismatch occurred */
9275 else {
9276 OUTL (nc_temp, dsp);
9277 OUTL_DSP (NCB_SCRIPT_PHYS (np, dispatch));
9278 }
9279 }
9280 else
9281 OUTL_DSP (NCB_SCRIPT_PHYS (np, clrack));
9282 return;
9283
9284 reset_all:
9285 ncr_start_reset(np);
9286 return;
9287 }
9288
9289 /*==========================================================
9290 **
9291 **
9292 ** ncr chip exception handler for phase errors.
9293 **
9294 **
9295 **==========================================================
9296 **
9297 ** We have to construct a new transfer descriptor,
9298 ** to transfer the rest of the current block.
9299 **
9300 **----------------------------------------------------------
9301 */
9302
9303 static void ncr_int_ma (ncb_p np)
9304 {
9305 u_int32 dbc;
9306 u_int32 rest;
9307 u_int32 dsp;
9308 u_int32 dsa;
9309 u_int32 nxtdsp;
9310 u_int32 *vdsp;
9311 u_int32 oadr, olen;
9312 u_int32 *tblp;
9313 u_int32 newcmd;
9314 u_int delta;
9315 u_char cmd;
9316 u_char hflags, hflags0;
9317 struct pm_ctx *pm;
9318 ccb_p cp;
9319
9320 dsp = INL (nc_dsp);
9321 dbc = INL (nc_dbc);
9322 dsa = INL (nc_dsa);
9323
9324 cmd = dbc >> 24;
9325 rest = dbc & 0xffffff;
9326 delta = 0;
9327
9328 /*
9329 ** locate matching cp.
9330 */
9331 cp = ncr_ccb_from_dsa(np, dsa);
9332
9333 if (DEBUG_FLAGS & DEBUG_PHASE)
9334 printk("CCB = %2x %2x %2x %2x %2x %2x\n",
9335 cp->cmd->cmnd[0], cp->cmd->cmnd[1], cp->cmd->cmnd[2],
9336 cp->cmd->cmnd[3], cp->cmd->cmnd[4], cp->cmd->cmnd[5]);
9337
9338 /*
9339 ** Donnot take into account dma fifo and various buffers in
9340 ** INPUT phase since the chip flushes everything before
9341 ** raising the MA interrupt for interrupted INPUT phases.
9342 ** For DATA IN phase, we will check for the SWIDE later.
9343 */
9344 if ((cmd & 7) != 1 && (cmd & 7) != 5) {
9345 u_int32 dfifo;
9346 u_char ss0, ss2;
9347
9348 /*
9349 ** If C1010, DFBC contains number of bytes in DMA fifo.
9350 ** else read DFIFO, CTEST[4-6] using 1 PCI bus ownership.
9351 */
9352 if ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
9353 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66))
9354 delta = INL(nc_dfbc) & 0xffff;
9355 else {
9356 dfifo = INL(nc_dfifo);
9357
9358 /*
9359 ** Calculate remaining bytes in DMA fifo.
9360 ** C1010 - always large fifo, value in dfbc
9361 ** Otherwise, (CTEST5 = dfifo >> 16)
9362 */
9363 if (dfifo & (DFS << 16))
9364 delta = ((((dfifo >> 8) & 0x300) |
9365 (dfifo & 0xff)) - rest) & 0x3ff;
9366 else
9367 delta = ((dfifo & 0xff) - rest) & 0x7f;
9368
9369 /*
9370 ** The data in the dma fifo has not been
9371 ** transferred to the target -> add the amount
9372 ** to the rest and clear the data.
9373 ** Check the sstat2 register in case of wide
9374 ** transfer.
9375 */
9376
9377 }
9378
9379 rest += delta;
9380 ss0 = INB (nc_sstat0);
9381 if (ss0 & OLF) rest++;
9382 if ((np->device_id != PCI_DEVICE_ID_LSI_53C1010) &&
9383 (np->device_id != PCI_DEVICE_ID_LSI_53C1010_66) && (ss0 & ORF))
9384 rest++;
9385 if (cp && (cp->phys.select.sel_scntl3 & EWS)) {
9386 ss2 = INB (nc_sstat2);
9387 if (ss2 & OLF1) rest++;
9388 if ((np->device_id != PCI_DEVICE_ID_LSI_53C1010) &&
9389 (np->device_id != PCI_DEVICE_ID_LSI_53C1010_66) && (ss2 & ORF))
9390 rest++;
9391 };
9392
9393 /*
9394 ** Clear fifos.
9395 */
9396 OUTB (nc_ctest3, np->rv_ctest3 | CLF); /* dma fifo */
9397 OUTB (nc_stest3, TE|CSF); /* scsi fifo */
9398 }
9399
9400 /*
9401 ** log the information
9402 */
9403
9404 if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
9405 printk ("P%x%x RL=%d D=%d ", cmd&7, INB(nc_sbcl)&7,
9406 (unsigned) rest, (unsigned) delta);
9407
9408 /*
9409 ** try to find the interrupted script command,
9410 ** and the address at which to continue.
9411 */
9412 vdsp = 0;
9413 nxtdsp = 0;
9414 if (dsp > np->p_script &&
9415 dsp <= np->p_script + sizeof(struct script)) {
9416 vdsp = (u_int32 *)((char*)np->script0 + (dsp-np->p_script-8));
9417 nxtdsp = dsp;
9418 }
9419 else if (dsp > np->p_scripth &&
9420 dsp <= np->p_scripth + sizeof(struct scripth)) {
9421 vdsp = (u_int32 *)((char*)np->scripth0 + (dsp-np->p_scripth-8));
9422 nxtdsp = dsp;
9423 }
9424
9425 /*
9426 ** log the information
9427 */
9428 if (DEBUG_FLAGS & DEBUG_PHASE) {
9429 printk ("\nCP=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
9430 cp, (unsigned)dsp, (unsigned)nxtdsp, vdsp, cmd);
9431 };
9432
9433 if (!vdsp) {
9434 printk ("%s: interrupted SCRIPT address not found.\n",
9435 ncr_name (np));
9436 goto reset_all;
9437 }
9438
9439 if (!cp) {
9440 printk ("%s: SCSI phase error fixup: CCB already dequeued.\n",
9441 ncr_name (np));
9442 goto reset_all;
9443 }
9444
9445 /*
9446 ** get old startaddress and old length.
9447 */
9448
9449 oadr = scr_to_cpu(vdsp[1]);
9450
9451 if (cmd & 0x10) { /* Table indirect */
9452 tblp = (u_int32 *) ((char*) &cp->phys + oadr);
9453 olen = scr_to_cpu(tblp[0]);
9454 oadr = scr_to_cpu(tblp[1]);
9455 } else {
9456 tblp = (u_int32 *) 0;
9457 olen = scr_to_cpu(vdsp[0]) & 0xffffff;
9458 };
9459
9460 if (DEBUG_FLAGS & DEBUG_PHASE) {
9461 printk ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
9462 (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
9463 tblp,
9464 (unsigned) olen,
9465 (unsigned) oadr);
9466 };
9467
9468 /*
9469 ** check cmd against assumed interrupted script command.
9470 ** If dt data phase, the MOVE instruction hasn't bit 4 of
9471 ** the phase.
9472 */
9473
9474 if (((cmd & 2) ? cmd : (cmd & ~4)) != (scr_to_cpu(vdsp[0]) >> 24)) {
9475 PRINT_ADDR(cp->cmd);
9476 printk ("internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
9477 (unsigned)cmd, (unsigned)scr_to_cpu(vdsp[0]) >> 24);
9478
9479 goto reset_all;
9480 };
9481
9482 /*
9483 ** if old phase not dataphase, leave here.
9484 ** C/D line is low if data.
9485 */
9486
9487 if (cmd & 0x02) {
9488 PRINT_ADDR(cp->cmd);
9489 printk ("phase change %x-%x %d@%08x resid=%d.\n",
9490 cmd&7, INB(nc_sbcl)&7, (unsigned)olen,
9491 (unsigned)oadr, (unsigned)rest);
9492 goto unexpected_phase;
9493 };
9494
9495 /*
9496 ** Choose the correct PM save area.
9497 **
9498 ** Look at the PM_SAVE SCRIPT if you want to understand
9499 ** this stuff. The equivalent code is implemented in
9500 ** SCRIPTS for the 895A and 896 that are able to handle
9501 ** PM from the SCRIPTS processor.
9502 */
9503
9504 hflags0 = INB (HF_PRT);
9505 hflags = hflags0;
9506
9507 if (hflags & (HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED)) {
9508 if (hflags & HF_IN_PM0)
9509 nxtdsp = scr_to_cpu(cp->phys.pm0.ret);
9510 else if (hflags & HF_IN_PM1)
9511 nxtdsp = scr_to_cpu(cp->phys.pm1.ret);
9512
9513 if (hflags & HF_DP_SAVED)
9514 hflags ^= HF_ACT_PM;
9515 }
9516
9517 if (!(hflags & HF_ACT_PM)) {
9518 pm = &cp->phys.pm0;
9519 newcmd = NCB_SCRIPT_PHYS(np, pm0_data);
9520 }
9521 else {
9522 pm = &cp->phys.pm1;
9523 newcmd = NCB_SCRIPT_PHYS(np, pm1_data);
9524 }
9525
9526 hflags &= ~(HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED);
9527 if (hflags != hflags0)
9528 OUTB (HF_PRT, hflags);
9529
9530 /*
9531 ** fillin the phase mismatch context
9532 */
9533
9534 pm->sg.addr = cpu_to_scr(oadr + olen - rest);
9535 pm->sg.size = cpu_to_scr(rest);
9536 pm->ret = cpu_to_scr(nxtdsp);
9537
9538 /*
9539 ** If we have a SWIDE,
9540 ** - prepare the address to write the SWIDE from SCRIPTS,
9541 ** - compute the SCRIPTS address to restart from,
9542 ** - move current data pointer context by one byte.
9543 */
9544 nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
9545 if ( ((cmd & 7) == 1 || (cmd & 7) == 5)
9546 && cp && (cp->phys.select.sel_scntl3 & EWS) &&
9547 (INB (nc_scntl2) & WSR)) {
9548 u32 tmp;
9549
9550 #ifdef SYM_DEBUG_PM_WITH_WSR
9551 PRINT_ADDR(cp);
9552 printk ("MA interrupt with WSR set - "
9553 "pm->sg.addr=%x - pm->sg.size=%d\n",
9554 pm->sg.addr, pm->sg.size);
9555 #endif
9556 /*
9557 * Set up the table indirect for the MOVE
9558 * of the residual byte and adjust the data
9559 * pointer context.
9560 */
9561 tmp = scr_to_cpu(pm->sg.addr);
9562 cp->phys.wresid.addr = cpu_to_scr(tmp);
9563 pm->sg.addr = cpu_to_scr(tmp + 1);
9564 tmp = scr_to_cpu(pm->sg.size);
9565 cp->phys.wresid.size = cpu_to_scr((tmp&0xff000000) | 1);
9566 pm->sg.size = cpu_to_scr(tmp - 1);
9567
9568 /*
9569 * If only the residual byte is to be moved,
9570 * no PM context is needed.
9571 */
9572 if ((tmp&0xffffff) == 1)
9573 newcmd = pm->ret;
9574
9575 /*
9576 * Prepare the address of SCRIPTS that will
9577 * move the residual byte to memory.
9578 */
9579 nxtdsp = NCB_SCRIPTH_PHYS (np, wsr_ma_helper);
9580 }
9581
9582 if (DEBUG_FLAGS & DEBUG_PHASE) {
9583 PRINT_ADDR(cp->cmd);
9584 printk ("PM %x %x %x / %x %x %x.\n",
9585 hflags0, hflags, newcmd,
9586 (unsigned)scr_to_cpu(pm->sg.addr),
9587 (unsigned)scr_to_cpu(pm->sg.size),
9588 (unsigned)scr_to_cpu(pm->ret));
9589 }
9590
9591 /*
9592 ** Restart the SCRIPTS processor.
9593 */
9594
9595 OUTL (nc_temp, newcmd);
9596 OUTL_DSP (nxtdsp);
9597 return;
9598
9599 /*
9600 ** Unexpected phase changes that occurs when the current phase
9601 ** is not a DATA IN or DATA OUT phase are due to error conditions.
9602 ** Such event may only happen when the SCRIPTS is using a
9603 ** multibyte SCSI MOVE.
9604 **
9605 ** Phase change Some possible cause
9606 **
9607 ** COMMAND --> MSG IN SCSI parity error detected by target.
9608 ** COMMAND --> STATUS Bad command or refused by target.
9609 ** MSG OUT --> MSG IN Message rejected by target.
9610 ** MSG OUT --> COMMAND Bogus target that discards extended
9611 ** negotiation messages.
9612 **
9613 ** The code below does not care of the new phase and so
9614 ** trusts the target. Why to annoy it ?
9615 ** If the interrupted phase is COMMAND phase, we restart at
9616 ** dispatcher.
9617 ** If a target does not get all the messages after selection,
9618 ** the code assumes blindly that the target discards extended
9619 ** messages and clears the negotiation status.
9620 ** If the target does not want all our response to negotiation,
9621 ** we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
9622 ** bloat for such a should_not_happen situation).
9623 ** In all other situation, we reset the BUS.
9624 ** Are these assumptions reasonnable ? (Wait and see ...)
9625 */
9626 unexpected_phase:
9627 dsp -= 8;
9628 nxtdsp = 0;
9629
9630 switch (cmd & 7) {
9631 case 2: /* COMMAND phase */
9632 nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
9633 break;
9634 #if 0
9635 case 3: /* STATUS phase */
9636 nxtdsp = NCB_SCRIPT_PHYS (np, dispatch);
9637 break;
9638 #endif
9639 case 6: /* MSG OUT phase */
9640 /*
9641 ** If the device may want to use untagged when we want
9642 ** tagged, we prepare an IDENTIFY without disc. granted,
9643 ** since we will not be able to handle reselect.
9644 ** Otherwise, we just don't care.
9645 */
9646 if (dsp == NCB_SCRIPT_PHYS (np, send_ident)) {
9647 if (cp->tag != NO_TAG && olen - rest <= 3) {
9648 cp->host_status = HS_BUSY;
9649 np->msgout[0] = M_IDENTIFY | cp->lun;
9650 nxtdsp = NCB_SCRIPTH_PHYS (np, ident_break_atn);
9651 }
9652 else
9653 nxtdsp = NCB_SCRIPTH_PHYS (np, ident_break);
9654 }
9655 else if (dsp == NCB_SCRIPTH_PHYS (np, send_wdtr) ||
9656 dsp == NCB_SCRIPTH_PHYS (np, send_sdtr) ||
9657 dsp == NCB_SCRIPTH_PHYS (np, send_ppr)) {
9658 nxtdsp = NCB_SCRIPTH_PHYS (np, nego_bad_phase);
9659 }
9660 break;
9661 #if 0
9662 case 7: /* MSG IN phase */
9663 nxtdsp = NCB_SCRIPT_PHYS (np, clrack);
9664 break;
9665 #endif
9666 }
9667
9668 if (nxtdsp) {
9669 OUTL_DSP (nxtdsp);
9670 return;
9671 }
9672
9673 reset_all:
9674 ncr_start_reset(np);
9675 }
9676
9677 /*==========================================================
9678 **
9679 ** ncr chip handler for QUEUE FULL and CHECK CONDITION
9680 **
9681 **==========================================================
9682 **
9683 ** On QUEUE FULL status, we set the actual tagged command
9684 ** queue depth to the number of disconnected CCBs that is
9685 ** hopefully a good value to avoid further QUEUE FULL.
9686 **
9687 ** On CHECK CONDITION or COMMAND TERMINATED, we use the
9688 ** CCB of the failed command for performing a REQUEST
9689 ** SENSE SCSI command.
9690 **
9691 ** We do not want to change the order commands will be
9692 ** actually queued to the device after we received a
9693 ** QUEUE FULL status. We also want to properly deal with
9694 ** contingent allegiance condition. For these reasons,
9695 ** we remove from the start queue all commands for this
9696 ** LUN that haven't been yet queued to the device and
9697 ** put them back in the correponding LUN queue, then
9698 ** requeue the CCB that failed in front of the LUN queue.
9699 ** I just hope this not to be performed too often. :)
9700 **
9701 ** If we are using IMMEDIATE ARBITRATION, we clear the
9702 ** IARB hint for every commands we encounter in order not
9703 ** to be stuck with a won arbitration and no job to queue
9704 ** to a device.
9705 **----------------------------------------------------------
9706 */
9707
9708 static void ncr_sir_to_redo(ncb_p np, int num, ccb_p cp)
9709 {
9710 Scsi_Cmnd *cmd = cp->cmd;
9711 tcb_p tp = &np->target[cp->target];
9712 lcb_p lp = ncr_lp(np, tp, cp->lun);
9713 ccb_p cp2;
9714 int busyccbs = 1;
9715 u_int32 startp;
9716 u_char s_status = INB (SS_PRT);
9717 int msglen;
9718 int i, j;
9719
9720
9721 /*
9722 ** If the LCB is not yet available, then only
9723 ** 1 IO is accepted, so we should have it.
9724 */
9725 if (!lp)
9726 goto next;
9727 /*
9728 ** Remove all CCBs queued to the chip for that LUN and put
9729 ** them back in the LUN CCB wait queue.
9730 */
9731 busyccbs = lp->queuedccbs;
9732 i = (INL (nc_scratcha) - np->p_squeue) / 4;
9733 j = i;
9734 while (i != np->squeueput) {
9735 cp2 = ncr_ccb_from_dsa(np, scr_to_cpu(np->squeue[i]));
9736 assert(cp2);
9737 #ifdef SCSI_NCR_IARB_SUPPORT
9738 /* IARB hints may not be relevant any more. Forget them. */
9739 cp2->host_flags &= ~HF_HINT_IARB;
9740 #endif
9741 if (cp2 && cp2->target == cp->target && cp2->lun == cp->lun) {
9742 xpt_remque(&cp2->link_ccbq);
9743 xpt_insque_head(&cp2->link_ccbq, &lp->wait_ccbq);
9744 --lp->queuedccbs;
9745 cp2->queued = 0;
9746 }
9747 else {
9748 if (i != j)
9749 np->squeue[j] = np->squeue[i];
9750 if ((j += 2) >= MAX_START*2) j = 0;
9751 }
9752 if ((i += 2) >= MAX_START*2) i = 0;
9753 }
9754 if (i != j) /* Copy back the idle task if needed */
9755 np->squeue[j] = np->squeue[i];
9756 np->squeueput = j; /* Update our current start queue pointer */
9757
9758 /*
9759 ** Requeue the interrupted CCB in front of the
9760 ** LUN CCB wait queue to preserve ordering.
9761 */
9762 xpt_remque(&cp->link_ccbq);
9763 xpt_insque_head(&cp->link_ccbq, &lp->wait_ccbq);
9764 --lp->queuedccbs;
9765 cp->queued = 0;
9766
9767 next:
9768
9769 #ifdef SCSI_NCR_IARB_SUPPORT
9770 /* IARB hint may not be relevant any more. Forget it. */
9771 cp->host_flags &= ~HF_HINT_IARB;
9772 if (np->last_cp)
9773 np->last_cp = 0;
9774 #endif
9775
9776 /*
9777 ** Now we can restart the SCRIPTS processor safely.
9778 */
9779 OUTL_DSP (NCB_SCRIPT_PHYS (np, start));
9780
9781 switch(s_status) {
9782 default:
9783 case S_BUSY:
9784 ncr_complete(np, cp);
9785 break;
9786 case S_QUEUE_FULL:
9787 if (!lp || !lp->queuedccbs) {
9788 ncr_complete(np, cp);
9789 break;
9790 }
9791 if (bootverbose >= 1) {
9792 PRINT_ADDR(cmd);
9793 printk ("QUEUE FULL! %d busy, %d disconnected CCBs\n",
9794 busyccbs, lp->queuedccbs);
9795 }
9796 /*
9797 ** Decrease number of tags to the number of
9798 ** disconnected commands.
9799 */
9800 if (lp->queuedccbs < lp->numtags) {
9801 lp->numtags = lp->queuedccbs;
9802 lp->num_good = 0;
9803 ncr_setup_tags (np, cp->target, cp->lun);
9804 }
9805 /*
9806 ** Repair the offending CCB.
9807 */
9808 cp->phys.header.savep = cp->startp;
9809 cp->phys.header.lastp = cp->lastp0;
9810 cp->host_status = HS_BUSY;
9811 cp->scsi_status = S_ILLEGAL;
9812 cp->xerr_status = 0;
9813 cp->extra_bytes = 0;
9814 cp->host_flags &= (HF_PM_TO_C|HF_DATA_IN);
9815
9816 break;
9817
9818 case S_TERMINATED:
9819 case S_CHECK_COND:
9820 /*
9821 ** If we were requesting sense, give up.
9822 */
9823 if (cp->host_flags & HF_AUTO_SENSE) {
9824 ncr_complete(np, cp);
9825 break;
9826 }
9827
9828 /*
9829 ** Save SCSI status and extended error.
9830 ** Compute the data residual now.
9831 */
9832 cp->sv_scsi_status = cp->scsi_status;
9833 cp->sv_xerr_status = cp->xerr_status;
9834 cp->resid = ncr_compute_residual(np, cp);
9835
9836 /*
9837 ** Device returned CHECK CONDITION status.
9838 ** Prepare all needed data strutures for getting
9839 ** sense data.
9840 */
9841
9842 /*
9843 ** identify message
9844 */
9845 cp->scsi_smsg2[0] = M_IDENTIFY | cp->lun;
9846 msglen = 1;
9847
9848 /*
9849 ** If we are currently using anything different from
9850 ** async. 8 bit data transfers with that target,
9851 ** start a negotiation, since the device may want
9852 ** to report us a UNIT ATTENTION condition due to
9853 ** a cause we currently ignore, and we donnot want
9854 ** to be stuck with WIDE and/or SYNC data transfer.
9855 **
9856 ** cp->nego_status is filled by ncr_prepare_nego().
9857 **
9858 ** Do NOT negotiate if performing integrity check
9859 ** or if integrity check has completed, all check
9860 ** conditions will have been cleared.
9861 */
9862
9863 #ifdef SCSI_NCR_INTEGRITY_CHECKING
9864 if (DEBUG_FLAGS & DEBUG_IC) {
9865 printk("%s: ncr_sir_to_redo: ic_done %2X, in_progress %2X\n",
9866 ncr_name(np), tp->ic_done, cp->cmd->ic_in_progress);
9867 }
9868
9869 /*
9870 ** If parity error during integrity check,
9871 ** set the target width to narrow. Otherwise,
9872 ** do not negotiate on a request sense.
9873 */
9874 if ( np->check_integ_par && np->check_integrity
9875 && cp->cmd->ic_in_progress ) {
9876 cp->nego_status = 0;
9877 msglen +=
9878 ncr_ic_nego (np, cp, cmd ,&cp->scsi_smsg2[msglen]);
9879 }
9880
9881 if (!np->check_integrity ||
9882 (np->check_integrity &&
9883 (!cp->cmd->ic_in_progress && !tp->ic_done)) ) {
9884 ncr_negotiate(np, tp);
9885 cp->nego_status = 0;
9886 {
9887 u_char sync_offset;
9888 if ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
9889 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66))
9890 sync_offset = tp->sval & 0x3f;
9891 else
9892 sync_offset = tp->sval & 0x1f;
9893
9894 if ((tp->wval & EWS) || sync_offset)
9895 msglen +=
9896 ncr_prepare_nego (np, cp, &cp->scsi_smsg2[msglen]);
9897 }
9898
9899 }
9900 #else
9901 ncr_negotiate(np, tp);
9902 cp->nego_status = 0;
9903 if ((tp->wval & EWS) || (tp->sval & 0x1f))
9904 msglen +=
9905 ncr_prepare_nego (np, cp, &cp->scsi_smsg2[msglen]);
9906 #endif /* SCSI_NCR_INTEGRITY_CHECKING */
9907
9908 /*
9909 ** Message table indirect structure.
9910 */
9911 cp->phys.smsg.addr = cpu_to_scr(CCB_PHYS (cp, scsi_smsg2));
9912 cp->phys.smsg.size = cpu_to_scr(msglen);
9913
9914 /*
9915 ** sense command
9916 */
9917 cp->phys.cmd.addr = cpu_to_scr(CCB_PHYS (cp, sensecmd));
9918 cp->phys.cmd.size = cpu_to_scr(6);
9919
9920 /*
9921 ** patch requested size into sense command
9922 */
9923 cp->sensecmd[0] = 0x03;
9924 cp->sensecmd[1] = cp->lun << 5;
9925 cp->sensecmd[4] = sizeof(cp->sense_buf);
9926
9927 /*
9928 ** sense data
9929 */
9930 bzero(cp->sense_buf, sizeof(cp->sense_buf));
9931 cp->phys.sense.addr = cpu_to_scr(CCB_PHYS(cp,sense_buf[0]));
9932 cp->phys.sense.size = cpu_to_scr(sizeof(cp->sense_buf));
9933
9934 /*
9935 ** requeue the command.
9936 */
9937 startp = NCB_SCRIPTH_PHYS (np, sdata_in);
9938
9939 cp->phys.header.savep = cpu_to_scr(startp);
9940 cp->phys.header.goalp = cpu_to_scr(startp + 16);
9941 cp->phys.header.lastp = cpu_to_scr(startp);
9942 cp->phys.header.wgoalp = cpu_to_scr(startp + 16);
9943 cp->phys.header.wlastp = cpu_to_scr(startp);
9944
9945 cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
9946 cp->scsi_status = S_ILLEGAL;
9947 cp->host_flags = (HF_AUTO_SENSE|HF_DATA_IN);
9948
9949 cp->phys.header.go.start =
9950 cpu_to_scr(NCB_SCRIPT_PHYS (np, select));
9951
9952 /*
9953 ** If lp not yet allocated, requeue the command.
9954 */
9955 if (!lp)
9956 ncr_put_start_queue(np, cp);
9957 break;
9958 }
9959
9960 /*
9961 ** requeue awaiting scsi commands for this lun.
9962 */
9963 if (lp)
9964 ncr_start_next_ccb(np, lp, 1);
9965
9966 return;
9967 }
9968
9969 /*----------------------------------------------------------
9970 **
9971 ** After a device has accepted some management message
9972 ** as BUS DEVICE RESET, ABORT TASK, etc ..., or when
9973 ** a device signals a UNIT ATTENTION condition, some
9974 ** tasks are thrown away by the device. We are required
9975 ** to reflect that on our tasks list since the device
9976 ** will never complete these tasks.
9977 **
9978 ** This function completes all disconnected CCBs for a
9979 ** given target that matches the following criteria:
9980 ** - lun=-1 means any logical UNIT otherwise a given one.
9981 ** - task=-1 means any task, otherwise a given one.
9982 **----------------------------------------------------------
9983 */
9984 static int ncr_clear_tasks(ncb_p np, u_char hsts,
9985 int target, int lun, int task)
9986 {
9987 int i = 0;
9988 ccb_p cp;
9989
9990 for (cp = np->ccbc; cp; cp = cp->link_ccb) {
9991 if (cp->host_status != HS_DISCONNECT)
9992 continue;
9993 if (cp->target != target)
9994 continue;
9995 if (lun != -1 && cp->lun != lun)
9996 continue;
9997 if (task != -1 && cp->tag != NO_TAG && cp->scsi_smsg[2] != task)
9998 continue;
9999 cp->host_status = hsts;
10000 cp->scsi_status = S_ILLEGAL;
10001 ncr_complete(np, cp);
10002 ++i;
10003 }
10004 return i;
10005 }
10006
10007 /*==========================================================
10008 **
10009 ** ncr chip handler for TASKS recovery.
10010 **
10011 **==========================================================
10012 **
10013 ** We cannot safely abort a command, while the SCRIPTS
10014 ** processor is running, since we just would be in race
10015 ** with it.
10016 **
10017 ** As long as we have tasks to abort, we keep the SEM
10018 ** bit set in the ISTAT. When this bit is set, the
10019 ** SCRIPTS processor interrupts (SIR_SCRIPT_STOPPED)
10020 ** each time it enters the scheduler.
10021 **
10022 ** If we have to reset a target, clear tasks of a unit,
10023 ** or to perform the abort of a disconnected job, we
10024 ** restart the SCRIPTS for selecting the target. Once
10025 ** selected, the SCRIPTS interrupts (SIR_TARGET_SELECTED).
10026 ** If it loses arbitration, the SCRIPTS will interrupt again
10027 ** the next time it will enter its scheduler, and so on ...
10028 **
10029 ** On SIR_TARGET_SELECTED, we scan for the more
10030 ** appropriate thing to do:
10031 **
10032 ** - If nothing, we just sent a M_ABORT message to the
10033 ** target to get rid of the useless SCSI bus ownership.
10034 ** According to the specs, no tasks shall be affected.
10035 ** - If the target is to be reset, we send it a M_RESET
10036 ** message.
10037 ** - If a logical UNIT is to be cleared , we send the
10038 ** IDENTIFY(lun) + M_ABORT.
10039 ** - If an untagged task is to be aborted, we send the
10040 ** IDENTIFY(lun) + M_ABORT.
10041 ** - If a tagged task is to be aborted, we send the
10042 ** IDENTIFY(lun) + task attributes + M_ABORT_TAG.
10043 **
10044 ** Once our 'kiss of death' :) message has been accepted
10045 ** by the target, the SCRIPTS interrupts again
10046 ** (SIR_ABORT_SENT). On this interrupt, we complete
10047 ** all the CCBs that should have been aborted by the
10048 ** target according to our message.
10049 **
10050 **----------------------------------------------------------
10051 */
10052 static void ncr_sir_task_recovery(ncb_p np, int num)
10053 {
10054 ccb_p cp;
10055 tcb_p tp;
10056 int target=-1, lun=-1, task;
10057 int i, k;
10058 u_char *p;
10059
10060 switch(num) {
10061 /*
10062 ** The SCRIPTS processor stopped before starting
10063 ** the next command in order to allow us to perform
10064 ** some task recovery.
10065 */
10066 case SIR_SCRIPT_STOPPED:
10067
10068 /*
10069 ** Do we have any target to reset or unit to clear ?
10070 */
10071 for (i = 0 ; i < MAX_TARGET ; i++) {
10072 tp = &np->target[i];
10073 if (tp->to_reset || (tp->l0p && tp->l0p->to_clear)) {
10074 target = i;
10075 break;
10076 }
10077 if (!tp->lmp)
10078 continue;
10079 for (k = 1 ; k < MAX_LUN ; k++) {
10080 if (tp->lmp[k] && tp->lmp[k]->to_clear) {
10081 target = i;
10082 break;
10083 }
10084 }
10085 if (target != -1)
10086 break;
10087 }
10088
10089 /*
10090 ** If not, look at the CCB list for any
10091 ** disconnected CCB to be aborted.
10092 */
10093 if (target == -1) {
10094 for (cp = np->ccbc; cp; cp = cp->link_ccb) {
10095 if (cp->host_status != HS_DISCONNECT)
10096 continue;
10097 if (cp->to_abort) {
10098 target = cp->target;
10099 break;
10100 }
10101 }
10102 }
10103
10104 /*
10105 ** If some target is to be selected,
10106 ** prepare and start the selection.
10107 */
10108 if (target != -1) {
10109 tp = &np->target[target];
10110 np->abrt_sel.sel_id = target;
10111 np->abrt_sel.sel_scntl3 = tp->wval;
10112 np->abrt_sel.sel_sxfer = tp->sval;
10113 np->abrt_sel.sel_scntl4 = tp->uval;
10114 OUTL(nc_dsa, np->p_ncb);
10115 OUTL_DSP (NCB_SCRIPTH_PHYS (np, sel_for_abort));
10116 return;
10117 }
10118
10119 /*
10120 ** Nothing is to be selected, so we donnot need
10121 ** to synchronize with the SCRIPTS anymore.
10122 ** Remove the SEM flag from the ISTAT.
10123 */
10124 np->istat_sem = 0;
10125 OUTB (nc_istat, SIGP);
10126
10127 /*
10128 ** Now look at CCBs to abort that haven't started yet.
10129 ** Remove all those CCBs from the start queue and
10130 ** complete them with appropriate status.
10131 ** Btw, the SCRIPTS processor is still stopped, so
10132 ** we are not in race.
10133 */
10134 for (cp = np->ccbc; cp; cp = cp->link_ccb) {
10135 if (cp->host_status != HS_BUSY &&
10136 cp->host_status != HS_NEGOTIATE)
10137 continue;
10138 if (!cp->to_abort)
10139 continue;
10140 #ifdef SCSI_NCR_IARB_SUPPORT
10141 /*
10142 ** If we are using IMMEDIATE ARBITRATION, we donnot
10143 ** want to cancel the last queued CCB, since the
10144 ** SCRIPTS may have anticipated the selection.
10145 */
10146 if (cp == np->last_cp) {
10147 cp->to_abort = 0;
10148 continue;
10149 }
10150 #endif
10151 /*
10152 ** Compute index of next position in the start
10153 ** queue the SCRIPTS will schedule.
10154 */
10155 i = (INL (nc_scratcha) - np->p_squeue) / 4;
10156
10157 /*
10158 ** Remove the job from the start queue.
10159 */
10160 k = -1;
10161 while (1) {
10162 if (i == np->squeueput)
10163 break;
10164 if (k == -1) { /* Not found yet */
10165 if (cp == ncr_ccb_from_dsa(np,
10166 scr_to_cpu(np->squeue[i])))
10167 k = i; /* Found */
10168 }
10169 else {
10170 /*
10171 ** Once found, we have to move
10172 ** back all jobs by 1 position.
10173 */
10174 np->squeue[k] = np->squeue[i];
10175 k += 2;
10176 if (k >= MAX_START*2)
10177 k = 0;
10178 }
10179
10180 i += 2;
10181 if (i >= MAX_START*2)
10182 i = 0;
10183 }
10184 /*
10185 ** If job removed, repair the start queue.
10186 */
10187 if (k != -1) {
10188 np->squeue[k] = np->squeue[i]; /* Idle task */
10189 np->squeueput = k; /* Start queue pointer */
10190 }
10191 cp->host_status = HS_ABORTED;
10192 cp->scsi_status = S_ILLEGAL;
10193 ncr_complete(np, cp);
10194 }
10195 break;
10196 /*
10197 ** The SCRIPTS processor has selected a target
10198 ** we may have some manual recovery to perform for.
10199 */
10200 case SIR_TARGET_SELECTED:
10201 target = (INB (nc_sdid) & 0xf);
10202 tp = &np->target[target];
10203
10204 np->abrt_tbl.addr = cpu_to_scr(vtobus(np->abrt_msg));
10205
10206 /*
10207 ** If the target is to be reset, prepare a
10208 ** M_RESET message and clear the to_reset flag
10209 ** since we donnot expect this operation to fail.
10210 */
10211 if (tp->to_reset) {
10212 np->abrt_msg[0] = M_RESET;
10213 np->abrt_tbl.size = 1;
10214 tp->to_reset = 0;
10215 break;
10216 }
10217
10218 /*
10219 ** Otherwise, look for some logical unit to be cleared.
10220 */
10221 if (tp->l0p && tp->l0p->to_clear)
10222 lun = 0;
10223 else if (tp->lmp) {
10224 for (k = 1 ; k < MAX_LUN ; k++) {
10225 if (tp->lmp[k] && tp->lmp[k]->to_clear) {
10226 lun = k;
10227 break;
10228 }
10229 }
10230 }
10231
10232 /*
10233 ** If a logical unit is to be cleared, prepare
10234 ** an IDENTIFY(lun) + ABORT MESSAGE.
10235 */
10236 if (lun != -1) {
10237 lcb_p lp = ncr_lp(np, tp, lun);
10238 lp->to_clear = 0; /* We donnot expect to fail here */
10239 np->abrt_msg[0] = M_IDENTIFY | lun;
10240 np->abrt_msg[1] = M_ABORT;
10241 np->abrt_tbl.size = 2;
10242 break;
10243 }
10244
10245 /*
10246 ** Otherwise, look for some disconnected job to
10247 ** abort for this target.
10248 */
10249 for (cp = np->ccbc; cp; cp = cp->link_ccb) {
10250 if (cp->host_status != HS_DISCONNECT)
10251 continue;
10252 if (cp->target != target)
10253 continue;
10254 if (cp->to_abort)
10255 break;
10256 }
10257
10258 /*
10259 ** If we have none, probably since the device has
10260 ** completed the command before we won abitration,
10261 ** send a M_ABORT message without IDENTIFY.
10262 ** According to the specs, the device must just
10263 ** disconnect the BUS and not abort any task.
10264 */
10265 if (!cp) {
10266 np->abrt_msg[0] = M_ABORT;
10267 np->abrt_tbl.size = 1;
10268 break;
10269 }
10270
10271 /*
10272 ** We have some task to abort.
10273 ** Set the IDENTIFY(lun)
10274 */
10275 np->abrt_msg[0] = M_IDENTIFY | cp->lun;
10276
10277 /*
10278 ** If we want to abort an untagged command, we
10279 ** will send a IDENTIFY + M_ABORT.
10280 ** Otherwise (tagged command), we will send
10281 ** a IDENTITFY + task attributes + ABORT TAG.
10282 */
10283 if (cp->tag == NO_TAG) {
10284 np->abrt_msg[1] = M_ABORT;
10285 np->abrt_tbl.size = 2;
10286 }
10287 else {
10288 np->abrt_msg[1] = cp->scsi_smsg[1];
10289 np->abrt_msg[2] = cp->scsi_smsg[2];
10290 np->abrt_msg[3] = M_ABORT_TAG;
10291 np->abrt_tbl.size = 4;
10292 }
10293 cp->to_abort = 0; /* We donnot expect to fail here */
10294 break;
10295
10296 /*
10297 ** The target has accepted our message and switched
10298 ** to BUS FREE phase as we expected.
10299 */
10300 case SIR_ABORT_SENT:
10301 target = (INB (nc_sdid) & 0xf);
10302 tp = &np->target[target];
10303
10304 /*
10305 ** If we didn't abort anything, leave here.
10306 */
10307 if (np->abrt_msg[0] == M_ABORT)
10308 break;
10309
10310 /*
10311 ** If we sent a M_RESET, then a hardware reset has
10312 ** been performed by the target.
10313 ** - Reset everything to async 8 bit
10314 ** - Tell ourself to negotiate next time :-)
10315 ** - Prepare to clear all disconnected CCBs for
10316 ** this target from our task list (lun=task=-1)
10317 */
10318 lun = -1;
10319 task = -1;
10320 if (np->abrt_msg[0] == M_RESET) {
10321 tp->sval = 0;
10322 tp->wval = np->rv_scntl3;
10323 tp->uval = np->rv_scntl4;
10324 ncr_set_sync_wide_status(np, target);
10325 ncr_negotiate(np, tp);
10326 }
10327
10328 /*
10329 ** Otherwise, check for the LUN and TASK(s)
10330 ** concerned by the cancelation.
10331 ** If it is not ABORT_TAG then it is CLEAR_QUEUE
10332 ** or an ABORT message :-)
10333 */
10334 else {
10335 lun = np->abrt_msg[0] & 0x3f;
10336 if (np->abrt_msg[1] == M_ABORT_TAG)
10337 task = np->abrt_msg[2];
10338 }
10339
10340 /*
10341 ** Complete all the CCBs the device should have
10342 ** aborted due to our 'kiss of death' message.
10343 */
10344 (void) ncr_clear_tasks(np, HS_ABORTED, target, lun, task);
10345 break;
10346
10347 /*
10348 ** We have performed a auto-sense that succeeded.
10349 ** If the device reports a UNIT ATTENTION condition
10350 ** due to a RESET condition, we must complete all
10351 ** disconnect CCBs for this unit since the device
10352 ** shall have thrown them away.
10353 ** Since I haven't time to guess what the specs are
10354 ** expecting for other UNIT ATTENTION conditions, I
10355 ** decided to only care about RESET conditions. :)
10356 */
10357 case SIR_AUTO_SENSE_DONE:
10358 cp = ncr_ccb_from_dsa(np, INL (nc_dsa));
10359 if (!cp)
10360 break;
10361 memcpy(cp->cmd->sense_buffer, cp->sense_buf,
10362 sizeof(cp->cmd->sense_buffer));
10363 p = &cp->cmd->sense_buffer[0];
10364
10365 if (p[0] != 0x70 || p[2] != 0x6 || p[12] != 0x29)
10366 break;
10367 #if 0
10368 (void) ncr_clear_tasks(np, HS_RESET, cp->target, cp->lun, -1);
10369 #endif
10370 break;
10371 }
10372
10373 /*
10374 ** Print to the log the message we intend to send.
10375 */
10376 if (num == SIR_TARGET_SELECTED) {
10377 PRINT_TARGET(np, target);
10378 ncr_printl_hex("control msgout:", np->abrt_msg,
10379 np->abrt_tbl.size);
10380 np->abrt_tbl.size = cpu_to_scr(np->abrt_tbl.size);
10381 }
10382
10383 /*
10384 ** Let the SCRIPTS processor continue.
10385 */
10386 OUTONB_STD ();
10387 }
10388
10389
10390 /*==========================================================
10391 **
10392 ** Gérard's alchemy:) that deals with with the data
10393 ** pointer for both MDP and the residual calculation.
10394 **
10395 **==========================================================
10396 **
10397 ** I didn't want to bloat the code by more than 200
10398 ** lignes for the handling of both MDP and the residual.
10399 ** This has been achieved by using a data pointer
10400 ** representation consisting in an index in the data
10401 ** array (dp_sg) and a negative offset (dp_ofs) that
10402 ** have the following meaning:
10403 **
10404 ** - dp_sg = MAX_SCATTER
10405 ** we are at the end of the data script.
10406 ** - dp_sg < MAX_SCATTER
10407 ** dp_sg points to the next entry of the scatter array
10408 ** we want to transfer.
10409 ** - dp_ofs < 0
10410 ** dp_ofs represents the residual of bytes of the
10411 ** previous entry scatter entry we will send first.
10412 ** - dp_ofs = 0
10413 ** no residual to send first.
10414 **
10415 ** The function ncr_evaluate_dp() accepts an arbitray
10416 ** offset (basically from the MDP message) and returns
10417 ** the corresponding values of dp_sg and dp_ofs.
10418 **
10419 **----------------------------------------------------------
10420 */
10421
10422 static int ncr_evaluate_dp(ncb_p np, ccb_p cp, u_int32 scr, int *ofs)
10423 {
10424 u_int32 dp_scr;
10425 int dp_ofs, dp_sg, dp_sgmin;
10426 int tmp;
10427 struct pm_ctx *pm;
10428
10429 /*
10430 ** Compute the resulted data pointer in term of a script
10431 ** address within some DATA script and a signed byte offset.
10432 */
10433 dp_scr = scr;
10434 dp_ofs = *ofs;
10435 if (dp_scr == NCB_SCRIPT_PHYS (np, pm0_data))
10436 pm = &cp->phys.pm0;
10437 else if (dp_scr == NCB_SCRIPT_PHYS (np, pm1_data))
10438 pm = &cp->phys.pm1;
10439 else
10440 pm = 0;
10441
10442 if (pm) {
10443 dp_scr = scr_to_cpu(pm->ret);
10444 dp_ofs -= scr_to_cpu(pm->sg.size);
10445 }
10446
10447 /*
10448 ** Deduce the index of the sg entry.
10449 ** Keep track of the index of the first valid entry.
10450 ** If result is dp_sg = MAX_SCATTER, then we are at the
10451 ** end of the data and vice-versa.
10452 */
10453 tmp = scr_to_cpu(cp->phys.header.goalp);
10454 dp_sg = MAX_SCATTER;
10455 if (dp_scr != tmp)
10456 dp_sg -= (tmp - 8 - (int)dp_scr) / (SCR_SG_SIZE*4);
10457 dp_sgmin = MAX_SCATTER - cp->segments;
10458
10459 /*
10460 ** Move to the sg entry the data pointer belongs to.
10461 **
10462 ** If we are inside the data area, we expect result to be:
10463 **
10464 ** Either,
10465 ** dp_ofs = 0 and dp_sg is the index of the sg entry
10466 ** the data pointer belongs to (or the end of the data)
10467 ** Or,
10468 ** dp_ofs < 0 and dp_sg is the index of the sg entry
10469 ** the data pointer belongs to + 1.
10470 */
10471 if (dp_ofs < 0) {
10472 int n;
10473 while (dp_sg > dp_sgmin) {
10474 --dp_sg;
10475 tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
10476 n = dp_ofs + (tmp & 0xffffff);
10477 if (n > 0) {
10478 ++dp_sg;
10479 break;
10480 }
10481 dp_ofs = n;
10482 }
10483 }
10484 else if (dp_ofs > 0) {
10485 while (dp_sg < MAX_SCATTER) {
10486 tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
10487 dp_ofs -= (tmp & 0xffffff);
10488 ++dp_sg;
10489 if (dp_ofs <= 0)
10490 break;
10491 }
10492 }
10493
10494 /*
10495 ** Make sure the data pointer is inside the data area.
10496 ** If not, return some error.
10497 */
10498 if (dp_sg < dp_sgmin || (dp_sg == dp_sgmin && dp_ofs < 0))
10499 goto out_err;
10500 else if (dp_sg > MAX_SCATTER || (dp_sg == MAX_SCATTER && dp_ofs > 0))
10501 goto out_err;
10502
10503 /*
10504 ** Save the extreme pointer if needed.
10505 */
10506 if (dp_sg > cp->ext_sg ||
10507 (dp_sg == cp->ext_sg && dp_ofs > cp->ext_ofs)) {
10508 cp->ext_sg = dp_sg;
10509 cp->ext_ofs = dp_ofs;
10510 }
10511
10512 /*
10513 ** Return data.
10514 */
10515 *ofs = dp_ofs;
10516 return dp_sg;
10517
10518 out_err:
10519 return -1;
10520 }
10521
10522 /*==========================================================
10523 **
10524 ** ncr chip handler for MODIFY DATA POINTER MESSAGE
10525 **
10526 **==========================================================
10527 **
10528 ** We also call this function on IGNORE WIDE RESIDUE
10529 ** messages that do not match a SWIDE full condition.
10530 ** Btw, we assume in that situation that such a message
10531 ** is equivalent to a MODIFY DATA POINTER (offset=-1).
10532 **
10533 **----------------------------------------------------------
10534 */
10535
10536 static void ncr_modify_dp(ncb_p np, tcb_p tp, ccb_p cp, int ofs)
10537 {
10538 int dp_ofs = ofs;
10539 u_int32 dp_scr = INL (nc_temp);
10540 u_int32 dp_ret;
10541 u_int32 tmp;
10542 u_char hflags;
10543 int dp_sg;
10544 struct pm_ctx *pm;
10545
10546 /*
10547 ** Not supported for auto_sense;
10548 */
10549 if (cp->host_flags & HF_AUTO_SENSE)
10550 goto out_reject;
10551
10552 /*
10553 ** Apply our alchemy:) (see comments in ncr_evaluate_dp()),
10554 ** to the resulted data pointer.
10555 */
10556 dp_sg = ncr_evaluate_dp(np, cp, dp_scr, &dp_ofs);
10557 if (dp_sg < 0)
10558 goto out_reject;
10559
10560 /*
10561 ** And our alchemy:) allows to easily calculate the data
10562 ** script address we want to return for the next data phase.
10563 */
10564 dp_ret = cpu_to_scr(cp->phys.header.goalp);
10565 dp_ret = dp_ret - 8 - (MAX_SCATTER - dp_sg) * (SCR_SG_SIZE*4);
10566
10567 /*
10568 ** If offset / scatter entry is zero we donnot need
10569 ** a context for the new current data pointer.
10570 */
10571 if (dp_ofs == 0) {
10572 dp_scr = dp_ret;
10573 goto out_ok;
10574 }
10575
10576 /*
10577 ** Get a context for the new current data pointer.
10578 */
10579 hflags = INB (HF_PRT);
10580
10581 if (hflags & HF_DP_SAVED)
10582 hflags ^= HF_ACT_PM;
10583
10584 if (!(hflags & HF_ACT_PM)) {
10585 pm = &cp->phys.pm0;
10586 dp_scr = NCB_SCRIPT_PHYS (np, pm0_data);
10587 }
10588 else {
10589 pm = &cp->phys.pm1;
10590 dp_scr = NCB_SCRIPT_PHYS (np, pm1_data);
10591 }
10592
10593 hflags &= ~(HF_DP_SAVED);
10594
10595 OUTB (HF_PRT, hflags);
10596
10597 /*
10598 ** Set up the new current data pointer.
10599 ** ofs < 0 there, and for the next data phase, we
10600 ** want to transfer part of the data of the sg entry
10601 ** corresponding to index dp_sg-1 prior to returning
10602 ** to the main data script.
10603 */
10604 pm->ret = cpu_to_scr(dp_ret);
10605 tmp = scr_to_cpu(cp->phys.data[dp_sg-1].addr);
10606 tmp += scr_to_cpu(cp->phys.data[dp_sg-1].size) + dp_ofs;
10607 pm->sg.addr = cpu_to_scr(tmp);
10608 pm->sg.size = cpu_to_scr(-dp_ofs);
10609
10610 out_ok:
10611 OUTL (nc_temp, dp_scr);
10612 OUTL_DSP (NCB_SCRIPT_PHYS (np, clrack));
10613 return;
10614
10615 out_reject:
10616 OUTL_DSP (NCB_SCRIPTH_PHYS (np, msg_bad));
10617 }
10618
10619
10620 /*==========================================================
10621 **
10622 ** ncr chip calculation of the data residual.
10623 **
10624 **==========================================================
10625 **
10626 ** As I used to say, the requirement of data residual
10627 ** in SCSI is broken, useless and cannot be achieved
10628 ** without huge complexity.
10629 ** But most OSes and even the official CAM require it.
10630 ** When stupidity happens to be so widely spread inside
10631 ** a community, it gets hard to convince.
10632 **
10633 ** Anyway, I don't care, since I am not going to use
10634 ** any software that considers this data residual as
10635 ** a relevant information. :)
10636 **
10637 **----------------------------------------------------------
10638 */
10639
10640 static int ncr_compute_residual(ncb_p np, ccb_p cp)
10641 {
10642 int dp_sg, dp_sgmin, tmp;
10643 int resid=0;
10644 int dp_ofs = 0;
10645
10646 /*
10647 * Check for some data lost or just thrown away.
10648 * We are not required to be quite accurate in this
10649 * situation. Btw, if we are odd for output and the
10650 * device claims some more data, it may well happen
10651 * than our residual be zero. :-)
10652 */
10653 if (cp->xerr_status & (XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN)) {
10654 if (cp->xerr_status & XE_EXTRA_DATA)
10655 resid -= cp->extra_bytes;
10656 if (cp->xerr_status & XE_SODL_UNRUN)
10657 ++resid;
10658 if (cp->xerr_status & XE_SWIDE_OVRUN)
10659 --resid;
10660 }
10661
10662
10663 /*
10664 ** If SCRIPTS reaches its goal point, then
10665 ** there is no additionnal residual.
10666 */
10667 if (cp->phys.header.lastp == cp->phys.header.goalp)
10668 return resid;
10669
10670 /*
10671 ** If the last data pointer is data_io (direction
10672 ** unknown), then no data transfer should have
10673 ** taken place.
10674 */
10675 if (cp->phys.header.lastp == NCB_SCRIPTH_PHYS (np, data_io))
10676 return cp->data_len;
10677
10678 /*
10679 ** If no data transfer occurs, or if the data
10680 ** pointer is weird, return full residual.
10681 */
10682 if (cp->startp == cp->phys.header.lastp ||
10683 ncr_evaluate_dp(np, cp, scr_to_cpu(cp->phys.header.lastp),
10684 &dp_ofs) < 0) {
10685 return cp->data_len;
10686 }
10687
10688 /*
10689 ** We are now full comfortable in the computation
10690 ** of the data residual (2's complement).
10691 */
10692 dp_sgmin = MAX_SCATTER - cp->segments;
10693 resid = -cp->ext_ofs;
10694 for (dp_sg = cp->ext_sg; dp_sg < MAX_SCATTER; ++dp_sg) {
10695 tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
10696 resid += (tmp & 0xffffff);
10697 }
10698
10699 /*
10700 ** Hopefully, the result is not too wrong.
10701 */
10702 return resid;
10703 }
10704
10705 /*==========================================================
10706 **
10707 ** Print out the containt of a SCSI message.
10708 **
10709 **==========================================================
10710 */
10711
10712 static int ncr_show_msg (u_char * msg)
10713 {
10714 u_char i;
10715 printk ("%x",*msg);
10716 if (*msg==M_EXTENDED) {
10717 for (i=1;i<8;i++) {
10718 if (i-1>msg[1]) break;
10719 printk ("-%x",msg[i]);
10720 };
10721 return (i+1);
10722 } else if ((*msg & 0xf0) == 0x20) {
10723 printk ("-%x",msg[1]);
10724 return (2);
10725 };
10726 return (1);
10727 }
10728
10729 static void ncr_print_msg (ccb_p cp, char *label, u_char *msg)
10730 {
10731 if (cp)
10732 PRINT_ADDR(cp->cmd);
10733 if (label)
10734 printk ("%s: ", label);
10735
10736 (void) ncr_show_msg (msg);
10737 printk (".\n");
10738 }
10739
10740 /*===================================================================
10741 **
10742 ** Negotiation for WIDE and SYNCHRONOUS DATA TRANSFER.
10743 **
10744 **===================================================================
10745 **
10746 ** Was Sie schon immer ueber transfermode negotiation wissen wollten ...
10747 **
10748 ** We try to negotiate sync and wide transfer only after
10749 ** a successful inquire command. We look at byte 7 of the
10750 ** inquire data to determine the capabilities of the target.
10751 **
10752 ** When we try to negotiate, we append the negotiation message
10753 ** to the identify and (maybe) simple tag message.
10754 ** The host status field is set to HS_NEGOTIATE to mark this
10755 ** situation.
10756 **
10757 ** If the target doesn't answer this message immediately
10758 ** (as required by the standard), the SIR_NEGO_FAILED interrupt
10759 ** will be raised eventually.
10760 ** The handler removes the HS_NEGOTIATE status, and sets the
10761 ** negotiated value to the default (async / nowide).
10762 **
10763 ** If we receive a matching answer immediately, we check it
10764 ** for validity, and set the values.
10765 **
10766 ** If we receive a Reject message immediately, we assume the
10767 ** negotiation has failed, and fall back to standard values.
10768 **
10769 ** If we receive a negotiation message while not in HS_NEGOTIATE
10770 ** state, it's a target initiated negotiation. We prepare a
10771 ** (hopefully) valid answer, set our parameters, and send back
10772 ** this answer to the target.
10773 **
10774 ** If the target doesn't fetch the answer (no message out phase),
10775 ** we assume the negotiation has failed, and fall back to default
10776 ** settings (SIR_NEGO_PROTO interrupt).
10777 **
10778 ** When we set the values, we adjust them in all ccbs belonging
10779 ** to this target, in the controller's register, and in the "phys"
10780 ** field of the controller's struct ncb.
10781 **
10782 **---------------------------------------------------------------------
10783 */
10784
10785 /*==========================================================
10786 **
10787 ** ncr chip handler for SYNCHRONOUS DATA TRANSFER
10788 ** REQUEST (SDTR) message.
10789 **
10790 **==========================================================
10791 **
10792 ** Read comments above.
10793 **
10794 **----------------------------------------------------------
10795 */
10796 static void ncr_sync_nego(ncb_p np, tcb_p tp, ccb_p cp)
10797 {
10798 u_char scntl3, scntl4;
10799 u_char chg, ofs, per, fak;
10800
10801 /*
10802 ** Synchronous request message received.
10803 */
10804
10805 if (DEBUG_FLAGS & DEBUG_NEGO) {
10806 ncr_print_msg(cp, "sync msg in", np->msgin);
10807 };
10808
10809 /*
10810 ** get requested values.
10811 */
10812
10813 chg = 0;
10814 per = np->msgin[3];
10815 ofs = np->msgin[4];
10816 if (ofs==0) per=255;
10817
10818 /*
10819 ** if target sends SDTR message,
10820 ** it CAN transfer synch.
10821 */
10822
10823 if (ofs)
10824 tp->inq_byte7 |= INQ7_SYNC;
10825
10826 /*
10827 ** check values against driver limits.
10828 */
10829
10830 if (per < np->minsync)
10831 {chg = 1; per = np->minsync;}
10832 if (per < tp->minsync)
10833 {chg = 1; per = tp->minsync;}
10834 if (ofs > np->maxoffs_st)
10835 {chg = 1; ofs = np->maxoffs_st;}
10836 if (ofs > tp->maxoffs)
10837 {chg = 1; ofs = tp->maxoffs;}
10838
10839 /*
10840 ** Check against controller limits.
10841 */
10842 fak = 7;
10843 scntl3 = 0;
10844 scntl4 = 0;
10845 if (ofs != 0) {
10846 ncr_getsync(np, per, &fak, &scntl3);
10847 if (fak > 7) {
10848 chg = 1;
10849 ofs = 0;
10850 }
10851 }
10852 if (ofs == 0) {
10853 fak = 7;
10854 per = 0;
10855 scntl3 = 0;
10856 scntl4 = 0;
10857 tp->minsync = 0;
10858 }
10859
10860 if (DEBUG_FLAGS & DEBUG_NEGO) {
10861 PRINT_ADDR(cp->cmd);
10862 printk ("sync: per=%d scntl3=0x%x scntl4=0x%x ofs=%d fak=%d chg=%d.\n",
10863 per, scntl3, scntl4, ofs, fak, chg);
10864 }
10865
10866 if (INB (HS_PRT) == HS_NEGOTIATE) {
10867 OUTB (HS_PRT, HS_BUSY);
10868 switch (cp->nego_status) {
10869 case NS_SYNC:
10870 /*
10871 ** This was an answer message
10872 */
10873 if (chg) {
10874 /*
10875 ** Answer wasn't acceptable.
10876 */
10877 ncr_setsync (np, cp, 0, 0xe0, 0);
10878 OUTL_DSP (NCB_SCRIPTH_PHYS (np, msg_bad));
10879 } else {
10880 /*
10881 ** Answer is ok.
10882 */
10883 if ((np->device_id != PCI_DEVICE_ID_LSI_53C1010) &&
10884 (np->device_id != PCI_DEVICE_ID_LSI_53C1010_66))
10885 ncr_setsync (np, cp, scntl3, (fak<<5)|ofs,0);
10886 else
10887 ncr_setsync (np, cp, scntl3, ofs, scntl4);
10888
10889 OUTL_DSP (NCB_SCRIPT_PHYS (np, clrack));
10890 };
10891 return;
10892
10893 case NS_WIDE:
10894 ncr_setwide (np, cp, 0, 0);
10895 break;
10896 };
10897 };
10898
10899 /*
10900 ** It was a request. Set value and
10901 ** prepare an answer message
10902 */
10903
10904 if ((np->device_id != PCI_DEVICE_ID_LSI_53C1010) &&
10905 (np->device_id != PCI_DEVICE_ID_LSI_53C1010_66))
10906 ncr_setsync (np, cp, scntl3, (fak<<5)|ofs,0);
10907 else
10908 ncr_setsync (np, cp, scntl3, ofs, scntl4);
10909
10910 np->msgout[0] = M_EXTENDED;
10911 np->msgout[1] = 3;
10912 np->msgout[2] = M_X_SYNC_REQ;
10913 np->msgout[3] = per;
10914 np->msgout[4] = ofs;
10915
10916 cp->nego_status = NS_SYNC;
10917
10918 if (DEBUG_FLAGS & DEBUG_NEGO) {
10919 ncr_print_msg(cp, "sync msgout", np->msgout);
10920 }
10921
10922 np->msgin [0] = M_NOOP;
10923
10924 if (!ofs)
10925 OUTL_DSP (NCB_SCRIPTH_PHYS (np, msg_bad));
10926 else
10927 OUTL_DSP (NCB_SCRIPTH_PHYS (np, sdtr_resp));
10928 }
10929
10930 /*==========================================================
10931 **
10932 ** ncr chip handler for WIDE DATA TRANSFER REQUEST
10933 ** (WDTR) message.
10934 **
10935 **==========================================================
10936 **
10937 ** Read comments above.
10938 **
10939 **----------------------------------------------------------
10940 */
10941 static void ncr_wide_nego(ncb_p np, tcb_p tp, ccb_p cp)
10942 {
10943 u_char chg, wide;
10944
10945 /*
10946 ** Wide request message received.
10947 */
10948 if (DEBUG_FLAGS & DEBUG_NEGO) {
10949 ncr_print_msg(cp, "wide msgin", np->msgin);
10950 };
10951
10952 /*
10953 ** get requested values.
10954 */
10955
10956 chg = 0;
10957 wide = np->msgin[3];
10958
10959 /*
10960 ** if target sends WDTR message,
10961 ** it CAN transfer wide.
10962 */
10963
10964 if (wide)
10965 tp->inq_byte7 |= INQ7_WIDE16;
10966
10967 /*
10968 ** check values against driver limits.
10969 */
10970
10971 if (wide > tp->usrwide)
10972 {chg = 1; wide = tp->usrwide;}
10973
10974 if (DEBUG_FLAGS & DEBUG_NEGO) {
10975 PRINT_ADDR(cp->cmd);
10976 printk ("wide: wide=%d chg=%d.\n", wide, chg);
10977 }
10978
10979 if (INB (HS_PRT) == HS_NEGOTIATE) {
10980 OUTB (HS_PRT, HS_BUSY);
10981 switch (cp->nego_status) {
10982 case NS_WIDE:
10983 /*
10984 ** This was an answer message
10985 */
10986 if (chg) {
10987 /*
10988 ** Answer wasn't acceptable.
10989 */
10990 ncr_setwide (np, cp, 0, 1);
10991 OUTL_DSP (NCB_SCRIPTH_PHYS (np, msg_bad));
10992 } else {
10993 /*
10994 ** Answer is ok.
10995 */
10996 ncr_setwide (np, cp, wide, 1);
10997 OUTL_DSP (NCB_SCRIPT_PHYS (np, clrack));
10998 };
10999 return;
11000
11001 case NS_SYNC:
11002 ncr_setsync (np, cp, 0, 0xe0, 0);
11003 break;
11004 };
11005 };
11006
11007 /*
11008 ** It was a request, set value and
11009 ** prepare an answer message
11010 */
11011
11012 ncr_setwide (np, cp, wide, 1);
11013
11014 np->msgout[0] = M_EXTENDED;
11015 np->msgout[1] = 2;
11016 np->msgout[2] = M_X_WIDE_REQ;
11017 np->msgout[3] = wide;
11018
11019 np->msgin [0] = M_NOOP;
11020
11021 cp->nego_status = NS_WIDE;
11022
11023 if (DEBUG_FLAGS & DEBUG_NEGO) {
11024 ncr_print_msg(cp, "wide msgout", np->msgout);
11025 }
11026
11027 OUTL_DSP (NCB_SCRIPTH_PHYS (np, wdtr_resp));
11028 }
11029 /*==========================================================
11030 **
11031 ** ncr chip handler for PARALLEL PROTOCOL REQUEST
11032 ** (PPR) message.
11033 **
11034 **==========================================================
11035 **
11036 ** Read comments above.
11037 **
11038 **----------------------------------------------------------
11039 */
11040 static void ncr_ppr_nego(ncb_p np, tcb_p tp, ccb_p cp)
11041 {
11042 u_char scntl3, scntl4;
11043 u_char chg, ofs, per, fak, wth, dt;
11044
11045 /*
11046 ** PPR message received.
11047 */
11048
11049 if (DEBUG_FLAGS & DEBUG_NEGO) {
11050 ncr_print_msg(cp, "ppr msg in", np->msgin);
11051 };
11052
11053 /*
11054 ** get requested values.
11055 */
11056
11057 chg = 0;
11058 per = np->msgin[3];
11059 ofs = np->msgin[5];
11060 wth = np->msgin[6];
11061 dt = np->msgin[7];
11062 if (ofs==0) per=255;
11063
11064 /*
11065 ** if target sends sync (wide),
11066 ** it CAN transfer synch (wide).
11067 */
11068
11069 if (ofs)
11070 tp->inq_byte7 |= INQ7_SYNC;
11071
11072 if (wth)
11073 tp->inq_byte7 |= INQ7_WIDE16;
11074
11075 /*
11076 ** check values against driver limits.
11077 */
11078
11079 if (wth > tp->usrwide)
11080 {chg = 1; wth = tp->usrwide;}
11081 if (per < np->minsync)
11082 {chg = 1; per = np->minsync;}
11083 if (per < tp->minsync)
11084 {chg = 1; per = tp->minsync;}
11085 if (ofs > tp->maxoffs)
11086 {chg = 1; ofs = tp->maxoffs;}
11087
11088 /*
11089 ** Check against controller limits.
11090 */
11091 fak = 7;
11092 scntl3 = 0;
11093 scntl4 = 0;
11094 if (ofs != 0) {
11095 scntl4 = dt ? 0x80 : 0;
11096 ncr_getsync(np, per, &fak, &scntl3);
11097 if (fak > 7) {
11098 chg = 1;
11099 ofs = 0;
11100 }
11101 }
11102 if (ofs == 0) {
11103 fak = 7;
11104 per = 0;
11105 scntl3 = 0;
11106 scntl4 = 0;
11107 tp->minsync = 0;
11108 }
11109
11110 /*
11111 ** If target responds with Ultra 3 speed
11112 ** but narrow or not DT, reject.
11113 ** If target responds with DT request
11114 ** but not Ultra3 speeds, reject message,
11115 ** reset min sync for target to 0x0A and
11116 ** set flags to re-negotiate.
11117 */
11118
11119 if ((per == 0x09) && ofs && (!wth || !dt))
11120 chg = 1;
11121 else if (( (per > 0x09) && dt) )
11122 chg = 2;
11123
11124 /* Not acceptable since beyond controller limit */
11125 if (!dt && ofs > np->maxoffs_st)
11126 {chg = 2; ofs = np->maxoffs_st;}
11127
11128 if (DEBUG_FLAGS & DEBUG_NEGO) {
11129 PRINT_ADDR(cp->cmd);
11130 printk ("ppr: wth=%d per=%d scntl3=0x%x scntl4=0x%x ofs=%d fak=%d chg=%d.\n",
11131 wth, per, scntl3, scntl4, ofs, fak, chg);
11132 }
11133
11134 if (INB (HS_PRT) == HS_NEGOTIATE) {
11135 OUTB (HS_PRT, HS_BUSY);
11136 switch (cp->nego_status) {
11137 case NS_PPR:
11138 /*
11139 ** This was an answer message
11140 */
11141 if (chg) {
11142 /*
11143 ** Answer wasn't acceptable.
11144 */
11145 if (chg == 2) {
11146 /* Send message reject and reset flags for
11147 ** host to re-negotiate with min period 0x0A.
11148 */
11149 tp->minsync = 0x0A;
11150 tp->period = 0;
11151 tp->widedone = 0;
11152 }
11153 ncr_setsyncwide (np, cp, 0, 0xe0, 0, 0);
11154 OUTL_DSP (NCB_SCRIPTH_PHYS (np, msg_bad));
11155 } else {
11156 /*
11157 ** Answer is ok.
11158 */
11159
11160 if ((np->device_id != PCI_DEVICE_ID_LSI_53C1010) &&
11161 (np->device_id != PCI_DEVICE_ID_LSI_53C1010_66))
11162 ncr_setsyncwide (np, cp, scntl3, (fak<<5)|ofs,0, wth);
11163 else
11164 ncr_setsyncwide (np, cp, scntl3, ofs, scntl4, wth);
11165
11166 OUTL_DSP (NCB_SCRIPT_PHYS (np, clrack));
11167
11168 };
11169 return;
11170
11171 case NS_SYNC:
11172 ncr_setsync (np, cp, 0, 0xe0, 0);
11173 break;
11174
11175 case NS_WIDE:
11176 ncr_setwide (np, cp, 0, 0);
11177 break;
11178 };
11179 };
11180
11181 /*
11182 ** It was a request. Set value and
11183 ** prepare an answer message
11184 **
11185 ** If narrow or not DT and requesting Ultra3
11186 ** slow the bus down and force ST. If not
11187 ** requesting Ultra3, force ST.
11188 ** Max offset is 31=0x1f if ST mode.
11189 */
11190
11191 if ((per == 0x09) && ofs && (!wth || !dt)) {
11192 per = 0x0A;
11193 dt = 0;
11194 }
11195 else if ( (per > 0x09) && dt) {
11196 dt = 0;
11197 }
11198 if (!dt && ofs > np->maxoffs_st)
11199 ofs = np->maxoffs_st;
11200
11201 if ((np->device_id != PCI_DEVICE_ID_LSI_53C1010) &&
11202 (np->device_id != PCI_DEVICE_ID_LSI_53C1010_66))
11203 ncr_setsyncwide (np, cp, scntl3, (fak<<5)|ofs,0, wth);
11204 else
11205 ncr_setsyncwide (np, cp, scntl3, ofs, scntl4, wth);
11206
11207 np->msgout[0] = M_EXTENDED;
11208 np->msgout[1] = 6;
11209 np->msgout[2] = M_X_PPR_REQ;
11210 np->msgout[3] = per;
11211 np->msgout[4] = 0;
11212 np->msgout[5] = ofs;
11213 np->msgout[6] = wth;
11214 np->msgout[7] = dt;
11215
11216 cp->nego_status = NS_PPR;
11217
11218 if (DEBUG_FLAGS & DEBUG_NEGO) {
11219 ncr_print_msg(cp, "ppr msgout", np->msgout);
11220 }
11221
11222 np->msgin [0] = M_NOOP;
11223
11224 if (!ofs)
11225 OUTL_DSP (NCB_SCRIPTH_PHYS (np, msg_bad));
11226 else
11227 OUTL_DSP (NCB_SCRIPTH_PHYS (np, ppr_resp));
11228 }
11229
11230
11231
11232 /*
11233 ** Reset SYNC or WIDE to default settings.
11234 ** Called when a negotiation does not succeed either
11235 ** on rejection or on protocol error.
11236 */
11237 static void ncr_nego_default(ncb_p np, tcb_p tp, ccb_p cp)
11238 {
11239 /*
11240 ** any error in negotiation:
11241 ** fall back to default mode.
11242 */
11243 switch (cp->nego_status) {
11244
11245 case NS_SYNC:
11246 ncr_setsync (np, cp, 0, 0xe0, 0);
11247 break;
11248
11249 case NS_WIDE:
11250 ncr_setwide (np, cp, 0, 0);
11251 break;
11252
11253 case NS_PPR:
11254 /*
11255 * ppr_negotiation is set to 1 on the first ppr nego command.
11256 * If ppr is successful, it is reset to 2.
11257 * If unsuccessful it is reset to 0.
11258 */
11259 if (DEBUG_FLAGS & DEBUG_NEGO) {
11260 tcb_p tp=&np->target[cp->target];
11261 u_char factor, offset, width;
11262
11263 ncr_get_xfer_info ( np, tp, &factor, &offset, &width);
11264
11265 printk("Current factor %d offset %d width %d\n",
11266 factor, offset, width);
11267 }
11268 if (tp->ppr_negotiation == 2)
11269 ncr_setsyncwide (np, cp, 0, 0xe0, 0, 0);
11270 else if (tp->ppr_negotiation == 1) {
11271
11272 /* First ppr command has received a M REJECT.
11273 * Do not change the existing wide/sync parameter
11274 * values (asyn/narrow if this as the first nego;
11275 * may be different if target initiates nego.).
11276 */
11277 tp->ppr_negotiation = 0;
11278 }
11279 else
11280 {
11281 tp->ppr_negotiation = 0;
11282 ncr_setwide (np, cp, 0, 0);
11283 }
11284 break;
11285 };
11286 np->msgin [0] = M_NOOP;
11287 np->msgout[0] = M_NOOP;
11288 cp->nego_status = 0;
11289 }
11290
11291 /*==========================================================
11292 **
11293 ** ncr chip handler for MESSAGE REJECT received for
11294 ** a WIDE or SYNCHRONOUS negotiation.
11295 **
11296 ** clear the PPR negotiation flag, all future nego.
11297 ** will be SDTR and WDTR
11298 **
11299 **==========================================================
11300 **
11301 ** Read comments above.
11302 **
11303 **----------------------------------------------------------
11304 */
11305 static void ncr_nego_rejected(ncb_p np, tcb_p tp, ccb_p cp)
11306 {
11307 ncr_nego_default(np, tp, cp);
11308 OUTB (HS_PRT, HS_BUSY);
11309 }
11310
11311
11312 /*==========================================================
11313 **
11314 **
11315 ** ncr chip exception handler for programmed interrupts.
11316 **
11317 **
11318 **==========================================================
11319 */
11320
11321 void ncr_int_sir (ncb_p np)
11322 {
11323 u_char num = INB (nc_dsps);
11324 u_long dsa = INL (nc_dsa);
11325 ccb_p cp = ncr_ccb_from_dsa(np, dsa);
11326 u_char target = INB (nc_sdid) & 0x0f;
11327 tcb_p tp = &np->target[target];
11328 int tmp;
11329
11330 if (DEBUG_FLAGS & DEBUG_TINY) printk ("I#%d", num);
11331
11332 switch (num) {
11333 /*
11334 ** See comments in the SCRIPTS code.
11335 */
11336 #ifdef SCSI_NCR_PCIQ_SYNC_ON_INTR
11337 case SIR_DUMMY_INTERRUPT:
11338 goto out;
11339 #endif
11340
11341 /*
11342 ** The C code is currently trying to recover from something.
11343 ** Typically, user want to abort some command.
11344 */
11345 case SIR_SCRIPT_STOPPED:
11346 case SIR_TARGET_SELECTED:
11347 case SIR_ABORT_SENT:
11348 case SIR_AUTO_SENSE_DONE:
11349 ncr_sir_task_recovery(np, num);
11350 return;
11351 /*
11352 ** The device didn't go to MSG OUT phase after having
11353 ** been selected with ATN. We donnot want to handle
11354 ** that.
11355 */
11356 case SIR_SEL_ATN_NO_MSG_OUT:
11357 printk ("%s:%d: No MSG OUT phase after selection with ATN.\n",
11358 ncr_name (np), target);
11359 goto out_stuck;
11360 /*
11361 ** The device didn't switch to MSG IN phase after
11362 ** having reseleted the initiator.
11363 */
11364 case SIR_RESEL_NO_MSG_IN:
11365 /*
11366 ** After reselection, the device sent a message that wasn't
11367 ** an IDENTIFY.
11368 */
11369 case SIR_RESEL_NO_IDENTIFY:
11370 /*
11371 ** If devices reselecting without sending an IDENTIFY
11372 ** message still exist, this should help.
11373 ** We just assume lun=0, 1 CCB, no tag.
11374 */
11375 if (tp->l0p) {
11376 OUTL (nc_dsa, scr_to_cpu(tp->l0p->tasktbl[0]));
11377 OUTL_DSP (NCB_SCRIPT_PHYS (np, resel_go));
11378 return;
11379 }
11380 /*
11381 ** The device reselected a LUN we donnot know of.
11382 */
11383 case SIR_RESEL_BAD_LUN:
11384 np->msgout[0] = M_RESET;
11385 goto out;
11386 /*
11387 ** The device reselected for an untagged nexus and we
11388 ** haven't any.
11389 */
11390 case SIR_RESEL_BAD_I_T_L:
11391 np->msgout[0] = M_ABORT;
11392 goto out;
11393 /*
11394 ** The device reselected for a tagged nexus that we donnot
11395 ** have.
11396 */
11397 case SIR_RESEL_BAD_I_T_L_Q:
11398 np->msgout[0] = M_ABORT_TAG;
11399 goto out;
11400 /*
11401 ** The SCRIPTS let us know that the device has grabbed
11402 ** our message and will abort the job.
11403 */
11404 case SIR_RESEL_ABORTED:
11405 np->lastmsg = np->msgout[0];
11406 np->msgout[0] = M_NOOP;
11407 printk ("%s:%d: message %x sent on bad reselection.\n",
11408 ncr_name (np), target, np->lastmsg);
11409 goto out;
11410 /*
11411 ** The SCRIPTS let us know that a message has been
11412 ** successfully sent to the device.
11413 */
11414 case SIR_MSG_OUT_DONE:
11415 np->lastmsg = np->msgout[0];
11416 np->msgout[0] = M_NOOP;
11417 /* Should we really care of that */
11418 if (np->lastmsg == M_PARITY || np->lastmsg == M_ID_ERROR) {
11419 if (cp) {
11420 cp->xerr_status &= ~XE_PARITY_ERR;
11421 if (!cp->xerr_status)
11422 OUTOFFB (HF_PRT, HF_EXT_ERR);
11423 }
11424 }
11425 goto out;
11426 /*
11427 ** The device didn't send a GOOD SCSI status.
11428 ** We may have some work to do prior to allow
11429 ** the SCRIPTS processor to continue.
11430 */
11431 case SIR_BAD_STATUS:
11432 if (!cp)
11433 goto out;
11434 ncr_sir_to_redo(np, num, cp);
11435 return;
11436 /*
11437 ** We are asked by the SCRIPTS to prepare a
11438 ** REJECT message.
11439 */
11440 case SIR_REJECT_TO_SEND:
11441 ncr_print_msg(cp, "M_REJECT to send for ", np->msgin);
11442 np->msgout[0] = M_REJECT;
11443 goto out;
11444 /*
11445 ** We have been ODD at the end of a DATA IN
11446 ** transfer and the device didn't send a
11447 ** IGNORE WIDE RESIDUE message.
11448 ** It is a data overrun condition.
11449 */
11450 case SIR_SWIDE_OVERRUN:
11451 if (cp) {
11452 OUTONB (HF_PRT, HF_EXT_ERR);
11453 cp->xerr_status |= XE_SWIDE_OVRUN;
11454 }
11455 goto out;
11456 /*
11457 ** We have been ODD at the end of a DATA OUT
11458 ** transfer.
11459 ** It is a data underrun condition.
11460 */
11461 case SIR_SODL_UNDERRUN:
11462 if (cp) {
11463 OUTONB (HF_PRT, HF_EXT_ERR);
11464 cp->xerr_status |= XE_SODL_UNRUN;
11465 }
11466 goto out;
11467 /*
11468 ** The device wants us to tranfer more data than
11469 ** expected or in the wrong direction.
11470 ** The number of extra bytes is in scratcha.
11471 ** It is a data overrun condition.
11472 */
11473 case SIR_DATA_OVERRUN:
11474 if (cp) {
11475 OUTONB (HF_PRT, HF_EXT_ERR);
11476 cp->xerr_status |= XE_EXTRA_DATA;
11477 cp->extra_bytes += INL (nc_scratcha);
11478 }
11479 goto out;
11480 /*
11481 ** The device switched to an illegal phase (4/5).
11482 */
11483 case SIR_BAD_PHASE:
11484 if (cp) {
11485 OUTONB (HF_PRT, HF_EXT_ERR);
11486 cp->xerr_status |= XE_BAD_PHASE;
11487 }
11488 goto out;
11489 /*
11490 ** We received a message.
11491 */
11492 case SIR_MSG_RECEIVED:
11493 if (!cp)
11494 goto out_stuck;
11495 switch (np->msgin [0]) {
11496 /*
11497 ** We received an extended message.
11498 ** We handle MODIFY DATA POINTER, SDTR, WDTR
11499 ** and reject all other extended messages.
11500 */
11501 case M_EXTENDED:
11502 switch (np->msgin [2]) {
11503 case M_X_MODIFY_DP:
11504 if (DEBUG_FLAGS & DEBUG_POINTER)
11505 ncr_print_msg(cp,"modify DP",np->msgin);
11506 tmp = (np->msgin[3]<<24) + (np->msgin[4]<<16) +
11507 (np->msgin[5]<<8) + (np->msgin[6]);
11508 ncr_modify_dp(np, tp, cp, tmp);
11509 return;
11510 case M_X_SYNC_REQ:
11511 ncr_sync_nego(np, tp, cp);
11512 return;
11513 case M_X_WIDE_REQ:
11514 ncr_wide_nego(np, tp, cp);
11515 return;
11516 case M_X_PPR_REQ:
11517 ncr_ppr_nego(np, tp, cp);
11518 return;
11519 default:
11520 goto out_reject;
11521 }
11522 break;
11523 /*
11524 ** We received a 1/2 byte message not handled from SCRIPTS.
11525 ** We are only expecting MESSAGE REJECT and IGNORE WIDE
11526 ** RESIDUE messages that haven't been anticipated by
11527 ** SCRIPTS on SWIDE full condition. Unanticipated IGNORE
11528 ** WIDE RESIDUE messages are aliased as MODIFY DP (-1).
11529 */
11530 case M_IGN_RESIDUE:
11531 if (DEBUG_FLAGS & DEBUG_POINTER)
11532 ncr_print_msg(cp,"ign wide residue", np->msgin);
11533 ncr_modify_dp(np, tp, cp, -1);
11534 return;
11535 case M_REJECT:
11536 if (INB (HS_PRT) == HS_NEGOTIATE)
11537 ncr_nego_rejected(np, tp, cp);
11538 else {
11539 PRINT_ADDR(cp->cmd);
11540 printk ("M_REJECT received (%x:%x).\n",
11541 scr_to_cpu(np->lastmsg), np->msgout[0]);
11542 }
11543 goto out_clrack;
11544 break;
11545 default:
11546 goto out_reject;
11547 }
11548 break;
11549 /*
11550 ** We received an unknown message.
11551 ** Ignore all MSG IN phases and reject it.
11552 */
11553 case SIR_MSG_WEIRD:
11554 ncr_print_msg(cp, "WEIRD message received", np->msgin);
11555 OUTL_DSP (NCB_SCRIPTH_PHYS (np, msg_weird));
11556 return;
11557 /*
11558 ** Negotiation failed.
11559 ** Target does not send us the reply.
11560 ** Remove the HS_NEGOTIATE status.
11561 */
11562 case SIR_NEGO_FAILED:
11563 OUTB (HS_PRT, HS_BUSY);
11564 /*
11565 ** Negotiation failed.
11566 ** Target does not want answer message.
11567 */
11568 case SIR_NEGO_PROTO:
11569 ncr_nego_default(np, tp, cp);
11570 goto out;
11571 };
11572
11573 out:
11574 OUTONB_STD ();
11575 return;
11576 out_reject:
11577 OUTL_DSP (NCB_SCRIPTH_PHYS (np, msg_bad));
11578 return;
11579 out_clrack:
11580 OUTL_DSP (NCB_SCRIPT_PHYS (np, clrack));
11581 return;
11582 out_stuck:
11583 return;
11584 }
11585
11586
11587 /*==========================================================
11588 **
11589 **
11590 ** Acquire a control block
11591 **
11592 **
11593 **==========================================================
11594 */
11595
11596 static ccb_p ncr_get_ccb (ncb_p np, u_char tn, u_char ln)
11597 {
11598 tcb_p tp = &np->target[tn];
11599 lcb_p lp = ncr_lp(np, tp, ln);
11600 u_short tag = NO_TAG;
11601 XPT_QUEHEAD *qp;
11602 ccb_p cp = (ccb_p) 0;
11603
11604 /*
11605 ** Allocate a new CCB if needed.
11606 */
11607 if (xpt_que_empty(&np->free_ccbq))
11608 (void) ncr_alloc_ccb(np);
11609
11610 /*
11611 ** Look for a free CCB
11612 */
11613 qp = xpt_remque_head(&np->free_ccbq);
11614 if (!qp)
11615 goto out;
11616 cp = xpt_que_entry(qp, struct ccb, link_ccbq);
11617
11618 /*
11619 ** If the LCB is not yet available and we already
11620 ** have queued a CCB for a LUN without LCB,
11621 ** give up. Otherwise all is fine. :-)
11622 */
11623 if (!lp) {
11624 if (xpt_que_empty(&np->b0_ccbq))
11625 xpt_insque_head(&cp->link_ccbq, &np->b0_ccbq);
11626 else
11627 goto out_free;
11628 } else {
11629 /*
11630 ** Tune tag mode if asked by user.
11631 */
11632 if (lp->queuedepth != lp->numtags) {
11633 ncr_setup_tags(np, tn, ln);
11634 }
11635
11636 /*
11637 ** Get a tag for this nexus if required.
11638 ** Keep from using more tags than we can handle.
11639 */
11640 if (lp->usetags) {
11641 if (lp->busyccbs < lp->maxnxs) {
11642 tag = lp->cb_tags[lp->ia_tag];
11643 ++lp->ia_tag;
11644 if (lp->ia_tag == MAX_TAGS)
11645 lp->ia_tag = 0;
11646 cp->tags_si = lp->tags_si;
11647 ++lp->tags_sum[cp->tags_si];
11648 }
11649 else
11650 goto out_free;
11651 }
11652
11653 /*
11654 ** Put the CCB in the LUN wait queue and
11655 ** count it as busy.
11656 */
11657 xpt_insque_tail(&cp->link_ccbq, &lp->wait_ccbq);
11658 ++lp->busyccbs;
11659 }
11660
11661 /*
11662 ** Remember all informations needed to free this CCB.
11663 */
11664 cp->to_abort = 0;
11665 cp->tag = tag;
11666 cp->target = tn;
11667 cp->lun = ln;
11668
11669 if (DEBUG_FLAGS & DEBUG_TAGS) {
11670 PRINT_LUN(np, tn, ln);
11671 printk ("ccb @%p using tag %d.\n", cp, tag);
11672 }
11673
11674 out:
11675 return cp;
11676 out_free:
11677 xpt_insque_head(&cp->link_ccbq, &np->free_ccbq);
11678 return (ccb_p) 0;
11679 }
11680
11681 /*==========================================================
11682 **
11683 **
11684 ** Release one control block
11685 **
11686 **
11687 **==========================================================
11688 */
11689
11690 static void ncr_free_ccb (ncb_p np, ccb_p cp)
11691 {
11692 tcb_p tp = &np->target[cp->target];
11693 lcb_p lp = ncr_lp(np, tp, cp->lun);
11694
11695 if (DEBUG_FLAGS & DEBUG_TAGS) {
11696 PRINT_LUN(np, cp->target, cp->lun);
11697 printk ("ccb @%p freeing tag %d.\n", cp, cp->tag);
11698 }
11699
11700 /*
11701 ** If lun control block available, make available
11702 ** the task slot and the tag if any.
11703 ** Decrement counters.
11704 */
11705 if (lp) {
11706 if (cp->tag != NO_TAG) {
11707 lp->cb_tags[lp->if_tag++] = cp->tag;
11708 if (lp->if_tag == MAX_TAGS)
11709 lp->if_tag = 0;
11710 --lp->tags_sum[cp->tags_si];
11711 lp->tasktbl[cp->tag] = cpu_to_scr(np->p_bad_i_t_l_q);
11712 } else {
11713 lp->tasktbl[0] = cpu_to_scr(np->p_bad_i_t_l);
11714 }
11715 --lp->busyccbs;
11716 if (cp->queued) {
11717 --lp->queuedccbs;
11718 }
11719 }
11720
11721 /*
11722 ** Make this CCB available.
11723 */
11724 xpt_remque(&cp->link_ccbq);
11725 xpt_insque_head(&cp->link_ccbq, &np->free_ccbq);
11726 cp -> host_status = HS_IDLE;
11727 cp -> queued = 0;
11728 }
11729
11730 /*------------------------------------------------------------------------
11731 ** Allocate a CCB and initialize its fixed part.
11732 **------------------------------------------------------------------------
11733 **------------------------------------------------------------------------
11734 */
11735 static ccb_p ncr_alloc_ccb(ncb_p np)
11736 {
11737 ccb_p cp = 0;
11738 int hcode;
11739
11740 /*
11741 ** Allocate memory for this CCB.
11742 */
11743 cp = m_calloc_dma(sizeof(struct ccb), "CCB");
11744 if (!cp)
11745 return 0;
11746
11747 /*
11748 ** Count it and initialyze it.
11749 */
11750 np->actccbs++;
11751
11752 /*
11753 ** Remember virtual and bus address of this ccb.
11754 */
11755 cp->p_ccb = vtobus(cp);
11756
11757 /*
11758 ** Insert this ccb into the hashed list.
11759 */
11760 hcode = CCB_HASH_CODE(cp->p_ccb);
11761 cp->link_ccbh = np->ccbh[hcode];
11762 np->ccbh[hcode] = cp;
11763
11764 /*
11765 ** Initialyze the start and restart actions.
11766 */
11767 cp->phys.header.go.start = cpu_to_scr(NCB_SCRIPT_PHYS (np, idle));
11768 cp->phys.header.go.restart = cpu_to_scr(NCB_SCRIPTH_PHYS(np,bad_i_t_l));
11769
11770 /*
11771 ** Initilialyze some other fields.
11772 */
11773 cp->phys.smsg_ext.addr = cpu_to_scr(NCB_PHYS(np, msgin[2]));
11774
11775 /*
11776 ** Chain into wakeup list and free ccb queue.
11777 */
11778 cp->link_ccb = np->ccbc;
11779 np->ccbc = cp;
11780
11781 xpt_insque_head(&cp->link_ccbq, &np->free_ccbq);
11782
11783 return cp;
11784 }
11785
11786 /*------------------------------------------------------------------------
11787 ** Look up a CCB from a DSA value.
11788 **------------------------------------------------------------------------
11789 **------------------------------------------------------------------------
11790 */
11791 static ccb_p ncr_ccb_from_dsa(ncb_p np, u_long dsa)
11792 {
11793 int hcode;
11794 ccb_p cp;
11795
11796 hcode = CCB_HASH_CODE(dsa);
11797 cp = np->ccbh[hcode];
11798 while (cp) {
11799 if (cp->p_ccb == dsa)
11800 break;
11801 cp = cp->link_ccbh;
11802 }
11803
11804 return cp;
11805 }
11806
11807 /*==========================================================
11808 **
11809 **
11810 ** Allocation of resources for Targets/Luns/Tags.
11811 **
11812 **
11813 **==========================================================
11814 */
11815
11816
11817 /*------------------------------------------------------------------------
11818 ** Target control block initialisation.
11819 **------------------------------------------------------------------------
11820 ** This data structure is fully initialized after a SCSI command
11821 ** has been successfully completed for this target.
11822 **------------------------------------------------------------------------
11823 */
11824 static void ncr_init_tcb (ncb_p np, u_char tn)
11825 {
11826 /*
11827 ** Check some alignments required by the chip.
11828 */
11829 assert (( (offsetof(struct ncr_reg, nc_sxfer) ^
11830 offsetof(struct tcb , sval )) &3) == 0);
11831 assert (( (offsetof(struct ncr_reg, nc_scntl3) ^
11832 offsetof(struct tcb , wval )) &3) == 0);
11833 if ((np->device_id == PCI_DEVICE_ID_LSI_53C1010) ||
11834 (np->device_id == PCI_DEVICE_ID_LSI_53C1010_66)){
11835 assert (( (offsetof(struct ncr_reg, nc_scntl4) ^
11836 offsetof(struct tcb , uval )) &3) == 0);
11837 }
11838 }
11839
11840 /*------------------------------------------------------------------------
11841 ** Lun control block allocation and initialization.
11842 **------------------------------------------------------------------------
11843 ** This data structure is allocated and initialized after a SCSI
11844 ** command has been successfully completed for this target/lun.
11845 **------------------------------------------------------------------------
11846 */
11847 static lcb_p ncr_alloc_lcb (ncb_p np, u_char tn, u_char ln)
11848 {
11849 tcb_p tp = &np->target[tn];
11850 lcb_p lp = ncr_lp(np, tp, ln);
11851
11852 /*
11853 ** Already done, return.
11854 */
11855 if (lp)
11856 return lp;
11857
11858 /*
11859 ** Initialize the target control block if not yet.
11860 */
11861 ncr_init_tcb(np, tn);
11862
11863 /*
11864 ** Allocate the lcb bus address array.
11865 ** Compute the bus address of this table.
11866 */
11867 if (ln && !tp->luntbl) {
11868 int i;
11869
11870 tp->luntbl = m_calloc_dma(256, "LUNTBL");
11871 if (!tp->luntbl)
11872 goto fail;
11873 for (i = 0 ; i < 64 ; i++)
11874 tp->luntbl[i] = cpu_to_scr(NCB_PHYS(np, resel_badlun));
11875 tp->b_luntbl = cpu_to_scr(vtobus(tp->luntbl));
11876 }
11877
11878 /*
11879 ** Allocate the table of pointers for LUN(s) > 0, if needed.
11880 */
11881 if (ln && !tp->lmp) {
11882 tp->lmp = m_calloc(MAX_LUN * sizeof(lcb_p), "LMP");
11883 if (!tp->lmp)
11884 goto fail;
11885 }
11886
11887 /*
11888 ** Allocate the lcb.
11889 ** Make it available to the chip.
11890 */
11891 lp = m_calloc_dma(sizeof(struct lcb), "LCB");
11892 if (!lp)
11893 goto fail;
11894 if (ln) {
11895 tp->lmp[ln] = lp;
11896 tp->luntbl[ln] = cpu_to_scr(vtobus(lp));
11897 }
11898 else {
11899 tp->l0p = lp;
11900 tp->b_lun0 = cpu_to_scr(vtobus(lp));
11901 }
11902
11903 /*
11904 ** Initialize the CCB queue headers.
11905 */
11906 xpt_que_init(&lp->busy_ccbq);
11907 xpt_que_init(&lp->wait_ccbq);
11908
11909 /*
11910 ** Set max CCBs to 1 and use the default task array
11911 ** by default.
11912 */
11913 lp->maxnxs = 1;
11914 lp->tasktbl = &lp->tasktbl_0;
11915 lp->b_tasktbl = cpu_to_scr(vtobus(lp->tasktbl));
11916 lp->tasktbl[0] = cpu_to_scr(np->p_notask);
11917 lp->resel_task = cpu_to_scr(NCB_SCRIPT_PHYS(np, resel_notag));
11918
11919 /*
11920 ** Initialize command queuing control.
11921 */
11922 lp->busyccbs = 1;
11923 lp->queuedccbs = 1;
11924 lp->queuedepth = 1;
11925 fail:
11926 return lp;
11927 }
11928
11929
11930 /*------------------------------------------------------------------------
11931 ** Lun control block setup on INQUIRY data received.
11932 **------------------------------------------------------------------------
11933 ** We only support WIDE, SYNC for targets and CMDQ for logical units.
11934 ** This setup is done on each INQUIRY since we are expecting user
11935 ** will play with CHANGE DEFINITION commands. :-)
11936 **------------------------------------------------------------------------
11937 */
11938 static lcb_p ncr_setup_lcb (ncb_p np, u_char tn, u_char ln, u_char *inq_data)
11939 {
11940 tcb_p tp = &np->target[tn];
11941 lcb_p lp = ncr_lp(np, tp, ln);
11942 u_char inq_byte7;
11943 int i;
11944
11945 /*
11946 ** If no lcb, try to allocate it.
11947 */
11948 if (!lp && !(lp = ncr_alloc_lcb(np, tn, ln)))
11949 goto fail;
11950
11951 #if 0 /* No more used. Left here as provision */
11952 /*
11953 ** Get device quirks.
11954 */
11955 tp->quirks = 0;
11956 if (tp->quirks && bootverbose) {
11957 PRINT_LUN(np, tn, ln);
11958 printk ("quirks=%x.\n", tp->quirks);
11959 }
11960 #endif
11961
11962 /*
11963 ** Evaluate trustable target/unit capabilities.
11964 ** We only believe device version >= SCSI-2 that
11965 ** use appropriate response data format (2).
11966 ** But it seems that some CCS devices also
11967 ** support SYNC and I donnot want to frustrate
11968 ** anybody. ;-)
11969 */
11970 inq_byte7 = 0;
11971 if ((inq_data[2] & 0x7) >= 2 && (inq_data[3] & 0xf) == 2)
11972 inq_byte7 = inq_data[7];
11973 else if ((inq_data[2] & 0x7) == 1 && (inq_data[3] & 0xf) == 1)
11974 inq_byte7 = INQ7_SYNC;
11975
11976 /*
11977 ** Throw away announced LUN capabilities if we are told
11978 ** that there is no real device supported by the logical unit.
11979 */
11980 if ((inq_data[0] & 0xe0) > 0x20 || (inq_data[0] & 0x1f) == 0x1f)
11981 inq_byte7 &= (INQ7_SYNC | INQ7_WIDE16);
11982
11983 /*
11984 ** If user is wanting SYNC, force this feature.
11985 */
11986 if (driver_setup.force_sync_nego)
11987 inq_byte7 |= INQ7_SYNC;
11988
11989 /*
11990 ** Prepare negotiation if SIP capabilities have changed.
11991 */
11992 tp->inq_done = 1;
11993 if ((inq_byte7 ^ tp->inq_byte7) & (INQ7_SYNC | INQ7_WIDE16)) {
11994 tp->inq_byte7 = inq_byte7;
11995 ncr_negotiate(np, tp);
11996 }
11997
11998 /*
11999 ** If unit supports tagged commands, allocate and
12000 ** initialyze the task table if not yet.
12001 */
12002 if ((inq_byte7 & INQ7_QUEUE) && lp->tasktbl == &lp->tasktbl_0) {
12003 lp->tasktbl = m_calloc_dma(MAX_TASKS*4, "TASKTBL");
12004 if (!lp->tasktbl) {
12005 lp->tasktbl = &lp->tasktbl_0;
12006 goto fail;
12007 }
12008 lp->b_tasktbl = cpu_to_scr(vtobus(lp->tasktbl));
12009 for (i = 0 ; i < MAX_TASKS ; i++)
12010 lp->tasktbl[i] = cpu_to_scr(np->p_notask);
12011
12012 lp->cb_tags = m_calloc(MAX_TAGS, "CB_TAGS");
12013 if (!lp->cb_tags)
12014 goto fail;
12015 for (i = 0 ; i < MAX_TAGS ; i++)
12016 lp->cb_tags[i] = i;
12017
12018 lp->maxnxs = MAX_TAGS;
12019 lp->tags_stime = ktime_get(3*HZ);
12020 }
12021
12022 /*
12023 ** Adjust tagged queueing status if needed.
12024 */
12025 if ((inq_byte7 ^ lp->inq_byte7) & INQ7_QUEUE) {
12026 lp->inq_byte7 = inq_byte7;
12027 lp->numtags = lp->maxtags;
12028 ncr_setup_tags (np, tn, ln);
12029 }
12030
12031 fail:
12032 return lp;
12033 }
12034
12035 /*==========================================================
12036 **
12037 **
12038 ** Build Scatter Gather Block
12039 **
12040 **
12041 **==========================================================
12042 **
12043 ** The transfer area may be scattered among
12044 ** several non adjacent physical pages.
12045 **
12046 ** We may use MAX_SCATTER blocks.
12047 **
12048 **----------------------------------------------------------
12049 */
12050
12051 /*
12052 ** We try to reduce the number of interrupts caused
12053 ** by unexpected phase changes due to disconnects.
12054 ** A typical harddisk may disconnect before ANY block.
12055 ** If we wanted to avoid unexpected phase changes at all
12056 ** we had to use a break point every 512 bytes.
12057 ** Of course the number of scatter/gather blocks is
12058 ** limited.
12059 ** Under Linux, the scatter/gatter blocks are provided by
12060 ** the generic driver. We just have to copy addresses and
12061 ** sizes to the data segment array.
12062 */
12063
12064 /*
12065 ** For 64 bit systems, we use the 8 upper bits of the size field
12066 ** to provide bus address bits 32-39 to the SCRIPTS processor.
12067 ** This allows the 895A and 896 to address up to 1 TB of memory.
12068 ** For 32 bit chips on 64 bit systems, we must be provided with
12069 ** memory addresses that fit into the first 32 bit bus address
12070 ** range and so, this does not matter and we expect an error from
12071 ** the chip if this ever happen.
12072 **
12073 ** We use a separate function for the case Linux does not provide
12074 ** a scatter list in order to allow better code optimization
12075 ** for the case we have a scatter list (BTW, for now this just wastes
12076 ** about 40 bytes of code for x86, but my guess is that the scatter
12077 ** code will get more complex later).
12078 */
12079
12080 #define SCATTER_ONE(data, badd, len) \
12081 (data)->addr = cpu_to_scr(badd); \
12082 (data)->size = cpu_to_scr((((badd) >> 8) & 0xff000000) + len);
12083
12084 #define CROSS_16MB(p, n) (((((u_long) p) + n - 1) ^ ((u_long) p)) & ~0xffffff)
12085
12086 static int ncr_scatter_no_sglist(ncb_p np, ccb_p cp, Scsi_Cmnd *cmd)
12087 {
12088 struct scr_tblmove *data = &cp->phys.data[MAX_SCATTER-1];
12089 int segment;
12090
12091 cp->data_len = cmd->request_bufflen;
12092
12093 if (cmd->request_bufflen) {
12094 dma_addr_t baddr = map_scsi_single_data(np, cmd);
12095
12096 SCATTER_ONE(data, baddr, cmd->request_bufflen);
12097 if (CROSS_16MB(baddr, cmd->request_bufflen)) {
12098 cp->host_flags |= HF_PM_TO_C;
12099 #ifdef DEBUG_896R1
12100 printk("He! we are crossing a 16 MB boundary (0x%lx, 0x%x)\n",
12101 baddr, cmd->request_bufflen);
12102 #endif
12103 }
12104 segment = 1;
12105 }
12106 else
12107 segment = 0;
12108
12109 return segment;
12110 }
12111
12112 /*
12113 ** DEL 472 - 53C896 Rev 1 - Part Number 609-0393055 - ITEM 5.
12114 **
12115 ** We disable data phase mismatch handling from SCRIPTS for data
12116 ** transfers that contains scatter/gather entries that cross
12117 ** a 16 MB boundary.
12118 ** We use a different scatter function for 896 rev. 1 that needs
12119 ** such a work-around. Doing so, we do not affect performance for
12120 ** other chips.
12121 ** This problem should not be triggered for disk IOs under Linux,
12122 ** since such IOs are performed using pages and buffers that are
12123 ** nicely power-of-two sized and aligned. But, since this may change
12124 ** at any time, a work-around was required.
12125 */
12126 static int ncr_scatter_896R1(ncb_p np, ccb_p cp, Scsi_Cmnd *cmd)
12127 {
12128 int segn;
12129 int use_sg = (int) cmd->use_sg;
12130
12131 cp->data_len = 0;
12132
12133 if (!use_sg)
12134 segn = ncr_scatter_no_sglist(np, cp, cmd);
12135 else {
12136 struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
12137 struct scr_tblmove *data;
12138
12139 use_sg = map_scsi_sg_data(np, cmd);
12140 if (use_sg > MAX_SCATTER) {
12141 unmap_scsi_data(np, cmd);
12142 return -1;
12143 }
12144
12145 data = &cp->phys.data[MAX_SCATTER - use_sg];
12146
12147 for (segn = 0; segn < use_sg; segn++) {
12148 dma_addr_t baddr = scsi_sg_dma_address(&scatter[segn]);
12149 unsigned int len = scsi_sg_dma_len(&scatter[segn]);
12150
12151 SCATTER_ONE(&data[segn],
12152 baddr,
12153 len);
12154 if (CROSS_16MB(baddr, scatter[segn].length)) {
12155 cp->host_flags |= HF_PM_TO_C;
12156 #ifdef DEBUG_896R1
12157 printk("He! we are crossing a 16 MB boundary (0x%lx, 0x%x)\n",
12158 baddr, scatter[segn].length);
12159 #endif
12160 }
12161 cp->data_len += len;
12162 }
12163 }
12164
12165 return segn;
12166 }
12167
12168 static int ncr_scatter(ncb_p np, ccb_p cp, Scsi_Cmnd *cmd)
12169 {
12170 int segment;
12171 int use_sg = (int) cmd->use_sg;
12172
12173 cp->data_len = 0;
12174
12175 if (!use_sg)
12176 segment = ncr_scatter_no_sglist(np, cp, cmd);
12177 else {
12178 struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
12179 struct scr_tblmove *data;
12180
12181 use_sg = map_scsi_sg_data(np, cmd);
12182 if (use_sg > MAX_SCATTER) {
12183 unmap_scsi_data(np, cmd);
12184 return -1;
12185 }
12186 data = &cp->phys.data[MAX_SCATTER - use_sg];
12187
12188 for (segment = 0; segment < use_sg; segment++) {
12189 dma_addr_t baddr = scsi_sg_dma_address(&scatter[segment]);
12190 unsigned int len = scsi_sg_dma_len(&scatter[segment]);
12191
12192 SCATTER_ONE(&data[segment],
12193 baddr,
12194 len);
12195 cp->data_len += len;
12196 }
12197 }
12198
12199 return segment;
12200 }
12201
12202 /*==========================================================
12203 **
12204 **
12205 ** Test the pci bus snoop logic :-(
12206 **
12207 ** Has to be called with interrupts disabled.
12208 **
12209 **
12210 **==========================================================
12211 */
12212
12213 #ifndef SCSI_NCR_IOMAPPED
12214 static int __init ncr_regtest (struct ncb* np)
12215 {
12216 register volatile u_int32 data;
12217 /*
12218 ** ncr registers may NOT be cached.
12219 ** write 0xffffffff to a read only register area,
12220 ** and try to read it back.
12221 */
12222 data = 0xffffffff;
12223 OUTL_OFF(offsetof(struct ncr_reg, nc_dstat), data);
12224 data = INL_OFF(offsetof(struct ncr_reg, nc_dstat));
12225 #if 1
12226 if (data == 0xffffffff) {
12227 #else
12228 if ((data & 0xe2f0fffd) != 0x02000080) {
12229 #endif
12230 printk ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
12231 (unsigned) data);
12232 return (0x10);
12233 };
12234 return (0);
12235 }
12236 #endif
12237
12238 static int __init ncr_snooptest (struct ncb* np)
12239 {
12240 u_int32 ncr_rd, ncr_wr, ncr_bk, host_rd, host_wr, pc;
12241 u_char dstat;
12242 int i, err=0;
12243 #ifndef SCSI_NCR_IOMAPPED
12244 if (np->reg) {
12245 err |= ncr_regtest (np);
12246 if (err) return (err);
12247 }
12248 #endif
12249 restart_test:
12250 /*
12251 ** Enable Master Parity Checking as we intend
12252 ** to enable it for normal operations.
12253 */
12254 OUTB (nc_ctest4, (np->rv_ctest4 & MPEE));
12255 /*
12256 ** init
12257 */
12258 pc = NCB_SCRIPTH0_PHYS (np, snooptest);
12259 host_wr = 1;
12260 ncr_wr = 2;
12261 /*
12262 ** Set memory and register.
12263 */
12264 np->ncr_cache = cpu_to_scr(host_wr);
12265 OUTL (nc_temp, ncr_wr);
12266 /*
12267 ** Start script (exchange values)
12268 */
12269 OUTL (nc_dsa, np->p_ncb);
12270 OUTL_DSP (pc);
12271 /*
12272 ** Wait 'til done (with timeout)
12273 */
12274 for (i=0; i<NCR_SNOOP_TIMEOUT; i++)
12275 if (INB(nc_istat) & (INTF|SIP|DIP))
12276 break;
12277 if (i>=NCR_SNOOP_TIMEOUT) {
12278 printk ("CACHE TEST FAILED: timeout.\n");
12279 return (0x20);
12280 };
12281 /*
12282 ** Check for fatal DMA errors.
12283 */
12284 dstat = INB (nc_dstat);
12285 #if 1 /* Band aiding for broken hardwares that fail PCI parity */
12286 if ((dstat & MDPE) && (np->rv_ctest4 & MPEE)) {
12287 printk ("%s: PCI DATA PARITY ERROR DETECTED - "
12288 "DISABLING MASTER DATA PARITY CHECKING.\n",
12289 ncr_name(np));
12290 np->rv_ctest4 &= ~MPEE;
12291 goto restart_test;
12292 }
12293 #endif
12294 if (dstat & (MDPE|BF|IID)) {
12295 printk ("CACHE TEST FAILED: DMA error (dstat=0x%02x).", dstat);
12296 return (0x80);
12297 }
12298 /*
12299 ** Save termination position.
12300 */
12301 pc = INL (nc_dsp);
12302 /*
12303 ** Read memory and register.
12304 */
12305 host_rd = scr_to_cpu(np->ncr_cache);
12306 ncr_rd = INL (nc_scratcha);
12307 ncr_bk = INL (nc_temp);
12308 /*
12309 ** Check termination position.
12310 */
12311 if (pc != NCB_SCRIPTH0_PHYS (np, snoopend)+8) {
12312 printk ("CACHE TEST FAILED: script execution failed.\n");
12313 printk ("start=%08lx, pc=%08lx, end=%08lx\n",
12314 (u_long) NCB_SCRIPTH0_PHYS (np, snooptest), (u_long) pc,
12315 (u_long) NCB_SCRIPTH0_PHYS (np, snoopend) +8);
12316 return (0x40);
12317 };
12318 /*
12319 ** Show results.
12320 */
12321 if (host_wr != ncr_rd) {
12322 printk ("CACHE TEST FAILED: host wrote %d, ncr read %d.\n",
12323 (int) host_wr, (int) ncr_rd);
12324 err |= 1;
12325 };
12326 if (host_rd != ncr_wr) {
12327 printk ("CACHE TEST FAILED: ncr wrote %d, host read %d.\n",
12328 (int) ncr_wr, (int) host_rd);
12329 err |= 2;
12330 };
12331 if (ncr_bk != ncr_wr) {
12332 printk ("CACHE TEST FAILED: ncr wrote %d, read back %d.\n",
12333 (int) ncr_wr, (int) ncr_bk);
12334 err |= 4;
12335 };
12336 return (err);
12337 }
12338
12339 /*==========================================================
12340 **
12341 ** Determine the ncr's clock frequency.
12342 ** This is essential for the negotiation
12343 ** of the synchronous transfer rate.
12344 **
12345 **==========================================================
12346 **
12347 ** Note: we have to return the correct value.
12348 ** THERE IS NO SAFE DEFAULT VALUE.
12349 **
12350 ** Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
12351 ** 53C860 and 53C875 rev. 1 support fast20 transfers but
12352 ** do not have a clock doubler and so are provided with a
12353 ** 80 MHz clock. All other fast20 boards incorporate a doubler
12354 ** and so should be delivered with a 40 MHz clock.
12355 ** The recent fast40 chips (895/896/895A) and the
12356 ** fast80 chip (C1010) use a 40 Mhz base clock
12357 ** and provide a clock quadrupler (160 Mhz). The code below
12358 ** tries to deal as cleverly as possible with all this stuff.
12359 **
12360 **----------------------------------------------------------
12361 */
12362
12363 /*
12364 * Select NCR SCSI clock frequency
12365 */
12366 static void ncr_selectclock(ncb_p np, u_char scntl3)
12367 {
12368 if (np->multiplier < 2) {
12369 OUTB(nc_scntl3, scntl3);
12370 return;
12371 }
12372
12373 if (bootverbose >= 2)
12374 printk ("%s: enabling clock multiplier\n", ncr_name(np));
12375
12376 OUTB(nc_stest1, DBLEN); /* Enable clock multiplier */
12377
12378 if ( (np->device_id != PCI_DEVICE_ID_LSI_53C1010) &&
12379 (np->device_id != PCI_DEVICE_ID_LSI_53C1010_66) &&
12380 (np->multiplier > 2)) {
12381 int i = 20; /* Poll bit 5 of stest4 for quadrupler */
12382 while (!(INB(nc_stest4) & LCKFRQ) && --i > 0)
12383 UDELAY (20);
12384 if (!i)
12385 printk("%s: the chip cannot lock the frequency\n",
12386 ncr_name(np));
12387
12388 } else /* Wait 120 micro-seconds for multiplier*/
12389 UDELAY (120);
12390
12391 OUTB(nc_stest3, HSC); /* Halt the scsi clock */
12392 OUTB(nc_scntl3, scntl3);
12393 OUTB(nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
12394 OUTB(nc_stest3, 0x00); /* Restart scsi clock */
12395 }
12396
12397
12398 /*
12399 * calculate NCR SCSI clock frequency (in KHz)
12400 */
12401 static unsigned __init ncrgetfreq (ncb_p np, int gen)
12402 {
12403 unsigned int ms = 0;
12404 unsigned int f;
12405 int count;
12406
12407 /*
12408 * Measure GEN timer delay in order
12409 * to calculate SCSI clock frequency
12410 *
12411 * This code will never execute too
12412 * many loop iterations (if DELAY is
12413 * reasonably correct). It could get
12414 * too low a delay (too high a freq.)
12415 * if the CPU is slow executing the
12416 * loop for some reason (an NMI, for
12417 * example). For this reason we will
12418 * if multiple measurements are to be
12419 * performed trust the higher delay
12420 * (lower frequency returned).
12421 */
12422 OUTW (nc_sien , 0x0);/* mask all scsi interrupts */
12423 /* enable general purpose timer */
12424 (void) INW (nc_sist); /* clear pending scsi interrupt */
12425 OUTB (nc_dien , 0); /* mask all dma interrupts */
12426 (void) INW (nc_sist); /* another one, just to be sure :) */
12427 OUTB (nc_scntl3, 4); /* set pre-scaler to divide by 3 */
12428 OUTB (nc_stime1, 0); /* disable general purpose timer */
12429 OUTB (nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */
12430 /* Temporary fix for udelay issue with Alpha
12431 platform */
12432 while (!(INW(nc_sist) & GEN) && ms++ < 100000) {
12433 /* count 1ms */
12434 for (count = 0; count < 10; count++)
12435 UDELAY (100);
12436 }
12437 OUTB (nc_stime1, 0); /* disable general purpose timer */
12438 /*
12439 * set prescaler to divide by whatever 0 means
12440 * 0 ought to choose divide by 2, but appears
12441 * to set divide by 3.5 mode in my 53c810 ...
12442 */
12443 OUTB (nc_scntl3, 0);
12444
12445 /*
12446 * adjust for prescaler, and convert into KHz
12447 * scale values derived empirically.
12448 */
12449 f = ms ? ((1 << gen) * 4340) / ms : 0;
12450
12451 if (bootverbose >= 2)
12452 printk ("%s: Delay (GEN=%d): %u msec, %u KHz\n",
12453 ncr_name(np), gen, ms, f);
12454
12455 return f;
12456 }
12457
12458 static unsigned __init ncr_getfreq (ncb_p np)
12459 {
12460 u_int f1, f2;
12461 int gen = 11;
12462
12463 (void) ncrgetfreq (np, gen); /* throw away first result */
12464 f1 = ncrgetfreq (np, gen);
12465 f2 = ncrgetfreq (np, gen);
12466 if (f1 > f2) f1 = f2; /* trust lower result */
12467 return f1;
12468 }
12469
12470 /*
12471 * Get/probe NCR SCSI clock frequency
12472 */
12473 static void __init ncr_getclock (ncb_p np, int mult)
12474 {
12475 unsigned char scntl3 = np->sv_scntl3;
12476 unsigned char stest1 = np->sv_stest1;
12477 unsigned f1;
12478
12479 np->multiplier = 1;
12480 f1 = 40000;
12481
12482 /*
12483 ** True with 875/895/896/895A with clock multiplier selected
12484 */
12485 if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
12486 if (bootverbose >= 2)
12487 printk ("%s: clock multiplier found\n", ncr_name(np));
12488 np->multiplier = mult;
12489 }
12490
12491 /*
12492 ** If multiplier not found or scntl3 not 7,5,3,
12493 ** reset chip and get frequency from general purpose timer.
12494 ** Otherwise trust scntl3 BIOS setting.
12495 */
12496 if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
12497 OUTB (nc_stest1, 0); /* make sure doubler is OFF */
12498 f1 = ncr_getfreq (np);
12499
12500 if (bootverbose)
12501 printk ("%s: NCR clock is %uKHz\n", ncr_name(np), f1);
12502
12503 if (f1 < 55000) f1 = 40000;
12504 else f1 = 80000;
12505
12506 /*
12507 ** Suggest to also check the PCI clock frequency
12508 ** to make sure our frequency calculation algorithm
12509 ** is not too biased.
12510 */
12511 if (np->features & FE_66MHZ) {
12512 np->pciclock_min = (66000*55+80-1)/80;
12513 np->pciclock_max = (66000*55)/40;
12514 }
12515 else {
12516 np->pciclock_min = (33000*55+80-1)/80;
12517 np->pciclock_max = (33000*55)/40;
12518 }
12519
12520 if (f1 == 40000 && mult > 1) {
12521 if (bootverbose >= 2)
12522 printk ("%s: clock multiplier assumed\n", ncr_name(np));
12523 np->multiplier = mult;
12524 }
12525 } else {
12526 if ((scntl3 & 7) == 3) f1 = 40000;
12527 else if ((scntl3 & 7) == 5) f1 = 80000;
12528 else f1 = 160000;
12529
12530 f1 /= np->multiplier;
12531 }
12532
12533 /*
12534 ** Compute controller synchronous parameters.
12535 */
12536 f1 *= np->multiplier;
12537 np->clock_khz = f1;
12538 }
12539
12540 /*
12541 * Get/probe PCI clock frequency
12542 */
12543 static u_int __init ncr_getpciclock (ncb_p np)
12544 {
12545 static u_int f;
12546
12547 OUTB (nc_stest1, SCLK); /* Use the PCI clock as SCSI clock */
12548 f = ncr_getfreq (np);
12549 OUTB (nc_stest1, 0);
12550
12551 return f;
12552 }
12553
12554 /*===================== LINUX ENTRY POINTS SECTION ==========================*/
12555
12556 #ifndef uchar
12557 #define uchar unsigned char
12558 #endif
12559
12560 #ifndef ushort
12561 #define ushort unsigned short
12562 #endif
12563
12564 #ifndef ulong
12565 #define ulong unsigned long
12566 #endif
12567
12568 /* ---------------------------------------------------------------------
12569 **
12570 ** Driver setup from the boot command line
12571 **
12572 ** ---------------------------------------------------------------------
12573 */
12574
12575 #ifdef MODULE
12576 #define ARG_SEP ' '
12577 #else
12578 #define ARG_SEP ','
12579 #endif
12580
12581 #define OPT_TAGS 1
12582 #define OPT_MASTER_PARITY 2
12583 #define OPT_SCSI_PARITY 3
12584 #define OPT_DISCONNECTION 4
12585 #define OPT_SPECIAL_FEATURES 5
12586 #define OPT_RESERVED_1 6
12587 #define OPT_FORCE_SYNC_NEGO 7
12588 #define OPT_REVERSE_PROBE 8
12589 #define OPT_DEFAULT_SYNC 9
12590 #define OPT_VERBOSE 10
12591 #define OPT_DEBUG 11
12592 #define OPT_BURST_MAX 12
12593 #define OPT_LED_PIN 13
12594 #define OPT_MAX_WIDE 14
12595 #define OPT_SETTLE_DELAY 15
12596 #define OPT_DIFF_SUPPORT 16
12597 #define OPT_IRQM 17
12598 #define OPT_PCI_FIX_UP 18
12599 #define OPT_BUS_CHECK 19
12600 #define OPT_OPTIMIZE 20
12601 #define OPT_RECOVERY 21
12602 #define OPT_SAFE_SETUP 22
12603 #define OPT_USE_NVRAM 23
12604 #define OPT_EXCLUDE 24
12605 #define OPT_HOST_ID 25
12606
12607 #ifdef SCSI_NCR_IARB_SUPPORT
12608 #define OPT_IARB 26
12609 #endif
12610
12611 static char setup_token[] __initdata =
12612 "tags:" "mpar:"
12613 "spar:" "disc:"
12614 "specf:" "_rsvd1:"
12615 "fsn:" "revprob:"
12616 "sync:" "verb:"
12617 "debug:" "burst:"
12618 "led:" "wide:"
12619 "settle:" "diff:"
12620 "irqm:" "pcifix:"
12621 "buschk:" "optim:"
12622 "recovery:"
12623 "safe:" "nvram:"
12624 "excl:" "hostid:"
12625 #ifdef SCSI_NCR_IARB_SUPPORT
12626 "iarb:"
12627 #endif
12628 ; /* DONNOT REMOVE THIS ';' */
12629
12630 #ifdef MODULE
12631 #define ARG_SEP ' '
12632 #else
12633 #define ARG_SEP ','
12634 #endif
12635
12636 static int __init get_setup_token(char *p)
12637 {
12638 char *cur = setup_token;
12639 char *pc;
12640 int i = 0;
12641
12642 while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
12643 ++pc;
12644 ++i;
12645 if (!strncmp(p, cur, pc - cur))
12646 return i;
12647 cur = pc;
12648 }
12649 return 0;
12650 }
12651
12652
12653 int __init sym53c8xx_setup(char *str)
12654 {
12655 #ifdef SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT
12656 char *cur = str;
12657 char *pc, *pv;
12658 unsigned long val;
12659 int i, c;
12660 int xi = 0;
12661
12662 while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
12663 char *pe;
12664
12665 val = 0;
12666 pv = pc;
12667 c = *++pv;
12668
12669 if (c == 'n')
12670 val = 0;
12671 else if (c == 'y')
12672 val = 1;
12673 else
12674 val = (int) simple_strtoul(pv, &pe, 0);
12675
12676 switch (get_setup_token(cur)) {
12677 case OPT_TAGS:
12678 driver_setup.default_tags = val;
12679 if (pe && *pe == '/') {
12680 i = 0;
12681 while (*pe && *pe != ARG_SEP &&
12682 i < sizeof(driver_setup.tag_ctrl)-1) {
12683 driver_setup.tag_ctrl[i++] = *pe++;
12684 }
12685 driver_setup.tag_ctrl[i] = '\0';
12686 }
12687 break;
12688 case OPT_MASTER_PARITY:
12689 driver_setup.master_parity = val;
12690 break;
12691 case OPT_SCSI_PARITY:
12692 driver_setup.scsi_parity = val;
12693 break;
12694 case OPT_DISCONNECTION:
12695 driver_setup.disconnection = val;
12696 break;
12697 case OPT_SPECIAL_FEATURES:
12698 driver_setup.special_features = val;
12699 break;
12700 case OPT_FORCE_SYNC_NEGO:
12701 driver_setup.force_sync_nego = val;
12702 break;
12703 case OPT_REVERSE_PROBE:
12704 driver_setup.reverse_probe = val;
12705 break;
12706 case OPT_DEFAULT_SYNC:
12707 driver_setup.default_sync = val;
12708 break;
12709 case OPT_VERBOSE:
12710 driver_setup.verbose = val;
12711 break;
12712 case OPT_DEBUG:
12713 driver_setup.debug = val;
12714 break;
12715 case OPT_BURST_MAX:
12716 driver_setup.burst_max = val;
12717 break;
12718 case OPT_LED_PIN:
12719 driver_setup.led_pin = val;
12720 break;
12721 case OPT_MAX_WIDE:
12722 driver_setup.max_wide = val? 1:0;
12723 break;
12724 case OPT_SETTLE_DELAY:
12725 driver_setup.settle_delay = val;
12726 break;
12727 case OPT_DIFF_SUPPORT:
12728 driver_setup.diff_support = val;
12729 break;
12730 case OPT_IRQM:
12731 driver_setup.irqm = val;
12732 break;
12733 case OPT_PCI_FIX_UP:
12734 driver_setup.pci_fix_up = val;
12735 break;
12736 case OPT_BUS_CHECK:
12737 driver_setup.bus_check = val;
12738 break;
12739 case OPT_OPTIMIZE:
12740 driver_setup.optimize = val;
12741 break;
12742 case OPT_RECOVERY:
12743 driver_setup.recovery = val;
12744 break;
12745 case OPT_USE_NVRAM:
12746 driver_setup.use_nvram = val;
12747 break;
12748 case OPT_SAFE_SETUP:
12749 memcpy(&driver_setup, &driver_safe_setup,
12750 sizeof(driver_setup));
12751 break;
12752 case OPT_EXCLUDE:
12753 if (xi < SCSI_NCR_MAX_EXCLUDES)
12754 driver_setup.excludes[xi++] = val;
12755 break;
12756 case OPT_HOST_ID:
12757 driver_setup.host_id = val;
12758 break;
12759 #ifdef SCSI_NCR_IARB_SUPPORT
12760 case OPT_IARB:
12761 driver_setup.iarb = val;
12762 break;
12763 #endif
12764 default:
12765 printk("sym53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
12766 break;
12767 }
12768
12769 if ((cur = strchr(cur, ARG_SEP)) != NULL)
12770 ++cur;
12771 }
12772 #endif /* SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT */
12773 return 1;
12774 }
12775
12776 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,13)
12777 #ifndef MODULE
12778 __setup("sym53c8xx=", sym53c8xx_setup);
12779 #endif
12780 #endif
12781
12782 static int
12783 sym53c8xx_pci_init(Scsi_Host_Template *tpnt, pcidev_t pdev, ncr_device *device);
12784
12785 /*
12786 ** Linux entry point for SYM53C8XX devices detection routine.
12787 **
12788 ** Called by the middle-level scsi drivers at initialization time,
12789 ** or at module installation.
12790 **
12791 ** Read the PCI configuration and try to attach each
12792 ** detected NCR board.
12793 **
12794 ** If NVRAM is present, try to attach boards according to
12795 ** the used defined boot order.
12796 **
12797 ** Returns the number of boards successfully attached.
12798 */
12799
12800 static void __init ncr_print_driver_setup(void)
12801 {
12802 #define YesNo(y) y ? 'y' : 'n'
12803 printk (NAME53C8XX ": setup=disc:%c,specf:%d,tags:%d,sync:%d,"
12804 "burst:%d,wide:%c,diff:%d,revprob:%c,buschk:0x%x\n",
12805 YesNo(driver_setup.disconnection),
12806 driver_setup.special_features,
12807 driver_setup.default_tags,
12808 driver_setup.default_sync,
12809 driver_setup.burst_max,
12810 YesNo(driver_setup.max_wide),
12811 driver_setup.diff_support,
12812 YesNo(driver_setup.reverse_probe),
12813 driver_setup.bus_check);
12814
12815 printk (NAME53C8XX ": setup=mpar:%c,spar:%c,fsn=%c,verb:%d,debug:0x%x,"
12816 "led:%c,settle:%d,irqm:0x%x,nvram:0x%x,pcifix:0x%x\n",
12817 YesNo(driver_setup.master_parity),
12818 YesNo(driver_setup.scsi_parity),
12819 YesNo(driver_setup.force_sync_nego),
12820 driver_setup.verbose,
12821 driver_setup.debug,
12822 YesNo(driver_setup.led_pin),
12823 driver_setup.settle_delay,
12824 driver_setup.irqm,
12825 driver_setup.use_nvram,
12826 driver_setup.pci_fix_up);
12827 #undef YesNo
12828 }
12829
12830 /*===================================================================
12831 ** SYM53C8XX devices description table and chip ids list.
12832 **===================================================================
12833 */
12834
12835 static ncr_chip ncr_chip_table[] __initdata = SCSI_NCR_CHIP_TABLE;
12836 static ushort ncr_chip_ids[] __initdata = SCSI_NCR_CHIP_IDS;
12837
12838 #ifdef SCSI_NCR_PQS_PDS_SUPPORT
12839 /*===================================================================
12840 ** Detect all NCR PQS/PDS boards and keep track of their bus nr.
12841 **
12842 ** The NCR PQS or PDS card is constructed as a DEC bridge
12843 ** behind which sit a proprietary NCR memory controller and
12844 ** four or two 53c875s as separate devices. In its usual mode
12845 ** of operation, the 875s are slaved to the memory controller
12846 ** for all transfers. We can tell if an 875 is part of a
12847 ** PQS/PDS or not since if it is, it will be on the same bus
12848 ** as the memory controller. To operate with the Linux
12849 ** driver, the memory controller is disabled and the 875s
12850 ** freed to function independently. The only wrinkle is that
12851 ** the preset SCSI ID (which may be zero) must be read in from
12852 ** a special configuration space register of the 875
12853 **===================================================================
12854 */
12855 #define SCSI_NCR_MAX_PQS_BUS 16
12856 static int pqs_bus[SCSI_NCR_MAX_PQS_BUS] __initdata = { 0 };
12857
12858 static void __init ncr_detect_pqs_pds(void)
12859 {
12860 short index;
12861 pcidev_t dev = PCIDEV_NULL;
12862
12863 for(index=0; index < SCSI_NCR_MAX_PQS_BUS; index++) {
12864 u_char tmp;
12865
12866 dev = pci_find_device(0x101a, 0x0009, dev);
12867 if (dev == PCIDEV_NULL) {
12868 pqs_bus[index] = -1;
12869 break;
12870 }
12871 printk(KERN_INFO NAME53C8XX ": NCR PQS/PDS memory controller detected on bus %d\n", PciBusNumber(dev));
12872 pci_read_config_byte(dev, 0x44, &tmp);
12873 /* bit 1: allow individual 875 configuration */
12874 tmp |= 0x2;
12875 pci_write_config_byte(dev, 0x44, tmp);
12876 pci_read_config_byte(dev, 0x45, &tmp);
12877 /* bit 2: drive individual 875 interrupts to the bus */
12878 tmp |= 0x4;
12879 pci_write_config_byte(dev, 0x45, tmp);
12880
12881 pqs_bus[index] = PciBusNumber(dev);
12882 }
12883 }
12884 #endif /* SCSI_NCR_PQS_PDS_SUPPORT */
12885
12886 /*===================================================================
12887 ** Detect all 53c8xx hosts and then attach them.
12888 **
12889 ** If we are using NVRAM, once all hosts are detected, we need to
12890 ** check any NVRAM for boot order in case detect and boot order
12891 ** differ and attach them using the order in the NVRAM.
12892 **
12893 ** If no NVRAM is found or data appears invalid attach boards in
12894 ** the order they are detected.
12895 **===================================================================
12896 */
12897 int __init sym53c8xx_detect(Scsi_Host_Template *tpnt)
12898 {
12899 pcidev_t pcidev;
12900 int i, j, chips, hosts, count;
12901 int attach_count = 0;
12902 ncr_device *devtbl, *devp;
12903 #ifdef SCSI_NCR_NVRAM_SUPPORT
12904 ncr_nvram nvram0, nvram, *nvp;
12905 #endif
12906
12907 /*
12908 ** PCI is required.
12909 */
12910 if (!pci_present())
12911 return 0;
12912
12913 /*
12914 ** Initialize driver general stuff.
12915 */
12916 #ifdef SCSI_NCR_PROC_INFO_SUPPORT
12917 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,27)
12918 tpnt->proc_dir = &proc_scsi_sym53c8xx;
12919 #else
12920 tpnt->proc_name = NAME53C8XX;
12921 #endif
12922 tpnt->proc_info = sym53c8xx_proc_info;
12923 #endif
12924
12925 #if defined(SCSI_NCR_BOOT_COMMAND_LINE_SUPPORT) && defined(MODULE)
12926 if (sym53c8xx)
12927 sym53c8xx_setup(sym53c8xx);
12928 #endif
12929 #ifdef SCSI_NCR_DEBUG_INFO_SUPPORT
12930 ncr_debug = driver_setup.debug;
12931 #endif
12932
12933 if (initverbose >= 2)
12934 ncr_print_driver_setup();
12935
12936 /*
12937 ** Allocate the device table since we donnot want to
12938 ** overflow the kernel stack.
12939 ** 1 x 4K PAGE is enough for more than 40 devices for i386.
12940 */
12941 devtbl = m_calloc(PAGE_SIZE, "devtbl");
12942 if (!devtbl)
12943 return 0;
12944
12945 /*
12946 ** Detect all NCR PQS/PDS memory controllers.
12947 */
12948 #ifdef SCSI_NCR_PQS_PDS_SUPPORT
12949 ncr_detect_pqs_pds();
12950 #endif
12951
12952 /*
12953 ** Detect all 53c8xx hosts.
12954 ** Save the first Symbios NVRAM content if any
12955 ** for the boot order.
12956 */
12957 chips = sizeof(ncr_chip_ids) / sizeof(ncr_chip_ids[0]);
12958 hosts = PAGE_SIZE / sizeof(*devtbl);
12959 #ifdef SCSI_NCR_NVRAM_SUPPORT
12960 nvp = (driver_setup.use_nvram & 0x1) ? &nvram0 : 0;
12961 #endif
12962 j = 0;
12963 count = 0;
12964 pcidev = PCIDEV_NULL;
12965 while (1) {
12966 char *msg = "";
12967 if (count >= hosts)
12968 break;
12969 if (j >= chips)
12970 break;
12971 i = driver_setup.reverse_probe ? chips - 1 - j : j;
12972 pcidev = pci_find_device(PCI_VENDOR_ID_NCR, ncr_chip_ids[i],
12973 pcidev);
12974 if (pcidev == PCIDEV_NULL) {
12975 ++j;
12976 continue;
12977 }
12978 if (pci_enable_device(pcidev)) /* @!*!$&*!%-*#;! */
12979 continue;
12980 /* Some HW as the HP LH4 may report twice PCI devices */
12981 for (i = 0; i < count ; i++) {
12982 if (devtbl[i].slot.bus == PciBusNumber(pcidev) &&
12983 devtbl[i].slot.device_fn == PciDeviceFn(pcidev))
12984 break;
12985 }
12986 if (i != count) /* Ignore this device if we already have it */
12987 continue;
12988 pci_set_master(pcidev);
12989 devp = &devtbl[count];
12990 devp->host_id = driver_setup.host_id;
12991 devp->attach_done = 0;
12992 if (sym53c8xx_pci_init(tpnt, pcidev, devp)) {
12993 continue;
12994 }
12995 ++count;
12996 #ifdef SCSI_NCR_NVRAM_SUPPORT
12997 if (nvp) {
12998 ncr_get_nvram(devp, nvp);
12999 switch(nvp->type) {
13000 case SCSI_NCR_SYMBIOS_NVRAM:
13001 /*
13002 * Switch to the other nvram buffer, so that
13003 * nvram0 will contain the first Symbios
13004 * format NVRAM content with boot order.
13005 */
13006 nvp = &nvram;
13007 msg = "with Symbios NVRAM";
13008 break;
13009 case SCSI_NCR_TEKRAM_NVRAM:
13010 msg = "with Tekram NVRAM";
13011 break;
13012 }
13013 }
13014 #endif
13015 #ifdef SCSI_NCR_PQS_PDS_SUPPORT
13016 if (devp->pqs_pds)
13017 msg = "(NCR PQS/PDS)";
13018 #endif
13019 printk(KERN_INFO NAME53C8XX ": 53c%s detected %s\n",
13020 devp->chip.name, msg);
13021 }
13022
13023 /*
13024 ** If we have found a SYMBIOS NVRAM, use first the NVRAM boot
13025 ** sequence as device boot order.
13026 ** check devices in the boot record against devices detected.
13027 ** attach devices if we find a match. boot table records that
13028 ** do not match any detected devices will be ignored.
13029 ** devices that do not match any boot table will not be attached
13030 ** here but will attempt to be attached during the device table
13031 ** rescan.
13032 */
13033 #ifdef SCSI_NCR_NVRAM_SUPPORT
13034 if (!nvp || nvram0.type != SCSI_NCR_SYMBIOS_NVRAM)
13035 goto next;
13036 for (i = 0; i < 4; i++) {
13037 Symbios_host *h = &nvram0.data.Symbios.host[i];
13038 for (j = 0 ; j < count ; j++) {
13039 devp = &devtbl[j];
13040 if (h->device_fn != devp->slot.device_fn ||
13041 h->bus_nr != devp->slot.bus ||
13042 h->device_id != devp->chip.device_id)
13043 continue;
13044 if (devp->attach_done)
13045 continue;
13046 if (h->flags & SYMBIOS_INIT_SCAN_AT_BOOT) {
13047 ncr_get_nvram(devp, nvp);
13048 if (!ncr_attach (tpnt, attach_count, devp))
13049 attach_count++;
13050 }
13051 else if (!(driver_setup.use_nvram & 0x80))
13052 printk(KERN_INFO NAME53C8XX
13053 ": 53c%s state OFF thus not attached\n",
13054 devp->chip.name);
13055 else
13056 continue;
13057
13058 devp->attach_done = 1;
13059 break;
13060 }
13061 }
13062 next:
13063 #endif
13064
13065 /*
13066 ** Rescan device list to make sure all boards attached.
13067 ** Devices without boot records will not be attached yet
13068 ** so try to attach them here.
13069 */
13070 for (i= 0; i < count; i++) {
13071 devp = &devtbl[i];
13072 if (!devp->attach_done) {
13073 #ifdef SCSI_NCR_NVRAM_SUPPORT
13074 ncr_get_nvram(devp, nvp);
13075 #endif
13076 if (!ncr_attach (tpnt, attach_count, devp))
13077 attach_count++;
13078 }
13079 }
13080
13081 m_free(devtbl, PAGE_SIZE, "devtbl");
13082
13083 return attach_count;
13084 }
13085
13086 /*===================================================================
13087 ** Read and check the PCI configuration for any detected NCR
13088 ** boards and save data for attaching after all boards have
13089 ** been detected.
13090 **===================================================================
13091 */
13092 static int __init
13093 sym53c8xx_pci_init(Scsi_Host_Template *tpnt, pcidev_t pdev, ncr_device *device)
13094 {
13095 u_short vendor_id, device_id, command, status_reg;
13096 u_char cache_line_size, latency_timer;
13097 u_char suggested_cache_line_size = 0;
13098 u_char pci_fix_up = driver_setup.pci_fix_up;
13099 u_char revision;
13100 u_int irq;
13101 u_long base, base_c, base_2, base_2_c, io_port;
13102 int i;
13103 ncr_chip *chip;
13104
13105 printk(KERN_INFO NAME53C8XX ": at PCI bus %d, device %d, function %d\n",
13106 PciBusNumber(pdev),
13107 (int) (PciDeviceFn(pdev) & 0xf8) >> 3,
13108 (int) (PciDeviceFn(pdev) & 7));
13109
13110 /*
13111 ** Read info from the PCI config space.
13112 ** pci_read_config_xxx() functions are assumed to be used for
13113 ** successfully detected PCI devices.
13114 */
13115 vendor_id = PciVendorId(pdev);
13116 device_id = PciDeviceId(pdev);
13117 irq = PciIrqLine(pdev);
13118
13119 i = pci_get_base_address(pdev, 0, &io_port);
13120 io_port = pci_get_base_cookie(pdev, 0);
13121
13122 base_c = pci_get_base_cookie(pdev, i);
13123 i = pci_get_base_address(pdev, i, &base);
13124
13125 base_2_c = pci_get_base_cookie(pdev, i);
13126 (void) pci_get_base_address(pdev, i, &base_2);
13127
13128 pci_read_config_word(pdev, PCI_COMMAND, &command);
13129 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
13130 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &cache_line_size);
13131 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency_timer);
13132 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
13133
13134 #ifdef SCSI_NCR_PQS_PDS_SUPPORT
13135 /*
13136 ** Match the BUS number for PQS/PDS devices.
13137 ** Read the SCSI ID from a special register mapped
13138 ** into the configuration space of the individual
13139 ** 875s. This register is set up by the PQS bios
13140 */
13141 for(i = 0; i < SCSI_NCR_MAX_PQS_BUS && pqs_bus[i] != -1; i++) {
13142 u_char tmp;
13143 if (pqs_bus[i] == PciBusNumber(pdev)) {
13144 pci_read_config_byte(pdev, 0x84, &tmp);
13145 device->pqs_pds = 1;
13146 device->host_id = tmp;
13147 break;
13148 }
13149 }
13150 #endif /* SCSI_NCR_PQS_PDS_SUPPORT */
13151
13152 /*
13153 ** If user excludes this chip, donnot initialize it.
13154 */
13155 for (i = 0 ; i < SCSI_NCR_MAX_EXCLUDES ; i++) {
13156 if (driver_setup.excludes[i] ==
13157 (io_port & PCI_BASE_ADDRESS_IO_MASK))
13158 return -1;
13159 }
13160 /*
13161 ** Check if the chip is supported
13162 */
13163 chip = 0;
13164 for (i = 0; i < sizeof(ncr_chip_table)/sizeof(ncr_chip_table[0]); i++) {
13165 if (device_id != ncr_chip_table[i].device_id)
13166 continue;
13167 if (revision > ncr_chip_table[i].revision_id)
13168 continue;
13169 if (!(ncr_chip_table[i].features & FE_LDSTR))
13170 break;
13171 chip = &device->chip;
13172 memcpy(chip, &ncr_chip_table[i], sizeof(*chip));
13173 chip->revision_id = revision;
13174 break;
13175 }
13176
13177 #ifdef SCSI_NCR_DYNAMIC_DMA_MAPPING
13178 /* Configure DMA attributes. For DAC capable boards, we can encode
13179 ** 32+8 bits for SCSI DMA data addresses with the extra bits used
13180 ** in the size field. We use normal 32-bit PCI addresses for
13181 ** descriptors.
13182 */
13183 if (chip && (chip->features & FE_DAC)) {
13184 if (pci_set_dma_mask(pdev, (u64) 0xffffffffff))
13185 chip->features &= ~FE_DAC_IN_USE;
13186 else
13187 chip->features |= FE_DAC_IN_USE;
13188 }
13189
13190 if (chip && !(chip->features & FE_DAC_IN_USE)) {
13191 if (pci_set_dma_mask(pdev, (u64) 0xffffffff)) {
13192 printk(KERN_WARNING NAME53C8XX
13193 "32 BIT PCI BUS DMA ADDRESSING NOT SUPPORTED\n");
13194 return -1;
13195 }
13196 }
13197 #endif
13198
13199 /*
13200 ** Ignore Symbios chips controlled by SISL RAID controller.
13201 ** This controller sets value 0x52414944 at RAM end - 16.
13202 */
13203 #if defined(__i386__) && !defined(SCSI_NCR_PCI_MEM_NOT_SUPPORTED)
13204 if (chip && (base_2_c & PCI_BASE_ADDRESS_MEM_MASK)) {
13205 unsigned int ram_size, ram_val;
13206 u_long ram_ptr;
13207
13208 if (chip->features & FE_RAM8K)
13209 ram_size = 8192;
13210 else
13211 ram_size = 4096;
13212
13213 ram_ptr = remap_pci_mem(base_2_c & PCI_BASE_ADDRESS_MEM_MASK,
13214 ram_size);
13215 if (ram_ptr) {
13216 ram_val = readl_raw(ram_ptr + ram_size - 16);
13217 unmap_pci_mem(ram_ptr, ram_size);
13218 if (ram_val == 0x52414944) {
13219 printk(NAME53C8XX": not initializing, "
13220 "driven by SISL RAID controller.\n");
13221 return -1;
13222 }
13223 }
13224 }
13225 #endif /* i386 and PCI MEMORY accessible */
13226
13227 if (!chip) {
13228 printk(NAME53C8XX ": not initializing, device not supported\n");
13229 return -1;
13230 }
13231
13232 #if defined(__powerpc__) || defined(__hppa__)
13233 /*
13234 ** Fix-up for power/pc and hppa.
13235 ** Should not be performed by the driver.
13236 */
13237 if ((command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
13238 != (PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) {
13239 printk(NAME53C8XX ": setting%s%s...\n",
13240 (command & PCI_COMMAND_IO) ? "" : " PCI_COMMAND_IO",
13241 (command & PCI_COMMAND_MEMORY) ? "" : " PCI_COMMAND_MEMORY");
13242 command |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
13243 pci_write_config_word(pdev, PCI_COMMAND, command);
13244 }
13245
13246 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,2,0)
13247 if ( is_prep ) {
13248 if (io_port >= 0x10000000) {
13249 printk(NAME53C8XX ": reallocating io_port (Wacky IBM)");
13250 io_port = (io_port & 0x00FFFFFF) | 0x01000000;
13251 pci_write_config_dword(pdev,
13252 PCI_BASE_ADDRESS_0, io_port);
13253 }
13254 if (base >= 0x10000000) {
13255 printk(NAME53C8XX ": reallocating base (Wacky IBM)");
13256 base = (base & 0x00FFFFFF) | 0x01000000;
13257 pci_write_config_dword(pdev,
13258 PCI_BASE_ADDRESS_1, base);
13259 }
13260 if (base_2 >= 0x10000000) {
13261 printk(NAME53C8XX ": reallocating base2 (Wacky IBM)");
13262 base_2 = (base_2 & 0x00FFFFFF) | 0x01000000;
13263 pci_write_config_dword(pdev,
13264 PCI_BASE_ADDRESS_2, base_2);
13265 }
13266 }
13267 #endif
13268 #endif /* __powerpc__ */
13269
13270 #if defined(__i386__) && !defined(MODULE)
13271 if (!cache_line_size) {
13272 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,75)
13273 extern char x86;
13274 switch(x86) {
13275 #else
13276 switch(boot_cpu_data.x86) {
13277 #endif
13278 case 4: suggested_cache_line_size = 4; break;
13279 case 6:
13280 case 5: suggested_cache_line_size = 8; break;
13281 }
13282 }
13283 #endif /* __i386__ */
13284
13285 /*
13286 ** Check availability of IO space, memory space.
13287 ** Enable master capability if not yet.
13288 **
13289 ** We shouldn't have to care about the IO region when
13290 ** we are using MMIO. But calling check_region() from
13291 ** both the ncr53c8xx and the sym53c8xx drivers prevents
13292 ** from attaching devices from the both drivers.
13293 ** If you have a better idea, let me know.
13294 */
13295 /* #ifdef SCSI_NCR_IOMAPPED */
13296 #if 1
13297 if (!(command & PCI_COMMAND_IO)) {
13298 printk(NAME53C8XX ": I/O base address (0x%lx) disabled.\n",
13299 (long) io_port);
13300 io_port = 0;
13301 }
13302 #endif
13303 if (!(command & PCI_COMMAND_MEMORY)) {
13304 printk(NAME53C8XX ": PCI_COMMAND_MEMORY not set.\n");
13305 base = 0;
13306 base_2 = 0;
13307 }
13308 io_port &= PCI_BASE_ADDRESS_IO_MASK;
13309 base &= PCI_BASE_ADDRESS_MEM_MASK;
13310 base_2 &= PCI_BASE_ADDRESS_MEM_MASK;
13311
13312 /* #ifdef SCSI_NCR_IOMAPPED */
13313 #if 1
13314 if (io_port && check_region (io_port, 128)) {
13315 printk(NAME53C8XX ": IO region 0x%lx[0..127] is in use\n",
13316 (long) io_port);
13317 io_port = 0;
13318 }
13319 if (!io_port)
13320 return -1;
13321 #endif
13322 #ifndef SCSI_NCR_IOMAPPED
13323 if (!base) {
13324 printk(NAME53C8XX ": MMIO base address disabled.\n");
13325 return -1;
13326 }
13327 #endif
13328
13329 /*
13330 ** Set MASTER capable and PARITY bit, if not yet.
13331 */
13332 if ((command & (PCI_COMMAND_MASTER | PCI_COMMAND_PARITY))
13333 != (PCI_COMMAND_MASTER | PCI_COMMAND_PARITY)) {
13334 printk(NAME53C8XX ": setting%s%s...(fix-up)\n",
13335 (command & PCI_COMMAND_MASTER) ? "" : " PCI_COMMAND_MASTER",
13336 (command & PCI_COMMAND_PARITY) ? "" : " PCI_COMMAND_PARITY");
13337 command |= (PCI_COMMAND_MASTER | PCI_COMMAND_PARITY);
13338 pci_write_config_word(pdev, PCI_COMMAND, command);
13339 }
13340
13341 /*
13342 ** Fix some features according to driver setup.
13343 */
13344 if (!(driver_setup.special_features & 1))
13345 chip->features &= ~FE_SPECIAL_SET;
13346 else {
13347 if (driver_setup.special_features & 2)
13348 chip->features &= ~FE_WRIE;
13349 if (driver_setup.special_features & 4)
13350 chip->features &= ~FE_NOPM;
13351 }
13352
13353 /*
13354 ** Work around for errant bit in 895A. The 66Mhz
13355 ** capable bit is set erroneously. Clear this bit.
13356 ** (Item 1 DEL 533)
13357 **
13358 ** Make sure Config space and Features agree.
13359 **
13360 ** Recall: writes are not normal to status register -
13361 ** write a 1 to clear and a 0 to leave unchanged.
13362 ** Can only reset bits.
13363 */
13364 if (chip->features & FE_66MHZ) {
13365 if (!(status_reg & PCI_STATUS_66MHZ))
13366 chip->features &= ~FE_66MHZ;
13367 }
13368 else {
13369 if (status_reg & PCI_STATUS_66MHZ) {
13370 status_reg = PCI_STATUS_66MHZ;
13371 pci_write_config_word(pdev, PCI_STATUS, status_reg);
13372 pci_read_config_word(pdev, PCI_STATUS, &status_reg);
13373 }
13374 }
13375
13376 /*
13377 ** Some features are required to be enabled in order to
13378 ** work around some chip problems. :) ;)
13379 ** (ITEM 12 of a DEL about the 896 I haven't yet).
13380 ** We must ensure the chip will use WRITE AND INVALIDATE.
13381 ** The revision number limit is for now arbitrary.
13382 */
13383 if (device_id == PCI_DEVICE_ID_NCR_53C896 && revision <= 0x10) {
13384 chip->features |= (FE_WRIE | FE_CLSE);
13385 pci_fix_up |= 3; /* Force appropriate PCI fix-up */
13386 }
13387
13388 #ifdef SCSI_NCR_PCI_FIX_UP_SUPPORT
13389 /*
13390 ** Try to fix up PCI config according to wished features.
13391 */
13392 if ((pci_fix_up & 1) && (chip->features & FE_CLSE) &&
13393 !cache_line_size && suggested_cache_line_size) {
13394 cache_line_size = suggested_cache_line_size;
13395 pci_write_config_byte(pdev,
13396 PCI_CACHE_LINE_SIZE, cache_line_size);
13397 printk(NAME53C8XX ": PCI_CACHE_LINE_SIZE set to %d (fix-up).\n",
13398 cache_line_size);
13399 }
13400
13401 if ((pci_fix_up & 2) && cache_line_size &&
13402 (chip->features & FE_WRIE) && !(command & PCI_COMMAND_INVALIDATE)) {
13403 printk(NAME53C8XX": setting PCI_COMMAND_INVALIDATE (fix-up)\n");
13404 command |= PCI_COMMAND_INVALIDATE;
13405 pci_write_config_word(pdev, PCI_COMMAND, command);
13406 }
13407
13408 /*
13409 ** Tune PCI LATENCY TIMER according to burst max length transfer.
13410 ** (latency timer >= burst length + 6, we add 10 to be quite sure)
13411 */
13412
13413 if (chip->burst_max && (latency_timer == 0 || (pci_fix_up & 4))) {
13414 uchar lt = (1 << chip->burst_max) + 6 + 10;
13415 if (latency_timer < lt) {
13416 printk(NAME53C8XX
13417 ": changing PCI_LATENCY_TIMER from %d to %d.\n",
13418 (int) latency_timer, (int) lt);
13419 latency_timer = lt;
13420 pci_write_config_byte(pdev,
13421 PCI_LATENCY_TIMER, latency_timer);
13422 }
13423 }
13424
13425 #endif /* SCSI_NCR_PCI_FIX_UP_SUPPORT */
13426
13427 /*
13428 ** Initialise ncr_device structure with items required by ncr_attach.
13429 */
13430 device->pdev = pdev;
13431 device->slot.bus = PciBusNumber(pdev);
13432 device->slot.device_fn = PciDeviceFn(pdev);
13433 device->slot.base = base;
13434 device->slot.base_2 = base_2;
13435 device->slot.base_c = base_c;
13436 device->slot.base_2_c = base_2_c;
13437 device->slot.io_port = io_port;
13438 device->slot.irq = irq;
13439 device->attach_done = 0;
13440
13441 return 0;
13442 }
13443
13444
13445 /*===================================================================
13446 ** Detect and try to read SYMBIOS and TEKRAM NVRAM.
13447 **
13448 ** Data can be used to order booting of boards.
13449 **
13450 ** Data is saved in ncr_device structure if NVRAM found. This
13451 ** is then used to find drive boot order for ncr_attach().
13452 **
13453 ** NVRAM data is passed to Scsi_Host_Template later during
13454 ** ncr_attach() for any device set up.
13455 *===================================================================
13456 */
13457 #ifdef SCSI_NCR_NVRAM_SUPPORT
13458 static void __init ncr_get_nvram(ncr_device *devp, ncr_nvram *nvp)
13459 {
13460 devp->nvram = nvp;
13461 if (!nvp)
13462 return;
13463 /*
13464 ** Get access to chip IO registers
13465 */
13466 #ifdef SCSI_NCR_IOMAPPED
13467 request_region(devp->slot.io_port, 128, NAME53C8XX);
13468 devp->slot.base_io = devp->slot.io_port;
13469 #else
13470 devp->slot.reg =
13471 (struct ncr_reg *) remap_pci_mem(devp->slot.base_c, 128);
13472 if (!devp->slot.reg)
13473 return;
13474 #endif
13475
13476 /*
13477 ** Try to read SYMBIOS nvram.
13478 ** Try to read TEKRAM nvram if Symbios nvram not found.
13479 */
13480 if (!sym_read_Symbios_nvram(&devp->slot, &nvp->data.Symbios))
13481 nvp->type = SCSI_NCR_SYMBIOS_NVRAM;
13482 else if (!sym_read_Tekram_nvram(&devp->slot, devp->chip.device_id,
13483 &nvp->data.Tekram))
13484 nvp->type = SCSI_NCR_TEKRAM_NVRAM;
13485 else {
13486 nvp->type = 0;
13487 devp->nvram = 0;
13488 }
13489
13490 /*
13491 ** Release access to chip IO registers
13492 */
13493 #ifdef SCSI_NCR_IOMAPPED
13494 release_region(devp->slot.base_io, 128);
13495 #else
13496 unmap_pci_mem((u_long) devp->slot.reg, 128ul);
13497 #endif
13498
13499 }
13500 #endif /* SCSI_NCR_NVRAM_SUPPORT */
13501
13502 /*
13503 ** Linux select queue depths function
13504 */
13505
13506 #define DEF_DEPTH (driver_setup.default_tags)
13507 #define ALL_TARGETS -2
13508 #define NO_TARGET -1
13509 #define ALL_LUNS -2
13510 #define NO_LUN -1
13511
13512 static int device_queue_depth(ncb_p np, int target, int lun)
13513 {
13514 int c, h, t, u, v;
13515 char *p = driver_setup.tag_ctrl;
13516 char *ep;
13517
13518 h = -1;
13519 t = NO_TARGET;
13520 u = NO_LUN;
13521 while ((c = *p++) != 0) {
13522 v = simple_strtoul(p, &ep, 0);
13523 switch(c) {
13524 case '/':
13525 ++h;
13526 t = ALL_TARGETS;
13527 u = ALL_LUNS;
13528 break;
13529 case 't':
13530 if (t != target)
13531 t = (target == v) ? v : NO_TARGET;
13532 u = ALL_LUNS;
13533 break;
13534 case 'u':
13535 if (u != lun)
13536 u = (lun == v) ? v : NO_LUN;
13537 break;
13538 case 'q':
13539 if (h == np->unit &&
13540 (t == ALL_TARGETS || t == target) &&
13541 (u == ALL_LUNS || u == lun))
13542 return v;
13543 break;
13544 case '-':
13545 t = ALL_TARGETS;
13546 u = ALL_LUNS;
13547 break;
13548 default:
13549 break;
13550 }
13551 p = ep;
13552 }
13553 return DEF_DEPTH;
13554 }
13555
13556 int sym53c8xx_slave_configure(Scsi_Device *device)
13557 {
13558 struct Scsi_Host *host = device->host;
13559 ncb_p np;
13560 tcb_p tp;
13561 lcb_p lp;
13562 int numtags, depth_to_use;
13563
13564 np = ((struct host_data *) host->hostdata)->ncb;
13565 tp = &np->target[device->id];
13566 lp = ncr_lp(np, tp, device->lun);
13567
13568 /*
13569 ** Select queue depth from driver setup.
13570 ** Donnot use more than configured by user.
13571 ** Use at least 2.
13572 ** Donnot use more than our maximum.
13573 */
13574 numtags = device_queue_depth(np, device->id, device->lun);
13575 if (numtags > tp->usrtags)
13576 numtags = tp->usrtags;
13577 if (!device->tagged_supported)
13578 numtags = 1;
13579 depth_to_use = numtags;
13580 if (depth_to_use < 2)
13581 depth_to_use = 2;
13582 if (depth_to_use > MAX_TAGS)
13583 depth_to_use = MAX_TAGS;
13584
13585 scsi_adjust_queue_depth(device,
13586 (device->tagged_supported ?
13587 MSG_SIMPLE_TAG : 0),
13588 depth_to_use);
13589
13590 /*
13591 ** Since the queue depth is not tunable under Linux,
13592 ** we need to know this value in order not to
13593 ** announce stupid things to user.
13594 */
13595 if (lp) {
13596 lp->numtags = lp->maxtags = numtags;
13597 lp->scdev_depth = depth_to_use;
13598 }
13599 ncr_setup_tags (np, device->id, device->lun);
13600
13601 #ifdef DEBUG_SYM53C8XX
13602 printk("sym53c8xx_select_queue_depth: host=%d, id=%d, lun=%d, depth=%d\n",
13603 np->unit, device->id, device->lun, depth_to_use);
13604 #endif
13605
13606 return 0;
13607 }
13608
13609 /*
13610 ** Linux entry point for info() function
13611 */
13612 const char *sym53c8xx_info (struct Scsi_Host *host)
13613 {
13614 return SCSI_NCR_DRIVER_NAME;
13615 }
13616
13617 /*
13618 ** Linux entry point of queuecommand() function
13619 */
13620
13621 int sym53c8xx_queue_command (Scsi_Cmnd *cmd, void (* done)(Scsi_Cmnd *))
13622 {
13623 ncb_p np = ((struct host_data *) cmd->device->host->hostdata)->ncb;
13624 unsigned long flags;
13625 int sts;
13626
13627 #ifdef DEBUG_SYM53C8XX
13628 printk("sym53c8xx_queue_command\n");
13629 #endif
13630
13631 cmd->scsi_done = done;
13632 cmd->host_scribble = NULL;
13633 cmd->SCp.ptr = NULL;
13634 cmd->SCp.buffer = NULL;
13635 #ifdef SCSI_NCR_DYNAMIC_DMA_MAPPING
13636 __data_mapped(cmd) = 0;
13637 __data_mapping(cmd) = 0;
13638 #endif
13639
13640 NCR_LOCK_NCB(np, flags);
13641
13642 if ((sts = ncr_queue_command(np, cmd)) != DID_OK) {
13643 SetScsiResult(cmd, sts, 0);
13644 #ifdef DEBUG_SYM53C8XX
13645 printk("sym53c8xx : command not queued - result=%d\n", sts);
13646 #endif
13647 }
13648 #ifdef DEBUG_SYM53C8XX
13649 else
13650 printk("sym53c8xx : command successfully queued\n");
13651 #endif
13652
13653 NCR_UNLOCK_NCB(np, flags);
13654
13655 if (sts != DID_OK) {
13656 unmap_scsi_data(np, cmd);
13657 done(cmd);
13658 }
13659
13660 return sts;
13661 }
13662
13663 /*
13664 ** Linux entry point of the interrupt handler.
13665 ** Since linux versions > 1.3.70, we trust the kernel for
13666 ** passing the internal host descriptor as 'dev_id'.
13667 ** Otherwise, we scan the host list and call the interrupt
13668 ** routine for each host that uses this IRQ.
13669 */
13670
13671 static void sym53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
13672 {
13673 unsigned long flags;
13674 ncb_p np = (ncb_p) dev_id;
13675 Scsi_Cmnd *done_list;
13676
13677 #ifdef DEBUG_SYM53C8XX
13678 printk("sym53c8xx : interrupt received\n");
13679 #endif
13680
13681 if (DEBUG_FLAGS & DEBUG_TINY) printk ("[");
13682
13683 NCR_LOCK_NCB(np, flags);
13684 ncr_exception(np);
13685 done_list = np->done_list;
13686 np->done_list = 0;
13687 NCR_UNLOCK_NCB(np, flags);
13688
13689 if (DEBUG_FLAGS & DEBUG_TINY) printk ("]\n");
13690
13691 if (done_list) {
13692 NCR_LOCK_SCSI_DONE(done_list->device->host, flags);
13693 ncr_flush_done_cmds(done_list);
13694 NCR_UNLOCK_SCSI_DONE(done_list->device->host, flags);
13695 }
13696 }
13697
13698 /*
13699 ** Linux entry point of the timer handler
13700 */
13701
13702 static void sym53c8xx_timeout(unsigned long npref)
13703 {
13704 ncb_p np = (ncb_p) npref;
13705 unsigned long flags;
13706 Scsi_Cmnd *done_list;
13707
13708 NCR_LOCK_NCB(np, flags);
13709 ncr_timeout((ncb_p) np);
13710 done_list = np->done_list;
13711 np->done_list = 0;
13712 NCR_UNLOCK_NCB(np, flags);
13713
13714 if (done_list) {
13715 NCR_LOCK_SCSI_DONE(done_list->device->host, flags);
13716 ncr_flush_done_cmds(done_list);
13717 NCR_UNLOCK_SCSI_DONE(done_list->device->host, flags);
13718 }
13719 }
13720
13721 /*
13722 ** Linux entry point of reset() function
13723 */
13724
13725 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
13726 int sym53c8xx_reset(Scsi_Cmnd *cmd, unsigned int reset_flags)
13727 #else
13728 int sym53c8xx_reset(Scsi_Cmnd *cmd)
13729 #endif
13730 {
13731 ncb_p np = ((struct host_data *) cmd->device->host->hostdata)->ncb;
13732 int sts;
13733 unsigned long flags;
13734 Scsi_Cmnd *done_list;
13735
13736 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
13737 printk("sym53c8xx_reset: pid=%lu reset_flags=%x serial_number=%ld serial_number_at_timeout=%ld\n",
13738 cmd->pid, reset_flags, cmd->serial_number, cmd->serial_number_at_timeout);
13739 #else
13740 printk("sym53c8xx_reset: command pid %lu\n", cmd->pid);
13741 #endif
13742
13743 NCR_LOCK_NCB(np, flags);
13744
13745 /*
13746 * We have to just ignore reset requests in some situations.
13747 */
13748 #if defined SCSI_RESET_NOT_RUNNING
13749 if (cmd->serial_number != cmd->serial_number_at_timeout) {
13750 sts = SCSI_RESET_NOT_RUNNING;
13751 goto out;
13752 }
13753 #endif
13754 /*
13755 * If the mid-level driver told us reset is synchronous, it seems
13756 * that we must call the done() callback for the involved command,
13757 * even if this command was not queued to the low-level driver,
13758 * before returning SCSI_RESET_SUCCESS.
13759 */
13760
13761 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
13762 sts = ncr_reset_bus(np, cmd,
13763 (reset_flags & (SCSI_RESET_SYNCHRONOUS | SCSI_RESET_ASYNCHRONOUS)) == SCSI_RESET_SYNCHRONOUS);
13764 #else
13765 sts = ncr_reset_bus(np, cmd, 0);
13766 #endif
13767
13768 /*
13769 * Since we always reset the controller, when we return success,
13770 * we add this information to the return code.
13771 */
13772 #if defined SCSI_RESET_HOST_RESET
13773 if (sts == SCSI_RESET_SUCCESS)
13774 sts |= SCSI_RESET_HOST_RESET;
13775 #endif
13776
13777 out:
13778 done_list = np->done_list;
13779 np->done_list = 0;
13780 NCR_UNLOCK_NCB(np, flags);
13781
13782 ncr_flush_done_cmds(done_list);
13783
13784 return sts;
13785 }
13786
13787 /*
13788 ** Linux entry point of abort() function
13789 */
13790
13791 int sym53c8xx_abort(Scsi_Cmnd *cmd)
13792 {
13793 ncb_p np = ((struct host_data *) cmd->device->host->hostdata)->ncb;
13794 int sts;
13795 unsigned long flags;
13796 Scsi_Cmnd *done_list;
13797
13798 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
13799 printk("sym53c8xx_abort: pid=%lu serial_number=%ld serial_number_at_timeout=%ld\n",
13800 cmd->pid, cmd->serial_number, cmd->serial_number_at_timeout);
13801 #else
13802 printk("sym53c8xx_abort: command pid %lu\n", cmd->pid);
13803 #endif
13804
13805 NCR_LOCK_NCB(np, flags);
13806
13807 #if defined SCSI_RESET_SYNCHRONOUS && defined SCSI_RESET_ASYNCHRONOUS
13808 /*
13809 * We have to just ignore abort requests in some situations.
13810 */
13811 if (cmd->serial_number != cmd->serial_number_at_timeout) {
13812 sts = SCSI_ABORT_NOT_RUNNING;
13813 goto out;
13814 }
13815 #endif
13816
13817 sts = ncr_abort_command(np, cmd);
13818 out:
13819 done_list = np->done_list;
13820 np->done_list = 0;
13821 NCR_UNLOCK_NCB(np, flags);
13822
13823 ncr_flush_done_cmds(done_list);
13824
13825 return sts;
13826 }
13827
13828
13829 int sym53c8xx_release(struct Scsi_Host *host)
13830 {
13831 #ifdef DEBUG_SYM53C8XX
13832 printk("sym53c8xx : release\n");
13833 #endif
13834 ncr_detach(((struct host_data *) host->hostdata)->ncb);
13835
13836 return 1;
13837 }
13838
13839
13840 /*
13841 ** Scsi command waiting list management.
13842 **
13843 ** It may happen that we cannot insert a scsi command into the start queue,
13844 ** in the following circumstances.
13845 ** Too few preallocated ccb(s),
13846 ** maxtags < cmd_per_lun of the Linux host control block,
13847 ** etc...
13848 ** Such scsi commands are inserted into a waiting list.
13849 ** When a scsi command complete, we try to requeue the commands of the
13850 ** waiting list.
13851 */
13852
13853 #define next_wcmd host_scribble
13854
13855 static void insert_into_waiting_list(ncb_p np, Scsi_Cmnd *cmd)
13856 {
13857 Scsi_Cmnd *wcmd;
13858
13859 #ifdef DEBUG_WAITING_LIST
13860 printk("%s: cmd %lx inserted into waiting list\n", ncr_name(np), (u_long) cmd);
13861 #endif
13862 cmd->next_wcmd = 0;
13863 if (!(wcmd = np->waiting_list)) np->waiting_list = cmd;
13864 else {
13865 while ((wcmd->next_wcmd) != 0)
13866 wcmd = (Scsi_Cmnd *) wcmd->next_wcmd;
13867 wcmd->next_wcmd = (char *) cmd;
13868 }
13869 }
13870
13871 static Scsi_Cmnd *retrieve_from_waiting_list(int to_remove, ncb_p np, Scsi_Cmnd *cmd)
13872 {
13873 Scsi_Cmnd **pcmd = &np->waiting_list;
13874
13875 while (*pcmd) {
13876 if (cmd == *pcmd) {
13877 if (to_remove) {
13878 *pcmd = (Scsi_Cmnd *) cmd->next_wcmd;
13879 cmd->next_wcmd = 0;
13880 }
13881 #ifdef DEBUG_WAITING_LIST
13882 printk("%s: cmd %lx retrieved from waiting list\n", ncr_name(np), (u_long) cmd);
13883 #endif
13884 return cmd;
13885 }
13886 pcmd = (Scsi_Cmnd **) &(*pcmd)->next_wcmd;
13887 }
13888 return 0;
13889 }
13890
13891 static void process_waiting_list(ncb_p np, int sts)
13892 {
13893 Scsi_Cmnd *waiting_list, *wcmd;
13894
13895 waiting_list = np->waiting_list;
13896 np->waiting_list = 0;
13897
13898 #ifdef DEBUG_WAITING_LIST
13899 if (waiting_list) printk("%s: waiting_list=%lx processing sts=%d\n", ncr_name(np), (u_long) waiting_list, sts);
13900 #endif
13901 while ((wcmd = waiting_list) != 0) {
13902 waiting_list = (Scsi_Cmnd *) wcmd->next_wcmd;
13903 wcmd->next_wcmd = 0;
13904 if (sts == DID_OK) {
13905 #ifdef DEBUG_WAITING_LIST
13906 printk("%s: cmd %lx trying to requeue\n", ncr_name(np), (u_long) wcmd);
13907 #endif
13908 sts = ncr_queue_command(np, wcmd);
13909 }
13910 if (sts != DID_OK) {
13911 #ifdef DEBUG_WAITING_LIST
13912 printk("%s: cmd %lx done forced sts=%d\n", ncr_name(np), (u_long) wcmd, sts);
13913 #endif
13914 SetScsiResult(wcmd, sts, 0);
13915 ncr_queue_done_cmd(np, wcmd);
13916 }
13917 }
13918 }
13919
13920 #undef next_wcmd
13921
13922 #ifdef SCSI_NCR_PROC_INFO_SUPPORT
13923
13924 /*=========================================================================
13925 ** Proc file system stuff
13926 **
13927 ** A read operation returns adapter information.
13928 ** A write operation is a control command.
13929 ** The string is parsed in the driver code and the command is passed
13930 ** to the ncr_usercmd() function.
13931 **=========================================================================
13932 */
13933
13934 #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
13935
13936 #define is_digit(c) ((c) >= '0' && (c) <= '9')
13937 #define digit_to_bin(c) ((c) - '0')
13938 #define is_space(c) ((c) == ' ' || (c) == '\t')
13939
13940 static int skip_spaces(char *ptr, int len)
13941 {
13942 int cnt, c;
13943
13944 for (cnt = len; cnt > 0 && (c = *ptr++) && is_space(c); cnt--);
13945
13946 return (len - cnt);
13947 }
13948
13949 static int get_int_arg(char *ptr, int len, u_long *pv)
13950 {
13951 int cnt, c;
13952 u_long v;
13953
13954 for (v = 0, cnt = len; cnt > 0 && (c = *ptr++) && is_digit(c); cnt--) {
13955 v = (v * 10) + digit_to_bin(c);
13956 }
13957
13958 if (pv)
13959 *pv = v;
13960
13961 return (len - cnt);
13962 }
13963
13964 static int is_keyword(char *ptr, int len, char *verb)
13965 {
13966 int verb_len = strlen(verb);
13967
13968 if (len >= strlen(verb) && !memcmp(verb, ptr, verb_len))
13969 return verb_len;
13970 else
13971 return 0;
13972
13973 }
13974
13975 #define SKIP_SPACES(min_spaces) \
13976 if ((arg_len = skip_spaces(ptr, len)) < (min_spaces)) \
13977 return -EINVAL; \
13978 ptr += arg_len; len -= arg_len;
13979
13980 #define GET_INT_ARG(v) \
13981 if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
13982 return -EINVAL; \
13983 ptr += arg_len; len -= arg_len;
13984
13985
13986 /*
13987 ** Parse a control command
13988 */
13989
13990 static int ncr_user_command(ncb_p np, char *buffer, int length)
13991 {
13992 char *ptr = buffer;
13993 int len = length;
13994 struct usrcmd *uc = &np->user;
13995 int arg_len;
13996 u_long target;
13997
13998 bzero(uc, sizeof(*uc));
13999
14000 if (len > 0 && ptr[len-1] == '\n')
14001 --len;
14002
14003 if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
14004 uc->cmd = UC_SETSYNC;
14005 else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
14006 uc->cmd = UC_SETTAGS;
14007 else if ((arg_len = is_keyword(ptr, len, "setorder")) != 0)
14008 uc->cmd = UC_SETORDER;
14009 else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
14010 uc->cmd = UC_SETVERBOSE;
14011 else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
14012 uc->cmd = UC_SETWIDE;
14013 else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
14014 uc->cmd = UC_SETDEBUG;
14015 else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
14016 uc->cmd = UC_SETFLAG;
14017 else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
14018 uc->cmd = UC_RESETDEV;
14019 else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
14020 uc->cmd = UC_CLEARDEV;
14021 else
14022 arg_len = 0;
14023
14024 #ifdef DEBUG_PROC_INFO
14025 printk("ncr_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
14026 #endif
14027
14028 if (!arg_len)
14029 return -EINVAL;
14030 ptr += arg_len; len -= arg_len;
14031
14032 switch(uc->cmd) {
14033 case UC_SETSYNC:
14034 case UC_SETTAGS:
14035 case UC_SETWIDE:
14036 case UC_SETFLAG:
14037 case UC_RESETDEV:
14038 case UC_CLEARDEV:
14039 SKIP_SPACES(1);
14040 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
14041 ptr += arg_len; len -= arg_len;
14042 uc->target = ~0;
14043 } else {
14044 GET_INT_ARG(target);
14045 uc->target = (1<<target);
14046 #ifdef DEBUG_PROC_INFO
14047 printk("ncr_user_command: target=%ld\n", target);
14048 #endif
14049 }
14050 break;
14051 }
14052
14053 switch(uc->cmd) {
14054 case UC_SETVERBOSE:
14055 case UC_SETSYNC:
14056 case UC_SETTAGS:
14057 case UC_SETWIDE:
14058 SKIP_SPACES(1);
14059 GET_INT_ARG(uc->data);
14060 #ifdef DEBUG_PROC_INFO
14061 printk("ncr_user_command: data=%ld\n", uc->data);
14062 #endif
14063 break;
14064 case UC_SETORDER:
14065 SKIP_SPACES(1);
14066 if ((arg_len = is_keyword(ptr, len, "simple")))
14067 uc->data = M_SIMPLE_TAG;
14068 else if ((arg_len = is_keyword(ptr, len, "ordered")))
14069 uc->data = M_ORDERED_TAG;
14070 else if ((arg_len = is_keyword(ptr, len, "default")))
14071 uc->data = 0;
14072 else
14073 return -EINVAL;
14074 break;
14075 case UC_SETDEBUG:
14076 while (len > 0) {
14077 SKIP_SPACES(1);
14078 if ((arg_len = is_keyword(ptr, len, "alloc")))
14079 uc->data |= DEBUG_ALLOC;
14080 else if ((arg_len = is_keyword(ptr, len, "phase")))
14081 uc->data |= DEBUG_PHASE;
14082 else if ((arg_len = is_keyword(ptr, len, "queue")))
14083 uc->data |= DEBUG_QUEUE;
14084 else if ((arg_len = is_keyword(ptr, len, "result")))
14085 uc->data |= DEBUG_RESULT;
14086 else if ((arg_len = is_keyword(ptr, len, "pointer")))
14087 uc->data |= DEBUG_POINTER;
14088 else if ((arg_len = is_keyword(ptr, len, "script")))
14089 uc->data |= DEBUG_SCRIPT;
14090 else if ((arg_len = is_keyword(ptr, len, "tiny")))
14091 uc->data |= DEBUG_TINY;
14092 else if ((arg_len = is_keyword(ptr, len, "timing")))
14093 uc->data |= DEBUG_TIMING;
14094 else if ((arg_len = is_keyword(ptr, len, "nego")))
14095 uc->data |= DEBUG_NEGO;
14096 else if ((arg_len = is_keyword(ptr, len, "tags")))
14097 uc->data |= DEBUG_TAGS;
14098 else
14099 return -EINVAL;
14100 ptr += arg_len; len -= arg_len;
14101 }
14102 #ifdef DEBUG_PROC_INFO
14103 printk("ncr_user_command: data=%ld\n", uc->data);
14104 #endif
14105 break;
14106 case UC_SETFLAG:
14107 while (len > 0) {
14108 SKIP_SPACES(1);
14109 if ((arg_len = is_keyword(ptr, len, "trace")))
14110 uc->data |= UF_TRACE;
14111 else if ((arg_len = is_keyword(ptr, len, "no_disc")))
14112 uc->data |= UF_NODISC;
14113 else
14114 return -EINVAL;
14115 ptr += arg_len; len -= arg_len;
14116 }
14117 break;
14118 default:
14119 break;
14120 }
14121
14122 if (len)
14123 return -EINVAL;
14124 else {
14125 unsigned long flags;
14126
14127 NCR_LOCK_NCB(np, flags);
14128 ncr_usercmd (np);
14129 NCR_UNLOCK_NCB(np, flags);
14130 }
14131 return length;
14132 }
14133
14134 #endif /* SCSI_NCR_USER_COMMAND_SUPPORT */
14135
14136 #ifdef SCSI_NCR_USER_INFO_SUPPORT
14137
14138 struct info_str
14139 {
14140 char *buffer;
14141 int length;
14142 int offset;
14143 int pos;
14144 };
14145
14146 static void copy_mem_info(struct info_str *info, char *data, int len)
14147 {
14148 if (info->pos + len > info->length)
14149 len = info->length - info->pos;
14150
14151 if (info->pos + len < info->offset) {
14152 info->pos += len;
14153 return;
14154 }
14155 if (info->pos < info->offset) {
14156 data += (info->offset - info->pos);
14157 len -= (info->offset - info->pos);
14158 }
14159
14160 if (len > 0) {
14161 memcpy(info->buffer + info->pos, data, len);
14162 info->pos += len;
14163 }
14164 }
14165
14166 static int copy_info(struct info_str *info, char *fmt, ...)
14167 {
14168 va_list args;
14169 char buf[81];
14170 int len;
14171
14172 va_start(args, fmt);
14173 len = vsprintf(buf, fmt, args);
14174 va_end(args);
14175
14176 copy_mem_info(info, buf, len);
14177 return len;
14178 }
14179
14180 /*
14181 ** Copy formatted information into the input buffer.
14182 */
14183
14184 static int ncr_host_info(ncb_p np, char *ptr, off_t offset, int len)
14185 {
14186 struct info_str info;
14187
14188 info.buffer = ptr;
14189 info.length = len;
14190 info.offset = offset;
14191 info.pos = 0;
14192
14193 copy_info(&info, "General information:\n");
14194 copy_info(&info, " Chip " NAME53C "%s, device id 0x%x, "
14195 "revision id 0x%x\n",
14196 np->chip_name, np->device_id, np->revision_id);
14197 copy_info(&info, " On PCI bus %d, device %d, function %d, "
14198 #ifdef __sparc__
14199 "IRQ %s\n",
14200 #else
14201 "IRQ %d\n",
14202 #endif
14203 np->bus, (np->device_fn & 0xf8) >> 3, np->device_fn & 7,
14204 #ifdef __sparc__
14205 __irq_itoa(np->irq));
14206 #else
14207 (int) np->irq);
14208 #endif
14209 copy_info(&info, " Synchronous period factor %d, "
14210 "max commands per lun %d\n",
14211 (int) np->minsync, MAX_TAGS);
14212
14213 if (driver_setup.debug || driver_setup.verbose > 1) {
14214 copy_info(&info, " Debug flags 0x%x, verbosity level %d\n",
14215 driver_setup.debug, driver_setup.verbose);
14216 }
14217
14218 return info.pos > info.offset? info.pos - info.offset : 0;
14219 }
14220
14221 #endif /* SCSI_NCR_USER_INFO_SUPPORT */
14222
14223 /*
14224 ** Entry point of the scsi proc fs of the driver.
14225 ** - func = 0 means read (returns adapter infos)
14226 ** - func = 1 means write (parse user control command)
14227 */
14228
14229 static int sym53c8xx_proc_info(char *buffer, char **start, off_t offset,
14230 int length, int hostno, int func)
14231 {
14232 struct Scsi_Host *host;
14233 struct host_data *host_data;
14234 ncb_p ncb = 0;
14235 int retv;
14236
14237 #ifdef DEBUG_PROC_INFO
14238 printk("sym53c8xx_proc_info: hostno=%d, func=%d\n", hostno, func);
14239 #endif
14240
14241 host = scsi_host_hn_get(hostno);
14242 if (!host)
14243 return -EINVAL;
14244
14245 host_data = (struct host_data *) host->hostdata;
14246 ncb = host_data->ncb;
14247 retv = -EINVAL;
14248 if (!ncb)
14249 goto out;
14250
14251 if (func) {
14252 #ifdef SCSI_NCR_USER_COMMAND_SUPPORT
14253 retv = ncr_user_command(ncb, buffer, length);
14254 #endif
14255 } else {
14256 if (start)
14257 *start = buffer;
14258 #ifdef SCSI_NCR_USER_INFO_SUPPORT
14259 retv = ncr_host_info(ncb, buffer, offset, length);
14260 #endif
14261 }
14262
14263 out:
14264 scsi_host_put(host);
14265 return retv;
14266 }
14267
14268
14269 /*=========================================================================
14270 ** End of proc file system stuff
14271 **=========================================================================
14272 */
14273 #endif
14274
14275
14276 #ifdef SCSI_NCR_NVRAM_SUPPORT
14277
14278 /*
14279 * 24C16 EEPROM reading.
14280 *
14281 * GPOI0 - data in/data out
14282 * GPIO1 - clock
14283 * Symbios NVRAM wiring now also used by Tekram.
14284 */
14285
14286 #define SET_BIT 0
14287 #define CLR_BIT 1
14288 #define SET_CLK 2
14289 #define CLR_CLK 3
14290
14291 /*
14292 * Set/clear data/clock bit in GPIO0
14293 */
14294 static void __init
14295 S24C16_set_bit(ncr_slot *np, u_char write_bit, u_char *gpreg, int bit_mode)
14296 {
14297 UDELAY (5);
14298 switch (bit_mode){
14299 case SET_BIT:
14300 *gpreg |= write_bit;
14301 break;
14302 case CLR_BIT:
14303 *gpreg &= 0xfe;
14304 break;
14305 case SET_CLK:
14306 *gpreg |= 0x02;
14307 break;
14308 case CLR_CLK:
14309 *gpreg &= 0xfd;
14310 break;
14311
14312 }
14313 OUTB (nc_gpreg, *gpreg);
14314 UDELAY (5);
14315 }
14316
14317 /*
14318 * Send START condition to NVRAM to wake it up.
14319 */
14320 static void __init S24C16_start(ncr_slot *np, u_char *gpreg)
14321 {
14322 S24C16_set_bit(np, 1, gpreg, SET_BIT);
14323 S24C16_set_bit(np, 0, gpreg, SET_CLK);
14324 S24C16_set_bit(np, 0, gpreg, CLR_BIT);
14325 S24C16_set_bit(np, 0, gpreg, CLR_CLK);
14326 }
14327
14328 /*
14329 * Send STOP condition to NVRAM - puts NVRAM to sleep... ZZzzzz!!
14330 */
14331 static void __init S24C16_stop(ncr_slot *np, u_char *gpreg)
14332 {
14333 S24C16_set_bit(np, 0, gpreg, SET_CLK);
14334 S24C16_set_bit(np, 1, gpreg, SET_BIT);
14335 }
14336
14337 /*
14338 * Read or write a bit to the NVRAM,
14339 * read if GPIO0 input else write if GPIO0 output
14340 */
14341 static void __init
14342 S24C16_do_bit(ncr_slot *np, u_char *read_bit, u_char write_bit, u_char *gpreg)
14343 {
14344 S24C16_set_bit(np, write_bit, gpreg, SET_BIT);
14345 S24C16_set_bit(np, 0, gpreg, SET_CLK);
14346 if (read_bit)
14347 *read_bit = INB (nc_gpreg);
14348 S24C16_set_bit(np, 0, gpreg, CLR_CLK);
14349 S24C16_set_bit(np, 0, gpreg, CLR_BIT);
14350 }
14351
14352 /*
14353 * Output an ACK to the NVRAM after reading,
14354 * change GPIO0 to output and when done back to an input
14355 */
14356 static void __init
14357 S24C16_write_ack(ncr_slot *np, u_char write_bit, u_char *gpreg, u_char *gpcntl)
14358 {
14359 OUTB (nc_gpcntl, *gpcntl & 0xfe);
14360 S24C16_do_bit(np, 0, write_bit, gpreg);
14361 OUTB (nc_gpcntl, *gpcntl);
14362 }
14363
14364 /*
14365 * Input an ACK from NVRAM after writing,
14366 * change GPIO0 to input and when done back to an output
14367 */
14368 static void __init
14369 S24C16_read_ack(ncr_slot *np, u_char *read_bit, u_char *gpreg, u_char *gpcntl)
14370 {
14371 OUTB (nc_gpcntl, *gpcntl | 0x01);
14372 S24C16_do_bit(np, read_bit, 1, gpreg);
14373 OUTB (nc_gpcntl, *gpcntl);
14374 }
14375
14376 /*
14377 * WRITE a byte to the NVRAM and then get an ACK to see it was accepted OK,
14378 * GPIO0 must already be set as an output
14379 */
14380 static void __init
14381 S24C16_write_byte(ncr_slot *np, u_char *ack_data, u_char write_data,
14382 u_char *gpreg, u_char *gpcntl)
14383 {
14384 int x;
14385
14386 for (x = 0; x < 8; x++)
14387 S24C16_do_bit(np, 0, (write_data >> (7 - x)) & 0x01, gpreg);
14388
14389 S24C16_read_ack(np, ack_data, gpreg, gpcntl);
14390 }
14391
14392 /*
14393 * READ a byte from the NVRAM and then send an ACK to say we have got it,
14394 * GPIO0 must already be set as an input
14395 */
14396 static void __init
14397 S24C16_read_byte(ncr_slot *np, u_char *read_data, u_char ack_data,
14398 u_char *gpreg, u_char *gpcntl)
14399 {
14400 int x;
14401 u_char read_bit;
14402
14403 *read_data = 0;
14404 for (x = 0; x < 8; x++) {
14405 S24C16_do_bit(np, &read_bit, 1, gpreg);
14406 *read_data |= ((read_bit & 0x01) << (7 - x));
14407 }
14408
14409 S24C16_write_ack(np, ack_data, gpreg, gpcntl);
14410 }
14411
14412 /*
14413 * Read 'len' bytes starting at 'offset'.
14414 */
14415 static int __init
14416 sym_read_S24C16_nvram (ncr_slot *np, int offset, u_char *data, int len)
14417 {
14418 u_char gpcntl, gpreg;
14419 u_char old_gpcntl, old_gpreg;
14420 u_char ack_data;
14421 int retv = 1;
14422 int x;
14423
14424 /* save current state of GPCNTL and GPREG */
14425 old_gpreg = INB (nc_gpreg);
14426 old_gpcntl = INB (nc_gpcntl);
14427 gpcntl = old_gpcntl & 0x1c;
14428
14429 /* set up GPREG & GPCNTL to set GPIO0 and GPIO1 in to known state */
14430 OUTB (nc_gpreg, old_gpreg);
14431 OUTB (nc_gpcntl, gpcntl);
14432
14433 /* this is to set NVRAM into a known state with GPIO0/1 both low */
14434 gpreg = old_gpreg;
14435 S24C16_set_bit(np, 0, &gpreg, CLR_CLK);
14436 S24C16_set_bit(np, 0, &gpreg, CLR_BIT);
14437
14438 /* now set NVRAM inactive with GPIO0/1 both high */
14439 S24C16_stop(np, &gpreg);
14440
14441 /* activate NVRAM */
14442 S24C16_start(np, &gpreg);
14443
14444 /* write device code and random address MSB */
14445 S24C16_write_byte(np, &ack_data,
14446 0xa0 | ((offset >> 7) & 0x0e), &gpreg, &gpcntl);
14447 if (ack_data & 0x01)
14448 goto out;
14449
14450 /* write random address LSB */
14451 S24C16_write_byte(np, &ack_data,
14452 offset & 0xff, &gpreg, &gpcntl);
14453 if (ack_data & 0x01)
14454 goto out;
14455
14456 /* regenerate START state to set up for reading */
14457 S24C16_start(np, &gpreg);
14458
14459 /* rewrite device code and address MSB with read bit set (lsb = 0x01) */
14460 S24C16_write_byte(np, &ack_data,
14461 0xa1 | ((offset >> 7) & 0x0e), &gpreg, &gpcntl);
14462 if (ack_data & 0x01)
14463 goto out;
14464
14465 /* now set up GPIO0 for inputting data */
14466 gpcntl |= 0x01;
14467 OUTB (nc_gpcntl, gpcntl);
14468
14469 /* input all requested data - only part of total NVRAM */
14470 for (x = 0; x < len; x++)
14471 S24C16_read_byte(np, &data[x], (x == (len-1)), &gpreg, &gpcntl);
14472
14473 /* finally put NVRAM back in inactive mode */
14474 gpcntl &= 0xfe;
14475 OUTB (nc_gpcntl, gpcntl);
14476 S24C16_stop(np, &gpreg);
14477 retv = 0;
14478 out:
14479 /* return GPIO0/1 to original states after having accessed NVRAM */
14480 OUTB (nc_gpcntl, old_gpcntl);
14481 OUTB (nc_gpreg, old_gpreg);
14482
14483 return retv;
14484 }
14485
14486 #undef SET_BIT
14487 #undef CLR_BIT
14488 #undef SET_CLK
14489 #undef CLR_CLK
14490
14491 /*
14492 * Try reading Symbios NVRAM.
14493 * Return 0 if OK.
14494 */
14495 static int __init sym_read_Symbios_nvram (ncr_slot *np, Symbios_nvram *nvram)
14496 {
14497 static u_char Symbios_trailer[6] = {0xfe, 0xfe, 0, 0, 0, 0};
14498 u_char *data = (u_char *) nvram;
14499 int len = sizeof(*nvram);
14500 u_short csum;
14501 int x;
14502
14503 /* probe the 24c16 and read the SYMBIOS 24c16 area */
14504 if (sym_read_S24C16_nvram (np, SYMBIOS_NVRAM_ADDRESS, data, len))
14505 return 1;
14506
14507 /* check valid NVRAM signature, verify byte count and checksum */
14508 if (nvram->type != 0 ||
14509 memcmp(nvram->trailer, Symbios_trailer, 6) ||
14510 nvram->byte_count != len - 12)
14511 return 1;
14512
14513 /* verify checksum */
14514 for (x = 6, csum = 0; x < len - 6; x++)
14515 csum += data[x];
14516 if (csum != nvram->checksum)
14517 return 1;
14518
14519 return 0;
14520 }
14521
14522 /*
14523 * 93C46 EEPROM reading.
14524 *
14525 * GPOI0 - data in
14526 * GPIO1 - data out
14527 * GPIO2 - clock
14528 * GPIO4 - chip select
14529 *
14530 * Used by Tekram.
14531 */
14532
14533 /*
14534 * Pulse clock bit in GPIO0
14535 */
14536 static void __init T93C46_Clk(ncr_slot *np, u_char *gpreg)
14537 {
14538 OUTB (nc_gpreg, *gpreg | 0x04);
14539 UDELAY (2);
14540 OUTB (nc_gpreg, *gpreg);
14541 }
14542
14543 /*
14544 * Read bit from NVRAM
14545 */
14546 static void __init T93C46_Read_Bit(ncr_slot *np, u_char *read_bit, u_char *gpreg)
14547 {
14548 UDELAY (2);
14549 T93C46_Clk(np, gpreg);
14550 *read_bit = INB (nc_gpreg);
14551 }
14552
14553 /*
14554 * Write bit to GPIO0
14555 */
14556 static void __init T93C46_Write_Bit(ncr_slot *np, u_char write_bit, u_char *gpreg)
14557 {
14558 if (write_bit & 0x01)
14559 *gpreg |= 0x02;
14560 else
14561 *gpreg &= 0xfd;
14562
14563 *gpreg |= 0x10;
14564
14565 OUTB (nc_gpreg, *gpreg);
14566 UDELAY (2);
14567
14568 T93C46_Clk(np, gpreg);
14569 }
14570
14571 /*
14572 * Send STOP condition to NVRAM - puts NVRAM to sleep... ZZZzzz!!
14573 */
14574 static void __init T93C46_Stop(ncr_slot *np, u_char *gpreg)
14575 {
14576 *gpreg &= 0xef;
14577 OUTB (nc_gpreg, *gpreg);
14578 UDELAY (2);
14579
14580 T93C46_Clk(np, gpreg);
14581 }
14582
14583 /*
14584 * Send read command and address to NVRAM
14585 */
14586 static void __init
14587 T93C46_Send_Command(ncr_slot *np, u_short write_data,
14588 u_char *read_bit, u_char *gpreg)
14589 {
14590 int x;
14591
14592 /* send 9 bits, start bit (1), command (2), address (6) */
14593 for (x = 0; x < 9; x++)
14594 T93C46_Write_Bit(np, (u_char) (write_data >> (8 - x)), gpreg);
14595
14596 *read_bit = INB (nc_gpreg);
14597 }
14598
14599 /*
14600 * READ 2 bytes from the NVRAM
14601 */
14602 static void __init
14603 T93C46_Read_Word(ncr_slot *np, u_short *nvram_data, u_char *gpreg)
14604 {
14605 int x;
14606 u_char read_bit;
14607
14608 *nvram_data = 0;
14609 for (x = 0; x < 16; x++) {
14610 T93C46_Read_Bit(np, &read_bit, gpreg);
14611
14612 if (read_bit & 0x01)
14613 *nvram_data |= (0x01 << (15 - x));
14614 else
14615 *nvram_data &= ~(0x01 << (15 - x));
14616 }
14617 }
14618
14619 /*
14620 * Read Tekram NvRAM data.
14621 */
14622 static int __init
14623 T93C46_Read_Data(ncr_slot *np, u_short *data,int len,u_char *gpreg)
14624 {
14625 u_char read_bit;
14626 int x;
14627
14628 for (x = 0; x < len; x++) {
14629
14630 /* output read command and address */
14631 T93C46_Send_Command(np, 0x180 | x, &read_bit, gpreg);
14632 if (read_bit & 0x01)
14633 return 1; /* Bad */
14634 T93C46_Read_Word(np, &data[x], gpreg);
14635 T93C46_Stop(np, gpreg);
14636 }
14637
14638 return 0;
14639 }
14640
14641 /*
14642 * Try reading 93C46 Tekram NVRAM.
14643 */
14644 static int __init
14645 sym_read_T93C46_nvram (ncr_slot *np, Tekram_nvram *nvram)
14646 {
14647 u_char gpcntl, gpreg;
14648 u_char old_gpcntl, old_gpreg;
14649 int retv = 1;
14650
14651 /* save current state of GPCNTL and GPREG */
14652 old_gpreg = INB (nc_gpreg);
14653 old_gpcntl = INB (nc_gpcntl);
14654
14655 /* set up GPREG & GPCNTL to set GPIO0/1/2/4 in to known state, 0 in,
14656 1/2/4 out */
14657 gpreg = old_gpreg & 0xe9;
14658 OUTB (nc_gpreg, gpreg);
14659 gpcntl = (old_gpcntl & 0xe9) | 0x09;
14660 OUTB (nc_gpcntl, gpcntl);
14661
14662 /* input all of NVRAM, 64 words */
14663 retv = T93C46_Read_Data(np, (u_short *) nvram,
14664 sizeof(*nvram) / sizeof(short), &gpreg);
14665
14666 /* return GPIO0/1/2/4 to original states after having accessed NVRAM */
14667 OUTB (nc_gpcntl, old_gpcntl);
14668 OUTB (nc_gpreg, old_gpreg);
14669
14670 return retv;
14671 }
14672
14673 /*
14674 * Try reading Tekram NVRAM.
14675 * Return 0 if OK.
14676 */
14677 static int __init
14678 sym_read_Tekram_nvram (ncr_slot *np, u_short device_id, Tekram_nvram *nvram)
14679 {
14680 u_char *data = (u_char *) nvram;
14681 int len = sizeof(*nvram);
14682 u_short csum;
14683 int x;
14684
14685 switch (device_id) {
14686 case PCI_DEVICE_ID_NCR_53C885:
14687 case PCI_DEVICE_ID_NCR_53C895:
14688 case PCI_DEVICE_ID_NCR_53C896:
14689 x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS,
14690 data, len);
14691 break;
14692 case PCI_DEVICE_ID_NCR_53C875:
14693 x = sym_read_S24C16_nvram(np, TEKRAM_24C16_NVRAM_ADDRESS,
14694 data, len);
14695 if (!x)
14696 break;
14697 default:
14698 x = sym_read_T93C46_nvram(np, nvram);
14699 break;
14700 }
14701 if (x)
14702 return 1;
14703
14704 /* verify checksum */
14705 for (x = 0, csum = 0; x < len - 1; x += 2)
14706 csum += data[x] + (data[x+1] << 8);
14707 if (csum != 0x1234)
14708 return 1;
14709
14710 return 0;
14711 }
14712
14713 #endif /* SCSI_NCR_NVRAM_SUPPORT */
14714
14715 /*
14716 ** Module stuff
14717 */
14718
14719 MODULE_LICENSE("GPL");
14720
14721 static Scsi_Host_Template driver_template = {
14722 .name = "sym53c8xx",
14723 .detect = sym53c8xx_detect,
14724 .release = sym53c8xx_release,
14725 .info = sym53c8xx_info,
14726 .queuecommand = sym53c8xx_queue_command,
14727 .slave_configure = sym53c8xx_slave_configure,
14728 .abort = sym53c8xx_abort,
14729 .reset = sym53c8xx_reset,
14730 .can_queue = SCSI_NCR_CAN_QUEUE,
14731 .this_id = 7,
14732 .sg_tablesize = SCSI_NCR_SG_TABLESIZE,
14733 .cmd_per_lun = SCSI_NCR_CMD_PER_LUN,
14734 .max_sectors = MAX_HW_SEGMENTS*8,
14735 .use_clustering = DISABLE_CLUSTERING,
14736 .highmem_io = 1
14737 };
14738 #include "scsi_module.c"