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