imm.c 29 KB

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