imm.c 30 KB

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