ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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/ctype.h>
55 #include <linux/init.h>
56 #include <linux/interrupt.h>
57 #include <linux/module.h>
58 #include <linux/spinlock.h>
59 #include <scsi/scsi.h>
60 #include <scsi/scsi_tcq.h>
61 #include <scsi/scsi_device.h>
62 #include <scsi/scsi_transport.h>
63 #include <scsi/scsi_transport_spi.h>
64
65 #include "sym_glue.h"
66 #include "sym_nvram.h"
67
68 #define NAME53C         "sym53c"
69 #define NAME53C8XX      "sym53c8xx"
70
71 static int __devinit
72 pci_get_base_address(struct pci_dev *pdev, int index, u_long *base)
73 {
74         u32 tmp;
75 #define PCI_BAR_OFFSET(index) (PCI_BASE_ADDRESS_0 + (index<<2))
76
77         pci_read_config_dword(pdev, PCI_BAR_OFFSET(index), &tmp);
78         *base = tmp;
79         ++index;
80         if ((tmp & 0x7) == PCI_BASE_ADDRESS_MEM_TYPE_64) {
81 #if BITS_PER_LONG > 32
82                 pci_read_config_dword(pdev, PCI_BAR_OFFSET(index), &tmp);
83                 *base |= (((u_long)tmp) << 32);
84 #endif
85                 ++index;
86         }
87         return index;
88 #undef PCI_BAR_OFFSET
89 }
90
91 /* This lock protects only the memory allocation/free.  */
92 spinlock_t sym53c8xx_lock = SPIN_LOCK_UNLOCKED;
93
94 static struct scsi_transport_template *sym2_transport_template = NULL;
95
96 /*
97  *  Wrappers to the generic memory allocator.
98  */
99 void *sym_calloc(int size, char *name)
100 {
101         unsigned long flags;
102         void *m;
103         spin_lock_irqsave(&sym53c8xx_lock, flags);
104         m = sym_calloc_unlocked(size, name);
105         spin_unlock_irqrestore(&sym53c8xx_lock, flags);
106         return m;
107 }
108
109 void sym_mfree(void *m, int size, char *name)
110 {
111         unsigned long flags;
112         spin_lock_irqsave(&sym53c8xx_lock, flags);
113         sym_mfree_unlocked(m, size, name);
114         spin_unlock_irqrestore(&sym53c8xx_lock, flags);
115 }
116
117 void *__sym_calloc_dma(m_pool_ident_t dev_dmat, int size, char *name)
118 {
119         unsigned long flags;
120         void *m;
121         spin_lock_irqsave(&sym53c8xx_lock, flags);
122         m = __sym_calloc_dma_unlocked(dev_dmat, size, name);
123         spin_unlock_irqrestore(&sym53c8xx_lock, flags);
124         return m;
125 }
126
127 void __sym_mfree_dma(m_pool_ident_t dev_dmat, void *m, int size, char *name)
128 {
129         unsigned long flags;
130         spin_lock_irqsave(&sym53c8xx_lock, flags);
131         __sym_mfree_dma_unlocked(dev_dmat, m, size, name);
132         spin_unlock_irqrestore(&sym53c8xx_lock, flags);
133 }
134
135 m_addr_t __vtobus(m_pool_ident_t dev_dmat, void *m)
136 {
137         unsigned long flags;
138         m_addr_t b;
139         spin_lock_irqsave(&sym53c8xx_lock, flags);
140         b = __vtobus_unlocked(dev_dmat, m);
141         spin_unlock_irqrestore(&sym53c8xx_lock, flags);
142         return b;
143 }
144
145 /*
146  *  Driver host data structure.
147  */
148 struct host_data {
149         struct sym_hcb *ncb;
150 };
151
152 /*
153  *  Used by the eh thread to wait for command completion.
154  *  It is allocated on the eh thread stack.
155  */
156 struct sym_eh_wait {
157         struct semaphore sem;
158         struct timer_list timer;
159         void (*old_done)(struct scsi_cmnd *);
160         int to_do;
161         int timed_out;
162 };
163
164 /*
165  *  Driver private area in the SCSI command structure.
166  */
167 struct sym_ucmd {               /* Override the SCSI pointer structure */
168         SYM_QUEHEAD link_cmdq;  /* Must stay at offset ZERO */
169         dma_addr_t data_mapping;
170         u_char  data_mapped;
171         struct sym_eh_wait *eh_wait;
172 };
173
174 #define SYM_UCMD_PTR(cmd)  ((struct sym_ucmd *)(&(cmd)->SCp))
175 #define SYM_SCMD_PTR(ucmd) sym_que_entry(ucmd, struct scsi_cmnd, SCp)
176 #define SYM_SOFTC_PTR(cmd) (((struct host_data *)cmd->device->host->hostdata)->ncb)
177
178 static void __unmap_scsi_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
179 {
180         int dma_dir = cmd->sc_data_direction;
181
182         switch(SYM_UCMD_PTR(cmd)->data_mapped) {
183         case 2:
184                 pci_unmap_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
185                 break;
186         case 1:
187                 pci_unmap_single(pdev, SYM_UCMD_PTR(cmd)->data_mapping,
188                                  cmd->request_bufflen, dma_dir);
189                 break;
190         }
191         SYM_UCMD_PTR(cmd)->data_mapped = 0;
192 }
193
194 static dma_addr_t __map_scsi_single_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
195 {
196         dma_addr_t mapping;
197         int dma_dir = cmd->sc_data_direction;
198
199         mapping = pci_map_single(pdev, cmd->request_buffer,
200                                  cmd->request_bufflen, dma_dir);
201         if (mapping) {
202                 SYM_UCMD_PTR(cmd)->data_mapped  = 1;
203                 SYM_UCMD_PTR(cmd)->data_mapping = mapping;
204         }
205
206         return mapping;
207 }
208
209 static int __map_scsi_sg_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
210 {
211         int use_sg;
212         int dma_dir = cmd->sc_data_direction;
213
214         use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
215         if (use_sg > 0) {
216                 SYM_UCMD_PTR(cmd)->data_mapped  = 2;
217                 SYM_UCMD_PTR(cmd)->data_mapping = use_sg;
218         }
219
220         return use_sg;
221 }
222
223 static void __sync_scsi_data_for_cpu(struct pci_dev *pdev, struct scsi_cmnd *cmd)
224 {
225         int dma_dir = cmd->sc_data_direction;
226
227         switch(SYM_UCMD_PTR(cmd)->data_mapped) {
228         case 2:
229                 pci_dma_sync_sg_for_cpu(pdev, cmd->buffer, cmd->use_sg, dma_dir);
230                 break;
231         case 1:
232                 pci_dma_sync_single_for_cpu(pdev, SYM_UCMD_PTR(cmd)->data_mapping,
233                                             cmd->request_bufflen, dma_dir);
234                 break;
235         }
236 }
237
238 static void __sync_scsi_data_for_device(struct pci_dev *pdev, struct scsi_cmnd *cmd)
239 {
240         int dma_dir = cmd->sc_data_direction;
241
242         switch(SYM_UCMD_PTR(cmd)->data_mapped) {
243         case 2:
244                 pci_dma_sync_sg_for_device(pdev, cmd->buffer, cmd->use_sg, dma_dir);
245                 break;
246         case 1:
247                 pci_dma_sync_single_for_device(pdev, SYM_UCMD_PTR(cmd)->data_mapping,
248                                                cmd->request_bufflen, dma_dir);
249                 break;
250         }
251 }
252
253 #define unmap_scsi_data(np, cmd)        \
254                 __unmap_scsi_data(np->s.device, cmd)
255 #define map_scsi_single_data(np, cmd)   \
256                 __map_scsi_single_data(np->s.device, cmd)
257 #define map_scsi_sg_data(np, cmd)       \
258                 __map_scsi_sg_data(np->s.device, cmd)
259 #define sync_scsi_data_for_cpu(np, cmd)         \
260                 __sync_scsi_data_for_cpu(np->s.device, cmd)
261 #define sync_scsi_data_for_device(np, cmd)              \
262                 __sync_scsi_data_for_device(np->s.device, cmd)
263
264 /*
265  *  Complete a pending CAM CCB.
266  */
267 void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *ccb)
268 {
269         sym_remque(&SYM_UCMD_PTR(ccb)->link_cmdq);
270         unmap_scsi_data(np, ccb);
271         ccb->scsi_done(ccb);
272 }
273
274 void sym_xpt_done2(struct sym_hcb *np, struct scsi_cmnd *ccb, int cam_status)
275 {
276         sym_set_cam_status(ccb, cam_status);
277         sym_xpt_done(np, ccb);
278 }
279
280
281 /*
282  *  Print something that identifies the IO.
283  */
284 void sym_print_addr(struct sym_ccb *cp)
285 {
286         struct scsi_cmnd *cmd = cp->cam_ccb;
287         if (cmd)
288                 printf("%s:%d:%d:", sym_name(SYM_SOFTC_PTR(cmd)),
289                                 cmd->device->id, cmd->device->lun);
290 }
291
292 /*
293  *  Tell the SCSI layer about a BUS RESET.
294  */
295 void sym_xpt_async_bus_reset(struct sym_hcb *np)
296 {
297         printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
298         np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
299         np->s.settle_time_valid = 1;
300         if (sym_verbose >= 2)
301                 printf_info("%s: command processing suspended for %d seconds\n",
302                             sym_name(np), sym_driver_setup.settle_delay);
303 }
304
305 /*
306  *  Tell the SCSI layer about a BUS DEVICE RESET message sent.
307  */
308 void sym_xpt_async_sent_bdr(struct sym_hcb *np, int target)
309 {
310         printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
311 }
312
313 /*
314  *  Tell the SCSI layer about the new transfer parameters.
315  */
316 void sym_xpt_async_nego_wide(struct sym_hcb *np, int target)
317 {
318         if (sym_verbose < 3)
319                 return;
320         sym_announce_transfer_rate(np, target);
321 }
322
323 /*
324  *  Choose the more appropriate CAM status if 
325  *  the IO encountered an extended error.
326  */
327 static int sym_xerr_cam_status(int cam_status, int x_status)
328 {
329         if (x_status) {
330                 if      (x_status & XE_PARITY_ERR)
331                         cam_status = DID_PARITY;
332                 else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
333                         cam_status = DID_ERROR;
334                 else if (x_status & XE_BAD_PHASE)
335                         cam_status = DID_ERROR;
336                 else
337                         cam_status = DID_ERROR;
338         }
339         return cam_status;
340 }
341
342 /*
343  *  Build CAM result for a failed or auto-sensed IO.
344  */
345 void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
346 {
347         struct scsi_cmnd *csio = cp->cam_ccb;
348         u_int cam_status, scsi_status, drv_status;
349
350         drv_status  = 0;
351         cam_status  = DID_OK;
352         scsi_status = cp->ssss_status;
353
354         if (cp->host_flags & HF_SENSE) {
355                 scsi_status = cp->sv_scsi_status;
356                 resid = cp->sv_resid;
357                 if (sym_verbose && cp->sv_xerr_status)
358                         sym_print_xerr(cp, cp->sv_xerr_status);
359                 if (cp->host_status == HS_COMPLETE &&
360                     cp->ssss_status == S_GOOD &&
361                     cp->xerr_status == 0) {
362                         cam_status = sym_xerr_cam_status(DID_OK,
363                                                          cp->sv_xerr_status);
364                         drv_status = DRIVER_SENSE;
365                         /*
366                          *  Bounce back the sense data to user.
367                          */
368                         bzero(&csio->sense_buffer, sizeof(csio->sense_buffer));
369                         memcpy(csio->sense_buffer, cp->sns_bbuf,
370                               min(sizeof(csio->sense_buffer),
371                                   (size_t)SYM_SNS_BBUF_LEN));
372 #if 0
373                         /*
374                          *  If the device reports a UNIT ATTENTION condition 
375                          *  due to a RESET condition, we should consider all 
376                          *  disconnect CCBs for this unit as aborted.
377                          */
378                         if (1) {
379                                 u_char *p;
380                                 p  = (u_char *) csio->sense_data;
381                                 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
382                                         sym_clear_tasks(np, DID_ABORT,
383                                                         cp->target,cp->lun, -1);
384                         }
385 #endif
386                 } else {
387                         /*
388                          * Error return from our internal request sense.  This
389                          * is bad: we must clear the contingent allegiance
390                          * condition otherwise the device will always return
391                          * BUSY.  Use a big stick.
392                          */
393                         sym_reset_scsi_target(np, csio->device->id);
394                         cam_status = DID_ERROR;
395                 }
396         } else if (cp->host_status == HS_COMPLETE)      /* Bad SCSI status */
397                 cam_status = DID_OK;
398         else if (cp->host_status == HS_SEL_TIMEOUT)     /* Selection timeout */
399                 cam_status = DID_NO_CONNECT;
400         else if (cp->host_status == HS_UNEXPECTED)      /* Unexpected BUS FREE*/
401                 cam_status = DID_ERROR;
402         else {                                          /* Extended error */
403                 if (sym_verbose) {
404                         PRINT_ADDR(cp);
405                         printf ("COMMAND FAILED (%x %x %x).\n",
406                                 cp->host_status, cp->ssss_status,
407                                 cp->xerr_status);
408                 }
409                 /*
410                  *  Set the most appropriate value for CAM status.
411                  */
412                 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
413         }
414         csio->resid = resid;
415         csio->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
416 }
417
418
419 /*
420  *  Called on successfull INQUIRY response.
421  */
422 void sym_sniff_inquiry(struct sym_hcb *np, struct scsi_cmnd *cmd, int resid)
423 {
424         int retv;
425
426         if (!cmd || cmd->use_sg)
427                 return;
428
429         sync_scsi_data_for_cpu(np, cmd);
430         retv = __sym_sniff_inquiry(np, cmd->device->id, cmd->device->lun,
431                                    (u_char *) cmd->request_buffer,
432                                    cmd->request_bufflen - resid);
433         sync_scsi_data_for_device(np, cmd);
434         if (retv < 0)
435                 return;
436         else if (retv)
437                 sym_update_trans_settings(np, &np->target[cmd->device->id]);
438 }
439
440 /*
441  *  Build the scatter/gather array for an I/O.
442  */
443
444 static int sym_scatter_no_sglist(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
445 {
446         struct sym_tblmove *data = &cp->phys.data[SYM_CONF_MAX_SG-1];
447         int segment;
448
449         cp->data_len = cmd->request_bufflen;
450
451         if (cmd->request_bufflen) {
452                 dma_addr_t baddr = map_scsi_single_data(np, cmd);
453                 if (baddr) {
454                         sym_build_sge(np, data, baddr, cmd->request_bufflen);
455                         segment = 1;
456                 } else {
457                         segment = -2;
458                 }
459         } else {
460                 segment = 0;
461         }
462
463         return segment;
464 }
465
466 static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
467 {
468         int segment;
469         int use_sg = (int) cmd->use_sg;
470
471         cp->data_len = 0;
472
473         if (!use_sg)
474                 segment = sym_scatter_no_sglist(np, cp, cmd);
475         else if ((use_sg = map_scsi_sg_data(np, cmd)) > 0) {
476                 struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
477                 struct sym_tblmove *data;
478
479                 if (use_sg > SYM_CONF_MAX_SG) {
480                         unmap_scsi_data(np, cmd);
481                         return -1;
482                 }
483
484                 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
485
486                 for (segment = 0; segment < use_sg; segment++) {
487                         dma_addr_t baddr = sg_dma_address(&scatter[segment]);
488                         unsigned int len = sg_dma_len(&scatter[segment]);
489
490                         sym_build_sge(np, &data[segment], baddr, len);
491                         cp->data_len += len;
492                 }
493         } else {
494                 segment = -2;
495         }
496
497         return segment;
498 }
499
500 /*
501  *  Queue a SCSI command.
502  */
503 static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *ccb)
504 {
505 /*      struct scsi_device        *device    = ccb->device; */
506         struct sym_tcb *tp;
507         struct sym_lcb *lp;
508         struct sym_ccb *cp;
509         int     order;
510
511         /*
512          *  Minimal checkings, so that we will not 
513          *  go outside our tables.
514          */
515         if (ccb->device->id == np->myaddr ||
516             ccb->device->id >= SYM_CONF_MAX_TARGET ||
517             ccb->device->lun >= SYM_CONF_MAX_LUN) {
518                 sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
519                 return 0;
520         }
521
522         /*
523          *  Retreive the target descriptor.
524          */
525         tp = &np->target[ccb->device->id];
526
527         /*
528          *  Complete the 1st INQUIRY command with error 
529          *  condition if the device is flagged NOSCAN 
530          *  at BOOT in the NVRAM. This may speed up 
531          *  the boot and maintain coherency with BIOS 
532          *  device numbering. Clearing the flag allows 
533          *  user to rescan skipped devices later.
534          *  We also return error for devices not flagged 
535          *  for SCAN LUNS in the NVRAM since some mono-lun 
536          *  devices behave badly when asked for some non 
537          *  zero LUN. Btw, this is an absolute hack.:-)
538          */
539         if (ccb->cmnd[0] == 0x12 || ccb->cmnd[0] == 0x0) {
540                 if ((tp->usrflags & SYM_SCAN_BOOT_DISABLED) ||
541                     ((tp->usrflags & SYM_SCAN_LUNS_DISABLED) && 
542                      ccb->device->lun != 0)) {
543                         tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
544                         sym_xpt_done2(np, ccb, CAM_DEV_NOT_THERE);
545                         return 0;
546                 }
547         }
548
549         /*
550          *  Select tagged/untagged.
551          */
552         lp = sym_lp(np, tp, ccb->device->lun);
553         order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
554
555         /*
556          *  Queue the SCSI IO.
557          */
558         cp = sym_get_ccb(np, ccb->device->id, ccb->device->lun, order);
559         if (!cp)
560                 return 1;       /* Means resource shortage */
561         sym_queue_scsiio(np, ccb, cp);
562         return 0;
563 }
564
565 /*
566  *  Setup buffers and pointers that address the CDB.
567  */
568 static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *ccb, struct sym_ccb *cp)
569 {
570         u32     cmd_ba;
571         int     cmd_len;
572
573         /*
574          *  CDB is 16 bytes max.
575          */
576         if (ccb->cmd_len > sizeof(cp->cdb_buf)) {
577                 sym_set_cam_status(cp->cam_ccb, CAM_REQ_INVALID);
578                 return -1;
579         }
580
581         memcpy(cp->cdb_buf, ccb->cmnd, ccb->cmd_len);
582         cmd_ba  = CCB_BA (cp, cdb_buf[0]);
583         cmd_len = ccb->cmd_len;
584
585         cp->phys.cmd.addr       = cpu_to_scr(cmd_ba);
586         cp->phys.cmd.size       = cpu_to_scr(cmd_len);
587
588         return 0;
589 }
590
591 /*
592  *  Setup pointers that address the data and start the I/O.
593  */
594 int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *csio, struct sym_ccb *cp)
595 {
596         int dir;
597         struct sym_tcb *tp = &np->target[cp->target];
598         struct sym_lcb *lp = sym_lp(np, tp, cp->lun);
599
600         /*
601          *  Build the CDB.
602          */
603         if (sym_setup_cdb(np, csio, cp))
604                 goto out_abort;
605
606         /*
607          *  No direction means no data.
608          */
609         dir = csio->sc_data_direction;
610         if (dir != DMA_NONE) {
611                 cp->segments = sym_scatter(np, cp, csio);
612                 if (cp->segments < 0) {
613                         if (cp->segments == -2)
614                                 sym_set_cam_status(csio, CAM_RESRC_UNAVAIL);
615                         else
616                                 sym_set_cam_status(csio, CAM_REQ_TOO_BIG);
617                         goto out_abort;
618                 }
619         } else {
620                 cp->data_len = 0;
621                 cp->segments = 0;
622         }
623
624         /*
625          *  Set data pointers.
626          */
627         sym_setup_data_pointers(np, cp, dir);
628
629         /*
630          *  When `#ifed 1', the code below makes the driver 
631          *  panic on the first attempt to write to a SCSI device.
632          *  It is the first test we want to do after a driver 
633          *  change that does not seem obviously safe. :)
634          */
635 #if 0
636         switch (cp->cdb_buf[0]) {
637         case 0x0A: case 0x2A: case 0xAA:
638                 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
639                 MDELAY(10000);
640                 break;
641         default:
642                 break;
643         }
644 #endif
645
646         /*
647          *      activate this job.
648          */
649         if (lp)
650                 sym_start_next_ccbs(np, lp, 2);
651         else
652                 sym_put_start_queue(np, cp);
653         return 0;
654
655 out_abort:
656         sym_free_ccb(np, cp);
657         sym_xpt_done(np, csio);
658         return 0;
659 }
660
661
662 /*
663  *  timer daemon.
664  *
665  *  Misused to keep the driver running when
666  *  interrupts are not configured correctly.
667  */
668 static void sym_timer(struct sym_hcb *np)
669 {
670         unsigned long thistime = jiffies;
671
672         /*
673          *  Restart the timer.
674          */
675         np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
676         add_timer(&np->s.timer);
677
678         /*
679          *  If we are resetting the ncr, wait for settle_time before 
680          *  clearing it. Then command processing will be resumed.
681          */
682         if (np->s.settle_time_valid) {
683                 if (time_before_eq(np->s.settle_time, thistime)) {
684                         if (sym_verbose >= 2 )
685                                 printk("%s: command processing resumed\n",
686                                        sym_name(np));
687                         np->s.settle_time_valid = 0;
688                 }
689                 return;
690         }
691
692         /*
693          *      Nothing to do for now, but that may come.
694          */
695         if (np->s.lasttime + 4*HZ < thistime) {
696                 np->s.lasttime = thistime;
697         }
698
699 #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
700         /*
701          *  Some way-broken PCI bridges may lead to 
702          *  completions being lost when the clearing 
703          *  of the INTFLY flag by the CPU occurs 
704          *  concurrently with the chip raising this flag.
705          *  If this ever happen, lost completions will 
706          * be reaped here.
707          */
708         sym_wakeup_done(np);
709 #endif
710 }
711
712
713 /*
714  *  PCI BUS error handler.
715  */
716 void sym_log_bus_error(struct sym_hcb *np)
717 {
718         u_short pci_sts;
719         pci_read_config_word(np->s.device, PCI_STATUS, &pci_sts);
720         if (pci_sts & 0xf900) {
721                 pci_write_config_word(np->s.device, PCI_STATUS, pci_sts);
722                 printf("%s: PCI STATUS = 0x%04x\n",
723                         sym_name(np), pci_sts & 0xf900);
724         }
725 }
726
727
728 /*
729  *  Requeue awaiting commands.
730  */
731 static void sym_requeue_awaiting_cmds(struct sym_hcb *np)
732 {
733         struct scsi_cmnd *cmd;
734         struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
735         SYM_QUEHEAD tmp_cmdq;
736         int sts;
737
738         sym_que_move(&np->s.wait_cmdq, &tmp_cmdq);
739
740         while ((ucp = (struct sym_ucmd *) sym_remque_head(&tmp_cmdq)) != 0) {
741                 sym_insque_tail(&ucp->link_cmdq, &np->s.busy_cmdq);
742                 cmd = SYM_SCMD_PTR(ucp);
743                 sts = sym_queue_command(np, cmd);
744                 if (sts) {
745                         sym_remque(&ucp->link_cmdq);
746                         sym_insque_head(&ucp->link_cmdq, &np->s.wait_cmdq);
747                 }
748         }
749 }
750
751 /*
752  * queuecommand method.  Entered with the host adapter lock held and
753  * interrupts disabled.
754  */
755 static int sym53c8xx_queue_command(struct scsi_cmnd *cmd,
756                                         void (*done)(struct scsi_cmnd *))
757 {
758         struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
759         struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
760         int sts = 0;
761
762         cmd->scsi_done     = done;
763         cmd->host_scribble = NULL;
764         memset(ucp, 0, sizeof(*ucp));
765
766         /*
767          *  Shorten our settle_time if needed for 
768          *  this command not to time out.
769          */
770         if (np->s.settle_time_valid && cmd->timeout_per_command) {
771                 unsigned long tlimit = jiffies + cmd->timeout_per_command;
772                 tlimit -= SYM_CONF_TIMER_INTERVAL*2;
773                 if (time_after(np->s.settle_time, tlimit)) {
774                         np->s.settle_time = tlimit;
775                 }
776         }
777
778         if (np->s.settle_time_valid || !sym_que_empty(&np->s.wait_cmdq)) {
779                 sym_insque_tail(&ucp->link_cmdq, &np->s.wait_cmdq);
780                 goto out;
781         }
782
783         sym_insque_tail(&ucp->link_cmdq, &np->s.busy_cmdq);
784         sts = sym_queue_command(np, cmd);
785         if (sts) {
786                 sym_remque(&ucp->link_cmdq);
787                 sym_insque_tail(&ucp->link_cmdq, &np->s.wait_cmdq);
788         }
789 out:
790         return 0;
791 }
792
793 /*
794  *  Linux entry point of the interrupt handler.
795  */
796 static irqreturn_t sym53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
797 {
798         unsigned long flags;
799         struct sym_hcb *np = (struct sym_hcb *)dev_id;
800
801         if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
802
803         spin_lock_irqsave(np->s.host->host_lock, flags);
804
805         sym_interrupt(np);
806
807         /*
808          * push queue walk-through to tasklet
809          */
810         if (!sym_que_empty(&np->s.wait_cmdq) && !np->s.settle_time_valid)
811                 sym_requeue_awaiting_cmds(np);
812
813         spin_unlock_irqrestore(np->s.host->host_lock, flags);
814
815         if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
816
817         return IRQ_HANDLED;
818 }
819
820 /*
821  *  Linux entry point of the timer handler
822  */
823 static void sym53c8xx_timer(unsigned long npref)
824 {
825         struct sym_hcb *np = (struct sym_hcb *)npref;
826         unsigned long flags;
827
828         spin_lock_irqsave(np->s.host->host_lock, flags);
829
830         sym_timer(np);
831
832         if (!sym_que_empty(&np->s.wait_cmdq) && !np->s.settle_time_valid)
833                 sym_requeue_awaiting_cmds(np);
834
835         spin_unlock_irqrestore(np->s.host->host_lock, flags);
836 }
837
838
839 /*
840  *  What the eh thread wants us to perform.
841  */
842 #define SYM_EH_ABORT            0
843 #define SYM_EH_DEVICE_RESET     1
844 #define SYM_EH_BUS_RESET        2
845 #define SYM_EH_HOST_RESET       3
846
847 /*
848  *  What we will do regarding the involved SCSI command.
849  */
850 #define SYM_EH_DO_IGNORE        0
851 #define SYM_EH_DO_COMPLETE      1
852 #define SYM_EH_DO_WAIT          2
853
854 /*
855  *  Our general completion handler.
856  */
857 static void __sym_eh_done(struct scsi_cmnd *cmd, int timed_out)
858 {
859         struct sym_eh_wait *ep = SYM_UCMD_PTR(cmd)->eh_wait;
860         if (!ep)
861                 return;
862
863         /* Try to avoid a race here (not 100% safe) */
864         if (!timed_out) {
865                 ep->timed_out = 0;
866                 if (ep->to_do == SYM_EH_DO_WAIT && !del_timer(&ep->timer))
867                         return;
868         }
869
870         /* Revert everything */
871         SYM_UCMD_PTR(cmd)->eh_wait = 0;
872         cmd->scsi_done = ep->old_done;
873
874         /* Wake up the eh thread if it wants to sleep */
875         if (ep->to_do == SYM_EH_DO_WAIT)
876                 up(&ep->sem);
877 }
878
879 /*
880  *  scsi_done() alias when error recovery is in progress. 
881  */
882 static void sym_eh_done(struct scsi_cmnd *cmd) { __sym_eh_done(cmd, 0); }
883
884 /*
885  *  Some timeout handler to avoid waiting too long.
886  */
887 static void sym_eh_timeout(u_long p) { __sym_eh_done((struct scsi_cmnd *)p, 1); }
888
889 /*
890  *  Generic method for our eh processing.
891  *  The 'op' argument tells what we have to do.
892  */
893 static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
894 {
895         struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
896         SYM_QUEHEAD *qp;
897         int to_do = SYM_EH_DO_IGNORE;
898         int sts = -1;
899         struct sym_eh_wait eh, *ep = &eh;
900         char devname[20];
901
902         sprintf(devname, "%s:%d:%d", sym_name(np), cmd->device->id, cmd->device->lun);
903
904         printf_warning("%s: %s operation started.\n", devname, opname);
905
906 #if 0
907         /* This one should be the result of some race, thus to ignore */
908         if (cmd->serial_number != cmd->serial_number_at_timeout)
909                 goto prepare;
910 #endif
911
912         /* This one is not queued to the core driver -> to complete here */ 
913         FOR_EACH_QUEUED_ELEMENT(&np->s.wait_cmdq, qp) {
914                 if (SYM_SCMD_PTR(qp) == cmd) {
915                         to_do = SYM_EH_DO_COMPLETE;
916                         goto prepare;
917                 }
918         }
919
920         /* This one is queued in some place -> to wait for completion */
921         FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
922                 struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
923                 if (cp->cam_ccb == cmd) {
924                         to_do = SYM_EH_DO_WAIT;
925                         goto prepare;
926                 }
927         }
928
929 prepare:
930         /* Prepare stuff to either ignore, complete or wait for completion */
931         switch(to_do) {
932         default:
933         case SYM_EH_DO_IGNORE:
934                 break;
935         case SYM_EH_DO_WAIT:
936                 init_MUTEX_LOCKED(&ep->sem);
937                 /* fall through */
938         case SYM_EH_DO_COMPLETE:
939                 ep->old_done = cmd->scsi_done;
940                 cmd->scsi_done = sym_eh_done;
941                 SYM_UCMD_PTR(cmd)->eh_wait = ep;
942         }
943
944         /* Try to proceed the operation we have been asked for */
945         sts = -1;
946         switch(op) {
947         case SYM_EH_ABORT:
948                 sts = sym_abort_scsiio(np, cmd, 1);
949                 break;
950         case SYM_EH_DEVICE_RESET:
951                 sts = sym_reset_scsi_target(np, cmd->device->id);
952                 break;
953         case SYM_EH_BUS_RESET:
954                 sym_reset_scsi_bus(np, 1);
955                 sts = 0;
956                 break;
957         case SYM_EH_HOST_RESET:
958                 sym_reset_scsi_bus(np, 0);
959                 sym_start_up (np, 1);
960                 sts = 0;
961                 break;
962         default:
963                 break;
964         }
965
966         /* On error, restore everything and cross fingers :) */
967         if (sts) {
968                 SYM_UCMD_PTR(cmd)->eh_wait = 0;
969                 cmd->scsi_done = ep->old_done;
970                 to_do = SYM_EH_DO_IGNORE;
971         }
972
973         ep->to_do = to_do;
974         /* Complete the command with locks held as required by the driver */
975         if (to_do == SYM_EH_DO_COMPLETE)
976                 sym_xpt_done2(np, cmd, CAM_REQ_ABORTED);
977
978         /* Wait for completion with locks released, as required by kernel */
979         if (to_do == SYM_EH_DO_WAIT) {
980                 init_timer(&ep->timer);
981                 ep->timer.expires = jiffies + (5*HZ);
982                 ep->timer.function = sym_eh_timeout;
983                 ep->timer.data = (u_long)cmd;
984                 ep->timed_out = 1;      /* Be pessimistic for once :) */
985                 add_timer(&ep->timer);
986                 spin_unlock_irq(np->s.host->host_lock);
987                 down(&ep->sem);
988                 spin_lock_irq(np->s.host->host_lock);
989                 if (ep->timed_out)
990                         sts = -2;
991         }
992         printf_warning("%s: %s operation %s.\n", devname, opname,
993                         sts==0?"complete":sts==-2?"timed-out":"failed");
994         return sts? SCSI_FAILED : SCSI_SUCCESS;
995 }
996
997
998 /*
999  * Error handlers called from the eh thread (one thread per HBA).
1000  */
1001 static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
1002 {
1003         return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
1004 }
1005
1006 static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
1007 {
1008         return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
1009 }
1010
1011 static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
1012 {
1013         return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
1014 }
1015
1016 static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
1017 {
1018         return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
1019 }
1020
1021 /*
1022  *  Tune device queuing depth, according to various limits.
1023  */
1024 static void sym_tune_dev_queuing(struct sym_hcb *np, int target, int lun, u_short reqtags)
1025 {
1026         struct sym_tcb *tp = &np->target[target];
1027         struct sym_lcb *lp = sym_lp(np, tp, lun);
1028         u_short oldtags;
1029
1030         if (!lp)
1031                 return;
1032
1033         oldtags = lp->s.reqtags;
1034
1035         if (reqtags > lp->s.scdev_depth)
1036                 reqtags = lp->s.scdev_depth;
1037
1038         lp->started_limit = reqtags ? reqtags : 2;
1039         lp->started_max   = 1;
1040         lp->s.reqtags     = reqtags;
1041
1042         if (reqtags != oldtags) {
1043                 printf_info("%s:%d:%d: "
1044                          "tagged command queuing %s, command queue depth %d.\n",
1045                           sym_name(np), target, lun,
1046                           lp->s.reqtags ? "enabled" : "disabled",
1047                           lp->started_limit);
1048         }
1049 }
1050
1051 #ifdef  SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT
1052 /*
1053  *  Linux select queue depths function
1054  */
1055 #define DEF_DEPTH       (sym_driver_setup.max_tag)
1056 #define ALL_TARGETS     -2
1057 #define NO_TARGET       -1
1058 #define ALL_LUNS        -2
1059 #define NO_LUN          -1
1060
1061 static int device_queue_depth(struct sym_hcb *np, int target, int lun)
1062 {
1063         int c, h, t, u, v;
1064         char *p = sym_driver_setup.tag_ctrl;
1065         char *ep;
1066
1067         h = -1;
1068         t = NO_TARGET;
1069         u = NO_LUN;
1070         while ((c = *p++) != 0) {
1071                 v = simple_strtoul(p, &ep, 0);
1072                 switch(c) {
1073                 case '/':
1074                         ++h;
1075                         t = ALL_TARGETS;
1076                         u = ALL_LUNS;
1077                         break;
1078                 case 't':
1079                         if (t != target)
1080                                 t = (target == v) ? v : NO_TARGET;
1081                         u = ALL_LUNS;
1082                         break;
1083                 case 'u':
1084                         if (u != lun)
1085                                 u = (lun == v) ? v : NO_LUN;
1086                         break;
1087                 case 'q':
1088                         if (h == np->s.unit &&
1089                                 (t == ALL_TARGETS || t == target) &&
1090                                 (u == ALL_LUNS    || u == lun))
1091                                 return v;
1092                         break;
1093                 case '-':
1094                         t = ALL_TARGETS;
1095                         u = ALL_LUNS;
1096                         break;
1097                 default:
1098                         break;
1099                 }
1100                 p = ep;
1101         }
1102         return DEF_DEPTH;
1103 }
1104 #else
1105 #define device_queue_depth(np, t, l)    (sym_driver_setup.max_tag)
1106 #endif  /* SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT */
1107
1108 /*
1109  * Linux entry point for device queue sizing.
1110  */
1111 static int sym53c8xx_slave_configure(struct scsi_device *device)
1112 {
1113         struct Scsi_Host *host = device->host;
1114         struct sym_hcb *np;
1115         struct sym_tcb *tp;
1116         struct sym_lcb *lp;
1117         int reqtags, depth_to_use;
1118
1119         np = ((struct host_data *) host->hostdata)->ncb;
1120         tp = &np->target[device->id];
1121
1122         /*
1123          *  Get user settings for transfer parameters.
1124          */
1125         tp->inq_byte7_valid = (INQ7_SYNC|INQ7_WIDE16);
1126         sym_update_trans_settings(np, tp);
1127
1128         /*
1129          *  Allocate the LCB if not yet.
1130          *  If it fail, we may well be in the sh*t. :)
1131          */
1132         lp = sym_alloc_lcb(np, device->id, device->lun);
1133         if (!lp)
1134                 return -ENOMEM;
1135
1136         /*
1137          *  Get user flags.
1138          */
1139         lp->curr_flags = lp->user_flags;
1140
1141         /*
1142          *  Select queue depth from driver setup.
1143          *  Donnot use more than configured by user.
1144          *  Use at least 2.
1145          *  Donnot use more than our maximum.
1146          */
1147         reqtags = device_queue_depth(np, device->id, device->lun);
1148         if (reqtags > tp->usrtags)
1149                 reqtags = tp->usrtags;
1150         if (!device->tagged_supported)
1151                 reqtags = 0;
1152 #if 1 /* Avoid to locally queue commands for no good reasons */
1153         if (reqtags > SYM_CONF_MAX_TAG)
1154                 reqtags = SYM_CONF_MAX_TAG;
1155         depth_to_use = (reqtags ? reqtags : 2);
1156 #else
1157         depth_to_use = (reqtags ? SYM_CONF_MAX_TAG : 2);
1158 #endif
1159         scsi_adjust_queue_depth(device,
1160                                 (device->tagged_supported ?
1161                                  MSG_SIMPLE_TAG : 0),
1162                                 depth_to_use);
1163         lp->s.scdev_depth = depth_to_use;
1164         sym_tune_dev_queuing(np, device->id, device->lun, reqtags);
1165
1166         spi_dv_device(device);
1167
1168         return 0;
1169 }
1170
1171 /*
1172  *  Linux entry point for info() function
1173  */
1174 static const char *sym53c8xx_info (struct Scsi_Host *host)
1175 {
1176         return sym_driver_name();
1177 }
1178
1179
1180 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1181 /*
1182  *  Proc file system stuff
1183  *
1184  *  A read operation returns adapter information.
1185  *  A write operation is a control command.
1186  *  The string is parsed in the driver code and the command is passed 
1187  *  to the sym_usercmd() function.
1188  */
1189
1190 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1191
1192 struct  sym_usrcmd {
1193         u_long  target;
1194         u_long  lun;
1195         u_long  data;
1196         u_long  cmd;
1197 };
1198
1199 #define UC_SETSYNC      10
1200 #define UC_SETTAGS      11
1201 #define UC_SETDEBUG     12
1202 #define UC_SETWIDE      14
1203 #define UC_SETFLAG      15
1204 #define UC_SETVERBOSE   17
1205 #define UC_RESETDEV     18
1206 #define UC_CLEARDEV     19
1207
1208 static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
1209 {
1210         struct sym_tcb *tp;
1211         int t, l;
1212
1213         switch (uc->cmd) {
1214         case 0: return;
1215
1216 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1217         case UC_SETDEBUG:
1218                 sym_debug_flags = uc->data;
1219                 break;
1220 #endif
1221         case UC_SETVERBOSE:
1222                 np->verbose = uc->data;
1223                 break;
1224         default:
1225                 /*
1226                  * We assume that other commands apply to targets.
1227                  * This should always be the case and avoid the below 
1228                  * 4 lines to be repeated 6 times.
1229                  */
1230                 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
1231                         if (!((uc->target >> t) & 1))
1232                                 continue;
1233                         tp = &np->target[t];
1234
1235                         switch (uc->cmd) {
1236
1237                         case UC_SETSYNC:
1238                                 if (!uc->data || uc->data >= 255) {
1239                                         tp->tinfo.goal.options = 0;
1240                                         tp->tinfo.goal.offset  = 0;
1241                                         break;
1242                                 }
1243                                 if (uc->data <= 9 && np->minsync_dt) {
1244                                         if (uc->data < np->minsync_dt)
1245                                                 uc->data = np->minsync_dt;
1246                                         tp->tinfo.goal.options = PPR_OPT_DT;
1247                                         tp->tinfo.goal.width   = 1;
1248                                         tp->tinfo.goal.period = uc->data;
1249                                         tp->tinfo.goal.offset = np->maxoffs_dt;
1250                                 } else {
1251                                         if (uc->data < np->minsync)
1252                                                 uc->data = np->minsync;
1253                                         tp->tinfo.goal.options = 0;
1254                                         tp->tinfo.goal.period = uc->data;
1255                                         tp->tinfo.goal.offset = np->maxoffs;
1256                                 }
1257                                 break;
1258                         case UC_SETWIDE:
1259                                 tp->tinfo.goal.width = uc->data ? 1 : 0;
1260                                 break;
1261                         case UC_SETTAGS:
1262                                 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
1263                                         sym_tune_dev_queuing(np, t,l, uc->data);
1264                                 break;
1265                         case UC_RESETDEV:
1266                                 tp->to_reset = 1;
1267                                 np->istat_sem = SEM;
1268                                 OUTB (nc_istat, SIGP|SEM);
1269                                 break;
1270                         case UC_CLEARDEV:
1271                                 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
1272                                         struct sym_lcb *lp = sym_lp(np, tp, l);
1273                                         if (lp) lp->to_clear = 1;
1274                                 }
1275                                 np->istat_sem = SEM;
1276                                 OUTB (nc_istat, SIGP|SEM);
1277                                 break;
1278                         case UC_SETFLAG:
1279                                 tp->usrflags = uc->data;
1280                                 break;
1281                         }
1282                 }
1283                 break;
1284         }
1285 }
1286
1287 #define digit_to_bin(c) ((c) - '0')
1288
1289 static int skip_spaces(char *ptr, int len)
1290 {
1291         int cnt, c;
1292
1293         for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
1294
1295         return (len - cnt);
1296 }
1297
1298 static int get_int_arg(char *ptr, int len, u_long *pv)
1299 {
1300         int     cnt, c;
1301         u_long  v;
1302
1303         for (v = 0, cnt = len; cnt > 0 && (c = *ptr++) && isdigit(c); cnt--) {
1304                 v = (v * 10) + digit_to_bin(c);
1305         }
1306
1307         if (pv)
1308                 *pv = v;
1309
1310         return (len - cnt);
1311 }
1312
1313 static int is_keyword(char *ptr, int len, char *verb)
1314 {
1315         int verb_len = strlen(verb);
1316
1317         if (len >= verb_len && !memcmp(verb, ptr, verb_len))
1318                 return verb_len;
1319         else
1320                 return 0;
1321
1322 }
1323
1324 #define SKIP_SPACES(min_spaces)                                         \
1325         if ((arg_len = skip_spaces(ptr, len)) < (min_spaces))           \
1326                 return -EINVAL;                                         \
1327         ptr += arg_len; len -= arg_len;
1328
1329 #define GET_INT_ARG(v)                                                  \
1330         if (!(arg_len = get_int_arg(ptr, len, &(v))))                   \
1331                 return -EINVAL;                                         \
1332         ptr += arg_len; len -= arg_len;
1333
1334
1335 /*
1336  * Parse a control command
1337  */
1338
1339 static int sym_user_command(struct sym_hcb *np, char *buffer, int length)
1340 {
1341         char *ptr       = buffer;
1342         int len         = length;
1343         struct sym_usrcmd cmd, *uc = &cmd;
1344         int             arg_len;
1345         u_long          target;
1346
1347         bzero(uc, sizeof(*uc));
1348
1349         if (len > 0 && ptr[len-1] == '\n')
1350                 --len;
1351
1352         if      ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1353                 uc->cmd = UC_SETSYNC;
1354         else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1355                 uc->cmd = UC_SETTAGS;
1356         else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1357                 uc->cmd = UC_SETVERBOSE;
1358         else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1359                 uc->cmd = UC_SETWIDE;
1360 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1361         else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1362                 uc->cmd = UC_SETDEBUG;
1363 #endif
1364         else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1365                 uc->cmd = UC_SETFLAG;
1366         else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1367                 uc->cmd = UC_RESETDEV;
1368         else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1369                 uc->cmd = UC_CLEARDEV;
1370         else
1371                 arg_len = 0;
1372
1373 #ifdef DEBUG_PROC_INFO
1374 printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1375 #endif
1376
1377         if (!arg_len)
1378                 return -EINVAL;
1379         ptr += arg_len; len -= arg_len;
1380
1381         switch(uc->cmd) {
1382         case UC_SETSYNC:
1383         case UC_SETTAGS:
1384         case UC_SETWIDE:
1385         case UC_SETFLAG:
1386         case UC_RESETDEV:
1387         case UC_CLEARDEV:
1388                 SKIP_SPACES(1);
1389                 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1390                         ptr += arg_len; len -= arg_len;
1391                         uc->target = ~0;
1392                 } else {
1393                         GET_INT_ARG(target);
1394                         uc->target = (1<<target);
1395 #ifdef DEBUG_PROC_INFO
1396 printk("sym_user_command: target=%ld\n", target);
1397 #endif
1398                 }
1399                 break;
1400         }
1401
1402         switch(uc->cmd) {
1403         case UC_SETVERBOSE:
1404         case UC_SETSYNC:
1405         case UC_SETTAGS:
1406         case UC_SETWIDE:
1407                 SKIP_SPACES(1);
1408                 GET_INT_ARG(uc->data);
1409 #ifdef DEBUG_PROC_INFO
1410 printk("sym_user_command: data=%ld\n", uc->data);
1411 #endif
1412                 break;
1413 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1414         case UC_SETDEBUG:
1415                 while (len > 0) {
1416                         SKIP_SPACES(1);
1417                         if      ((arg_len = is_keyword(ptr, len, "alloc")))
1418                                 uc->data |= DEBUG_ALLOC;
1419                         else if ((arg_len = is_keyword(ptr, len, "phase")))
1420                                 uc->data |= DEBUG_PHASE;
1421                         else if ((arg_len = is_keyword(ptr, len, "queue")))
1422                                 uc->data |= DEBUG_QUEUE;
1423                         else if ((arg_len = is_keyword(ptr, len, "result")))
1424                                 uc->data |= DEBUG_RESULT;
1425                         else if ((arg_len = is_keyword(ptr, len, "scatter")))
1426                                 uc->data |= DEBUG_SCATTER;
1427                         else if ((arg_len = is_keyword(ptr, len, "script")))
1428                                 uc->data |= DEBUG_SCRIPT;
1429                         else if ((arg_len = is_keyword(ptr, len, "tiny")))
1430                                 uc->data |= DEBUG_TINY;
1431                         else if ((arg_len = is_keyword(ptr, len, "timing")))
1432                                 uc->data |= DEBUG_TIMING;
1433                         else if ((arg_len = is_keyword(ptr, len, "nego")))
1434                                 uc->data |= DEBUG_NEGO;
1435                         else if ((arg_len = is_keyword(ptr, len, "tags")))
1436                                 uc->data |= DEBUG_TAGS;
1437                         else if ((arg_len = is_keyword(ptr, len, "pointer")))
1438                                 uc->data |= DEBUG_POINTER;
1439                         else
1440                                 return -EINVAL;
1441                         ptr += arg_len; len -= arg_len;
1442                 }
1443 #ifdef DEBUG_PROC_INFO
1444 printk("sym_user_command: data=%ld\n", uc->data);
1445 #endif
1446                 break;
1447 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1448         case UC_SETFLAG:
1449                 while (len > 0) {
1450                         SKIP_SPACES(1);
1451                         if      ((arg_len = is_keyword(ptr, len, "no_disc")))
1452                                 uc->data &= ~SYM_DISC_ENABLED;
1453                         else
1454                                 return -EINVAL;
1455                         ptr += arg_len; len -= arg_len;
1456                 }
1457                 break;
1458         default:
1459                 break;
1460         }
1461
1462         if (len)
1463                 return -EINVAL;
1464         else {
1465                 unsigned long flags;
1466
1467                 spin_lock_irqsave(np->s.host->host_lock, flags);
1468                 sym_exec_user_command (np, uc);
1469                 spin_unlock_irqrestore(np->s.host->host_lock, flags);
1470         }
1471         return length;
1472 }
1473
1474 #endif  /* SYM_LINUX_USER_COMMAND_SUPPORT */
1475
1476
1477 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1478 /*
1479  *  Informations through the proc file system.
1480  */
1481 struct info_str {
1482         char *buffer;
1483         int length;
1484         int offset;
1485         int pos;
1486 };
1487
1488 static void copy_mem_info(struct info_str *info, char *data, int len)
1489 {
1490         if (info->pos + len > info->length)
1491                 len = info->length - info->pos;
1492
1493         if (info->pos + len < info->offset) {
1494                 info->pos += len;
1495                 return;
1496         }
1497         if (info->pos < info->offset) {
1498                 data += (info->offset - info->pos);
1499                 len  -= (info->offset - info->pos);
1500         }
1501
1502         if (len > 0) {
1503                 memcpy(info->buffer + info->pos, data, len);
1504                 info->pos += len;
1505         }
1506 }
1507
1508 static int copy_info(struct info_str *info, char *fmt, ...)
1509 {
1510         va_list args;
1511         char buf[81];
1512         int len;
1513
1514         va_start(args, fmt);
1515         len = vsprintf(buf, fmt, args);
1516         va_end(args);
1517
1518         copy_mem_info(info, buf, len);
1519         return len;
1520 }
1521
1522 /*
1523  *  Copy formatted information into the input buffer.
1524  */
1525 static int sym_host_info(struct sym_hcb *np, char *ptr, off_t offset, int len)
1526 {
1527         struct info_str info;
1528
1529         info.buffer     = ptr;
1530         info.length     = len;
1531         info.offset     = offset;
1532         info.pos        = 0;
1533
1534         copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
1535                          "revision id 0x%x\n",
1536                          np->s.chip_name, np->device_id, np->revision_id);
1537         copy_info(&info, "At PCI address %s, "
1538 #ifdef __sparc__
1539                 "IRQ %s\n",
1540 #else
1541                 "IRQ %d\n",
1542 #endif
1543                 pci_name(np->s.device),
1544 #ifdef __sparc__
1545                 __irq_itoa(np->s.irq));
1546 #else
1547                 (int) np->s.irq);
1548 #endif
1549         copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
1550                          (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1551                          np->maxwide ? "Wide" : "Narrow",
1552                          np->minsync_dt ? ", DT capable" : "");
1553
1554         copy_info(&info, "Max. started commands %d, "
1555                          "max. commands per LUN %d\n",
1556                          SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1557
1558         return info.pos > info.offset? info.pos - info.offset : 0;
1559 }
1560 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1561
1562 /*
1563  *  Entry point of the scsi proc fs of the driver.
1564  *  - func = 0 means read  (returns adapter infos)
1565  *  - func = 1 means write (not yet merget from sym53c8xx)
1566  */
1567 static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
1568                         char **start, off_t offset, int length, int func)
1569 {
1570         struct host_data *host_data;
1571         struct sym_hcb *np = 0;
1572         int retv;
1573
1574         host_data = (struct host_data *) host->hostdata;
1575         np = host_data->ncb;
1576         if (!np)
1577                 return -EINVAL;
1578
1579         if (func) {
1580 #ifdef  SYM_LINUX_USER_COMMAND_SUPPORT
1581                 retv = sym_user_command(np, buffer, length);
1582 #else
1583                 retv = -EINVAL;
1584 #endif
1585         } else {
1586                 if (start)
1587                         *start = buffer;
1588 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1589                 retv = sym_host_info(np, buffer, offset, length);
1590 #else
1591                 retv = -EINVAL;
1592 #endif
1593         }
1594
1595         return retv;
1596 }
1597 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1598
1599 /*
1600  *      Free controller resources.
1601  */
1602 static void sym_free_resources(struct sym_hcb *np)
1603 {
1604         /*
1605          *  Free O/S specific resources.
1606          */
1607         if (np->s.irq)
1608                 free_irq(np->s.irq, np);
1609 #ifndef SYM_CONF_IOMAPPED
1610         if (np->s.mmio_va)
1611                 iounmap(np->s.mmio_va);
1612 #endif
1613         if (np->s.ram_va)
1614                 iounmap(np->s.ram_va);
1615         /*
1616          *  Free O/S independent resources.
1617          */
1618         sym_hcb_free(np);
1619
1620         sym_mfree_dma(np, sizeof(*np), "HCB");
1621 }
1622
1623 /*
1624  *  Ask/tell the system about DMA addressing.
1625  */
1626 static int sym_setup_bus_dma_mask(struct sym_hcb *np)
1627 {
1628 #if   SYM_CONF_DMA_ADDRESSING_MODE == 0
1629         if (pci_set_dma_mask(np->s.device, 0xffffffffUL))
1630                 goto out_err32;
1631 #else
1632 #if   SYM_CONF_DMA_ADDRESSING_MODE == 1
1633 #define PciDmaMask      0xffffffffffULL
1634 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
1635 #define PciDmaMask      0xffffffffffffffffULL
1636 #endif
1637         if (np->features & FE_DAC) {
1638                 if (!pci_set_dma_mask(np->s.device, PciDmaMask)) {
1639                         np->use_dac = 1;
1640                         printf_info("%s: using 64 bit DMA addressing\n",
1641                                         sym_name(np));
1642                 } else {
1643                         if (pci_set_dma_mask(np->s.device, 0xffffffffUL))
1644                                 goto out_err32;
1645                 }
1646         }
1647 #undef  PciDmaMask
1648 #endif
1649         return 0;
1650
1651 out_err32:
1652         printf_warning("%s: 32 BIT DMA ADDRESSING NOT SUPPORTED\n",
1653                         sym_name(np));
1654         return -1;
1655 }
1656
1657 /*
1658  *  Host attach and initialisations.
1659  *
1660  *  Allocate host data and ncb structure.
1661  *  Remap MMIO region.
1662  *  Do chip initialization.
1663  *  If all is OK, install interrupt handling and
1664  *  start the timer daemon.
1665  */
1666 static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
1667                 int unit, struct sym_device *dev)
1668 {
1669         struct host_data *host_data;
1670         struct sym_hcb *np = NULL;
1671         struct Scsi_Host *instance = NULL;
1672         unsigned long flags;
1673         struct sym_fw *fw;
1674
1675         printk(KERN_INFO
1676                 "sym%d: <%s> rev 0x%x at pci %s "
1677 #ifdef __sparc__
1678                 "irq %s\n",
1679 #else
1680                 "irq %d\n",
1681 #endif
1682                 unit, dev->chip.name, dev->chip.revision_id,
1683                 pci_name(dev->pdev),
1684 #ifdef __sparc__
1685                 __irq_itoa(dev->s.irq));
1686 #else
1687                 dev->s.irq);
1688 #endif
1689
1690         /*
1691          *  Get the firmware for this chip.
1692          */
1693         fw = sym_find_firmware(&dev->chip);
1694         if (!fw)
1695                 goto attach_failed;
1696
1697         /*
1698          *      Allocate host_data structure
1699          */
1700         instance = scsi_host_alloc(tpnt, sizeof(*host_data));
1701         if (!instance)
1702                 goto attach_failed;
1703         host_data = (struct host_data *) instance->hostdata;
1704
1705         /*
1706          *  Allocate immediately the host control block, 
1707          *  since we are only expecting to succeed. :)
1708          *  We keep track in the HCB of all the resources that 
1709          *  are to be released on error.
1710          */
1711         np = __sym_calloc_dma(dev->pdev, sizeof(*np), "HCB");
1712         if (!np)
1713                 goto attach_failed;
1714         np->s.device = dev->pdev;
1715         np->bus_dmat = dev->pdev; /* Result in 1 DMA pool per HBA */
1716         host_data->ncb = np;
1717         np->s.host = instance;
1718
1719         pci_set_drvdata(dev->pdev, np);
1720
1721         /*
1722          *  Copy some useful infos to the HCB.
1723          */
1724         np->hcb_ba      = vtobus(np);
1725         np->verbose     = sym_driver_setup.verbose;
1726         np->s.device    = dev->pdev;
1727         np->s.unit      = unit;
1728         np->device_id   = dev->chip.device_id;
1729         np->revision_id = dev->chip.revision_id;
1730         np->features    = dev->chip.features;
1731         np->clock_divn  = dev->chip.nr_divisor;
1732         np->maxoffs     = dev->chip.offset_max;
1733         np->maxburst    = dev->chip.burst_max;
1734         np->myaddr      = dev->host_id;
1735
1736         /*
1737          *  Edit its name.
1738          */
1739         strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
1740         sprintf(np->s.inst_name, "sym%d", np->s.unit);
1741
1742         /*
1743          *  Ask/tell the system about DMA addressing.
1744          */
1745         if (sym_setup_bus_dma_mask(np))
1746                 goto attach_failed;
1747
1748         /*
1749          *  Try to map the controller chip to
1750          *  virtual and physical memory.
1751          */
1752         np->mmio_ba     = (u32)dev->s.base;
1753         np->s.io_ws     = (np->features & FE_IO256)? 256 : 128;
1754
1755 #ifndef SYM_CONF_IOMAPPED
1756         np->s.mmio_va = ioremap(dev->s.base_c, np->s.io_ws);
1757         if (!np->s.mmio_va) {
1758                 printf_err("%s: can't map PCI MMIO region\n", sym_name(np));
1759                 goto attach_failed;
1760         } else if (sym_verbose > 1)
1761                 printf_info("%s: using memory mapped IO\n", sym_name(np));
1762 #endif /* !defined SYM_CONF_IOMAPPED */
1763
1764         np->s.io_port = dev->s.io_port;
1765
1766         /*
1767          *  Map on-chip RAM if present and supported.
1768          */
1769         if (!(np->features & FE_RAM))
1770                 dev->s.base_2 = 0;
1771         if (dev->s.base_2) {
1772                 np->ram_ba = (u32)dev->s.base_2;
1773                 if (np->features & FE_RAM8K)
1774                         np->ram_ws = 8192;
1775                 else
1776                         np->ram_ws = 4096;
1777                 np->s.ram_va = ioremap(dev->s.base_2_c, np->ram_ws);
1778                 if (!np->s.ram_va) {
1779                         printf_err("%s: can't map PCI MEMORY region\n",
1780                                 sym_name(np));
1781                         goto attach_failed;
1782                 }
1783         }
1784
1785         /*
1786          *  Perform O/S independent stuff.
1787          */
1788         if (sym_hcb_attach(np, fw, dev->nvram))
1789                 goto attach_failed;
1790
1791
1792         /*
1793          *  Install the interrupt handler.
1794          *  If we synchonize the C code with SCRIPTS on interrupt, 
1795          *  we donnot want to share the INTR line at all.
1796          */
1797         if (request_irq(dev->s.irq, sym53c8xx_intr, SA_SHIRQ,
1798                         NAME53C8XX, np)) {
1799                 printf_err("%s: request irq %d failure\n",
1800                         sym_name(np), dev->s.irq);
1801                 goto attach_failed;
1802         }
1803         np->s.irq = dev->s.irq;
1804
1805         /*
1806          *  After SCSI devices have been opened, we cannot
1807          *  reset the bus safely, so we do it here.
1808          */
1809         spin_lock_irqsave(instance->host_lock, flags);
1810         if (sym_reset_scsi_bus(np, 0))
1811                 goto reset_failed;
1812
1813         /*
1814          *  Initialize some queue headers.
1815          */
1816         sym_que_init(&np->s.wait_cmdq);
1817         sym_que_init(&np->s.busy_cmdq);
1818
1819         /*
1820          *  Start the SCRIPTS.
1821          */
1822         sym_start_up (np, 1);
1823
1824         /*
1825          *  Start the timer daemon
1826          */
1827         init_timer(&np->s.timer);
1828         np->s.timer.data     = (unsigned long) np;
1829         np->s.timer.function = sym53c8xx_timer;
1830         np->s.lasttime=0;
1831         sym_timer (np);
1832
1833         /*
1834          *  Fill Linux host instance structure
1835          *  and return success.
1836          */
1837         instance->max_channel   = 0;
1838         instance->this_id       = np->myaddr;
1839         instance->max_id        = np->maxwide ? 16 : 8;
1840         instance->max_lun       = SYM_CONF_MAX_LUN;
1841 #ifndef SYM_CONF_IOMAPPED
1842         instance->base          = (unsigned long) np->s.mmio_va;
1843 #endif
1844         instance->irq           = np->s.irq;
1845         instance->unique_id     = np->s.io_port;
1846         instance->io_port       = np->s.io_port;
1847         instance->n_io_port     = np->s.io_ws;
1848         instance->dma_channel   = 0;
1849         instance->cmd_per_lun   = SYM_CONF_MAX_TAG;
1850         instance->can_queue     = (SYM_CONF_MAX_START-2);
1851         instance->sg_tablesize  = SYM_CONF_MAX_SG;
1852         instance->max_cmd_len   = 16;
1853         BUG_ON(sym2_transport_template == NULL);
1854         instance->transportt    = sym2_transport_template;
1855
1856         spin_unlock_irqrestore(instance->host_lock, flags);
1857
1858         return instance;
1859
1860  reset_failed:
1861         printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1862                    "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
1863         spin_unlock_irqrestore(instance->host_lock, flags);
1864  attach_failed:
1865         if (!instance)
1866                 return NULL;
1867         printf_info("%s: giving up ...\n", sym_name(np));
1868         if (np)
1869                 sym_free_resources(np);
1870         scsi_host_put(instance);
1871
1872         return NULL;
1873  }
1874
1875
1876 /*
1877  *    Detect and try to read SYMBIOS and TEKRAM NVRAM.
1878  */
1879 #if SYM_CONF_NVRAM_SUPPORT
1880 static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1881 {
1882         devp->nvram = nvp;
1883         devp->device_id = devp->chip.device_id;
1884         nvp->type = 0;
1885
1886         /*
1887          *  Get access to chip IO registers
1888          */
1889 #ifndef SYM_CONF_IOMAPPED
1890         devp->s.mmio_va = ioremap(devp->s.base_c, 128);
1891         if (!devp->s.mmio_va)
1892                 return;
1893 #endif
1894
1895         sym_read_nvram(devp, nvp);
1896
1897         /*
1898          *  Release access to chip IO registers
1899          */
1900 #ifndef SYM_CONF_IOMAPPED
1901         iounmap(devp->s.mmio_va);
1902 #endif
1903 }
1904 #else
1905 static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1906 {
1907 }
1908 #endif  /* SYM_CONF_NVRAM_SUPPORT */
1909
1910 /*
1911  *  Driver setup from the boot command line
1912  */
1913 #ifdef  SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT
1914
1915 static struct sym_driver_setup
1916         sym_driver_safe_setup __initdata = SYM_LINUX_DRIVER_SAFE_SETUP;
1917 #ifdef  MODULE
1918 char *sym53c8xx = 0;    /* command line passed by insmod */
1919 MODULE_PARM(sym53c8xx, "s");
1920 #endif
1921
1922 #define OPT_MAX_TAG             1
1923 #define OPT_BURST_ORDER         2
1924 #define OPT_SCSI_LED            3
1925 #define OPT_SCSI_DIFF           4
1926 #define OPT_IRQ_MODE            5
1927 #define OPT_SCSI_BUS_CHECK      6
1928 #define OPT_HOST_ID             7
1929 #define OPT_REVERSE_PROBE       8
1930 #define OPT_VERBOSE             9
1931 #define OPT_DEBUG               10
1932 #define OPT_SETTLE_DELAY        11
1933 #define OPT_USE_NVRAM           12
1934 #define OPT_EXCLUDE             13
1935 #define OPT_SAFE_SETUP          14
1936
1937 static char setup_token[] __initdata =
1938         "tags:"         "burst:"
1939         "led:"          "diff:"
1940         "irqm:"         "buschk:"
1941         "hostid:"       "revprob:"
1942         "verb:"         "debug:"
1943         "settle:"       "nvram:"
1944         "excl:"         "safe:"
1945         ;
1946
1947 #ifdef MODULE
1948 #define ARG_SEP ' '
1949 #else
1950 #define ARG_SEP ','
1951 #endif
1952
1953 static int __init get_setup_token(char *p)
1954 {
1955         char *cur = setup_token;
1956         char *pc;
1957         int i = 0;
1958
1959         while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
1960                 ++pc;
1961                 ++i;
1962                 if (!strncmp(p, cur, pc - cur))
1963                         return i;
1964                 cur = pc;
1965         }
1966         return 0;
1967 }
1968 #endif  /* SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT */
1969
1970 int __init sym53c8xx_setup(char *str)
1971 {
1972 #ifdef  SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT
1973         char *cur = str;
1974         char *pc, *pv;
1975         unsigned long val;
1976         unsigned int i,  c;
1977         int xi = 0;
1978
1979         while (cur != NULL && (pc = strchr(cur, ':')) != NULL) {
1980                 char *pe;
1981
1982                 val = 0;
1983                 pv = pc;
1984                 c = *++pv;
1985
1986                 if      (c == 'n')
1987                         val = 0;
1988                 else if (c == 'y')
1989                         val = 1;
1990                 else
1991                         val = (int) simple_strtoul(pv, &pe, 0);
1992
1993                 switch (get_setup_token(cur)) {
1994                 case OPT_MAX_TAG:
1995                         sym_driver_setup.max_tag = val;
1996                         if (!(pe && *pe == '/'))
1997                                 break;
1998                         i = 0;
1999                         while (*pe && *pe != ARG_SEP && 
2000                                 i < sizeof(sym_driver_setup.tag_ctrl)-1) {
2001                                 sym_driver_setup.tag_ctrl[i++] = *pe++;
2002                         }
2003                         sym_driver_setup.tag_ctrl[i] = '\0';
2004                         break;
2005                 case OPT_SAFE_SETUP:
2006                         memcpy(&sym_driver_setup, &sym_driver_safe_setup,
2007                                 sizeof(sym_driver_setup));
2008                         break;
2009                 case OPT_EXCLUDE:
2010                         if (xi < 8)
2011                                 sym_driver_setup.excludes[xi++] = val;
2012                         break;
2013
2014 #define __SIMPLE_OPTION(NAME, name) \
2015                 case OPT_ ## NAME :             \
2016                         sym_driver_setup.name = val;\
2017                         break;
2018
2019                 __SIMPLE_OPTION(BURST_ORDER, burst_order)
2020                 __SIMPLE_OPTION(SCSI_LED, scsi_led)
2021                 __SIMPLE_OPTION(SCSI_DIFF, scsi_diff)
2022                 __SIMPLE_OPTION(IRQ_MODE, irq_mode)
2023                 __SIMPLE_OPTION(SCSI_BUS_CHECK, scsi_bus_check)
2024                 __SIMPLE_OPTION(HOST_ID, host_id)
2025                 __SIMPLE_OPTION(REVERSE_PROBE, reverse_probe)
2026                 __SIMPLE_OPTION(VERBOSE, verbose)
2027                 __SIMPLE_OPTION(DEBUG, debug)
2028                 __SIMPLE_OPTION(SETTLE_DELAY, settle_delay)
2029                 __SIMPLE_OPTION(USE_NVRAM, use_nvram)
2030
2031 #undef __SIMPLE_OPTION
2032
2033                 default:
2034                         printk("sym53c8xx_setup: unexpected boot option '%.*s' ignored\n", (int)(pc-cur+1), cur);
2035                         break;
2036                 }
2037
2038                 if ((cur = strchr(cur, ARG_SEP)) != NULL)
2039                         ++cur;
2040         }
2041 #endif  /* SYM_LINUX_BOOT_COMMAND_LINE_SUPPORT */
2042         return 1;
2043 }
2044
2045 #ifndef MODULE
2046 __setup("sym53c8xx=", sym53c8xx_setup);
2047 #endif
2048
2049 /*
2050  *  Read and check the PCI configuration for any detected NCR 
2051  *  boards and save data for attaching after all boards have 
2052  *  been detected.
2053  */
2054 static int __devinit
2055 sym53c8xx_pci_init(struct pci_dev *pdev, struct sym_device *device)
2056 {
2057         struct sym_pci_chip *chip;
2058         u_long base, base_2; 
2059         u_long base_c, base_2_c, io_port; 
2060         int i;
2061         u_short device_id, status_reg;
2062         u_char revision;
2063
2064         /* Choose some short name for this device */
2065         sprintf(device->s.inst_name, "sym.%d.%d.%d", pdev->bus->number,
2066                         PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2067
2068         device_id = pdev->device;
2069
2070         io_port = pdev->resource[0].start;
2071
2072         base_c = pdev->resource[1].start;
2073         i = pci_get_base_address(pdev, 1, &base);
2074
2075         base_2_c = pdev->resource[i].start;
2076         pci_get_base_address(pdev, i, &base_2);
2077
2078         base    &= PCI_BASE_ADDRESS_MEM_MASK;
2079         base_2  &= PCI_BASE_ADDRESS_MEM_MASK;
2080
2081         pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
2082
2083         /*
2084          *  If user excluded this chip, do not initialize it.
2085          */
2086         if (io_port) {
2087                 for (i = 0 ; i < 8 ; i++) {
2088                         if (sym_driver_setup.excludes[i] == io_port)
2089                                 return -1;
2090                 }
2091         }
2092
2093         /*
2094          *  Check if the chip is supported.
2095          */
2096         chip = sym_lookup_pci_chip_table(device_id, revision);
2097         if (!chip) {
2098                 printf_info("%s: device not supported\n", sym_name(device));
2099                 return -1;
2100         }
2101
2102         /*
2103          *  Check if the chip has been assigned resources we need.
2104          *  XXX: can this still happen with Linux 2.6's PCI layer?
2105          */
2106 #ifdef SYM_CONF_IOMAPPED
2107         if (!io_port) {
2108                 printf_info("%s: IO base address disabled.\n",
2109                             sym_name(device));
2110                 return -1;
2111         }
2112 #else
2113         if (!base) {
2114                 printf_info("%s: MMIO base address disabled.\n",
2115                             sym_name(device));
2116                 return -1;
2117         }
2118 #endif
2119
2120         /*
2121          *  Ignore Symbios chips controlled by various RAID controllers.
2122          *  These controllers set value 0x52414944 at RAM end - 16.
2123          */
2124 #if defined(__i386__)
2125         if (base_2_c) {
2126                 unsigned int ram_size, ram_val;
2127                 void *ram_ptr;
2128
2129                 if (chip->features & FE_RAM8K)
2130                         ram_size = 8192;
2131                 else
2132                         ram_size = 4096;
2133
2134                 ram_ptr = ioremap(base_2_c, ram_size);
2135                 if (ram_ptr) {
2136                         ram_val = readl_raw(ram_ptr + ram_size - 16);
2137                         iounmap(ram_ptr);
2138                         if (ram_val == 0x52414944) {
2139                                 printf_info("%s: not initializing, "
2140                                             "driven by RAID controller.\n",
2141                                             sym_name(device));
2142                                 return -1;
2143                         }
2144                 }
2145         }
2146 #endif /* i386 and PCI MEMORY accessible */
2147
2148         /*
2149          *  Copy the chip description to our device structure, 
2150          *  so we can make it match the actual device and options.
2151          */
2152         memcpy(&device->chip, chip, sizeof(device->chip));
2153         device->chip.revision_id = revision;
2154
2155         /*
2156          *  Some features are required to be enabled in order to 
2157          *  work around some chip problems. :) ;)
2158          *  (ITEM 12 of a DEL about the 896 I haven't yet).
2159          *  We must ensure the chip will use WRITE AND INVALIDATE.
2160          *  The revision number limit is for now arbitrary.
2161          */
2162         if (device_id == PCI_DEVICE_ID_NCR_53C896 && revision < 0x4) {
2163                 chip->features  |= (FE_WRIE | FE_CLSE);
2164         }
2165
2166         /* If the chip can do Memory Write Invalidate, enable it */
2167         if (chip->features & FE_WRIE) {
2168                 if (pci_set_mwi(pdev))
2169                         return -1;
2170         }
2171
2172         /*
2173          *  Work around for errant bit in 895A. The 66Mhz
2174          *  capable bit is set erroneously. Clear this bit.
2175          *  (Item 1 DEL 533)
2176          *
2177          *  Make sure Config space and Features agree.
2178          *
2179          *  Recall: writes are not normal to status register -
2180          *  write a 1 to clear and a 0 to leave unchanged.
2181          *  Can only reset bits.
2182          */
2183         pci_read_config_word(pdev, PCI_STATUS, &status_reg);
2184         if (chip->features & FE_66MHZ) {
2185                 if (!(status_reg & PCI_STATUS_66MHZ))
2186                         chip->features &= ~FE_66MHZ;
2187         } else {
2188                 if (status_reg & PCI_STATUS_66MHZ) {
2189                         status_reg = PCI_STATUS_66MHZ;
2190                         pci_write_config_word(pdev, PCI_STATUS, status_reg);
2191                         pci_read_config_word(pdev, PCI_STATUS, &status_reg);
2192                 }
2193         }
2194
2195         /*
2196          *  Initialise device structure with items required by sym_attach.
2197          */
2198         device->pdev            = pdev;
2199         device->s.base          = base;
2200         device->s.base_2        = base_2;
2201         device->s.base_c        = base_c;
2202         device->s.base_2_c      = base_2_c;
2203         device->s.io_port       = io_port;
2204         device->s.irq           = pdev->irq;
2205
2206         return 0;
2207 }
2208
2209 /*
2210  * The NCR PQS and PDS cards are constructed as a DEC bridge
2211  * behind which sits a proprietary NCR memory controller and
2212  * either four or two 53c875s as separate devices.  We can tell
2213  * if an 875 is part of a PQS/PDS or not since if it is, it will
2214  * be on the same bus as the memory controller.  In its usual
2215  * mode of operation, the 875s are slaved to the memory
2216  * controller for all transfers.  To operate with the Linux
2217  * driver, the memory controller is disabled and the 875s
2218  * freed to function independently.  The only wrinkle is that
2219  * the preset SCSI ID (which may be zero) must be read in from
2220  * a special configuration space register of the 875.
2221  */
2222 void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
2223 {
2224         int slot;
2225
2226         for (slot = 0; slot < 256; slot++) {
2227                 u8 tmp;
2228                 struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
2229
2230                 if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
2231                         pci_dev_put(memc);
2232                         continue;
2233                 }
2234
2235                 /*
2236                  * We set these bits in the memory controller once per 875.
2237                  * This isn't a problem in practice.
2238                  */
2239
2240                 /* bit 1: allow individual 875 configuration */
2241                 pci_read_config_byte(memc, 0x44, &tmp);
2242                 tmp |= 0x2;
2243                 pci_write_config_byte(memc, 0x44, tmp);
2244
2245                 /* bit 2: drive individual 875 interrupts to the bus */
2246                 pci_read_config_byte(memc, 0x45, &tmp);
2247                 tmp |= 0x4;
2248                 pci_write_config_byte(memc, 0x45, tmp);
2249
2250                 pci_read_config_byte(pdev, 0x84, &tmp);
2251                 sym_dev->host_id = tmp;
2252
2253                 pci_dev_put(memc);
2254
2255                 break;
2256         }
2257 }
2258
2259 /*
2260  *  Called before unloading the module.
2261  *  Detach the host.
2262  *  We have to free resources and halt the NCR chip.
2263  */
2264 static int sym_detach(struct sym_hcb *np)
2265 {
2266         printk("%s: detaching ...\n", sym_name(np));
2267
2268         del_timer_sync(&np->s.timer);
2269
2270         /*
2271          * Reset NCR chip.
2272          * We should use sym_soft_reset(), but we don't want to do 
2273          * so, since we may not be safe if interrupts occur.
2274          */
2275         printk("%s: resetting chip\n", sym_name(np));
2276         OUTB (nc_istat, SRST);
2277         UDELAY (10);
2278         OUTB (nc_istat, 0);
2279
2280         sym_free_resources(np);
2281
2282         return 1;
2283 }
2284
2285 MODULE_LICENSE("Dual BSD/GPL");
2286
2287 /*
2288  * Driver host template.
2289  */
2290 static struct scsi_host_template sym2_template = {
2291         .module                 = THIS_MODULE,
2292         .name                   = "sym53c8xx",
2293         .info                   = sym53c8xx_info, 
2294         .queuecommand           = sym53c8xx_queue_command,
2295         .slave_configure        = sym53c8xx_slave_configure,
2296         .eh_abort_handler       = sym53c8xx_eh_abort_handler,
2297         .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
2298         .eh_bus_reset_handler   = sym53c8xx_eh_bus_reset_handler,
2299         .eh_host_reset_handler  = sym53c8xx_eh_host_reset_handler,
2300         .this_id                = 7,
2301         .use_clustering         = DISABLE_CLUSTERING,
2302 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
2303         .proc_info              = sym53c8xx_proc_info,
2304         .proc_name              = NAME53C8XX,
2305 #endif
2306 };
2307
2308 static int attach_count;
2309
2310 static int __devinit sym2_probe(struct pci_dev *pdev,
2311                                 const struct pci_device_id *ent)
2312 {
2313         struct sym_device sym_dev;
2314         struct sym_nvram nvram;
2315         struct Scsi_Host *instance;
2316
2317         memset(&sym_dev, 0, sizeof(sym_dev));
2318         memset(&nvram, 0, sizeof(nvram));
2319
2320         if (pci_enable_device(pdev))
2321                 return -ENODEV;
2322
2323         pci_set_master(pdev);
2324
2325         if (pci_request_regions(pdev, NAME53C8XX))
2326                 goto disable;
2327
2328         sym_dev.host_id = SYM_SETUP_HOST_ID;
2329         if (sym53c8xx_pci_init(pdev, &sym_dev))
2330                 goto free;
2331
2332         sym_config_pqs(pdev, &sym_dev);
2333
2334         sym_get_nvram(&sym_dev, &nvram);
2335
2336         instance = sym_attach(&sym2_template, attach_count, &sym_dev);
2337         if (!instance)
2338                 goto free;
2339
2340         if (scsi_add_host(instance, &pdev->dev))
2341                 goto detach;
2342         scsi_scan_host(instance);
2343
2344         attach_count++;
2345
2346         return 0;
2347
2348  detach:
2349         sym_detach(pci_get_drvdata(pdev));
2350  free:
2351         pci_release_regions(pdev);
2352  disable:
2353         pci_disable_device(pdev);
2354         return -ENODEV;
2355 }
2356
2357 static void __devexit sym2_remove(struct pci_dev *pdev)
2358 {
2359         struct sym_hcb *np = pci_get_drvdata(pdev);
2360         struct Scsi_Host *host = np->s.host;
2361
2362         scsi_remove_host(host);
2363         scsi_host_put(host);
2364
2365         sym_detach(np);
2366
2367         pci_release_regions(pdev);
2368         pci_disable_device(pdev);
2369
2370         attach_count--;
2371 }
2372
2373 static void sym2_get_offset(struct scsi_device *sdev)
2374 {
2375         struct sym_hcb *np = ((struct host_data *)sdev->host->hostdata)->ncb;
2376         struct sym_tcb *tp = &np->target[sdev->id];
2377
2378         spi_offset(sdev) = tp->tinfo.curr.offset;
2379 }
2380
2381 static void sym2_set_offset(struct scsi_device *sdev, int offset)
2382 {
2383         struct sym_hcb *np = ((struct host_data *)sdev->host->hostdata)->ncb;
2384         struct sym_tcb *tp = &np->target[sdev->id];
2385
2386         if (tp->tinfo.curr.options & PPR_OPT_DT) {
2387                 if (offset > np->maxoffs_dt)
2388                         offset = np->maxoffs_dt;
2389         } else {
2390                 if (offset > np->maxoffs)
2391                         offset = np->maxoffs;
2392         }
2393         tp->tinfo.goal.offset = offset;
2394 }
2395
2396
2397 static void sym2_get_period(struct scsi_device *sdev)
2398 {
2399         struct sym_hcb *np = ((struct host_data *)sdev->host->hostdata)->ncb;
2400         struct sym_tcb *tp = &np->target[sdev->id];
2401
2402         spi_period(sdev) = tp->tinfo.curr.period;
2403 }
2404
2405 static void sym2_set_period(struct scsi_device *sdev, int period)
2406 {
2407         struct sym_hcb *np = ((struct host_data *)sdev->host->hostdata)->ncb;
2408         struct sym_tcb *tp = &np->target[sdev->id];
2409
2410         if (period <= 9 && np->minsync_dt) {
2411                 if (period < np->minsync_dt)
2412                         period = np->minsync_dt;
2413                 tp->tinfo.goal.options = PPR_OPT_DT;
2414                 tp->tinfo.goal.period = period;
2415                 if (!tp->tinfo.curr.offset ||
2416                                         tp->tinfo.curr.offset > np->maxoffs_dt)
2417                         tp->tinfo.goal.offset = np->maxoffs_dt;
2418         } else {
2419                 if (period < np->minsync)
2420                         period = np->minsync;
2421                 tp->tinfo.goal.options = 0;
2422                 tp->tinfo.goal.period = period;
2423                 if (!tp->tinfo.curr.offset ||
2424                                         tp->tinfo.curr.offset > np->maxoffs)
2425                         tp->tinfo.goal.offset = np->maxoffs;
2426         }
2427 }
2428
2429 static void sym2_get_width(struct scsi_device *sdev)
2430 {
2431         struct sym_hcb *np = ((struct host_data *)sdev->host->hostdata)->ncb;
2432         struct sym_tcb *tp = &np->target[sdev->id];
2433
2434         spi_width(sdev) = tp->tinfo.curr.width ? 1 : 0;
2435 }
2436
2437 static void sym2_set_width(struct scsi_device *sdev, int width)
2438 {
2439         struct sym_hcb *np = ((struct host_data *)sdev->host->hostdata)->ncb;
2440         struct sym_tcb *tp = &np->target[sdev->id];
2441
2442         tp->tinfo.goal.width = width;
2443 }
2444
2445 static void sym2_get_dt(struct scsi_device *sdev)
2446 {
2447         struct sym_hcb *np = ((struct host_data *)sdev->host->hostdata)->ncb;
2448         struct sym_tcb *tp = &np->target[sdev->id];
2449
2450         spi_dt(sdev) = (tp->tinfo.curr.options & PPR_OPT_DT) ? 1 : 0;
2451 }
2452
2453 static void sym2_set_dt(struct scsi_device *sdev, int dt)
2454 {
2455         struct sym_hcb *np = ((struct host_data *)sdev->host->hostdata)->ncb;
2456         struct sym_tcb *tp = &np->target[sdev->id];
2457
2458         if (!dt) {
2459                 /* if clearing DT, then we may need to reduce the
2460                  * period and the offset */
2461                 if (tp->tinfo.curr.period < np->minsync)
2462                         tp->tinfo.goal.period = np->minsync;
2463                 if (tp->tinfo.curr.offset > np->maxoffs)
2464                         tp->tinfo.goal.offset = np->maxoffs;
2465                 tp->tinfo.goal.options &= ~PPR_OPT_DT;
2466         } else {
2467                 tp->tinfo.goal.options |= PPR_OPT_DT;
2468         }
2469 }
2470         
2471
2472 static struct spi_function_template sym2_transport_functions = {
2473         .set_offset     = sym2_set_offset,
2474         .get_offset     = sym2_get_offset,
2475         .show_offset    = 1,
2476         .set_period     = sym2_set_period,
2477         .get_period     = sym2_get_period,
2478         .show_period    = 1,
2479         .set_width      = sym2_set_width,
2480         .get_width      = sym2_get_width,
2481         .show_width     = 1,
2482         .get_dt         = sym2_get_dt,
2483         .set_dt         = sym2_set_dt,
2484         .show_dt        = 1,
2485 };
2486
2487 static struct pci_device_id sym2_id_table[] __devinitdata = {
2488         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
2489           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2490         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
2491           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2492         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
2493           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2494         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
2495           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2496         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
2497           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2498         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
2499           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2500         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
2501           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2502         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
2503           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2504         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
2505           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2506         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
2507           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2508         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
2509           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2510         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
2511           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2512         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
2513           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2514         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
2515           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2516         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
2517           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2518         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
2519           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2520         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
2521           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2522         { 0, }
2523 };
2524
2525 MODULE_DEVICE_TABLE(pci, sym2_id_table);
2526
2527 static struct pci_driver sym2_driver = {
2528         .name           = NAME53C8XX,
2529         .id_table       = sym2_id_table,
2530         .probe          = sym2_probe,
2531         .remove         = __devexit_p(sym2_remove),
2532 };
2533
2534 static int __init sym2_init(void)
2535 {
2536         sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
2537         if (!sym2_transport_template)
2538                 return -ENODEV;
2539
2540         pci_register_driver(&sym2_driver);
2541         return 0;
2542 }
2543
2544 static void __exit sym2_exit(void)
2545 {
2546         pci_unregister_driver(&sym2_driver);
2547         spi_release_transport(sym2_transport_template);
2548 }
2549
2550 module_init(sym2_init);
2551 module_exit(sym2_exit);