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