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