imm.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /* imm.c -- low level driver for the IOMEGA MatchMaker
  2. * parallel port SCSI host adapter.
  3. *
  4. * (The IMM is the embedded controller in the ZIP Plus drive.)
  5. *
  6. * My unofficial company acronym list is 21 pages long:
  7. * FLA: Four letter acronym with built in facility for
  8. * future expansion to five letters.
  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/slab.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. /* The following #define is to avoid a clash with hosts.c */
  24. #define IMM_PROBE_SPP 0x0001
  25. #define IMM_PROBE_PS2 0x0002
  26. #define IMM_PROBE_ECR 0x0010
  27. #define IMM_PROBE_EPP17 0x0100
  28. #define IMM_PROBE_EPP19 0x0200
  29. typedef struct {
  30. struct pardevice *dev; /* Parport device entry */
  31. int base; /* Actual port address */
  32. int base_hi; /* Hi Base address for ECP-ISA chipset */
  33. int mode; /* Transfer mode */
  34. struct scsi_cmnd *cur_cmd; /* Current queued command */
  35. struct delayed_work imm_tq; /* Polling interrupt stuff */
  36. unsigned long jstart; /* Jiffies at start */
  37. unsigned failed:1; /* Failure flag */
  38. unsigned dp:1; /* Data phase present */
  39. unsigned rd:1; /* Read data in data phase */
  40. unsigned wanted:1; /* Parport sharing busy flag */
  41. wait_queue_head_t *waiting;
  42. struct Scsi_Host *host;
  43. struct list_head list;
  44. } imm_struct;
  45. static void imm_reset_pulse(unsigned int base);
  46. static int device_check(imm_struct *dev);
  47. #include "imm.h"
  48. static inline imm_struct *imm_dev(struct Scsi_Host *host)
  49. {
  50. return *(imm_struct **)&host->hostdata;
  51. }
  52. static DEFINE_SPINLOCK(arbitration_lock);
  53. static void got_it(imm_struct *dev)
  54. {
  55. dev->base = dev->dev->port->base;
  56. if (dev->cur_cmd)
  57. dev->cur_cmd->SCp.phase = 1;
  58. else
  59. wake_up(dev->waiting);
  60. }
  61. static void imm_wakeup(void *ref)
  62. {
  63. imm_struct *dev = (imm_struct *) ref;
  64. unsigned long flags;
  65. spin_lock_irqsave(&arbitration_lock, flags);
  66. if (dev->wanted) {
  67. parport_claim(dev->dev);
  68. got_it(dev);
  69. dev->wanted = 0;
  70. }
  71. spin_unlock_irqrestore(&arbitration_lock, flags);
  72. }
  73. static int imm_pb_claim(imm_struct *dev)
  74. {
  75. unsigned long flags;
  76. int res = 1;
  77. spin_lock_irqsave(&arbitration_lock, flags);
  78. if (parport_claim(dev->dev) == 0) {
  79. got_it(dev);
  80. res = 0;
  81. }
  82. dev->wanted = res;
  83. spin_unlock_irqrestore(&arbitration_lock, flags);
  84. return res;
  85. }
  86. static void imm_pb_dismiss(imm_struct *dev)
  87. {
  88. unsigned long flags;
  89. int wanted;
  90. spin_lock_irqsave(&arbitration_lock, flags);
  91. wanted = dev->wanted;
  92. dev->wanted = 0;
  93. spin_unlock_irqrestore(&arbitration_lock, flags);
  94. if (!wanted)
  95. parport_release(dev->dev);
  96. }
  97. static inline void imm_pb_release(imm_struct *dev)
  98. {
  99. parport_release(dev->dev);
  100. }
  101. /* This is to give the imm driver a way to modify the timings (and other
  102. * parameters) by writing to the /proc/scsi/imm/0 file.
  103. * Very simple method really... (Too simple, no error checking :( )
  104. * Reason: Kernel hackers HATE having to unload and reload modules for
  105. * testing...
  106. * Also gives a method to use a script to obtain optimum timings (TODO)
  107. */
  108. static int imm_write_info(struct Scsi_Host *host, char *buffer, int length)
  109. {
  110. imm_struct *dev = imm_dev(host);
  111. if ((length > 5) && (strncmp(buffer, "mode=", 5) == 0)) {
  112. dev->mode = simple_strtoul(buffer + 5, NULL, 0);
  113. return length;
  114. }
  115. printk("imm /proc: invalid variable\n");
  116. return -EINVAL;
  117. }
  118. static int imm_show_info(struct seq_file *m, struct Scsi_Host *host)
  119. {
  120. imm_struct *dev = imm_dev(host);
  121. seq_printf(m, "Version : %s\n", IMM_VERSION);
  122. seq_printf(m, "Parport : %s\n", dev->dev->port->name);
  123. seq_printf(m, "Mode : %s\n", IMM_MODE_STRING[dev->mode]);
  124. return 0;
  125. }
  126. #if IMM_DEBUG > 0
  127. #define imm_fail(x,y) printk("imm: imm_fail(%i) from %s at line %d\n",\
  128. y, __func__, __LINE__); imm_fail_func(x,y);
  129. static inline void
  130. imm_fail_func(imm_struct *dev, int error_code)
  131. #else
  132. static inline void
  133. imm_fail(imm_struct *dev, int error_code)
  134. #endif
  135. {
  136. /* If we fail a device then we trash status / message bytes */
  137. if (dev->cur_cmd) {
  138. dev->cur_cmd->result = error_code << 16;
  139. dev->failed = 1;
  140. }
  141. }
  142. /*
  143. * Wait for the high bit to be set.
  144. *
  145. * In principle, this could be tied to an interrupt, but the adapter
  146. * doesn't appear to be designed to support interrupts. We spin on
  147. * the 0x80 ready bit.
  148. */
  149. static unsigned char imm_wait(imm_struct *dev)
  150. {
  151. int k;
  152. unsigned short ppb = dev->base;
  153. unsigned char r;
  154. w_ctr(ppb, 0x0c);
  155. k = IMM_SPIN_TMO;
  156. do {
  157. r = r_str(ppb);
  158. k--;
  159. udelay(1);
  160. }
  161. while (!(r & 0x80) && (k));
  162. /*
  163. * STR register (LPT base+1) to SCSI mapping:
  164. *
  165. * STR imm imm
  166. * ===================================
  167. * 0x80 S_REQ S_REQ
  168. * 0x40 !S_BSY (????)
  169. * 0x20 !S_CD !S_CD
  170. * 0x10 !S_IO !S_IO
  171. * 0x08 (????) !S_BSY
  172. *
  173. * imm imm meaning
  174. * ==================================
  175. * 0xf0 0xb8 Bit mask
  176. * 0xc0 0x88 ZIP wants more data
  177. * 0xd0 0x98 ZIP wants to send more data
  178. * 0xe0 0xa8 ZIP is expecting SCSI command data
  179. * 0xf0 0xb8 end of transfer, ZIP is sending status
  180. */
  181. w_ctr(ppb, 0x04);
  182. if (k)
  183. return (r & 0xb8);
  184. /* Counter expired - Time out occurred */
  185. imm_fail(dev, DID_TIME_OUT);
  186. printk("imm timeout in imm_wait\n");
  187. return 0; /* command timed out */
  188. }
  189. static int imm_negotiate(imm_struct * tmp)
  190. {
  191. /*
  192. * The following is supposedly the IEEE 1284-1994 negotiate
  193. * sequence. I have yet to obtain a copy of the above standard
  194. * so this is a bit of a guess...
  195. *
  196. * A fair chunk of this is based on the Linux parport implementation
  197. * of IEEE 1284.
  198. *
  199. * Return 0 if data available
  200. * 1 if no data available
  201. */
  202. unsigned short base = tmp->base;
  203. unsigned char a, mode;
  204. switch (tmp->mode) {
  205. case IMM_NIBBLE:
  206. mode = 0x00;
  207. break;
  208. case IMM_PS2:
  209. mode = 0x01;
  210. break;
  211. default:
  212. return 0;
  213. }
  214. w_ctr(base, 0x04);
  215. udelay(5);
  216. w_dtr(base, mode);
  217. udelay(100);
  218. w_ctr(base, 0x06);
  219. udelay(5);
  220. a = (r_str(base) & 0x20) ? 0 : 1;
  221. udelay(5);
  222. w_ctr(base, 0x07);
  223. udelay(5);
  224. w_ctr(base, 0x06);
  225. if (a) {
  226. printk
  227. ("IMM: IEEE1284 negotiate indicates no data available.\n");
  228. imm_fail(tmp, DID_ERROR);
  229. }
  230. return a;
  231. }
  232. /*
  233. * Clear EPP timeout bit.
  234. */
  235. static inline void epp_reset(unsigned short ppb)
  236. {
  237. int i;
  238. i = r_str(ppb);
  239. w_str(ppb, i);
  240. w_str(ppb, i & 0xfe);
  241. }
  242. /*
  243. * Wait for empty ECP fifo (if we are in ECP fifo mode only)
  244. */
  245. static inline void ecp_sync(imm_struct *dev)
  246. {
  247. int i, ppb_hi = dev->base_hi;
  248. if (ppb_hi == 0)
  249. return;
  250. if ((r_ecr(ppb_hi) & 0xe0) == 0x60) { /* mode 011 == ECP fifo mode */
  251. for (i = 0; i < 100; i++) {
  252. if (r_ecr(ppb_hi) & 0x01)
  253. return;
  254. udelay(5);
  255. }
  256. printk("imm: ECP sync failed as data still present in FIFO.\n");
  257. }
  258. }
  259. static int imm_byte_out(unsigned short base, const char *buffer, int len)
  260. {
  261. int i;
  262. w_ctr(base, 0x4); /* apparently a sane mode */
  263. for (i = len >> 1; i; i--) {
  264. w_dtr(base, *buffer++);
  265. w_ctr(base, 0x5); /* Drop STROBE low */
  266. w_dtr(base, *buffer++);
  267. w_ctr(base, 0x0); /* STROBE high + INIT low */
  268. }
  269. w_ctr(base, 0x4); /* apparently a sane mode */
  270. return 1; /* All went well - we hope! */
  271. }
  272. static int imm_nibble_in(unsigned short base, char *buffer, int len)
  273. {
  274. unsigned char l;
  275. int i;
  276. /*
  277. * The following is based on documented timing signals
  278. */
  279. w_ctr(base, 0x4);
  280. for (i = len; i; i--) {
  281. w_ctr(base, 0x6);
  282. l = (r_str(base) & 0xf0) >> 4;
  283. w_ctr(base, 0x5);
  284. *buffer++ = (r_str(base) & 0xf0) | l;
  285. w_ctr(base, 0x4);
  286. }
  287. return 1; /* All went well - we hope! */
  288. }
  289. static int imm_byte_in(unsigned short base, char *buffer, int len)
  290. {
  291. int i;
  292. /*
  293. * The following is based on documented timing signals
  294. */
  295. w_ctr(base, 0x4);
  296. for (i = len; i; i--) {
  297. w_ctr(base, 0x26);
  298. *buffer++ = r_dtr(base);
  299. w_ctr(base, 0x25);
  300. }
  301. return 1; /* All went well - we hope! */
  302. }
  303. static int imm_out(imm_struct *dev, char *buffer, int len)
  304. {
  305. unsigned short ppb = dev->base;
  306. int r = imm_wait(dev);
  307. /*
  308. * Make sure that:
  309. * a) the SCSI bus is BUSY (device still listening)
  310. * b) the device is listening
  311. */
  312. if ((r & 0x18) != 0x08) {
  313. imm_fail(dev, DID_ERROR);
  314. printk("IMM: returned SCSI status %2x\n", r);
  315. return 0;
  316. }
  317. switch (dev->mode) {
  318. case IMM_EPP_32:
  319. case IMM_EPP_16:
  320. case IMM_EPP_8:
  321. epp_reset(ppb);
  322. w_ctr(ppb, 0x4);
  323. #ifdef CONFIG_SCSI_IZIP_EPP16
  324. if (!(((long) buffer | len) & 0x01))
  325. outsw(ppb + 4, buffer, len >> 1);
  326. #else
  327. if (!(((long) buffer | len) & 0x03))
  328. outsl(ppb + 4, buffer, len >> 2);
  329. #endif
  330. else
  331. outsb(ppb + 4, buffer, len);
  332. w_ctr(ppb, 0xc);
  333. r = !(r_str(ppb) & 0x01);
  334. w_ctr(ppb, 0xc);
  335. ecp_sync(dev);
  336. break;
  337. case IMM_NIBBLE:
  338. case IMM_PS2:
  339. /* 8 bit output, with a loop */
  340. r = imm_byte_out(ppb, buffer, len);
  341. break;
  342. default:
  343. printk("IMM: bug in imm_out()\n");
  344. r = 0;
  345. }
  346. return r;
  347. }
  348. static int imm_in(imm_struct *dev, char *buffer, int len)
  349. {
  350. unsigned short ppb = dev->base;
  351. int r = imm_wait(dev);
  352. /*
  353. * Make sure that:
  354. * a) the SCSI bus is BUSY (device still listening)
  355. * b) the device is sending data
  356. */
  357. if ((r & 0x18) != 0x18) {
  358. imm_fail(dev, DID_ERROR);
  359. return 0;
  360. }
  361. switch (dev->mode) {
  362. case IMM_NIBBLE:
  363. /* 4 bit input, with a loop */
  364. r = imm_nibble_in(ppb, buffer, len);
  365. w_ctr(ppb, 0xc);
  366. break;
  367. case IMM_PS2:
  368. /* 8 bit input, with a loop */
  369. r = imm_byte_in(ppb, buffer, len);
  370. w_ctr(ppb, 0xc);
  371. break;
  372. case IMM_EPP_32:
  373. case IMM_EPP_16:
  374. case IMM_EPP_8:
  375. epp_reset(ppb);
  376. w_ctr(ppb, 0x24);
  377. #ifdef CONFIG_SCSI_IZIP_EPP16
  378. if (!(((long) buffer | len) & 0x01))
  379. insw(ppb + 4, buffer, len >> 1);
  380. #else
  381. if (!(((long) buffer | len) & 0x03))
  382. insl(ppb + 4, buffer, len >> 2);
  383. #endif
  384. else
  385. insb(ppb + 4, buffer, len);
  386. w_ctr(ppb, 0x2c);
  387. r = !(r_str(ppb) & 0x01);
  388. w_ctr(ppb, 0x2c);
  389. ecp_sync(dev);
  390. break;
  391. default:
  392. printk("IMM: bug in imm_ins()\n");
  393. r = 0;
  394. break;
  395. }
  396. return r;
  397. }
  398. static int imm_cpp(unsigned short ppb, unsigned char b)
  399. {
  400. /*
  401. * Comments on udelay values refer to the
  402. * Command Packet Protocol (CPP) timing diagram.
  403. */
  404. unsigned char s1, s2, s3;
  405. w_ctr(ppb, 0x0c);
  406. udelay(2); /* 1 usec - infinite */
  407. w_dtr(ppb, 0xaa);
  408. udelay(10); /* 7 usec - infinite */
  409. w_dtr(ppb, 0x55);
  410. udelay(10); /* 7 usec - infinite */
  411. w_dtr(ppb, 0x00);
  412. udelay(10); /* 7 usec - infinite */
  413. w_dtr(ppb, 0xff);
  414. udelay(10); /* 7 usec - infinite */
  415. s1 = r_str(ppb) & 0xb8;
  416. w_dtr(ppb, 0x87);
  417. udelay(10); /* 7 usec - infinite */
  418. s2 = r_str(ppb) & 0xb8;
  419. w_dtr(ppb, 0x78);
  420. udelay(10); /* 7 usec - infinite */
  421. s3 = r_str(ppb) & 0x38;
  422. /*
  423. * Values for b are:
  424. * 0000 00aa Assign address aa to current device
  425. * 0010 00aa Select device aa in EPP Winbond mode
  426. * 0010 10aa Select device aa in EPP mode
  427. * 0011 xxxx Deselect all devices
  428. * 0110 00aa Test device aa
  429. * 1101 00aa Select device aa in ECP mode
  430. * 1110 00aa Select device aa in Compatible mode
  431. */
  432. w_dtr(ppb, b);
  433. udelay(2); /* 1 usec - infinite */
  434. w_ctr(ppb, 0x0c);
  435. udelay(10); /* 7 usec - infinite */
  436. w_ctr(ppb, 0x0d);
  437. udelay(2); /* 1 usec - infinite */
  438. w_ctr(ppb, 0x0c);
  439. udelay(10); /* 7 usec - infinite */
  440. w_dtr(ppb, 0xff);
  441. udelay(10); /* 7 usec - infinite */
  442. /*
  443. * The following table is electrical pin values.
  444. * (BSY is inverted at the CTR register)
  445. *
  446. * BSY ACK POut SEL Fault
  447. * S1 0 X 1 1 1
  448. * S2 1 X 0 1 1
  449. * S3 L X 1 1 S
  450. *
  451. * L => Last device in chain
  452. * S => Selected
  453. *
  454. * Observered values for S1,S2,S3 are:
  455. * Disconnect => f8/58/78
  456. * Connect => f8/58/70
  457. */
  458. if ((s1 == 0xb8) && (s2 == 0x18) && (s3 == 0x30))
  459. return 1; /* Connected */
  460. if ((s1 == 0xb8) && (s2 == 0x18) && (s3 == 0x38))
  461. return 0; /* Disconnected */
  462. return -1; /* No device present */
  463. }
  464. static inline int imm_connect(imm_struct *dev, int flag)
  465. {
  466. unsigned short ppb = dev->base;
  467. imm_cpp(ppb, 0xe0); /* Select device 0 in compatible mode */
  468. imm_cpp(ppb, 0x30); /* Disconnect all devices */
  469. if ((dev->mode == IMM_EPP_8) ||
  470. (dev->mode == IMM_EPP_16) ||
  471. (dev->mode == IMM_EPP_32))
  472. return imm_cpp(ppb, 0x28); /* Select device 0 in EPP mode */
  473. return imm_cpp(ppb, 0xe0); /* Select device 0 in compatible mode */
  474. }
  475. static void imm_disconnect(imm_struct *dev)
  476. {
  477. imm_cpp(dev->base, 0x30); /* Disconnect all devices */
  478. }
  479. static int imm_select(imm_struct *dev, int target)
  480. {
  481. int k;
  482. unsigned short ppb = dev->base;
  483. /*
  484. * Firstly we want to make sure there is nothing
  485. * holding onto the SCSI bus.
  486. */
  487. w_ctr(ppb, 0xc);
  488. k = IMM_SELECT_TMO;
  489. do {
  490. k--;
  491. } while ((r_str(ppb) & 0x08) && (k));
  492. if (!k)
  493. return 0;
  494. /*
  495. * Now assert the SCSI ID (HOST and TARGET) on the data bus
  496. */
  497. w_ctr(ppb, 0x4);
  498. w_dtr(ppb, 0x80 | (1 << target));
  499. udelay(1);
  500. /*
  501. * Deassert SELIN first followed by STROBE
  502. */
  503. w_ctr(ppb, 0xc);
  504. w_ctr(ppb, 0xd);
  505. /*
  506. * ACK should drop low while SELIN is deasserted.
  507. * FAULT should drop low when the SCSI device latches the bus.
  508. */
  509. k = IMM_SELECT_TMO;
  510. do {
  511. k--;
  512. }
  513. while (!(r_str(ppb) & 0x08) && (k));
  514. /*
  515. * Place the interface back into a sane state (status mode)
  516. */
  517. w_ctr(ppb, 0xc);
  518. return (k) ? 1 : 0;
  519. }
  520. static int imm_init(imm_struct *dev)
  521. {
  522. if (imm_connect(dev, 0) != 1)
  523. return -EIO;
  524. imm_reset_pulse(dev->base);
  525. mdelay(1); /* Delay to allow devices to settle */
  526. imm_disconnect(dev);
  527. mdelay(1); /* Another delay to allow devices to settle */
  528. return device_check(dev);
  529. }
  530. static inline int imm_send_command(struct scsi_cmnd *cmd)
  531. {
  532. imm_struct *dev = imm_dev(cmd->device->host);
  533. int k;
  534. /* NOTE: IMM uses byte pairs */
  535. for (k = 0; k < cmd->cmd_len; k += 2)
  536. if (!imm_out(dev, &cmd->cmnd[k], 2))
  537. return 0;
  538. return 1;
  539. }
  540. /*
  541. * The bulk flag enables some optimisations in the data transfer loops,
  542. * it should be true for any command that transfers data in integral
  543. * numbers of sectors.
  544. *
  545. * The driver appears to remain stable if we speed up the parallel port
  546. * i/o in this function, but not elsewhere.
  547. */
  548. static int imm_completion(struct scsi_cmnd *cmd)
  549. {
  550. /* Return codes:
  551. * -1 Error
  552. * 0 Told to schedule
  553. * 1 Finished data transfer
  554. */
  555. imm_struct *dev = imm_dev(cmd->device->host);
  556. unsigned short ppb = dev->base;
  557. unsigned long start_jiffies = jiffies;
  558. unsigned char r, v;
  559. int fast, bulk, status;
  560. v = cmd->cmnd[0];
  561. bulk = ((v == READ_6) ||
  562. (v == READ_10) || (v == WRITE_6) || (v == WRITE_10));
  563. /*
  564. * We only get here if the drive is ready to comunicate,
  565. * hence no need for a full imm_wait.
  566. */
  567. w_ctr(ppb, 0x0c);
  568. r = (r_str(ppb) & 0xb8);
  569. /*
  570. * while (device is not ready to send status byte)
  571. * loop;
  572. */
  573. while (r != (unsigned char) 0xb8) {
  574. /*
  575. * If we have been running for more than a full timer tick
  576. * then take a rest.
  577. */
  578. if (time_after(jiffies, start_jiffies + 1))
  579. return 0;
  580. /*
  581. * FAIL if:
  582. * a) Drive status is screwy (!ready && !present)
  583. * b) Drive is requesting/sending more data than expected
  584. */
  585. if (((r & 0x88) != 0x88) || (cmd->SCp.this_residual <= 0)) {
  586. imm_fail(dev, DID_ERROR);
  587. return -1; /* ERROR_RETURN */
  588. }
  589. /* determine if we should use burst I/O */
  590. if (dev->rd == 0) {
  591. fast = (bulk
  592. && (cmd->SCp.this_residual >=
  593. IMM_BURST_SIZE)) ? IMM_BURST_SIZE : 2;
  594. status = imm_out(dev, cmd->SCp.ptr, fast);
  595. } else {
  596. fast = (bulk
  597. && (cmd->SCp.this_residual >=
  598. IMM_BURST_SIZE)) ? IMM_BURST_SIZE : 1;
  599. status = imm_in(dev, cmd->SCp.ptr, fast);
  600. }
  601. cmd->SCp.ptr += fast;
  602. cmd->SCp.this_residual -= fast;
  603. if (!status) {
  604. imm_fail(dev, DID_BUS_BUSY);
  605. return -1; /* ERROR_RETURN */
  606. }
  607. if (cmd->SCp.buffer && !cmd->SCp.this_residual) {
  608. /* if scatter/gather, advance to the next segment */
  609. if (cmd->SCp.buffers_residual--) {
  610. cmd->SCp.buffer++;
  611. cmd->SCp.this_residual =
  612. cmd->SCp.buffer->length;
  613. cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
  614. /*
  615. * Make sure that we transfer even number of bytes
  616. * otherwise it makes imm_byte_out() messy.
  617. */
  618. if (cmd->SCp.this_residual & 0x01)
  619. cmd->SCp.this_residual++;
  620. }
  621. }
  622. /* Now check to see if the drive is ready to comunicate */
  623. w_ctr(ppb, 0x0c);
  624. r = (r_str(ppb) & 0xb8);
  625. /* If not, drop back down to the scheduler and wait a timer tick */
  626. if (!(r & 0x80))
  627. return 0;
  628. }
  629. return 1; /* FINISH_RETURN */
  630. }
  631. /*
  632. * Since the IMM itself doesn't generate interrupts, we use
  633. * the scheduler's task queue to generate a stream of call-backs and
  634. * complete the request when the drive is ready.
  635. */
  636. static void imm_interrupt(struct work_struct *work)
  637. {
  638. imm_struct *dev = container_of(work, imm_struct, imm_tq.work);
  639. struct scsi_cmnd *cmd = dev->cur_cmd;
  640. struct Scsi_Host *host = cmd->device->host;
  641. unsigned long flags;
  642. if (imm_engine(dev, cmd)) {
  643. schedule_delayed_work(&dev->imm_tq, 1);
  644. return;
  645. }
  646. /* Command must of completed hence it is safe to let go... */
  647. #if IMM_DEBUG > 0
  648. switch ((cmd->result >> 16) & 0xff) {
  649. case DID_OK:
  650. break;
  651. case DID_NO_CONNECT:
  652. printk("imm: no device at SCSI ID %i\n", cmd->device->id);
  653. break;
  654. case DID_BUS_BUSY:
  655. printk("imm: BUS BUSY - EPP timeout detected\n");
  656. break;
  657. case DID_TIME_OUT:
  658. printk("imm: unknown timeout\n");
  659. break;
  660. case DID_ABORT:
  661. printk("imm: told to abort\n");
  662. break;
  663. case DID_PARITY:
  664. printk("imm: parity error (???)\n");
  665. break;
  666. case DID_ERROR:
  667. printk("imm: internal driver error\n");
  668. break;
  669. case DID_RESET:
  670. printk("imm: told to reset device\n");
  671. break;
  672. case DID_BAD_INTR:
  673. printk("imm: bad interrupt (???)\n");
  674. break;
  675. default:
  676. printk("imm: bad return code (%02x)\n",
  677. (cmd->result >> 16) & 0xff);
  678. }
  679. #endif
  680. if (cmd->SCp.phase > 1)
  681. imm_disconnect(dev);
  682. imm_pb_dismiss(dev);
  683. spin_lock_irqsave(host->host_lock, flags);
  684. dev->cur_cmd = NULL;
  685. cmd->scsi_done(cmd);
  686. spin_unlock_irqrestore(host->host_lock, flags);
  687. return;
  688. }
  689. static int imm_engine(imm_struct *dev, struct scsi_cmnd *cmd)
  690. {
  691. unsigned short ppb = dev->base;
  692. unsigned char l = 0, h = 0;
  693. int retv, x;
  694. /* First check for any errors that may have occurred
  695. * Here we check for internal errors
  696. */
  697. if (dev->failed)
  698. return 0;
  699. switch (cmd->SCp.phase) {
  700. case 0: /* Phase 0 - Waiting for parport */
  701. if (time_after(jiffies, dev->jstart + HZ)) {
  702. /*
  703. * We waited more than a second
  704. * for parport to call us
  705. */
  706. imm_fail(dev, DID_BUS_BUSY);
  707. return 0;
  708. }
  709. return 1; /* wait until imm_wakeup claims parport */
  710. /* Phase 1 - Connected */
  711. case 1:
  712. imm_connect(dev, CONNECT_EPP_MAYBE);
  713. cmd->SCp.phase++;
  714. /* Phase 2 - We are now talking to the scsi bus */
  715. case 2:
  716. if (!imm_select(dev, scmd_id(cmd))) {
  717. imm_fail(dev, DID_NO_CONNECT);
  718. return 0;
  719. }
  720. cmd->SCp.phase++;
  721. /* Phase 3 - Ready to accept a command */
  722. case 3:
  723. w_ctr(ppb, 0x0c);
  724. if (!(r_str(ppb) & 0x80))
  725. return 1;
  726. if (!imm_send_command(cmd))
  727. return 0;
  728. cmd->SCp.phase++;
  729. /* Phase 4 - Setup scatter/gather buffers */
  730. case 4:
  731. if (scsi_bufflen(cmd)) {
  732. cmd->SCp.buffer = scsi_sglist(cmd);
  733. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  734. cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
  735. } else {
  736. cmd->SCp.buffer = NULL;
  737. cmd->SCp.this_residual = 0;
  738. cmd->SCp.ptr = NULL;
  739. }
  740. cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
  741. cmd->SCp.phase++;
  742. if (cmd->SCp.this_residual & 0x01)
  743. cmd->SCp.this_residual++;
  744. /* Phase 5 - Pre-Data transfer stage */
  745. case 5:
  746. /* Spin lock for BUSY */
  747. w_ctr(ppb, 0x0c);
  748. if (!(r_str(ppb) & 0x80))
  749. return 1;
  750. /* Require negotiation for read requests */
  751. x = (r_str(ppb) & 0xb8);
  752. dev->rd = (x & 0x10) ? 1 : 0;
  753. dev->dp = (x & 0x20) ? 0 : 1;
  754. if ((dev->dp) && (dev->rd))
  755. if (imm_negotiate(dev))
  756. return 0;
  757. cmd->SCp.phase++;
  758. /* Phase 6 - Data transfer stage */
  759. case 6:
  760. /* Spin lock for BUSY */
  761. w_ctr(ppb, 0x0c);
  762. if (!(r_str(ppb) & 0x80))
  763. return 1;
  764. if (dev->dp) {
  765. retv = imm_completion(cmd);
  766. if (retv == -1)
  767. return 0;
  768. if (retv == 0)
  769. return 1;
  770. }
  771. cmd->SCp.phase++;
  772. /* Phase 7 - Post data transfer stage */
  773. case 7:
  774. if ((dev->dp) && (dev->rd)) {
  775. if ((dev->mode == IMM_NIBBLE) || (dev->mode == IMM_PS2)) {
  776. w_ctr(ppb, 0x4);
  777. w_ctr(ppb, 0xc);
  778. w_ctr(ppb, 0xe);
  779. w_ctr(ppb, 0x4);
  780. }
  781. }
  782. cmd->SCp.phase++;
  783. /* Phase 8 - Read status/message */
  784. case 8:
  785. /* Check for data overrun */
  786. if (imm_wait(dev) != (unsigned char) 0xb8) {
  787. imm_fail(dev, DID_ERROR);
  788. return 0;
  789. }
  790. if (imm_negotiate(dev))
  791. return 0;
  792. if (imm_in(dev, &l, 1)) { /* read status byte */
  793. /* Check for optional message byte */
  794. if (imm_wait(dev) == (unsigned char) 0xb8)
  795. imm_in(dev, &h, 1);
  796. cmd->result = (DID_OK << 16) + (l & STATUS_MASK);
  797. }
  798. if ((dev->mode == IMM_NIBBLE) || (dev->mode == IMM_PS2)) {
  799. w_ctr(ppb, 0x4);
  800. w_ctr(ppb, 0xc);
  801. w_ctr(ppb, 0xe);
  802. w_ctr(ppb, 0x4);
  803. }
  804. return 0; /* Finished */
  805. break;
  806. default:
  807. printk("imm: Invalid scsi phase\n");
  808. }
  809. return 0;
  810. }
  811. static int imm_queuecommand_lck(struct scsi_cmnd *cmd,
  812. void (*done)(struct scsi_cmnd *))
  813. {
  814. imm_struct *dev = imm_dev(cmd->device->host);
  815. if (dev->cur_cmd) {
  816. printk("IMM: bug in imm_queuecommand\n");
  817. return 0;
  818. }
  819. dev->failed = 0;
  820. dev->jstart = jiffies;
  821. dev->cur_cmd = cmd;
  822. cmd->scsi_done = done;
  823. cmd->result = DID_ERROR << 16; /* default return code */
  824. cmd->SCp.phase = 0; /* bus free */
  825. schedule_delayed_work(&dev->imm_tq, 0);
  826. imm_pb_claim(dev);
  827. return 0;
  828. }
  829. static DEF_SCSI_QCMD(imm_queuecommand)
  830. /*
  831. * Apparently the disk->capacity attribute is off by 1 sector
  832. * for all disk drives. We add the one here, but it should really
  833. * be done in sd.c. Even if it gets fixed there, this will still
  834. * work.
  835. */
  836. static int imm_biosparam(struct scsi_device *sdev, struct block_device *dev,
  837. sector_t capacity, int ip[])
  838. {
  839. ip[0] = 0x40;
  840. ip[1] = 0x20;
  841. ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
  842. if (ip[2] > 1024) {
  843. ip[0] = 0xff;
  844. ip[1] = 0x3f;
  845. ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
  846. }
  847. return 0;
  848. }
  849. static int imm_abort(struct scsi_cmnd *cmd)
  850. {
  851. imm_struct *dev = imm_dev(cmd->device->host);
  852. /*
  853. * There is no method for aborting commands since Iomega
  854. * have tied the SCSI_MESSAGE line high in the interface
  855. */
  856. switch (cmd->SCp.phase) {
  857. case 0: /* Do not have access to parport */
  858. case 1: /* Have not connected to interface */
  859. dev->cur_cmd = NULL; /* Forget the problem */
  860. return SUCCESS;
  861. break;
  862. default: /* SCSI command sent, can not abort */
  863. return FAILED;
  864. break;
  865. }
  866. }
  867. static void imm_reset_pulse(unsigned int base)
  868. {
  869. w_ctr(base, 0x04);
  870. w_dtr(base, 0x40);
  871. udelay(1);
  872. w_ctr(base, 0x0c);
  873. w_ctr(base, 0x0d);
  874. udelay(50);
  875. w_ctr(base, 0x0c);
  876. w_ctr(base, 0x04);
  877. }
  878. static int imm_reset(struct scsi_cmnd *cmd)
  879. {
  880. imm_struct *dev = imm_dev(cmd->device->host);
  881. if (cmd->SCp.phase)
  882. imm_disconnect(dev);
  883. dev->cur_cmd = NULL; /* Forget the problem */
  884. imm_connect(dev, CONNECT_NORMAL);
  885. imm_reset_pulse(dev->base);
  886. mdelay(1); /* device settle delay */
  887. imm_disconnect(dev);
  888. mdelay(1); /* device settle delay */
  889. return SUCCESS;
  890. }
  891. static int device_check(imm_struct *dev)
  892. {
  893. /* This routine looks for a device and then attempts to use EPP
  894. to send a command. If all goes as planned then EPP is available. */
  895. static char cmd[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  896. int loop, old_mode, status, k, ppb = dev->base;
  897. unsigned char l;
  898. old_mode = dev->mode;
  899. for (loop = 0; loop < 8; loop++) {
  900. /* Attempt to use EPP for Test Unit Ready */
  901. if ((ppb & 0x0007) == 0x0000)
  902. dev->mode = IMM_EPP_32;
  903. second_pass:
  904. imm_connect(dev, CONNECT_EPP_MAYBE);
  905. /* Select SCSI device */
  906. if (!imm_select(dev, loop)) {
  907. imm_disconnect(dev);
  908. continue;
  909. }
  910. printk("imm: Found device at ID %i, Attempting to use %s\n",
  911. loop, IMM_MODE_STRING[dev->mode]);
  912. /* Send SCSI command */
  913. status = 1;
  914. w_ctr(ppb, 0x0c);
  915. for (l = 0; (l < 3) && (status); l++)
  916. status = imm_out(dev, &cmd[l << 1], 2);
  917. if (!status) {
  918. imm_disconnect(dev);
  919. imm_connect(dev, CONNECT_EPP_MAYBE);
  920. imm_reset_pulse(dev->base);
  921. udelay(1000);
  922. imm_disconnect(dev);
  923. udelay(1000);
  924. if (dev->mode == IMM_EPP_32) {
  925. dev->mode = old_mode;
  926. goto second_pass;
  927. }
  928. printk("imm: Unable to establish communication\n");
  929. return -EIO;
  930. }
  931. w_ctr(ppb, 0x0c);
  932. k = 1000000; /* 1 Second */
  933. do {
  934. l = r_str(ppb);
  935. k--;
  936. udelay(1);
  937. } while (!(l & 0x80) && (k));
  938. l &= 0xb8;
  939. if (l != 0xb8) {
  940. imm_disconnect(dev);
  941. imm_connect(dev, CONNECT_EPP_MAYBE);
  942. imm_reset_pulse(dev->base);
  943. udelay(1000);
  944. imm_disconnect(dev);
  945. udelay(1000);
  946. if (dev->mode == IMM_EPP_32) {
  947. dev->mode = old_mode;
  948. goto second_pass;
  949. }
  950. printk
  951. ("imm: Unable to establish communication\n");
  952. return -EIO;
  953. }
  954. imm_disconnect(dev);
  955. printk
  956. ("imm: Communication established at 0x%x with ID %i using %s\n",
  957. ppb, loop, IMM_MODE_STRING[dev->mode]);
  958. imm_connect(dev, CONNECT_EPP_MAYBE);
  959. imm_reset_pulse(dev->base);
  960. udelay(1000);
  961. imm_disconnect(dev);
  962. udelay(1000);
  963. return 0;
  964. }
  965. printk("imm: No devices found\n");
  966. return -ENODEV;
  967. }
  968. /*
  969. * imm cannot deal with highmem, so this causes all IO pages for this host
  970. * to reside in low memory (hence mapped)
  971. */
  972. static int imm_adjust_queue(struct scsi_device *device)
  973. {
  974. blk_queue_bounce_limit(device->request_queue, BLK_BOUNCE_HIGH);
  975. return 0;
  976. }
  977. static struct scsi_host_template imm_template = {
  978. .module = THIS_MODULE,
  979. .proc_name = "imm",
  980. .show_info = imm_show_info,
  981. .write_info = imm_write_info,
  982. .name = "Iomega VPI2 (imm) interface",
  983. .queuecommand = imm_queuecommand,
  984. .eh_abort_handler = imm_abort,
  985. .eh_bus_reset_handler = imm_reset,
  986. .eh_host_reset_handler = imm_reset,
  987. .bios_param = imm_biosparam,
  988. .this_id = 7,
  989. .sg_tablesize = SG_ALL,
  990. .use_clustering = ENABLE_CLUSTERING,
  991. .can_queue = 1,
  992. .slave_alloc = imm_adjust_queue,
  993. };
  994. /***************************************************************************
  995. * Parallel port probing routines *
  996. ***************************************************************************/
  997. static LIST_HEAD(imm_hosts);
  998. static int __imm_attach(struct parport *pb)
  999. {
  1000. struct Scsi_Host *host;
  1001. imm_struct *dev;
  1002. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waiting);
  1003. DEFINE_WAIT(wait);
  1004. int ports;
  1005. int modes, ppb;
  1006. int err = -ENOMEM;
  1007. init_waitqueue_head(&waiting);
  1008. dev = kzalloc(sizeof(imm_struct), GFP_KERNEL);
  1009. if (!dev)
  1010. return -ENOMEM;
  1011. dev->base = -1;
  1012. dev->mode = IMM_AUTODETECT;
  1013. INIT_LIST_HEAD(&dev->list);
  1014. dev->dev = parport_register_device(pb, "imm", NULL, imm_wakeup,
  1015. NULL, 0, dev);
  1016. if (!dev->dev)
  1017. goto out;
  1018. /* Claim the bus so it remembers what we do to the control
  1019. * registers. [ CTR and ECP ]
  1020. */
  1021. err = -EBUSY;
  1022. dev->waiting = &waiting;
  1023. prepare_to_wait(&waiting, &wait, TASK_UNINTERRUPTIBLE);
  1024. if (imm_pb_claim(dev))
  1025. schedule_timeout(3 * HZ);
  1026. if (dev->wanted) {
  1027. printk(KERN_ERR "imm%d: failed to claim parport because "
  1028. "a pardevice is owning the port for too long "
  1029. "time!\n", pb->number);
  1030. imm_pb_dismiss(dev);
  1031. dev->waiting = NULL;
  1032. finish_wait(&waiting, &wait);
  1033. goto out1;
  1034. }
  1035. dev->waiting = NULL;
  1036. finish_wait(&waiting, &wait);
  1037. ppb = dev->base = dev->dev->port->base;
  1038. dev->base_hi = dev->dev->port->base_hi;
  1039. w_ctr(ppb, 0x0c);
  1040. modes = dev->dev->port->modes;
  1041. /* Mode detection works up the chain of speed
  1042. * This avoids a nasty if-then-else-if-... tree
  1043. */
  1044. dev->mode = IMM_NIBBLE;
  1045. if (modes & PARPORT_MODE_TRISTATE)
  1046. dev->mode = IMM_PS2;
  1047. /* Done configuration */
  1048. err = imm_init(dev);
  1049. imm_pb_release(dev);
  1050. if (err)
  1051. goto out1;
  1052. /* now the glue ... */
  1053. if (dev->mode == IMM_NIBBLE || dev->mode == IMM_PS2)
  1054. ports = 3;
  1055. else
  1056. ports = 8;
  1057. INIT_DELAYED_WORK(&dev->imm_tq, imm_interrupt);
  1058. err = -ENOMEM;
  1059. host = scsi_host_alloc(&imm_template, sizeof(imm_struct *));
  1060. if (!host)
  1061. goto out1;
  1062. host->io_port = pb->base;
  1063. host->n_io_port = ports;
  1064. host->dma_channel = -1;
  1065. host->unique_id = pb->number;
  1066. *(imm_struct **)&host->hostdata = dev;
  1067. dev->host = host;
  1068. list_add_tail(&dev->list, &imm_hosts);
  1069. err = scsi_add_host(host, NULL);
  1070. if (err)
  1071. goto out2;
  1072. scsi_scan_host(host);
  1073. return 0;
  1074. out2:
  1075. list_del_init(&dev->list);
  1076. scsi_host_put(host);
  1077. out1:
  1078. parport_unregister_device(dev->dev);
  1079. out:
  1080. kfree(dev);
  1081. return err;
  1082. }
  1083. static void imm_attach(struct parport *pb)
  1084. {
  1085. __imm_attach(pb);
  1086. }
  1087. static void imm_detach(struct parport *pb)
  1088. {
  1089. imm_struct *dev;
  1090. list_for_each_entry(dev, &imm_hosts, list) {
  1091. if (dev->dev->port == pb) {
  1092. list_del_init(&dev->list);
  1093. scsi_remove_host(dev->host);
  1094. scsi_host_put(dev->host);
  1095. parport_unregister_device(dev->dev);
  1096. kfree(dev);
  1097. break;
  1098. }
  1099. }
  1100. }
  1101. static struct parport_driver imm_driver = {
  1102. .name = "imm",
  1103. .attach = imm_attach,
  1104. .detach = imm_detach,
  1105. };
  1106. static int __init imm_driver_init(void)
  1107. {
  1108. printk("imm: Version %s\n", IMM_VERSION);
  1109. return parport_register_driver(&imm_driver);
  1110. }
  1111. static void __exit imm_driver_exit(void)
  1112. {
  1113. parport_unregister_driver(&imm_driver);
  1114. }
  1115. module_init(imm_driver_init);
  1116. module_exit(imm_driver_exit);
  1117. MODULE_LICENSE("GPL");