ppa.c 26 KB

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