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