imm.c 29 KB

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