imm.c 30 KB

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