imm.c 29 KB

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