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