sym53c500_cs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. struct pcmcia_device *p_dev;
  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)
  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(struct pcmcia_device *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->io_port && shost->n_io_port)
  483. release_region(shost->io_port, shost->n_io_port);
  484. pcmcia_disable_device(link);
  485. scsi_host_put(shost);
  486. } /* SYM53C500_release */
  487. static const char*
  488. SYM53C500_info(struct Scsi_Host *SChost)
  489. {
  490. static char info_msg[256];
  491. struct sym53c500_data *data =
  492. (struct sym53c500_data *)SChost->hostdata;
  493. DEB(printk("SYM53C500_info called\n"));
  494. (void)snprintf(info_msg, sizeof(info_msg),
  495. "SYM53C500 at 0x%lx, IRQ %d, %s PIO mode.",
  496. SChost->io_port, SChost->irq, data->fast_pio ? "fast" : "slow");
  497. return (info_msg);
  498. }
  499. static int
  500. SYM53C500_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  501. {
  502. int i;
  503. int port_base = SCpnt->device->host->io_port;
  504. struct sym53c500_data *data =
  505. (struct sym53c500_data *)SCpnt->device->host->hostdata;
  506. VDEB(printk("SYM53C500_queue called\n"));
  507. DEB(printk("cmd=%02x, cmd_len=%02x, target=%02x, lun=%02x, bufflen=%d\n",
  508. SCpnt->cmnd[0], SCpnt->cmd_len, SCpnt->device->id,
  509. SCpnt->device->lun, SCpnt->request_bufflen));
  510. VDEB(for (i = 0; i < SCpnt->cmd_len; i++)
  511. printk("cmd[%d]=%02x ", i, SCpnt->cmnd[i]));
  512. VDEB(printk("\n"));
  513. data->current_SC = SCpnt;
  514. data->current_SC->scsi_done = done;
  515. data->current_SC->SCp.phase = command_ph;
  516. data->current_SC->SCp.Status = 0;
  517. data->current_SC->SCp.Message = 0;
  518. /* We are locked here already by the mid layer */
  519. REG0(port_base);
  520. outb(scmd_id(SCpnt), port_base + DEST_ID); /* set destination */
  521. outb(FLUSH_FIFO, port_base + CMD_REG); /* reset the fifos */
  522. for (i = 0; i < SCpnt->cmd_len; i++) {
  523. outb(SCpnt->cmnd[i], port_base + SCSI_FIFO);
  524. }
  525. outb(SELECT_NO_ATN, port_base + CMD_REG);
  526. return 0;
  527. }
  528. static int
  529. SYM53C500_host_reset(struct scsi_cmnd *SCpnt)
  530. {
  531. int port_base = SCpnt->device->host->io_port;
  532. DEB(printk("SYM53C500_host_reset called\n"));
  533. spin_lock_irq(SCpnt->device->host->host_lock);
  534. SYM53C500_int_host_reset(port_base);
  535. spin_unlock_irq(SCpnt->device->host->host_lock);
  536. return SUCCESS;
  537. }
  538. static int
  539. SYM53C500_biosparm(struct scsi_device *disk,
  540. struct block_device *dev,
  541. sector_t capacity, int *info_array)
  542. {
  543. int size;
  544. DEB(printk("SYM53C500_biosparm called\n"));
  545. size = capacity;
  546. info_array[0] = 64; /* heads */
  547. info_array[1] = 32; /* sectors */
  548. info_array[2] = size >> 11; /* cylinders */
  549. if (info_array[2] > 1024) { /* big disk */
  550. info_array[0] = 255;
  551. info_array[1] = 63;
  552. info_array[2] = size / (255 * 63);
  553. }
  554. return 0;
  555. }
  556. static ssize_t
  557. SYM53C500_show_pio(struct class_device *cdev, char *buf)
  558. {
  559. struct Scsi_Host *SHp = class_to_shost(cdev);
  560. struct sym53c500_data *data =
  561. (struct sym53c500_data *)SHp->hostdata;
  562. return snprintf(buf, 4, "%d\n", data->fast_pio);
  563. }
  564. static ssize_t
  565. SYM53C500_store_pio(struct class_device *cdev, const char *buf, size_t count)
  566. {
  567. int pio;
  568. struct Scsi_Host *SHp = class_to_shost(cdev);
  569. struct sym53c500_data *data =
  570. (struct sym53c500_data *)SHp->hostdata;
  571. pio = simple_strtoul(buf, NULL, 0);
  572. if (pio == 0 || pio == 1) {
  573. data->fast_pio = pio;
  574. return count;
  575. }
  576. else
  577. return -EINVAL;
  578. }
  579. /*
  580. * SCSI HBA device attributes we want to
  581. * make available via sysfs.
  582. */
  583. static struct class_device_attribute SYM53C500_pio_attr = {
  584. .attr = {
  585. .name = "fast_pio",
  586. .mode = (S_IRUGO | S_IWUSR),
  587. },
  588. .show = SYM53C500_show_pio,
  589. .store = SYM53C500_store_pio,
  590. };
  591. static struct class_device_attribute *SYM53C500_shost_attrs[] = {
  592. &SYM53C500_pio_attr,
  593. NULL,
  594. };
  595. /*
  596. * scsi_host_template initializer
  597. */
  598. static struct scsi_host_template sym53c500_driver_template = {
  599. .module = THIS_MODULE,
  600. .name = "SYM53C500",
  601. .info = SYM53C500_info,
  602. .queuecommand = SYM53C500_queue,
  603. .eh_host_reset_handler = SYM53C500_host_reset,
  604. .bios_param = SYM53C500_biosparm,
  605. .proc_name = "SYM53C500",
  606. .can_queue = 1,
  607. .this_id = 7,
  608. .sg_tablesize = 32,
  609. .cmd_per_lun = 1,
  610. .use_clustering = ENABLE_CLUSTERING,
  611. .shost_attrs = SYM53C500_shost_attrs
  612. };
  613. #define CS_CHECK(fn, ret) \
  614. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  615. static int
  616. SYM53C500_config(struct pcmcia_device *link)
  617. {
  618. struct scsi_info_t *info = link->priv;
  619. tuple_t tuple;
  620. cisparse_t parse;
  621. int i, last_ret, last_fn;
  622. int irq_level, port_base;
  623. unsigned short tuple_data[32];
  624. struct Scsi_Host *host;
  625. struct scsi_host_template *tpnt = &sym53c500_driver_template;
  626. struct sym53c500_data *data;
  627. DEBUG(0, "SYM53C500_config(0x%p)\n", link);
  628. info->manf_id = link->manf_id;
  629. tuple.TupleData = (cisdata_t *)tuple_data;
  630. tuple.TupleDataMax = 64;
  631. tuple.TupleOffset = 0;
  632. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  633. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
  634. while (1) {
  635. if (pcmcia_get_tuple_data(link, &tuple) != 0 ||
  636. pcmcia_parse_tuple(link, &tuple, &parse) != 0)
  637. goto next_entry;
  638. link->conf.ConfigIndex = parse.cftable_entry.index;
  639. link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
  640. link->io.NumPorts1 = parse.cftable_entry.io.win[0].len;
  641. if (link->io.BasePort1 != 0) {
  642. i = pcmcia_request_io(link, &link->io);
  643. if (i == CS_SUCCESS)
  644. break;
  645. }
  646. next_entry:
  647. CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(link, &tuple));
  648. }
  649. CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
  650. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
  651. /*
  652. * That's the trouble with copying liberally from another driver.
  653. * Some things probably aren't relevant, and I suspect this entire
  654. * section dealing with manufacturer IDs can be scrapped. --rct
  655. */
  656. if ((info->manf_id == MANFID_MACNICA) ||
  657. (info->manf_id == MANFID_PIONEER) ||
  658. (info->manf_id == 0x0098)) {
  659. /* set ATAcmd */
  660. outb(0xb4, link->io.BasePort1 + 0xd);
  661. outb(0x24, link->io.BasePort1 + 0x9);
  662. outb(0x04, link->io.BasePort1 + 0xd);
  663. }
  664. /*
  665. * irq_level == 0 implies tpnt->can_queue == 0, which
  666. * is not supported in 2.6. Thus, only irq_level > 0
  667. * will be allowed.
  668. *
  669. * Possible port_base values are as follows:
  670. *
  671. * 0x130, 0x230, 0x280, 0x290,
  672. * 0x320, 0x330, 0x340, 0x350
  673. */
  674. port_base = link->io.BasePort1;
  675. irq_level = link->irq.AssignedIRQ;
  676. DEB(printk("SYM53C500: port_base=0x%x, irq=%d, fast_pio=%d\n",
  677. port_base, irq_level, USE_FAST_PIO);)
  678. chip_init(port_base);
  679. host = scsi_host_alloc(tpnt, sizeof(struct sym53c500_data));
  680. if (!host) {
  681. printk("SYM53C500: Unable to register host, giving up.\n");
  682. goto err_release;
  683. }
  684. data = (struct sym53c500_data *)host->hostdata;
  685. if (irq_level > 0) {
  686. if (request_irq(irq_level, SYM53C500_intr, IRQF_SHARED, "SYM53C500", host)) {
  687. printk("SYM53C500: unable to allocate IRQ %d\n", irq_level);
  688. goto err_free_scsi;
  689. }
  690. DEB(printk("SYM53C500: allocated IRQ %d\n", irq_level));
  691. } else if (irq_level == 0) {
  692. DEB(printk("SYM53C500: No interrupts detected\n"));
  693. goto err_free_scsi;
  694. } else {
  695. DEB(printk("SYM53C500: Shouldn't get here!\n"));
  696. goto err_free_scsi;
  697. }
  698. host->unique_id = port_base;
  699. host->irq = irq_level;
  700. host->io_port = port_base;
  701. host->n_io_port = 0x10;
  702. host->dma_channel = -1;
  703. /*
  704. * Note fast_pio is set to USE_FAST_PIO by
  705. * default, but can be changed via "sysfs".
  706. */
  707. data->fast_pio = USE_FAST_PIO;
  708. sprintf(info->node.dev_name, "scsi%d", host->host_no);
  709. link->dev_node = &info->node;
  710. info->host = host;
  711. if (scsi_add_host(host, NULL))
  712. goto err_free_irq;
  713. scsi_scan_host(host);
  714. return 0;
  715. err_free_irq:
  716. free_irq(irq_level, host);
  717. err_free_scsi:
  718. scsi_host_put(host);
  719. err_release:
  720. release_region(port_base, 0x10);
  721. printk(KERN_INFO "sym53c500_cs: no SCSI devices found\n");
  722. return -ENODEV;
  723. cs_failed:
  724. cs_error(link, last_fn, last_ret);
  725. SYM53C500_release(link);
  726. return -ENODEV;
  727. } /* SYM53C500_config */
  728. static int sym53c500_resume(struct pcmcia_device *link)
  729. {
  730. struct scsi_info_t *info = link->priv;
  731. /* See earlier comment about manufacturer IDs. */
  732. if ((info->manf_id == MANFID_MACNICA) ||
  733. (info->manf_id == MANFID_PIONEER) ||
  734. (info->manf_id == 0x0098)) {
  735. outb(0x80, link->io.BasePort1 + 0xd);
  736. outb(0x24, link->io.BasePort1 + 0x9);
  737. outb(0x04, link->io.BasePort1 + 0xd);
  738. }
  739. /*
  740. * If things don't work after a "resume",
  741. * this is a good place to start looking.
  742. */
  743. SYM53C500_int_host_reset(link->io.BasePort1);
  744. return 0;
  745. }
  746. static void
  747. SYM53C500_detach(struct pcmcia_device *link)
  748. {
  749. DEBUG(0, "SYM53C500_detach(0x%p)\n", link);
  750. SYM53C500_release(link);
  751. kfree(link->priv);
  752. link->priv = NULL;
  753. } /* SYM53C500_detach */
  754. static int
  755. SYM53C500_probe(struct pcmcia_device *link)
  756. {
  757. struct scsi_info_t *info;
  758. DEBUG(0, "SYM53C500_attach()\n");
  759. /* Create new SCSI device */
  760. info = kmalloc(sizeof(*info), GFP_KERNEL);
  761. if (!info)
  762. return -ENOMEM;
  763. memset(info, 0, sizeof(*info));
  764. info->p_dev = link;
  765. link->priv = info;
  766. link->io.NumPorts1 = 16;
  767. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  768. link->io.IOAddrLines = 10;
  769. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  770. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  771. link->conf.Attributes = CONF_ENABLE_IRQ;
  772. link->conf.IntType = INT_MEMORY_AND_IO;
  773. return SYM53C500_config(link);
  774. } /* SYM53C500_attach */
  775. MODULE_AUTHOR("Bob Tracy <rct@frus.com>");
  776. MODULE_DESCRIPTION("SYM53C500 PCMCIA SCSI driver");
  777. MODULE_LICENSE("GPL");
  778. static struct pcmcia_device_id sym53c500_ids[] = {
  779. PCMCIA_DEVICE_PROD_ID12("BASICS by New Media Corporation", "SCSI Sym53C500", 0x23c78a9d, 0x0099e7f7),
  780. PCMCIA_DEVICE_PROD_ID12("New Media Corporation", "SCSI Bus Toaster Sym53C500", 0x085a850b, 0x45432eb8),
  781. PCMCIA_DEVICE_PROD_ID2("SCSI9000", 0x21648f44),
  782. PCMCIA_DEVICE_NULL,
  783. };
  784. MODULE_DEVICE_TABLE(pcmcia, sym53c500_ids);
  785. static struct pcmcia_driver sym53c500_cs_driver = {
  786. .owner = THIS_MODULE,
  787. .drv = {
  788. .name = "sym53c500_cs",
  789. },
  790. .probe = SYM53C500_probe,
  791. .remove = SYM53C500_detach,
  792. .id_table = sym53c500_ids,
  793. .resume = sym53c500_resume,
  794. };
  795. static int __init
  796. init_sym53c500_cs(void)
  797. {
  798. return pcmcia_register_driver(&sym53c500_cs_driver);
  799. }
  800. static void __exit
  801. exit_sym53c500_cs(void)
  802. {
  803. pcmcia_unregister_driver(&sym53c500_cs_driver);
  804. }
  805. module_init(init_sym53c500_cs);
  806. module_exit(exit_sym53c500_cs);