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