ppa.c 27 KB

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