ppa.c 26 KB

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