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