sym53c500_cs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /*
  2. * sym53c500_cs.c Bob Tracy (rct@frus.com)
  3. *
  4. * A rewrite of the pcmcia-cs add-on driver for newer (circa 1997)
  5. * New Media Bus Toaster PCMCIA SCSI cards using the Symbios Logic
  6. * 53c500 controller: intended for use with 2.6 and later kernels.
  7. * The pcmcia-cs add-on version of this driver is not supported
  8. * beyond 2.4. It consisted of three files with history/copyright
  9. * information as follows:
  10. *
  11. * SYM53C500.h
  12. * Bob Tracy (rct@frus.com)
  13. * Original by Tom Corner (tcorner@via.at).
  14. * Adapted from NCR53c406a.h which is Copyrighted (C) 1994
  15. * Normunds Saumanis (normunds@rx.tech.swh.lv)
  16. *
  17. * SYM53C500.c
  18. * Bob Tracy (rct@frus.com)
  19. * Original driver by Tom Corner (tcorner@via.at) was adapted
  20. * from NCR53c406a.c which is Copyrighted (C) 1994, 1995, 1996
  21. * Normunds Saumanis (normunds@fi.ibm.com)
  22. *
  23. * sym53c500.c
  24. * Bob Tracy (rct@frus.com)
  25. * Original by Tom Corner (tcorner@via.at) was adapted from a
  26. * driver for the Qlogic SCSI card written by
  27. * David Hinds (dhinds@allegro.stanford.edu).
  28. *
  29. * This program is free software; you can redistribute it and/or modify it
  30. * under the terms of the GNU General Public License as published by the
  31. * Free Software Foundation; either version 2, or (at your option) any
  32. * later version.
  33. *
  34. * This program is distributed in the hope that it will be useful, but
  35. * WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  37. * General Public License for more details.
  38. */
  39. #define SYM53C500_DEBUG 0
  40. #define VERBOSE_SYM53C500_DEBUG 0
  41. /*
  42. * Set this to 0 if you encounter kernel lockups while transferring
  43. * data in PIO mode. Note this can be changed via "sysfs".
  44. */
  45. #define USE_FAST_PIO 1
  46. /* =============== End of user configurable parameters ============== */
  47. #include <linux/module.h>
  48. #include <linux/moduleparam.h>
  49. #include <linux/errno.h>
  50. #include <linux/init.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/kernel.h>
  53. #include <linux/slab.h>
  54. #include <linux/string.h>
  55. #include <linux/ioport.h>
  56. #include <linux/blkdev.h>
  57. #include <linux/spinlock.h>
  58. #include <linux/bitops.h>
  59. #include <asm/io.h>
  60. #include <asm/dma.h>
  61. #include <asm/irq.h>
  62. #include <scsi/scsi_ioctl.h>
  63. #include <scsi/scsi_cmnd.h>
  64. #include <scsi/scsi_device.h>
  65. #include <scsi/scsi.h>
  66. #include <scsi/scsi_host.h>
  67. #include <pcmcia/cs_types.h>
  68. #include <pcmcia/cs.h>
  69. #include <pcmcia/cistpl.h>
  70. #include <pcmcia/ds.h>
  71. #include <pcmcia/ciscode.h>
  72. /* ================================================================== */
  73. #ifdef PCMCIA_DEBUG
  74. static int pc_debug = PCMCIA_DEBUG;
  75. module_param(pc_debug, int, 0);
  76. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
  77. static char *version =
  78. "sym53c500_cs.c 0.9c 2004/10/27 (Bob Tracy)";
  79. #else
  80. #define DEBUG(n, args...)
  81. #endif
  82. /* ================================================================== */
  83. #define SYNC_MODE 0 /* Synchronous transfer mode */
  84. /* Default configuration */
  85. #define C1_IMG 0x07 /* ID=7 */
  86. #define C2_IMG 0x48 /* FE SCSI2 */
  87. #define C3_IMG 0x20 /* CDB */
  88. #define C4_IMG 0x04 /* ANE */
  89. #define C5_IMG 0xa4 /* ? changed from b6= AA PI SIE POL */
  90. #define C7_IMG 0x80 /* added for SYM53C500 t. corner */
  91. /* Hardware Registers: offsets from io_port (base) */
  92. /* Control Register Set 0 */
  93. #define TC_LSB 0x00 /* transfer counter lsb */
  94. #define TC_MSB 0x01 /* transfer counter msb */
  95. #define SCSI_FIFO 0x02 /* scsi fifo register */
  96. #define CMD_REG 0x03 /* command register */
  97. #define STAT_REG 0x04 /* status register */
  98. #define DEST_ID 0x04 /* selection/reselection bus id */
  99. #define INT_REG 0x05 /* interrupt status register */
  100. #define SRTIMOUT 0x05 /* select/reselect timeout reg */
  101. #define SEQ_REG 0x06 /* sequence step register */
  102. #define SYNCPRD 0x06 /* synchronous transfer period */
  103. #define FIFO_FLAGS 0x07 /* indicates # of bytes in fifo */
  104. #define SYNCOFF 0x07 /* synchronous offset register */
  105. #define CONFIG1 0x08 /* configuration register */
  106. #define CLKCONV 0x09 /* clock conversion register */
  107. /* #define TESTREG 0x0A */ /* test mode register */
  108. #define CONFIG2 0x0B /* configuration 2 register */
  109. #define CONFIG3 0x0C /* configuration 3 register */
  110. #define CONFIG4 0x0D /* configuration 4 register */
  111. #define TC_HIGH 0x0E /* transfer counter high */
  112. /* #define FIFO_BOTTOM 0x0F */ /* reserve FIFO byte register */
  113. /* Control Register Set 1 */
  114. /* #define JUMPER_SENSE 0x00 */ /* jumper sense port reg (r/w) */
  115. /* #define SRAM_PTR 0x01 */ /* SRAM address pointer reg (r/w) */
  116. /* #define SRAM_DATA 0x02 */ /* SRAM data register (r/w) */
  117. #define PIO_FIFO 0x04 /* PIO FIFO registers (r/w) */
  118. /* #define PIO_FIFO1 0x05 */ /* */
  119. /* #define PIO_FIFO2 0x06 */ /* */
  120. /* #define PIO_FIFO3 0x07 */ /* */
  121. #define PIO_STATUS 0x08 /* PIO status (r/w) */
  122. /* #define ATA_CMD 0x09 */ /* ATA command/status reg (r/w) */
  123. /* #define ATA_ERR 0x0A */ /* ATA features/error reg (r/w) */
  124. #define PIO_FLAG 0x0B /* PIO flag interrupt enable (r/w) */
  125. #define CONFIG5 0x09 /* configuration 5 register */
  126. /* #define SIGNATURE 0x0E */ /* signature register (r) */
  127. /* #define CONFIG6 0x0F */ /* configuration 6 register (r) */
  128. #define CONFIG7 0x0d
  129. /* select register set 0 */
  130. #define REG0(x) (outb(C4_IMG, (x) + CONFIG4))
  131. /* select register set 1 */
  132. #define REG1(x) outb(C7_IMG, (x) + CONFIG7); outb(C5_IMG, (x) + CONFIG5)
  133. #if SYM53C500_DEBUG
  134. #define DEB(x) x
  135. #else
  136. #define DEB(x)
  137. #endif
  138. #if VERBOSE_SYM53C500_DEBUG
  139. #define VDEB(x) x
  140. #else
  141. #define VDEB(x)
  142. #endif
  143. #define LOAD_DMA_COUNT(x, count) \
  144. outb(count & 0xff, (x) + TC_LSB); \
  145. outb((count >> 8) & 0xff, (x) + TC_MSB); \
  146. outb((count >> 16) & 0xff, (x) + TC_HIGH);
  147. /* Chip commands */
  148. #define DMA_OP 0x80
  149. #define SCSI_NOP 0x00
  150. #define FLUSH_FIFO 0x01
  151. #define CHIP_RESET 0x02
  152. #define SCSI_RESET 0x03
  153. #define RESELECT 0x40
  154. #define SELECT_NO_ATN 0x41
  155. #define SELECT_ATN 0x42
  156. #define SELECT_ATN_STOP 0x43
  157. #define ENABLE_SEL 0x44
  158. #define DISABLE_SEL 0x45
  159. #define SELECT_ATN3 0x46
  160. #define RESELECT3 0x47
  161. #define TRANSFER_INFO 0x10
  162. #define INIT_CMD_COMPLETE 0x11
  163. #define MSG_ACCEPT 0x12
  164. #define TRANSFER_PAD 0x18
  165. #define SET_ATN 0x1a
  166. #define RESET_ATN 0x1b
  167. #define SEND_MSG 0x20
  168. #define SEND_STATUS 0x21
  169. #define SEND_DATA 0x22
  170. #define DISCONN_SEQ 0x23
  171. #define TERMINATE_SEQ 0x24
  172. #define TARG_CMD_COMPLETE 0x25
  173. #define DISCONN 0x27
  174. #define RECV_MSG 0x28
  175. #define RECV_CMD 0x29
  176. #define RECV_DATA 0x2a
  177. #define RECV_CMD_SEQ 0x2b
  178. #define TARGET_ABORT_DMA 0x04
  179. /* ================================================================== */
  180. struct scsi_info_t {
  181. struct pcmcia_device *p_dev;
  182. dev_node_t node;
  183. struct Scsi_Host *host;
  184. unsigned short manf_id;
  185. };
  186. /*
  187. * Repository for per-instance host data.
  188. */
  189. struct sym53c500_data {
  190. struct scsi_cmnd *current_SC;
  191. int fast_pio;
  192. };
  193. enum Phase {
  194. idle,
  195. data_out,
  196. data_in,
  197. command_ph,
  198. status_ph,
  199. message_out,
  200. message_in
  201. };
  202. /* ================================================================== */
  203. static void
  204. chip_init(int io_port)
  205. {
  206. REG1(io_port);
  207. outb(0x01, io_port + PIO_STATUS);
  208. outb(0x00, io_port + PIO_FLAG);
  209. outb(C4_IMG, io_port + CONFIG4); /* REG0(io_port); */
  210. outb(C3_IMG, io_port + CONFIG3);
  211. outb(C2_IMG, io_port + CONFIG2);
  212. outb(C1_IMG, io_port + CONFIG1);
  213. outb(0x05, io_port + CLKCONV); /* clock conversion factor */
  214. outb(0x9C, io_port + SRTIMOUT); /* Selection timeout */
  215. outb(0x05, io_port + SYNCPRD); /* Synchronous transfer period */
  216. outb(SYNC_MODE, io_port + SYNCOFF); /* synchronous mode */
  217. }
  218. static void
  219. SYM53C500_int_host_reset(int io_port)
  220. {
  221. outb(C4_IMG, io_port + CONFIG4); /* REG0(io_port); */
  222. outb(CHIP_RESET, io_port + CMD_REG);
  223. outb(SCSI_NOP, io_port + CMD_REG); /* required after reset */
  224. outb(SCSI_RESET, io_port + CMD_REG);
  225. chip_init(io_port);
  226. }
  227. static __inline__ int
  228. SYM53C500_pio_read(int fast_pio, int base, unsigned char *request, unsigned int reqlen)
  229. {
  230. int i;
  231. int len; /* current scsi fifo size */
  232. REG1(base);
  233. while (reqlen) {
  234. i = inb(base + PIO_STATUS);
  235. /* VDEB(printk("pio_status=%x\n", i)); */
  236. if (i & 0x80)
  237. return 0;
  238. switch (i & 0x1e) {
  239. default:
  240. case 0x10: /* fifo empty */
  241. len = 0;
  242. break;
  243. case 0x0:
  244. len = 1;
  245. break;
  246. case 0x8: /* fifo 1/3 full */
  247. len = 42;
  248. break;
  249. case 0xc: /* fifo 2/3 full */
  250. len = 84;
  251. break;
  252. case 0xe: /* fifo full */
  253. len = 128;
  254. break;
  255. }
  256. if ((i & 0x40) && len == 0) { /* fifo empty and interrupt occurred */
  257. return 0;
  258. }
  259. if (len) {
  260. if (len > reqlen)
  261. len = reqlen;
  262. if (fast_pio && len > 3) {
  263. insl(base + PIO_FIFO, request, len >> 2);
  264. request += len & 0xfc;
  265. reqlen -= len & 0xfc;
  266. } else {
  267. while (len--) {
  268. *request++ = inb(base + PIO_FIFO);
  269. reqlen--;
  270. }
  271. }
  272. }
  273. }
  274. return 0;
  275. }
  276. static __inline__ int
  277. SYM53C500_pio_write(int fast_pio, int base, unsigned char *request, unsigned int reqlen)
  278. {
  279. int i = 0;
  280. int len; /* current scsi fifo size */
  281. REG1(base);
  282. while (reqlen && !(i & 0x40)) {
  283. i = inb(base + PIO_STATUS);
  284. /* VDEB(printk("pio_status=%x\n", i)); */
  285. if (i & 0x80) /* error */
  286. return 0;
  287. switch (i & 0x1e) {
  288. case 0x10:
  289. len = 128;
  290. break;
  291. case 0x0:
  292. len = 84;
  293. break;
  294. case 0x8:
  295. len = 42;
  296. break;
  297. case 0xc:
  298. len = 1;
  299. break;
  300. default:
  301. case 0xe:
  302. len = 0;
  303. break;
  304. }
  305. if (len) {
  306. if (len > reqlen)
  307. len = reqlen;
  308. if (fast_pio && len > 3) {
  309. outsl(base + PIO_FIFO, request, len >> 2);
  310. request += len & 0xfc;
  311. reqlen -= len & 0xfc;
  312. } else {
  313. while (len--) {
  314. outb(*request++, base + PIO_FIFO);
  315. reqlen--;
  316. }
  317. }
  318. }
  319. }
  320. return 0;
  321. }
  322. static irqreturn_t
  323. SYM53C500_intr(int irq, void *dev_id)
  324. {
  325. unsigned long flags;
  326. struct Scsi_Host *dev = dev_id;
  327. DEB(unsigned char fifo_size;)
  328. DEB(unsigned char seq_reg;)
  329. unsigned char status, int_reg;
  330. unsigned char pio_status;
  331. struct scatterlist *sglist;
  332. unsigned int sgcount;
  333. int port_base = dev->io_port;
  334. struct sym53c500_data *data =
  335. (struct sym53c500_data *)dev->hostdata;
  336. struct scsi_cmnd *curSC = data->current_SC;
  337. int fast_pio = data->fast_pio;
  338. spin_lock_irqsave(dev->host_lock, flags);
  339. VDEB(printk("SYM53C500_intr called\n"));
  340. REG1(port_base);
  341. pio_status = inb(port_base + PIO_STATUS);
  342. REG0(port_base);
  343. status = inb(port_base + STAT_REG);
  344. DEB(seq_reg = inb(port_base + SEQ_REG));
  345. int_reg = inb(port_base + INT_REG);
  346. DEB(fifo_size = inb(port_base + FIFO_FLAGS) & 0x1f);
  347. #if SYM53C500_DEBUG
  348. printk("status=%02x, seq_reg=%02x, int_reg=%02x, fifo_size=%02x",
  349. status, seq_reg, int_reg, fifo_size);
  350. printk(", pio=%02x\n", pio_status);
  351. #endif /* SYM53C500_DEBUG */
  352. if (int_reg & 0x80) { /* SCSI reset intr */
  353. DEB(printk("SYM53C500: reset intr received\n"));
  354. curSC->result = DID_RESET << 16;
  355. goto idle_out;
  356. }
  357. if (pio_status & 0x80) {
  358. printk("SYM53C500: Warning: PIO error!\n");
  359. curSC->result = DID_ERROR << 16;
  360. goto idle_out;
  361. }
  362. if (status & 0x20) { /* Parity error */
  363. printk("SYM53C500: Warning: parity error!\n");
  364. curSC->result = DID_PARITY << 16;
  365. goto idle_out;
  366. }
  367. if (status & 0x40) { /* Gross error */
  368. printk("SYM53C500: Warning: gross error!\n");
  369. curSC->result = DID_ERROR << 16;
  370. goto idle_out;
  371. }
  372. if (int_reg & 0x20) { /* Disconnect */
  373. DEB(printk("SYM53C500: disconnect intr received\n"));
  374. if (curSC->SCp.phase != message_in) { /* Unexpected disconnect */
  375. curSC->result = DID_NO_CONNECT << 16;
  376. } else { /* Command complete, return status and message */
  377. curSC->result = (curSC->SCp.Status & 0xff)
  378. | ((curSC->SCp.Message & 0xff) << 8) | (DID_OK << 16);
  379. }
  380. goto idle_out;
  381. }
  382. switch (status & 0x07) { /* scsi phase */
  383. case 0x00: /* DATA-OUT */
  384. if (int_reg & 0x10) { /* Target requesting info transfer */
  385. curSC->SCp.phase = data_out;
  386. VDEB(printk("SYM53C500: Data-Out phase\n"));
  387. outb(FLUSH_FIFO, port_base + CMD_REG);
  388. LOAD_DMA_COUNT(port_base, curSC->request_bufflen); /* Max transfer size */
  389. outb(TRANSFER_INFO | DMA_OP, port_base + CMD_REG);
  390. if (!curSC->use_sg) /* Don't use scatter-gather */
  391. SYM53C500_pio_write(fast_pio, port_base, curSC->request_buffer, curSC->request_bufflen);
  392. else { /* use scatter-gather */
  393. sgcount = curSC->use_sg;
  394. sglist = curSC->request_buffer;
  395. while (sgcount--) {
  396. SYM53C500_pio_write(fast_pio, port_base, page_address(sglist->page) + sglist->offset, sglist->length);
  397. sglist++;
  398. }
  399. }
  400. REG0(port_base);
  401. }
  402. break;
  403. case 0x01: /* DATA-IN */
  404. if (int_reg & 0x10) { /* Target requesting info transfer */
  405. curSC->SCp.phase = data_in;
  406. VDEB(printk("SYM53C500: Data-In phase\n"));
  407. outb(FLUSH_FIFO, port_base + CMD_REG);
  408. LOAD_DMA_COUNT(port_base, curSC->request_bufflen); /* Max transfer size */
  409. outb(TRANSFER_INFO | DMA_OP, port_base + CMD_REG);
  410. if (!curSC->use_sg) /* Don't use scatter-gather */
  411. SYM53C500_pio_read(fast_pio, port_base, curSC->request_buffer, curSC->request_bufflen);
  412. else { /* Use scatter-gather */
  413. sgcount = curSC->use_sg;
  414. sglist = curSC->request_buffer;
  415. while (sgcount--) {
  416. SYM53C500_pio_read(fast_pio, port_base, page_address(sglist->page) + sglist->offset, sglist->length);
  417. sglist++;
  418. }
  419. }
  420. REG0(port_base);
  421. }
  422. break;
  423. case 0x02: /* COMMAND */
  424. curSC->SCp.phase = command_ph;
  425. printk("SYM53C500: Warning: Unknown interrupt occurred in command phase!\n");
  426. break;
  427. case 0x03: /* STATUS */
  428. curSC->SCp.phase = status_ph;
  429. VDEB(printk("SYM53C500: Status phase\n"));
  430. outb(FLUSH_FIFO, port_base + CMD_REG);
  431. outb(INIT_CMD_COMPLETE, port_base + CMD_REG);
  432. break;
  433. case 0x04: /* Reserved */
  434. case 0x05: /* Reserved */
  435. printk("SYM53C500: WARNING: Reserved phase!!!\n");
  436. break;
  437. case 0x06: /* MESSAGE-OUT */
  438. DEB(printk("SYM53C500: Message-Out phase\n"));
  439. curSC->SCp.phase = message_out;
  440. outb(SET_ATN, port_base + CMD_REG); /* Reject the message */
  441. outb(MSG_ACCEPT, port_base + CMD_REG);
  442. break;
  443. case 0x07: /* MESSAGE-IN */
  444. VDEB(printk("SYM53C500: Message-In phase\n"));
  445. curSC->SCp.phase = message_in;
  446. curSC->SCp.Status = inb(port_base + SCSI_FIFO);
  447. curSC->SCp.Message = inb(port_base + SCSI_FIFO);
  448. VDEB(printk("SCSI FIFO size=%d\n", inb(port_base + FIFO_FLAGS) & 0x1f));
  449. DEB(printk("Status = %02x Message = %02x\n", curSC->SCp.Status, curSC->SCp.Message));
  450. if (curSC->SCp.Message == SAVE_POINTERS || curSC->SCp.Message == DISCONNECT) {
  451. outb(SET_ATN, port_base + CMD_REG); /* Reject message */
  452. DEB(printk("Discarding SAVE_POINTERS message\n"));
  453. }
  454. outb(MSG_ACCEPT, port_base + CMD_REG);
  455. break;
  456. }
  457. out:
  458. spin_unlock_irqrestore(dev->host_lock, flags);
  459. return IRQ_HANDLED;
  460. idle_out:
  461. curSC->SCp.phase = idle;
  462. curSC->scsi_done(curSC);
  463. goto out;
  464. }
  465. static void
  466. SYM53C500_release(struct pcmcia_device *link)
  467. {
  468. struct scsi_info_t *info = link->priv;
  469. struct Scsi_Host *shost = info->host;
  470. DEBUG(0, "SYM53C500_release(0x%p)\n", link);
  471. /*
  472. * Do this before releasing/freeing resources.
  473. */
  474. scsi_remove_host(shost);
  475. /*
  476. * Interrupts getting hosed on card removal. Try
  477. * the following code, mostly from qlogicfas.c.
  478. */
  479. if (shost->irq)
  480. free_irq(shost->irq, shost);
  481. if (shost->io_port && shost->n_io_port)
  482. release_region(shost->io_port, shost->n_io_port);
  483. pcmcia_disable_device(link);
  484. scsi_host_put(shost);
  485. } /* SYM53C500_release */
  486. static const char*
  487. SYM53C500_info(struct Scsi_Host *SChost)
  488. {
  489. static char info_msg[256];
  490. struct sym53c500_data *data =
  491. (struct sym53c500_data *)SChost->hostdata;
  492. DEB(printk("SYM53C500_info called\n"));
  493. (void)snprintf(info_msg, sizeof(info_msg),
  494. "SYM53C500 at 0x%lx, IRQ %d, %s PIO mode.",
  495. SChost->io_port, SChost->irq, data->fast_pio ? "fast" : "slow");
  496. return (info_msg);
  497. }
  498. static int
  499. SYM53C500_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  500. {
  501. int i;
  502. int port_base = SCpnt->device->host->io_port;
  503. struct sym53c500_data *data =
  504. (struct sym53c500_data *)SCpnt->device->host->hostdata;
  505. VDEB(printk("SYM53C500_queue called\n"));
  506. DEB(printk("cmd=%02x, cmd_len=%02x, target=%02x, lun=%02x, bufflen=%d\n",
  507. SCpnt->cmnd[0], SCpnt->cmd_len, SCpnt->device->id,
  508. SCpnt->device->lun, SCpnt->request_bufflen));
  509. VDEB(for (i = 0; i < SCpnt->cmd_len; i++)
  510. printk("cmd[%d]=%02x ", i, SCpnt->cmnd[i]));
  511. VDEB(printk("\n"));
  512. data->current_SC = SCpnt;
  513. data->current_SC->scsi_done = done;
  514. data->current_SC->SCp.phase = command_ph;
  515. data->current_SC->SCp.Status = 0;
  516. data->current_SC->SCp.Message = 0;
  517. /* We are locked here already by the mid layer */
  518. REG0(port_base);
  519. outb(scmd_id(SCpnt), port_base + DEST_ID); /* set destination */
  520. outb(FLUSH_FIFO, port_base + CMD_REG); /* reset the fifos */
  521. for (i = 0; i < SCpnt->cmd_len; i++) {
  522. outb(SCpnt->cmnd[i], port_base + SCSI_FIFO);
  523. }
  524. outb(SELECT_NO_ATN, port_base + CMD_REG);
  525. return 0;
  526. }
  527. static int
  528. SYM53C500_host_reset(struct scsi_cmnd *SCpnt)
  529. {
  530. int port_base = SCpnt->device->host->io_port;
  531. DEB(printk("SYM53C500_host_reset called\n"));
  532. spin_lock_irq(SCpnt->device->host->host_lock);
  533. SYM53C500_int_host_reset(port_base);
  534. spin_unlock_irq(SCpnt->device->host->host_lock);
  535. return SUCCESS;
  536. }
  537. static int
  538. SYM53C500_biosparm(struct scsi_device *disk,
  539. struct block_device *dev,
  540. sector_t capacity, int *info_array)
  541. {
  542. int size;
  543. DEB(printk("SYM53C500_biosparm called\n"));
  544. size = capacity;
  545. info_array[0] = 64; /* heads */
  546. info_array[1] = 32; /* sectors */
  547. info_array[2] = size >> 11; /* cylinders */
  548. if (info_array[2] > 1024) { /* big disk */
  549. info_array[0] = 255;
  550. info_array[1] = 63;
  551. info_array[2] = size / (255 * 63);
  552. }
  553. return 0;
  554. }
  555. static ssize_t
  556. SYM53C500_show_pio(struct class_device *cdev, char *buf)
  557. {
  558. struct Scsi_Host *SHp = class_to_shost(cdev);
  559. struct sym53c500_data *data =
  560. (struct sym53c500_data *)SHp->hostdata;
  561. return snprintf(buf, 4, "%d\n", data->fast_pio);
  562. }
  563. static ssize_t
  564. SYM53C500_store_pio(struct class_device *cdev, const char *buf, size_t count)
  565. {
  566. int pio;
  567. struct Scsi_Host *SHp = class_to_shost(cdev);
  568. struct sym53c500_data *data =
  569. (struct sym53c500_data *)SHp->hostdata;
  570. pio = simple_strtoul(buf, NULL, 0);
  571. if (pio == 0 || pio == 1) {
  572. data->fast_pio = pio;
  573. return count;
  574. }
  575. else
  576. return -EINVAL;
  577. }
  578. /*
  579. * SCSI HBA device attributes we want to
  580. * make available via sysfs.
  581. */
  582. static struct class_device_attribute SYM53C500_pio_attr = {
  583. .attr = {
  584. .name = "fast_pio",
  585. .mode = (S_IRUGO | S_IWUSR),
  586. },
  587. .show = SYM53C500_show_pio,
  588. .store = SYM53C500_store_pio,
  589. };
  590. static struct class_device_attribute *SYM53C500_shost_attrs[] = {
  591. &SYM53C500_pio_attr,
  592. NULL,
  593. };
  594. /*
  595. * scsi_host_template initializer
  596. */
  597. static struct scsi_host_template sym53c500_driver_template = {
  598. .module = THIS_MODULE,
  599. .name = "SYM53C500",
  600. .info = SYM53C500_info,
  601. .queuecommand = SYM53C500_queue,
  602. .eh_host_reset_handler = SYM53C500_host_reset,
  603. .bios_param = SYM53C500_biosparm,
  604. .proc_name = "SYM53C500",
  605. .can_queue = 1,
  606. .this_id = 7,
  607. .sg_tablesize = 32,
  608. .cmd_per_lun = 1,
  609. .use_clustering = ENABLE_CLUSTERING,
  610. .shost_attrs = SYM53C500_shost_attrs
  611. };
  612. #define CS_CHECK(fn, ret) \
  613. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  614. static int
  615. SYM53C500_config(struct pcmcia_device *link)
  616. {
  617. struct scsi_info_t *info = link->priv;
  618. tuple_t tuple;
  619. cisparse_t parse;
  620. int i, last_ret, last_fn;
  621. int irq_level, port_base;
  622. unsigned short tuple_data[32];
  623. struct Scsi_Host *host;
  624. struct scsi_host_template *tpnt = &sym53c500_driver_template;
  625. struct sym53c500_data *data;
  626. DEBUG(0, "SYM53C500_config(0x%p)\n", link);
  627. info->manf_id = link->manf_id;
  628. tuple.TupleData = (cisdata_t *)tuple_data;
  629. tuple.TupleDataMax = 64;
  630. tuple.TupleOffset = 0;
  631. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  632. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
  633. while (1) {
  634. if (pcmcia_get_tuple_data(link, &tuple) != 0 ||
  635. pcmcia_parse_tuple(link, &tuple, &parse) != 0)
  636. goto next_entry;
  637. link->conf.ConfigIndex = parse.cftable_entry.index;
  638. link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
  639. link->io.NumPorts1 = parse.cftable_entry.io.win[0].len;
  640. if (link->io.BasePort1 != 0) {
  641. i = pcmcia_request_io(link, &link->io);
  642. if (i == CS_SUCCESS)
  643. break;
  644. }
  645. next_entry:
  646. CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(link, &tuple));
  647. }
  648. CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
  649. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
  650. /*
  651. * That's the trouble with copying liberally from another driver.
  652. * Some things probably aren't relevant, and I suspect this entire
  653. * section dealing with manufacturer IDs can be scrapped. --rct
  654. */
  655. if ((info->manf_id == MANFID_MACNICA) ||
  656. (info->manf_id == MANFID_PIONEER) ||
  657. (info->manf_id == 0x0098)) {
  658. /* set ATAcmd */
  659. outb(0xb4, link->io.BasePort1 + 0xd);
  660. outb(0x24, link->io.BasePort1 + 0x9);
  661. outb(0x04, link->io.BasePort1 + 0xd);
  662. }
  663. /*
  664. * irq_level == 0 implies tpnt->can_queue == 0, which
  665. * is not supported in 2.6. Thus, only irq_level > 0
  666. * will be allowed.
  667. *
  668. * Possible port_base values are as follows:
  669. *
  670. * 0x130, 0x230, 0x280, 0x290,
  671. * 0x320, 0x330, 0x340, 0x350
  672. */
  673. port_base = link->io.BasePort1;
  674. irq_level = link->irq.AssignedIRQ;
  675. DEB(printk("SYM53C500: port_base=0x%x, irq=%d, fast_pio=%d\n",
  676. port_base, irq_level, USE_FAST_PIO);)
  677. chip_init(port_base);
  678. host = scsi_host_alloc(tpnt, sizeof(struct sym53c500_data));
  679. if (!host) {
  680. printk("SYM53C500: Unable to register host, giving up.\n");
  681. goto err_release;
  682. }
  683. data = (struct sym53c500_data *)host->hostdata;
  684. if (irq_level > 0) {
  685. if (request_irq(irq_level, SYM53C500_intr, IRQF_SHARED, "SYM53C500", host)) {
  686. printk("SYM53C500: unable to allocate IRQ %d\n", irq_level);
  687. goto err_free_scsi;
  688. }
  689. DEB(printk("SYM53C500: allocated IRQ %d\n", irq_level));
  690. } else if (irq_level == 0) {
  691. DEB(printk("SYM53C500: No interrupts detected\n"));
  692. goto err_free_scsi;
  693. } else {
  694. DEB(printk("SYM53C500: Shouldn't get here!\n"));
  695. goto err_free_scsi;
  696. }
  697. host->unique_id = port_base;
  698. host->irq = irq_level;
  699. host->io_port = port_base;
  700. host->n_io_port = 0x10;
  701. host->dma_channel = -1;
  702. /*
  703. * Note fast_pio is set to USE_FAST_PIO by
  704. * default, but can be changed via "sysfs".
  705. */
  706. data->fast_pio = USE_FAST_PIO;
  707. sprintf(info->node.dev_name, "scsi%d", host->host_no);
  708. link->dev_node = &info->node;
  709. info->host = host;
  710. if (scsi_add_host(host, NULL))
  711. goto err_free_irq;
  712. scsi_scan_host(host);
  713. return 0;
  714. err_free_irq:
  715. free_irq(irq_level, host);
  716. err_free_scsi:
  717. scsi_host_put(host);
  718. err_release:
  719. release_region(port_base, 0x10);
  720. printk(KERN_INFO "sym53c500_cs: no SCSI devices found\n");
  721. return -ENODEV;
  722. cs_failed:
  723. cs_error(link, last_fn, last_ret);
  724. SYM53C500_release(link);
  725. return -ENODEV;
  726. } /* SYM53C500_config */
  727. static int sym53c500_resume(struct pcmcia_device *link)
  728. {
  729. struct scsi_info_t *info = link->priv;
  730. /* See earlier comment about manufacturer IDs. */
  731. if ((info->manf_id == MANFID_MACNICA) ||
  732. (info->manf_id == MANFID_PIONEER) ||
  733. (info->manf_id == 0x0098)) {
  734. outb(0x80, link->io.BasePort1 + 0xd);
  735. outb(0x24, link->io.BasePort1 + 0x9);
  736. outb(0x04, link->io.BasePort1 + 0xd);
  737. }
  738. /*
  739. * If things don't work after a "resume",
  740. * this is a good place to start looking.
  741. */
  742. SYM53C500_int_host_reset(link->io.BasePort1);
  743. return 0;
  744. }
  745. static void
  746. SYM53C500_detach(struct pcmcia_device *link)
  747. {
  748. DEBUG(0, "SYM53C500_detach(0x%p)\n", link);
  749. SYM53C500_release(link);
  750. kfree(link->priv);
  751. link->priv = NULL;
  752. } /* SYM53C500_detach */
  753. static int
  754. SYM53C500_probe(struct pcmcia_device *link)
  755. {
  756. struct scsi_info_t *info;
  757. DEBUG(0, "SYM53C500_attach()\n");
  758. /* Create new SCSI device */
  759. info = kmalloc(sizeof(*info), GFP_KERNEL);
  760. if (!info)
  761. return -ENOMEM;
  762. memset(info, 0, sizeof(*info));
  763. info->p_dev = link;
  764. link->priv = info;
  765. link->io.NumPorts1 = 16;
  766. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  767. link->io.IOAddrLines = 10;
  768. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  769. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  770. link->conf.Attributes = CONF_ENABLE_IRQ;
  771. link->conf.IntType = INT_MEMORY_AND_IO;
  772. return SYM53C500_config(link);
  773. } /* SYM53C500_attach */
  774. MODULE_AUTHOR("Bob Tracy <rct@frus.com>");
  775. MODULE_DESCRIPTION("SYM53C500 PCMCIA SCSI driver");
  776. MODULE_LICENSE("GPL");
  777. static struct pcmcia_device_id sym53c500_ids[] = {
  778. PCMCIA_DEVICE_PROD_ID12("BASICS by New Media Corporation", "SCSI Sym53C500", 0x23c78a9d, 0x0099e7f7),
  779. PCMCIA_DEVICE_PROD_ID12("New Media Corporation", "SCSI Bus Toaster Sym53C500", 0x085a850b, 0x45432eb8),
  780. PCMCIA_DEVICE_PROD_ID2("SCSI9000", 0x21648f44),
  781. PCMCIA_DEVICE_NULL,
  782. };
  783. MODULE_DEVICE_TABLE(pcmcia, sym53c500_ids);
  784. static struct pcmcia_driver sym53c500_cs_driver = {
  785. .owner = THIS_MODULE,
  786. .drv = {
  787. .name = "sym53c500_cs",
  788. },
  789. .probe = SYM53C500_probe,
  790. .remove = SYM53C500_detach,
  791. .id_table = sym53c500_ids,
  792. .resume = sym53c500_resume,
  793. };
  794. static int __init
  795. init_sym53c500_cs(void)
  796. {
  797. return pcmcia_register_driver(&sym53c500_cs_driver);
  798. }
  799. static void __exit
  800. exit_sym53c500_cs(void)
  801. {
  802. pcmcia_unregister_driver(&sym53c500_cs_driver);
  803. }
  804. module_init(init_sym53c500_cs);
  805. module_exit(exit_sym53c500_cs);