imm.c 29 KB

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