1 /* ppa.c   --  low level driver for the IOMEGA PPA3
2  * parallel port SCSI host adapter.
3  *
4  * (The PPA3 is the embedded controller in the ZIP drive.)
5  *
6  * (c) 1995,1996 Grant R. Guenther, grant@torque.net,
7  * under the terms of the GNU General Public License.
8  *
9  * Current Maintainer: David Campbell (Perth, Western Australia, GMT+0800)
10  *                     campbell@torque.net
11  */
12 
13 #include <linux/config.h>
14 
15 /* The following #define is to avoid a clash with hosts.c */
16 #define PPA_CODE 1
17 
18 #include <linux/blk.h>
19 #include <asm/io.h>
20 #include <linux/parport.h>
21 #include "sd.h"
22 #include "hosts.h"
23 int ppa_release(struct Scsi_Host *);
24 static void ppa_reset_pulse(unsigned int base);
25 
26 typedef struct {
27     struct pardevice *dev;	/* Parport device entry         */
28     int base;			/* Actual port address          */
29     int mode;			/* Transfer mode                */
30     int host;			/* Host number (for proc)       */
31     Scsi_Cmnd *cur_cmd;		/* Current queued command       */
32     struct tq_struct ppa_tq;	/* Polling interrupt stuff       */
33     unsigned long jstart;	/* Jiffies at start             */
34     unsigned long recon_tmo;    /* How many usecs to wait for reconnection (6th bit) */
35     unsigned int failed:1;	/* Failure flag                 */
36     unsigned int p_busy:1;	/* Parport sharing busy flag    */
37 } ppa_struct;
38 
39 #define PPA_EMPTY	\
40 {	dev:		NULL,		\
41 	base:		-1,		\
42 	mode:		PPA_AUTODETECT,	\
43 	host:		-1,		\
44 	cur_cmd:	NULL,		\
45 	ppa_tq:		{ routine: ppa_interrupt },	\
46 	jstart:		0,		\
47 	recon_tmo:      PPA_RECON_TMO,	\
48 	failed:		0,		\
49 	p_busy:		0		\
50 }
51 
52 #include  "ppa.h"
53 
54 #define NO_HOSTS 4
55 static ppa_struct ppa_hosts[NO_HOSTS] =
56 {PPA_EMPTY, PPA_EMPTY, PPA_EMPTY, PPA_EMPTY};
57 
58 #define PPA_BASE(x)	ppa_hosts[(x)].base
59 
ppa_wakeup(void * ref)60 void ppa_wakeup(void *ref)
61 {
62     ppa_struct *ppa_dev = (ppa_struct *) ref;
63 
64     if (!ppa_dev->p_busy)
65 	return;
66 
67     if (parport_claim(ppa_dev->dev)) {
68 	printk("ppa: bug in ppa_wakeup\n");
69 	return;
70     }
71     ppa_dev->p_busy = 0;
72     ppa_dev->base = ppa_dev->dev->port->base;
73     if (ppa_dev->cur_cmd)
74 	ppa_dev->cur_cmd->SCp.phase++;
75     return;
76 }
77 
ppa_release(struct Scsi_Host * host)78 int ppa_release(struct Scsi_Host *host)
79 {
80     int host_no = host->unique_id;
81 
82     printk("Releasing ppa%i\n", host_no);
83     parport_unregister_device(ppa_hosts[host_no].dev);
84     return 0;
85 }
86 
ppa_pb_claim(int host_no)87 static int ppa_pb_claim(int host_no)
88 {
89     if (parport_claim(ppa_hosts[host_no].dev)) {
90 	ppa_hosts[host_no].p_busy = 1;
91 	return 1;
92     }
93     if (ppa_hosts[host_no].cur_cmd)
94 	ppa_hosts[host_no].cur_cmd->SCp.phase++;
95     return 0;
96 }
97 
98 #define ppa_pb_release(x) parport_release(ppa_hosts[(x)].dev)
99 
100 /***************************************************************************
101  *                   Parallel port probing routines                        *
102  ***************************************************************************/
103 
104 static Scsi_Host_Template driver_template = PPA;
105 #include  "scsi_module.c"
106 
107 /*
108  * Start of Chipset kludges
109  */
110 
ppa_detect(Scsi_Host_Template * host)111 int ppa_detect(Scsi_Host_Template * host)
112 {
113     struct Scsi_Host *hreg;
114     int ports;
115     int i, nhosts, try_again;
116     struct parport *pb;
117 
118     /*
119      * unlock to allow the lowlevel parport driver to probe
120      * the irqs
121      */
122     spin_unlock_irq(&io_request_lock);
123     pb = parport_enumerate();
124 
125     printk("ppa: Version %s\n", PPA_VERSION);
126     nhosts = 0;
127     try_again = 0;
128 
129     if (!pb) {
130 	printk("ppa: parport reports no devices.\n");
131 	spin_lock_irq(&io_request_lock);
132 	return 0;
133     }
134   retry_entry:
135     for (i = 0; pb; i++, pb = pb->next) {
136 	int modes, ppb, ppb_hi;
137 
138 	ppa_hosts[i].dev =
139 	    parport_register_device(pb, "ppa", NULL, ppa_wakeup,
140 				    NULL, 0, (void *) &ppa_hosts[i]);
141 
142 	if (!ppa_hosts[i].dev)
143 	    continue;
144 
145 	/* Claim the bus so it remembers what we do to the control
146 	 * registers. [ CTR and ECP ]
147 	 */
148 	if (ppa_pb_claim(i)) {
149 	    unsigned long now = jiffies;
150 	    while (ppa_hosts[i].p_busy) {
151 		schedule();	/* We are safe to schedule here */
152 		if (time_after(jiffies, now + 3 * HZ)) {
153 		    printk(KERN_ERR "ppa%d: failed to claim parport because a "
154 		      "pardevice is owning the port for too longtime!\n",
155 			   i);
156 		    parport_unregister_device(ppa_hosts[i].dev);
157 		    spin_lock_irq(&io_request_lock);
158 		    return 0;
159 		}
160 	    }
161 	}
162 	ppb = PPA_BASE(i) = ppa_hosts[i].dev->port->base;
163 	ppb_hi =  ppa_hosts[i].dev->port->base_hi;
164 	w_ctr(ppb, 0x0c);
165 	modes = ppa_hosts[i].dev->port->modes;
166 
167 	/* Mode detection works up the chain of speed
168 	 * This avoids a nasty if-then-else-if-... tree
169 	 */
170 	ppa_hosts[i].mode = PPA_NIBBLE;
171 
172 	if (modes & PARPORT_MODE_TRISTATE)
173 	    ppa_hosts[i].mode = PPA_PS2;
174 
175 	if (modes & PARPORT_MODE_ECP) {
176 	    w_ecr(ppb_hi, 0x20);
177 	    ppa_hosts[i].mode = PPA_PS2;
178 	}
179 	if ((modes & PARPORT_MODE_EPP) && (modes & PARPORT_MODE_ECP))
180 	    w_ecr(ppb_hi, 0x80);
181 
182 	/* Done configuration */
183 	ppa_pb_release(i);
184 
185 	if (ppa_init(i)) {
186 	    parport_unregister_device(ppa_hosts[i].dev);
187 	    continue;
188 	}
189 	/* now the glue ... */
190 	switch (ppa_hosts[i].mode) {
191 	case PPA_NIBBLE:
192 	    ports = 3;
193 	    break;
194 	case PPA_PS2:
195 	    ports = 3;
196 	    break;
197 	case PPA_EPP_8:
198 	case PPA_EPP_16:
199 	case PPA_EPP_32:
200 	    ports = 8;
201 	    break;
202 	default:		/* Never gets here */
203 	    continue;
204 	}
205 
206 	host->can_queue = PPA_CAN_QUEUE;
207 	host->sg_tablesize = ppa_sg;
208 	hreg = scsi_register(host, 0);
209 	if(hreg == NULL)
210 		continue;
211 	hreg->io_port = pb->base;
212 	hreg->n_io_port = ports;
213 	hreg->dma_channel = -1;
214 	hreg->unique_id = i;
215 	ppa_hosts[i].host = hreg->host_no;
216 	nhosts++;
217     }
218     if (nhosts == 0) {
219 	if (try_again == 1) {
220 	    printk("WARNING - no ppa compatible devices found.\n");
221 	    printk("  As of 31/Aug/1998 Iomega started shipping parallel\n");
222 	    printk("  port ZIP drives with a different interface which is\n");
223 	    printk("  supported by the imm (ZIP Plus) driver. If the\n");
224 	    printk("  cable is marked with \"AutoDetect\", this is what has\n");
225 	    printk("  happened.\n");
226 	    spin_lock_irq(&io_request_lock);
227 	    return 0;
228 	}
229 	try_again = 1;
230 	goto retry_entry;
231     } else {
232 	spin_lock_irq(&io_request_lock);
233 	return 1;		/* return number of hosts detected */
234     }
235 }
236 
237 /* This is to give the ppa driver a way to modify the timings (and other
238  * parameters) by writing to the /proc/scsi/ppa/0 file.
239  * Very simple method really... (To simple, no error checking :( )
240  * Reason: Kernel hackers HATE having to unload and reload modules for
241  * testing...
242  * Also gives a method to use a script to obtain optimum timings (TODO)
243  */
244 
ppa_proc_write(int hostno,char * buffer,int length)245 static inline int ppa_proc_write(int hostno, char *buffer, int length)
246 {
247     unsigned long x;
248 
249     if ((length > 5) && (strncmp(buffer, "mode=", 5) == 0)) {
250 	x = simple_strtoul(buffer + 5, NULL, 0);
251 	ppa_hosts[hostno].mode = x;
252 	return length;
253     }
254     if ((length > 10) && (strncmp(buffer, "recon_tmo=", 10) == 0)) {
255 	x = simple_strtoul(buffer + 10, NULL, 0);
256 	ppa_hosts[hostno].recon_tmo = x;
257         printk("ppa: recon_tmo set to %ld\n", x);
258 	return length;
259     }
260     printk("ppa /proc: invalid variable\n");
261     return (-EINVAL);
262 }
263 
ppa_proc_info(char * buffer,char ** start,off_t offset,int length,int hostno,int inout)264 int ppa_proc_info(char *buffer, char **start, off_t offset,
265 		  int length, int hostno, int inout)
266 {
267     int i;
268     int len = 0;
269 
270     for (i = 0; i < 4; i++)
271 	if (ppa_hosts[i].host == hostno)
272 	    break;
273 
274     if (inout)
275 	return ppa_proc_write(i, buffer, length);
276 
277     len += sprintf(buffer + len, "Version : %s\n", PPA_VERSION);
278     len += sprintf(buffer + len, "Parport : %s\n", ppa_hosts[i].dev->port->name);
279     len += sprintf(buffer + len, "Mode    : %s\n", PPA_MODE_STRING[ppa_hosts[i].mode]);
280 #if PPA_DEBUG > 0
281     len += sprintf(buffer + len, "recon_tmo : %lu\n", ppa_hosts[i].recon_tmo);
282 #endif
283 
284     /* Request for beyond end of buffer */
285     if (offset > length)
286 	return 0;
287 
288     *start = buffer + offset;
289     len -= offset;
290     if (len > length)
291 	len = length;
292     return len;
293 }
294 
295 static int device_check(int host_no);
296 
297 #if PPA_DEBUG > 0
298 #define ppa_fail(x,y) printk("ppa: ppa_fail(%i) from %s at line %d\n",\
299 	   y, __FUNCTION__, __LINE__); ppa_fail_func(x,y);
ppa_fail_func(int host_no,int error_code)300 static inline void ppa_fail_func(int host_no, int error_code)
301 #else
302 static inline void ppa_fail(int host_no, int error_code)
303 #endif
304 {
305     /* If we fail a device then we trash status / message bytes */
306     if (ppa_hosts[host_no].cur_cmd) {
307 	ppa_hosts[host_no].cur_cmd->result = error_code << 16;
308 	ppa_hosts[host_no].failed = 1;
309     }
310 }
311 
312 /*
313  * Wait for the high bit to be set.
314  *
315  * In principle, this could be tied to an interrupt, but the adapter
316  * doesn't appear to be designed to support interrupts.  We spin on
317  * the 0x80 ready bit.
318  */
ppa_wait(int host_no)319 static unsigned char ppa_wait(int host_no)
320 {
321     int k;
322     unsigned short ppb = PPA_BASE(host_no);
323     unsigned char r;
324 
325     k = PPA_SPIN_TMO;
326     /* Wait for bit 6 and 7 - PJC */
327     for (r = r_str (ppb); ((r & 0xc0)!=0xc0) && (k); k--) {
328 	    udelay (1);
329 	    r = r_str (ppb);
330     }
331 
332     /*
333      * return some status information.
334      * Semantics: 0xc0 = ZIP wants more data
335      *            0xd0 = ZIP wants to send more data
336      *            0xe0 = ZIP is expecting SCSI command data
337      *            0xf0 = end of transfer, ZIP is sending status
338      */
339     if (k)
340 	return (r & 0xf0);
341 
342     /* Counter expired - Time out occurred */
343     ppa_fail(host_no, DID_TIME_OUT);
344     printk("ppa timeout in ppa_wait\n");
345     return 0;			/* command timed out */
346 }
347 
348 /*
349  * Clear EPP Timeout Bit
350  */
epp_reset(unsigned short ppb)351 static inline void epp_reset(unsigned short ppb)
352 {
353     int i;
354 
355     i = r_str(ppb);
356     w_str(ppb, i);
357     w_str(ppb, i & 0xfe);
358 }
359 
360 /*
361  * Wait for empty ECP fifo (if we are in ECP fifo mode only)
362  */
ecp_sync(unsigned short hostno)363 static inline void ecp_sync(unsigned short hostno)
364 {
365     int i, ppb_hi=ppa_hosts[hostno].dev->port->base_hi;
366 
367     if (ppb_hi == 0) return;
368 
369     if ((r_ecr(ppb_hi) & 0xe0) == 0x60) { /* mode 011 == ECP fifo mode */
370         for (i = 0; i < 100; i++) {
371             if (r_ecr(ppb_hi) & 0x01)
372                 return;
373             udelay(5);
374         }
375         printk("ppa: ECP sync failed as data still present in FIFO.\n");
376     }
377 }
378 
ppa_byte_out(unsigned short base,const char * buffer,int len)379 static int ppa_byte_out(unsigned short base, const char *buffer, int len)
380 {
381     int i;
382 
383     for (i = len; i; i--) {
384 	w_dtr(base, *buffer++);
385 	w_ctr(base, 0xe);
386 	w_ctr(base, 0xc);
387     }
388     return 1;			/* All went well - we hope! */
389 }
390 
ppa_byte_in(unsigned short base,char * buffer,int len)391 static int ppa_byte_in(unsigned short base, char *buffer, int len)
392 {
393     int i;
394 
395     for (i = len; i; i--) {
396 	*buffer++ = r_dtr(base);
397 	w_ctr(base, 0x27);
398 	w_ctr(base, 0x25);
399     }
400     return 1;			/* All went well - we hope! */
401 }
402 
ppa_nibble_in(unsigned short base,char * buffer,int len)403 static int ppa_nibble_in(unsigned short base, char *buffer, int len)
404 {
405     for (; len; len--) {
406 	unsigned char h;
407 
408 	w_ctr(base, 0x4);
409 	h = r_str(base) & 0xf0;
410 	w_ctr(base, 0x6);
411 	*buffer++ = h | ((r_str(base) & 0xf0) >> 4);
412     }
413     return 1;			/* All went well - we hope! */
414 }
415 
ppa_out(int host_no,char * buffer,int len)416 static int ppa_out(int host_no, char *buffer, int len)
417 {
418     int r;
419     unsigned short ppb = PPA_BASE(host_no);
420 
421     r = ppa_wait(host_no);
422 
423     if ((r & 0x50) != 0x40) {
424 	ppa_fail(host_no, DID_ERROR);
425 	return 0;
426     }
427     switch (ppa_hosts[host_no].mode) {
428     case PPA_NIBBLE:
429     case PPA_PS2:
430 	/* 8 bit output, with a loop */
431 	r = ppa_byte_out(ppb, buffer, len);
432 	break;
433 
434     case PPA_EPP_32:
435     case PPA_EPP_16:
436     case PPA_EPP_8:
437 	epp_reset(ppb);
438 	w_ctr(ppb, 0x4);
439 #ifdef CONFIG_SCSI_IZIP_EPP16
440 	if (!(((long) buffer | len) & 0x01))
441 	    outsw(ppb + 4, buffer, len >> 1);
442 #else
443 	if (!(((long) buffer | len) & 0x03))
444 	    outsl(ppb + 4, buffer, len >> 2);
445 #endif
446 	else
447 	    outsb(ppb + 4, buffer, len);
448 	w_ctr(ppb, 0xc);
449 	r = !(r_str(ppb) & 0x01);
450 	w_ctr(ppb, 0xc);
451 	ecp_sync(host_no);
452 	break;
453 
454     default:
455 	printk("PPA: bug in ppa_out()\n");
456 	r = 0;
457     }
458     return r;
459 }
460 
ppa_in(int host_no,char * buffer,int len)461 static int ppa_in(int host_no, char *buffer, int len)
462 {
463     int r;
464     unsigned short ppb = PPA_BASE(host_no);
465 
466     r = ppa_wait(host_no);
467 
468     if ((r & 0x50) != 0x50) {
469 	ppa_fail(host_no, DID_ERROR);
470 	return 0;
471     }
472     switch (ppa_hosts[host_no].mode) {
473     case PPA_NIBBLE:
474 	/* 4 bit input, with a loop */
475 	r = ppa_nibble_in(ppb, buffer, len);
476 	w_ctr(ppb, 0xc);
477 	break;
478 
479     case PPA_PS2:
480 	/* 8 bit input, with a loop */
481 	w_ctr(ppb, 0x25);
482 	r = ppa_byte_in(ppb, buffer, len);
483 	w_ctr(ppb, 0x4);
484 	w_ctr(ppb, 0xc);
485 	break;
486 
487     case PPA_EPP_32:
488     case PPA_EPP_16:
489     case PPA_EPP_8:
490 	epp_reset(ppb);
491 	w_ctr(ppb, 0x24);
492 #ifdef CONFIG_SCSI_IZIP_EPP16
493 	if (!(((long) buffer | len) & 0x01))
494 	    insw(ppb + 4, buffer, len >> 1);
495 #else
496 	if (!(((long) buffer | len) & 0x03))
497 	    insl(ppb + 4, buffer, len >> 2);
498 #endif
499 	else
500 	    insb(ppb + 4, buffer, len);
501 	w_ctr(ppb, 0x2c);
502 	r = !(r_str(ppb) & 0x01);
503 	w_ctr(ppb, 0x2c);
504 	ecp_sync(host_no);
505 	break;
506 
507     default:
508 	printk("PPA: bug in ppa_ins()\n");
509 	r = 0;
510 	break;
511     }
512     return r;
513 }
514 
515 /* end of ppa_io.h */
ppa_d_pulse(unsigned short ppb,unsigned char b)516 static inline void ppa_d_pulse(unsigned short ppb, unsigned char b)
517 {
518     w_dtr(ppb, b);
519     w_ctr(ppb, 0xc);
520     w_ctr(ppb, 0xe);
521     w_ctr(ppb, 0xc);
522     w_ctr(ppb, 0x4);
523     w_ctr(ppb, 0xc);
524 }
525 
ppa_disconnect(int host_no)526 static void ppa_disconnect(int host_no)
527 {
528     unsigned short ppb = PPA_BASE(host_no);
529 
530     ppa_d_pulse(ppb, 0);
531     ppa_d_pulse(ppb, 0x3c);
532     ppa_d_pulse(ppb, 0x20);
533     ppa_d_pulse(ppb, 0xf);
534 }
535 
ppa_c_pulse(unsigned short ppb,unsigned char b)536 static inline void ppa_c_pulse(unsigned short ppb, unsigned char b)
537 {
538     w_dtr(ppb, b);
539     w_ctr(ppb, 0x4);
540     w_ctr(ppb, 0x6);
541     w_ctr(ppb, 0x4);
542     w_ctr(ppb, 0xc);
543 }
544 
ppa_connect(int host_no,int flag)545 static inline void ppa_connect(int host_no, int flag)
546 {
547     unsigned short ppb = PPA_BASE(host_no);
548 
549     ppa_c_pulse(ppb, 0);
550     ppa_c_pulse(ppb, 0x3c);
551     ppa_c_pulse(ppb, 0x20);
552     if ((flag == CONNECT_EPP_MAYBE) &&
553 	IN_EPP_MODE(ppa_hosts[host_no].mode))
554 	ppa_c_pulse(ppb, 0xcf);
555     else
556 	ppa_c_pulse(ppb, 0x8f);
557 }
558 
ppa_select(int host_no,int target)559 static int ppa_select(int host_no, int target)
560 {
561     int k;
562     unsigned short ppb = PPA_BASE(host_no);
563 
564     /*
565      * Bit 6 (0x40) is the device selected bit.
566      * First we must wait till the current device goes off line...
567      */
568     k = PPA_SELECT_TMO;
569     do {
570 	k--;
571 	udelay(1);
572     } while ((r_str(ppb) & 0x40) && (k));
573     if (!k)
574 	return 0;
575 
576     w_dtr(ppb, (1 << target));
577     w_ctr(ppb, 0xe);
578     w_ctr(ppb, 0xc);
579     w_dtr(ppb, 0x80);		/* This is NOT the initator */
580     w_ctr(ppb, 0x8);
581 
582     k = PPA_SELECT_TMO;
583     do {
584 	k--;
585 	udelay(1);
586     }
587     while (!(r_str(ppb) & 0x40) && (k));
588     if (!k)
589 	return 0;
590 
591     return 1;
592 }
593 
594 /*
595  * This is based on a trace of what the Iomega DOS 'guest' driver does.
596  * I've tried several different kinds of parallel ports with guest and
597  * coded this to react in the same ways that it does.
598  *
599  * The return value from this function is just a hint about where the
600  * handshaking failed.
601  *
602  */
ppa_init(int host_no)603 static int ppa_init(int host_no)
604 {
605     int retv;
606     unsigned short ppb = PPA_BASE(host_no);
607 
608 #if defined(CONFIG_PARPORT) || defined(CONFIG_PARPORT_MODULE)
609     if (ppa_pb_claim(host_no))
610 	while (ppa_hosts[host_no].p_busy)
611 	    schedule();		/* We can safe schedule here */
612 #endif
613 
614     ppa_disconnect(host_no);
615     ppa_connect(host_no, CONNECT_NORMAL);
616 
617     retv = 2;			/* Failed */
618 
619     w_ctr(ppb, 0xe);
620     if ((r_str(ppb) & 0x08) == 0x08)
621 	retv--;
622 
623     w_ctr(ppb, 0xc);
624     if ((r_str(ppb) & 0x08) == 0x00)
625 	retv--;
626 
627     if (!retv)
628 	ppa_reset_pulse(ppb);
629     udelay(1000);		/* Allow devices to settle down */
630     ppa_disconnect(host_no);
631     udelay(1000);		/* Another delay to allow devices to settle */
632 
633     if (!retv)
634 	retv = device_check(host_no);
635 
636     ppa_pb_release(host_no);
637     return retv;
638 }
639 
ppa_send_command(Scsi_Cmnd * cmd)640 static inline int ppa_send_command(Scsi_Cmnd * cmd)
641 {
642     int host_no = cmd->host->unique_id;
643     int k;
644 
645     w_ctr(PPA_BASE(host_no), 0x0c);
646 
647     for (k = 0; k < cmd->cmd_len; k++)
648 	if (!ppa_out(host_no, &cmd->cmnd[k], 1))
649 	    return 0;
650     return 1;
651 }
652 
653 /*
654  * The bulk flag enables some optimisations in the data transfer loops,
655  * it should be true for any command that transfers data in integral
656  * numbers of sectors.
657  *
658  * The driver appears to remain stable if we speed up the parallel port
659  * i/o in this function, but not elsewhere.
660  */
ppa_completion(Scsi_Cmnd * cmd)661 static int ppa_completion(Scsi_Cmnd * cmd)
662 {
663     /* Return codes:
664      * -1     Error
665      *  0     Told to schedule
666      *  1     Finished data transfer
667      */
668     int host_no = cmd->host->unique_id;
669     unsigned short ppb = PPA_BASE(host_no);
670     unsigned long start_jiffies = jiffies;
671 
672     unsigned char r, v;
673     int fast, bulk, status;
674 
675     v = cmd->cmnd[0];
676     bulk = ((v == READ_6) ||
677 	    (v == READ_10) ||
678 	    (v == WRITE_6) ||
679 	    (v == WRITE_10));
680 
681     /*
682      * We only get here if the drive is ready to comunicate,
683      * hence no need for a full ppa_wait.
684      */
685     r = (r_str(ppb) & 0xf0);
686 
687     while (r != (unsigned char) 0xf0) {
688 	/*
689 	 * If we have been running for more than a full timer tick
690 	 * then take a rest.
691 	 */
692 	if (time_after(jiffies, start_jiffies + 1))
693 	    return 0;
694 
695 	if ((cmd->SCp.this_residual <= 0)) {
696 	    ppa_fail(host_no, DID_ERROR);
697 	    return -1;		/* ERROR_RETURN */
698 	}
699 
700 	/* On some hardware we have SCSI disconnected (6th bit low)
701 	 * for about 100usecs. It is too expensive to wait a
702 	 * tick on every loop so we busy wait for no more than
703 	 * 500usecs to give the drive a chance first. We do not
704 	 * change things for "normal" hardware since generally
705 	 * the 6th bit is always high.
706 	 * This makes the CPU load higher on some hardware
707 	 * but otherwise we can not get more then 50K/secs
708 	 * on this problem hardware.
709 	 */
710 	if ((r & 0xc0) != 0xc0) {
711 	   /* Wait for reconnection should be no more than
712 	    * jiffy/2 = 5ms = 5000 loops
713 	    */
714 	   unsigned long k = ppa_hosts[host_no].recon_tmo;
715 	   for (; k && ((r = (r_str(ppb) & 0xf0)) & 0xc0) != 0xc0; k--)
716 	     udelay(1);
717 
718 	   if(!k)
719 	     return 0;
720 	}
721 
722 	/* determine if we should use burst I/O */
723 	fast = (bulk && (cmd->SCp.this_residual >= PPA_BURST_SIZE))
724 	     ? PPA_BURST_SIZE : 1;
725 
726 	if (r == (unsigned char) 0xc0)
727 	    status = ppa_out(host_no, cmd->SCp.ptr, fast);
728 	else
729 	    status = ppa_in(host_no, cmd->SCp.ptr, fast);
730 
731 	cmd->SCp.ptr += fast;
732 	cmd->SCp.this_residual -= fast;
733 
734 	if (!status) {
735 	    ppa_fail(host_no, DID_BUS_BUSY);
736 	    return -1;		/* ERROR_RETURN */
737 	}
738 	if (cmd->SCp.buffer && !cmd->SCp.this_residual) {
739 	    /* if scatter/gather, advance to the next segment */
740 	    if (cmd->SCp.buffers_residual--) {
741 		cmd->SCp.buffer++;
742 		cmd->SCp.this_residual = cmd->SCp.buffer->length;
743 		cmd->SCp.ptr = cmd->SCp.buffer->address;
744 	    }
745 	}
746 	/* Now check to see if the drive is ready to comunicate */
747 	r = (r_str(ppb) & 0xf0);
748 	/* If not, drop back down to the scheduler and wait a timer tick */
749 	if (!(r & 0x80))
750 	    return 0;
751     }
752     return 1;			/* FINISH_RETURN */
753 }
754 
755 /* deprecated synchronous interface */
ppa_command(Scsi_Cmnd * cmd)756 int ppa_command(Scsi_Cmnd * cmd)
757 {
758     static int first_pass = 1;
759     int host_no = cmd->host->unique_id;
760 
761     if (first_pass) {
762 	printk("ppa: using non-queuing interface\n");
763 	first_pass = 0;
764     }
765     if (ppa_hosts[host_no].cur_cmd) {
766 	printk("PPA: bug in ppa_command\n");
767 	return 0;
768     }
769     ppa_hosts[host_no].failed = 0;
770     ppa_hosts[host_no].jstart = jiffies;
771     ppa_hosts[host_no].cur_cmd = cmd;
772     cmd->result = DID_ERROR << 16;	/* default return code */
773     cmd->SCp.phase = 0;
774 
775     ppa_pb_claim(host_no);
776 
777     while (ppa_engine(&ppa_hosts[host_no], cmd))
778 	schedule();
779 
780     if (cmd->SCp.phase)		/* Only disconnect if we have connected */
781 	ppa_disconnect(cmd->host->unique_id);
782 
783     ppa_pb_release(host_no);
784     ppa_hosts[host_no].cur_cmd = 0;
785     return cmd->result;
786 }
787 
788 /*
789  * Since the PPA itself doesn't generate interrupts, we use
790  * the scheduler's task queue to generate a stream of call-backs and
791  * complete the request when the drive is ready.
792  */
ppa_interrupt(void * data)793 static void ppa_interrupt(void *data)
794 {
795     ppa_struct *tmp = (ppa_struct *) data;
796     Scsi_Cmnd *cmd = tmp->cur_cmd;
797     unsigned long flags;
798 
799     if (!cmd) {
800 	printk("PPA: bug in ppa_interrupt\n");
801 	return;
802     }
803     if (ppa_engine(tmp, cmd)) {
804 	tmp->ppa_tq.data = (void *) tmp;
805 	tmp->ppa_tq.sync = 0;
806 	queue_task(&tmp->ppa_tq, &tq_timer);
807 	return;
808     }
809     /* Command must of completed hence it is safe to let go... */
810 #if PPA_DEBUG > 0
811     switch ((cmd->result >> 16) & 0xff) {
812     case DID_OK:
813 	break;
814     case DID_NO_CONNECT:
815 	printk("ppa: no device at SCSI ID %i\n", cmd->target);
816 	break;
817     case DID_BUS_BUSY:
818 	printk("ppa: BUS BUSY - EPP timeout detected\n");
819 	break;
820     case DID_TIME_OUT:
821 	printk("ppa: unknown timeout\n");
822 	break;
823     case DID_ABORT:
824 	printk("ppa: told to abort\n");
825 	break;
826     case DID_PARITY:
827 	printk("ppa: parity error (???)\n");
828 	break;
829     case DID_ERROR:
830 	printk("ppa: internal driver error\n");
831 	break;
832     case DID_RESET:
833 	printk("ppa: told to reset device\n");
834 	break;
835     case DID_BAD_INTR:
836 	printk("ppa: bad interrupt (???)\n");
837 	break;
838     default:
839 	printk("ppa: bad return code (%02x)\n", (cmd->result >> 16) & 0xff);
840     }
841 #endif
842 
843     if (cmd->SCp.phase > 1)
844 	ppa_disconnect(cmd->host->unique_id);
845     if (cmd->SCp.phase > 0)
846 	ppa_pb_release(cmd->host->unique_id);
847 
848     tmp->cur_cmd = 0;
849 
850     spin_lock_irqsave(&io_request_lock, flags);
851     cmd->scsi_done(cmd);
852     spin_unlock_irqrestore(&io_request_lock, flags);
853     return;
854 }
855 
ppa_engine(ppa_struct * tmp,Scsi_Cmnd * cmd)856 static int ppa_engine(ppa_struct * tmp, Scsi_Cmnd * cmd)
857 {
858     int host_no = cmd->host->unique_id;
859     unsigned short ppb = PPA_BASE(host_no);
860     unsigned char l = 0, h = 0;
861     int retv;
862 
863     /* First check for any errors that may of occurred
864      * Here we check for internal errors
865      */
866     if (tmp->failed)
867 	return 0;
868 
869     switch (cmd->SCp.phase) {
870     case 0:			/* Phase 0 - Waiting for parport */
871 	if ((jiffies - tmp->jstart) > HZ) {
872 	    /*
873 	     * We waited more than a second
874 	     * for parport to call us
875 	     */
876 	    ppa_fail(host_no, DID_BUS_BUSY);
877 	    return 0;
878 	}
879 	return 1;		/* wait until ppa_wakeup claims parport */
880     case 1:			/* Phase 1 - Connected */
881 	{			/* Perform a sanity check for cable unplugged */
882 	    int retv = 2;	/* Failed */
883 
884 	    ppa_connect(host_no, CONNECT_EPP_MAYBE);
885 
886 	    w_ctr(ppb, 0xe);
887 	    if ((r_str(ppb) & 0x08) == 0x08)
888 		retv--;
889 
890 	    w_ctr(ppb, 0xc);
891 	    if ((r_str(ppb) & 0x08) == 0x00)
892 		retv--;
893 
894 	    if (retv) {
895 		if ((jiffies - tmp->jstart) > (1 * HZ)) {
896 		    printk("ppa: Parallel port cable is unplugged!!\n");
897 		    ppa_fail(host_no, DID_BUS_BUSY);
898 		    return 0;
899 		} else {
900 		    ppa_disconnect(host_no);
901 		    return 1;	/* Try again in a jiffy */
902 		}
903 	    }
904 	    cmd->SCp.phase++;
905 	}
906 
907     case 2:			/* Phase 2 - We are now talking to the scsi bus */
908 	if (!ppa_select(host_no, cmd->target)) {
909 	    ppa_fail(host_no, DID_NO_CONNECT);
910 	    return 0;
911 	}
912 	cmd->SCp.phase++;
913 
914     case 3:			/* Phase 3 - Ready to accept a command */
915 	w_ctr(ppb, 0x0c);
916 	if (!(r_str(ppb) & 0x80))
917 	    return 1;
918 
919 	if (!ppa_send_command(cmd))
920 	    return 0;
921 	cmd->SCp.phase++;
922 
923     case 4:			/* Phase 4 - Setup scatter/gather buffers */
924 	if (cmd->use_sg) {
925 	    /* if many buffers are available, start filling the first */
926 	    cmd->SCp.buffer = (struct scatterlist *) cmd->request_buffer;
927 	    cmd->SCp.this_residual = cmd->SCp.buffer->length;
928 	    cmd->SCp.ptr = cmd->SCp.buffer->address;
929 	} else {
930 	    /* else fill the only available buffer */
931 	    cmd->SCp.buffer = NULL;
932 	    cmd->SCp.this_residual = cmd->request_bufflen;
933 	    cmd->SCp.ptr = cmd->request_buffer;
934 	}
935 	cmd->SCp.buffers_residual = cmd->use_sg;
936 	cmd->SCp.phase++;
937 
938     case 5:			/* Phase 5 - Data transfer stage */
939 	w_ctr(ppb, 0x0c);
940 	if (!(r_str(ppb) & 0x80))
941 	    return 1;
942 
943 	retv = ppa_completion(cmd);
944 	if (retv == -1)
945 	    return 0;
946 	if (retv == 0)
947 	    return 1;
948 	cmd->SCp.phase++;
949 
950     case 6:			/* Phase 6 - Read status/message */
951 	cmd->result = DID_OK << 16;
952 	/* Check for data overrun */
953 	if (ppa_wait(host_no) != (unsigned char) 0xf0) {
954 	    ppa_fail(host_no, DID_ERROR);
955 	    return 0;
956 	}
957 	if (ppa_in(host_no, &l, 1)) {	/* read status byte */
958 	    /* Check for optional message byte */
959 	    if (ppa_wait(host_no) == (unsigned char) 0xf0)
960 		ppa_in(host_no, &h, 1);
961 	    cmd->result = (DID_OK << 16) + (h << 8) + (l & STATUS_MASK);
962 	}
963 	return 0;		/* Finished */
964 	break;
965 
966     default:
967 	printk("ppa: Invalid scsi phase\n");
968     }
969     return 0;
970 }
971 
ppa_queuecommand(Scsi_Cmnd * cmd,void (* done)(Scsi_Cmnd *))972 int ppa_queuecommand(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
973 {
974     int host_no = cmd->host->unique_id;
975 
976     if (ppa_hosts[host_no].cur_cmd) {
977 	printk("PPA: bug in ppa_queuecommand\n");
978 	return 0;
979     }
980     ppa_hosts[host_no].failed = 0;
981     ppa_hosts[host_no].jstart = jiffies;
982     ppa_hosts[host_no].cur_cmd = cmd;
983     cmd->scsi_done = done;
984     cmd->result = DID_ERROR << 16;	/* default return code */
985     cmd->SCp.phase = 0;		/* bus free */
986 
987     ppa_pb_claim(host_no);
988 
989     ppa_hosts[host_no].ppa_tq.data = ppa_hosts + host_no;
990     ppa_hosts[host_no].ppa_tq.sync = 0;
991     queue_task(&ppa_hosts[host_no].ppa_tq, &tq_immediate);
992     mark_bh(IMMEDIATE_BH);
993 
994     return 0;
995 }
996 
997 /*
998  * Apparently the disk->capacity attribute is off by 1 sector
999  * for all disk drives.  We add the one here, but it should really
1000  * be done in sd.c.  Even if it gets fixed there, this will still
1001  * work.
1002  */
ppa_biosparam(Disk * disk,kdev_t dev,int ip[])1003 int ppa_biosparam(Disk * disk, kdev_t dev, int ip[])
1004 {
1005     ip[0] = 0x40;
1006     ip[1] = 0x20;
1007     ip[2] = (disk->capacity + 1) / (ip[0] * ip[1]);
1008     if (ip[2] > 1024) {
1009 	ip[0] = 0xff;
1010 	ip[1] = 0x3f;
1011 	ip[2] = (disk->capacity + 1) / (ip[0] * ip[1]);
1012 	if (ip[2] > 1023)
1013 	    ip[2] = 1023;
1014     }
1015     return 0;
1016 }
1017 
ppa_abort(Scsi_Cmnd * cmd)1018 int ppa_abort(Scsi_Cmnd * cmd)
1019 {
1020     int host_no = cmd->host->unique_id;
1021     /*
1022      * There is no method for aborting commands since Iomega
1023      * have tied the SCSI_MESSAGE line high in the interface
1024      */
1025 
1026     switch (cmd->SCp.phase) {
1027     case 0:			/* Do not have access to parport */
1028     case 1:			/* Have not connected to interface */
1029 	ppa_hosts[host_no].cur_cmd = NULL;	/* Forget the problem */
1030 	return SUCCESS;
1031 	break;
1032     default:			/* SCSI command sent, can not abort */
1033 	return FAILED;
1034 	break;
1035     }
1036 }
1037 
ppa_reset_pulse(unsigned int base)1038 static void ppa_reset_pulse(unsigned int base)
1039 {
1040     w_dtr(base, 0x40);
1041     w_ctr(base, 0x8);
1042     udelay(30);
1043     w_ctr(base, 0xc);
1044 }
1045 
ppa_reset(Scsi_Cmnd * cmd)1046 int ppa_reset(Scsi_Cmnd * cmd)
1047 {
1048     int host_no = cmd->host->unique_id;
1049 
1050     if (cmd->SCp.phase)
1051 	ppa_disconnect(host_no);
1052     ppa_hosts[host_no].cur_cmd = NULL;	/* Forget the problem */
1053 
1054     ppa_connect(host_no, CONNECT_NORMAL);
1055     ppa_reset_pulse(PPA_BASE(host_no));
1056     udelay(1000);		/* device settle delay */
1057     ppa_disconnect(host_no);
1058     udelay(1000);		/* device settle delay */
1059     return SUCCESS;
1060 }
1061 
device_check(int host_no)1062 static int device_check(int host_no)
1063 {
1064     /* This routine looks for a device and then attempts to use EPP
1065        to send a command. If all goes as planned then EPP is available. */
1066 
1067     static char cmd[6] =
1068     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1069     int loop, old_mode, status, k, ppb = PPA_BASE(host_no);
1070     unsigned char l;
1071 
1072     old_mode = ppa_hosts[host_no].mode;
1073     for (loop = 0; loop < 8; loop++) {
1074 	/* Attempt to use EPP for Test Unit Ready */
1075 	if ((ppb & 0x0007) == 0x0000)
1076 	    ppa_hosts[host_no].mode = PPA_EPP_32;
1077 
1078       second_pass:
1079 	ppa_connect(host_no, CONNECT_EPP_MAYBE);
1080 	/* Select SCSI device */
1081 	if (!ppa_select(host_no, loop)) {
1082 	    ppa_disconnect(host_no);
1083 	    continue;
1084 	}
1085 	printk("ppa: Found device at ID %i, Attempting to use %s\n", loop,
1086 	       PPA_MODE_STRING[ppa_hosts[host_no].mode]);
1087 
1088 	/* Send SCSI command */
1089 	status = 1;
1090 	w_ctr(ppb, 0x0c);
1091 	for (l = 0; (l < 6) && (status); l++)
1092 	    status = ppa_out(host_no, cmd, 1);
1093 
1094 	if (!status) {
1095 	    ppa_disconnect(host_no);
1096 	    ppa_connect(host_no, CONNECT_EPP_MAYBE);
1097 	    w_dtr(ppb, 0x40);
1098 	    w_ctr(ppb, 0x08);
1099 	    udelay(30);
1100 	    w_ctr(ppb, 0x0c);
1101 	    udelay(1000);
1102 	    ppa_disconnect(host_no);
1103 	    udelay(1000);
1104 	    if (ppa_hosts[host_no].mode == PPA_EPP_32) {
1105 		ppa_hosts[host_no].mode = old_mode;
1106 		goto second_pass;
1107 	    }
1108 	    printk("ppa: Unable to establish communication, aborting driver load.\n");
1109 	    return 1;
1110 	}
1111 	w_ctr(ppb, 0x0c);
1112 	k = 1000000;		/* 1 Second */
1113 	do {
1114 	    l = r_str(ppb);
1115 	    k--;
1116 	    udelay(1);
1117 	} while (!(l & 0x80) && (k));
1118 
1119 	l &= 0xf0;
1120 
1121 	if (l != 0xf0) {
1122 	    ppa_disconnect(host_no);
1123 	    ppa_connect(host_no, CONNECT_EPP_MAYBE);
1124 	    ppa_reset_pulse(ppb);
1125 	    udelay(1000);
1126 	    ppa_disconnect(host_no);
1127 	    udelay(1000);
1128 	    if (ppa_hosts[host_no].mode == PPA_EPP_32) {
1129 		ppa_hosts[host_no].mode = old_mode;
1130 		goto second_pass;
1131 	    }
1132 	    printk("ppa: Unable to establish communication, aborting driver load.\n");
1133 	    return 1;
1134 	}
1135 	ppa_disconnect(host_no);
1136 	printk("ppa: Communication established with ID %i using %s\n", loop,
1137 	       PPA_MODE_STRING[ppa_hosts[host_no].mode]);
1138 	ppa_connect(host_no, CONNECT_EPP_MAYBE);
1139 	ppa_reset_pulse(ppb);
1140 	udelay(1000);
1141 	ppa_disconnect(host_no);
1142 	udelay(1000);
1143 	return 0;
1144     }
1145     printk("ppa: No devices found, aborting driver load.\n");
1146     return 1;
1147 }
1148 MODULE_LICENSE("GPL");
1149