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