ppa.c 26 KB

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