Import 2.2.8pre4
[davej-history.git] / drivers / scsi / imm.c
blobfa8844b673eb355223680f3992331fef7d85582b
1 /* imm.c -- low level driver for the IOMEGA MatchMaker
2 * parallel port SCSI host adapter.
3 *
4 * (The IMM is the embedded controller in the ZIP Plus drive.)
5 *
6 * Current Maintainer: David Campbell (Perth, Western Australia)
7 * campbell@torque.net
9 * My unoffical company acronym list is 21 pages long:
10 * FLA: Four letter acronym with built in facility for
11 * future expansion to five letters.
14 #include <linux/config.h>
16 /* The following #define is to avoid a clash with hosts.c */
17 #define IMM_CODE 1
18 #define IMM_PROBE_SPP 0x0001
19 #define IMM_PROBE_PS2 0x0002
20 #define IMM_PROBE_ECR 0x0010
21 #define IMM_PROBE_EPP17 0x0100
22 #define IMM_PROBE_EPP19 0x0200
24 voidimm_reset_pulse(unsigned int base);
25 static intdevice_check(int host_no);
27 #include <linux/blk.h>
28 #include <asm/io.h>
29 #include <linux/parport.h>
30 #include"sd.h"
31 #include"hosts.h"
32 typedefstruct{
33 struct pardevice *dev;/* Parport device entry */
34 int base;/* Actual port address */
35 int mode;/* Transfer mode */
36 int host;/* Host number (for proc) */
37 Scsi_Cmnd *cur_cmd;/* Current queued command */
38 struct tq_struct imm_tq;/* Polling interupt stuff */
39 unsigned long jstart;/* Jiffies at start */
40 unsigned failed:1;/* Failure flag */
41 unsigned dp:1;/* Data phase present */
42 unsigned rd:1;/* Read data in data phase */
43 unsigned p_busy:1;/* Parport sharing busy flag */
44 } imm_struct;
46 #define IMM_EMPTY \
47 { dev: NULL, \
48 base: -1, \
49 mode: IMM_AUTODETECT, \
50 host: -1, \
51 cur_cmd: NULL, \
52 imm_tq: {0, 0, imm_interrupt, NULL}, \
53 jstart: 0, \
54 failed: 0, \
55 dp: 0, \
56 rd: 0, \
57 p_busy: 0 \
60 #include"imm.h"
61 #define NO_HOSTS 4
62 static imm_struct imm_hosts[NO_HOSTS] =
63 {IMM_EMPTY, IMM_EMPTY, IMM_EMPTY, IMM_EMPTY};
65 #define IMM_BASE(x) imm_hosts[(x)].base
67 int parbus_base[NO_HOSTS] =
68 {0x03bc,0x0378,0x0278,0x0000};
70 voidimm_wakeup(void*ref)
72 imm_struct *imm_dev = (imm_struct *) ref;
74 if(!imm_dev->p_busy)
75 return;
77 if(parport_claim(imm_dev->dev)) {
78 printk("imm: bug in imm_wakeup\n");
79 return;
81 imm_dev->p_busy =0;
82 imm_dev->base = imm_dev->dev->port->base;
83 if(imm_dev->cur_cmd)
84 imm_dev->cur_cmd->SCp.phase++;
85 return;
88 intimm_release(struct Scsi_Host *host)
90 int host_no = host->unique_id;
92 printk("Releasing imm%i\n", host_no);
93 parport_unregister_device(imm_hosts[host_no].dev);
94 return0;
97 static intimm_pb_claim(int host_no)
99 if(parport_claim(imm_hosts[host_no].dev)) {
100 imm_hosts[host_no].p_busy =1;
101 return1;
103 if(imm_hosts[host_no].cur_cmd)
104 imm_hosts[host_no].cur_cmd->SCp.phase++;
105 return0;
108 #define imm_pb_release(x) parport_release(imm_hosts[(x)].dev)
110 /***************************************************************************
111 * Parallel port probing routines *
112 ***************************************************************************/
114 #ifdef MODULE
115 Scsi_Host_Template driver_template = IMM;
116 #include"scsi_module.c"
117 #endif
119 intimm_detect(Scsi_Host_Template * host)
121 struct Scsi_Host *hreg;
122 int ports;
123 int i, nhosts, try_again;
124 struct parport *pb =parport_enumerate();
126 printk("imm: Version %s\n", IMM_VERSION);
127 nhosts =0;
128 try_again =0;
130 if(!pb) {
131 printk("imm: parport reports no devices.\n");
132 return0;
134 retry_entry:
135 for(i =0; pb; i++, pb = pb->next) {
136 int modes, ppb;
138 imm_hosts[i].dev =
139 parport_register_device(pb,"imm", NULL, imm_wakeup,
140 NULL,0, (void*) &imm_hosts[i]);
142 if(!imm_hosts[i].dev)
143 continue;
145 /* Claim the bus so it remembers what we do to the control
146 * registers. [ CTR and ECP ]
148 if(imm_pb_claim(i)) {
149 unsigned long now = jiffies;
150 while(imm_hosts[i].p_busy) {
151 schedule();/* We are safe to schedule here */
152 if(time_after(jiffies, now +3* HZ)) {
153 printk(KERN_ERR "imm%d: failed to claim parport because a "
154 "pardevice is owning the port for too longtime!\n",
156 return0;
160 ppb =IMM_BASE(i) = imm_hosts[i].dev->port->base;
161 w_ctr(ppb,0x0c);
162 modes = imm_hosts[i].dev->port->modes;
164 /* Mode detection works up the chain of speed
165 * This avoids a nasty if-then-else-if-... tree
167 imm_hosts[i].mode = IMM_NIBBLE;
169 if(modes & PARPORT_MODE_PCPS2)
170 imm_hosts[i].mode = IMM_PS2;
172 if(modes & PARPORT_MODE_PCECPPS2) {
173 w_ecr(ppb,0x20);
174 imm_hosts[i].mode = IMM_PS2;
176 if(modes & PARPORT_MODE_PCECPEPP)
177 w_ecr(ppb,0x80);
179 /* Done configuration */
180 imm_pb_release(i);
182 if(imm_init(i)) {
183 parport_unregister_device(imm_hosts[i].dev);
184 continue;
186 /* now the glue ... */
187 switch(imm_hosts[i].mode) {
188 case IMM_NIBBLE:
189 ports =3;
190 break;
191 case IMM_PS2:
192 ports =3;
193 break;
194 case IMM_EPP_8:
195 case IMM_EPP_16:
196 case IMM_EPP_32:
197 ports =8;
198 break;
199 default:/* Never gets here */
200 continue;
203 host->can_queue = IMM_CAN_QUEUE;
204 host->sg_tablesize = imm_sg;
205 hreg =scsi_register(host,0);
206 hreg->io_port = pb->base;
207 hreg->n_io_port = ports;
208 hreg->dma_channel = -1;
209 hreg->unique_id = i;
210 imm_hosts[i].host = hreg->host_no;
211 nhosts++;
213 if(nhosts ==0) {
214 if(try_again ==1)
215 return0;
216 try_again =1;
217 goto retry_entry;
218 }else
219 return1;/* return number of hosts detected */
222 /* This is to give the imm driver a way to modify the timings (and other
223 * parameters) by writing to the /proc/scsi/imm/0 file.
224 * Very simple method really... (To simple, no error checking :( )
225 * Reason: Kernel hackers HATE having to unload and reload modules for
226 * testing...
227 * Also gives a method to use a script to obtain optimum timings (TODO)
229 staticinlineintimm_proc_write(int hostno,char*buffer,int length)
231 unsigned long x;
233 if((length >5) && (strncmp(buffer,"mode=",5) ==0)) {
234 x =simple_strtoul(buffer +5, NULL,0);
235 imm_hosts[hostno].mode = x;
236 return length;
238 printk("imm /proc: invalid variable\n");
239 return(-EINVAL);
242 intimm_proc_info(char*buffer,char**start, off_t offset,
243 int length,int hostno,int inout)
245 int i;
246 int len =0;
248 for(i =0; i <4; i++)
249 if(imm_hosts[i].host == hostno)
250 break;
252 if(inout)
253 returnimm_proc_write(i, buffer, length);
255 len +=sprintf(buffer + len,"Version : %s\n", IMM_VERSION);
256 len +=sprintf(buffer + len,"Parport : %s\n", imm_hosts[i].dev->port->name);
257 len +=sprintf(buffer + len,"Mode : %s\n", IMM_MODE_STRING[imm_hosts[i].mode]);
259 /* Request for beyond end of buffer */
260 if(offset > len)
261 return0;
263 *start = buffer + offset;
264 len -= offset;
265 if(len > length)
266 len = length;
267 return len;
270 #if IMM_DEBUG > 0
271 #define imm_fail(x,y) printk("imm: imm_fail(%i) from %s at line %d\n",\
272 y, __FUNCTION__, __LINE__); imm_fail_func(x,y);
273 staticinlinevoidimm_fail_func(int host_no,int error_code)
274 #else
275 staticinlinevoidimm_fail(int host_no,int error_code)
276 #endif
278 /* If we fail a device then we trash status / message bytes */
279 if(imm_hosts[host_no].cur_cmd) {
280 imm_hosts[host_no].cur_cmd->result = error_code <<16;
281 imm_hosts[host_no].failed =1;
286 * Wait for the high bit to be set.
288 * In principle, this could be tied to an interrupt, but the adapter
289 * doesn't appear to be designed to support interrupts. We spin on
290 * the 0x80 ready bit.
292 static unsigned charimm_wait(int host_no)
294 int k;
295 unsigned short ppb =IMM_BASE(host_no);
296 unsigned char r;
298 w_ctr(ppb,0x0c);
300 k = IMM_SPIN_TMO;
302 r =r_str(ppb);
303 k--;
304 udelay(1);
306 while(!(r &0x80) && (k));
309 * STR register (LPT base+1) to SCSI mapping:
311 * STR imm imm
312 * ===================================
313 * 0x80 S_REQ S_REQ
314 * 0x40 !S_BSY (????)
315 * 0x20 !S_CD !S_CD
316 * 0x10 !S_IO !S_IO
317 * 0x08 (????) !S_BSY
319 * imm imm meaning
320 * ==================================
321 * 0xf0 0xb8 Bit mask
322 * 0xc0 0x88 ZIP wants more data
323 * 0xd0 0x98 ZIP wants to send more data
324 * 0xe0 0xa8 ZIP is expecting SCSI command data
325 * 0xf0 0xb8 end of transfer, ZIP is sending status
327 w_ctr(ppb,0x04);
328 if(k)
329 return(r &0xb8);
331 /* Counter expired - Time out occurred */
332 imm_fail(host_no, DID_TIME_OUT);
333 printk("imm timeout in imm_wait\n");
334 return0;/* command timed out */
337 static intimm_negotiate(imm_struct * tmp)
340 * The following is supposedly the IEEE 1248-1994 negotiate
341 * sequence. I have yet to obtain a copy of the above standard
342 * so this is a bit of a guess...
344 * A fair chunk of this is based on the Linux parport implementation
345 * of IEEE 1284.
347 * Return 0 if data available
348 * 1 if no data available
351 unsigned short base = tmp->base;
352 unsigned char a, mode;
354 switch(tmp->mode) {
355 case IMM_NIBBLE:
356 mode =0x00;
357 break;
358 case IMM_PS2:
359 mode =0x01;
360 break;
361 default:
362 return0;
365 w_ctr(base,0x04);
366 udelay(5);
367 w_dtr(base, mode);
368 udelay(100);
369 w_ctr(base,0x06);
370 udelay(5);
371 a = (r_str(base) &0x20) ?0:1;
372 udelay(5);
373 w_ctr(base,0x07);
374 udelay(5);
375 w_ctr(base,0x06);
377 if(a) {
378 printk("IMM: IEEE1284 negotiate indicates no data available.\n");
379 imm_fail(tmp->host, DID_ERROR);
381 return a;
384 staticinlinevoidepp_reset(unsigned short ppb)
386 int i;
388 i =r_str(ppb);
389 w_str(ppb, i);
390 w_str(ppb, i &0xfe);
393 staticinlinevoidecp_sync(unsigned short ppb)
395 int i;
397 if((r_ecr(ppb) &0xe0) !=0x80)
398 return;
400 for(i =0; i <100; i++) {
401 if(r_ecr(ppb) &0x01)
402 return;
403 udelay(5);
405 printk("imm: ECP sync failed as data still present in FIFO.\n");
408 static intimm_byte_out(unsigned short base,const char*buffer,int len)
410 int i;
412 w_ctr(base,0x4);/* apparently a sane mode */
413 for(i = len >>1; i; i--) {
414 w_dtr(base, *buffer++);
415 w_ctr(base,0x5);/* Drop STROBE low */
416 w_dtr(base, *buffer++);
417 w_ctr(base,0x0);/* STROBE high + INIT low */
419 w_ctr(base,0x4);/* apparently a sane mode */
420 return1;/* All went well - we hope! */
423 static intimm_nibble_in(unsigned short base,char*buffer,int len)
425 unsigned char l;
426 int i;
429 * The following is based on documented timing signals
431 w_ctr(base,0x4);
432 for(i = len; i; i--) {
433 w_ctr(base,0x6);
434 l = (r_str(base) &0xf0) >>4;
435 w_ctr(base,0x5);
436 *buffer++ = (r_str(base) &0xf0) | l;
437 w_ctr(base,0x4);
439 return1;/* All went well - we hope! */
442 static intimm_byte_in(unsigned short base,char*buffer,int len)
444 int i;
447 * The following is based on documented timing signals
449 w_ctr(base,0x4);
450 for(i = len; i; i--) {
451 w_ctr(base,0x26);
452 *buffer++ =r_dtr(base);
453 w_ctr(base,0x25);
455 return1;/* All went well - we hope! */
458 static intimm_out(int host_no,char*buffer,int len)
460 int r;
461 unsigned short ppb =IMM_BASE(host_no);
463 r =imm_wait(host_no);
466 * Make sure that:
467 * a) the SCSI bus is BUSY (device still listening)
468 * b) the device is listening
470 if((r &0x18) !=0x08) {
471 imm_fail(host_no, DID_ERROR);
472 printk("IMM: returned SCSI status %2x\n", r);
473 return0;
475 switch(imm_hosts[host_no].mode) {
476 case IMM_EPP_32:
477 case IMM_EPP_16:
478 case IMM_EPP_8:
479 epp_reset(ppb);
480 w_ctr(ppb,0x4);
481 #ifdef CONFIG_SCSI_IZIP_EPP16
482 if(!(((long) buffer | len) &0x01))
483 outsw(ppb +4, buffer, len >>1);
484 #else
485 if(!(((long) buffer | len) &0x03))
486 outsl(ppb +4, buffer, len >>2);
487 #endif
488 else
489 outsb(ppb +4, buffer, len);
490 w_ctr(ppb,0xc);
491 r = !(r_str(ppb) &0x01);
492 w_ctr(ppb,0xc);
493 ecp_sync(ppb);
494 break;
496 case IMM_NIBBLE:
497 case IMM_PS2:
498 /* 8 bit output, with a loop */
499 r =imm_byte_out(ppb, buffer, len);
500 break;
502 default:
503 printk("IMM: bug in imm_out()\n");
504 r =0;
506 return r;
509 static intimm_in(int host_no,char*buffer,int len)
511 int r;
512 unsigned short ppb =IMM_BASE(host_no);
514 r =imm_wait(host_no);
517 * Make sure that:
518 * a) the SCSI bus is BUSY (device still listening)
519 * b) the device is sending data
521 if((r &0x18) !=0x18) {
522 imm_fail(host_no, DID_ERROR);
523 return0;
525 switch(imm_hosts[host_no].mode) {
526 case IMM_NIBBLE:
527 /* 4 bit input, with a loop */
528 r =imm_nibble_in(ppb, buffer, len);
529 w_ctr(ppb,0xc);
530 break;
532 case IMM_PS2:
533 /* 8 bit input, with a loop */
534 r =imm_byte_in(ppb, buffer, len);
535 w_ctr(ppb,0xc);
536 break;
538 case IMM_EPP_32:
539 case IMM_EPP_16:
540 case IMM_EPP_8:
541 epp_reset(ppb);
542 w_ctr(ppb,0x24);
543 #ifdef CONFIG_SCSI_IZIP_EPP16
544 if(!(((long) buffer | len) &0x01))
545 insw(ppb +4, buffer, len >>1);
546 #else
547 if(!(((long) buffer | len) &0x03))
548 insl(ppb +4, buffer, len >>2);
549 #endif
550 else
551 insb(ppb +4, buffer, len);
552 w_ctr(ppb,0x2c);
553 r = !(r_str(ppb) &0x01);
554 w_ctr(ppb,0x2c);
555 ecp_sync(ppb);
556 break;
558 default:
559 printk("IMM: bug in imm_ins()\n");
560 r =0;
561 break;
563 return r;
566 static intimm_cpp(unsigned short ppb,unsigned char b)
569 * Comments on udelay values refer to the
570 * Command Packet Protocol (CPP) timing diagram.
573 unsigned char s1, s2, s3;
574 w_ctr(ppb,0x0c);
575 udelay(2);/* 1 usec - infinite */
576 w_dtr(ppb,0xaa);
577 udelay(10);/* 7 usec - infinite */
578 w_dtr(ppb,0x55);
579 udelay(10);/* 7 usec - infinite */
580 w_dtr(ppb,0x00);
581 udelay(10);/* 7 usec - infinite */
582 w_dtr(ppb,0xff);
583 udelay(10);/* 7 usec - infinite */
584 s1 =r_str(ppb) &0xb8;
585 w_dtr(ppb,0x87);
586 udelay(10);/* 7 usec - infinite */
587 s2 =r_str(ppb) &0xb8;
588 w_dtr(ppb,0x78);
589 udelay(10);/* 7 usec - infinite */
590 s3 =r_str(ppb) &0x38;
592 * Values for b are:
593 * 0000 00aa Assign address aa to current device
594 * 0010 00aa Select device aa in EPP Winbond mode
595 * 0010 10aa Select device aa in EPP mode
596 * 0011 xxxx Deselect all devices
597 * 0110 00aa Test device aa
598 * 1101 00aa Select device aa in ECP mode
599 * 1110 00aa Select device aa in Compatible mode
601 w_dtr(ppb, b);
602 udelay(2);/* 1 usec - infinite */
603 w_ctr(ppb,0x0c);
604 udelay(10);/* 7 usec - infinite */
605 w_ctr(ppb,0x0d);
606 udelay(2);/* 1 usec - infinite */
607 w_ctr(ppb,0x0c);
608 udelay(10);/* 7 usec - infinite */
609 w_dtr(ppb,0xff);
610 udelay(10);/* 7 usec - infinite */
613 * The following table is electrical pin values.
614 * (BSY is inverted at the CTR register)
616 * BSY ACK POut SEL Fault
617 * S1 0 X 1 1 1
618 * S2 1 X 0 1 1
619 * S3 L X 1 1 S
621 * L => Last device in chain
622 * S => Selected
624 * Observered values for S1,S2,S3 are:
625 * Disconnect => f8/58/78
626 * Connect => f8/58/70
628 if((s1 ==0xb8) && (s2 ==0x18) && (s3 ==0x30))
629 return1;/* Connected */
630 if((s1 ==0xb8) && (s2 ==0x18) && (s3 ==0x38))
631 return0;/* Disconnected */
633 return-1;/* No device present */
636 staticinlineintimm_connect(int host_no,int flag)
638 unsigned short ppb =IMM_BASE(host_no);
640 imm_cpp(ppb,0xe0);/* Select device 0 in compatible mode */
641 imm_cpp(ppb,0x30);/* Disconnect all devices */
643 if((imm_hosts[host_no].mode == IMM_EPP_8) ||
644 (imm_hosts[host_no].mode == IMM_EPP_16) ||
645 (imm_hosts[host_no].mode == IMM_EPP_32))
646 returnimm_cpp(ppb,0x28);/* Select device 0 in EPP mode */
647 returnimm_cpp(ppb,0xe0);/* Select device 0 in compatible mode */
650 static voidimm_disconnect(int host_no)
652 unsigned short ppb =IMM_BASE(host_no);
654 imm_cpp(ppb,0x30);/* Disconnect all devices */
657 static intimm_select(int host_no,int target)
659 int k;
660 unsigned short ppb =IMM_BASE(host_no);
663 * Firstly we want to make sure there is nothing
664 * holding onto the SCSI bus.
666 w_ctr(ppb,0xc);
668 k = IMM_SELECT_TMO;
670 k--;
671 }while((r_str(ppb) &0x08) && (k));
673 if(!k)
674 return0;
677 * Now assert the SCSI ID (HOST and TARGET) on the data bus
679 w_ctr(ppb,0x4);
680 w_dtr(ppb,0x80| (1<< target));
681 udelay(1);
684 * Deassert SELIN first followed by STROBE
686 w_ctr(ppb,0xc);
687 w_ctr(ppb,0xd);
690 * ACK should drop low while SELIN is deasserted.
691 * FAULT should drop low when the SCSI device latches the bus.
693 k = IMM_SELECT_TMO;
695 k--;
697 while(!(r_str(ppb) &0x08) && (k));
700 * Place the interface back into a sane state (status mode)
702 w_ctr(ppb,0xc);
703 return(k) ?1:0;
706 static intimm_init(int host_no)
708 int retv;
710 #if defined(CONFIG_PARPORT) || defined(CONFIG_PARPORT_MODULE)
711 if(imm_pb_claim(host_no))
712 while(imm_hosts[host_no].p_busy)
713 schedule();/* We can safe schedule here */
714 #endif
715 retv =imm_connect(host_no,0);
717 if(retv ==1) {
718 imm_reset_pulse(IMM_BASE(host_no));
719 udelay(1000);/* Delay to allow devices to settle */
720 imm_disconnect(host_no);
721 udelay(1000);/* Another delay to allow devices to settle */
722 retv =device_check(host_no);
723 imm_pb_release(host_no);
724 return retv;
726 imm_pb_release(host_no);
727 return1;
730 staticinlineintimm_send_command(Scsi_Cmnd * cmd)
732 int host_no = cmd->host->unique_id;
733 int k;
735 /* NOTE: IMM uses byte pairs */
736 for(k =0; k < cmd->cmd_len; k +=2)
737 if(!imm_out(host_no, &cmd->cmnd[k],2))
738 return0;
739 return1;
743 * The bulk flag enables some optimisations in the data transfer loops,
744 * it should be true for any command that transfers data in integral
745 * numbers of sectors.
747 * The driver appears to remain stable if we speed up the parallel port
748 * i/o in this function, but not elsewhere.
750 static intimm_completion(Scsi_Cmnd * cmd)
752 /* Return codes:
753 * -1 Error
754 * 0 Told to schedule
755 * 1 Finished data transfer
757 int host_no = cmd->host->unique_id;
758 unsigned short ppb =IMM_BASE(host_no);
759 unsigned long start_jiffies = jiffies;
761 unsigned char r, v;
762 int fast, bulk, status;
764 v = cmd->cmnd[0];
765 bulk = ((v == READ_6) ||
766 (v == READ_10) ||
767 (v == WRITE_6) ||
768 (v == WRITE_10));
771 * We only get here if the drive is ready to comunicate,
772 * hence no need for a full imm_wait.
774 w_ctr(ppb,0x0c);
775 r = (r_str(ppb) &0xb8);
778 * while (device is not ready to send status byte)
779 * loop;
781 while(r != (unsigned char)0xb8) {
783 * If we have been running for more than a full timer tick
784 * then take a rest.
786 if(time_after(jiffies, start_jiffies +1))
787 return0;
790 * FAIL if:
791 * a) Drive status is screwy (!ready && !present)
792 * b) Drive is requesting/sending more data than expected
794 if(((r &0x88) !=0x88) || (cmd->SCp.this_residual <=0)) {
795 imm_fail(host_no, DID_ERROR);
796 return-1;/* ERROR_RETURN */
798 /* determine if we should use burst I/O */
799 if(imm_hosts[host_no].rd ==0) {
800 fast = (bulk && (cmd->SCp.this_residual >= IMM_BURST_SIZE)) ? IMM_BURST_SIZE :2;
801 status =imm_out(host_no, cmd->SCp.ptr, fast);
802 }else{
803 fast = (bulk && (cmd->SCp.this_residual >= IMM_BURST_SIZE)) ? IMM_BURST_SIZE :1;
804 status =imm_in(host_no, cmd->SCp.ptr, fast);
807 cmd->SCp.ptr += fast;
808 cmd->SCp.this_residual -= fast;
810 if(!status) {
811 imm_fail(host_no, DID_BUS_BUSY);
812 return-1;/* ERROR_RETURN */
814 if(cmd->SCp.buffer && !cmd->SCp.this_residual) {
815 /* if scatter/gather, advance to the next segment */
816 if(cmd->SCp.buffers_residual--) {
817 cmd->SCp.buffer++;
818 cmd->SCp.this_residual = cmd->SCp.buffer->length;
819 cmd->SCp.ptr = cmd->SCp.buffer->address;
822 * Make sure that we transfer even number of bytes
823 * otherwise it makes imm_byte_out() messy.
825 if(cmd->SCp.this_residual &0x01) {
826 cmd->SCp.this_residual++;
827 printk("IMM: adjusted buffer for 16 bit transfer\n");
831 /* Now check to see if the drive is ready to comunicate */
832 w_ctr(ppb,0x0c);
833 r = (r_str(ppb) &0xb8);
835 /* If not, drop back down to the scheduler and wait a timer tick */
836 if(!(r &0x80))
837 return0;
839 return1;/* FINISH_RETURN */
842 /* deprecated synchronous interface */
843 intimm_command(Scsi_Cmnd * cmd)
845 static int first_pass =1;
846 int host_no = cmd->host->unique_id;
848 if(first_pass) {
849 printk("imm: using non-queuing interface\n");
850 first_pass =0;
852 if(imm_hosts[host_no].cur_cmd) {
853 printk("IMM: bug in imm_command\n");
854 return0;
856 imm_hosts[host_no].failed =0;
857 imm_hosts[host_no].jstart = jiffies;
858 imm_hosts[host_no].cur_cmd = cmd;
859 cmd->result = DID_ERROR <<16;/* default return code */
860 cmd->SCp.phase =0;
862 imm_pb_claim(host_no);
864 while(imm_engine(&imm_hosts[host_no], cmd))
865 schedule();
867 if(cmd->SCp.phase)/* Only disconnect if we have connected */
868 imm_disconnect(cmd->host->unique_id);
870 imm_pb_release(host_no);
871 imm_hosts[host_no].cur_cmd =0;
872 return cmd->result;
876 * Since the IMM itself doesn't generate interrupts, we use
877 * the scheduler's task queue to generate a stream of call-backs and
878 * complete the request when the drive is ready.
880 static voidimm_interrupt(void*data)
882 imm_struct *tmp = (imm_struct *) data;
883 Scsi_Cmnd *cmd = tmp->cur_cmd;
884 unsigned long flags;
886 if(!cmd) {
887 printk("IMM: bug in imm_interrupt\n");
888 return;
890 if(imm_engine(tmp, cmd)) {
891 tmp->imm_tq.data = (void*) tmp;
892 tmp->imm_tq.sync =0;
893 queue_task(&tmp->imm_tq, &tq_timer);
894 return;
896 /* Command must of completed hence it is safe to let go... */
897 #if IMM_DEBUG > 0
898 switch((cmd->result >>16) &0xff) {
899 case DID_OK:
900 break;
901 case DID_NO_CONNECT:
902 printk("imm: no device at SCSI ID %i\n", cmd->target);
903 break;
904 case DID_BUS_BUSY:
905 printk("imm: BUS BUSY - EPP timeout detected\n");
906 break;
907 case DID_TIME_OUT:
908 printk("imm: unknown timeout\n");
909 break;
910 case DID_ABORT:
911 printk("imm: told to abort\n");
912 break;
913 case DID_PARITY:
914 printk("imm: parity error (???)\n");
915 break;
916 case DID_ERROR:
917 printk("imm: internal driver error\n");
918 break;
919 case DID_RESET:
920 printk("imm: told to reset device\n");
921 break;
922 case DID_BAD_INTR:
923 printk("imm: bad interrupt (???)\n");
924 break;
925 default:
926 printk("imm: bad return code (%02x)\n", (cmd->result >>16) &0xff);
928 #endif
930 if(cmd->SCp.phase >1)
931 imm_disconnect(cmd->host->unique_id);
932 if(cmd->SCp.phase >0)
933 imm_pb_release(cmd->host->unique_id);
935 spin_lock_irqsave(&io_request_lock, flags);
936 tmp->cur_cmd =0;
937 cmd->scsi_done(cmd);
938 spin_unlock_irqrestore(&io_request_lock, flags);
939 return;
942 static intimm_engine(imm_struct * tmp, Scsi_Cmnd * cmd)
944 int host_no = cmd->host->unique_id;
945 unsigned short ppb =IMM_BASE(host_no);
946 unsigned char l =0, h =0;
947 int retv, x;
949 /* First check for any errors that may of occurred
950 * Here we check for internal errors
952 if(tmp->failed)
953 return0;
955 switch(cmd->SCp.phase) {
956 case0:/* Phase 0 - Waiting for parport */
957 if((jiffies - tmp->jstart) > HZ) {
959 * We waited more than a second
960 * for parport to call us
962 imm_fail(host_no, DID_BUS_BUSY);
963 return0;
965 return1;/* wait until imm_wakeup claims parport */
966 /* Phase 1 - Connected */
967 case1:
968 imm_connect(host_no, CONNECT_EPP_MAYBE);
969 cmd->SCp.phase++;
971 /* Phase 2 - We are now talking to the scsi bus */
972 case2:
973 if(!imm_select(host_no, cmd->target)) {
974 imm_fail(host_no, DID_NO_CONNECT);
975 return0;
977 cmd->SCp.phase++;
979 /* Phase 3 - Ready to accept a command */
980 case3:
981 w_ctr(ppb,0x0c);
982 if(!(r_str(ppb) &0x80))
983 return1;
985 if(!imm_send_command(cmd))
986 return0;
987 cmd->SCp.phase++;
989 /* Phase 4 - Setup scatter/gather buffers */
990 case4:
991 if(cmd->use_sg) {
992 /* if many buffers are available, start filling the first */
993 cmd->SCp.buffer = (struct scatterlist *) cmd->request_buffer;
994 cmd->SCp.this_residual = cmd->SCp.buffer->length;
995 cmd->SCp.ptr = cmd->SCp.buffer->address;
996 }else{
997 /* else fill the only available buffer */
998 cmd->SCp.buffer = NULL;
999 cmd->SCp.this_residual = cmd->request_bufflen;
1000 cmd->SCp.ptr = cmd->request_buffer;
1002 cmd->SCp.buffers_residual = cmd->use_sg;
1003 cmd->SCp.phase++;
1004 if(cmd->SCp.this_residual &0x01) {
1005 cmd->SCp.this_residual++;
1006 printk("IMM: adjusted buffer for 16 bit transfer\n");
1008 /* Phase 5 - Pre-Data transfer stage */
1009 case5:
1010 /* Spin lock for BUSY */
1011 w_ctr(ppb,0x0c);
1012 if(!(r_str(ppb) &0x80))
1013 return1;
1015 /* Require negotiation for read requests */
1016 x = (r_str(ppb) &0xb8);
1017 tmp->rd = (x &0x10) ?1:0;
1018 tmp->dp = (x &0x20) ?0:1;
1020 if((tmp->dp) && (tmp->rd))
1021 if(imm_negotiate(tmp))
1022 return0;
1023 cmd->SCp.phase++;
1025 /* Phase 6 - Data transfer stage */
1026 case6:
1027 /* Spin lock for BUSY */
1028 w_ctr(ppb,0x0c);
1029 if(!(r_str(ppb) &0x80))
1030 return1;
1032 if(tmp->dp) {
1033 retv =imm_completion(cmd);
1034 if(retv == -1)
1035 return0;
1036 if(retv ==0)
1037 return1;
1039 cmd->SCp.phase++;
1041 /* Phase 7 - Post data transfer stage */
1042 case7:
1043 if((tmp->dp) && (tmp->rd)) {
1044 if((tmp->mode == IMM_NIBBLE) || (tmp->mode == IMM_PS2)) {
1045 w_ctr(ppb,0x4);
1046 w_ctr(ppb,0xc);
1047 w_ctr(ppb,0xe);
1048 w_ctr(ppb,0x4);
1051 cmd->SCp.phase++;
1053 /* Phase 8 - Read status/message */
1054 case8:
1055 /* Check for data overrun */
1056 if(imm_wait(host_no) != (unsigned char)0xb8) {
1057 imm_fail(host_no, DID_ERROR);
1058 return0;
1060 if(imm_negotiate(tmp))
1061 return0;
1062 if(imm_in(host_no, &l,1)) {/* read status byte */
1063 /* Check for optional message byte */
1064 if(imm_wait(host_no) == (unsigned char)0xb8)
1065 imm_in(host_no, &h,1);
1066 cmd->result = (DID_OK <<16) + (l & STATUS_MASK);
1068 if((tmp->mode == IMM_NIBBLE) || (tmp->mode == IMM_PS2)) {
1069 w_ctr(ppb,0x4);
1070 w_ctr(ppb,0xc);
1071 w_ctr(ppb,0xe);
1072 w_ctr(ppb,0x4);
1074 return0;/* Finished */
1075 break;
1077 default:
1078 printk("imm: Invalid scsi phase\n");
1080 return0;
1083 intimm_queuecommand(Scsi_Cmnd * cmd,void(*done) (Scsi_Cmnd *))
1085 int host_no = cmd->host->unique_id;
1087 if(imm_hosts[host_no].cur_cmd) {
1088 printk("IMM: bug in imm_queuecommand\n");
1089 return0;
1091 imm_hosts[host_no].failed =0;
1092 imm_hosts[host_no].jstart = jiffies;
1093 imm_hosts[host_no].cur_cmd = cmd;
1094 cmd->scsi_done = done;
1095 cmd->result = DID_ERROR <<16;/* default return code */
1096 cmd->SCp.phase =0;/* bus free */
1098 imm_pb_claim(host_no);
1100 imm_hosts[host_no].imm_tq.data = imm_hosts + host_no;
1101 imm_hosts[host_no].imm_tq.sync =0;
1102 queue_task(&imm_hosts[host_no].imm_tq, &tq_immediate);
1103 mark_bh(IMMEDIATE_BH);
1105 return0;
1109 * Aimmrently the the disk->capacity attribute is off by 1 sector
1110 * for all disk drives. We add the one here, but it should really
1111 * be done in sd.c. Even if it gets fixed there, this will still
1112 * work.
1114 intimm_biosparam(Disk * disk, kdev_t dev,int ip[])
1116 ip[0] =0x40;
1117 ip[1] =0x20;
1118 ip[2] = (disk->capacity +1) / (ip[0] * ip[1]);
1119 if(ip[2] >1024) {
1120 ip[0] =0xff;
1121 ip[1] =0x3f;
1122 ip[2] = (disk->capacity +1) / (ip[0] * ip[1]);
1123 if(ip[2] >1023)
1124 ip[2] =1023;
1126 return0;
1129 intimm_abort(Scsi_Cmnd * cmd)
1131 int host_no = cmd->host->unique_id;
1133 * There is no method for aborting commands since Iomega
1134 * have tied the SCSI_MESSAGE line high in the interface
1137 switch(cmd->SCp.phase) {
1138 case0:/* Do not have access to parport */
1139 case1:/* Have not connected to interface */
1140 imm_hosts[host_no].cur_cmd = NULL;/* Forget the problem */
1141 return SUCCESS;
1142 break;
1143 default:/* SCSI command sent, can not abort */
1144 return FAILED;
1145 break;
1149 voidimm_reset_pulse(unsigned int base)
1151 w_ctr(base,0x04);
1152 w_dtr(base,0x40);
1153 udelay(1);
1154 w_ctr(base,0x0c);
1155 w_ctr(base,0x0d);
1156 udelay(50);
1157 w_ctr(base,0x0c);
1158 w_ctr(base,0x04);
1161 intimm_reset(Scsi_Cmnd * cmd)
1163 int host_no = cmd->host->unique_id;
1165 if(cmd->SCp.phase)
1166 imm_disconnect(host_no);
1167 imm_hosts[host_no].cur_cmd = NULL;/* Forget the problem */
1169 imm_connect(host_no, CONNECT_NORMAL);
1170 imm_reset_pulse(IMM_BASE(host_no));
1171 udelay(1000);/* device settle delay */
1172 imm_disconnect(host_no);
1173 udelay(1000);/* device settle delay */
1174 return SUCCESS;
1177 static intdevice_check(int host_no)
1179 /* This routine looks for a device and then attempts to use EPP
1180 to send a command. If all goes as planned then EPP is available. */
1182 static char cmd[6] =
1183 {0x00,0x00,0x00,0x00,0x00,0x00};
1184 int loop, old_mode, status, k, ppb =IMM_BASE(host_no);
1185 unsigned char l;
1187 old_mode = imm_hosts[host_no].mode;
1188 for(loop =0; loop <8; loop++) {
1189 /* Attempt to use EPP for Test Unit Ready */
1190 if((ppb &0x0007) ==0x0000)
1191 imm_hosts[host_no].mode = IMM_EPP_32;
1193 second_pass:
1194 imm_connect(host_no, CONNECT_EPP_MAYBE);
1195 /* Select SCSI device */
1196 if(!imm_select(host_no, loop)) {
1197 imm_disconnect(host_no);
1198 continue;
1200 printk("imm: Found device at ID %i, Attempting to use %s\n", loop,
1201 IMM_MODE_STRING[imm_hosts[host_no].mode]);
1203 /* Send SCSI command */
1204 status =1;
1205 w_ctr(ppb,0x0c);
1206 for(l =0; (l <3) && (status); l++)
1207 status =imm_out(host_no, &cmd[l <<1],2);
1209 if(!status) {
1210 imm_disconnect(host_no);
1211 imm_connect(host_no, CONNECT_EPP_MAYBE);
1212 imm_reset_pulse(IMM_BASE(host_no));
1213 udelay(1000);
1214 imm_disconnect(host_no);
1215 udelay(1000);
1216 if(imm_hosts[host_no].mode == IMM_EPP_32) {
1217 imm_hosts[host_no].mode = old_mode;
1218 goto second_pass;
1220 printk("imm: Unable to establish communication, aborting driver load.\n");
1221 return1;
1223 w_ctr(ppb,0x0c);
1225 k =1000000;/* 1 Second */
1227 l =r_str(ppb);
1228 k--;
1229 udelay(1);
1230 }while(!(l &0x80) && (k));
1232 l &=0xb8;
1234 if(l !=0xb8) {
1235 imm_disconnect(host_no);
1236 imm_connect(host_no, CONNECT_EPP_MAYBE);
1237 imm_reset_pulse(IMM_BASE(host_no));
1238 udelay(1000);
1239 imm_disconnect(host_no);
1240 udelay(1000);
1241 if(imm_hosts[host_no].mode == IMM_EPP_32) {
1242 imm_hosts[host_no].mode = old_mode;
1243 goto second_pass;
1245 printk("imm: Unable to establish communication, aborting driver load.\n");
1246 return1;
1248 imm_disconnect(host_no);
1249 printk("imm: Communication established with ID %i using %s\n", loop,
1250 IMM_MODE_STRING[imm_hosts[host_no].mode]);
1251 imm_connect(host_no, CONNECT_EPP_MAYBE);
1252 imm_reset_pulse(IMM_BASE(host_no));
1253 udelay(1000);
1254 imm_disconnect(host_no);
1255 udelay(1000);
1256 return0;
1258 printk("imm: No devices found, aborting driver load.\n");
1259 return1;
close