imm.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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_bus_reset_handler = imm_reset,
  988. .eh_host_reset_handler = imm_reset,
  989. .bios_param = imm_biosparam,
  990. .this_id = 7,
  991. .sg_tablesize = SG_ALL,
  992. .use_clustering = ENABLE_CLUSTERING,
  993. .can_queue = 1,
  994. .slave_alloc = imm_adjust_queue,
  995. };
  996. /***************************************************************************
  997. * Parallel port probing routines *
  998. ***************************************************************************/
  999. static LIST_HEAD(imm_hosts);
  1000. /*
  1001. * Finds the first available device number that can be alloted to the
  1002. * new imm device and returns the address of the previous node so that
  1003. * we can add to the tail and have a list in the ascending order.
  1004. */
  1005. static inline imm_struct *find_parent(void)
  1006. {
  1007. imm_struct *dev, *par = NULL;
  1008. unsigned int cnt = 0;
  1009. if (list_empty(&imm_hosts))
  1010. return NULL;
  1011. list_for_each_entry(dev, &imm_hosts, list) {
  1012. if (dev->dev_no != cnt)
  1013. return par;
  1014. cnt++;
  1015. par = dev;
  1016. }
  1017. return par;
  1018. }
  1019. static int __imm_attach(struct parport *pb)
  1020. {
  1021. struct Scsi_Host *host;
  1022. imm_struct *dev, *temp;
  1023. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waiting);
  1024. DEFINE_WAIT(wait);
  1025. int ports;
  1026. int modes, ppb;
  1027. int err = -ENOMEM;
  1028. struct pardev_cb imm_cb;
  1029. init_waitqueue_head(&waiting);
  1030. dev = kzalloc(sizeof(imm_struct), GFP_KERNEL);
  1031. if (!dev)
  1032. return -ENOMEM;
  1033. dev->base = -1;
  1034. dev->mode = IMM_AUTODETECT;
  1035. INIT_LIST_HEAD(&dev->list);
  1036. temp = find_parent();
  1037. if (temp)
  1038. dev->dev_no = temp->dev_no + 1;
  1039. memset(&imm_cb, 0, sizeof(imm_cb));
  1040. imm_cb.private = dev;
  1041. imm_cb.wakeup = imm_wakeup;
  1042. dev->dev = parport_register_dev_model(pb, "imm", &imm_cb, dev->dev_no);
  1043. if (!dev->dev)
  1044. goto out;
  1045. /* Claim the bus so it remembers what we do to the control
  1046. * registers. [ CTR and ECP ]
  1047. */
  1048. err = -EBUSY;
  1049. dev->waiting = &waiting;
  1050. prepare_to_wait(&waiting, &wait, TASK_UNINTERRUPTIBLE);
  1051. if (imm_pb_claim(dev))
  1052. schedule_timeout(3 * HZ);
  1053. if (dev->wanted) {
  1054. printk(KERN_ERR "imm%d: failed to claim parport because "
  1055. "a pardevice is owning the port for too long "
  1056. "time!\n", pb->number);
  1057. imm_pb_dismiss(dev);
  1058. dev->waiting = NULL;
  1059. finish_wait(&waiting, &wait);
  1060. goto out1;
  1061. }
  1062. dev->waiting = NULL;
  1063. finish_wait(&waiting, &wait);
  1064. ppb = dev->base = dev->dev->port->base;
  1065. dev->base_hi = dev->dev->port->base_hi;
  1066. w_ctr(ppb, 0x0c);
  1067. modes = dev->dev->port->modes;
  1068. /* Mode detection works up the chain of speed
  1069. * This avoids a nasty if-then-else-if-... tree
  1070. */
  1071. dev->mode = IMM_NIBBLE;
  1072. if (modes & PARPORT_MODE_TRISTATE)
  1073. dev->mode = IMM_PS2;
  1074. /* Done configuration */
  1075. err = imm_init(dev);
  1076. imm_pb_release(dev);
  1077. if (err)
  1078. goto out1;
  1079. /* now the glue ... */
  1080. if (dev->mode == IMM_NIBBLE || dev->mode == IMM_PS2)
  1081. ports = 3;
  1082. else
  1083. ports = 8;
  1084. INIT_DELAYED_WORK(&dev->imm_tq, imm_interrupt);
  1085. err = -ENOMEM;
  1086. host = scsi_host_alloc(&imm_template, sizeof(imm_struct *));
  1087. if (!host)
  1088. goto out1;
  1089. host->io_port = pb->base;
  1090. host->n_io_port = ports;
  1091. host->dma_channel = -1;
  1092. host->unique_id = pb->number;
  1093. *(imm_struct **)&host->hostdata = dev;
  1094. dev->host = host;
  1095. if (!temp)
  1096. list_add_tail(&dev->list, &imm_hosts);
  1097. else
  1098. list_add_tail(&dev->list, &temp->list);
  1099. err = scsi_add_host(host, NULL);
  1100. if (err)
  1101. goto out2;
  1102. scsi_scan_host(host);
  1103. return 0;
  1104. out2:
  1105. list_del_init(&dev->list);
  1106. scsi_host_put(host);
  1107. out1:
  1108. parport_unregister_device(dev->dev);
  1109. out:
  1110. kfree(dev);
  1111. return err;
  1112. }
  1113. static void imm_attach(struct parport *pb)
  1114. {
  1115. __imm_attach(pb);
  1116. }
  1117. static void imm_detach(struct parport *pb)
  1118. {
  1119. imm_struct *dev;
  1120. list_for_each_entry(dev, &imm_hosts, list) {
  1121. if (dev->dev->port == pb) {
  1122. list_del_init(&dev->list);
  1123. scsi_remove_host(dev->host);
  1124. scsi_host_put(dev->host);
  1125. parport_unregister_device(dev->dev);
  1126. kfree(dev);
  1127. break;
  1128. }
  1129. }
  1130. }
  1131. static struct parport_driver imm_driver = {
  1132. .name = "imm",
  1133. .match_port = imm_attach,
  1134. .detach = imm_detach,
  1135. .devmodel = true,
  1136. };
  1137. static int __init imm_driver_init(void)
  1138. {
  1139. printk("imm: Version %s\n", IMM_VERSION);
  1140. return parport_register_driver(&imm_driver);
  1141. }
  1142. static void __exit imm_driver_exit(void)
  1143. {
  1144. parport_unregister_driver(&imm_driver);
  1145. }
  1146. module_init(imm_driver_init);
  1147. module_exit(imm_driver_exit);
  1148. MODULE_LICENSE("GPL");