imm.c 29 KB

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