ppa.c 25 KB

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