imm.c 29 KB

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