imm.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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 delayed_work 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, __func__, __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 = sg_virt(cmd->SCp.buffer);
  629. /*
  630. * Make sure that we transfer even number of bytes
  631. * otherwise it makes imm_byte_out() messy.
  632. */
  633. if (cmd->SCp.this_residual & 0x01)
  634. cmd->SCp.this_residual++;
  635. }
  636. }
  637. /* Now check to see if the drive is ready to comunicate */
  638. w_ctr(ppb, 0x0c);
  639. r = (r_str(ppb) & 0xb8);
  640. /* If not, drop back down to the scheduler and wait a timer tick */
  641. if (!(r & 0x80))
  642. return 0;
  643. }
  644. return 1; /* FINISH_RETURN */
  645. }
  646. /*
  647. * Since the IMM itself doesn't generate interrupts, we use
  648. * the scheduler's task queue to generate a stream of call-backs and
  649. * complete the request when the drive is ready.
  650. */
  651. static void imm_interrupt(struct work_struct *work)
  652. {
  653. imm_struct *dev = container_of(work, imm_struct, imm_tq.work);
  654. struct scsi_cmnd *cmd = dev->cur_cmd;
  655. struct Scsi_Host *host = cmd->device->host;
  656. unsigned long flags;
  657. if (imm_engine(dev, cmd)) {
  658. schedule_delayed_work(&dev->imm_tq, 1);
  659. return;
  660. }
  661. /* Command must of completed hence it is safe to let go... */
  662. #if IMM_DEBUG > 0
  663. switch ((cmd->result >> 16) & 0xff) {
  664. case DID_OK:
  665. break;
  666. case DID_NO_CONNECT:
  667. printk("imm: no device at SCSI ID %i\n", cmd->device->id);
  668. break;
  669. case DID_BUS_BUSY:
  670. printk("imm: BUS BUSY - EPP timeout detected\n");
  671. break;
  672. case DID_TIME_OUT:
  673. printk("imm: unknown timeout\n");
  674. break;
  675. case DID_ABORT:
  676. printk("imm: told to abort\n");
  677. break;
  678. case DID_PARITY:
  679. printk("imm: parity error (???)\n");
  680. break;
  681. case DID_ERROR:
  682. printk("imm: internal driver error\n");
  683. break;
  684. case DID_RESET:
  685. printk("imm: told to reset device\n");
  686. break;
  687. case DID_BAD_INTR:
  688. printk("imm: bad interrupt (???)\n");
  689. break;
  690. default:
  691. printk("imm: bad return code (%02x)\n",
  692. (cmd->result >> 16) & 0xff);
  693. }
  694. #endif
  695. if (cmd->SCp.phase > 1)
  696. imm_disconnect(dev);
  697. imm_pb_dismiss(dev);
  698. spin_lock_irqsave(host->host_lock, flags);
  699. dev->cur_cmd = NULL;
  700. cmd->scsi_done(cmd);
  701. spin_unlock_irqrestore(host->host_lock, flags);
  702. return;
  703. }
  704. static int imm_engine(imm_struct *dev, struct scsi_cmnd *cmd)
  705. {
  706. unsigned short ppb = dev->base;
  707. unsigned char l = 0, h = 0;
  708. int retv, x;
  709. /* First check for any errors that may have occurred
  710. * Here we check for internal errors
  711. */
  712. if (dev->failed)
  713. return 0;
  714. switch (cmd->SCp.phase) {
  715. case 0: /* Phase 0 - Waiting for parport */
  716. if (time_after(jiffies, dev->jstart + HZ)) {
  717. /*
  718. * We waited more than a second
  719. * for parport to call us
  720. */
  721. imm_fail(dev, DID_BUS_BUSY);
  722. return 0;
  723. }
  724. return 1; /* wait until imm_wakeup claims parport */
  725. /* Phase 1 - Connected */
  726. case 1:
  727. imm_connect(dev, CONNECT_EPP_MAYBE);
  728. cmd->SCp.phase++;
  729. /* Phase 2 - We are now talking to the scsi bus */
  730. case 2:
  731. if (!imm_select(dev, scmd_id(cmd))) {
  732. imm_fail(dev, DID_NO_CONNECT);
  733. return 0;
  734. }
  735. cmd->SCp.phase++;
  736. /* Phase 3 - Ready to accept a command */
  737. case 3:
  738. w_ctr(ppb, 0x0c);
  739. if (!(r_str(ppb) & 0x80))
  740. return 1;
  741. if (!imm_send_command(cmd))
  742. return 0;
  743. cmd->SCp.phase++;
  744. /* Phase 4 - Setup scatter/gather buffers */
  745. case 4:
  746. if (scsi_bufflen(cmd)) {
  747. cmd->SCp.buffer = scsi_sglist(cmd);
  748. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  749. cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
  750. } else {
  751. cmd->SCp.buffer = NULL;
  752. cmd->SCp.this_residual = 0;
  753. cmd->SCp.ptr = NULL;
  754. }
  755. cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
  756. cmd->SCp.phase++;
  757. if (cmd->SCp.this_residual & 0x01)
  758. cmd->SCp.this_residual++;
  759. /* Phase 5 - Pre-Data transfer stage */
  760. case 5:
  761. /* Spin lock for BUSY */
  762. w_ctr(ppb, 0x0c);
  763. if (!(r_str(ppb) & 0x80))
  764. return 1;
  765. /* Require negotiation for read requests */
  766. x = (r_str(ppb) & 0xb8);
  767. dev->rd = (x & 0x10) ? 1 : 0;
  768. dev->dp = (x & 0x20) ? 0 : 1;
  769. if ((dev->dp) && (dev->rd))
  770. if (imm_negotiate(dev))
  771. return 0;
  772. cmd->SCp.phase++;
  773. /* Phase 6 - Data transfer stage */
  774. case 6:
  775. /* Spin lock for BUSY */
  776. w_ctr(ppb, 0x0c);
  777. if (!(r_str(ppb) & 0x80))
  778. return 1;
  779. if (dev->dp) {
  780. retv = imm_completion(cmd);
  781. if (retv == -1)
  782. return 0;
  783. if (retv == 0)
  784. return 1;
  785. }
  786. cmd->SCp.phase++;
  787. /* Phase 7 - Post data transfer stage */
  788. case 7:
  789. if ((dev->dp) && (dev->rd)) {
  790. if ((dev->mode == IMM_NIBBLE) || (dev->mode == IMM_PS2)) {
  791. w_ctr(ppb, 0x4);
  792. w_ctr(ppb, 0xc);
  793. w_ctr(ppb, 0xe);
  794. w_ctr(ppb, 0x4);
  795. }
  796. }
  797. cmd->SCp.phase++;
  798. /* Phase 8 - Read status/message */
  799. case 8:
  800. /* Check for data overrun */
  801. if (imm_wait(dev) != (unsigned char) 0xb8) {
  802. imm_fail(dev, DID_ERROR);
  803. return 0;
  804. }
  805. if (imm_negotiate(dev))
  806. return 0;
  807. if (imm_in(dev, &l, 1)) { /* read status byte */
  808. /* Check for optional message byte */
  809. if (imm_wait(dev) == (unsigned char) 0xb8)
  810. imm_in(dev, &h, 1);
  811. cmd->result = (DID_OK << 16) + (l & STATUS_MASK);
  812. }
  813. if ((dev->mode == IMM_NIBBLE) || (dev->mode == IMM_PS2)) {
  814. w_ctr(ppb, 0x4);
  815. w_ctr(ppb, 0xc);
  816. w_ctr(ppb, 0xe);
  817. w_ctr(ppb, 0x4);
  818. }
  819. return 0; /* Finished */
  820. break;
  821. default:
  822. printk("imm: Invalid scsi phase\n");
  823. }
  824. return 0;
  825. }
  826. static int imm_queuecommand(struct scsi_cmnd *cmd,
  827. void (*done)(struct scsi_cmnd *))
  828. {
  829. imm_struct *dev = imm_dev(cmd->device->host);
  830. if (dev->cur_cmd) {
  831. printk("IMM: bug in imm_queuecommand\n");
  832. return 0;
  833. }
  834. dev->failed = 0;
  835. dev->jstart = jiffies;
  836. dev->cur_cmd = cmd;
  837. cmd->scsi_done = done;
  838. cmd->result = DID_ERROR << 16; /* default return code */
  839. cmd->SCp.phase = 0; /* bus free */
  840. schedule_delayed_work(&dev->imm_tq, 0);
  841. imm_pb_claim(dev);
  842. return 0;
  843. }
  844. /*
  845. * Apparently the disk->capacity attribute is off by 1 sector
  846. * for all disk drives. We add the one here, but it should really
  847. * be done in sd.c. Even if it gets fixed there, this will still
  848. * work.
  849. */
  850. static int imm_biosparam(struct scsi_device *sdev, struct block_device *dev,
  851. sector_t capacity, int ip[])
  852. {
  853. ip[0] = 0x40;
  854. ip[1] = 0x20;
  855. ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
  856. if (ip[2] > 1024) {
  857. ip[0] = 0xff;
  858. ip[1] = 0x3f;
  859. ip[2] = ((unsigned long) capacity + 1) / (ip[0] * ip[1]);
  860. }
  861. return 0;
  862. }
  863. static int imm_abort(struct scsi_cmnd *cmd)
  864. {
  865. imm_struct *dev = imm_dev(cmd->device->host);
  866. /*
  867. * There is no method for aborting commands since Iomega
  868. * have tied the SCSI_MESSAGE line high in the interface
  869. */
  870. switch (cmd->SCp.phase) {
  871. case 0: /* Do not have access to parport */
  872. case 1: /* Have not connected to interface */
  873. dev->cur_cmd = NULL; /* Forget the problem */
  874. return SUCCESS;
  875. break;
  876. default: /* SCSI command sent, can not abort */
  877. return FAILED;
  878. break;
  879. }
  880. }
  881. static void imm_reset_pulse(unsigned int base)
  882. {
  883. w_ctr(base, 0x04);
  884. w_dtr(base, 0x40);
  885. udelay(1);
  886. w_ctr(base, 0x0c);
  887. w_ctr(base, 0x0d);
  888. udelay(50);
  889. w_ctr(base, 0x0c);
  890. w_ctr(base, 0x04);
  891. }
  892. static int imm_reset(struct scsi_cmnd *cmd)
  893. {
  894. imm_struct *dev = imm_dev(cmd->device->host);
  895. if (cmd->SCp.phase)
  896. imm_disconnect(dev);
  897. dev->cur_cmd = NULL; /* Forget the problem */
  898. imm_connect(dev, CONNECT_NORMAL);
  899. imm_reset_pulse(dev->base);
  900. mdelay(1); /* device settle delay */
  901. imm_disconnect(dev);
  902. mdelay(1); /* device settle delay */
  903. return SUCCESS;
  904. }
  905. static int device_check(imm_struct *dev)
  906. {
  907. /* This routine looks for a device and then attempts to use EPP
  908. to send a command. If all goes as planned then EPP is available. */
  909. static char cmd[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  910. int loop, old_mode, status, k, ppb = dev->base;
  911. unsigned char l;
  912. old_mode = dev->mode;
  913. for (loop = 0; loop < 8; loop++) {
  914. /* Attempt to use EPP for Test Unit Ready */
  915. if ((ppb & 0x0007) == 0x0000)
  916. dev->mode = IMM_EPP_32;
  917. second_pass:
  918. imm_connect(dev, CONNECT_EPP_MAYBE);
  919. /* Select SCSI device */
  920. if (!imm_select(dev, loop)) {
  921. imm_disconnect(dev);
  922. continue;
  923. }
  924. printk("imm: Found device at ID %i, Attempting to use %s\n",
  925. loop, IMM_MODE_STRING[dev->mode]);
  926. /* Send SCSI command */
  927. status = 1;
  928. w_ctr(ppb, 0x0c);
  929. for (l = 0; (l < 3) && (status); l++)
  930. status = imm_out(dev, &cmd[l << 1], 2);
  931. if (!status) {
  932. imm_disconnect(dev);
  933. imm_connect(dev, CONNECT_EPP_MAYBE);
  934. imm_reset_pulse(dev->base);
  935. udelay(1000);
  936. imm_disconnect(dev);
  937. udelay(1000);
  938. if (dev->mode == IMM_EPP_32) {
  939. dev->mode = old_mode;
  940. goto second_pass;
  941. }
  942. printk("imm: Unable to establish communication\n");
  943. return -EIO;
  944. }
  945. w_ctr(ppb, 0x0c);
  946. k = 1000000; /* 1 Second */
  947. do {
  948. l = r_str(ppb);
  949. k--;
  950. udelay(1);
  951. } while (!(l & 0x80) && (k));
  952. l &= 0xb8;
  953. if (l != 0xb8) {
  954. imm_disconnect(dev);
  955. imm_connect(dev, CONNECT_EPP_MAYBE);
  956. imm_reset_pulse(dev->base);
  957. udelay(1000);
  958. imm_disconnect(dev);
  959. udelay(1000);
  960. if (dev->mode == IMM_EPP_32) {
  961. dev->mode = old_mode;
  962. goto second_pass;
  963. }
  964. printk
  965. ("imm: Unable to establish communication\n");
  966. return -EIO;
  967. }
  968. imm_disconnect(dev);
  969. printk
  970. ("imm: Communication established at 0x%x with ID %i using %s\n",
  971. ppb, loop, IMM_MODE_STRING[dev->mode]);
  972. imm_connect(dev, CONNECT_EPP_MAYBE);
  973. imm_reset_pulse(dev->base);
  974. udelay(1000);
  975. imm_disconnect(dev);
  976. udelay(1000);
  977. return 0;
  978. }
  979. printk("imm: No devices found\n");
  980. return -ENODEV;
  981. }
  982. /*
  983. * imm cannot deal with highmem, so this causes all IO pages for this host
  984. * to reside in low memory (hence mapped)
  985. */
  986. static int imm_adjust_queue(struct scsi_device *device)
  987. {
  988. blk_queue_bounce_limit(device->request_queue, BLK_BOUNCE_HIGH);
  989. return 0;
  990. }
  991. static struct scsi_host_template imm_template = {
  992. .module = THIS_MODULE,
  993. .proc_name = "imm",
  994. .proc_info = imm_proc_info,
  995. .name = "Iomega VPI2 (imm) interface",
  996. .queuecommand = imm_queuecommand,
  997. .eh_abort_handler = imm_abort,
  998. .eh_bus_reset_handler = imm_reset,
  999. .eh_host_reset_handler = imm_reset,
  1000. .bios_param = imm_biosparam,
  1001. .this_id = 7,
  1002. .sg_tablesize = SG_ALL,
  1003. .cmd_per_lun = 1,
  1004. .use_clustering = ENABLE_CLUSTERING,
  1005. .can_queue = 1,
  1006. .slave_alloc = imm_adjust_queue,
  1007. };
  1008. /***************************************************************************
  1009. * Parallel port probing routines *
  1010. ***************************************************************************/
  1011. static LIST_HEAD(imm_hosts);
  1012. static int __imm_attach(struct parport *pb)
  1013. {
  1014. struct Scsi_Host *host;
  1015. imm_struct *dev;
  1016. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waiting);
  1017. DEFINE_WAIT(wait);
  1018. int ports;
  1019. int modes, ppb;
  1020. int err = -ENOMEM;
  1021. init_waitqueue_head(&waiting);
  1022. dev = kzalloc(sizeof(imm_struct), GFP_KERNEL);
  1023. if (!dev)
  1024. return -ENOMEM;
  1025. dev->base = -1;
  1026. dev->mode = IMM_AUTODETECT;
  1027. INIT_LIST_HEAD(&dev->list);
  1028. dev->dev = parport_register_device(pb, "imm", NULL, imm_wakeup,
  1029. NULL, 0, dev);
  1030. if (!dev->dev)
  1031. goto out;
  1032. /* Claim the bus so it remembers what we do to the control
  1033. * registers. [ CTR and ECP ]
  1034. */
  1035. err = -EBUSY;
  1036. dev->waiting = &waiting;
  1037. prepare_to_wait(&waiting, &wait, TASK_UNINTERRUPTIBLE);
  1038. if (imm_pb_claim(dev))
  1039. schedule_timeout(3 * HZ);
  1040. if (dev->wanted) {
  1041. printk(KERN_ERR "imm%d: failed to claim parport because "
  1042. "a pardevice is owning the port for too long "
  1043. "time!\n", pb->number);
  1044. imm_pb_dismiss(dev);
  1045. dev->waiting = NULL;
  1046. finish_wait(&waiting, &wait);
  1047. goto out1;
  1048. }
  1049. dev->waiting = NULL;
  1050. finish_wait(&waiting, &wait);
  1051. ppb = dev->base = dev->dev->port->base;
  1052. dev->base_hi = dev->dev->port->base_hi;
  1053. w_ctr(ppb, 0x0c);
  1054. modes = dev->dev->port->modes;
  1055. /* Mode detection works up the chain of speed
  1056. * This avoids a nasty if-then-else-if-... tree
  1057. */
  1058. dev->mode = IMM_NIBBLE;
  1059. if (modes & PARPORT_MODE_TRISTATE)
  1060. dev->mode = IMM_PS2;
  1061. /* Done configuration */
  1062. err = imm_init(dev);
  1063. imm_pb_release(dev);
  1064. if (err)
  1065. goto out1;
  1066. /* now the glue ... */
  1067. if (dev->mode == IMM_NIBBLE || dev->mode == IMM_PS2)
  1068. ports = 3;
  1069. else
  1070. ports = 8;
  1071. INIT_DELAYED_WORK(&dev->imm_tq, imm_interrupt);
  1072. err = -ENOMEM;
  1073. host = scsi_host_alloc(&imm_template, sizeof(imm_struct *));
  1074. if (!host)
  1075. goto out1;
  1076. host->io_port = pb->base;
  1077. host->n_io_port = ports;
  1078. host->dma_channel = -1;
  1079. host->unique_id = pb->number;
  1080. *(imm_struct **)&host->hostdata = dev;
  1081. dev->host = host;
  1082. list_add_tail(&dev->list, &imm_hosts);
  1083. err = scsi_add_host(host, NULL);
  1084. if (err)
  1085. goto out2;
  1086. scsi_scan_host(host);
  1087. return 0;
  1088. out2:
  1089. list_del_init(&dev->list);
  1090. scsi_host_put(host);
  1091. out1:
  1092. parport_unregister_device(dev->dev);
  1093. out:
  1094. kfree(dev);
  1095. return err;
  1096. }
  1097. static void imm_attach(struct parport *pb)
  1098. {
  1099. __imm_attach(pb);
  1100. }
  1101. static void imm_detach(struct parport *pb)
  1102. {
  1103. imm_struct *dev;
  1104. list_for_each_entry(dev, &imm_hosts, list) {
  1105. if (dev->dev->port == pb) {
  1106. list_del_init(&dev->list);
  1107. scsi_remove_host(dev->host);
  1108. scsi_host_put(dev->host);
  1109. parport_unregister_device(dev->dev);
  1110. kfree(dev);
  1111. break;
  1112. }
  1113. }
  1114. }
  1115. static struct parport_driver imm_driver = {
  1116. .name = "imm",
  1117. .attach = imm_attach,
  1118. .detach = imm_detach,
  1119. };
  1120. static int __init imm_driver_init(void)
  1121. {
  1122. printk("imm: Version %s\n", IMM_VERSION);
  1123. return parport_register_driver(&imm_driver);
  1124. }
  1125. static void __exit imm_driver_exit(void)
  1126. {
  1127. parport_unregister_driver(&imm_driver);
  1128. }
  1129. module_init(imm_driver_init);
  1130. module_exit(imm_driver_exit);
  1131. MODULE_LICENSE("GPL");