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