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