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