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