ppa.c 26 KB

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