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