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