imm.c 29 KB

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