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