imm.c 29 KB

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