OSDN Git Service

[PATCH] sym53c8xx_2 SMP deadlock on driver load
[linux-kernel-docs/linux-2.4.36.git] / drivers / scsi / sym53c8xx_2 / sym_glue.c
1 /*
2  * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family 
3  * of PCI-SCSI IO processors.
4  *
5  * Copyright (C) 1999-2001  Gerard Roudier <groudier@free.fr>
6  *
7  * This driver is derived from the Linux sym53c8xx driver.
8  * Copyright (C) 1998-2000  Gerard Roudier
9  *
10  * The sym53c8xx driver is derived from the ncr53c8xx driver that had been 
11  * a port of the FreeBSD ncr driver to Linux-1.2.13.
12  *
13  * The original ncr driver has been written for 386bsd and FreeBSD by
14  *         Wolfgang Stanglmeier        <wolf@cologne.de>
15  *         Stefan Esser                <se@mi.Uni-Koeln.de>
16  * Copyright (C) 1994  Wolfgang Stanglmeier
17  *
18  * Other major contributions:
19  *
20  * NVRAM detection and reading.
21  * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
22  *
23  *-----------------------------------------------------------------------------
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  * 1. Redistributions of source code must retain the above copyright
29  *    notice, this list of conditions and the following disclaimer.
30  * 2. The name of the author may not be used to endorse or promote products
31  *    derived from this software without specific prior written permission.
32  *
33  * Where this Software is combined with software released under the terms of 
34  * the GNU Public License ("GPL") and the terms of the GPL would require the 
35  * combined work to also be released under the terms of the GPL, the terms
36  * and conditions of this License will apply in addition to those of the
37  * GPL with the exception of any terms or conditions of this License that
38  * conflict with, or are expressly prohibited by, the GPL.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
44  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  */
52 #define SYM_GLUE_C
53
54 #include <linux/module.h>
55 #include "sym_glue.h"
56
57 #define NAME53C         "sym53c"
58 #define NAME53C8XX      "sym53c8xx"
59
60 /*
61  *  Simple Wrapper to kernel PCI bus interface.
62  */
63
64 typedef struct pci_dev *pcidev_t;
65 #define PCIDEV_NULL             (0)
66 #define PciBusNumber(d)         (d)->bus->number
67 #define PciDeviceFn(d)          (d)->devfn
68 #define PciVendorId(d)          (d)->vendor
69 #define PciDeviceId(d)          (d)->device
70 #define PciIrqLine(d)           (d)->irq
71
72 static u_long __init
73 pci_get_base_cookie(struct pci_dev *pdev, int index)
74 {
75         u_long base;
76
77 #if LINUX_VERSION_CODE > LinuxVersionCode(2,3,12)
78         base = pdev->resource[index].start;
79 #else
80         base = pdev->base_address[index];
81 #if BITS_PER_LONG > 32
82         if ((base & 0x7) == 0x4)
83                 base |= (((u_long)pdev->base_address[++index]) << 32);
84 #endif
85 #endif
86         return (base & ~0x7ul);
87 }
88
89 static int __init
90 pci_get_base_address(struct pci_dev *pdev, int index, u_long *base)
91 {
92         u32 tmp;
93 #define PCI_BAR_OFFSET(index) (PCI_BASE_ADDRESS_0 + (index<<2))
94
95         pci_read_config_dword(pdev, PCI_BAR_OFFSET(index), &tmp);
96         *base = tmp;
97         ++index;
98         if ((tmp & 0x7) == 0x4) {
99 #if BITS_PER_LONG > 32
100                 pci_read_config_dword(pdev, PCI_BAR_OFFSET(index), &tmp);
101                 *base |= (((u_long)tmp) << 32);
102 #endif
103                 ++index;
104         }
105         return index;
106 #undef PCI_BAR_OFFSET
107 }
108
109 #if LINUX_VERSION_CODE  < LinuxVersionCode(2,4,0)
110 #define pci_enable_device(pdev)         (0)
111 #endif
112
113 #if LINUX_VERSION_CODE  < LinuxVersionCode(2,4,4)
114 #define scsi_set_pci_device(inst, pdev) do { ;} while (0)
115 #endif
116
117 /*
118  *  Insert a delay in micro-seconds and milli-seconds.
119  */
120 void sym_udelay(int us) { udelay(us); }
121 void sym_mdelay(int ms) { mdelay(ms); }
122
123 /*
124  *  SMP threading.
125  *
126  *  The whole SCSI sub-system under Linux is basically single-threaded.
127  *  Everything, including low-level driver interrupt routine, happens 
128  *  with the `io_request_lock' held.
129  *  The sym53c8xx-1.x drivers series ran their interrupt code using a 
130  *  spin mutex per controller. This added complexity without improving 
131  *  scalability significantly. the sym-2 driver still use a spinlock 
132  *  per controller for safety, but basically runs with the damned 
133  *  io_request_lock held.
134  */
135
136 spinlock_t sym53c8xx_lock = SPIN_LOCK_UNLOCKED;
137
138 #define SYM_LOCK_DRIVER(flags)    spin_lock_irqsave(&sym53c8xx_lock, flags)
139 #define SYM_UNLOCK_DRIVER(flags)  spin_unlock_irqrestore(&sym53c8xx_lock,flags)
140
141 #define SYM_INIT_LOCK_HCB(np)     spin_lock_init(&np->s.smp_lock);
142 #define SYM_LOCK_HCB(np, flags)   spin_lock_irqsave(&np->s.smp_lock, flags)
143 #define SYM_UNLOCK_HCB(np, flags) spin_unlock_irqrestore(&np->s.smp_lock, flags)
144
145 #define SYM_LOCK_SCSI(np, flags) \
146                 spin_lock_irqsave(&io_request_lock, flags)
147 #define SYM_UNLOCK_SCSI(np, flags) \
148                 spin_unlock_irqrestore(&io_request_lock, flags)
149
150 /* Ugly, but will make things easier if this locking will ever disappear */
151 #define SYM_LOCK_SCSI_NOSAVE(np)        spin_lock_irq(&io_request_lock)
152 #define SYM_UNLOCK_SCSI_NORESTORE(np)   spin_unlock_irq(&io_request_lock)
153
154 /*
155  *  These simple macros limit expression involving 
156  *  kernel time values (jiffies) to some that have 
157  *  chance not to be too much incorrect. :-)
158  */
159 #define ktime_get(o)            (jiffies + (u_long) o)
160 #define ktime_exp(b)            ((long)(jiffies) - (long)(b) >= 0)
161 #define ktime_dif(a, b)         ((long)(a) - (long)(b))
162 #define ktime_add(a, o)         ((a) + (u_long)(o))
163 #define ktime_sub(a, o)         ((a) - (u_long)(o))
164
165 /*
166  *  Wrappers to the generic memory allocator.
167  */
168 void *sym_calloc(int size, char *name)
169 {
170         u_long flags;
171         void *m;
172         SYM_LOCK_DRIVER(flags);
173         m = sym_calloc_unlocked(size, name);
174         SYM_UNLOCK_DRIVER(flags);
175         return m;
176 }
177
178 void sym_mfree(void *m, int size, char *name)
179 {
180         u_long flags;
181         SYM_LOCK_DRIVER(flags);
182         sym_mfree_unlocked(m, size, name);
183         SYM_UNLOCK_DRIVER(flags);
184 }
185
186 #ifdef  SYM_LINUX_DYNAMIC_DMA_MAPPING
187
188 void *__sym_calloc_dma(m_pool_ident_t dev_dmat, int size, char *name)
189 {
190         u_long flags;
191         void *m;
192         SYM_LOCK_DRIVER(flags);
193         m = __sym_calloc_dma_unlocked(dev_dmat, size, name);
194         SYM_UNLOCK_DRIVER(flags);
195         return m;
196 }
197
198 void __sym_mfree_dma(m_pool_ident_t dev_dmat, void *m, int size, char *name)
199 {
200         u_long flags;
201         SYM_LOCK_DRIVER(flags);
202         __sym_mfree_dma_unlocked(dev_dmat, m, size, name);
203         SYM_UNLOCK_DRIVER(flags);
204 }
205
206 m_addr_t __vtobus(m_pool_ident_t dev_dmat, void *m)
207 {
208         u_long flags;
209         m_addr_t b;
210         SYM_LOCK_DRIVER(flags);
211         b = __vtobus_unlocked(dev_dmat, m);
212         SYM_UNLOCK_DRIVER(flags);
213         return b;
214 }
215
216 #endif  /* SYM_LINUX_DYNAMIC_DMA_MAPPING */
217
218
219 /*
220  *  Map/unmap a PCI memory window.
221  */
222 #ifndef SYM_OPT_NO_BUS_MEMORY_MAPPING
223 static u_long __init pci_map_mem(u_long base, u_long size)
224 {
225         u_long page_base        = ((u_long) base) & PAGE_MASK;
226         u_long page_offs        = ((u_long) base) - page_base;
227         u_long page_remapped;
228
229         spin_unlock_irq(&io_request_lock);
230         page_remapped = (u_long) ioremap(page_base, page_offs+size);
231         spin_lock_irq(&io_request_lock);
232
233         return page_remapped? (page_remapped + page_offs) : 0UL;
234 }
235
236 static void pci_unmap_mem(u_long vaddr,
237                           u_long size,
238                           int holding_io_request_lock)
239 {
240         if (vaddr) {
241                 if (holding_io_request_lock)
242                         spin_unlock_irq(&io_request_lock);
243                 iounmap((void *) (vaddr & PAGE_MASK));
244                 if (holding_io_request_lock)
245                         spin_lock_irq(&io_request_lock);
246         }
247 }
248 #endif
249
250 /*
251  *  Used to retrieve the host structure when the 
252  *  driver is called from the proc FS.
253  */
254 static struct Scsi_Host *first_host = NULL;
255
256 /*
257  *  /proc directory entry and proc_info.
258  */
259 #if LINUX_VERSION_CODE < LinuxVersionCode(2,3,27)
260 static struct proc_dir_entry proc_scsi_sym53c8xx = {
261     PROC_SCSI_SYM53C8XX, 9, NAME53C8XX,
262     S_IFDIR | S_IRUGO | S_IXUGO, 2
263 };
264 #endif
265
266 /*
267  *  Transfer direction
268  *
269  *  Until some linux kernel version near 2.3.40, low-level scsi 
270  *  drivers were not told about data transfer direction.
271  */
272 #if LINUX_VERSION_CODE > LinuxVersionCode(2, 3, 40)
273
274 #define scsi_data_direction(cmd)        (cmd->sc_data_direction)
275
276 #else
277
278 static __inline__ int scsi_data_direction(Scsi_Cmnd *cmd)
279 {
280         int direction;
281
282         switch((int) cmd->cmnd[0]) {
283         case 0x08:  /*  READ(6)                         08 */
284         case 0x28:  /*  READ(10)                        28 */
285         case 0xA8:  /*  READ(12)                        A8 */
286                 direction = SCSI_DATA_READ;
287                 break;
288         case 0x0A:  /*  WRITE(6)                        0A */
289         case 0x2A:  /*  WRITE(10)                       2A */
290         case 0xAA:  /*  WRITE(12)                       AA */
291                 direction = SCSI_DATA_WRITE;
292                 break;
293         default:
294                 direction = SCSI_DATA_UNKNOWN;
295                 break;
296         }
297
298         return direction;
299 }
300
301 #endif
302
303 /*
304  *  Driver host data structure.
305  */
306 struct host_data {
307      hcb_p ncb;
308 };
309
310 /*
311  * Some type that fit DMA addresses as seen from BUS.
312  */
313 #ifndef SYM_LINUX_DYNAMIC_DMA_MAPPING
314 typedef u_long          bus_addr_t;
315 #else
316 #if     SYM_CONF_DMA_ADDRESSING_MODE > 0
317 typedef dma64_addr_t    bus_addr_t;
318 #else
319 typedef dma_addr_t      bus_addr_t;
320 #endif
321 #endif
322
323 /*
324  *  Used by the eh thread to wait for command completion.
325  *  It is allocated on the eh thread stack.
326  */
327 struct sym_eh_wait {
328         struct semaphore sem;
329         struct timer_list timer;
330         void (*old_done)(Scsi_Cmnd *);
331         int to_do;
332         int timed_out;
333 };
334
335 /*
336  *  Driver private area in the SCSI command structure.
337  */
338 struct sym_ucmd {               /* Override the SCSI pointer structure */
339         SYM_QUEHEAD link_cmdq;  /* Must stay at offset ZERO */
340 #ifdef SYM_LINUX_DYNAMIC_DMA_MAPPING
341         bus_addr_t data_mapping;
342         u_char  data_mapped;
343 #endif
344         struct sym_eh_wait *eh_wait;
345 };
346
347 typedef struct sym_ucmd *ucmd_p;
348
349 #define SYM_UCMD_PTR(cmd)  ((ucmd_p)(&(cmd)->SCp))
350 #define SYM_SCMD_PTR(ucmd) sym_que_entry(ucmd, Scsi_Cmnd, SCp)
351 #define SYM_SOFTC_PTR(cmd) (((struct host_data *)cmd->host->hostdata)->ncb)
352
353 /*
354  *  Deal with DMA mapping/unmapping.
355  */
356
357 #ifndef SYM_LINUX_DYNAMIC_DMA_MAPPING
358
359 /* Linux versions prior to pci bus iommu kernel interface */
360
361 #define __unmap_scsi_data(pdev, cmd)    do {; } while (0)
362 #define __map_scsi_single_data(pdev, cmd) (__vtobus(pdev,(cmd)->request_buffer))
363 #define __map_scsi_sg_data(pdev, cmd)   ((cmd)->use_sg)
364 #define __sync_scsi_data(pdev, cmd)     do {; } while (0)
365
366 #define bus_sg_dma_address(sc)          vtobus((sc)->address)
367 #define bus_sg_dma_len(sc)              ((sc)->length)
368
369 #else /* Linux version with pci bus iommu kernel interface */
370
371 #define bus_unmap_sg(pdev, sgptr, sgcnt, dir)           \
372         pci_unmap_sg(pdev, sgptr, sgcnt, dir)
373
374 #define bus_unmap_single(pdev, mapping, bufptr, dir)    \
375         pci_unmap_single(pdev, mapping, bufptr, dir)
376
377 #define bus_map_single(pdev, bufptr, bufsiz, dir)       \
378         pci_map_single(pdev, bufptr, bufsiz, dir)
379  
380 #define bus_map_sg(pdev, sgptr, sgcnt, dir)             \
381         pci_map_sg(pdev, sgptr, sgcnt, dir)
382
383 #define bus_dma_sync_sg(pdev, sgptr, sgcnt, dir)        \
384         pci_dma_sync_sg(pdev, sgptr, sgcnt, dir)
385
386 #define bus_dma_sync_single(pdev, mapping, bufsiz, dir) \
387         pci_dma_sync_single(pdev, mapping, bufsiz, dir)
388
389 #define bus_sg_dma_address(sc)  sg_dma_address(sc)
390 #define bus_sg_dma_len(sc)      sg_dma_len(sc)
391
392 static void __unmap_scsi_data(pcidev_t pdev, Scsi_Cmnd *cmd)
393 {
394         int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
395
396         switch(SYM_UCMD_PTR(cmd)->data_mapped) {
397         case 2:
398                 bus_unmap_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
399                 break;
400         case 1:
401                 bus_unmap_single(pdev, SYM_UCMD_PTR(cmd)->data_mapping,
402                                  cmd->request_bufflen, dma_dir);
403                 break;
404         }
405         SYM_UCMD_PTR(cmd)->data_mapped = 0;
406 }
407
408 static bus_addr_t __map_scsi_single_data(pcidev_t pdev, Scsi_Cmnd *cmd)
409 {
410         bus_addr_t mapping;
411         int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
412
413         mapping = bus_map_single(pdev, cmd->request_buffer,
414                                  cmd->request_bufflen, dma_dir);
415         if (mapping) {
416                 SYM_UCMD_PTR(cmd)->data_mapped  = 1;
417                 SYM_UCMD_PTR(cmd)->data_mapping = mapping;
418         }
419
420         return mapping;
421 }
422
423 static int __map_scsi_sg_data(pcidev_t pdev, Scsi_Cmnd *cmd)
424 {
425         int use_sg;
426         int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
427
428         use_sg = bus_map_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
429         if (use_sg > 0) {
430                 SYM_UCMD_PTR(cmd)->data_mapped  = 2;
431                 SYM_UCMD_PTR(cmd)->data_mapping = use_sg;
432         }
433
434         return use_sg;
435 }
436
437 static void __sync_scsi_data(pcidev_t pdev, Scsi_Cmnd *cmd)
438 {
439         int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
440
441         switch(SYM_UCMD_PTR(cmd)->data_mapped) {
442         case 2:
443                 bus_dma_sync_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
444                 break;
445         case 1:
446                 bus_dma_sync_single(pdev, SYM_UCMD_PTR(cmd)->data_mapping,
447                                     cmd->request_bufflen, dma_dir);
448                 break;
449         }
450 }
451
452 #endif  /* SYM_LINUX_DYNAMIC_DMA_MAPPING */
453
454 #define unmap_scsi_data(np, cmd)        \
455                 __unmap_scsi_data(np->s.device, cmd)
456 #define map_scsi_single_data(np, cmd)   \
457                 __map_scsi_single_data(np->s.device, cmd)
458 #define map_scsi_sg_data(np, cmd)       \
459                 __map_scsi_sg_data(np->s.device, cmd)
460 #define sync_scsi_data(np, cmd)         \
461                 __sync_scsi_data(np->s.device, cmd)
462
463 /*
464  *  Complete a pending CAM CCB.
465  */
466 void sym_xpt_done(hcb_p np, Scsi_Cmnd *ccb)
467 {
468         sym_remque(&SYM_UCMD_PTR(ccb)->link_cmdq);
469         unmap_scsi_data(np, ccb);
470         ccb->scsi_done(ccb);
471 }
472
473 void sym_xpt_done2(hcb_p np, Scsi_Cmnd *ccb, int cam_status)
474 {
475         sym_set_cam_status(ccb, cam_status);
476         sym_xpt_done(np, ccb);
477 }
478
479
480 /*
481  *  Print something that identifies the IO.
482  */
483 void sym_print_addr (ccb_p cp)
484 {
485         Scsi_Cmnd *cmd = cp->cam_ccb;
486         if (cmd)
487                 printf("%s:%d:%d:", sym_name(SYM_SOFTC_PTR(cmd)),
488                        cmd->target,cmd->lun);
489 }
490
491 /*
492  *  Tell the SCSI layer about a BUS RESET.
493  */
494 void sym_xpt_async_bus_reset(hcb_p np)
495 {
496         printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
497         np->s.settle_time = ktime_get(sym_driver_setup.settle_delay * HZ);
498         np->s.settle_time_valid = 1;
499         if (sym_verbose >= 2)
500                 printf_info("%s: command processing suspended for %d seconds\n",
501                             sym_name(np), sym_driver_setup.settle_delay);
502 }
503
504 /*
505  *  Tell the SCSI layer about a BUS DEVICE RESET message sent.
506  */
507 void sym_xpt_async_sent_bdr(hcb_p np, int target)
508 {
509         printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
510 }
511
512 /*
513  *  Tell the SCSI layer about the new transfer parameters.
514  */
515 void sym_xpt_async_nego_wide(hcb_p np, int target)
516 {
517         if (sym_verbose < 3)
518                 return;
519         sym_announce_transfer_rate(np, target);
520 }
521
522 /*
523  *  Choose the more appropriate CAM status if 
524  *  the IO encountered an extended error.
525  */
526 static int sym_xerr_cam_status(int cam_status, int x_status)
527 {
528         if (x_status) {
529                 if      (x_status & XE_PARITY_ERR)
530                         cam_status = DID_PARITY;
531                 else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
532                         cam_status = DID_ERROR;
533                 else if (x_status & XE_BAD_PHASE)
534                         cam_status = DID_ERROR;
535                 else
536                         cam_status = DID_ERROR;
537         }
538         return cam_status;
539 }
540
541 /*
542  *  Build CAM result for a failed or auto-sensed IO.
543  */
544 void sym_set_cam_result_error(hcb_p np, ccb_p cp, int resid)
545 {
546         Scsi_Cmnd *csio = cp->cam_ccb;
547         u_int cam_status, scsi_status, drv_status;
548
549         drv_status  = 0;
550         cam_status  = DID_OK;
551         scsi_status = cp->ssss_status;
552
553         if (cp->host_flags & HF_SENSE) {
554                 scsi_status = cp->sv_scsi_status;
555                 resid = cp->sv_resid;
556                 if (sym_verbose && cp->sv_xerr_status)
557                         sym_print_xerr(cp, cp->sv_xerr_status);
558                 if (cp->host_status == HS_COMPLETE &&
559                     cp->ssss_status == S_GOOD &&
560                     cp->xerr_status == 0) {
561                         cam_status = sym_xerr_cam_status(DID_OK,
562                                                          cp->sv_xerr_status);
563                         drv_status = DRIVER_SENSE;
564                         /*
565                          *  Bounce back the sense data to user.
566                          */
567                         bzero(&csio->sense_buffer, sizeof(csio->sense_buffer));
568                         bcopy(cp->sns_bbuf, csio->sense_buffer,
569                               MIN(sizeof(csio->sense_buffer),SYM_SNS_BBUF_LEN));
570 #if 0
571                         /*
572                          *  If the device reports a UNIT ATTENTION condition 
573                          *  due to a RESET condition, we should consider all 
574                          *  disconnect CCBs for this unit as aborted.
575                          */
576                         if (1) {
577                                 u_char *p;
578                                 p  = (u_char *) csio->sense_data;
579                                 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
580                                         sym_clear_tasks(np, DID_ABORT,
581                                                         cp->target,cp->lun, -1);
582                         }
583 #endif
584                 }
585                 else
586                         cam_status = DID_ERROR;
587         }
588         else if (cp->host_status == HS_COMPLETE)        /* Bad SCSI status */
589                 cam_status = DID_OK;
590         else if (cp->host_status == HS_SEL_TIMEOUT)     /* Selection timeout */
591                 cam_status = DID_NO_CONNECT;
592         else if (cp->host_status == HS_UNEXPECTED)      /* Unexpected BUS FREE*/
593                 cam_status = DID_ERROR;
594         else {                                          /* Extended error */
595                 if (sym_verbose) {
596                         PRINT_ADDR(cp);
597                         printf ("COMMAND FAILED (%x %x %x).\n",
598                                 cp->host_status, cp->ssss_status,
599                                 cp->xerr_status);
600                 }
601                 /*
602                  *  Set the most appropriate value for CAM status.
603                  */
604                 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
605         }
606 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,3,99)
607         csio->resid = resid;
608 #endif
609         csio->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
610 }
611
612
613 /*
614  *  Called on successfull INQUIRY response.
615  */
616 void sym_sniff_inquiry(hcb_p np, Scsi_Cmnd *cmd, int resid)
617 {
618         int retv;
619
620         if (!cmd || cmd->use_sg)
621                 return;
622
623         sync_scsi_data(np, cmd);
624         retv = __sym_sniff_inquiry(np, cmd->target, cmd->lun,
625                                    (u_char *) cmd->request_buffer,
626                                    cmd->request_bufflen - resid);
627         if (retv < 0)
628                 return;
629         else if (retv)
630                 sym_update_trans_settings(np, &np->target[cmd->target]);
631 }
632
633 /*
634  *  Build the scatter/gather array for an I/O.
635  */
636
637 static int sym_scatter_no_sglist(hcb_p np, ccb_p cp, Scsi_Cmnd *cmd)
638 {
639         struct sym_tblmove *data = &cp->phys.data[SYM_CONF_MAX_SG-1];
640         int segment;
641
642         cp->data_len = cmd->request_bufflen;
643
644         if (cmd->request_bufflen) {
645                 bus_addr_t baddr = map_scsi_single_data(np, cmd);
646                 if (baddr) {
647                         sym_build_sge(np, data, baddr, cmd->request_bufflen);
648                         segment = 1;
649                 }
650                 else
651                         segment = -2;
652         }
653         else
654                 segment = 0;
655
656         return segment;
657 }
658
659 static int sym_scatter(hcb_p np, ccb_p cp, Scsi_Cmnd *cmd)
660 {
661         int segment;
662         int use_sg = (int) cmd->use_sg;
663
664         cp->data_len = 0;
665
666         if (!use_sg)
667                 segment = sym_scatter_no_sglist(np, cp, cmd);
668         else if (use_sg > SYM_CONF_MAX_SG)
669                 segment = -1;
670         else if ((use_sg = map_scsi_sg_data(np, cmd)) > 0) {
671                 struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
672                 struct sym_tblmove *data;
673
674                 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
675
676                 for (segment = 0; segment < use_sg; segment++) {
677                         bus_addr_t baddr = bus_sg_dma_address(&scatter[segment]);
678                         unsigned int len = bus_sg_dma_len(&scatter[segment]);
679
680                         sym_build_sge(np, &data[segment], baddr, len);
681                         cp->data_len += len;
682                 }
683         }
684         else
685                 segment = -2;
686
687         return segment;
688 }
689
690 /*
691  *  Queue a SCSI command.
692  */
693 static int sym_queue_command(hcb_p np, Scsi_Cmnd *ccb)
694 {
695 /*      Scsi_Device        *device    = ccb->device; */
696         tcb_p   tp;
697         lcb_p   lp;
698         ccb_p   cp;
699         int     order;
700
701         /*
702          *  Minimal checkings, so that we will not 
703          *  go outside our tables.
704          */
705         if (ccb->target == np->myaddr ||
706             ccb->target >= SYM_CONF_MAX_TARGET ||
707             ccb->lun    >= SYM_CONF_MAX_LUN) {
708                 sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
709                 return 0;
710         }
711
712         /*
713          *  Retreive the target descriptor.
714          */
715         tp = &np->target[ccb->target];
716
717         /*
718          *  Complete the 1st INQUIRY command with error 
719          *  condition if the device is flagged NOSCAN 
720          *  at BOOT in the NVRAM. This may speed up 
721          *  the boot and maintain coherency with BIOS 
722          *  device numbering. Clearing the flag allows 
723          *  user to rescan skipped devices later.
724          *  We also return error for devices not flagged 
725          *  for SCAN LUNS in the NVRAM since some mono-lun 
726          *  devices behave badly when asked for some non 
727          *  zero LUN. Btw, this is an absolute hack.:-)
728          */
729         if (ccb->cmnd[0] == 0x12 || ccb->cmnd[0] == 0x0) {
730                 if ((tp->usrflags & SYM_SCAN_BOOT_DISABLED) ||
731                     ((tp->usrflags & SYM_SCAN_LUNS_DISABLED) && 
732                      ccb->lun != 0)) {
733                         tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
734                         sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
735                         return 0;
736                 }
737         }
738
739         /*
740          *  Select tagged/untagged.
741          */
742         lp = sym_lp(np, tp, ccb->lun);
743         order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
744
745         /*
746          *  Queue the SCSI IO.
747          */
748         cp = sym_get_ccb(np, ccb->target, ccb->lun, order);
749         if (!cp)
750                 return 1;       /* Means resource shortage */
751         (void) sym_queue_scsiio(np, ccb, cp);
752         return 0;
753 }
754
755 /*
756  *  Setup buffers and pointers that address the CDB.
757  */
758 static int __inline sym_setup_cdb(hcb_p np, Scsi_Cmnd *ccb, ccb_p cp)
759 {
760         u32     cmd_ba;
761         int     cmd_len;
762
763         /*
764          *  CDB is 16 bytes max.
765          */
766         if (ccb->cmd_len > sizeof(cp->cdb_buf)) {
767                 sym_set_cam_status(cp->cam_ccb, CAM_REQ_INVALID);
768                 return -1;
769         }
770
771         bcopy(ccb->cmnd, cp->cdb_buf, ccb->cmd_len);
772         cmd_ba  = CCB_BA (cp, cdb_buf[0]);
773         cmd_len = ccb->cmd_len;
774
775         cp->phys.cmd.addr       = cpu_to_scr(cmd_ba);
776         cp->phys.cmd.size       = cpu_to_scr(cmd_len);
777
778         return 0;
779 }
780
781 /*
782  *  Setup pointers that address the data and start the I/O.
783  */
784 int sym_setup_data_and_start(hcb_p np, Scsi_Cmnd *csio, ccb_p cp)
785 {
786         int dir;
787         tcb_p tp = &np->target[cp->target];
788         lcb_p lp = sym_lp(np, tp, cp->lun);
789
790         /*
791          *  Build the CDB.
792          */
793         if (sym_setup_cdb(np, csio, cp))
794                 goto out_abort;
795
796         /*
797          *  No direction means no data.
798          */
799         dir = scsi_data_direction(csio);
800         if (dir != SCSI_DATA_NONE) {
801                 cp->segments = sym_scatter (np, cp, csio);
802                 if (cp->segments < 0) {
803                         if (cp->segments == -2)
804                                 sym_set_cam_status(csio, CAM_RESRC_UNAVAIL);
805                         else
806                                 sym_set_cam_status(csio, CAM_REQ_TOO_BIG);
807                         goto out_abort;
808                 }
809         }
810         else {
811                 cp->data_len = 0;
812                 cp->segments = 0;
813         }
814
815         /*
816          *  Set data pointers.
817          */
818         sym_setup_data_pointers(np, cp, dir);
819
820         /*
821          *  When `#ifed 1', the code below makes the driver 
822          *  panic on the first attempt to write to a SCSI device.
823          *  It is the first test we want to do after a driver 
824          *  change that does not seem obviously safe. :)
825          */
826 #if 0
827         switch (cp->cdb_buf[0]) {
828         case 0x0A: case 0x2A: case 0xAA:
829                 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
830                 MDELAY(10000);
831                 break;
832         default:
833                 break;
834         }
835 #endif
836
837         /*
838          *      activate this job.
839          */
840         if (lp)
841                 sym_start_next_ccbs(np, lp, 2);
842         else
843                 sym_put_start_queue(np, cp);
844         return 0;
845
846 out_abort:
847         sym_free_ccb(np, cp);
848         sym_xpt_done(np, csio);
849         return 0;
850 }
851
852
853 /*
854  *  timer daemon.
855  *
856  *  Misused to keep the driver running when
857  *  interrupts are not configured correctly.
858  */
859 static void sym_timer (hcb_p np)
860 {
861         u_long  thistime = ktime_get(0);
862
863 #if LINUX_VERSION_CODE < LinuxVersionCode(2, 4, 0)
864         /*
865          *  If release process in progress, let's go
866          *  Set the release stage from 1 to 2 to synchronize
867          *  with the release process.
868          */
869
870         if (np->s.release_stage) {
871                 if (np->s.release_stage == 1)
872                         np->s.release_stage = 2;
873                 return;
874         }
875 #endif
876
877         /*
878          *  Restart the timer.
879          */
880 #ifdef SYM_CONF_PCIQ_BROKEN_INTR
881         np->s.timer.expires = ktime_get((HZ+99)/100);
882 #else
883         np->s.timer.expires = ktime_get(SYM_CONF_TIMER_INTERVAL);
884 #endif
885         add_timer(&np->s.timer);
886
887         /*
888          *  If we are resetting the ncr, wait for settle_time before 
889          *  clearing it. Then command processing will be resumed.
890          */
891         if (np->s.settle_time_valid) {
892                 if (ktime_dif(np->s.settle_time, thistime) <= 0){
893                         if (sym_verbose >= 2 )
894                                 printk("%s: command processing resumed\n",
895                                        sym_name(np));
896                         np->s.settle_time_valid = 0;
897                 }
898                 return;
899         }
900
901         /*
902          *      Nothing to do for now, but that may come.
903          */
904         if (np->s.lasttime + 4*HZ < thistime) {
905                 np->s.lasttime = thistime;
906         }
907
908 #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
909         /*
910          *  Some way-broken PCI bridges may lead to 
911          *  completions being lost when the clearing 
912          *  of the INTFLY flag by the CPU occurs 
913          *  concurrently with the chip raising this flag.
914          *  If this ever happen, lost completions will 
915          * be reaped here.
916          */
917         sym_wakeup_done(np);
918 #endif
919
920 #ifdef SYM_CONF_PCIQ_BROKEN_INTR
921         if (INB(nc_istat) & (INTF|SIP|DIP)) {
922
923                 /*
924                 **      Process pending interrupts.
925                 */
926                 if (DEBUG_FLAGS & DEBUG_TINY) printk ("{");
927                 sym_interrupt(np);
928                 if (DEBUG_FLAGS & DEBUG_TINY) printk ("}");
929         }
930 #endif /* SYM_CONF_PCIQ_BROKEN_INTR */
931 }
932
933
934 /*
935  *  PCI BUS error handler.
936  */
937 void sym_log_bus_error(hcb_p np)
938 {
939         u_short pci_sts;
940         pci_read_config_word(np->s.device, PCI_STATUS, &pci_sts);
941         if (pci_sts & 0xf900) {
942                 pci_write_config_word(np->s.device, PCI_STATUS,
943                                          pci_sts);
944                 printf("%s: PCI STATUS = 0x%04x\n",
945                         sym_name(np), pci_sts & 0xf900);
946         }
947 }
948
949
950 /*
951  *  Requeue awaiting commands.
952  */
953 static void sym_requeue_awaiting_cmds(hcb_p np)
954 {
955         Scsi_Cmnd *cmd;
956         ucmd_p ucp = SYM_UCMD_PTR(cmd);
957         SYM_QUEHEAD tmp_cmdq;
958         int sts;
959
960         sym_que_move(&np->s.wait_cmdq, &tmp_cmdq);
961
962         while ((ucp = (ucmd_p) sym_remque_head(&tmp_cmdq)) != 0) {
963                 sym_insque_tail(&ucp->link_cmdq, &np->s.busy_cmdq);
964                 cmd = SYM_SCMD_PTR(ucp);
965                 sts = sym_queue_command(np, cmd);
966                 if (sts) {
967                         sym_remque(&ucp->link_cmdq);
968                         sym_insque_head(&ucp->link_cmdq, &np->s.wait_cmdq);
969                 }
970         }
971 }
972
973 /*
974  *  Linux entry point of the queuecommand() function
975  */
976 int sym53c8xx_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
977 {
978         hcb_p  np  = SYM_SOFTC_PTR(cmd);
979         ucmd_p ucp = SYM_UCMD_PTR(cmd);
980         u_long flags;
981         int sts = 0;
982
983         cmd->scsi_done     = done;
984         cmd->host_scribble = NULL;
985         memset(ucp, 0, sizeof(*ucp));
986
987         SYM_LOCK_HCB(np, flags);
988
989         /*
990          *  Shorten our settle_time if needed for 
991          *  this command not to time out.
992          */
993         if (np->s.settle_time_valid && cmd->timeout_per_command) {
994                 u_long tlimit = ktime_get(cmd->timeout_per_command);
995                 tlimit = ktime_sub(tlimit, SYM_CONF_TIMER_INTERVAL*2);
996                 if (ktime_dif(np->s.settle_time, tlimit) > 0) {
997                         np->s.settle_time = tlimit;
998                 }
999         }
1000
1001         if (np->s.settle_time_valid || !sym_que_empty(&np->s.wait_cmdq)) {
1002                 sym_insque_tail(&ucp->link_cmdq, &np->s.wait_cmdq);
1003                 goto out;
1004         }
1005
1006         sym_insque_tail(&ucp->link_cmdq, &np->s.busy_cmdq);
1007         sts = sym_queue_command(np, cmd);
1008         if (sts) {
1009                 sym_remque(&ucp->link_cmdq);
1010                 sym_insque_tail(&ucp->link_cmdq, &np->s.wait_cmdq);
1011         }
1012 out:
1013         SYM_UNLOCK_HCB(np, flags);
1014
1015         return 0;
1016 }
1017
1018 /*
1019  *  Linux entry point of the interrupt handler.
1020  */
1021 static void sym53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
1022 {
1023         unsigned long flags;
1024         unsigned long flags1;
1025         hcb_p np = (hcb_p) dev_id;
1026
1027         if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
1028
1029         SYM_LOCK_SCSI(np, flags1);
1030         SYM_LOCK_HCB(np, flags);
1031
1032         sym_interrupt(np);
1033
1034         if (!sym_que_empty(&np->s.wait_cmdq) && !np->s.settle_time_valid)
1035                 sym_requeue_awaiting_cmds(np);
1036
1037         SYM_UNLOCK_HCB(np, flags);
1038         SYM_UNLOCK_SCSI(np, flags1);
1039
1040         if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
1041 }
1042
1043 /*
1044  *  Linux entry point of the timer handler
1045  */
1046 static void sym53c8xx_timer(unsigned long npref)
1047 {
1048         hcb_p np = (hcb_p) npref;
1049         unsigned long flags;
1050         unsigned long flags1;
1051
1052         SYM_LOCK_SCSI(np, flags1);
1053         SYM_LOCK_HCB(np, flags);
1054
1055         sym_timer(np);
1056
1057         if (!sym_que_empty(&np->s.wait_cmdq) && !np->s.settle_time_valid)
1058                 sym_requeue_awaiting_cmds(np);
1059
1060         SYM_UNLOCK_HCB(np, flags);
1061         SYM_UNLOCK_SCSI(np, flags1);
1062 }
1063
1064
1065 /*
1066  *  What the eh thread wants us to perform.
1067  */
1068 #define SYM_EH_ABORT            0
1069 #define SYM_EH_DEVICE_RESET     1
1070 #define SYM_EH_BUS_RESET        2
1071 #define SYM_EH_HOST_RESET       3
1072
1073 /*
1074  *  What we will do regarding the involved SCSI command.
1075  */
1076 #define SYM_EH_DO_IGNORE        0
1077 #define SYM_EH_DO_COMPLETE      1
1078 #define SYM_EH_DO_WAIT          2
1079
1080 /*
1081  *  Our general completion handler.
1082  */
1083 static void __sym_eh_done(Scsi_Cmnd *cmd, int timed_out)
1084 {
1085         struct sym_eh_wait *ep = SYM_UCMD_PTR(cmd)->eh_wait;
1086         if (!ep)
1087                 return;
1088
1089         /* Try to avoid a race here (not 100% safe) */
1090         if (!timed_out) {
1091                 ep->timed_out = 0;
1092                 if (ep->to_do == SYM_EH_DO_WAIT && !del_timer(&ep->timer))
1093                         return;
1094         }
1095
1096         /* Revert everything */
1097         SYM_UCMD_PTR(cmd)->eh_wait = 0;
1098         cmd->scsi_done = ep->old_done;
1099
1100         /* Wake up the eh thread if it wants to sleep */
1101         if (ep->to_do == SYM_EH_DO_WAIT)
1102                 up(&ep->sem);
1103 }
1104
1105 /*
1106  *  scsi_done() alias when error recovery is in progress. 
1107  */
1108 static void sym_eh_done(Scsi_Cmnd *cmd) { __sym_eh_done(cmd, 0); }
1109
1110 /*
1111  *  Some timeout handler to avoid waiting too long.
1112  */
1113 static void sym_eh_timeout(u_long p) { __sym_eh_done((Scsi_Cmnd *)p, 1); }
1114
1115 /*
1116  *  Generic method for our eh processing.
1117  *  The 'op' argument tells what we have to do.
1118  */
1119 static int sym_eh_handler(int op, char *opname, Scsi_Cmnd *cmd)
1120 {
1121         hcb_p np = SYM_SOFTC_PTR(cmd);
1122         unsigned long flags;
1123         SYM_QUEHEAD *qp;
1124         int to_do = SYM_EH_DO_IGNORE;
1125         int sts = -1;
1126         struct sym_eh_wait eh, *ep = &eh;
1127         char devname[20];
1128
1129         sprintf(devname, "%s:%d:%d", sym_name(np), cmd->target, cmd->lun);
1130
1131         printf_warning("%s: %s operation started.\n", devname, opname);
1132
1133         SYM_LOCK_HCB(np, flags);
1134
1135 #if 0
1136         /* This one should be the result of some race, thus to ignore */
1137         if (cmd->serial_number != cmd->serial_number_at_timeout)
1138                 goto prepare;
1139 #endif
1140
1141         /* This one is not queued to the core driver -> to complete here */ 
1142         FOR_EACH_QUEUED_ELEMENT(&np->s.wait_cmdq, qp) {
1143                 if (SYM_SCMD_PTR(qp) == cmd) {
1144                         to_do = SYM_EH_DO_COMPLETE;
1145                         goto prepare;
1146                 }
1147         }
1148
1149         /* This one is queued in some place -> to wait for completion */
1150         FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
1151                 ccb_p cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
1152                 if (cp->cam_ccb == cmd) {
1153                         to_do = SYM_EH_DO_WAIT;
1154                         goto prepare;
1155                 }
1156         }
1157
1158 prepare:
1159         /* Prepare stuff to either ignore, complete or wait for completion */
1160         switch(to_do) {
1161         default:
1162         case SYM_EH_DO_IGNORE:
1163                 break;
1164         case SYM_EH_DO_WAIT:
1165 #if LINUX_VERSION_CODE > LinuxVersionCode(2,3,0)
1166                 init_MUTEX_LOCKED(&ep->sem);
1167 #else
1168                 ep->sem = MUTEX_LOCKED;
1169 #endif
1170                 /* fall through */
1171         case SYM_EH_DO_COMPLETE:
1172                 ep->old_done = cmd->scsi_done;
1173                 cmd->scsi_done = sym_eh_done;
1174                 SYM_UCMD_PTR(cmd)->eh_wait = ep;
1175         }
1176
1177         /* Try to proceed the operation we have been asked for */
1178         sts = -1;
1179         switch(op) {
1180         case SYM_EH_ABORT:
1181                 sts = sym_abort_scsiio(np, cmd, 1);
1182                 break;
1183         case SYM_EH_DEVICE_RESET:
1184                 sts = sym_reset_scsi_target(np, cmd->target);
1185                 break;
1186         case SYM_EH_BUS_RESET:
1187                 sym_reset_scsi_bus(np, 1);
1188                 sts = 0;
1189                 break;
1190         case SYM_EH_HOST_RESET:
1191                 sym_reset_scsi_bus(np, 0);
1192                 sym_start_up (np, 1);
1193                 sts = 0;
1194                 break;
1195         default:
1196                 break;
1197         }
1198
1199         /* On error, restore everything and cross fingers :) */
1200         if (sts) {
1201                 SYM_UCMD_PTR(cmd)->eh_wait = 0;
1202                 cmd->scsi_done = ep->old_done;
1203                 to_do = SYM_EH_DO_IGNORE;
1204         }
1205
1206         ep->to_do = to_do;
1207         /* Complete the command with locks held as required by the driver */
1208         if (to_do == SYM_EH_DO_COMPLETE)
1209                 sym_xpt_done2(np, cmd, CAM_REQ_ABORTED);
1210
1211         SYM_UNLOCK_HCB(np, flags);
1212
1213         /* Wait for completion with locks released, as required by kernel */
1214         if (to_do == SYM_EH_DO_WAIT) {
1215                 init_timer(&ep->timer);
1216                 ep->timer.expires = jiffies + (5*HZ);
1217                 ep->timer.function = sym_eh_timeout;
1218                 ep->timer.data = (u_long)cmd;
1219                 ep->timed_out = 1;      /* Be pessimistic for once :) */
1220                 add_timer(&ep->timer);
1221                 SYM_UNLOCK_SCSI_NORESTORE(np);
1222                 down(&ep->sem);
1223                 SYM_LOCK_SCSI_NOSAVE(np);
1224                 if (ep->timed_out)
1225                         sts = -2;
1226         }
1227         printf_warning("%s: %s operation %s.\n", devname, opname,
1228                         sts==0?"complete":sts==-2?"timed-out":"failed");
1229         return sts? SCSI_FAILED : SCSI_SUCCESS;
1230 }
1231
1232
1233 /*
1234  * Error handlers called from the eh thread (one thread per HBA).
1235  */
1236 int sym53c8xx_eh_abort_handler(Scsi_Cmnd *cmd)
1237 {
1238         return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
1239 }
1240
1241 int sym53c8xx_eh_device_reset_handler(Scsi_Cmnd *cmd)
1242 {
1243         return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
1244 }
1245
1246 int sym53c8xx_eh_bus_reset_handler(Scsi_Cmnd *cmd)
1247 {
1248         return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
1249 }
1250
1251 int sym53c8xx_eh_host_reset_handler(Scsi_Cmnd *cmd)
1252 {
1253         return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
1254 }
1255
1256 /*
1257  *  Tune device queuing depth, according to various limits.
1258  */
1259 static void 
1260 sym_tune_dev_queuing(hcb_p np, int target, int lun, u_short reqtags)
1261 {
1262         tcb_p   tp = &np->target[target];
1263         lcb_p   lp = sym_lp(np, tp, lun);
1264         u_short oldtags;
1265
1266         if (!lp)
1267                 return;
1268
1269         oldtags = lp->s.reqtags;
1270
1271         if (reqtags > lp->s.scdev_depth)
1272                 reqtags = lp->s.scdev_depth;
1273
1274         lp->started_limit = reqtags ? reqtags : 2;
1275         lp->started_max   = 1;
1276         lp->s.reqtags     = reqtags;
1277
1278         if (reqtags != oldtags) {
1279                 printf_info("%s:%d:%d: "
1280                          "tagged command queuing %s, command queue depth %d.\n",
1281                           sym_name(np), target, lun,
1282                           lp->s.reqtags ? "enabled" : "disabled",
1283                           lp->started_limit);
1284         }
1285 }
1286
1287 #ifdef  SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT
1288 /*
1289  *  Linux select queue depths function
1290  */
1291 #define DEF_DEPTH       (sym_driver_setup.max_tag)
1292 #define ALL_TARGETS     -2
1293 #define NO_TARGET       -1
1294 #define ALL_LUNS        -2
1295 #define NO_LUN          -1
1296
1297 static int device_queue_depth(hcb_p np, int target, int lun)
1298 {
1299         int c, h, t, u, v;
1300         char *p = sym_driver_setup.tag_ctrl;
1301         char *ep;
1302
1303         h = -1;
1304         t = NO_TARGET;
1305         u = NO_LUN;
1306         while ((c = *p++) != 0) {
1307                 v = simple_strtoul(p, &ep, 0);
1308                 switch(c) {
1309                 case '/':
1310                         ++h;
1311                         t = ALL_TARGETS;
1312                         u = ALL_LUNS;
1313                         break;
1314                 case 't':
1315                         if (t != target)
1316                                 t = (target == v) ? v : NO_TARGET;
1317                         u = ALL_LUNS;
1318                         break;
1319                 case 'u':
1320                         if (u != lun)
1321                                 u = (lun == v) ? v : NO_LUN;
1322                         break;
1323                 case 'q':
1324                         if (h == np->s.unit &&
1325                                 (t == ALL_TARGETS || t == target) &&
1326                                 (u == ALL_LUNS    || u == lun))
1327                                 return v;
1328                         break;
1329                 case '-':
1330                         t = ALL_TARGETS;
1331                         u = ALL_LUNS;
1332                         break;
1333                 default:
1334                         break;
1335                 }
1336                 p = ep;
1337         }
1338         return DEF_DEPTH;
1339 }
1340 #else
1341 #define device_queue_depth(np, t, l)    (sym_driver_setup.max_tag)
1342 #endif  /* SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT */
1343
1344 /*
1345  * Linux entry point for device queue sizing.
1346  */
1347 static void 
1348 sym53c8xx_select_queue_depths(struct Scsi_Host *host, 
1349                               struct scsi_device *devlist)
1350 {
1351         struct scsi_device *device;
1352
1353         for (device = devlist; device; device = device->next) {
1354                 hcb_p np;
1355                 tcb_p tp;
1356                 lcb_p lp;
1357                 int reqtags;
1358
1359                 if (device->host != host)
1360                         continue;
1361
1362                 np = ((struct host_data *) host->hostdata)->ncb;
1363                 tp = &np->target[device->id];
1364
1365                 /*
1366                  *  Get user settings for transfer parameters.
1367                  */
1368                 tp->inq_byte7_valid = (INQ7_SYNC|INQ7_WIDE16);
1369                 sym_update_trans_settings(np, tp);
1370
1371                 /*
1372                  *  Allocate the LCB if not yet.
1373                  *  If it fail, we may well be in the sh*t. :)
1374                  */
1375                 lp = sym_alloc_lcb(np, device->id, device->lun);
1376                 if (!lp) {
1377                         device->queue_depth = 1;
1378                         continue;
1379                 }
1380
1381                 /*
1382                  *  Get user flags.
1383                  */
1384                 lp->curr_flags = lp->user_flags;
1385
1386                 /*
1387                  *  Select queue depth from driver setup.
1388                  *  Donnot use more than configured by user.
1389                  *  Use at least 2.
1390                  *  Donnot use more than our maximum.
1391                  */
1392                 reqtags = device_queue_depth(np, device->id, device->lun);
1393                 if (reqtags > tp->usrtags)
1394                         reqtags = tp->usrtags;
1395                 if (!device->tagged_supported)
1396                         reqtags = 0;
1397 #if 1 /* Avoid to locally queue commands for no good reasons */
1398                 if (reqtags > SYM_CONF_MAX_TAG)
1399                         reqtags = SYM_CONF_MAX_TAG;
1400                 device->queue_depth = reqtags ? reqtags : 2;
1401 #else
1402                 device->queue_depth = reqtags ? SYM_CONF_MAX_TAG : 2;
1403 #endif
1404                 lp->s.scdev_depth = device->queue_depth;
1405                 sym_tune_dev_queuing(np, device->id, device->lun, reqtags);
1406         }
1407 }
1408
1409 /*
1410  *  Linux entry point for info() function
1411  */
1412 const char *sym53c8xx_info (struct Scsi_Host *host)
1413 {
1414         return sym_driver_name();
1415 }
1416
1417
1418 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1419 /*
1420  *  Proc file system stuff
1421  *
1422  *  A read operation returns adapter information.
1423  *  A write operation is a control command.
1424  *  The string is parsed in the driver code and the command is passed 
1425  *  to the sym_usercmd() function.
1426  */
1427
1428 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1429
1430 struct  sym_usrcmd {
1431         u_long  target;
1432         u_long  lun;
1433         u_long  data;
1434         u_long  cmd;
1435 };
1436
1437 #define UC_SETSYNC      10
1438 #define UC_SETTAGS      11
1439 #define UC_SETDEBUG     12
1440 #define UC_SETWIDE      14
1441 #define UC_SETFLAG      15
1442 #define UC_SETVERBOSE   17
1443 #define UC_RESETDEV     18
1444 #define UC_CLEARDEV     19
1445
1446 static void sym_exec_user_command (hcb_p np, struct sym_usrcmd *uc)
1447 {
1448         tcb_p tp;
1449         int t, l;
1450
1451         switch (uc->cmd) {
1452         case 0: return;
1453
1454 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1455         case UC_SETDEBUG:
1456                 sym_debug_flags = uc->data;
1457                 break;
1458 #endif
1459         case UC_SETVERBOSE:
1460                 np->verbose = uc->data;
1461                 break;
1462         default:
1463                 /*
1464                  * We assume that other commands apply to targets.
1465                  * This should always be the case and avoid the below 
1466                  * 4 lines to be repeated 6 times.
1467                  */
1468                 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
1469                         if (!((uc->target >> t) & 1))
1470                                 continue;
1471                         tp = &np->target[t];
1472
1473                         switch (uc->cmd) {
1474
1475                         case UC_SETSYNC:
1476                                 if (!uc->data || uc->data >= 255) {
1477                                         tp->tinfo.goal.options = 0;
1478                                         tp->tinfo.goal.offset  = 0;
1479                                         break;
1480                                 }
1481                                 if (uc->data <= 9 && np->minsync_dt &&
1482                                     np->scsi_mode == SMODE_LVD) {
1483                                         if (uc->data < np->minsync_dt)
1484                                                 uc->data = np->minsync_dt;
1485                                         tp->tinfo.goal.options = PPR_OPT_DT;
1486                                         tp->tinfo.goal.width   = 1;
1487                                         tp->tinfo.goal.period = uc->data;
1488                                         tp->tinfo.goal.offset = np->maxoffs_dt;
1489                                 }
1490                                 else {
1491                                         if (uc->data < np->minsync)
1492                                                 uc->data = np->minsync;
1493                                         tp->tinfo.goal.options = 0;
1494                                         tp->tinfo.goal.period = uc->data;
1495                                         tp->tinfo.goal.offset = np->maxoffs;
1496                                 }
1497                                 break;
1498                         case UC_SETWIDE:
1499                                 tp->tinfo.goal.width = uc->data ? 1 : 0;
1500                                 break;
1501                         case UC_SETTAGS:
1502                                 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
1503                                         sym_tune_dev_queuing(np, t,l, uc->data);
1504                                 break;
1505                         case UC_RESETDEV:
1506                                 tp->to_reset = 1;
1507                                 np->istat_sem = SEM;
1508                                 OUTB (nc_istat, SIGP|SEM);
1509                                 break;
1510                         case UC_CLEARDEV:
1511                                 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
1512                                         lcb_p lp = sym_lp(np, tp, l);
1513                                         if (lp) lp->to_clear = 1;
1514                                 }
1515                                 np->istat_sem = SEM;
1516                                 OUTB (nc_istat, SIGP|SEM);
1517                                 break;
1518                         case UC_SETFLAG:
1519                                 tp->usrflags = uc->data;
1520                                 break;
1521                         }
1522                 }
1523                 break;
1524         }
1525 }
1526
1527 #define is_digit(c)     ((c) >= '0' && (c) <= '9')
1528 #define digit_to_bin(c) ((c) - '0')
1529 #define is_space(c)     ((c) == ' ' || (c) == '\t')
1530
1531 static int skip_spaces(char *ptr, int len)
1532 {
1533         int cnt, c;
1534
1535         for (cnt = len; cnt > 0 && (c = *ptr++) && is_space(c); cnt--);
1536
1537         return (len - cnt);
1538 }
1539
1540 static int get_int_arg(char *ptr, int len, u_long *pv)
1541 {
1542         int     cnt, c;
1543         u_long  v;
1544
1545         for (v = 0, cnt = len; cnt > 0 && (c = *ptr++) && is_digit(c); cnt--) {
1546                 v = (v * 10) + digit_to_bin(c);
1547         }
1548
1549         if (pv)
1550                 *pv = v;
1551
1552         return (len - cnt);
1553 }
1554
1555 static int is_keyword(char *ptr, int len, char *verb)
1556 {
1557         int verb_len = strlen(verb);
1558
1559         if (len >= strlen(verb) && !memcmp(verb, ptr, verb_len))
1560                 return verb_len;
1561         else
1562                 return 0;
1563
1564 }
1565
1566 #define SKIP_SPACES(min_spaces)                                         \
1567         if ((arg_len = skip_spaces(ptr, len)) < (min_spaces))           \
1568                 return -EINVAL;                                         \
1569         ptr += arg_len; len -= arg_len;
1570
1571 #define GET_INT_ARG(v)                                                  \
1572         if (!(arg_len = get_int_arg(ptr, len, &(v))))                   \
1573                 return -EINVAL;                                         \
1574         ptr += arg_len; len -= arg_len;
1575
1576
1577 /*
1578  * Parse a control command
1579  */
1580
1581 static int sym_user_command(hcb_p np, char *buffer, int length)
1582 {
1583         char *ptr       = buffer;
1584         int len         = length;
1585         struct sym_usrcmd cmd, *uc = &cmd;
1586         int             arg_len;
1587         u_long          target;
1588
1589         bzero(uc, sizeof(*uc));
1590
1591         if (len > 0 && ptr[len-1] == '\n')
1592                 --len;
1593
1594         if      ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1595                 uc->cmd = UC_SETSYNC;
1596         else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1597                 uc->cmd = UC_SETTAGS;
1598         else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1599                 uc->cmd = UC_SETVERBOSE;
1600         else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1601                 uc->cmd = UC_SETWIDE;
1602 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1603         else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1604                 uc->cmd = UC_SETDEBUG;
1605 #endif
1606         else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1607                 uc->cmd = UC_SETFLAG;
1608         else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1609                 uc->cmd = UC_RESETDEV;
1610         else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1611                 uc->cmd = UC_CLEARDEV;
1612         else
1613                 arg_len = 0;
1614
1615 #ifdef DEBUG_PROC_INFO
1616 printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1617 #endif
1618
1619         if (!arg_len)
1620                 return -EINVAL;
1621         ptr += arg_len; len -= arg_len;
1622
1623         switch(uc->cmd) {
1624         case UC_SETSYNC:
1625         case UC_SETTAGS:
1626         case UC_SETWIDE:
1627         case UC_SETFLAG:
1628         case UC_RESETDEV:
1629         case UC_CLEARDEV:
1630                 SKIP_SPACES(1);
1631                 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1632                         ptr += arg_len; len -= arg_len;
1633                         uc->target = ~0;
1634                 } else {
1635                         GET_INT_ARG(target);
1636                         uc->target = (1<<target);
1637 #ifdef DEBUG_PROC_INFO
1638 printk("sym_user_command: target=%ld\n", target);
1639 #endif
1640                 }
1641                 break;
1642         }
1643
1644         switch(uc->cmd) {
1645         case UC_SETVERBOSE:
1646         case UC_SETSYNC:
1647         case UC_SETTAGS:
1648         case UC_SETWIDE:
1649                 SKIP_SPACES(1);
1650                 GET_INT_ARG(uc->data);
1651 #ifdef DEBUG_PROC_INFO
1652 printk("sym_user_command: data=%ld\n", uc->data);
1653 #endif
1654                 break;
1655 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1656         case UC_SETDEBUG:
1657                 while (len > 0) {
1658                         SKIP_SPACES(1);
1659                         if      ((arg_len = is_keyword(ptr, len, "alloc")))
1660                                 uc->data |= DEBUG_ALLOC;
1661                         else if ((arg_len = is_keyword(ptr, len, "phase")))
1662                                 uc->data |= DEBUG_PHASE;
1663                         else if ((arg_len = is_keyword(ptr, len, "queue")))
1664                                 uc->data |= DEBUG_QUEUE;
1665                         else if ((arg_len = is_keyword(ptr, len, "result")))
1666                                 uc->data |= DEBUG_RESULT;
1667                         else if ((arg_len = is_keyword(ptr, len, "scatter")))
1668                                 uc->data |= DEBUG_SCATTER;
1669                         else if ((arg_len = is_keyword(ptr, len, "script")))
1670                                 uc->data |= DEBUG_SCRIPT;
1671                         else if ((arg_len = is_keyword(ptr, len, "tiny")))
1672                                 uc->data |= DEBUG_TINY;
1673                         else if ((arg_len = is_keyword(ptr, len, "timing")))
1674                                 uc->data |= DEBUG_TIMING;
1675                         else if ((arg_len = is_keyword(ptr, len, "nego")))
1676                                 uc->data |= DEBUG_NEGO;
1677                         else if ((arg_len = is_keyword(ptr, len, "tags")))
1678                                 uc->data |= DEBUG_TAGS;
1679                         else if ((arg_len = is_keyword(ptr, len, "pointer")))
1680                                 uc->data |= DEBUG_POINTER;
1681                         else
1682                                 return -EINVAL;
1683                         ptr += arg_len; len -= arg_len;
1684                 }
1685 #ifdef DEBUG_PROC_INFO
1686 printk("sym_user_command: data=%ld\n", uc->data);
1687 #endif
1688                 break;
1689 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1690         case UC_SETFLAG:
1691                 while (len > 0) {
1692                         SKIP_SPACES(1);
1693                         if      ((arg_len = is_keyword(ptr, len, "no_disc")))
1694                                 uc->data &= ~SYM_DISC_ENABLED;
1695                         else
1696                                 return -EINVAL;
1697                         ptr += arg_len; len -= arg_len;
1698                 }
1699                 break;
1700         default:
1701                 break;
1702         }
1703
1704         if (len)
1705                 return -EINVAL;
1706         else {
1707                 long flags;
1708
1709                 SYM_LOCK_HCB(np, flags);
1710                 sym_exec_user_command (np, uc);
1711                 SYM_UNLOCK_HCB(np, flags);
1712         }
1713         return length;
1714 }
1715
1716 #endif  /* SYM_LINUX_USER_COMMAND_SUPPORT */
1717
1718
1719 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1720 /*
1721  *  Informations through the proc file system.
1722  */
1723 struct info_str {
1724         char *buffer;
1725         int length;
1726         int offset;
1727         int pos;
1728 };
1729
1730 static void copy_mem_info(struct info_str *info, char *data, int len)
1731 {
1732         if (info->pos + len > info->length)
1733                 len = info->length - info->pos;
1734
1735         if (info->pos + len < info->offset) {
1736                 info->pos += len;
1737                 return;
1738         }
1739         if (info->pos < info->offset) {
1740                 data += (info->offset - info->pos);
1741                 len  -= (info->offset - info->pos);
1742         }
1743
1744         if (len > 0) {
1745                 memcpy(info->buffer + info->pos, data, len);
1746                 info->pos += len;
1747         }
1748 }
1749
1750 static int copy_info(struct info_str *info, char *fmt, ...)
1751 {
1752         va_list args;
1753         char buf[81];
1754         int len;
1755
1756         va_start(args, fmt);
1757         len = vsprintf(buf, fmt, args);
1758         va_end(args);
1759
1760         copy_mem_info(info, buf, len);
1761         return len;
1762 }
1763
1764 /*
1765  *  Copy formatted information into the input buffer.
1766  */
1767 static int sym_host_info(hcb_p np, char *ptr, off_t offset, int len)
1768 {
1769         struct info_str info;
1770
1771         info.buffer     = ptr;
1772         info.length     = len;
1773         info.offset     = offset;
1774         info.pos        = 0;
1775
1776         copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
1777                          "revision id 0x%x\n",
1778                          np->s.chip_name, np->device_id, np->revision_id);
1779         copy_info(&info, "On PCI bus %d, device %d, function %d, "
1780 #ifdef __sparc__
1781                 "IRQ %s\n",
1782 #else
1783                 "IRQ %d\n",
1784 #endif
1785                 np->s.bus, (np->s.device_fn & 0xf8) >> 3, np->s.device_fn & 7,
1786 #ifdef __sparc__
1787                 __irq_itoa(np->s.irq));
1788 #else
1789                 (int) np->s.irq);
1790 #endif
1791         copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
1792                          (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1793                          np->maxwide ? "Wide" : "Narrow",
1794                          np->minsync_dt ? ", DT capable" : "");
1795
1796         copy_info(&info, "Max. started commands %d, "
1797                          "max. commands per LUN %d\n",
1798                          SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1799
1800         return info.pos > info.offset? info.pos - info.offset : 0;
1801 }
1802 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1803
1804 /*
1805  *  Entry point of the scsi proc fs of the driver.
1806  *  - func = 0 means read  (returns adapter infos)
1807  *  - func = 1 means write (not yet merget from sym53c8xx)
1808  */
1809 static int sym53c8xx_proc_info(char *buffer, char **start, off_t offset,
1810                         int length, int hostno, int func)
1811 {
1812         struct Scsi_Host *host;
1813         struct host_data *host_data;
1814         hcb_p np = 0;
1815         int retv;
1816
1817         for (host = first_host; host; host = host->next) {
1818                 if (host->hostt != first_host->hostt)
1819                         continue;
1820                 if (host->host_no == hostno) {
1821                         host_data = (struct host_data *) host->hostdata;
1822                         np = host_data->ncb;
1823                         break;
1824                 }
1825         }
1826
1827         if (!np)
1828                 return -EINVAL;
1829
1830         if (func) {
1831 #ifdef  SYM_LINUX_USER_COMMAND_SUPPORT
1832                 retv = sym_user_command(np, buffer, length);
1833 #else
1834                 retv = -EINVAL;
1835 #endif
1836         }
1837         else {
1838                 if (start)
1839                         *start = buffer;
1840 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1841                 retv = sym_host_info(np, buffer, offset, length);
1842 #else
1843                 retv = -EINVAL;
1844 #endif
1845         }
1846
1847         return retv;
1848 }
1849 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1850
1851 /*
1852  *      Free controller resources.
1853  */
1854 static void sym_free_resources(hcb_p np, int holding_io_request_lock)
1855 {
1856         /*
1857          *  Free O/S specific resources.
1858          */
1859         if (np->s.irq)
1860                 free_irq(np->s.irq, np);
1861         if (np->s.io_port)
1862                 release_region(np->s.io_port, np->s.io_ws);
1863 #ifndef SYM_OPT_NO_BUS_MEMORY_MAPPING
1864         if (np->s.mmio_va)
1865                 pci_unmap_mem(np->s.mmio_va,
1866                               np->s.io_ws,
1867                               holding_io_request_lock);
1868         if (np->s.ram_va)
1869                 pci_unmap_mem(np->s.ram_va,
1870                               np->ram_ws,
1871                               holding_io_request_lock);
1872 #endif
1873         /*
1874          *  Free O/S independant resources.
1875          */
1876         sym_hcb_free(np);
1877
1878         sym_mfree_dma(np, sizeof(*np), "HCB");
1879 }
1880
1881 /*
1882  *  Ask/tell the system about DMA addressing.
1883  */
1884 #ifdef SYM_LINUX_DYNAMIC_DMA_MAPPING
1885 static int sym_setup_bus_dma_mask(hcb_p np)
1886 {
1887 #if LINUX_VERSION_CODE < LinuxVersionCode(2,4,3)
1888         if (!pci_dma_supported(np->s.device, 0xffffffffUL))
1889                 goto out_err32;
1890 #else
1891 #if   SYM_CONF_DMA_ADDRESSING_MODE == 0
1892         if (pci_set_dma_mask(np->s.device, 0xffffffffUL))
1893                 goto out_err32;
1894 #else
1895 #if   SYM_CONF_DMA_ADDRESSING_MODE == 1
1896 #define PciDmaMask      0xffffffffff
1897 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
1898 #define PciDmaMask      0xffffffffffffffff
1899 #endif
1900         if (np->features & FE_DAC) {
1901                 if (!pci_set_dma_mask(np->s.device, PciDmaMask)) {
1902                         np->use_dac = 1;
1903                         printf_info("%s: using 64 bit DMA addressing\n",
1904                                         sym_name(np));
1905                 }
1906                 else {
1907                         if (pci_set_dma_mask(np->s.device, 0xffffffffUL))
1908                                 goto out_err32;
1909                 }
1910         }
1911 #undef  PciDmaMask
1912 #endif
1913 #endif
1914         return 0;
1915
1916 out_err32:
1917         printf_warning("%s: 32 BIT DMA ADDRESSING NOT SUPPORTED\n",
1918                         sym_name(np));
1919         return -1;
1920 }
1921 #endif /* SYM_LINUX_DYNAMIC_DMA_MAPPING */
1922
1923 /*
1924  *  Host attach and initialisations.
1925  *
1926  *  Allocate host data and ncb structure.
1927  *  Request IO region and remap MMIO region.
1928  *  Do chip initialization.
1929  *  If all is OK, install interrupt handling and
1930  *  start the timer daemon.
1931  */
1932 static int __init 
1933 sym_attach (Scsi_Host_Template *tpnt, int unit, sym_device *dev)
1934 {
1935         struct host_data *host_data;
1936         hcb_p np = 0;
1937         struct Scsi_Host *instance = 0;
1938         u_long flags = 0;
1939         sym_nvram *nvram = dev->nvram;
1940         struct sym_fw *fw;
1941
1942         printk(KERN_INFO
1943                 "sym%d: <%s> rev 0x%x on pci bus %d device %d function %d "
1944 #ifdef __sparc__
1945                 "irq %s\n",
1946 #else
1947                 "irq %d\n",
1948 #endif
1949                 unit, dev->chip.name, dev->chip.revision_id,
1950                 dev->s.bus, (dev->s.device_fn & 0xf8) >> 3,
1951                 dev->s.device_fn & 7,
1952 #ifdef __sparc__
1953                 __irq_itoa(dev->s.irq));
1954 #else
1955                 dev->s.irq);
1956 #endif
1957
1958         /*
1959          *  Get the firmware for this chip.
1960          */
1961         fw = sym_find_firmware(&dev->chip);
1962         if (!fw)
1963                 goto attach_failed;
1964
1965         /*
1966          *      Allocate host_data structure
1967          */
1968         if (!(instance = scsi_register(tpnt, sizeof(*host_data))))
1969                 goto attach_failed;
1970         host_data = (struct host_data *) instance->hostdata;
1971
1972         /*
1973          *  Allocate immediately the host control block, 
1974          *  since we are only expecting to succeed. :)
1975          *  We keep track in the HCB of all the resources that 
1976          *  are to be released on error.
1977          */
1978 #ifdef  SYM_LINUX_DYNAMIC_DMA_MAPPING
1979         np = __sym_calloc_dma(dev->pdev, sizeof(*np), "HCB");
1980         if (np) {
1981                 np->s.device = dev->pdev;
1982                 np->bus_dmat = dev->pdev; /* Result in 1 DMA pool per HBA */
1983         }
1984         else
1985                 goto attach_failed;
1986 #else
1987         np = sym_calloc_dma(sizeof(*np), "HCB");
1988         if (!np)
1989                 goto attach_failed;
1990 #endif
1991         host_data->ncb = np;
1992
1993         SYM_INIT_LOCK_HCB(np);
1994
1995         /*
1996          *  Copy some useful infos to the HCB.
1997          */
1998         np->hcb_ba      = vtobus(np);
1999         np->verbose     = sym_driver_setup.verbose;
2000         np->s.device    = dev->pdev;
2001         np->s.unit      = unit;
2002         np->device_id   = dev->chip.device_id;
2003         np->revision_id = dev->chip.revision_id;
2004         np->s.bus       = dev->s.bus;
2005         np->s.device_fn = dev->s.device_fn;
2006         np->features    = dev->chip.features;
2007         np->clock_divn  = dev->chip.nr_divisor;
2008         np->maxoffs     = dev->chip.offset_max;
2009         np->maxburst    = dev->chip.burst_max;
2010         np->myaddr      = dev->host_id;
2011
2012         /*
2013          *  Edit its name.
2014          */
2015         strncpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name)-1);
2016         sprintf(np->s.inst_name, "sym%d", np->s.unit);
2017
2018         /*
2019          *  Ask/tell the system about DMA addressing.
2020          */
2021 #ifdef SYM_LINUX_DYNAMIC_DMA_MAPPING
2022         if (sym_setup_bus_dma_mask(np))
2023                 goto attach_failed;
2024 #endif
2025
2026         /*
2027          *  Try to map the controller chip to
2028          *  virtual and physical memory.
2029          */
2030         np->mmio_ba     = (u32)dev->s.base;
2031         np->s.io_ws     = (np->features & FE_IO256)? 256 : 128;
2032
2033 #ifndef SYM_CONF_IOMAPPED
2034         np->s.mmio_va = pci_map_mem(dev->s.base_c, np->s.io_ws);
2035         if (!np->s.mmio_va) {
2036                 printf_err("%s: can't map PCI MMIO region\n", sym_name(np));
2037                 goto attach_failed;
2038         }
2039         else if (sym_verbose > 1)
2040                 printf_info("%s: using memory mapped IO\n", sym_name(np));
2041 #endif /* !defined SYM_CONF_IOMAPPED */
2042
2043         /*
2044          *  Try to map the controller chip into iospace.
2045          */
2046         if (dev->s.io_port) {
2047                 request_region(dev->s.io_port, np->s.io_ws, NAME53C8XX);
2048                 np->s.io_port = dev->s.io_port;
2049         }
2050
2051         /*
2052          *  Map on-chip RAM if present and supported.
2053          */
2054         if (!(np->features & FE_RAM))
2055                 dev->s.base_2 = 0;
2056         if (dev->s.base_2) {
2057                 np->ram_ba = (u32)dev->s.base_2;
2058                 if (np->features & FE_RAM8K)
2059                         np->ram_ws = 8192;
2060                 else
2061                         np->ram_ws = 4096;
2062 #ifndef SYM_OPT_NO_BUS_MEMORY_MAPPING
2063                 np->s.ram_va = pci_map_mem(dev->s.base_2_c, np->ram_ws);
2064                 if (!np->s.ram_va) {
2065                         printf_err("%s: can't map PCI MEMORY region\n",
2066                                sym_name(np));
2067                         goto attach_failed;
2068                 }
2069 #endif
2070         }
2071
2072         /*
2073          *  Perform O/S independant stuff.
2074          */
2075         if (sym_hcb_attach(np, fw, nvram))
2076                 goto attach_failed;
2077
2078
2079         /*
2080          *  Install the interrupt handler.
2081          *  If we synchonize the C code with SCRIPTS on interrupt, 
2082          *  we donnot want to share the INTR line at all.
2083          */
2084         if (request_irq(dev->s.irq, sym53c8xx_intr, SA_SHIRQ,
2085                         NAME53C8XX, np)) {
2086                 printf_err("%s: request irq %d failure\n",
2087                         sym_name(np), dev->s.irq);
2088                 goto attach_failed;
2089         }
2090         np->s.irq = dev->s.irq;
2091
2092         /*
2093          *  After SCSI devices have been opened, we cannot
2094          *  reset the bus safely, so we do it here.
2095          */
2096         SYM_LOCK_HCB(np, flags);
2097         if (sym_reset_scsi_bus(np, 0)) {
2098                 printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
2099                            "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
2100                 SYM_UNLOCK_HCB(np, flags);
2101                 goto attach_failed;
2102         }
2103
2104         /*
2105          *  Initialize some queue headers.
2106          */
2107         sym_que_init(&np->s.wait_cmdq);
2108         sym_que_init(&np->s.busy_cmdq);
2109
2110         /*
2111          *  Start the SCRIPTS.
2112          */
2113         sym_start_up (np, 1);
2114
2115         /*
2116          *  Start the timer daemon
2117          */
2118         init_timer(&np->s.timer);
2119         np->s.timer.data     = (unsigned long) np;
2120         np->s.timer.function = sym53c8xx_timer;
2121         np->s.lasttime=0;
2122         sym_timer (np);
2123
2124         /*
2125          *  Done.
2126          */
2127         if (!first_host)
2128                 first_host = instance;
2129
2130         /*
2131          *  Fill Linux host instance structure
2132          *  and return success.
2133          */
2134         instance->max_channel   = 0;
2135         instance->this_id       = np->myaddr;
2136         instance->max_id        = np->maxwide ? 16 : 8;
2137         instance->max_lun       = SYM_CONF_MAX_LUN;
2138 #ifndef SYM_CONF_IOMAPPED
2139 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,3,29)
2140         instance->base          = (unsigned long) np->s.mmio_va;
2141 #else
2142         instance->base          = (char *) np->s.mmio_va;
2143 #endif
2144 #endif
2145         instance->irq           = np->s.irq;
2146         instance->unique_id     = np->s.io_port;
2147         instance->io_port       = np->s.io_port;
2148         instance->n_io_port     = np->s.io_ws;
2149         instance->dma_channel   = 0;
2150         instance->cmd_per_lun   = SYM_CONF_MAX_TAG;
2151         instance->can_queue     = (SYM_CONF_MAX_START-2);
2152         instance->sg_tablesize  = SYM_CONF_MAX_SG;
2153 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
2154         instance->max_cmd_len   = 16;
2155 #endif
2156         instance->select_queue_depths = sym53c8xx_select_queue_depths;
2157         instance->highmem_io    = 1;
2158
2159         SYM_UNLOCK_HCB(np, flags);
2160
2161         scsi_set_pci_device(instance, dev->pdev);
2162
2163         /*
2164          *  Now let the generic SCSI driver
2165          *  look for the SCSI devices on the bus ..
2166          */
2167         return 0;
2168
2169 attach_failed:
2170         if (!instance) return -1;
2171         printf_info("%s: giving up ...\n", sym_name(np));
2172         if (np)
2173                 sym_free_resources(np, 1);
2174         scsi_unregister(instance);
2175
2176         return -1;
2177  }
2178
2179
2180 /*
2181  *    Detect and try to read SYMBIOS and TEKRAM NVRAM.
2182  */
2183 #if SYM_CONF_NVRAM_SUPPORT
2184 static void __init sym_get_nvram(sym_device *devp, sym_nvram *nvp)
2185 {
2186         if (!nvp)
2187                 return;
2188
2189         devp->nvram = nvp;
2190         devp->device_id = devp->chip.device_id;
2191         nvp->type = 0;
2192
2193         /*
2194          *  Get access to chip IO registers
2195          */
2196 #ifdef SYM_CONF_IOMAPPED
2197         request_region(devp->s.io_port, 128, NAME53C8XX);
2198 #else
2199         devp->s.mmio_va = pci_map_mem(devp->s.base_c, 128);
2200         if (!devp->s.mmio_va)
2201                 return;
2202 #endif
2203
2204         /*
2205          *  Try to read SYMBIOS|TEKRAM nvram.
2206          */
2207         (void) sym_read_nvram(devp, nvp);
2208
2209         /*
2210          *  Release access to chip IO registers
2211          */
2212 #ifdef SYM_CONF_IOMAPPED
2213         release_region(devp->s.io_port, 128);
2214 #else
2215         pci_unmap_mem((u_long) devp->s.mmio_va, 128ul, 1);
2216 #endif
2217 }
2218 #endif  /* SYM_CONF_NVRAM_SUPPORT */
2219
2220 /*
2221  *  Driver setup from the boot command line
2222  */
2223 #ifdef  SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT
2224
2225 static struct sym_driver_setup
2226         sym_driver_safe_setup __initdata = SYM_LINUX_DRIVER_SAFE_SETUP;
2227 #ifdef  MODULE
2228 char *sym53c8xx = 0;    /* command line passed by insmod */
2229 MODULE_PARM(sym53c8xx, "s");
2230 #endif
2231
2232 static void __init sym53c8xx_print_driver_setup(void)
2233 {
2234         printf_info (NAME53C8XX ": setup="
2235                 "mpar:%d,spar:%d,tags:%d,sync:%d,burst:%d,"
2236                 "led:%d,wide:%d,diff:%d,irqm:%d, buschk:%d\n",
2237                 sym_driver_setup.pci_parity,
2238                 sym_driver_setup.scsi_parity,
2239                 sym_driver_setup.max_tag,
2240                 sym_driver_setup.min_sync,
2241                 sym_driver_setup.burst_order,
2242                 sym_driver_setup.scsi_led,
2243                 sym_driver_setup.max_wide,
2244                 sym_driver_setup.scsi_diff,
2245                 sym_driver_setup.irq_mode,
2246                 sym_driver_setup.scsi_bus_check);
2247         printf_info (NAME53C8XX ": setup="
2248                 "hostid:%d,offs:%d,luns:%d,pcifix:%d,revprob:%d,"
2249                 "verb:%d,debug:0x%x,setlle_delay:%d\n",
2250                 sym_driver_setup.host_id,
2251                 sym_driver_setup.max_offs,
2252                 sym_driver_setup.max_lun,
2253                 sym_driver_setup.pci_fix_up,
2254                 sym_driver_setup.reverse_probe,
2255                 sym_driver_setup.verbose,
2256                 sym_driver_setup.debug,
2257                 sym_driver_setup.settle_delay);
2258 #ifdef DEBUG_2_0_X
2259 MDELAY(5000);
2260 #endif
2261 };
2262
2263 #define OPT_PCI_PARITY          1
2264 #define OPT_SCSI_PARITY         2
2265 #define OPT_MAX_TAG             3
2266 #define OPT_MIN_SYNC            4
2267 #define OPT_BURST_ORDER         5
2268 #define OPT_SCSI_LED            6
2269 #define OPT_MAX_WIDE            7
2270 #define OPT_SCSI_DIFF           8
2271 #define OPT_IRQ_MODE            9
2272 #define OPT_SCSI_BUS_CHECK      10
2273 #define OPT_HOST_ID             11
2274 #define OPT_MAX_OFFS            12
2275 #define OPT_MAX_LUN             13
2276 #define OPT_PCI_FIX_UP          14
2277
2278 #define OPT_REVERSE_PROBE       15
2279 #define OPT_VERBOSE             16
2280 #define OPT_DEBUG               17
2281 #define OPT_SETTLE_DELAY        18
2282 #define OPT_USE_NVRAM           19
2283 #define OPT_EXCLUDE             20
2284 #define OPT_SAFE_SETUP          21
2285
2286 static char setup_token[] __initdata =
2287         "mpar:"         "spar:"
2288         "tags:"         "sync:"
2289         "burst:"        "led:"
2290         "wide:"         "diff:"
2291         "irqm:"         "buschk:"
2292         "hostid:"       "offset:"
2293         "luns:"         "pcifix:"
2294         "revprob:"      "verb:"
2295         "debug:"        "settle:"
2296         "nvram:"        "excl:"
2297         "safe:"
2298         ;
2299
2300 #ifdef MODULE
2301 #define ARG_SEP ' '
2302 #else
2303 #define ARG_SEP ','
2304 #endif
2305
2306 static int __init get_setup_token(char *p)
2307 {
2308         char *cur = setup_token;
2309         char *pc;
2310         int i = 0;
2311
2312         while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
2313                 ++pc;
2314                 ++i;
2315                 if (!strncmp(p, cur, pc - cur))
2316                         return i;
2317                 cur = pc;
2318         }
2319         return 0;
2320 }
2321 #endif  /* SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT */
2322
2323 int __init sym53c8xx_setup(char *str)
2324 {
2325 #ifdef  SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT
2326         char *cur = str;
2327         char *pc, *pv;
2328         unsigned long val;
2329         int i,  c;
2330         int xi = 0;
2331
2332         while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
2333                 char *pe;
2334
2335                 val = 0;
2336                 pv = pc;
2337                 c = *++pv;
2338
2339                 if      (c == 'n')
2340                         val = 0;
2341                 else if (c == 'y')
2342                         val = 1;
2343                 else
2344                         val = (int) simple_strtoul(pv, &pe, 0);
2345
2346                 switch (get_setup_token(cur)) {
2347                 case OPT_MAX_TAG:
2348                         sym_driver_setup.max_tag = val;
2349                         if (!(pe && *pe == '/'))
2350                                 break;
2351                         i = 0;
2352                         while (*pe && *pe != ARG_SEP && 
2353                                 i < sizeof(sym_driver_setup.tag_ctrl)-1) {
2354                                 sym_driver_setup.tag_ctrl[i++] = *pe++;
2355                         }
2356                         sym_driver_setup.tag_ctrl[i] = '\0';
2357                         break;
2358                 case OPT_SAFE_SETUP:
2359                         memcpy(&sym_driver_setup, &sym_driver_safe_setup,
2360                                 sizeof(sym_driver_setup));
2361                         break;
2362                 case OPT_EXCLUDE:
2363                         if (xi < 8)
2364                                 sym_driver_setup.excludes[xi++] = val;
2365                         break;
2366
2367 #define __SIMPLE_OPTION(NAME, name) \
2368                 case OPT_ ## NAME :             \
2369                         sym_driver_setup.name = val;\
2370                         break;
2371
2372                 __SIMPLE_OPTION(PCI_PARITY, pci_parity)
2373                 __SIMPLE_OPTION(SCSI_PARITY, scsi_parity)
2374                 __SIMPLE_OPTION(MIN_SYNC, min_sync)
2375                 __SIMPLE_OPTION(BURST_ORDER, burst_order)
2376                 __SIMPLE_OPTION(SCSI_LED, scsi_led)
2377                 __SIMPLE_OPTION(MAX_WIDE, max_wide)
2378                 __SIMPLE_OPTION(SCSI_DIFF, scsi_diff)
2379                 __SIMPLE_OPTION(IRQ_MODE, irq_mode)
2380                 __SIMPLE_OPTION(SCSI_BUS_CHECK, scsi_bus_check)
2381                 __SIMPLE_OPTION(HOST_ID, host_id)
2382                 __SIMPLE_OPTION(MAX_OFFS, max_offs)
2383                 __SIMPLE_OPTION(MAX_LUN, max_lun)
2384                 __SIMPLE_OPTION(PCI_FIX_UP, pci_fix_up)
2385                 __SIMPLE_OPTION(REVERSE_PROBE, reverse_probe)
2386                 __SIMPLE_OPTION(VERBOSE, verbose)
2387                 __SIMPLE_OPTION(DEBUG, debug)
2388                 __SIMPLE_OPTION(SETTLE_DELAY, settle_delay)
2389                 __SIMPLE_OPTION(USE_NVRAM, use_nvram)
2390
2391 #undef __SIMPLE_OPTION
2392
2393                 default:
2394                         printk("sym53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
2395                         break;
2396                 }
2397
2398                 if ((cur = strchr(cur, ARG_SEP)) != NULL)
2399                         ++cur;
2400         }
2401 #endif  /* SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT */
2402         return 1;
2403 }
2404
2405 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,3,13)
2406 #ifndef MODULE
2407 __setup("sym53c8xx=", sym53c8xx_setup);
2408 #endif
2409 #endif
2410
2411 #ifdef  SYM_CONF_PQS_PDS_SUPPORT
2412 /*
2413  *  Detect all NCR PQS/PDS boards and keep track of their bus nr.
2414  *
2415  *  The NCR PQS or PDS card is constructed as a DEC bridge
2416  *  behind which sit a proprietary NCR memory controller and
2417  *  four or two 53c875s as separate devices.  In its usual mode
2418  *  of operation, the 875s are slaved to the memory controller
2419  *  for all transfers.  We can tell if an 875 is part of a
2420  *  PQS/PDS or not since if it is, it will be on the same bus
2421  *  as the memory controller.  To operate with the Linux
2422  *  driver, the memory controller is disabled and the 875s
2423  *  freed to function independently.  The only wrinkle is that
2424  *  the preset SCSI ID (which may be zero) must be read in from
2425  *  a special configuration space register of the 875
2426  */
2427 #ifndef SYM_CONF_MAX_PQS_BUS
2428 #define SYM_CONF_MAX_PQS_BUS 16
2429 #endif
2430 static int pqs_bus[SYM_CONF_MAX_PQS_BUS] __initdata = { 0 };
2431
2432 static void __init sym_detect_pqs_pds(void)
2433 {
2434         short index;
2435         pcidev_t dev = PCIDEV_NULL;
2436
2437         for(index=0; index < SYM_CONF_MAX_PQS_BUS; index++) {
2438                 u_char tmp;
2439
2440                 dev = pci_find_device(0x101a, 0x0009, dev);
2441                 if (dev == PCIDEV_NULL) {
2442                         pqs_bus[index] = -1;
2443                         break;
2444                 }
2445                 printf_info(NAME53C8XX ": NCR PQS/PDS memory controller detected on bus %d\n", PciBusNumber(dev));
2446                 pci_read_config_byte(dev, 0x44, &tmp);
2447                 /* bit 1: allow individual 875 configuration */
2448                 tmp |= 0x2;
2449                 pci_write_config_byte(dev, 0x44, tmp);
2450                 pci_read_config_byte(dev, 0x45, &tmp);
2451                 /* bit 2: drive individual 875 interrupts to the bus */
2452                 tmp |= 0x4;
2453                 pci_write_config_byte(dev, 0x45, tmp);
2454
2455                 pqs_bus[index] = PciBusNumber(dev);
2456         }
2457 }
2458 #endif /* SYM_CONF_PQS_PDS_SUPPORT */
2459
2460 /*
2461  *  Read and check the PCI configuration for any detected NCR 
2462  *  boards and save data for attaching after all boards have 
2463  *  been detected.
2464  */
2465 static int __init
2466 sym53c8xx_pci_init(Scsi_Host_Template *tpnt, pcidev_t pdev, sym_device *device)
2467 {
2468         u_short vendor_id, device_id, command, status_reg;
2469         u_char cache_line_size;
2470         u_char suggested_cache_line_size = 0;
2471         u_char pci_fix_up = SYM_SETUP_PCI_FIX_UP;
2472         u_char revision;
2473         u_int irq;
2474         u_long base, base_2, base_io; 
2475         u_long base_c, base_2_c, io_port; 
2476         int i;
2477         sym_chip *chip;
2478
2479         /* Choose some short name for this device */
2480         sprintf(device->s.inst_name, "sym.%d.%d.%d",
2481                 PciBusNumber(pdev),
2482                 (int) (PciDeviceFn(pdev) & 0xf8) >> 3,
2483                 (int) (PciDeviceFn(pdev) & 7));
2484
2485         /*
2486          *  Read needed minimal info from the PCI config space.
2487          */
2488         vendor_id = PciVendorId(pdev);
2489         device_id = PciDeviceId(pdev);
2490         irq       = PciIrqLine(pdev);
2491
2492         i = pci_get_base_address(pdev, 0, &base_io);
2493         io_port = pci_get_base_cookie(pdev, 0);
2494
2495         base_c = pci_get_base_cookie(pdev, i);
2496         i = pci_get_base_address(pdev, i, &base);
2497
2498         base_2_c = pci_get_base_cookie(pdev, i);
2499         (void) pci_get_base_address(pdev, i, &base_2);
2500
2501         io_port &= PCI_BASE_ADDRESS_IO_MASK;
2502         base    &= PCI_BASE_ADDRESS_MEM_MASK;
2503         base_2  &= PCI_BASE_ADDRESS_MEM_MASK;
2504
2505         pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
2506
2507         /*
2508          *  If user excluded this chip, donnot initialize it.
2509          */
2510         if (base_io) {
2511                 for (i = 0 ; i < 8 ; i++) {
2512                         if (sym_driver_setup.excludes[i] == base_io)
2513                                 return -1;
2514                 }
2515         }
2516
2517         /*
2518          *  Leave here if another driver attached the chip.
2519          */
2520         if (io_port && check_region (io_port, 128)) {
2521                 printf_info("%s: IO region 0x%lx[0..127] is in use\n",
2522                             sym_name(device), (long) io_port);
2523                 return -1;
2524         }
2525
2526         /*
2527          *  Check if the chip is supported.
2528          */
2529         chip = sym_lookup_pci_chip_table(device_id, revision);
2530         if (!chip) {
2531                 printf_info("%s: device not supported\n", sym_name(device));
2532                 return -1;
2533         }
2534
2535         /*
2536          *  Check if the chip has been assigned resources we need.
2537          */
2538 #ifdef SYM_CONF_IOMAPPED
2539         if (!io_port) {
2540                 printf_info("%s: IO base address disabled.\n",
2541                             sym_name(device));
2542                 return -1;
2543         }
2544 #else
2545         if (!base) {
2546                 printf_info("%s: MMIO base address disabled.\n",
2547                             sym_name(device));
2548                 return -1;
2549         }
2550 #endif
2551
2552         /*
2553          *  Ignore Symbios chips controlled by various RAID controllers.
2554          *  These controllers set value 0x52414944 at RAM end - 16.
2555          */
2556 #if defined(__i386__) && !defined(SYM_OPT_NO_BUS_MEMORY_MAPPING)
2557         if (base_2_c) {
2558                 unsigned int ram_size, ram_val;
2559                 u_long ram_ptr;
2560
2561                 if (chip->features & FE_RAM8K)
2562                         ram_size = 8192;
2563                 else
2564                         ram_size = 4096;
2565
2566                 ram_ptr = pci_map_mem(base_2_c, ram_size);
2567                 if (ram_ptr) {
2568                         ram_val = readl_raw(ram_ptr + ram_size - 16);
2569                         pci_unmap_mem(ram_ptr, ram_size, 1);
2570                         if (ram_val == 0x52414944) {
2571                                 printf_info("%s: not initializing, "
2572                                             "driven by RAID controller.\n",
2573                                             sym_name(device));
2574                                 return -1;
2575                         }
2576                 }
2577         }
2578 #endif /* i386 and PCI MEMORY accessible */
2579
2580         /*
2581          *  Copy the chip description to our device structure, 
2582          *  so we can make it match the actual device and options.
2583          */
2584         bcopy(chip, &device->chip, sizeof(device->chip));
2585         device->chip.revision_id = revision;
2586
2587         /*
2588          *  Read additionnal info from the configuration space.
2589          */
2590         pci_read_config_word(pdev, PCI_COMMAND,         &command);
2591         pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &cache_line_size);
2592
2593         /*
2594          * Enable missing capabilities in the PCI COMMAND register.
2595          */
2596 #ifdef SYM_CONF_IOMAPPED
2597 #define PCI_COMMAND_BITS_TO_ENABLE (PCI_COMMAND_IO | \
2598         PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_PARITY)
2599 #else
2600 #define PCI_COMMAND_BITS_TO_ENABLE \
2601         (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_PARITY)
2602 #endif
2603         if ((command & PCI_COMMAND_BITS_TO_ENABLE)
2604                     != PCI_COMMAND_BITS_TO_ENABLE) {
2605                 printf_info("%s: setting%s%s%s%s...\n", sym_name(device),
2606                 (command & PCI_COMMAND_IO)     ? "" : " PCI_COMMAND_IO",
2607                 (command & PCI_COMMAND_MEMORY) ? "" : " PCI_COMMAND_MEMORY",
2608                 (command & PCI_COMMAND_MASTER) ? "" : " PCI_COMMAND_MASTER",
2609                 (command & PCI_COMMAND_PARITY) ? "" : " PCI_COMMAND_PARITY");
2610                 command |= PCI_COMMAND_BITS_TO_ENABLE;
2611                 pci_write_config_word(pdev, PCI_COMMAND, command);
2612         }
2613 #undef  PCI_COMMAND_BITS_TO_ENABLE
2614
2615         /*
2616          *  If cache line size is not configured, suggest
2617          *  a value for well known CPUs.
2618          */
2619 #if defined(__i386__) && !defined(MODULE)
2620         if (!cache_line_size && boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
2621                 switch(boot_cpu_data.x86) {
2622                 case 4: suggested_cache_line_size = 4;   break;
2623                 case 6: if (boot_cpu_data.x86_model > 8) break;
2624                 case 5: suggested_cache_line_size = 8;   break;
2625                 }
2626         }
2627 #endif  /* __i386__ */
2628
2629         /*
2630          *  Some features are required to be enabled in order to 
2631          *  work around some chip problems. :) ;)
2632          *  (ITEM 12 of a DEL about the 896 I haven't yet).
2633          *  We must ensure the chip will use WRITE AND INVALIDATE.
2634          *  The revision number limit is for now arbitrary.
2635          */
2636         if (device_id == PCI_DEVICE_ID_NCR_53C896 && revision < 0x4) {
2637                 chip->features  |= (FE_WRIE | FE_CLSE);
2638                 pci_fix_up      |=  3;  /* Force appropriate PCI fix-up */
2639         }
2640
2641 #ifdef  SYM_CONF_PCI_FIX_UP
2642         /*
2643          *  Try to fix up PCI config according to wished features.
2644          */
2645         if ((pci_fix_up & 1) && (chip->features & FE_CLSE) && 
2646             !cache_line_size && suggested_cache_line_size) {
2647                 cache_line_size = suggested_cache_line_size;
2648                 pci_write_config_byte(pdev,
2649                                       PCI_CACHE_LINE_SIZE, cache_line_size);
2650                 printf_info("%s: PCI_CACHE_LINE_SIZE set to %d.\n",
2651                             sym_name(device), cache_line_size);
2652         }
2653
2654         if ((pci_fix_up & 2) && cache_line_size &&
2655             (chip->features & FE_WRIE) && !(command & PCI_COMMAND_INVALIDATE)) {
2656                 printf_info("%s: setting PCI_COMMAND_INVALIDATE.\n",
2657                             sym_name(device));
2658                 command |= PCI_COMMAND_INVALIDATE;
2659                 pci_write_config_word(pdev, PCI_COMMAND, command);
2660         }
2661 #endif  /* SYM_CONF_PCI_FIX_UP */
2662
2663         /*
2664          *  Work around for errant bit in 895A. The 66Mhz
2665          *  capable bit is set erroneously. Clear this bit.
2666          *  (Item 1 DEL 533)
2667          *
2668          *  Make sure Config space and Features agree.
2669          *
2670          *  Recall: writes are not normal to status register -
2671          *  write a 1 to clear and a 0 to leave unchanged.
2672          *  Can only reset bits.
2673          */
2674         pci_read_config_word(pdev, PCI_STATUS, &status_reg);
2675         if (chip->features & FE_66MHZ) {
2676                 if (!(status_reg & PCI_STATUS_66MHZ))
2677                         chip->features &= ~FE_66MHZ;
2678         }
2679         else {
2680                 if (status_reg & PCI_STATUS_66MHZ) {
2681                         status_reg = PCI_STATUS_66MHZ;
2682                         pci_write_config_word(pdev, PCI_STATUS, status_reg);
2683                         pci_read_config_word(pdev, PCI_STATUS, &status_reg);
2684                 }
2685         }
2686
2687         /*
2688          *  Initialise device structure with items required by sym_attach.
2689          */
2690         device->pdev            = pdev;
2691         device->s.bus           = PciBusNumber(pdev);
2692         device->s.device_fn     = PciDeviceFn(pdev);
2693         device->s.base          = base;
2694         device->s.base_2        = base_2;
2695         device->s.base_c        = base_c;
2696         device->s.base_2_c      = base_2_c;
2697         device->s.io_port       = io_port;
2698         device->s.irq           = irq;
2699         device->attach_done     = 0;
2700
2701         return 0;
2702 }
2703
2704 /*
2705  *  List of supported NCR chip ids
2706  */
2707 static u_short sym_chip_ids[] __initdata        = {
2708         PCI_ID_SYM53C810,
2709         PCI_ID_SYM53C815,
2710         PCI_ID_SYM53C825,
2711         PCI_ID_SYM53C860,
2712         PCI_ID_SYM53C875,
2713         PCI_ID_SYM53C875_2,
2714         PCI_ID_SYM53C885,
2715         PCI_ID_SYM53C875A,
2716         PCI_ID_SYM53C895,
2717         PCI_ID_SYM53C896,
2718         PCI_ID_SYM53C895A,
2719         PCI_ID_LSI53C1510D,
2720         PCI_ID_LSI53C1010,
2721         PCI_ID_LSI53C1010_2
2722 };
2723
2724 /*
2725  *  Detect all 53c8xx hosts and then attach them.
2726  *
2727  *  If we are using NVRAM, once all hosts are detected, we need to 
2728  *  check any NVRAM for boot order in case detect and boot order 
2729  *  differ and attach them using the order in the NVRAM.
2730  *
2731  *  If no NVRAM is found or data appears invalid attach boards in 
2732  *  the order they are detected.
2733  */
2734 int __init sym53c8xx_detect(Scsi_Host_Template *tpnt)
2735 {
2736         pcidev_t pcidev;
2737         int i, j, chips, hosts, count;
2738         int attach_count = 0;
2739         sym_device *devtbl, *devp;
2740         sym_nvram  nvram;
2741 #if SYM_CONF_NVRAM_SUPPORT
2742         sym_nvram  nvram0, *nvp;
2743 #endif
2744
2745         /*
2746          *  PCI is required.
2747          */
2748         if (!pci_present())
2749                 return 0;
2750
2751         /*
2752          *    Initialize driver general stuff.
2753          */
2754 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
2755 #if LINUX_VERSION_CODE < LinuxVersionCode(2,3,27)
2756      tpnt->proc_dir  = &proc_scsi_sym53c8xx;
2757 #else
2758      tpnt->proc_name = NAME53C8XX;
2759 #endif
2760      tpnt->proc_info = sym53c8xx_proc_info;
2761 #endif
2762
2763 #ifdef SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT
2764 #ifdef MODULE
2765 if (sym53c8xx)
2766         sym53c8xx_setup(sym53c8xx);
2767 #endif
2768 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
2769         sym_debug_flags = sym_driver_setup.debug;
2770 #endif
2771         if (boot_verbose >= 2)
2772                 sym53c8xx_print_driver_setup();
2773 #endif /* SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT */
2774
2775         /*
2776          *  Allocate the device table since we donnot want to 
2777          *  overflow the kernel stack.
2778          *  1 x 4K PAGE is enough for more than 40 devices for i386.
2779          */
2780         devtbl = sym_calloc(PAGE_SIZE, "DEVTBL");
2781         if (!devtbl)
2782                 return 0;
2783
2784         /*
2785          *  Detect all NCR PQS/PDS memory controllers.
2786          */
2787 #ifdef  SYM_CONF_PQS_PDS_SUPPORT
2788         sym_detect_pqs_pds();
2789 #endif
2790
2791         /* 
2792          *  Detect all 53c8xx hosts.
2793          *  Save the first Symbios NVRAM content if any 
2794          *  for the boot order.
2795          */
2796         chips   = sizeof(sym_chip_ids)  / sizeof(sym_chip_ids[0]);
2797         hosts   = PAGE_SIZE             / sizeof(*devtbl);
2798 #if SYM_CONF_NVRAM_SUPPORT
2799         nvp = (sym_driver_setup.use_nvram & 0x1) ? &nvram0 : 0;
2800 #endif
2801         j = 0;
2802         count = 0;
2803         pcidev = PCIDEV_NULL;
2804         while (1) {
2805                 char *msg = "";
2806                 if (count >= hosts)
2807                         break;
2808                 if (j >= chips)
2809                         break;
2810                 i = sym_driver_setup.reverse_probe ? chips - 1 - j : j;
2811                 pcidev = pci_find_device(PCI_VENDOR_ID_NCR, sym_chip_ids[i],
2812                                          pcidev);
2813                 if (pcidev == PCIDEV_NULL) {
2814                         ++j;
2815                         continue;
2816                 }
2817                 /* This one is guaranteed by AC to do nothing :-) */
2818                 if (pci_enable_device(pcidev))
2819                         continue;
2820                 /* Some HW as the HP LH4 may report twice PCI devices */
2821                 for (i = 0; i < count ; i++) {
2822                         if (devtbl[i].s.bus       == PciBusNumber(pcidev) && 
2823                             devtbl[i].s.device_fn == PciDeviceFn(pcidev))
2824                                 break;
2825                 }
2826                 if (i != count) /* Ignore this device if we already have it */
2827                         continue;
2828                 devp = &devtbl[count];
2829                 devp->host_id = SYM_SETUP_HOST_ID;
2830                 devp->attach_done = 0;
2831                 if (sym53c8xx_pci_init(tpnt, pcidev, devp)) {
2832                         continue;
2833                 }
2834                 ++count;
2835 #if SYM_CONF_NVRAM_SUPPORT
2836                 if (nvp) {
2837                         sym_get_nvram(devp, nvp);
2838                         switch(nvp->type) {
2839                         case SYM_SYMBIOS_NVRAM:
2840                                 /*
2841                                  *   Switch to the other nvram buffer, so that 
2842                                  *   nvram0 will contain the first Symbios 
2843                                  *   format NVRAM content with boot order.
2844                                  */
2845                                 nvp = &nvram;
2846                                 msg = "with Symbios NVRAM";
2847                                 break;
2848                         case SYM_TEKRAM_NVRAM:
2849                                 msg = "with Tekram NVRAM";
2850                                 break;
2851                         }
2852                 }
2853 #endif
2854 #ifdef  SYM_CONF_PQS_PDS_SUPPORT
2855                 /*
2856                  *  Match the BUS number for PQS/PDS devices.
2857                  *  Read the SCSI ID from a special register mapped
2858                  *  into the configuration space of the individual
2859                  *  875s.  This register is set up by the PQS bios
2860                  */
2861                 for(i = 0; i < SYM_CONF_MAX_PQS_BUS && pqs_bus[i] != -1; i++) {
2862                         u_char tmp;
2863                         if (pqs_bus[i] == PciBusNumber(pcidev)) {
2864                                 pci_read_config_byte(pcidev, 0x84, &tmp);
2865                                 devp->pqs_pds = 1;
2866                                 devp->host_id = tmp;
2867                                 break;
2868                         }
2869                 }
2870                 if (devp->pqs_pds)
2871                         msg = "(NCR PQS/PDS)";
2872 #endif
2873                 if (boot_verbose)
2874                         printf_info("%s: 53c%s detected %s\n",
2875                                     sym_name(devp), devp->chip.name, msg);
2876         }
2877
2878         /*
2879          *  If we have found a SYMBIOS NVRAM, use first the NVRAM boot 
2880          *  sequence as device boot order.
2881          *  check devices in the boot record against devices detected. 
2882          *  attach devices if we find a match. boot table records that 
2883          *  do not match any detected devices will be ignored. 
2884          *  devices that do not match any boot table will not be attached
2885          *  here but will attempt to be attached during the device table 
2886          *  rescan.
2887          */
2888 #if SYM_CONF_NVRAM_SUPPORT
2889         if (!nvp || nvram0.type != SYM_SYMBIOS_NVRAM)
2890                 goto next;
2891         for (i = 0; i < 4; i++) {
2892                 Symbios_host *h = &nvram0.data.Symbios.host[i];
2893                 for (j = 0 ; j < count ; j++) {
2894                         devp = &devtbl[j];
2895                         if (h->device_fn != devp->s.device_fn ||
2896                             h->bus_nr    != devp->s.bus  ||
2897                             h->device_id != devp->chip.device_id)
2898                                 continue;
2899                         if (devp->attach_done)
2900                                 continue;
2901                         if (h->flags & SYMBIOS_INIT_SCAN_AT_BOOT) {
2902                                 sym_get_nvram(devp, nvp);
2903                                 if (!sym_attach (tpnt, attach_count, devp))
2904                                         attach_count++;
2905                         }
2906                         else if (!(sym_driver_setup.use_nvram & 0x80))
2907                                 printf_info(
2908                                       "%s: 53c%s state OFF thus not attached\n",
2909                                       sym_name(devp), devp->chip.name);
2910                         else
2911                                 continue;
2912
2913                         devp->attach_done = 1;
2914                         break;
2915                 }
2916         }
2917 next:
2918 #endif
2919
2920         /* 
2921          *  Rescan device list to make sure all boards attached.
2922          *  Devices without boot records will not be attached yet
2923          *  so try to attach them here.
2924          */
2925         for (i= 0; i < count; i++) {
2926                 devp = &devtbl[i];
2927                 if (!devp->attach_done) {
2928                         devp->nvram = &nvram;
2929                         nvram.type = 0;
2930 #if SYM_CONF_NVRAM_SUPPORT
2931                         sym_get_nvram(devp, nvp);
2932 #endif
2933                         if (!sym_attach (tpnt, attach_count, devp))
2934                                 attach_count++;
2935                 }
2936         }
2937
2938         sym_mfree(devtbl, PAGE_SIZE, "DEVTBL");
2939
2940         return attach_count;
2941 }
2942
2943
2944
2945 #ifdef MODULE
2946 /*
2947  *  Linux release module stuff.
2948  *
2949  *  Called before unloading the module.
2950  *  Detach the host.
2951  *  We have to free resources and halt the NCR chip.
2952  *
2953  */
2954 static int sym_detach(hcb_p np)
2955 {
2956         printk("%s: detaching ...\n", sym_name(np));
2957
2958         /*
2959          *  Try to delete the timer.
2960          *  In the unlikely situation where this failed,
2961          *  try to synchronize with the timer handler.
2962          */
2963 #if LINUX_VERSION_CODE < LinuxVersionCode(2, 4, 0)
2964         np->s.release_stage = 1;
2965         if (!del_timer(&np->s.timer)) {
2966                 int i = 1000;
2967                 int k = 1;
2968                 while (1) {
2969                         u_long flags;
2970                         SYM_LOCK_HCB(np, flags);
2971                         k = np->s.release_stage;
2972                         SYM_UNLOCK_HCB(np, flags);
2973                         if (k == 2 || !--i)
2974                                 break;
2975                         MDELAY(5);
2976                 }
2977                 if (!i)
2978                         printk("%s: failed to kill timer!\n", sym_name(np));
2979         }
2980         np->s.release_stage = 2;
2981 #else
2982         (void)del_timer_sync(&np->s.timer);
2983 #endif
2984
2985         /*
2986          *  Reset NCR chip.
2987          *  We should use sym_soft_reset(), but we donnot want to do 
2988          *  so, since we may not be safe if interrupts occur.
2989          */
2990         printk("%s: resetting chip\n", sym_name(np));
2991         OUTB (nc_istat, SRST);
2992         UDELAY (10);
2993         OUTB (nc_istat, 0);
2994
2995         /*
2996          *  Free host resources
2997          */
2998         sym_free_resources(np, 0);
2999
3000         return 1;
3001 }
3002
3003 int sym53c8xx_release(struct Scsi_Host *host)
3004 {
3005      sym_detach(((struct host_data *) host->hostdata)->ncb);
3006
3007      return 0;
3008 }
3009 #endif /* MODULE */
3010
3011 /*
3012  * For bigots to keep silent. :)
3013  */
3014 #ifdef MODULE_LICENSE
3015 MODULE_LICENSE("Dual BSD/GPL");
3016 #endif
3017
3018 /*
3019  * Driver host template.
3020  */
3021 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0)
3022 static
3023 #endif
3024 #if LINUX_VERSION_CODE >= LinuxVersionCode(2,4,0) || defined(MODULE)
3025 Scsi_Host_Template driver_template = SYM53C8XX;
3026 #include "../scsi_module.c"
3027 #endif