sym53c500_cs.c 25 KB

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