sym53c500_cs.c 26 KB

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