ppa.c 26 KB

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