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