File: /usr/src/linux/drivers/scsi/pcmcia/nsp_cs.c
1 /*======================================================================
2
3 NinjaSCSI-3 / NinjaSCSI-32Bi PCMCIA SCSI hostadapter card driver
4 By: YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>
5
6 Ver.2.0 Support 32bit PIO mode
7 Ver.1.1.2 Fix for scatter list buffer exceeds
8 Ver.1.1 Support scatter list
9 Ver.0.1 Initial version
10
11 This software may be used and distributed according to the terms of
12 the GNU General Public License.
13
14 ======================================================================*/
15
16 /***********************************************************************
17 This driver is for these PCcards.
18
19 I-O DATA PCSC-F (Workbit NinjaSCSI-3)
20 "WBT", "NinjaSCSI-3", "R1.0"
21 I-O DATA CBSC-II (Workbit NinjaSCSI-32Bi in 16bit mode)
22 "IO DATA", "CBSC16 ", "1"
23
24 ***********************************************************************/
25
26 /* $Id: nsp_cs.c,v 1.35 2001/07/05 16:58:24 elca Exp $ */
27
28 #ifdef NSP_KERNEL_2_2
29 #include <pcmcia/config.h>
30 #include <pcmcia/k_compat.h>
31 #endif
32
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/init.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/string.h>
39 #include <linux/timer.h>
40 #include <linux/ioport.h>
41 #include <linux/delay.h>
42 #include <linux/tqueue.h>
43 #include <linux/interrupt.h>
44 #include <linux/module.h>
45 #include <linux/major.h>
46 #include <linux/blk.h>
47 #include <linux/stat.h>
48
49 #include <asm/io.h>
50 #include <asm/irq.h>
51
52 #include <../drivers/scsi/scsi.h>
53 #include <../drivers/scsi/hosts.h>
54 #include <../drivers/scsi/sd.h>
55
56 #include <scsi/scsi.h>
57 #include <scsi/scsi_ioctl.h>
58
59 #include <pcmcia/version.h>
60 #include <pcmcia/cs_types.h>
61 #include <pcmcia/cs.h>
62 #include <pcmcia/cistpl.h>
63 #include <pcmcia/ds.h>
64
65 #include "nsp_cs.h"
66
67 MODULE_AUTHOR("YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>");
68 MODULE_DESCRIPTION("WorkBit NinjaSCSI-3 / NinjaSCSI-32Bi(16bit) PCMCIA SCSI host adapter module");
69 MODULE_SUPPORTED_DEVICE("sd,sr,sg,st");
70
71 #ifdef PCMCIA_DEBUG
72 static int pc_debug = PCMCIA_DEBUG;
73 MODULE_PARM(pc_debug, "i");
74 MODULE_PARM_DESC(pc_debug, "set debug level");
75 static char *version = "$Id: nsp_cs.c,v 1.35 2001/07/05 16:58:24 elca Exp $";
76 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
77 #else
78 #define DEBUG(n, args...) /* */
79 #endif
80
81 #include "nsp_io.h"
82
83 /*====================================================================*/
84
85 typedef struct scsi_info_t {
86 dev_link_t link;
87 struct Scsi_Host *host;
88 int ndev;
89 dev_node_t node[8];
90 int stop;
91 struct bus_operations *bus;
92 } scsi_info_t;
93
94 static void nsp_cs_release(u_long arg);
95 static int nsp_cs_event(event_t event, int priority,
96 event_callback_args_t *args);
97 static dev_link_t *nsp_cs_attach(void);
98 static void nsp_cs_detach(dev_link_t *);
99 static int nsp_detect(Scsi_Host_Template * );
100 static int nsp_release(struct Scsi_Host *shpnt);
101 static const char * nsp_info(struct Scsi_Host *shpnt);
102 static int nsp_queuecommand(Scsi_Cmnd *, void (* done)(Scsi_Cmnd *));
103 static int nsp_abort(Scsi_Cmnd *);
104 static int nsp_reset(Scsi_Cmnd *, unsigned int);
105
106
107 /*----------------------------------------------------------------*/
108
109 #if (KERNEL_VERSION(2,4,0) > LINUX_VERSION_CODE)
110 #define PROC_SCSI_NSP PROC_SCSI_IBMMCA /* bad hack... */
111 static struct proc_dir_entry proc_scsi_nsp = {
112 PROC_SCSI_NSP, 6, "nsp_cs",
113 S_IFDIR | S_IRUGO | S_IXUGO, 2
114 };
115 #endif
116
117 /*====================================================================*/
118 /* Parameters that can be set with 'insmod' */
119
120 static unsigned int irq_mask = 0xffff;
121 MODULE_PARM(irq_mask, "i");
122 MODULE_PARM_DESC(irq_mask, "IRQ mask bits");
123
124 static int irq_list[4] = { -1 };
125 MODULE_PARM(irq_list, "1-4i");
126 MODULE_PARM_DESC(irq_list, "IRQ number list");
127
128 /*----------------------------------------------------------------*/
129 /* driver state info, local to driver */
130 static char nspinfo[100]; /* description */
131
132 /* /usr/src/linux/drivers/scsi/hosts.h */
133 static Scsi_Host_Template driver_template = {
134 /* next: NULL,*/
135 #if (KERNEL_VERSION(2,3,99) > LINUX_VERSION_CODE)
136 proc_dir: &proc_scsi_nsp, /* kernel 2.2 */
137 #else
138 proc_name: "nsp_cs", /* kernel 2.4 */
139 #endif
140 /* proc_info: NULL,*/
141 name: "WorkBit NinjaSCSI-3/32Bi",
142 detect: nsp_detect,
143 release: nsp_release,
144 info: nsp_info,
145 /* command: NULL,*/
146 queuecommand: nsp_queuecommand,
147 /* eh_strategy_handler: nsp_eh_strategy,*/
148 eh_abort_handler: nsp_eh_abort,
149 eh_device_reset_handler: nsp_eh_device_reset,
150 eh_bus_reset_handler: nsp_eh_bus_reset,
151 eh_host_reset_handler: nsp_eh_host_reset,
152 abort: nsp_abort,
153 reset: nsp_reset,
154 /* slave_attach: NULL,*/
155 /* bios_param: NULL,*/
156 can_queue: 1,
157 this_id: SCSI_INITIATOR_ID,
158 sg_tablesize: SG_ALL,
159 cmd_per_lun: 1,
160 /* present: 0,*/
161 /* unchecked_isa_dma: 0,*/
162 use_clustering: DISABLE_CLUSTERING,
163 use_new_eh_code: 0,
164 /* emulated: 0,*/
165 };
166
167 static dev_link_t *dev_list = NULL;
168 static dev_info_t dev_info = {"nsp_cs"};
169
170 static nsp_hw_data nsp_data;
171
172 /***********************************************************/
173
174 static int nsp_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
175 {
176 #ifdef PCMCIA_DEBUG
177 //unsigned int host_id = SCpnt->host->this_id;
178 //unsigned int base = SCpnt->host->io_port;
179 unsigned char target = SCpnt->target;
180 #endif
181 nsp_hw_data *data = &nsp_data;
182
183 DEBUG(0, __FUNCTION__ "() SCpnt=0x%p target=%d lun=%d buff=0x%p bufflen=%d use_sg=%d\n",
184 SCpnt, target, SCpnt->lun, SCpnt->request_buffer, SCpnt->request_bufflen, SCpnt->use_sg);
185 //DEBUG(0, " before CurrentSC=0x%p\n", data->CurrentSC);
186
187 if(data->CurrentSC != NULL) {
188 printk(KERN_DEBUG " " __FUNCTION__ "() CurrentSC!=NULL this can't be happen\n");
189 data->CurrentSC = NULL;
190 SCpnt->result = DID_BAD_TARGET << 16;
191 done(SCpnt);
192 return FALSE;
193 }
194
195 show_command(SCpnt);
196
197 SCpnt->scsi_done = done;
198 data->CurrentSC = SCpnt;
199 RESID = SCpnt->request_bufflen;
200
201 SCpnt->SCp.Status = -1;
202 SCpnt->SCp.Message = -1;
203 SCpnt->SCp.have_data_in = IO_UNKNOWN;
204 SCpnt->SCp.sent_command = 0;
205 SCpnt->SCp.phase = PH_UNDETERMINED;
206
207 /* setup scratch area
208 SCp.ptr : buffer pointer
209 SCp.this_residual : buffer length
210 SCp.buffer : next buffer
211 SCp.buffers_residual : left buffers in list
212 SCp.phase : current state of the command */
213 if (SCpnt->use_sg) {
214 SCpnt->SCp.buffer = (struct scatterlist *) SCpnt->request_buffer;
215 SCpnt->SCp.ptr = SCpnt->SCp.buffer->address;
216 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
217 SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
218 } else {
219 SCpnt->SCp.ptr = (char *) SCpnt->request_buffer;
220 SCpnt->SCp.this_residual = SCpnt->request_bufflen;
221 SCpnt->SCp.buffer = NULL;
222 SCpnt->SCp.buffers_residual = 0;
223 }
224
225 if(nsphw_start_selection(SCpnt, data) == FALSE) {
226 DEBUG(0, " selection fail\n");
227 data->CurrentSC = NULL;
228 SCpnt->result = DID_NO_CONNECT << 16;
229 done(SCpnt);
230 return FALSE;
231 }
232
233
234 //DEBUG(0, __FUNCTION__ "() out\n");
235 return TRUE;
236 }
237
238 /*
239 * setup PIO FIFO transfer mode and enable/disable to data out
240 */
241 static void nsp_setup_fifo(nsp_hw_data *data, int enabled)
242 {
243 unsigned int base = data->BaseAddress;
244 unsigned char transfer_mode_reg;
245
246 //DEBUG(0, __FUNCTION__ "() enabled=%d\n", enabled);
247
248 if (enabled != FALSE) {
249 transfer_mode_reg = TRANSFER_GO | BRAIND;
250 } else {
251 transfer_mode_reg = 0;
252 }
253
254 transfer_mode_reg |= data->TransferMode;
255
256 nsp_index_write(base, TRANSFERMODE, transfer_mode_reg);
257 }
258
259 /*
260 * Initialize Ninja hardware
261 */
262 static int nsphw_init(nsp_hw_data *data)
263 {
264 unsigned int base = data->BaseAddress;
265 int i, j;
266 sync_data tmp_sync = { SyncNegotiation: SYNC_NOT_YET,
267 SyncPeriod: 0,
268 SyncOffset: 0
269 };
270
271 DEBUG(0, __FUNCTION__ "() in base=0x%x\n", base);
272
273 data->ScsiClockDiv = CLOCK_40M;
274 data->CurrentSC = NULL;
275 data->FifoCount = 0;
276 data->TransferMode = MODE_IO8;
277
278 /* setup sync data */
279 for ( i = 0; i < N_TARGET; i++ ) {
280 for ( j = 0; j < N_LUN; j++ ) {
281 data->Sync[i][j] = tmp_sync;
282 }
283 }
284
285 /* block all interrupts */
286 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLMASK);
287
288 /* setup SCSI interface */
289 nsp_write(base, IFSELECT, IF_IFSEL);
290
291 nsp_index_write(base, SCSIIRQMODE, 0);
292
293 nsp_index_write(base, TRANSFERMODE, MODE_IO8);
294 nsp_index_write(base, CLOCKDIV, data->ScsiClockDiv);
295
296 nsp_index_write(base, PARITYCTRL, 0);
297 nsp_index_write(base, POINTERCLR, POINTER_CLEAR |
298 ACK_COUNTER_CLEAR |
299 REQ_COUNTER_CLEAR |
300 HOST_COUNTER_CLEAR);
301
302 /* setup fifo asic */
303 nsp_write(base, IFSELECT, IF_REGSEL);
304 nsp_index_write(base, TERMPWRCTRL, 0);
305 if ((nsp_index_read(base, OTHERCONTROL) & TPWR_SENSE) == 0) {
306 printk(KERN_INFO "nsp_cs: terminator power on\n");
307 nsp_index_write(base, TERMPWRCTRL, POWER_ON);
308 }
309
310 nsp_index_write(base, TIMERCOUNT, 0);
311 nsp_index_write(base, TIMERCOUNT, 0); /* requires 2 times!! */
312
313 nsp_index_write(base, SYNCREG, 0);
314 nsp_index_write(base, ACKWIDTH, 0);
315
316 /* enable interrupts and ack them */
317 nsp_index_write(base, SCSIIRQMODE, SCSI_PHASE_CHANGE_EI |
318 RESELECT_EI |
319 SCSI_RESET_IRQ_EI );
320 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLCLEAR);
321
322 nsp_setup_fifo(data, FALSE);
323
324 return TRUE;
325 }
326
327 /*
328 * Start selection phase
329 */
330 static unsigned int nsphw_start_selection(Scsi_Cmnd *SCpnt,
331 nsp_hw_data *data)
332 {
333 unsigned int host_id = SCpnt->host->this_id;
334 unsigned int base = SCpnt->host->io_port;
335 unsigned char target = SCpnt->target;
336 int wait_count;
337 unsigned char phase, arbit;
338
339 //DEBUG(0, __FUNCTION__ "()in\n");
340
341 phase = nsp_index_read(base, SCSIBUSMON);
342 if(phase != BUSMON_BUS_FREE) {
343 //DEBUG(0, " bus busy\n");
344 return FALSE;
345 }
346
347 /* start arbitration */
348 //DEBUG(0, " start arbit\n");
349 SCpnt->SCp.phase = PH_ARBSTART;
350 nsp_index_write(base, SETARBIT, ARBIT_GO);
351
352 wait_count = jiffies + 10 * HZ;
353 do {
354 /* XXX: what a stupid chip! */
355 arbit = nsp_index_read(base, ARBITSTATUS);
356 //DEBUG(0, " arbit=%d, wait_count=%d\n", arbit, wait_count);
357 udelay(1); /* hold 1.2us */
358 } while((arbit & (ARBIT_WIN | ARBIT_FAIL)) == 0 &&
359 time_before(jiffies, wait_count));
360
361 if((arbit & ARBIT_WIN) == 0) {
362 //DEBUG(0, " arbit fail\n");
363 nsp_index_write(base, SETARBIT, ARBIT_FLAG_CLEAR);
364 return FALSE;
365 }
366
367 /* assert select line */
368 //DEBUG(0, " assert SEL line\n");
369 SCpnt->SCp.phase = PH_SELSTART;
370 udelay(3);
371 nsp_index_write(base, SCSIDATALATCH, (1 << host_id) | (1 << target));
372 nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_BSY | SCSI_ATN);
373 udelay(3);
374 nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_BSY | SCSI_DATAOUT_ENB | SCSI_ATN);
375 nsp_index_write(base, SETARBIT, ARBIT_FLAG_CLEAR);
376 udelay(3);
377 nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_DATAOUT_ENB | SCSI_ATN);
378
379 /* check selection timeout */
380 nsp_start_timer(SCpnt, data, 1000/51);
381 data->SelectionTimeOut = 1;
382
383 return TRUE;
384 }
385
386 struct nsp_sync_table {
387 unsigned int min_period;
388 unsigned int max_period;
389 unsigned int chip_period;
390 unsigned int ack_width;
391 };
392
393 static struct nsp_sync_table nsp_sync_table_40M[] = {
394 {0x0c,0x0c,0x1,0}, /* 20MB 50ns*/
395 {0x19,0x19,0x3,1}, /* 10MB 100ns*/
396 {0x1a,0x25,0x5,2}, /* 7.5MB 150ns*/
397 {0x26,0x32,0x7,3}, /* 5MB 200ns*/
398 {0x0, 0, 0, 0}
399 };
400
401 static struct nsp_sync_table nsp_sync_table_20M[] = {
402 {0x19,0x19,0x1,0}, /* 10MB 100ns*/
403 {0x1a,0x25,0x2,0}, /* 7.5MB 150ns*/
404 {0x26,0x32,0x3,1}, /* 5MB 200ns*/
405 {0x0, 0, 0, 0}
406 };
407
408 /*
409 * setup synchronous data transfer mode
410 */
411 static int nsp_msg(Scsi_Cmnd *SCpnt, nsp_hw_data *data)
412 {
413 unsigned char target = SCpnt->target;
414 unsigned char lun = SCpnt->lun;
415 sync_data *sync = &(data->Sync[target][lun]);
416 struct nsp_sync_table *sync_table;
417 unsigned int period, offset;
418 int i;
419
420
421 DEBUG(0, __FUNCTION__ "()\n");
422
423 /**!**/
424
425 period = sync->SyncPeriod;
426 offset = sync->SyncOffset;
427
428 DEBUG(0, " period=0x%x, offset=0x%x\n", period, offset);
429
430 if (data->ScsiClockDiv == CLOCK_20M) {
431 sync_table = &nsp_sync_table_20M[0];
432 } else {
433 sync_table = &nsp_sync_table_40M[0];
434 }
435
436 for ( i = 0; sync_table->max_period != 0; i++, sync_table++) {
437 if ( period >= sync_table->min_period &&
438 period <= sync_table->max_period ) {
439 break;
440 }
441 }
442
443 if (period != 0 && sync_table->max_period == 0) {
444 /*
445 * No proper period/offset found
446 */
447 DEBUG(0, " no proper period/offset\n");
448
449 sync->SyncPeriod = 0;
450 sync->SyncOffset = 0;
451 sync->SyncRegister = 0;
452 sync->AckWidth = 0;
453 sync->SyncNegotiation = SYNC_OK;
454
455 return FALSE;
456 }
457
458 sync->SyncRegister = (sync_table->chip_period << SYNCREG_PERIOD_SHIFT) |
459 (offset & SYNCREG_OFFSET_MASK);
460 sync->AckWidth = sync_table->ack_width;
461 sync->SyncNegotiation = SYNC_OK;
462
463 DEBUG(0, " sync_reg=0x%x, ack_width=0x%x\n", sync->SyncRegister, sync->AckWidth);
464
465 return TRUE;
466 }
467
468
469 /*
470 * start ninja hardware timer
471 */
472 static void nsp_start_timer(Scsi_Cmnd *SCpnt, nsp_hw_data *data, int time)
473 {
474 unsigned int base = SCpnt->host->io_port;
475
476 //DEBUG(0, __FUNCTION__ "() in SCpnt=0x%p, time=%d\n", SCpnt, time);
477 data->TimerCount = time;
478 nsp_index_write(base, TIMERCOUNT, time);
479 }
480
481 /*
482 * wait for bus phase change
483 */
484 static int nsp_negate_signal(Scsi_Cmnd *SCpnt, unsigned char mask, char *str)
485 {
486 unsigned int base = SCpnt->host->io_port;
487 unsigned char reg;
488 int count, i = TRUE;
489
490 //DEBUG(0, __FUNCTION__ "()\n");
491
492 count = jiffies + HZ;
493
494 do {
495 reg = nsp_index_read(base, SCSIBUSMON);
496 if (reg == 0xff) {
497 break;
498 }
499 } while ((i = time_before(jiffies, count)) && (reg & mask) != 0);
500
501 if (!i) {
502 printk(KERN_DEBUG __FUNCTION__ " %s signal off timeut\n", str);
503 }
504
505 return 0;
506 }
507
508 /*
509 * expect Ninja Irq
510 */
511 static int nsp_expect_signal(Scsi_Cmnd *SCpnt,
512 unsigned char current_phase,
513 unsigned char mask)
514 {
515 unsigned int base = SCpnt->host->io_port;
516 int wait_count;
517 unsigned char phase, i_src;
518
519 //DEBUG(0, __FUNCTION__ "() current_phase=0x%x, mask=0x%x\n", current_phase, mask);
520
521 wait_count = jiffies + HZ;
522 do {
523 phase = nsp_index_read(base, SCSIBUSMON);
524 if (phase == 0xff) {
525 //DEBUG(0, " ret -1\n");
526 return -1;
527 }
528 i_src = nsp_read(base, IRQSTATUS);
529 if (i_src & IRQSTATUS_SCSI) {
530 //DEBUG(0, " ret 0 found scsi signal\n");
531 return 0;
532 }
533 if ((phase & mask) != 0 && (phase & BUSMON_PHASE_MASK) == current_phase) {
534 //DEBUG(0, " ret 1 phase=0x%x\n", phase);
535 return 1;
536 }
537 } while(time_before(jiffies, wait_count));
538
539 //DEBUG(0, __FUNCTION__ " : " __FUNCTION__ " timeout\n");
540 return -1;
541 }
542
543 /*
544 * transfer SCSI message
545 */
546 static int nsp_xfer(Scsi_Cmnd *SCpnt, nsp_hw_data *data, int phase)
547 {
548 unsigned int base = SCpnt->host->io_port;
549 char *buf = data->MsgBuffer;
550 int len = MIN(MSGBUF_SIZE, data->MsgLen);
551 int ptr;
552 int ret;
553
554 //DEBUG(0, __FUNCTION__ "()\n");
555 for (ptr = 0; len > 0; len --, ptr ++) {
556
557 ret = nsp_expect_signal(SCpnt, phase, BUSMON_REQ);
558 if (ret <= 0) {
559 DEBUG(0, " xfer quit\n");
560 return 0;
561 }
562
563 /* if last byte, negate ATN */
564 if (len == 1 && SCpnt->SCp.phase == PH_MSG_OUT) {
565 nsp_index_write(base, SCSIBUSCTRL, AUTODIRECTION | ACKENB);
566 }
567
568 /* read & write message */
569 if (phase & BUSMON_IO) {
570 DEBUG(0, " read msg\n");
571 buf[ptr] = nsp_index_read(base, SCSIDATAWITHACK);
572 } else {
573 DEBUG(0, " write msg\n");
574 nsp_index_write(base, SCSIDATAWITHACK, buf[ptr]);
575 }
576 nsp_negate_signal(SCpnt, BUSMON_ACK, "xfer<ack>");
577
578 }
579 return len;
580 }
581
582 /*
583 * get extra SCSI data from fifo
584 */
585 static int nsp_dataphase_bypass(Scsi_Cmnd *SCpnt, nsp_hw_data *data)
586 {
587 unsigned int count;
588
589 //DEBUG(0, __FUNCTION__ "()\n");
590
591 if (SCpnt->SCp.have_data_in != IO_IN) {
592 return 0;
593 }
594
595 count = nsp_fifo_count(SCpnt);
596 if (data->FifoCount == count) {
597 //DEBUG(0, " not use bypass quirk\n");
598 return 0;
599 }
600
601 /*
602 * XXX: NSP_QUIRK
603 * data phase skip only occures in case of SCSI_LOW_READ
604 */
605 SCpnt->SCp.phase = PH_DATA;
606 nsp_pio_read(SCpnt, data);
607 nsp_setup_fifo(data, FALSE);
608
609 DEBUG(0, " use bypass quirk\n");
610 return 0;
611 }
612
613 /*
614 * accept reselection
615 */
616 static int nsp_reselected(Scsi_Cmnd *SCpnt, nsp_hw_data *data)
617 {
618 unsigned int base = SCpnt->host->io_port;
619 unsigned char reg;
620
621 //DEBUG(0, __FUNCTION__ "()\n");
622
623 nsp_negate_signal(SCpnt, BUSMON_SEL, "reselect<SEL>");
624
625 nsp_nexus(SCpnt, data);
626 reg = nsp_index_read(base, SCSIBUSCTRL) & ~(SCSI_BSY | SCSI_ATN);
627 nsp_index_write(base, SCSIBUSCTRL, reg);
628 nsp_index_write(base, SCSIBUSCTRL, reg | AUTODIRECTION | ACKENB);
629
630 return TRUE;
631 }
632
633 /*
634 * count how many data transferd
635 */
636 static int nsp_fifo_count(Scsi_Cmnd *SCpnt)
637 {
638 unsigned int base = SCpnt->host->io_port;
639 unsigned int count;
640 unsigned int l, m, h;
641
642 nsp_index_write(base, POINTERCLR, POINTER_CLEAR);
643
644 l = (unsigned int)nsp_read(base, DATAREG);
645 m = (unsigned int)nsp_read(base, DATAREG);
646 h = (unsigned int)nsp_read(base, DATAREG);
647
648 count = (h << 16) | (m << 8) | (l << 0);
649
650 //DEBUG(0, __FUNCTION__ "() =0x%x\n", count);
651
652 return count;
653 }
654
655 /* fifo size */
656 #define RFIFO_CRIT 64
657 #define WFIFO_CRIT 64
658
659 /*
660 * read data in DATA IN phase
661 */
662 static void nsp_pio_read(Scsi_Cmnd *SCpnt, nsp_hw_data *data)
663 {
664 unsigned int base = SCpnt->host->io_port;
665 int time_out, i;
666 int ocount, res;
667 unsigned char stat, fifo_stat;
668
669 ocount = data->FifoCount;
670
671 DEBUG(0, __FUNCTION__ "() in SCpnt=0x%p resid=%d ocount=%d ptr=0x%p this_residual=%d buffers=0x%p nbuf=%d\n", SCpnt, RESID, ocount, SCpnt->SCp.ptr, SCpnt->SCp.this_residual, SCpnt->SCp.buffer, SCpnt->SCp.buffers_residual);
672
673 time_out = jiffies + 10 * HZ;
674
675 while ((i = time_before(jiffies,time_out)) &&
676 (SCpnt->SCp.this_residual > 0 || SCpnt->SCp.buffers_residual > 0 ) ) {
677
678 stat = nsp_index_read(base, SCSIBUSMON);
679 stat &= BUSMON_PHASE_MASK;
680
681 res = nsp_fifo_count(SCpnt) - ocount;
682
683 //DEBUG(0, " ptr=0x%p this=0x%x ocount=0x%x res=0x%x\n", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, ocount, res);
684 if (res == 0) { /* if some data avilable ? */
685 if (stat == BUSPHASE_DATA_IN) { /* phase changed? */
686 //DEBUG(0, " wait for data this=%d\n", SCpnt->SCp.this_residual);
687 continue;
688 } else {
689 DEBUG(0, " phase changed stat=0x%x\n", stat);
690 break;
691 }
692 }
693
694 fifo_stat = nsp_read(base, FIFOSTATUS);
695 if ((fifo_stat & FIFOSTATUS_FULL_EMPTY) == 0 &&
696 stat == BUSPHASE_DATA_IN) {
697 continue;
698 }
699
700 res = MIN(res, SCpnt->SCp.this_residual);
701
702 switch (data->TransferMode) {
703 case MODE_IO32:
704 res &= ~(BIT(1)|BIT(0)); /* align 4 */
705 nsp_fifo32_read(base, SCpnt->SCp.ptr, res >> 2);
706 break;
707 case MODE_IO8:
708 nsp_fifo8_read (base, SCpnt->SCp.ptr, res );
709 break;
710 default:
711 DEBUG(0, "unknown read mode\n");
712 break;
713 }
714
715 RESID -= res;
716 SCpnt->SCp.ptr += res;
717 SCpnt->SCp.this_residual -= res;
718 ocount += res;
719 //DEBUG(0, " ptr=0x%p this_residual=0x%x ocount=0x%x\n", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, ocount);
720
721 /* go to next scatter list if availavle */
722 if (SCpnt->SCp.this_residual == 0 &&
723 SCpnt->SCp.buffers_residual != 0 ) {
724 //DEBUG(0, " scatterlist next timeout=%d\n", time_out);
725 SCpnt->SCp.buffers_residual--;
726 SCpnt->SCp.buffer++;
727 SCpnt->SCp.ptr = SCpnt->SCp.buffer->address;
728 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
729 }
730
731 time_out = jiffies + 10 * HZ;
732 }
733
734 data->FifoCount = ocount;
735
736 if (!i) {
737 printk(KERN_DEBUG __FUNCTION__ "() pio read timeout resid=%d this_residual=%d buffers_residual=%d\n", RESID, SCpnt->SCp.this_residual, SCpnt->SCp.buffers_residual);
738 }
739 DEBUG(0, " read ocount=0x%x\n", ocount);
740 }
741
742 /*
743 * write data in DATA OUT phase
744 */
745 static void nsp_pio_write(Scsi_Cmnd *SCpnt, nsp_hw_data *data)
746 {
747 unsigned int base = SCpnt->host->io_port;
748 int time_out, i;
749 unsigned int ocount, res;
750 unsigned char stat;
751
752 ocount = data->FifoCount;
753
754 DEBUG(0, __FUNCTION__ "() in fifocount=%d ptr=0x%p this_residual=%d buffers=0x%p nbuf=%d resid=0x%x\n", data->FifoCount, SCpnt->SCp.ptr, SCpnt->SCp.this_residual, SCpnt->SCp.buffer, SCpnt->SCp.buffers_residual, RESID);
755
756 time_out = jiffies + 10 * HZ;
757
758 while ((i = time_before(jiffies, time_out)) &&
759 (SCpnt->SCp.this_residual > 0 || SCpnt->SCp.buffers_residual > 0)) {
760 stat = nsp_index_read(base, SCSIBUSMON);
761 stat &= BUSMON_PHASE_MASK;
762 if (stat != BUSPHASE_DATA_OUT) {
763 DEBUG(0, " phase changed stat=0x%x\n", stat);
764 break;
765 }
766
767 res = ocount - nsp_fifo_count(SCpnt);
768 if (res > 0) { /* write all data? */
769 DEBUG(0, " wait for all data out. ocount=0x%x res=%d\n", ocount, res);
770 continue;
771 }
772
773 res = MIN(SCpnt->SCp.this_residual, WFIFO_CRIT);
774
775 //DEBUG(0, " ptr=0x%p this=0x%x res=0x%x\n", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, res);
776 switch (data->TransferMode) {
777 case MODE_IO32:
778 res &= ~(BIT(1)|BIT(0)); /* align 4 */
779 nsp_fifo32_write(base, SCpnt->SCp.ptr, res >> 2);
780 break;
781 case MODE_IO8:
782 nsp_fifo8_write (base, SCpnt->SCp.ptr, res );
783 break;
784 default:
785 DEBUG(0, "unknown write mode\n");
786 break;
787 }
788
789 RESID -= res;
790 SCpnt->SCp.ptr += res;
791 SCpnt->SCp.this_residual -= res;
792 ocount += res;
793
794 /* go to next scatter list if availavle */
795 if (SCpnt->SCp.this_residual == 0 &&
796 SCpnt->SCp.buffers_residual != 0 ) {
797 //DEBUG(0, " scatterlist next\n");
798 SCpnt->SCp.buffers_residual--;
799 SCpnt->SCp.buffer++;
800 SCpnt->SCp.ptr = SCpnt->SCp.buffer->address;
801 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
802 }
803
804 time_out = jiffies + 10 * HZ;
805 }
806
807 data->FifoCount = ocount;
808
809 if (!i) {
810 printk(KERN_DEBUG __FUNCTION__ "() pio write timeout resid=%d\n", RESID);
811 }
812 //DEBUG(0, " write ocount=%d\n", ocount);
813 }
814
815 #undef RFIFO_CRIT
816 #undef WFIFO_CRIT
817
818 /*
819 * setup synchronous/asynchronous data transfer mode
820 */
821 static int nsp_nexus(Scsi_Cmnd *SCpnt, nsp_hw_data *data)
822 {
823 unsigned int base = SCpnt->host->io_port;
824 unsigned char target = SCpnt->target;
825 unsigned char lun = SCpnt->lun;
826 sync_data *sync = &(data->Sync[target][lun]);
827
828 //DEBUG(0, __FUNCTION__ "() in SCpnt=0x%p\n", SCpnt);
829
830 /* setup synch transfer registers */
831 nsp_index_write(base, SYNCREG, sync->SyncRegister);
832 nsp_index_write(base, ACKWIDTH, sync->AckWidth);
833
834 if (RESID % 4 != 0 ||
835 RESID <= 256 ) {
836 data->TransferMode = MODE_IO8;
837 } else {
838 data->TransferMode = MODE_IO32;
839 }
840
841 /* setup pdma fifo */
842 nsp_setup_fifo(data, TRUE);
843
844 /* clear ack counter */
845 data->FifoCount = 0;
846 nsp_index_write(base, POINTERCLR, POINTER_CLEAR |
847 ACK_COUNTER_CLEAR |
848 REQ_COUNTER_CLEAR |
849 HOST_COUNTER_CLEAR);
850
851 return 0;
852 }
853
854 #include "nsp_message.c"
855 /*
856 * interrupt handler
857 */
858 static void nspintr(int irq, void *dev_id, struct pt_regs *regs)
859 {
860 unsigned int base;
861 unsigned char i_src, irq_phase, phase;
862 Scsi_Cmnd *tmpSC;
863 int len;
864 unsigned char target, lun;
865 unsigned int *sync_neg;
866 int i, tmp;
867 nsp_hw_data *data;
868
869 //printk("&nsp_data=0x%p, dev_id=0x%p\n", &nsp_data, dev_id);
870
871 /* sanity check */
872 if (&nsp_data != dev_id) {
873 DEBUG(0, " irq conflict? this can't happen\n");
874 return;
875 }
876 data = dev_id;
877 if (irq != data->IrqNumber) {
878 return;
879 }
880
881 base = data->BaseAddress;
882 //DEBUG(0, " base=0x%x\n", base);
883
884 /*
885 * interrupt check
886 */
887 nsp_write(base, IRQCONTROL, IRQCONTROL_IRQDISABLE);
888 i_src = nsp_read(base, IRQSTATUS);
889 if (i_src == 0xff || (i_src & IRQSTATUS_MASK) == 0) {
890 nsp_write(base, IRQCONTROL, 0);
891 //DEBUG(0, " no irq\n");
892 return;
893 }
894
895 //DEBUG(0, " i_src=0x%x\n", i_src);
896
897 /* XXX: IMPORTANT
898 * Do not read an irq_phase register if no scsi phase interrupt.
899 * Unless, you should lose a scsi phase interrupt.
900 */
901 phase = nsp_index_read(base, SCSIBUSMON);
902 if((i_src & IRQSTATUS_SCSI) != 0) {
903 irq_phase = nsp_index_read(base, IRQPHASESENCE);
904 } else {
905 irq_phase = 0;
906 }
907
908 //DEBUG(0, " irq_phase=0x%x\n", irq_phase);
909
910 /*
911 * timer interrupt handler (scsi vs timer interrupts)
912 */
913 //DEBUG(0, " timercount=%d\n", data->TimerCount);
914 if (data->TimerCount != 0) {
915 //DEBUG(0, " stop timer\n");
916 nsp_index_write(base, TIMERCOUNT, 0);
917 nsp_index_write(base, TIMERCOUNT, 0);
918 data->TimerCount = 0;
919 }
920
921 if ((i_src & IRQSTATUS_MASK) == IRQSTATUS_TIMER &&
922 data->SelectionTimeOut == 0) {
923 //DEBUG(0, " timer start\n");
924 nsp_write(base, IRQCONTROL, IRQCONTROL_TIMER_CLEAR);
925 return;
926 }
927
928 nsp_write(base, IRQCONTROL, IRQCONTROL_TIMER_CLEAR | IRQCONTROL_FIFO_CLEAR);
929
930 if (data->CurrentSC == NULL) {
931 printk(KERN_DEBUG __FUNCTION__ " CurrentSC==NULL irq_status=0x%x phase=0x%x irq_phase=0x%x this can't be happen\n", i_src, phase, irq_phase);
932 return;
933 } else {
934 tmpSC = data->CurrentSC;
935 target = tmpSC->target;
936 lun = tmpSC->lun;
937 sync_neg = &(data->Sync[target][lun].SyncNegotiation);
938 }
939
940 /*
941 * parse hardware SCSI irq reasons register
942 */
943 if ((i_src & IRQSTATUS_SCSI) != 0) {
944 if ((irq_phase & SCSI_RESET_IRQ) != 0) {
945 printk(KERN_DEBUG " " __FUNCTION__ "() bus reset (power off?)\n");
946 *sync_neg = SYNC_NOT_YET;
947 data->CurrentSC = NULL;
948 tmpSC->result = DID_RESET << 16;
949 tmpSC->scsi_done(tmpSC);
950 return;
951 }
952
953 if ((irq_phase & RESELECT_IRQ) != 0) {
954 DEBUG(0, " reselect\n");
955 nsp_write(base, IRQCONTROL, IRQCONTROL_RESELECT_CLEAR);
956 if (nsp_reselected(tmpSC, data) != FALSE) {
957 return;
958 }
959 }
960
961 if ((irq_phase & (PHASE_CHANGE_IRQ | LATCHED_BUS_FREE)) == 0) {
962 return;
963 }
964 }
965
966 //show_phase(tmpSC);
967
968 switch(tmpSC->SCp.phase) {
969 case PH_SELSTART:
970 *sync_neg = SYNC_NOT_YET;
971 if ((phase & BUSMON_BSY) == 0) {
972 //DEBUG(0, " selection count=%d\n", data->SelectionTimeOut);
973 if (data->SelectionTimeOut >= NSP_SELTIMEOUT) {
974 DEBUG(0, " selection time out\n");
975 data->SelectionTimeOut = 0;
976 nsp_index_write(base, SCSIBUSCTRL, 0);
977
978 data->CurrentSC = NULL;
979 tmpSC->result = DID_NO_CONNECT << 16;
980 tmpSC->scsi_done(tmpSC);
981
982 return;
983 }
984 data->SelectionTimeOut += 1;
985 nsp_start_timer(tmpSC, data, 1000/51);
986 return;
987 }
988
989 /* attention assert */
990 //DEBUG(0, " attention assert\n");
991 data->SelectionTimeOut = 0;
992 tmpSC->SCp.phase = PH_SELECTED;
993 nsp_index_write(base, SCSIBUSCTRL, SCSI_ATN);
994 udelay(1);
995 nsp_index_write(base, SCSIBUSCTRL, SCSI_ATN | AUTODIRECTION | ACKENB);
996 return;
997
998 break;
999
1000 case PH_RESELECT:
1001 //DEBUG(0, " phase reselect\n");
1002 *sync_neg = SYNC_NOT_YET;
1003 if ((phase & BUSMON_PHASE_MASK) != BUSPHASE_MESSAGE_IN) {
1004
1005 data->CurrentSC = NULL;
1006 tmpSC->result = DID_ABORT << 16;
1007 tmpSC->scsi_done(tmpSC);
1008 return;
1009 }
1010 /* fall thru */
1011 default:
1012 if ((i_src & (IRQSTATUS_SCSI | IRQSTATUS_FIFO)) == 0) {
1013 return;
1014 }
1015 break;
1016 }
1017
1018 /*
1019 * SCSI sequencer
1020 */
1021 //DEBUG(0, " start scsi seq\n");
1022
1023 /* normal disconnect */
1024 if ((irq_phase & LATCHED_BUS_FREE) != 0) {
1025 //DEBUG(0, " normal disconnect i_src=0x%x, phase=0x%x, irq_phase=0x%x\n", i_src, phase, irq_phase);
1026 if ((tmpSC->SCp.Message == MSG_COMMAND_COMPLETE)) { /* all command complete and return status */
1027 *sync_neg = SYNC_NOT_YET;
1028 data->CurrentSC = NULL;
1029 tmpSC->result = (DID_OK << 16) |
1030 (tmpSC->SCp.Message << 8) |
1031 (tmpSC->SCp.Status << 0);
1032 DEBUG(0, " command complete result=0x%x\n", tmpSC->result);
1033 tmpSC->scsi_done(tmpSC);
1034 return;
1035 }
1036
1037 return;
1038 }
1039
1040
1041 /* check unexpected bus free state */
1042 if (phase == 0) {
1043 printk(KERN_DEBUG " " __FUNCTION__ " unexpected bus free. i_src=0x%x, phase=0x%x, irq_phase=0x%x\n", i_src, phase, irq_phase);
1044
1045 *sync_neg = SYNC_NOT_YET;
1046 data->CurrentSC = NULL;
1047 tmpSC->result = DID_ERROR << 16;
1048 tmpSC->scsi_done(tmpSC);
1049 return;
1050 }
1051
1052 switch (phase & BUSMON_PHASE_MASK) {
1053 case BUSPHASE_COMMAND:
1054 DEBUG(0, " BUSPHASE_COMMAND\n");
1055 if ((phase & BUSMON_REQ) == 0) {
1056 DEBUG(0, " REQ == 0\n");
1057 return;
1058 }
1059
1060 tmpSC->SCp.phase = PH_COMMAND;
1061
1062 nsp_nexus(tmpSC, data);
1063
1064 /* write scsi command */
1065 nsp_index_write(base, COMMANDCTRL, CLEAR_COMMAND_POINTER);
1066 for (len = 0; len < COMMAND_SIZE(tmpSC->cmnd[0]); len++) {
1067 nsp_index_write(base, COMMANDDATA, tmpSC->cmnd[len]);
1068 }
1069 nsp_index_write(base, COMMANDCTRL, CLEAR_COMMAND_POINTER | AUTO_COMMAND_GO);
1070 break;
1071
1072 case BUSPHASE_DATA_OUT:
1073 DEBUG(0, " BUSPHASE_DATA_OUT\n");
1074
1075 tmpSC->SCp.phase = PH_DATA;
1076 tmpSC->SCp.have_data_in = IO_OUT;
1077
1078 nsp_pio_write(tmpSC, data);
1079
1080 break;
1081
1082 case BUSPHASE_DATA_IN:
1083 DEBUG(0, " BUSPHASE_DATA_IN\n");
1084
1085 tmpSC->SCp.phase = PH_DATA;
1086 tmpSC->SCp.have_data_in = IO_IN;
1087
1088 nsp_pio_read(tmpSC, data);
1089
1090 break;
1091
1092 case BUSPHASE_STATUS:
1093 nsp_dataphase_bypass(tmpSC, data);
1094 DEBUG(0, " BUSPHASE_STATUS\n");
1095
1096 tmpSC->SCp.phase = PH_STATUS;
1097
1098 tmpSC->SCp.Status = nsp_index_read(base, SCSIDATAWITHACK);
1099 //DEBUG(0, " message=0x%x status=0x%x\n", tmpSC->SCp.Message, tmpSC->SCp.Status);
1100
1101 break;
1102
1103 case BUSPHASE_MESSAGE_OUT:
1104 DEBUG(0, " BUSPHASE_MESSAGE_OUT\n");
1105 if ((phase & BUSMON_REQ) == 0) {
1106 goto timer_out;
1107 }
1108
1109 tmpSC->SCp.phase = PH_MSG_OUT;
1110
1111 data->MsgLen = len = 0;
1112 if (*sync_neg == SYNC_NOT_YET) {
1113 data->Sync[target][lun].SyncPeriod = 0;
1114 data->Sync[target][lun].SyncOffset = 0;
1115 nsp_msg(tmpSC, data);
1116
1117 data->MsgBuffer[len] = IDENTIFY(TRUE, lun); len++;
1118 /*
1119 data->MsgBuffer[len] = MSG_EXTENDED; len++;
1120 data->MsgBuffer[len] = 3; len++;
1121 data->MsgBuffer[len] = MSG_EXT_SDTR; len++;
1122 data->MsgBuffer[len] = 0x0c; len++;
1123 data->MsgBuffer[len] = 15; len++;
1124 */
1125 }
1126 if (len == 0) {
1127 data->MsgBuffer[len] = MSG_NO_OPERATION; len++;
1128 }
1129 data->MsgLen = len;
1130
1131 show_message(data);
1132 nsp_message_out(tmpSC, data);
1133 break;
1134
1135 case BUSPHASE_MESSAGE_IN:
1136 nsp_dataphase_bypass(tmpSC, data);
1137 DEBUG(0, " BUSPHASE_MESSAGE_IN\n");
1138 if ((phase & BUSMON_REQ) == 0) {
1139 goto timer_out;
1140 }
1141
1142 tmpSC->SCp.phase = PH_MSG_IN;
1143 nsp_message_in(tmpSC, data);
1144
1145 /*
1146 if (data->MsgLen >= 5 &&
1147 data->MsgBuffer[0] == MSG_EXTENDED &&
1148 data->MsgBuffer[1] == 3 &&
1149 data->MsgBuffer[2] == MSG_EXT_SDTR ) {
1150 data->Sync[target][lun].SyncPeriod = data->MsgBuffer[3];
1151 data->Sync[target][lun].SyncOffset = data->MsgBuffer[4];
1152 nsp_msg(tmpSC, data);
1153 }
1154 */
1155
1156 /* search last messeage byte */
1157 tmp = -1;
1158 for (i = 0; i < data->MsgLen; i++) {
1159 tmp = data->MsgBuffer[i];
1160 if (data->MsgBuffer[i] == MSG_EXTENDED) {
1161 i += (1 + data->MsgBuffer[i+1]);
1162 }
1163 }
1164 tmpSC->SCp.Message = tmp;
1165
1166 DEBUG(0, " message=0x%x len=%d\n", tmpSC->SCp.Message, data->MsgLen);
1167 show_message(data);
1168
1169 break;
1170
1171 case BUSPHASE_SELECT:
1172 default:
1173 DEBUG(0, " BUSPHASE other\n");
1174
1175 break;
1176 }
1177
1178 //DEBUG(0, __FUNCTION__ "() out\n");
1179 return;
1180
1181 timer_out:
1182 nsp_start_timer(tmpSC, data, 1000/102);
1183 return;
1184 }
1185
1186 #ifdef DBG_SHOWCOMMAND
1187 #include "nsp_debug.c"
1188 #endif /* DBG_SHOWCOMMAND */
1189
1190 /*----------------------------------------------------------------*/
1191 /* look for ninja3 card and init if found */
1192 /*----------------------------------------------------------------*/
1193 static int nsp_detect(Scsi_Host_Template *sht)
1194 {
1195 struct Scsi_Host *host; /* registered host structure */
1196 nsp_hw_data *data = &nsp_data;
1197
1198 DEBUG(0, __FUNCTION__ " this_id=%d\n", sht->this_id);
1199
1200 request_region(data->BaseAddress, data->NumAddress, "nsp_cs");
1201 host = scsi_register(sht, 0);
1202 host->io_port = data->BaseAddress;
1203 host->unique_id = data->BaseAddress;
1204 host->n_io_port = data->NumAddress;
1205 host->irq = data->IrqNumber;
1206 host->dma_channel = -1;
1207
1208 sprintf(nspinfo,
1209 /* Buffer size is 100 bytes */
1210 /* 0 1 2 3 4 5 6 7 8 9 0*/
1211 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890*/
1212 "NinjaSCSI-3/32Bi Driver version 2.7, I/O 0x%04lx-0x%04lx IRQ %2d",
1213 host->io_port, host->io_port + host->n_io_port,
1214 host->irq);
1215 sht->name = nspinfo;
1216
1217 DEBUG(0, __FUNCTION__ " end\n");
1218
1219 return 1; /* detect done. */
1220 }
1221
1222 static int nsp_release(struct Scsi_Host *shpnt)
1223 {
1224 nsp_hw_data *data = &nsp_data;
1225
1226 if (shpnt->irq) {
1227 free_irq(shpnt->irq, data);
1228 }
1229 if (shpnt->io_port) {
1230 release_region(shpnt->io_port, shpnt->n_io_port);
1231 }
1232 return 0;
1233 }
1234
1235 /*----------------------------------------------------------------*/
1236 /* return info string */
1237 /*----------------------------------------------------------------*/
1238 static const char *nsp_info(struct Scsi_Host *shpnt)
1239 {
1240 return nspinfo;
1241 }
1242
1243 /*---------------------------------------------------------------*/
1244 /* error handler */
1245 /*---------------------------------------------------------------*/
1246 static int nsp_reset(Scsi_Cmnd *SCpnt, unsigned int why)
1247 {
1248 DEBUG(0, __FUNCTION__ " SCpnt=0x%p why=%d\n", SCpnt, why);
1249
1250 return nsp_eh_bus_reset(SCpnt);
1251 }
1252
1253 static int nsp_abort(Scsi_Cmnd *SCpnt)
1254 {
1255 DEBUG(0, __FUNCTION__ " SCpnt=0x%p\n", SCpnt);
1256
1257 nsp_eh_bus_reset(SCpnt);
1258
1259 return SUCCESS;
1260 }
1261
1262 /*static int nsp_eh_strategy(struct Scsi_Host *Shost)
1263 {
1264 return FAILED;
1265 }*/
1266
1267 static int nsp_eh_abort(Scsi_Cmnd *SCpnt)
1268 {
1269 DEBUG(0, __FUNCTION__ " SCpnt=0x%p\n", SCpnt);
1270
1271 nsp_eh_bus_reset(SCpnt);
1272 return SUCCESS;
1273 }
1274
1275 static int nsp_eh_device_reset(Scsi_Cmnd *SCpnt)
1276 {
1277 DEBUG(0, __FUNCTION__ " SCpnt=0x%p\n", SCpnt);
1278
1279 return FAILED;
1280 }
1281
1282 static int nsp_eh_bus_reset(Scsi_Cmnd *SCpnt)
1283 {
1284 unsigned int base = SCpnt->host->io_port;
1285 int i;
1286
1287 DEBUG(0, __FUNCTION__ "() SCpnt=0x%p base=0x%x\n", SCpnt, base);
1288
1289 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLMASK);
1290
1291 nsp_index_write(base, SCSIBUSCTRL, SCSI_RST);
1292 mdelay(100); /* 100ms */
1293 nsp_index_write(base, SCSIBUSCTRL, 0);
1294 for(i = 0; i < 5; i++) {
1295 nsp_index_read(base, IRQPHASESENCE); /* dummy read */
1296 }
1297
1298 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLCLEAR);
1299
1300 return SUCCESS;
1301 }
1302
1303 static int nsp_eh_host_reset(Scsi_Cmnd *SCpnt)
1304 {
1305 nsp_hw_data *data = &nsp_data;
1306
1307 DEBUG(0, __FUNCTION__ "\n");
1308
1309 nsphw_init(data);
1310 nsp_eh_bus_reset(SCpnt);
1311
1312 return SUCCESS;
1313 }
1314
1315
1316 /**********************************************************************
1317 PCMCIA functions
1318 *********************************************************************/
1319
1320 /*====================================================================*/
1321 static void cs_error(client_handle_t handle, int func, int ret)
1322 {
1323 error_info_t err = { func, ret };
1324 CardServices(ReportError, handle, &err);
1325 }
1326
1327 /*======================================================================
1328 nsp_cs_attach() creates an "instance" of the driver, allocating
1329 local data structures for one device. The device is registered
1330 with Card Services.
1331
1332 The dev_link structure is initialized, but we don't actually
1333 configure the card at this point -- we wait until we receive a
1334 card insertion event.
1335 ======================================================================*/
1336 static dev_link_t *nsp_cs_attach(void)
1337 {
1338 scsi_info_t *info;
1339 client_reg_t client_reg;
1340 dev_link_t *link;
1341 int ret, i;
1342
1343 DEBUG(0, __FUNCTION__ "()\n");
1344
1345 /* Create new SCSI device */
1346 info = kmalloc(sizeof(*info), GFP_KERNEL);
1347 if (!info) { return NULL; }
1348 memset(info, 0, sizeof(*info));
1349 link = &info->link;
1350 link->priv = info;
1351
1352 /* Initialize the dev_link_t structure */
1353 link->release.function = &nsp_cs_release;
1354 link->release.data = (u_long)link;
1355
1356 /* The io structure describes IO port mapping */
1357 link->io.NumPorts1 = 0x10;
1358 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
1359 link->io.IOAddrLines = 10; /* not used */
1360
1361 /* Interrupt setup */
1362 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
1363 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
1364 if (irq_list[0] == -1) {
1365 link->irq.IRQInfo2 = irq_mask;
1366 } else {
1367 for (i = 0; i < 4; i++) {
1368 link->irq.IRQInfo2 |= 1 << irq_list[i];
1369 }
1370 }
1371 link->irq.Handler = &nspintr;
1372 link->irq.Instance = &nsp_data;
1373
1374 /* General socket configuration */
1375 link->conf.Attributes = CONF_ENABLE_IRQ;
1376 link->conf.Vcc = 50;
1377 link->conf.IntType = INT_MEMORY_AND_IO;
1378 link->conf.Present = PRESENT_OPTION;
1379
1380
1381 /* Register with Card Services */
1382 link->next = dev_list;
1383 dev_list = link;
1384 client_reg.dev_info = &dev_info;
1385 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
1386 client_reg.EventMask =
1387 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
1388 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
1389 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME ;
1390 client_reg.event_handler = &nsp_cs_event;
1391 client_reg.Version = 0x0210;
1392 client_reg.event_callback_args.client_data = link;
1393 ret = CardServices(RegisterClient, &link->handle, &client_reg);
1394 if (ret != CS_SUCCESS) {
1395 cs_error(link->handle, RegisterClient, ret);
1396 nsp_cs_detach(link);
1397 return NULL;
1398 }
1399
1400 return link;
1401 } /* nsp_cs_attach */
1402
1403
1404 /*======================================================================
1405 This deletes a driver "instance". The device is de-registered
1406 with Card Services. If it has been released, all local data
1407 structures are freed. Otherwise, the structures will be freed
1408 when the device is released.
1409 ======================================================================*/
1410 static void nsp_cs_detach(dev_link_t *link)
1411 {
1412 dev_link_t **linkp;
1413
1414 DEBUG(0, __FUNCTION__ "(0x%p)\n", link);
1415
1416 /* Locate device structure */
1417 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next) {
1418 if (*linkp == link) {
1419 break;
1420 }
1421 }
1422 if (*linkp == NULL) {
1423 return;
1424 }
1425
1426 del_timer(&link->release);
1427 if (link->state & DEV_CONFIG) {
1428 nsp_cs_release((u_long)link);
1429 if (link->state & DEV_STALE_CONFIG) {
1430 link->state |= DEV_STALE_LINK;
1431 return;
1432 }
1433 }
1434
1435 /* Break the link with Card Services */
1436 if (link->handle) {
1437 CardServices(DeregisterClient, link->handle);
1438 }
1439
1440 /* Unlink device structure, free bits */
1441 *linkp = link->next;
1442 kfree(link->priv);
1443
1444 } /* nsp_cs_detach */
1445
1446
1447 /*======================================================================
1448 nsp_cs_config() is scheduled to run after a CARD_INSERTION event
1449 is received, to configure the PCMCIA socket, and to make the
1450 ethernet device available to the system.
1451 ======================================================================*/
1452 #define CS_CHECK(fn, args...) \
1453 while ((last_ret=CardServices(last_fn=(fn),args))!=0) goto cs_failed
1454 #define CFG_CHECK(fn, args...) \
1455 if (CardServices(fn, args) != 0) goto next_entry
1456 /*====================================================================*/
1457
1458 static void nsp_cs_config(dev_link_t *link)
1459 {
1460 client_handle_t handle = link->handle;
1461 scsi_info_t *info = link->priv;
1462 tuple_t tuple;
1463 cisparse_t parse;
1464 int i, last_ret, last_fn;
1465 u_char tuple_data[64];
1466 config_info_t conf;
1467 Scsi_Device *dev;
1468 dev_node_t **tail, *node;
1469 struct Scsi_Host *host;
1470 nsp_hw_data *data = &nsp_data;
1471
1472 DEBUG(0, __FUNCTION__ "() in\n");
1473
1474 tuple.DesiredTuple = CISTPL_CONFIG;
1475 tuple.Attributes = 0;
1476 tuple.TupleData = tuple_data;
1477 tuple.TupleDataMax = sizeof(tuple_data);
1478 tuple.TupleOffset = 0;
1479 CS_CHECK(GetFirstTuple, handle, &tuple);
1480 CS_CHECK(GetTupleData, handle, &tuple);
1481 CS_CHECK(ParseTuple, handle, &tuple, &parse);
1482 link->conf.ConfigBase = parse.config.base;
1483 link->conf.Present = parse.config.rmask[0];
1484
1485 /* Configure card */
1486 driver_template.module = &__this_module;
1487 link->state |= DEV_CONFIG;
1488
1489 /* Look up the current Vcc */
1490 CS_CHECK(GetConfigurationInfo, handle, &conf);
1491 link->conf.Vcc = conf.Vcc;
1492
1493 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
1494 CS_CHECK(GetFirstTuple, handle, &tuple);
1495 while (1) {
1496 CFG_CHECK(GetTupleData, handle, &tuple);
1497 CFG_CHECK(ParseTuple, handle, &tuple, &parse);
1498 link->conf.ConfigIndex = parse.cftable_entry.index;
1499 link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
1500 i = CardServices(RequestIO, handle, &link->io);
1501 if (i == CS_SUCCESS) {
1502 break;
1503 }
1504 next_entry:
1505 DEBUG(0, __FUNCTION__ " next\n");
1506 CS_CHECK(GetNextTuple, handle, &tuple);
1507 }
1508
1509 CS_CHECK(RequestIRQ, handle, &link->irq);
1510 CS_CHECK(RequestConfiguration, handle, &link->conf);
1511
1512 /* A bad hack... */
1513 release_region(link->io.BasePort1, link->io.NumPorts1);
1514
1515 /* Set port and IRQ */
1516 data->BaseAddress = link->io.BasePort1;
1517 data->NumAddress = link->io.NumPorts1;
1518 data->IrqNumber = link->irq.AssignedIRQ;
1519
1520 DEBUG(0, __FUNCTION__ " I/O[0x%x+0x%x] IRQ %d\n",
1521 data->BaseAddress, data->NumAddress, data->IrqNumber);
1522
1523 if(nsphw_init(data) == FALSE) {
1524 goto cs_failed;
1525 }
1526
1527 scsi_register_module(MODULE_SCSI_HA, &driver_template);
1528
1529 DEBUG(0, "GET_SCSI_INFO\n");
1530 tail = &link->dev;
1531 info->ndev = 0;
1532 for (host = scsi_hostlist; host != NULL; host = host->next) {
1533 if (host->hostt == &driver_template) {
1534 for (dev = host->host_queue; dev != NULL; dev = dev->next) {
1535 u_long arg[2], id;
1536 kernel_scsi_ioctl(dev, SCSI_IOCTL_GET_IDLUN, arg);
1537 id = (arg[0]&0x0f) + ((arg[0]>>4)&0xf0) +
1538 ((arg[0]>>8)&0xf00) + ((arg[0]>>12)&0xf000);
1539 node = &info->node[info->ndev];
1540 node->minor = 0;
1541 switch (dev->type) {
1542 case TYPE_TAPE:
1543 node->major = SCSI_TAPE_MAJOR;
1544 sprintf(node->dev_name, "st#%04lx", id);
1545 break;
1546 case TYPE_DISK:
1547 case TYPE_MOD:
1548 node->major = SCSI_DISK0_MAJOR;
1549 sprintf(node->dev_name, "sd#%04lx", id);
1550 break;
1551 case TYPE_ROM:
1552 case TYPE_WORM:
1553 node->major = SCSI_CDROM_MAJOR;
1554 sprintf(node->dev_name, "sr#%04lx", id);
1555 break;
1556 default:
1557 node->major = SCSI_GENERIC_MAJOR;
1558 sprintf(node->dev_name, "sg#%04lx", id);
1559 break;
1560 }
1561 *tail = node; tail = &node->next;
1562 info->ndev++;
1563 info->host = dev->host;
1564 }
1565 }
1566 }
1567 *tail = NULL;
1568 if (info->ndev == 0) {
1569 printk(KERN_INFO "nsp_cs: no SCSI devices found\n");
1570 }
1571
1572 /* Finally, report what we've done */
1573 printk(KERN_INFO "nsp_cs: index 0x%02x: Vcc %d.%d",
1574 link->conf.ConfigIndex,
1575 link->conf.Vcc/10, link->conf.Vcc%10);
1576 if (link->conf.Vpp1) {
1577 printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
1578 }
1579 if (link->conf.Attributes & CONF_ENABLE_IRQ) {
1580 printk(", irq %d", link->irq.AssignedIRQ);
1581 }
1582 if (link->io.NumPorts1) {
1583 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
1584 link->io.BasePort1+link->io.NumPorts1-1);
1585 }
1586 printk("\n");
1587
1588 link->state &= ~DEV_CONFIG_PENDING;
1589 return;
1590
1591 cs_failed:
1592 cs_error(link->handle, last_fn, last_ret);
1593 nsp_cs_release((u_long)link);
1594 return;
1595
1596 } /* nsp_cs_config */
1597 #undef CS_CHECK
1598 #undef CFG_CHECK
1599
1600 /*======================================================================
1601 After a card is removed, nsp_cs_release() will unregister the net
1602 device, and release the PCMCIA configuration. If the device is
1603 still open, this will be postponed until it is closed.
1604 ======================================================================*/
1605 static void nsp_cs_release(u_long arg)
1606 {
1607 dev_link_t *link = (dev_link_t *)arg;
1608
1609 DEBUG(0, __FUNCTION__ "(0x%p)\n", link);
1610
1611 /*
1612 * If the device is currently in use, we won't release until it
1613 * is actually closed.
1614 */
1615 if (link->open) {
1616 DEBUG(1, "nsp_cs: release postponed, '%s' still open\n",
1617 link->dev->dev_name);
1618 link->state |= DEV_STALE_CONFIG;
1619 return;
1620 }
1621
1622 /* Unlink the device chain */
1623 scsi_unregister_module(MODULE_SCSI_HA, &driver_template);
1624 link->dev = NULL;
1625
1626 if (link->win) {
1627 CardServices(ReleaseWindow, link->win);
1628 }
1629 CardServices(ReleaseConfiguration, link->handle);
1630 if (link->io.NumPorts1) {
1631 CardServices(ReleaseIO, link->handle, &link->io);
1632 }
1633 if (link->irq.AssignedIRQ) {
1634 CardServices(ReleaseIRQ, link->handle, &link->irq);
1635 }
1636 link->state &= ~DEV_CONFIG;
1637
1638 if (link->state & DEV_STALE_LINK) {
1639 nsp_cs_detach(link);
1640 }
1641 } /* nsp_cs_release */
1642
1643 /*======================================================================
1644
1645 The card status event handler. Mostly, this schedules other
1646 stuff to run after an event is received. A CARD_REMOVAL event
1647 also sets some flags to discourage the net drivers from trying
1648 to talk to the card any more.
1649
1650 When a CARD_REMOVAL event is received, we immediately set a flag
1651 to block future accesses to this device. All the functions that
1652 actually access the device should check this flag to make sure
1653 the card is still present.
1654
1655 ======================================================================*/
1656 static int nsp_cs_event(event_t event,
1657 int priority,
1658 event_callback_args_t *args)
1659 {
1660 dev_link_t *link = args->client_data;
1661 scsi_info_t *info = link->priv;
1662
1663 DEBUG(1, __FUNCTION__ "(0x%06x)\n", event);
1664
1665 switch (event) {
1666 case CS_EVENT_CARD_REMOVAL:
1667 DEBUG(0, " event: remove\n");
1668 link->state &= ~DEV_PRESENT;
1669 if (link->state & DEV_CONFIG) {
1670 ((scsi_info_t *)link->priv)->stop = 1;
1671 mod_timer(&link->release, jiffies + HZ/20);
1672 }
1673 break;
1674
1675 case CS_EVENT_CARD_INSERTION:
1676 DEBUG(0, " event: insert\n");
1677 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1678 info->bus = args->bus;
1679 nsp_cs_config(link);
1680 break;
1681
1682 case CS_EVENT_PM_SUSPEND:
1683 link->state |= DEV_SUSPEND;
1684 /* Fall through... */
1685 case CS_EVENT_RESET_PHYSICAL:
1686 /* Mark the device as stopped, to block IO until later */
1687 info->stop = 1;
1688 if (link->state & DEV_CONFIG) {
1689 CardServices(ReleaseConfiguration, link->handle);
1690 }
1691 break;
1692
1693 case CS_EVENT_PM_RESUME:
1694 link->state &= ~DEV_SUSPEND;
1695 /* Fall through... */
1696 case CS_EVENT_CARD_RESET:
1697 DEBUG(0, " event: reset\n");
1698 if (link->state & DEV_CONFIG) {
1699 Scsi_Cmnd tmp;
1700
1701 CardServices(RequestConfiguration, link->handle, &link->conf);
1702 tmp.host = info->host;
1703 nsp_eh_host_reset(&tmp);
1704 }
1705 info->stop = 0;
1706 break;
1707
1708 default:
1709 DEBUG(0, " event: unknown\n");
1710 break;
1711 }
1712 DEBUG(0, __FUNCTION__ " end\n");
1713 return 0;
1714 } /* nsp_cs_event */
1715
1716 /*======================================================================*
1717 * module entry point
1718 *====================================================================*/
1719 static int __init nsp_cs_init(void)
1720 {
1721 servinfo_t serv;
1722
1723 DEBUG(0, __FUNCTION__ "() in\n");
1724 DEBUG(0, "%s\n", version);
1725 CardServices(GetCardServicesInfo, &serv);
1726 if (serv.Revision != CS_RELEASE_CODE) {
1727 printk(KERN_DEBUG "nsp_cs: Card Services release "
1728 "does not match!\n");
1729 return -1;
1730 }
1731 register_pcmcia_driver(&dev_info, &nsp_cs_attach, &nsp_cs_detach);
1732
1733 DEBUG(0, __FUNCTION__ "() out\n");
1734 return 0;
1735 }
1736
1737
1738 static void __exit nsp_cs_cleanup(void)
1739 {
1740 DEBUG(0, __FUNCTION__ "() unloading\n");
1741 unregister_pcmcia_driver(&dev_info);
1742 while (dev_list != NULL) {
1743 if (dev_list->state & DEV_CONFIG) {
1744 nsp_cs_release((u_long)dev_list);
1745 }
1746 nsp_cs_detach(dev_list);
1747 }
1748 }
1749
1750 module_init(nsp_cs_init);
1751 module_exit(nsp_cs_cleanup);
1752
1753 /*
1754 *
1755 *
1756 */
1757
1758 /* end */
1759