]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/sym53c8xx_2/sym_glue.c
[SCSI] allow sleeping in ->eh_device_reset_handler()
[linux-2.6-omap-h63xx.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  * Copyright (c) 2003-2005  Matthew Wilcox <matthew@wil.cx>
7  *
8  * This driver is derived from the Linux sym53c8xx driver.
9  * Copyright (C) 1998-2000  Gerard Roudier
10  *
11  * The sym53c8xx driver is derived from the ncr53c8xx driver that had been 
12  * a port of the FreeBSD ncr driver to Linux-1.2.13.
13  *
14  * The original ncr driver has been written for 386bsd and FreeBSD by
15  *         Wolfgang Stanglmeier        <wolf@cologne.de>
16  *         Stefan Esser                <se@mi.Uni-Koeln.de>
17  * Copyright (C) 1994  Wolfgang Stanglmeier
18  *
19  * Other major contributions:
20  *
21  * NVRAM detection and reading.
22  * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
23  *
24  *-----------------------------------------------------------------------------
25  *
26  * This program is free software; you can redistribute it and/or modify
27  * it under the terms of the GNU General Public License as published by
28  * the Free Software Foundation; either version 2 of the License, or
29  * (at your option) any later version.
30  *
31  * This program is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34  * GNU General Public License for more details.
35  *
36  * You should have received a copy of the GNU General Public License
37  * along with this program; if not, write to the Free Software
38  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
39  */
40 #include <linux/ctype.h>
41 #include <linux/init.h>
42 #include <linux/interrupt.h>
43 #include <linux/module.h>
44 #include <linux/moduleparam.h>
45 #include <linux/spinlock.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_tcq.h>
48 #include <scsi/scsi_device.h>
49 #include <scsi/scsi_transport.h>
50
51 #include "sym_glue.h"
52 #include "sym_nvram.h"
53
54 #define NAME53C         "sym53c"
55 #define NAME53C8XX      "sym53c8xx"
56
57 /* SPARC just has to be different ... */
58 #ifdef __sparc__
59 #define IRQ_FMT "%s"
60 #define IRQ_PRM(x) __irq_itoa(x)
61 #else
62 #define IRQ_FMT "%d"
63 #define IRQ_PRM(x) (x)
64 #endif
65
66 struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
67 unsigned int sym_debug_flags = 0;
68
69 static char *excl_string;
70 static char *safe_string;
71 module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
72 module_param_string(tag_ctrl, sym_driver_setup.tag_ctrl, 100, 0);
73 module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
74 module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
75 module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
76 module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
77 module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
78 module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
79 module_param_named(verb, sym_driver_setup.verbose, byte, 0);
80 module_param_named(debug, sym_debug_flags, uint, 0);
81 module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
82 module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
83 module_param_named(excl, excl_string, charp, 0);
84 module_param_named(safe, safe_string, charp, 0);
85
86 MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
87 MODULE_PARM_DESC(tag_ctrl, "More detailed control over tags per LUN");
88 MODULE_PARM_DESC(burst, "Maximum burst.  0 to disable, 255 to read from registers");
89 MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
90 MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
91 MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
92 MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
93 MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
94 MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
95 MODULE_PARM_DESC(debug, "Set bits to enable debugging");
96 MODULE_PARM_DESC(settle, "Settle delay in seconds.  Default 3");
97 MODULE_PARM_DESC(nvram, "Option currently not used");
98 MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
99 MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
100
101 MODULE_LICENSE("GPL");
102 MODULE_VERSION(SYM_VERSION);
103 MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
104 MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
105
106 static void sym2_setup_params(void)
107 {
108         char *p = excl_string;
109         int xi = 0;
110
111         while (p && (xi < 8)) {
112                 char *next_p;
113                 int val = (int) simple_strtoul(p, &next_p, 0);
114                 sym_driver_setup.excludes[xi++] = val;
115                 p = next_p;
116         }
117
118         if (safe_string) {
119                 if (*safe_string == 'y') {
120                         sym_driver_setup.max_tag = 0;
121                         sym_driver_setup.burst_order = 0;
122                         sym_driver_setup.scsi_led = 0;
123                         sym_driver_setup.scsi_diff = 1;
124                         sym_driver_setup.irq_mode = 0;
125                         sym_driver_setup.scsi_bus_check = 2;
126                         sym_driver_setup.host_id = 7;
127                         sym_driver_setup.verbose = 2;
128                         sym_driver_setup.settle_delay = 10;
129                         sym_driver_setup.use_nvram = 1;
130                 } else if (*safe_string != 'n') {
131                         printk(KERN_WARNING NAME53C8XX "Ignoring parameter %s"
132                                         " passed to safe option", safe_string);
133                 }
134         }
135 }
136
137 /*
138  * We used to try to deal with 64-bit BARs here, but don't any more.
139  * There are many parts of this driver which would need to be modified
140  * to handle a 64-bit base address, including scripts.  I'm uncomfortable
141  * with making those changes when I have no way of testing it, so I'm
142  * just going to disable it.
143  *
144  * Note that some machines (eg HP rx8620 and Superdome) have bus addresses
145  * below 4GB and physical addresses above 4GB.  These will continue to work.
146  */
147 static int __devinit
148 pci_get_base_address(struct pci_dev *pdev, int index, unsigned long *basep)
149 {
150         u32 tmp;
151         unsigned long base;
152 #define PCI_BAR_OFFSET(index) (PCI_BASE_ADDRESS_0 + (index<<2))
153
154         pci_read_config_dword(pdev, PCI_BAR_OFFSET(index++), &tmp);
155         base = tmp;
156         if ((tmp & 0x7) == PCI_BASE_ADDRESS_MEM_TYPE_64) {
157                 pci_read_config_dword(pdev, PCI_BAR_OFFSET(index++), &tmp);
158                 if (tmp > 0) {
159                         dev_err(&pdev->dev,
160                                 "BAR %d is 64-bit, disabling\n", index - 1);
161                         base = 0;
162                 }
163         }
164
165         if ((base & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
166                 base &= PCI_BASE_ADDRESS_IO_MASK;
167         } else {
168                 base &= PCI_BASE_ADDRESS_MEM_MASK;
169         }
170
171         *basep = base;
172         return index;
173 #undef PCI_BAR_OFFSET
174 }
175
176 static struct scsi_transport_template *sym2_transport_template = NULL;
177
178 /*
179  *  Used by the eh thread to wait for command completion.
180  *  It is allocated on the eh thread stack.
181  */
182 struct sym_eh_wait {
183         struct completion done;
184         struct timer_list timer;
185         void (*old_done)(struct scsi_cmnd *);
186         int to_do;
187         int timed_out;
188 };
189
190 /*
191  *  Driver private area in the SCSI command structure.
192  */
193 struct sym_ucmd {               /* Override the SCSI pointer structure */
194         dma_addr_t data_mapping;
195         u_char  data_mapped;
196         struct sym_eh_wait *eh_wait;
197 };
198
199 #define SYM_UCMD_PTR(cmd)  ((struct sym_ucmd *)(&(cmd)->SCp))
200 #define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
201
202 static void __unmap_scsi_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
203 {
204         int dma_dir = cmd->sc_data_direction;
205
206         switch(SYM_UCMD_PTR(cmd)->data_mapped) {
207         case 2:
208                 pci_unmap_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
209                 break;
210         case 1:
211                 pci_unmap_single(pdev, SYM_UCMD_PTR(cmd)->data_mapping,
212                                  cmd->request_bufflen, dma_dir);
213                 break;
214         }
215         SYM_UCMD_PTR(cmd)->data_mapped = 0;
216 }
217
218 static dma_addr_t __map_scsi_single_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
219 {
220         dma_addr_t mapping;
221         int dma_dir = cmd->sc_data_direction;
222
223         mapping = pci_map_single(pdev, cmd->request_buffer,
224                                  cmd->request_bufflen, dma_dir);
225         if (mapping) {
226                 SYM_UCMD_PTR(cmd)->data_mapped  = 1;
227                 SYM_UCMD_PTR(cmd)->data_mapping = mapping;
228         }
229
230         return mapping;
231 }
232
233 static int __map_scsi_sg_data(struct pci_dev *pdev, struct scsi_cmnd *cmd)
234 {
235         int use_sg;
236         int dma_dir = cmd->sc_data_direction;
237
238         use_sg = pci_map_sg(pdev, cmd->buffer, cmd->use_sg, dma_dir);
239         if (use_sg > 0) {
240                 SYM_UCMD_PTR(cmd)->data_mapped  = 2;
241                 SYM_UCMD_PTR(cmd)->data_mapping = use_sg;
242         }
243
244         return use_sg;
245 }
246
247 #define unmap_scsi_data(np, cmd)        \
248                 __unmap_scsi_data(np->s.device, cmd)
249 #define map_scsi_single_data(np, cmd)   \
250                 __map_scsi_single_data(np->s.device, cmd)
251 #define map_scsi_sg_data(np, cmd)       \
252                 __map_scsi_sg_data(np->s.device, cmd)
253 /*
254  *  Complete a pending CAM CCB.
255  */
256 void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
257 {
258         unmap_scsi_data(np, cmd);
259         cmd->scsi_done(cmd);
260 }
261
262 static void sym_xpt_done2(struct sym_hcb *np, struct scsi_cmnd *cmd, int cam_status)
263 {
264         sym_set_cam_status(cmd, cam_status);
265         sym_xpt_done(np, cmd);
266 }
267
268
269 /*
270  *  Tell the SCSI layer about a BUS RESET.
271  */
272 void sym_xpt_async_bus_reset(struct sym_hcb *np)
273 {
274         printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
275         np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
276         np->s.settle_time_valid = 1;
277         if (sym_verbose >= 2)
278                 printf_info("%s: command processing suspended for %d seconds\n",
279                             sym_name(np), sym_driver_setup.settle_delay);
280 }
281
282 /*
283  *  Tell the SCSI layer about a BUS DEVICE RESET message sent.
284  */
285 void sym_xpt_async_sent_bdr(struct sym_hcb *np, int target)
286 {
287         printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
288 }
289
290 /*
291  *  Choose the more appropriate CAM status if 
292  *  the IO encountered an extended error.
293  */
294 static int sym_xerr_cam_status(int cam_status, int x_status)
295 {
296         if (x_status) {
297                 if      (x_status & XE_PARITY_ERR)
298                         cam_status = DID_PARITY;
299                 else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
300                         cam_status = DID_ERROR;
301                 else if (x_status & XE_BAD_PHASE)
302                         cam_status = DID_ERROR;
303                 else
304                         cam_status = DID_ERROR;
305         }
306         return cam_status;
307 }
308
309 /*
310  *  Build CAM result for a failed or auto-sensed IO.
311  */
312 void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
313 {
314         struct scsi_cmnd *cmd = cp->cmd;
315         u_int cam_status, scsi_status, drv_status;
316
317         drv_status  = 0;
318         cam_status  = DID_OK;
319         scsi_status = cp->ssss_status;
320
321         if (cp->host_flags & HF_SENSE) {
322                 scsi_status = cp->sv_scsi_status;
323                 resid = cp->sv_resid;
324                 if (sym_verbose && cp->sv_xerr_status)
325                         sym_print_xerr(cmd, cp->sv_xerr_status);
326                 if (cp->host_status == HS_COMPLETE &&
327                     cp->ssss_status == S_GOOD &&
328                     cp->xerr_status == 0) {
329                         cam_status = sym_xerr_cam_status(DID_OK,
330                                                          cp->sv_xerr_status);
331                         drv_status = DRIVER_SENSE;
332                         /*
333                          *  Bounce back the sense data to user.
334                          */
335                         memset(&cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
336                         memcpy(cmd->sense_buffer, cp->sns_bbuf,
337                               min(sizeof(cmd->sense_buffer),
338                                   (size_t)SYM_SNS_BBUF_LEN));
339 #if 0
340                         /*
341                          *  If the device reports a UNIT ATTENTION condition 
342                          *  due to a RESET condition, we should consider all 
343                          *  disconnect CCBs for this unit as aborted.
344                          */
345                         if (1) {
346                                 u_char *p;
347                                 p  = (u_char *) cmd->sense_data;
348                                 if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
349                                         sym_clear_tasks(np, DID_ABORT,
350                                                         cp->target,cp->lun, -1);
351                         }
352 #endif
353                 } else {
354                         /*
355                          * Error return from our internal request sense.  This
356                          * is bad: we must clear the contingent allegiance
357                          * condition otherwise the device will always return
358                          * BUSY.  Use a big stick.
359                          */
360                         sym_reset_scsi_target(np, cmd->device->id);
361                         cam_status = DID_ERROR;
362                 }
363         } else if (cp->host_status == HS_COMPLETE)      /* Bad SCSI status */
364                 cam_status = DID_OK;
365         else if (cp->host_status == HS_SEL_TIMEOUT)     /* Selection timeout */
366                 cam_status = DID_NO_CONNECT;
367         else if (cp->host_status == HS_UNEXPECTED)      /* Unexpected BUS FREE*/
368                 cam_status = DID_ERROR;
369         else {                                          /* Extended error */
370                 if (sym_verbose) {
371                         sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
372                                 cp->host_status, cp->ssss_status,
373                                 cp->xerr_status);
374                 }
375                 /*
376                  *  Set the most appropriate value for CAM status.
377                  */
378                 cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
379         }
380         cmd->resid = resid;
381         cmd->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
382 }
383
384
385 /*
386  *  Build the scatter/gather array for an I/O.
387  */
388
389 static int sym_scatter_no_sglist(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
390 {
391         struct sym_tblmove *data = &cp->phys.data[SYM_CONF_MAX_SG-1];
392         int segment;
393         unsigned int len = cmd->request_bufflen;
394
395         if (len) {
396                 dma_addr_t baddr = map_scsi_single_data(np, cmd);
397                 if (baddr) {
398                         if (len & 1) {
399                                 struct sym_tcb *tp = &np->target[cp->target];
400                                 if (tp->head.wval & EWS) {
401                                         len++;
402                                         cp->odd_byte_adjustment++;
403                                 }
404                         }
405                         cp->data_len = len;
406                         sym_build_sge(np, data, baddr, len);
407                         segment = 1;
408                 } else {
409                         segment = -2;
410                 }
411         } else {
412                 segment = 0;
413         }
414
415         return segment;
416 }
417
418 static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
419 {
420         int segment;
421         int use_sg = (int) cmd->use_sg;
422
423         cp->data_len = 0;
424
425         if (!use_sg)
426                 segment = sym_scatter_no_sglist(np, cp, cmd);
427         else if ((use_sg = map_scsi_sg_data(np, cmd)) > 0) {
428                 struct scatterlist *scatter = (struct scatterlist *)cmd->buffer;
429                 struct sym_tcb *tp = &np->target[cp->target];
430                 struct sym_tblmove *data;
431
432                 if (use_sg > SYM_CONF_MAX_SG) {
433                         unmap_scsi_data(np, cmd);
434                         return -1;
435                 }
436
437                 data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
438
439                 for (segment = 0; segment < use_sg; segment++) {
440                         dma_addr_t baddr = sg_dma_address(&scatter[segment]);
441                         unsigned int len = sg_dma_len(&scatter[segment]);
442
443                         if ((len & 1) && (tp->head.wval & EWS)) {
444                                 len++;
445                                 cp->odd_byte_adjustment++;
446                         }
447
448                         sym_build_sge(np, &data[segment], baddr, len);
449                         cp->data_len += len;
450                 }
451         } else {
452                 segment = -2;
453         }
454
455         return segment;
456 }
457
458 /*
459  *  Queue a SCSI command.
460  */
461 static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
462 {
463         struct scsi_device *sdev = cmd->device;
464         struct sym_tcb *tp;
465         struct sym_lcb *lp;
466         struct sym_ccb *cp;
467         int     order;
468
469         /*
470          *  Minimal checkings, so that we will not 
471          *  go outside our tables.
472          */
473         if (sdev->id == np->myaddr) {
474                 sym_xpt_done2(np, cmd, DID_NO_CONNECT);
475                 return 0;
476         }
477
478         /*
479          *  Retrieve the target descriptor.
480          */
481         tp = &np->target[sdev->id];
482
483         /*
484          *  Select tagged/untagged.
485          */
486         lp = sym_lp(tp, sdev->lun);
487         order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
488
489         /*
490          *  Queue the SCSI IO.
491          */
492         cp = sym_get_ccb(np, cmd, order);
493         if (!cp)
494                 return 1;       /* Means resource shortage */
495         sym_queue_scsiio(np, cmd, cp);
496         return 0;
497 }
498
499 /*
500  *  Setup buffers and pointers that address the CDB.
501  */
502 static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
503 {
504         memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
505
506         cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
507         cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
508
509         return 0;
510 }
511
512 /*
513  *  Setup pointers that address the data and start the I/O.
514  */
515 int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
516 {
517         int dir;
518         struct sym_tcb *tp = &np->target[cp->target];
519         struct sym_lcb *lp = sym_lp(tp, cp->lun);
520
521         /*
522          *  Build the CDB.
523          */
524         if (sym_setup_cdb(np, cmd, cp))
525                 goto out_abort;
526
527         /*
528          *  No direction means no data.
529          */
530         dir = cmd->sc_data_direction;
531         if (dir != DMA_NONE) {
532                 cp->segments = sym_scatter(np, cp, cmd);
533                 if (cp->segments < 0) {
534                         sym_set_cam_status(cmd, DID_ERROR);
535                         goto out_abort;
536                 }
537         } else {
538                 cp->data_len = 0;
539                 cp->segments = 0;
540         }
541
542         /*
543          *  Set data pointers.
544          */
545         sym_setup_data_pointers(np, cp, dir);
546
547         /*
548          *  When `#ifed 1', the code below makes the driver 
549          *  panic on the first attempt to write to a SCSI device.
550          *  It is the first test we want to do after a driver 
551          *  change that does not seem obviously safe. :)
552          */
553 #if 0
554         switch (cp->cdb_buf[0]) {
555         case 0x0A: case 0x2A: case 0xAA:
556                 panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
557                 break;
558         default:
559                 break;
560         }
561 #endif
562
563         /*
564          *      activate this job.
565          */
566         if (lp)
567                 sym_start_next_ccbs(np, lp, 2);
568         else
569                 sym_put_start_queue(np, cp);
570         return 0;
571
572 out_abort:
573         sym_free_ccb(np, cp);
574         sym_xpt_done(np, cmd);
575         return 0;
576 }
577
578
579 /*
580  *  timer daemon.
581  *
582  *  Misused to keep the driver running when
583  *  interrupts are not configured correctly.
584  */
585 static void sym_timer(struct sym_hcb *np)
586 {
587         unsigned long thistime = jiffies;
588
589         /*
590          *  Restart the timer.
591          */
592         np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
593         add_timer(&np->s.timer);
594
595         /*
596          *  If we are resetting the ncr, wait for settle_time before 
597          *  clearing it. Then command processing will be resumed.
598          */
599         if (np->s.settle_time_valid) {
600                 if (time_before_eq(np->s.settle_time, thistime)) {
601                         if (sym_verbose >= 2 )
602                                 printk("%s: command processing resumed\n",
603                                        sym_name(np));
604                         np->s.settle_time_valid = 0;
605                 }
606                 return;
607         }
608
609         /*
610          *      Nothing to do for now, but that may come.
611          */
612         if (np->s.lasttime + 4*HZ < thistime) {
613                 np->s.lasttime = thistime;
614         }
615
616 #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
617         /*
618          *  Some way-broken PCI bridges may lead to 
619          *  completions being lost when the clearing 
620          *  of the INTFLY flag by the CPU occurs 
621          *  concurrently with the chip raising this flag.
622          *  If this ever happen, lost completions will 
623          * be reaped here.
624          */
625         sym_wakeup_done(np);
626 #endif
627 }
628
629
630 /*
631  *  PCI BUS error handler.
632  */
633 void sym_log_bus_error(struct sym_hcb *np)
634 {
635         u_short pci_sts;
636         pci_read_config_word(np->s.device, PCI_STATUS, &pci_sts);
637         if (pci_sts & 0xf900) {
638                 pci_write_config_word(np->s.device, PCI_STATUS, pci_sts);
639                 printf("%s: PCI STATUS = 0x%04x\n",
640                         sym_name(np), pci_sts & 0xf900);
641         }
642 }
643
644 /*
645  * queuecommand method.  Entered with the host adapter lock held and
646  * interrupts disabled.
647  */
648 static int sym53c8xx_queue_command(struct scsi_cmnd *cmd,
649                                         void (*done)(struct scsi_cmnd *))
650 {
651         struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
652         struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
653         int sts = 0;
654
655         cmd->scsi_done     = done;
656         memset(ucp, 0, sizeof(*ucp));
657
658         /*
659          *  Shorten our settle_time if needed for 
660          *  this command not to time out.
661          */
662         if (np->s.settle_time_valid && cmd->timeout_per_command) {
663                 unsigned long tlimit = jiffies + cmd->timeout_per_command;
664                 tlimit -= SYM_CONF_TIMER_INTERVAL*2;
665                 if (time_after(np->s.settle_time, tlimit)) {
666                         np->s.settle_time = tlimit;
667                 }
668         }
669
670         if (np->s.settle_time_valid)
671                 return SCSI_MLQUEUE_HOST_BUSY;
672
673         sts = sym_queue_command(np, cmd);
674         if (sts)
675                 return SCSI_MLQUEUE_HOST_BUSY;
676         return 0;
677 }
678
679 /*
680  *  Linux entry point of the interrupt handler.
681  */
682 static irqreturn_t sym53c8xx_intr(int irq, void *dev_id, struct pt_regs * regs)
683 {
684         unsigned long flags;
685         struct sym_hcb *np = (struct sym_hcb *)dev_id;
686
687         if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
688
689         spin_lock_irqsave(np->s.host->host_lock, flags);
690         sym_interrupt(np);
691         spin_unlock_irqrestore(np->s.host->host_lock, flags);
692
693         if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
694
695         return IRQ_HANDLED;
696 }
697
698 /*
699  *  Linux entry point of the timer handler
700  */
701 static void sym53c8xx_timer(unsigned long npref)
702 {
703         struct sym_hcb *np = (struct sym_hcb *)npref;
704         unsigned long flags;
705
706         spin_lock_irqsave(np->s.host->host_lock, flags);
707         sym_timer(np);
708         spin_unlock_irqrestore(np->s.host->host_lock, flags);
709 }
710
711
712 /*
713  *  What the eh thread wants us to perform.
714  */
715 #define SYM_EH_ABORT            0
716 #define SYM_EH_DEVICE_RESET     1
717 #define SYM_EH_BUS_RESET        2
718 #define SYM_EH_HOST_RESET       3
719
720 /*
721  *  What we will do regarding the involved SCSI command.
722  */
723 #define SYM_EH_DO_IGNORE        0
724 #define SYM_EH_DO_COMPLETE      1
725 #define SYM_EH_DO_WAIT          2
726
727 /*
728  *  Our general completion handler.
729  */
730 static void __sym_eh_done(struct scsi_cmnd *cmd, int timed_out)
731 {
732         struct sym_eh_wait *ep = SYM_UCMD_PTR(cmd)->eh_wait;
733         if (!ep)
734                 return;
735
736         /* Try to avoid a race here (not 100% safe) */
737         if (!timed_out) {
738                 ep->timed_out = 0;
739                 if (ep->to_do == SYM_EH_DO_WAIT && !del_timer(&ep->timer))
740                         return;
741         }
742
743         /* Revert everything */
744         SYM_UCMD_PTR(cmd)->eh_wait = NULL;
745         cmd->scsi_done = ep->old_done;
746
747         /* Wake up the eh thread if it wants to sleep */
748         if (ep->to_do == SYM_EH_DO_WAIT)
749                 complete(&ep->done);
750 }
751
752 /*
753  *  scsi_done() alias when error recovery is in progress. 
754  */
755 static void sym_eh_done(struct scsi_cmnd *cmd) { __sym_eh_done(cmd, 0); }
756
757 /*
758  *  Some timeout handler to avoid waiting too long.
759  */
760 static void sym_eh_timeout(u_long p) { __sym_eh_done((struct scsi_cmnd *)p, 1); }
761
762 /*
763  *  Generic method for our eh processing.
764  *  The 'op' argument tells what we have to do.
765  */
766 static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
767 {
768         struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
769         SYM_QUEHEAD *qp;
770         int to_do = SYM_EH_DO_IGNORE;
771         int sts = -1;
772         struct sym_eh_wait eh, *ep = &eh;
773
774         dev_warn(&cmd->device->sdev_gendev, "%s operation started.\n", opname);
775
776         /* This one is queued in some place -> to wait for completion */
777         FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
778                 struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
779                 if (cp->cmd == cmd) {
780                         to_do = SYM_EH_DO_WAIT;
781                         goto prepare;
782                 }
783         }
784
785 prepare:
786         /* Prepare stuff to either ignore, complete or wait for completion */
787         switch(to_do) {
788         default:
789         case SYM_EH_DO_IGNORE:
790                 break;
791         case SYM_EH_DO_WAIT:
792                 init_completion(&ep->done);
793                 /* fall through */
794         case SYM_EH_DO_COMPLETE:
795                 ep->old_done = cmd->scsi_done;
796                 cmd->scsi_done = sym_eh_done;
797                 SYM_UCMD_PTR(cmd)->eh_wait = ep;
798         }
799
800         /* Try to proceed the operation we have been asked for */
801         sts = -1;
802         switch(op) {
803         case SYM_EH_ABORT:
804                 sts = sym_abort_scsiio(np, cmd, 1);
805                 break;
806         case SYM_EH_DEVICE_RESET:
807                 sts = sym_reset_scsi_target(np, cmd->device->id);
808                 break;
809         case SYM_EH_BUS_RESET:
810                 sym_reset_scsi_bus(np, 1);
811                 sts = 0;
812                 break;
813         case SYM_EH_HOST_RESET:
814                 sym_reset_scsi_bus(np, 0);
815                 sym_start_up (np, 1);
816                 sts = 0;
817                 break;
818         default:
819                 break;
820         }
821
822         /* On error, restore everything and cross fingers :) */
823         if (sts) {
824                 SYM_UCMD_PTR(cmd)->eh_wait = NULL;
825                 cmd->scsi_done = ep->old_done;
826                 to_do = SYM_EH_DO_IGNORE;
827         }
828
829         ep->to_do = to_do;
830         /* Complete the command with locks held as required by the driver */
831         if (to_do == SYM_EH_DO_COMPLETE)
832                 sym_xpt_done2(np, cmd, DID_ABORT);
833
834         /* Wait for completion with locks released, as required by kernel */
835         if (to_do == SYM_EH_DO_WAIT) {
836                 init_timer(&ep->timer);
837                 ep->timer.expires = jiffies + (5*HZ);
838                 ep->timer.function = sym_eh_timeout;
839                 ep->timer.data = (u_long)cmd;
840                 ep->timed_out = 1;      /* Be pessimistic for once :) */
841                 add_timer(&ep->timer);
842                 spin_unlock_irq(np->s.host->host_lock);
843                 wait_for_completion(&ep->done);
844                 spin_lock_irq(np->s.host->host_lock);
845                 if (ep->timed_out)
846                         sts = -2;
847         }
848         dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
849                         sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
850         return sts ? SCSI_FAILED : SCSI_SUCCESS;
851 }
852
853
854 /*
855  * Error handlers called from the eh thread (one thread per HBA).
856  */
857 static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
858 {
859         int rc;
860
861         spin_lock_irq(cmd->device->host->host_lock);
862         rc = sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
863         spin_unlock_irq(cmd->device->host->host_lock);
864
865         return rc;
866 }
867
868 static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
869 {
870         int rc;
871
872         spin_lock_irq(cmd->device->host->host_lock);
873         rc = sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
874         spin_unlock_irq(cmd->device->host->host_lock);
875
876         return rc;
877 }
878
879 static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
880 {
881         return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
882 }
883
884 static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
885 {
886         return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
887 }
888
889 /*
890  *  Tune device queuing depth, according to various limits.
891  */
892 static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
893 {
894         struct sym_lcb *lp = sym_lp(tp, lun);
895         u_short oldtags;
896
897         if (!lp)
898                 return;
899
900         oldtags = lp->s.reqtags;
901
902         if (reqtags > lp->s.scdev_depth)
903                 reqtags = lp->s.scdev_depth;
904
905         lp->started_limit = reqtags ? reqtags : 2;
906         lp->started_max   = 1;
907         lp->s.reqtags     = reqtags;
908
909         if (reqtags != oldtags) {
910                 dev_info(&tp->starget->dev,
911                          "tagged command queuing %s, command queue depth %d.\n",
912                           lp->s.reqtags ? "enabled" : "disabled",
913                           lp->started_limit);
914         }
915 }
916
917 /*
918  *  Linux select queue depths function
919  */
920 #define DEF_DEPTH       (sym_driver_setup.max_tag)
921 #define ALL_TARGETS     -2
922 #define NO_TARGET       -1
923 #define ALL_LUNS        -2
924 #define NO_LUN          -1
925
926 static int device_queue_depth(struct sym_hcb *np, int target, int lun)
927 {
928         int c, h, t, u, v;
929         char *p = sym_driver_setup.tag_ctrl;
930         char *ep;
931
932         h = -1;
933         t = NO_TARGET;
934         u = NO_LUN;
935         while ((c = *p++) != 0) {
936                 v = simple_strtoul(p, &ep, 0);
937                 switch(c) {
938                 case '/':
939                         ++h;
940                         t = ALL_TARGETS;
941                         u = ALL_LUNS;
942                         break;
943                 case 't':
944                         if (t != target)
945                                 t = (target == v) ? v : NO_TARGET;
946                         u = ALL_LUNS;
947                         break;
948                 case 'u':
949                         if (u != lun)
950                                 u = (lun == v) ? v : NO_LUN;
951                         break;
952                 case 'q':
953                         if (h == np->s.unit &&
954                                 (t == ALL_TARGETS || t == target) &&
955                                 (u == ALL_LUNS    || u == lun))
956                                 return v;
957                         break;
958                 case '-':
959                         t = ALL_TARGETS;
960                         u = ALL_LUNS;
961                         break;
962                 default:
963                         break;
964                 }
965                 p = ep;
966         }
967         return DEF_DEPTH;
968 }
969
970 static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
971 {
972         struct sym_hcb *np;
973         struct sym_tcb *tp;
974
975         if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
976                 return -ENXIO;
977
978         np = sym_get_hcb(sdev->host);
979         tp = &np->target[sdev->id];
980
981         /*
982          * Fail the device init if the device is flagged NOSCAN at BOOT in
983          * the NVRAM.  This may speed up boot and maintain coherency with
984          * BIOS device numbering.  Clearing the flag allows the user to
985          * rescan skipped devices later.  We also return an error for
986          * devices not flagged for SCAN LUNS in the NVRAM since some single
987          * lun devices behave badly when asked for a non zero LUN.
988          */
989
990         if ((tp->usrflags & SYM_SCAN_BOOT_DISABLED) ||
991             ((tp->usrflags & SYM_SCAN_LUNS_DISABLED) && sdev->lun != 0)) {
992                 tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
993                 return -ENXIO;
994         }
995
996         tp->starget = sdev->sdev_target;
997         return 0;
998 }
999
1000 /*
1001  * Linux entry point for device queue sizing.
1002  */
1003 static int sym53c8xx_slave_configure(struct scsi_device *device)
1004 {
1005         struct sym_hcb *np = sym_get_hcb(device->host);
1006         struct sym_tcb *tp = &np->target[device->id];
1007         struct sym_lcb *lp;
1008         int reqtags, depth_to_use;
1009
1010         /*
1011          *  Allocate the LCB if not yet.
1012          *  If it fail, we may well be in the sh*t. :)
1013          */
1014         lp = sym_alloc_lcb(np, device->id, device->lun);
1015         if (!lp)
1016                 return -ENOMEM;
1017
1018         /*
1019          *  Get user flags.
1020          */
1021         lp->curr_flags = lp->user_flags;
1022
1023         /*
1024          *  Select queue depth from driver setup.
1025          *  Donnot use more than configured by user.
1026          *  Use at least 2.
1027          *  Donnot use more than our maximum.
1028          */
1029         reqtags = device_queue_depth(np, device->id, device->lun);
1030         if (reqtags > tp->usrtags)
1031                 reqtags = tp->usrtags;
1032         if (!device->tagged_supported)
1033                 reqtags = 0;
1034 #if 1 /* Avoid to locally queue commands for no good reasons */
1035         if (reqtags > SYM_CONF_MAX_TAG)
1036                 reqtags = SYM_CONF_MAX_TAG;
1037         depth_to_use = (reqtags ? reqtags : 2);
1038 #else
1039         depth_to_use = (reqtags ? SYM_CONF_MAX_TAG : 2);
1040 #endif
1041         scsi_adjust_queue_depth(device,
1042                                 (device->tagged_supported ?
1043                                  MSG_SIMPLE_TAG : 0),
1044                                 depth_to_use);
1045         lp->s.scdev_depth = depth_to_use;
1046         sym_tune_dev_queuing(tp, device->lun, reqtags);
1047
1048         if (!spi_initial_dv(device->sdev_target))
1049                 spi_dv_device(device);
1050
1051         return 0;
1052 }
1053
1054 /*
1055  *  Linux entry point for info() function
1056  */
1057 static const char *sym53c8xx_info (struct Scsi_Host *host)
1058 {
1059         return SYM_DRIVER_NAME;
1060 }
1061
1062
1063 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1064 /*
1065  *  Proc file system stuff
1066  *
1067  *  A read operation returns adapter information.
1068  *  A write operation is a control command.
1069  *  The string is parsed in the driver code and the command is passed 
1070  *  to the sym_usercmd() function.
1071  */
1072
1073 #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
1074
1075 struct  sym_usrcmd {
1076         u_long  target;
1077         u_long  lun;
1078         u_long  data;
1079         u_long  cmd;
1080 };
1081
1082 #define UC_SETSYNC      10
1083 #define UC_SETTAGS      11
1084 #define UC_SETDEBUG     12
1085 #define UC_SETWIDE      14
1086 #define UC_SETFLAG      15
1087 #define UC_SETVERBOSE   17
1088 #define UC_RESETDEV     18
1089 #define UC_CLEARDEV     19
1090
1091 static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
1092 {
1093         struct sym_tcb *tp;
1094         int t, l;
1095
1096         switch (uc->cmd) {
1097         case 0: return;
1098
1099 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1100         case UC_SETDEBUG:
1101                 sym_debug_flags = uc->data;
1102                 break;
1103 #endif
1104         case UC_SETVERBOSE:
1105                 np->verbose = uc->data;
1106                 break;
1107         default:
1108                 /*
1109                  * We assume that other commands apply to targets.
1110                  * This should always be the case and avoid the below 
1111                  * 4 lines to be repeated 6 times.
1112                  */
1113                 for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
1114                         if (!((uc->target >> t) & 1))
1115                                 continue;
1116                         tp = &np->target[t];
1117
1118                         switch (uc->cmd) {
1119
1120                         case UC_SETSYNC:
1121                                 if (!uc->data || uc->data >= 255) {
1122                                         tp->tgoal.iu = tp->tgoal.dt =
1123                                                 tp->tgoal.qas = 0;
1124                                         tp->tgoal.offset = 0;
1125                                 } else if (uc->data <= 9 && np->minsync_dt) {
1126                                         if (uc->data < np->minsync_dt)
1127                                                 uc->data = np->minsync_dt;
1128                                         tp->tgoal.iu = tp->tgoal.dt =
1129                                                 tp->tgoal.qas = 1;
1130                                         tp->tgoal.width = 1;
1131                                         tp->tgoal.period = uc->data;
1132                                         tp->tgoal.offset = np->maxoffs_dt;
1133                                 } else {
1134                                         if (uc->data < np->minsync)
1135                                                 uc->data = np->minsync;
1136                                         tp->tgoal.iu = tp->tgoal.dt =
1137                                                 tp->tgoal.qas = 0;
1138                                         tp->tgoal.period = uc->data;
1139                                         tp->tgoal.offset = np->maxoffs;
1140                                 }
1141                                 tp->tgoal.check_nego = 1;
1142                                 break;
1143                         case UC_SETWIDE:
1144                                 tp->tgoal.width = uc->data ? 1 : 0;
1145                                 tp->tgoal.check_nego = 1;
1146                                 break;
1147                         case UC_SETTAGS:
1148                                 for (l = 0; l < SYM_CONF_MAX_LUN; l++)
1149                                         sym_tune_dev_queuing(tp, l, uc->data);
1150                                 break;
1151                         case UC_RESETDEV:
1152                                 tp->to_reset = 1;
1153                                 np->istat_sem = SEM;
1154                                 OUTB(np, nc_istat, SIGP|SEM);
1155                                 break;
1156                         case UC_CLEARDEV:
1157                                 for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
1158                                         struct sym_lcb *lp = sym_lp(tp, l);
1159                                         if (lp) lp->to_clear = 1;
1160                                 }
1161                                 np->istat_sem = SEM;
1162                                 OUTB(np, nc_istat, SIGP|SEM);
1163                                 break;
1164                         case UC_SETFLAG:
1165                                 tp->usrflags = uc->data;
1166                                 break;
1167                         }
1168                 }
1169                 break;
1170         }
1171 }
1172
1173 static int skip_spaces(char *ptr, int len)
1174 {
1175         int cnt, c;
1176
1177         for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
1178
1179         return (len - cnt);
1180 }
1181
1182 static int get_int_arg(char *ptr, int len, u_long *pv)
1183 {
1184         char *end;
1185
1186         *pv = simple_strtoul(ptr, &end, 10);
1187         return (end - ptr);
1188 }
1189
1190 static int is_keyword(char *ptr, int len, char *verb)
1191 {
1192         int verb_len = strlen(verb);
1193
1194         if (len >= verb_len && !memcmp(verb, ptr, verb_len))
1195                 return verb_len;
1196         else
1197                 return 0;
1198 }
1199
1200 #define SKIP_SPACES(ptr, len)                                           \
1201         if ((arg_len = skip_spaces(ptr, len)) < 1)                      \
1202                 return -EINVAL;                                         \
1203         ptr += arg_len; len -= arg_len;
1204
1205 #define GET_INT_ARG(ptr, len, v)                                        \
1206         if (!(arg_len = get_int_arg(ptr, len, &(v))))                   \
1207                 return -EINVAL;                                         \
1208         ptr += arg_len; len -= arg_len;
1209
1210
1211 /*
1212  * Parse a control command
1213  */
1214
1215 static int sym_user_command(struct sym_hcb *np, char *buffer, int length)
1216 {
1217         char *ptr       = buffer;
1218         int len         = length;
1219         struct sym_usrcmd cmd, *uc = &cmd;
1220         int             arg_len;
1221         u_long          target;
1222
1223         memset(uc, 0, sizeof(*uc));
1224
1225         if (len > 0 && ptr[len-1] == '\n')
1226                 --len;
1227
1228         if      ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
1229                 uc->cmd = UC_SETSYNC;
1230         else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
1231                 uc->cmd = UC_SETTAGS;
1232         else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
1233                 uc->cmd = UC_SETVERBOSE;
1234         else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
1235                 uc->cmd = UC_SETWIDE;
1236 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1237         else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
1238                 uc->cmd = UC_SETDEBUG;
1239 #endif
1240         else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
1241                 uc->cmd = UC_SETFLAG;
1242         else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
1243                 uc->cmd = UC_RESETDEV;
1244         else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
1245                 uc->cmd = UC_CLEARDEV;
1246         else
1247                 arg_len = 0;
1248
1249 #ifdef DEBUG_PROC_INFO
1250 printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
1251 #endif
1252
1253         if (!arg_len)
1254                 return -EINVAL;
1255         ptr += arg_len; len -= arg_len;
1256
1257         switch(uc->cmd) {
1258         case UC_SETSYNC:
1259         case UC_SETTAGS:
1260         case UC_SETWIDE:
1261         case UC_SETFLAG:
1262         case UC_RESETDEV:
1263         case UC_CLEARDEV:
1264                 SKIP_SPACES(ptr, len);
1265                 if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
1266                         ptr += arg_len; len -= arg_len;
1267                         uc->target = ~0;
1268                 } else {
1269                         GET_INT_ARG(ptr, len, target);
1270                         uc->target = (1<<target);
1271 #ifdef DEBUG_PROC_INFO
1272 printk("sym_user_command: target=%ld\n", target);
1273 #endif
1274                 }
1275                 break;
1276         }
1277
1278         switch(uc->cmd) {
1279         case UC_SETVERBOSE:
1280         case UC_SETSYNC:
1281         case UC_SETTAGS:
1282         case UC_SETWIDE:
1283                 SKIP_SPACES(ptr, len);
1284                 GET_INT_ARG(ptr, len, uc->data);
1285 #ifdef DEBUG_PROC_INFO
1286 printk("sym_user_command: data=%ld\n", uc->data);
1287 #endif
1288                 break;
1289 #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
1290         case UC_SETDEBUG:
1291                 while (len > 0) {
1292                         SKIP_SPACES(ptr, len);
1293                         if      ((arg_len = is_keyword(ptr, len, "alloc")))
1294                                 uc->data |= DEBUG_ALLOC;
1295                         else if ((arg_len = is_keyword(ptr, len, "phase")))
1296                                 uc->data |= DEBUG_PHASE;
1297                         else if ((arg_len = is_keyword(ptr, len, "queue")))
1298                                 uc->data |= DEBUG_QUEUE;
1299                         else if ((arg_len = is_keyword(ptr, len, "result")))
1300                                 uc->data |= DEBUG_RESULT;
1301                         else if ((arg_len = is_keyword(ptr, len, "scatter")))
1302                                 uc->data |= DEBUG_SCATTER;
1303                         else if ((arg_len = is_keyword(ptr, len, "script")))
1304                                 uc->data |= DEBUG_SCRIPT;
1305                         else if ((arg_len = is_keyword(ptr, len, "tiny")))
1306                                 uc->data |= DEBUG_TINY;
1307                         else if ((arg_len = is_keyword(ptr, len, "timing")))
1308                                 uc->data |= DEBUG_TIMING;
1309                         else if ((arg_len = is_keyword(ptr, len, "nego")))
1310                                 uc->data |= DEBUG_NEGO;
1311                         else if ((arg_len = is_keyword(ptr, len, "tags")))
1312                                 uc->data |= DEBUG_TAGS;
1313                         else if ((arg_len = is_keyword(ptr, len, "pointer")))
1314                                 uc->data |= DEBUG_POINTER;
1315                         else
1316                                 return -EINVAL;
1317                         ptr += arg_len; len -= arg_len;
1318                 }
1319 #ifdef DEBUG_PROC_INFO
1320 printk("sym_user_command: data=%ld\n", uc->data);
1321 #endif
1322                 break;
1323 #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
1324         case UC_SETFLAG:
1325                 while (len > 0) {
1326                         SKIP_SPACES(ptr, len);
1327                         if      ((arg_len = is_keyword(ptr, len, "no_disc")))
1328                                 uc->data &= ~SYM_DISC_ENABLED;
1329                         else
1330                                 return -EINVAL;
1331                         ptr += arg_len; len -= arg_len;
1332                 }
1333                 break;
1334         default:
1335                 break;
1336         }
1337
1338         if (len)
1339                 return -EINVAL;
1340         else {
1341                 unsigned long flags;
1342
1343                 spin_lock_irqsave(np->s.host->host_lock, flags);
1344                 sym_exec_user_command (np, uc);
1345                 spin_unlock_irqrestore(np->s.host->host_lock, flags);
1346         }
1347         return length;
1348 }
1349
1350 #endif  /* SYM_LINUX_USER_COMMAND_SUPPORT */
1351
1352
1353 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1354 /*
1355  *  Informations through the proc file system.
1356  */
1357 struct info_str {
1358         char *buffer;
1359         int length;
1360         int offset;
1361         int pos;
1362 };
1363
1364 static void copy_mem_info(struct info_str *info, char *data, int len)
1365 {
1366         if (info->pos + len > info->length)
1367                 len = info->length - info->pos;
1368
1369         if (info->pos + len < info->offset) {
1370                 info->pos += len;
1371                 return;
1372         }
1373         if (info->pos < info->offset) {
1374                 data += (info->offset - info->pos);
1375                 len  -= (info->offset - info->pos);
1376         }
1377
1378         if (len > 0) {
1379                 memcpy(info->buffer + info->pos, data, len);
1380                 info->pos += len;
1381         }
1382 }
1383
1384 static int copy_info(struct info_str *info, char *fmt, ...)
1385 {
1386         va_list args;
1387         char buf[81];
1388         int len;
1389
1390         va_start(args, fmt);
1391         len = vsprintf(buf, fmt, args);
1392         va_end(args);
1393
1394         copy_mem_info(info, buf, len);
1395         return len;
1396 }
1397
1398 /*
1399  *  Copy formatted information into the input buffer.
1400  */
1401 static int sym_host_info(struct sym_hcb *np, char *ptr, off_t offset, int len)
1402 {
1403         struct info_str info;
1404
1405         info.buffer     = ptr;
1406         info.length     = len;
1407         info.offset     = offset;
1408         info.pos        = 0;
1409
1410         copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
1411                          "revision id 0x%x\n",
1412                          np->s.chip_name, np->device_id, np->revision_id);
1413         copy_info(&info, "At PCI address %s, IRQ " IRQ_FMT "\n",
1414                 pci_name(np->s.device), IRQ_PRM(np->s.irq));
1415         copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
1416                          (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
1417                          np->maxwide ? "Wide" : "Narrow",
1418                          np->minsync_dt ? ", DT capable" : "");
1419
1420         copy_info(&info, "Max. started commands %d, "
1421                          "max. commands per LUN %d\n",
1422                          SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
1423
1424         return info.pos > info.offset? info.pos - info.offset : 0;
1425 }
1426 #endif /* SYM_LINUX_USER_INFO_SUPPORT */
1427
1428 /*
1429  *  Entry point of the scsi proc fs of the driver.
1430  *  - func = 0 means read  (returns adapter infos)
1431  *  - func = 1 means write (not yet merget from sym53c8xx)
1432  */
1433 static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
1434                         char **start, off_t offset, int length, int func)
1435 {
1436         struct sym_hcb *np = sym_get_hcb(host);
1437         int retv;
1438
1439         if (func) {
1440 #ifdef  SYM_LINUX_USER_COMMAND_SUPPORT
1441                 retv = sym_user_command(np, buffer, length);
1442 #else
1443                 retv = -EINVAL;
1444 #endif
1445         } else {
1446                 if (start)
1447                         *start = buffer;
1448 #ifdef SYM_LINUX_USER_INFO_SUPPORT
1449                 retv = sym_host_info(np, buffer, offset, length);
1450 #else
1451                 retv = -EINVAL;
1452 #endif
1453         }
1454
1455         return retv;
1456 }
1457 #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
1458
1459 /*
1460  *      Free controller resources.
1461  */
1462 static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev)
1463 {
1464         /*
1465          *  Free O/S specific resources.
1466          */
1467         if (np->s.irq)
1468                 free_irq(np->s.irq, np);
1469         if (np->s.ioaddr)
1470                 pci_iounmap(pdev, np->s.ioaddr);
1471         if (np->s.ramaddr)
1472                 pci_iounmap(pdev, np->s.ramaddr);
1473         /*
1474          *  Free O/S independent resources.
1475          */
1476         sym_hcb_free(np);
1477
1478         sym_mfree_dma(np, sizeof(*np), "HCB");
1479 }
1480
1481 /*
1482  *  Ask/tell the system about DMA addressing.
1483  */
1484 static int sym_setup_bus_dma_mask(struct sym_hcb *np)
1485 {
1486 #if SYM_CONF_DMA_ADDRESSING_MODE > 0
1487 #if   SYM_CONF_DMA_ADDRESSING_MODE == 1
1488 #define DMA_DAC_MASK    0x000000ffffffffffULL /* 40-bit */
1489 #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
1490 #define DMA_DAC_MASK    DMA_64BIT_MASK
1491 #endif
1492         if ((np->features & FE_DAC) &&
1493                         !pci_set_dma_mask(np->s.device, DMA_DAC_MASK)) {
1494                 np->use_dac = 1;
1495                 return 0;
1496         }
1497 #endif
1498
1499         if (!pci_set_dma_mask(np->s.device, DMA_32BIT_MASK))
1500                 return 0;
1501
1502         printf_warning("%s: No suitable DMA available\n", sym_name(np));
1503         return -1;
1504 }
1505
1506 /*
1507  *  Host attach and initialisations.
1508  *
1509  *  Allocate host data and ncb structure.
1510  *  Remap MMIO region.
1511  *  Do chip initialization.
1512  *  If all is OK, install interrupt handling and
1513  *  start the timer daemon.
1514  */
1515 static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
1516                 int unit, struct sym_device *dev)
1517 {
1518         struct host_data *host_data;
1519         struct sym_hcb *np = NULL;
1520         struct Scsi_Host *instance = NULL;
1521         struct pci_dev *pdev = dev->pdev;
1522         unsigned long flags;
1523         struct sym_fw *fw;
1524
1525         printk(KERN_INFO
1526                 "sym%d: <%s> rev 0x%x at pci %s irq " IRQ_FMT "\n",
1527                 unit, dev->chip.name, dev->chip.revision_id,
1528                 pci_name(pdev), IRQ_PRM(pdev->irq));
1529
1530         /*
1531          *  Get the firmware for this chip.
1532          */
1533         fw = sym_find_firmware(&dev->chip);
1534         if (!fw)
1535                 goto attach_failed;
1536
1537         /*
1538          *      Allocate host_data structure
1539          */
1540         instance = scsi_host_alloc(tpnt, sizeof(*host_data));
1541         if (!instance)
1542                 goto attach_failed;
1543         host_data = (struct host_data *) instance->hostdata;
1544
1545         /*
1546          *  Allocate immediately the host control block, 
1547          *  since we are only expecting to succeed. :)
1548          *  We keep track in the HCB of all the resources that 
1549          *  are to be released on error.
1550          */
1551         np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
1552         if (!np)
1553                 goto attach_failed;
1554         np->s.device = pdev;
1555         np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
1556         host_data->ncb = np;
1557         np->s.host = instance;
1558
1559         pci_set_drvdata(pdev, np);
1560
1561         /*
1562          *  Copy some useful infos to the HCB.
1563          */
1564         np->hcb_ba      = vtobus(np);
1565         np->verbose     = sym_driver_setup.verbose;
1566         np->s.device    = pdev;
1567         np->s.unit      = unit;
1568         np->device_id   = dev->chip.device_id;
1569         np->revision_id = dev->chip.revision_id;
1570         np->features    = dev->chip.features;
1571         np->clock_divn  = dev->chip.nr_divisor;
1572         np->maxoffs     = dev->chip.offset_max;
1573         np->maxburst    = dev->chip.burst_max;
1574         np->myaddr      = dev->host_id;
1575
1576         /*
1577          *  Edit its name.
1578          */
1579         strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
1580         sprintf(np->s.inst_name, "sym%d", np->s.unit);
1581
1582         if (sym_setup_bus_dma_mask(np))
1583                 goto attach_failed;
1584
1585         /*
1586          *  Try to map the controller chip to
1587          *  virtual and physical memory.
1588          */
1589         np->mmio_ba = (u32)dev->mmio_base;
1590         np->s.ioaddr    = dev->s.ioaddr;
1591         np->s.ramaddr   = dev->s.ramaddr;
1592         np->s.io_ws = (np->features & FE_IO256) ? 256 : 128;
1593
1594         /*
1595          *  Map on-chip RAM if present and supported.
1596          */
1597         if (!(np->features & FE_RAM))
1598                 dev->ram_base = 0;
1599         if (dev->ram_base) {
1600                 np->ram_ba = (u32)dev->ram_base;
1601                 np->ram_ws = (np->features & FE_RAM8K) ? 8192 : 4096;
1602         }
1603
1604         if (sym_hcb_attach(instance, fw, dev->nvram))
1605                 goto attach_failed;
1606
1607         /*
1608          *  Install the interrupt handler.
1609          *  If we synchonize the C code with SCRIPTS on interrupt, 
1610          *  we do not want to share the INTR line at all.
1611          */
1612         if (request_irq(pdev->irq, sym53c8xx_intr, SA_SHIRQ, NAME53C8XX, np)) {
1613                 printf_err("%s: request irq %d failure\n",
1614                         sym_name(np), pdev->irq);
1615                 goto attach_failed;
1616         }
1617         np->s.irq = pdev->irq;
1618
1619         /*
1620          *  After SCSI devices have been opened, we cannot
1621          *  reset the bus safely, so we do it here.
1622          */
1623         spin_lock_irqsave(instance->host_lock, flags);
1624         if (sym_reset_scsi_bus(np, 0))
1625                 goto reset_failed;
1626
1627         /*
1628          *  Start the SCRIPTS.
1629          */
1630         sym_start_up (np, 1);
1631
1632         /*
1633          *  Start the timer daemon
1634          */
1635         init_timer(&np->s.timer);
1636         np->s.timer.data     = (unsigned long) np;
1637         np->s.timer.function = sym53c8xx_timer;
1638         np->s.lasttime=0;
1639         sym_timer (np);
1640
1641         /*
1642          *  Fill Linux host instance structure
1643          *  and return success.
1644          */
1645         instance->max_channel   = 0;
1646         instance->this_id       = np->myaddr;
1647         instance->max_id        = np->maxwide ? 16 : 8;
1648         instance->max_lun       = SYM_CONF_MAX_LUN;
1649         instance->unique_id     = pci_resource_start(pdev, 0);
1650         instance->cmd_per_lun   = SYM_CONF_MAX_TAG;
1651         instance->can_queue     = (SYM_CONF_MAX_START-2);
1652         instance->sg_tablesize  = SYM_CONF_MAX_SG;
1653         instance->max_cmd_len   = 16;
1654         BUG_ON(sym2_transport_template == NULL);
1655         instance->transportt    = sym2_transport_template;
1656
1657         spin_unlock_irqrestore(instance->host_lock, flags);
1658
1659         return instance;
1660
1661  reset_failed:
1662         printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
1663                    "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
1664         spin_unlock_irqrestore(instance->host_lock, flags);
1665  attach_failed:
1666         if (!instance)
1667                 return NULL;
1668         printf_info("%s: giving up ...\n", sym_name(np));
1669         if (np)
1670                 sym_free_resources(np, pdev);
1671         scsi_host_put(instance);
1672
1673         return NULL;
1674  }
1675
1676
1677 /*
1678  *    Detect and try to read SYMBIOS and TEKRAM NVRAM.
1679  */
1680 #if SYM_CONF_NVRAM_SUPPORT
1681 static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1682 {
1683         devp->nvram = nvp;
1684         devp->device_id = devp->chip.device_id;
1685         nvp->type = 0;
1686
1687         sym_read_nvram(devp, nvp);
1688 }
1689 #else
1690 static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
1691 {
1692 }
1693 #endif  /* SYM_CONF_NVRAM_SUPPORT */
1694
1695 static int __devinit sym_check_supported(struct sym_device *device)
1696 {
1697         struct sym_chip *chip;
1698         struct pci_dev *pdev = device->pdev;
1699         u_char revision;
1700         unsigned long io_port = pci_resource_start(pdev, 0);
1701         int i;
1702
1703         /*
1704          *  If user excluded this chip, do not initialize it.
1705          *  I hate this code so much.  Must kill it.
1706          */
1707         if (io_port) {
1708                 for (i = 0 ; i < 8 ; i++) {
1709                         if (sym_driver_setup.excludes[i] == io_port)
1710                                 return -ENODEV;
1711                 }
1712         }
1713
1714         /*
1715          * Check if the chip is supported.  Then copy the chip description
1716          * to our device structure so we can make it match the actual device
1717          * and options.
1718          */
1719         pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
1720         chip = sym_lookup_chip_table(pdev->device, revision);
1721         if (!chip) {
1722                 dev_info(&pdev->dev, "device not supported\n");
1723                 return -ENODEV;
1724         }
1725         memcpy(&device->chip, chip, sizeof(device->chip));
1726         device->chip.revision_id = revision;
1727
1728         return 0;
1729 }
1730
1731 /*
1732  * Ignore Symbios chips controlled by various RAID controllers.
1733  * These controllers set value 0x52414944 at RAM end - 16.
1734  */
1735 static int __devinit sym_check_raid(struct sym_device *device)
1736 {
1737         unsigned int ram_size, ram_val;
1738
1739         if (!device->s.ramaddr)
1740                 return 0;
1741
1742         if (device->chip.features & FE_RAM8K)
1743                 ram_size = 8192;
1744         else
1745                 ram_size = 4096;
1746
1747         ram_val = readl(device->s.ramaddr + ram_size - 16);
1748         if (ram_val != 0x52414944)
1749                 return 0;
1750
1751         dev_info(&device->pdev->dev,
1752                         "not initializing, driven by RAID controller.\n");
1753         return -ENODEV;
1754 }
1755
1756 static int __devinit sym_set_workarounds(struct sym_device *device)
1757 {
1758         struct sym_chip *chip = &device->chip;
1759         struct pci_dev *pdev = device->pdev;
1760         u_short status_reg;
1761
1762         /*
1763          *  (ITEM 12 of a DEL about the 896 I haven't yet).
1764          *  We must ensure the chip will use WRITE AND INVALIDATE.
1765          *  The revision number limit is for now arbitrary.
1766          */
1767         if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && chip->revision_id < 0x4) {
1768                 chip->features  |= (FE_WRIE | FE_CLSE);
1769         }
1770
1771         /* If the chip can do Memory Write Invalidate, enable it */
1772         if (chip->features & FE_WRIE) {
1773                 if (pci_set_mwi(pdev))
1774                         return -ENODEV;
1775         }
1776
1777         /*
1778          *  Work around for errant bit in 895A. The 66Mhz
1779          *  capable bit is set erroneously. Clear this bit.
1780          *  (Item 1 DEL 533)
1781          *
1782          *  Make sure Config space and Features agree.
1783          *
1784          *  Recall: writes are not normal to status register -
1785          *  write a 1 to clear and a 0 to leave unchanged.
1786          *  Can only reset bits.
1787          */
1788         pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1789         if (chip->features & FE_66MHZ) {
1790                 if (!(status_reg & PCI_STATUS_66MHZ))
1791                         chip->features &= ~FE_66MHZ;
1792         } else {
1793                 if (status_reg & PCI_STATUS_66MHZ) {
1794                         status_reg = PCI_STATUS_66MHZ;
1795                         pci_write_config_word(pdev, PCI_STATUS, status_reg);
1796                         pci_read_config_word(pdev, PCI_STATUS, &status_reg);
1797                 }
1798         }
1799
1800         return 0;
1801 }
1802
1803 /*
1804  *  Read and check the PCI configuration for any detected NCR 
1805  *  boards and save data for attaching after all boards have 
1806  *  been detected.
1807  */
1808 static void __devinit
1809 sym_init_device(struct pci_dev *pdev, struct sym_device *device)
1810 {
1811         int i;
1812
1813         device->host_id = SYM_SETUP_HOST_ID;
1814         device->pdev = pdev;
1815
1816         i = pci_get_base_address(pdev, 1, &device->mmio_base);
1817         pci_get_base_address(pdev, i, &device->ram_base);
1818
1819 #ifndef CONFIG_SCSI_SYM53C8XX_IOMAPPED
1820         if (device->mmio_base)
1821                 device->s.ioaddr = pci_iomap(pdev, 1,
1822                                                 pci_resource_len(pdev, 1));
1823 #endif
1824         if (!device->s.ioaddr)
1825                 device->s.ioaddr = pci_iomap(pdev, 0,
1826                                                 pci_resource_len(pdev, 0));
1827         if (device->ram_base)
1828                 device->s.ramaddr = pci_iomap(pdev, i,
1829                                                 pci_resource_len(pdev, i));
1830 }
1831
1832 /*
1833  * The NCR PQS and PDS cards are constructed as a DEC bridge
1834  * behind which sits a proprietary NCR memory controller and
1835  * either four or two 53c875s as separate devices.  We can tell
1836  * if an 875 is part of a PQS/PDS or not since if it is, it will
1837  * be on the same bus as the memory controller.  In its usual
1838  * mode of operation, the 875s are slaved to the memory
1839  * controller for all transfers.  To operate with the Linux
1840  * driver, the memory controller is disabled and the 875s
1841  * freed to function independently.  The only wrinkle is that
1842  * the preset SCSI ID (which may be zero) must be read in from
1843  * a special configuration space register of the 875.
1844  */
1845 static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
1846 {
1847         int slot;
1848         u8 tmp;
1849
1850         for (slot = 0; slot < 256; slot++) {
1851                 struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
1852
1853                 if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
1854                         pci_dev_put(memc);
1855                         continue;
1856                 }
1857
1858                 /* bit 1: allow individual 875 configuration */
1859                 pci_read_config_byte(memc, 0x44, &tmp);
1860                 if ((tmp & 0x2) == 0) {
1861                         tmp |= 0x2;
1862                         pci_write_config_byte(memc, 0x44, tmp);
1863                 }
1864
1865                 /* bit 2: drive individual 875 interrupts to the bus */
1866                 pci_read_config_byte(memc, 0x45, &tmp);
1867                 if ((tmp & 0x4) == 0) {
1868                         tmp |= 0x4;
1869                         pci_write_config_byte(memc, 0x45, tmp);
1870                 }
1871
1872                 pci_dev_put(memc);
1873                 break;
1874         }
1875
1876         pci_read_config_byte(pdev, 0x84, &tmp);
1877         sym_dev->host_id = tmp;
1878 }
1879
1880 /*
1881  *  Called before unloading the module.
1882  *  Detach the host.
1883  *  We have to free resources and halt the NCR chip.
1884  */
1885 static int sym_detach(struct sym_hcb *np, struct pci_dev *pdev)
1886 {
1887         printk("%s: detaching ...\n", sym_name(np));
1888
1889         del_timer_sync(&np->s.timer);
1890
1891         /*
1892          * Reset NCR chip.
1893          * We should use sym_soft_reset(), but we don't want to do 
1894          * so, since we may not be safe if interrupts occur.
1895          */
1896         printk("%s: resetting chip\n", sym_name(np));
1897         OUTB(np, nc_istat, SRST);
1898         INB(np, nc_mbox1);
1899         udelay(10);
1900         OUTB(np, nc_istat, 0);
1901
1902         sym_free_resources(np, pdev);
1903
1904         return 1;
1905 }
1906
1907 /*
1908  * Driver host template.
1909  */
1910 static struct scsi_host_template sym2_template = {
1911         .module                 = THIS_MODULE,
1912         .name                   = "sym53c8xx",
1913         .info                   = sym53c8xx_info, 
1914         .queuecommand           = sym53c8xx_queue_command,
1915         .slave_alloc            = sym53c8xx_slave_alloc,
1916         .slave_configure        = sym53c8xx_slave_configure,
1917         .eh_abort_handler       = sym53c8xx_eh_abort_handler,
1918         .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
1919         .eh_bus_reset_handler   = sym53c8xx_eh_bus_reset_handler,
1920         .eh_host_reset_handler  = sym53c8xx_eh_host_reset_handler,
1921         .this_id                = 7,
1922         .use_clustering         = DISABLE_CLUSTERING,
1923 #ifdef SYM_LINUX_PROC_INFO_SUPPORT
1924         .proc_info              = sym53c8xx_proc_info,
1925         .proc_name              = NAME53C8XX,
1926 #endif
1927 };
1928
1929 static int attach_count;
1930
1931 static int __devinit sym2_probe(struct pci_dev *pdev,
1932                                 const struct pci_device_id *ent)
1933 {
1934         struct sym_device sym_dev;
1935         struct sym_nvram nvram;
1936         struct Scsi_Host *instance;
1937
1938         memset(&sym_dev, 0, sizeof(sym_dev));
1939         memset(&nvram, 0, sizeof(nvram));
1940
1941         if (pci_enable_device(pdev))
1942                 goto leave;
1943
1944         pci_set_master(pdev);
1945
1946         if (pci_request_regions(pdev, NAME53C8XX))
1947                 goto disable;
1948
1949         sym_init_device(pdev, &sym_dev);
1950         if (sym_check_supported(&sym_dev))
1951                 goto free;
1952
1953         if (sym_check_raid(&sym_dev))
1954                 goto leave;     /* Don't disable the device */
1955
1956         if (sym_set_workarounds(&sym_dev))
1957                 goto free;
1958
1959         sym_config_pqs(pdev, &sym_dev);
1960
1961         sym_get_nvram(&sym_dev, &nvram);
1962
1963         instance = sym_attach(&sym2_template, attach_count, &sym_dev);
1964         if (!instance)
1965                 goto free;
1966
1967         if (scsi_add_host(instance, &pdev->dev))
1968                 goto detach;
1969         scsi_scan_host(instance);
1970
1971         attach_count++;
1972
1973         return 0;
1974
1975  detach:
1976         sym_detach(pci_get_drvdata(pdev), pdev);
1977  free:
1978         pci_release_regions(pdev);
1979  disable:
1980         pci_disable_device(pdev);
1981  leave:
1982         return -ENODEV;
1983 }
1984
1985 static void __devexit sym2_remove(struct pci_dev *pdev)
1986 {
1987         struct sym_hcb *np = pci_get_drvdata(pdev);
1988         struct Scsi_Host *host = np->s.host;
1989
1990         scsi_remove_host(host);
1991         scsi_host_put(host);
1992
1993         sym_detach(np, pdev);
1994
1995         pci_release_regions(pdev);
1996         pci_disable_device(pdev);
1997
1998         attach_count--;
1999 }
2000
2001 static void sym2_get_signalling(struct Scsi_Host *shost)
2002 {
2003         struct sym_hcb *np = sym_get_hcb(shost);
2004         enum spi_signal_type type;
2005
2006         switch (np->scsi_mode) {
2007         case SMODE_SE:
2008                 type = SPI_SIGNAL_SE;
2009                 break;
2010         case SMODE_LVD:
2011                 type = SPI_SIGNAL_LVD;
2012                 break;
2013         case SMODE_HVD:
2014                 type = SPI_SIGNAL_HVD;
2015                 break;
2016         default:
2017                 type = SPI_SIGNAL_UNKNOWN;
2018                 break;
2019         }
2020         spi_signalling(shost) = type;
2021 }
2022
2023 static void sym2_set_offset(struct scsi_target *starget, int offset)
2024 {
2025         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2026         struct sym_hcb *np = sym_get_hcb(shost);
2027         struct sym_tcb *tp = &np->target[starget->id];
2028
2029         tp->tgoal.offset = offset;
2030         tp->tgoal.check_nego = 1;
2031 }
2032
2033 static void sym2_set_period(struct scsi_target *starget, int period)
2034 {
2035         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2036         struct sym_hcb *np = sym_get_hcb(shost);
2037         struct sym_tcb *tp = &np->target[starget->id];
2038
2039         /* have to have DT for these transfers, but DT will also
2040          * set width, so check that this is allowed */
2041         if (period <= np->minsync && spi_width(starget))
2042                 tp->tgoal.dt = 1;
2043
2044         tp->tgoal.period = period;
2045         tp->tgoal.check_nego = 1;
2046 }
2047
2048 static void sym2_set_width(struct scsi_target *starget, int width)
2049 {
2050         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2051         struct sym_hcb *np = sym_get_hcb(shost);
2052         struct sym_tcb *tp = &np->target[starget->id];
2053
2054         /* It is illegal to have DT set on narrow transfers.  If DT is
2055          * clear, we must also clear IU and QAS.  */
2056         if (width == 0)
2057                 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
2058
2059         tp->tgoal.width = width;
2060         tp->tgoal.check_nego = 1;
2061 }
2062
2063 static void sym2_set_dt(struct scsi_target *starget, int dt)
2064 {
2065         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2066         struct sym_hcb *np = sym_get_hcb(shost);
2067         struct sym_tcb *tp = &np->target[starget->id];
2068
2069         /* We must clear QAS and IU if DT is clear */
2070         if (dt)
2071                 tp->tgoal.dt = 1;
2072         else
2073                 tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
2074         tp->tgoal.check_nego = 1;
2075 }
2076
2077 static void sym2_set_iu(struct scsi_target *starget, int iu)
2078 {
2079         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2080         struct sym_hcb *np = sym_get_hcb(shost);
2081         struct sym_tcb *tp = &np->target[starget->id];
2082
2083         if (iu)
2084                 tp->tgoal.iu = tp->tgoal.dt = 1;
2085         else
2086                 tp->tgoal.iu = 0;
2087         tp->tgoal.check_nego = 1;
2088 }
2089
2090 static void sym2_set_qas(struct scsi_target *starget, int qas)
2091 {
2092         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2093         struct sym_hcb *np = sym_get_hcb(shost);
2094         struct sym_tcb *tp = &np->target[starget->id];
2095
2096         if (qas)
2097                 tp->tgoal.dt = tp->tgoal.qas = 1;
2098         else
2099                 tp->tgoal.qas = 0;
2100         tp->tgoal.check_nego = 1;
2101 }
2102
2103
2104 static struct spi_function_template sym2_transport_functions = {
2105         .set_offset     = sym2_set_offset,
2106         .show_offset    = 1,
2107         .set_period     = sym2_set_period,
2108         .show_period    = 1,
2109         .set_width      = sym2_set_width,
2110         .show_width     = 1,
2111         .set_dt         = sym2_set_dt,
2112         .show_dt        = 1,
2113         .set_iu         = sym2_set_iu,
2114         .show_iu        = 1,
2115         .set_qas        = sym2_set_qas,
2116         .show_qas       = 1,
2117         .get_signalling = sym2_get_signalling,
2118 };
2119
2120 static struct pci_device_id sym2_id_table[] __devinitdata = {
2121         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
2122           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2123         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
2124           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2125         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
2126           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2127         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
2128           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2129         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
2130           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2131         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
2132           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2133         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
2134           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2135         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
2136           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2137         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
2138           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2139         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
2140           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2141         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
2142           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2143         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
2144           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
2145         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
2146           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2147         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
2148           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2149         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
2150           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2151         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
2152           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2153         { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
2154           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
2155         { 0, }
2156 };
2157
2158 MODULE_DEVICE_TABLE(pci, sym2_id_table);
2159
2160 static struct pci_driver sym2_driver = {
2161         .name           = NAME53C8XX,
2162         .id_table       = sym2_id_table,
2163         .probe          = sym2_probe,
2164         .remove         = __devexit_p(sym2_remove),
2165 };
2166
2167 static int __init sym2_init(void)
2168 {
2169         int error;
2170
2171         sym2_setup_params();
2172         sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
2173         if (!sym2_transport_template)
2174                 return -ENODEV;
2175
2176         error = pci_register_driver(&sym2_driver);
2177         if (error)
2178                 spi_release_transport(sym2_transport_template);
2179         return error;
2180 }
2181
2182 static void __exit sym2_exit(void)
2183 {
2184         pci_unregister_driver(&sym2_driver);
2185         spi_release_transport(sym2_transport_template);
2186 }
2187
2188 module_init(sym2_init);
2189 module_exit(sym2_exit);