sym53c500_cs.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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. /*
  205. * Global (within this module) variables other than
  206. * sym53c500_driver_template (the scsi_host_template).
  207. */
  208. static dev_link_t *dev_list;
  209. static dev_info_t dev_info = "sym53c500_cs";
  210. /* ================================================================== */
  211. static void
  212. chip_init(int io_port)
  213. {
  214. REG1(io_port);
  215. outb(0x01, io_port + PIO_STATUS);
  216. outb(0x00, io_port + PIO_FLAG);
  217. outb(C4_IMG, io_port + CONFIG4); /* REG0(io_port); */
  218. outb(C3_IMG, io_port + CONFIG3);
  219. outb(C2_IMG, io_port + CONFIG2);
  220. outb(C1_IMG, io_port + CONFIG1);
  221. outb(0x05, io_port + CLKCONV); /* clock conversion factor */
  222. outb(0x9C, io_port + SRTIMOUT); /* Selection timeout */
  223. outb(0x05, io_port + SYNCPRD); /* Synchronous transfer period */
  224. outb(SYNC_MODE, io_port + SYNCOFF); /* synchronous mode */
  225. }
  226. static void
  227. SYM53C500_int_host_reset(int io_port)
  228. {
  229. outb(C4_IMG, io_port + CONFIG4); /* REG0(io_port); */
  230. outb(CHIP_RESET, io_port + CMD_REG);
  231. outb(SCSI_NOP, io_port + CMD_REG); /* required after reset */
  232. outb(SCSI_RESET, io_port + CMD_REG);
  233. chip_init(io_port);
  234. }
  235. static __inline__ int
  236. SYM53C500_pio_read(int fast_pio, int base, unsigned char *request, unsigned int reqlen)
  237. {
  238. int i;
  239. int len; /* current scsi fifo size */
  240. REG1(base);
  241. while (reqlen) {
  242. i = inb(base + PIO_STATUS);
  243. /* VDEB(printk("pio_status=%x\n", i)); */
  244. if (i & 0x80)
  245. return 0;
  246. switch (i & 0x1e) {
  247. default:
  248. case 0x10: /* fifo empty */
  249. len = 0;
  250. break;
  251. case 0x0:
  252. len = 1;
  253. break;
  254. case 0x8: /* fifo 1/3 full */
  255. len = 42;
  256. break;
  257. case 0xc: /* fifo 2/3 full */
  258. len = 84;
  259. break;
  260. case 0xe: /* fifo full */
  261. len = 128;
  262. break;
  263. }
  264. if ((i & 0x40) && len == 0) { /* fifo empty and interrupt occurred */
  265. return 0;
  266. }
  267. if (len) {
  268. if (len > reqlen)
  269. len = reqlen;
  270. if (fast_pio && len > 3) {
  271. insl(base + PIO_FIFO, request, len >> 2);
  272. request += len & 0xfc;
  273. reqlen -= len & 0xfc;
  274. } else {
  275. while (len--) {
  276. *request++ = inb(base + PIO_FIFO);
  277. reqlen--;
  278. }
  279. }
  280. }
  281. }
  282. return 0;
  283. }
  284. static __inline__ int
  285. SYM53C500_pio_write(int fast_pio, int base, unsigned char *request, unsigned int reqlen)
  286. {
  287. int i = 0;
  288. int len; /* current scsi fifo size */
  289. REG1(base);
  290. while (reqlen && !(i & 0x40)) {
  291. i = inb(base + PIO_STATUS);
  292. /* VDEB(printk("pio_status=%x\n", i)); */
  293. if (i & 0x80) /* error */
  294. return 0;
  295. switch (i & 0x1e) {
  296. case 0x10:
  297. len = 128;
  298. break;
  299. case 0x0:
  300. len = 84;
  301. break;
  302. case 0x8:
  303. len = 42;
  304. break;
  305. case 0xc:
  306. len = 1;
  307. break;
  308. default:
  309. case 0xe:
  310. len = 0;
  311. break;
  312. }
  313. if (len) {
  314. if (len > reqlen)
  315. len = reqlen;
  316. if (fast_pio && len > 3) {
  317. outsl(base + PIO_FIFO, request, len >> 2);
  318. request += len & 0xfc;
  319. reqlen -= len & 0xfc;
  320. } else {
  321. while (len--) {
  322. outb(*request++, base + PIO_FIFO);
  323. reqlen--;
  324. }
  325. }
  326. }
  327. }
  328. return 0;
  329. }
  330. static irqreturn_t
  331. SYM53C500_intr(int irq, void *dev_id, struct pt_regs *regs)
  332. {
  333. unsigned long flags;
  334. struct Scsi_Host *dev = dev_id;
  335. DEB(unsigned char fifo_size;)
  336. DEB(unsigned char seq_reg;)
  337. unsigned char status, int_reg;
  338. unsigned char pio_status;
  339. struct scatterlist *sglist;
  340. unsigned int sgcount;
  341. int port_base = dev->io_port;
  342. struct sym53c500_data *data =
  343. (struct sym53c500_data *)dev->hostdata;
  344. struct scsi_cmnd *curSC = data->current_SC;
  345. int fast_pio = data->fast_pio;
  346. spin_lock_irqsave(dev->host_lock, flags);
  347. VDEB(printk("SYM53C500_intr called\n"));
  348. REG1(port_base);
  349. pio_status = inb(port_base + PIO_STATUS);
  350. REG0(port_base);
  351. status = inb(port_base + STAT_REG);
  352. DEB(seq_reg = inb(port_base + SEQ_REG));
  353. int_reg = inb(port_base + INT_REG);
  354. DEB(fifo_size = inb(port_base + FIFO_FLAGS) & 0x1f);
  355. #if SYM53C500_DEBUG
  356. printk("status=%02x, seq_reg=%02x, int_reg=%02x, fifo_size=%02x",
  357. status, seq_reg, int_reg, fifo_size);
  358. printk(", pio=%02x\n", pio_status);
  359. #endif /* SYM53C500_DEBUG */
  360. if (int_reg & 0x80) { /* SCSI reset intr */
  361. DEB(printk("SYM53C500: reset intr received\n"));
  362. curSC->result = DID_RESET << 16;
  363. goto idle_out;
  364. }
  365. if (pio_status & 0x80) {
  366. printk("SYM53C500: Warning: PIO error!\n");
  367. curSC->result = DID_ERROR << 16;
  368. goto idle_out;
  369. }
  370. if (status & 0x20) { /* Parity error */
  371. printk("SYM53C500: Warning: parity error!\n");
  372. curSC->result = DID_PARITY << 16;
  373. goto idle_out;
  374. }
  375. if (status & 0x40) { /* Gross error */
  376. printk("SYM53C500: Warning: gross error!\n");
  377. curSC->result = DID_ERROR << 16;
  378. goto idle_out;
  379. }
  380. if (int_reg & 0x20) { /* Disconnect */
  381. DEB(printk("SYM53C500: disconnect intr received\n"));
  382. if (curSC->SCp.phase != message_in) { /* Unexpected disconnect */
  383. curSC->result = DID_NO_CONNECT << 16;
  384. } else { /* Command complete, return status and message */
  385. curSC->result = (curSC->SCp.Status & 0xff)
  386. | ((curSC->SCp.Message & 0xff) << 8) | (DID_OK << 16);
  387. }
  388. goto idle_out;
  389. }
  390. switch (status & 0x07) { /* scsi phase */
  391. case 0x00: /* DATA-OUT */
  392. if (int_reg & 0x10) { /* Target requesting info transfer */
  393. curSC->SCp.phase = data_out;
  394. VDEB(printk("SYM53C500: Data-Out phase\n"));
  395. outb(FLUSH_FIFO, port_base + CMD_REG);
  396. LOAD_DMA_COUNT(port_base, curSC->request_bufflen); /* Max transfer size */
  397. outb(TRANSFER_INFO | DMA_OP, port_base + CMD_REG);
  398. if (!curSC->use_sg) /* Don't use scatter-gather */
  399. SYM53C500_pio_write(fast_pio, port_base, curSC->request_buffer, curSC->request_bufflen);
  400. else { /* use scatter-gather */
  401. sgcount = curSC->use_sg;
  402. sglist = curSC->request_buffer;
  403. while (sgcount--) {
  404. SYM53C500_pio_write(fast_pio, port_base, page_address(sglist->page) + sglist->offset, sglist->length);
  405. sglist++;
  406. }
  407. }
  408. REG0(port_base);
  409. }
  410. break;
  411. case 0x01: /* DATA-IN */
  412. if (int_reg & 0x10) { /* Target requesting info transfer */
  413. curSC->SCp.phase = data_in;
  414. VDEB(printk("SYM53C500: Data-In phase\n"));
  415. outb(FLUSH_FIFO, port_base + CMD_REG);
  416. LOAD_DMA_COUNT(port_base, curSC->request_bufflen); /* Max transfer size */
  417. outb(TRANSFER_INFO | DMA_OP, port_base + CMD_REG);
  418. if (!curSC->use_sg) /* Don't use scatter-gather */
  419. SYM53C500_pio_read(fast_pio, port_base, curSC->request_buffer, curSC->request_bufflen);
  420. else { /* Use scatter-gather */
  421. sgcount = curSC->use_sg;
  422. sglist = curSC->request_buffer;
  423. while (sgcount--) {
  424. SYM53C500_pio_read(fast_pio, port_base, page_address(sglist->page) + sglist->offset, sglist->length);
  425. sglist++;
  426. }
  427. }
  428. REG0(port_base);
  429. }
  430. break;
  431. case 0x02: /* COMMAND */
  432. curSC->SCp.phase = command_ph;
  433. printk("SYM53C500: Warning: Unknown interrupt occurred in command phase!\n");
  434. break;
  435. case 0x03: /* STATUS */
  436. curSC->SCp.phase = status_ph;
  437. VDEB(printk("SYM53C500: Status phase\n"));
  438. outb(FLUSH_FIFO, port_base + CMD_REG);
  439. outb(INIT_CMD_COMPLETE, port_base + CMD_REG);
  440. break;
  441. case 0x04: /* Reserved */
  442. case 0x05: /* Reserved */
  443. printk("SYM53C500: WARNING: Reserved phase!!!\n");
  444. break;
  445. case 0x06: /* MESSAGE-OUT */
  446. DEB(printk("SYM53C500: Message-Out phase\n"));
  447. curSC->SCp.phase = message_out;
  448. outb(SET_ATN, port_base + CMD_REG); /* Reject the message */
  449. outb(MSG_ACCEPT, port_base + CMD_REG);
  450. break;
  451. case 0x07: /* MESSAGE-IN */
  452. VDEB(printk("SYM53C500: Message-In phase\n"));
  453. curSC->SCp.phase = message_in;
  454. curSC->SCp.Status = inb(port_base + SCSI_FIFO);
  455. curSC->SCp.Message = inb(port_base + SCSI_FIFO);
  456. VDEB(printk("SCSI FIFO size=%d\n", inb(port_base + FIFO_FLAGS) & 0x1f));
  457. DEB(printk("Status = %02x Message = %02x\n", curSC->SCp.Status, curSC->SCp.Message));
  458. if (curSC->SCp.Message == SAVE_POINTERS || curSC->SCp.Message == DISCONNECT) {
  459. outb(SET_ATN, port_base + CMD_REG); /* Reject message */
  460. DEB(printk("Discarding SAVE_POINTERS message\n"));
  461. }
  462. outb(MSG_ACCEPT, port_base + CMD_REG);
  463. break;
  464. }
  465. out:
  466. spin_unlock_irqrestore(dev->host_lock, flags);
  467. return IRQ_HANDLED;
  468. idle_out:
  469. curSC->SCp.phase = idle;
  470. curSC->scsi_done(curSC);
  471. goto out;
  472. }
  473. static void
  474. SYM53C500_release(dev_link_t *link)
  475. {
  476. struct scsi_info_t *info = link->priv;
  477. struct Scsi_Host *shost = info->host;
  478. DEBUG(0, "SYM53C500_release(0x%p)\n", link);
  479. /*
  480. * Do this before releasing/freeing resources.
  481. */
  482. scsi_remove_host(shost);
  483. /*
  484. * Interrupts getting hosed on card removal. Try
  485. * the following code, mostly from qlogicfas.c.
  486. */
  487. if (shost->irq)
  488. free_irq(shost->irq, shost);
  489. if (shost->dma_channel != 0xff)
  490. free_dma(shost->dma_channel);
  491. if (shost->io_port && shost->n_io_port)
  492. release_region(shost->io_port, shost->n_io_port);
  493. link->dev = NULL;
  494. pcmcia_release_configuration(link->handle);
  495. pcmcia_release_io(link->handle, &link->io);
  496. pcmcia_release_irq(link->handle, &link->irq);
  497. link->state &= ~DEV_CONFIG;
  498. scsi_host_put(shost);
  499. } /* SYM53C500_release */
  500. static const char*
  501. SYM53C500_info(struct Scsi_Host *SChost)
  502. {
  503. static char info_msg[256];
  504. struct sym53c500_data *data =
  505. (struct sym53c500_data *)SChost->hostdata;
  506. DEB(printk("SYM53C500_info called\n"));
  507. (void)snprintf(info_msg, sizeof(info_msg),
  508. "SYM53C500 at 0x%lx, IRQ %d, %s PIO mode.",
  509. SChost->io_port, SChost->irq, data->fast_pio ? "fast" : "slow");
  510. return (info_msg);
  511. }
  512. static int
  513. SYM53C500_queue(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  514. {
  515. int i;
  516. int port_base = SCpnt->device->host->io_port;
  517. struct sym53c500_data *data =
  518. (struct sym53c500_data *)SCpnt->device->host->hostdata;
  519. VDEB(printk("SYM53C500_queue called\n"));
  520. DEB(printk("cmd=%02x, cmd_len=%02x, target=%02x, lun=%02x, bufflen=%d\n",
  521. SCpnt->cmnd[0], SCpnt->cmd_len, SCpnt->device->id,
  522. SCpnt->device->lun, SCpnt->request_bufflen));
  523. VDEB(for (i = 0; i < SCpnt->cmd_len; i++)
  524. printk("cmd[%d]=%02x ", i, SCpnt->cmnd[i]));
  525. VDEB(printk("\n"));
  526. data->current_SC = SCpnt;
  527. data->current_SC->scsi_done = done;
  528. data->current_SC->SCp.phase = command_ph;
  529. data->current_SC->SCp.Status = 0;
  530. data->current_SC->SCp.Message = 0;
  531. /* We are locked here already by the mid layer */
  532. REG0(port_base);
  533. outb(SCpnt->device->id, port_base + DEST_ID); /* set destination */
  534. outb(FLUSH_FIFO, port_base + CMD_REG); /* reset the fifos */
  535. for (i = 0; i < SCpnt->cmd_len; i++) {
  536. outb(SCpnt->cmnd[i], port_base + SCSI_FIFO);
  537. }
  538. outb(SELECT_NO_ATN, port_base + CMD_REG);
  539. return 0;
  540. }
  541. static int
  542. SYM53C500_host_reset(struct scsi_cmnd *SCpnt)
  543. {
  544. int port_base = SCpnt->device->host->io_port;
  545. DEB(printk("SYM53C500_host_reset called\n"));
  546. spin_lock_irq(SCpnt->device->host->host_lock);
  547. SYM53C500_int_host_reset(port_base);
  548. spin_unlock_irq(SCpnt->device->host->host_lock);
  549. return SUCCESS;
  550. }
  551. static int
  552. SYM53C500_biosparm(struct scsi_device *disk,
  553. struct block_device *dev,
  554. sector_t capacity, int *info_array)
  555. {
  556. int size;
  557. DEB(printk("SYM53C500_biosparm called\n"));
  558. size = capacity;
  559. info_array[0] = 64; /* heads */
  560. info_array[1] = 32; /* sectors */
  561. info_array[2] = size >> 11; /* cylinders */
  562. if (info_array[2] > 1024) { /* big disk */
  563. info_array[0] = 255;
  564. info_array[1] = 63;
  565. info_array[2] = size / (255 * 63);
  566. }
  567. return 0;
  568. }
  569. static ssize_t
  570. SYM53C500_show_pio(struct class_device *cdev, char *buf)
  571. {
  572. struct Scsi_Host *SHp = class_to_shost(cdev);
  573. struct sym53c500_data *data =
  574. (struct sym53c500_data *)SHp->hostdata;
  575. return snprintf(buf, 4, "%d\n", data->fast_pio);
  576. }
  577. static ssize_t
  578. SYM53C500_store_pio(struct class_device *cdev, const char *buf, size_t count)
  579. {
  580. int pio;
  581. struct Scsi_Host *SHp = class_to_shost(cdev);
  582. struct sym53c500_data *data =
  583. (struct sym53c500_data *)SHp->hostdata;
  584. pio = simple_strtoul(buf, NULL, 0);
  585. if (pio == 0 || pio == 1) {
  586. data->fast_pio = pio;
  587. return count;
  588. }
  589. else
  590. return -EINVAL;
  591. }
  592. /*
  593. * SCSI HBA device attributes we want to
  594. * make available via sysfs.
  595. */
  596. static struct class_device_attribute SYM53C500_pio_attr = {
  597. .attr = {
  598. .name = "fast_pio",
  599. .mode = (S_IRUGO | S_IWUSR),
  600. },
  601. .show = SYM53C500_show_pio,
  602. .store = SYM53C500_store_pio,
  603. };
  604. static struct class_device_attribute *SYM53C500_shost_attrs[] = {
  605. &SYM53C500_pio_attr,
  606. NULL,
  607. };
  608. /*
  609. * scsi_host_template initializer
  610. */
  611. static struct scsi_host_template sym53c500_driver_template = {
  612. .module = THIS_MODULE,
  613. .name = "SYM53C500",
  614. .info = SYM53C500_info,
  615. .queuecommand = SYM53C500_queue,
  616. .eh_host_reset_handler = SYM53C500_host_reset,
  617. .bios_param = SYM53C500_biosparm,
  618. .proc_name = "SYM53C500",
  619. .can_queue = 1,
  620. .this_id = 7,
  621. .sg_tablesize = 32,
  622. .cmd_per_lun = 1,
  623. .use_clustering = ENABLE_CLUSTERING,
  624. .shost_attrs = SYM53C500_shost_attrs
  625. };
  626. #define CS_CHECK(fn, ret) \
  627. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  628. static void
  629. SYM53C500_config(dev_link_t *link)
  630. {
  631. client_handle_t handle = link->handle;
  632. struct scsi_info_t *info = link->priv;
  633. tuple_t tuple;
  634. cisparse_t parse;
  635. int i, last_ret, last_fn;
  636. int irq_level, port_base;
  637. unsigned short tuple_data[32];
  638. struct Scsi_Host *host;
  639. struct scsi_host_template *tpnt = &sym53c500_driver_template;
  640. struct sym53c500_data *data;
  641. DEBUG(0, "SYM53C500_config(0x%p)\n", link);
  642. tuple.TupleData = (cisdata_t *)tuple_data;
  643. tuple.TupleDataMax = 64;
  644. tuple.TupleOffset = 0;
  645. tuple.DesiredTuple = CISTPL_CONFIG;
  646. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  647. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  648. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
  649. link->conf.ConfigBase = parse.config.base;
  650. tuple.DesiredTuple = CISTPL_MANFID;
  651. if ((pcmcia_get_first_tuple(handle, &tuple) == CS_SUCCESS) &&
  652. (pcmcia_get_tuple_data(handle, &tuple) == CS_SUCCESS))
  653. info->manf_id = le16_to_cpu(tuple.TupleData[0]);
  654. /* Configure card */
  655. link->state |= DEV_CONFIG;
  656. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  657. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  658. while (1) {
  659. if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
  660. pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
  661. goto next_entry;
  662. link->conf.ConfigIndex = parse.cftable_entry.index;
  663. link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
  664. link->io.NumPorts1 = parse.cftable_entry.io.win[0].len;
  665. if (link->io.BasePort1 != 0) {
  666. i = pcmcia_request_io(handle, &link->io);
  667. if (i == CS_SUCCESS)
  668. break;
  669. }
  670. next_entry:
  671. CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(handle, &tuple));
  672. }
  673. CS_CHECK(RequestIRQ, pcmcia_request_irq(handle, &link->irq));
  674. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(handle, &link->conf));
  675. /*
  676. * That's the trouble with copying liberally from another driver.
  677. * Some things probably aren't relevant, and I suspect this entire
  678. * section dealing with manufacturer IDs can be scrapped. --rct
  679. */
  680. if ((info->manf_id == MANFID_MACNICA) ||
  681. (info->manf_id == MANFID_PIONEER) ||
  682. (info->manf_id == 0x0098)) {
  683. /* set ATAcmd */
  684. outb(0xb4, link->io.BasePort1 + 0xd);
  685. outb(0x24, link->io.BasePort1 + 0x9);
  686. outb(0x04, link->io.BasePort1 + 0xd);
  687. }
  688. /*
  689. * irq_level == 0 implies tpnt->can_queue == 0, which
  690. * is not supported in 2.6. Thus, only irq_level > 0
  691. * will be allowed.
  692. *
  693. * Possible port_base values are as follows:
  694. *
  695. * 0x130, 0x230, 0x280, 0x290,
  696. * 0x320, 0x330, 0x340, 0x350
  697. */
  698. port_base = link->io.BasePort1;
  699. irq_level = link->irq.AssignedIRQ;
  700. DEB(printk("SYM53C500: port_base=0x%x, irq=%d, fast_pio=%d\n",
  701. port_base, irq_level, USE_FAST_PIO);)
  702. chip_init(port_base);
  703. host = scsi_host_alloc(tpnt, sizeof(struct sym53c500_data));
  704. if (!host) {
  705. printk("SYM53C500: Unable to register host, giving up.\n");
  706. goto err_release;
  707. }
  708. data = (struct sym53c500_data *)host->hostdata;
  709. if (irq_level > 0) {
  710. if (request_irq(irq_level, SYM53C500_intr, SA_SHIRQ, "SYM53C500", host)) {
  711. printk("SYM53C500: unable to allocate IRQ %d\n", irq_level);
  712. goto err_free_scsi;
  713. }
  714. DEB(printk("SYM53C500: allocated IRQ %d\n", irq_level));
  715. } else if (irq_level == 0) {
  716. DEB(printk("SYM53C500: No interrupts detected\n"));
  717. goto err_free_scsi;
  718. } else {
  719. DEB(printk("SYM53C500: Shouldn't get here!\n"));
  720. goto err_free_scsi;
  721. }
  722. host->unique_id = port_base;
  723. host->irq = irq_level;
  724. host->io_port = port_base;
  725. host->n_io_port = 0x10;
  726. host->dma_channel = -1;
  727. /*
  728. * Note fast_pio is set to USE_FAST_PIO by
  729. * default, but can be changed via "sysfs".
  730. */
  731. data->fast_pio = USE_FAST_PIO;
  732. sprintf(info->node.dev_name, "scsi%d", host->host_no);
  733. link->dev = &info->node;
  734. info->host = host;
  735. if (scsi_add_host(host, NULL))
  736. goto err_free_irq;
  737. scsi_scan_host(host);
  738. goto out; /* SUCCESS */
  739. err_free_irq:
  740. free_irq(irq_level, host);
  741. err_free_scsi:
  742. scsi_host_put(host);
  743. err_release:
  744. release_region(port_base, 0x10);
  745. printk(KERN_INFO "sym53c500_cs: no SCSI devices found\n");
  746. out:
  747. link->state &= ~DEV_CONFIG_PENDING;
  748. return;
  749. cs_failed:
  750. cs_error(link->handle, last_fn, last_ret);
  751. SYM53C500_release(link);
  752. return;
  753. } /* SYM53C500_config */
  754. static int
  755. SYM53C500_event(event_t event, int priority, event_callback_args_t *args)
  756. {
  757. dev_link_t *link = args->client_data;
  758. struct scsi_info_t *info = link->priv;
  759. DEBUG(1, "SYM53C500_event(0x%06x)\n", event);
  760. switch (event) {
  761. case CS_EVENT_CARD_REMOVAL:
  762. link->state &= ~DEV_PRESENT;
  763. if (link->state & DEV_CONFIG)
  764. SYM53C500_release(link);
  765. break;
  766. case CS_EVENT_CARD_INSERTION:
  767. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  768. SYM53C500_config(link);
  769. break;
  770. case CS_EVENT_PM_SUSPEND:
  771. link->state |= DEV_SUSPEND;
  772. /* Fall through... */
  773. case CS_EVENT_RESET_PHYSICAL:
  774. if (link->state & DEV_CONFIG)
  775. pcmcia_release_configuration(link->handle);
  776. break;
  777. case CS_EVENT_PM_RESUME:
  778. link->state &= ~DEV_SUSPEND;
  779. /* Fall through... */
  780. case CS_EVENT_CARD_RESET:
  781. if (link->state & DEV_CONFIG) {
  782. pcmcia_request_configuration(link->handle, &link->conf);
  783. /* See earlier comment about manufacturer IDs. */
  784. if ((info->manf_id == MANFID_MACNICA) ||
  785. (info->manf_id == MANFID_PIONEER) ||
  786. (info->manf_id == 0x0098)) {
  787. outb(0x80, link->io.BasePort1 + 0xd);
  788. outb(0x24, link->io.BasePort1 + 0x9);
  789. outb(0x04, link->io.BasePort1 + 0xd);
  790. }
  791. /*
  792. * If things don't work after a "resume",
  793. * this is a good place to start looking.
  794. */
  795. SYM53C500_int_host_reset(link->io.BasePort1);
  796. }
  797. break;
  798. }
  799. return 0;
  800. } /* SYM53C500_event */
  801. static void
  802. SYM53C500_detach(dev_link_t *link)
  803. {
  804. dev_link_t **linkp;
  805. DEBUG(0, "SYM53C500_detach(0x%p)\n", link);
  806. /* Locate device structure */
  807. for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  808. if (*linkp == link)
  809. break;
  810. if (*linkp == NULL)
  811. return;
  812. if (link->state & DEV_CONFIG)
  813. SYM53C500_release(link);
  814. if (link->handle)
  815. pcmcia_deregister_client(link->handle);
  816. /* Unlink device structure, free bits. */
  817. *linkp = link->next;
  818. kfree(link->priv);
  819. link->priv = NULL;
  820. } /* SYM53C500_detach */
  821. static dev_link_t *
  822. SYM53C500_attach(void)
  823. {
  824. struct scsi_info_t *info;
  825. client_reg_t client_reg;
  826. dev_link_t *link;
  827. int ret;
  828. DEBUG(0, "SYM53C500_attach()\n");
  829. /* Create new SCSI device */
  830. info = kmalloc(sizeof(*info), GFP_KERNEL);
  831. if (!info)
  832. return NULL;
  833. memset(info, 0, sizeof(*info));
  834. link = &info->link;
  835. link->priv = info;
  836. link->io.NumPorts1 = 16;
  837. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  838. link->io.IOAddrLines = 10;
  839. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  840. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  841. link->conf.Attributes = CONF_ENABLE_IRQ;
  842. link->conf.Vcc = 50;
  843. link->conf.IntType = INT_MEMORY_AND_IO;
  844. link->conf.Present = PRESENT_OPTION;
  845. /* Register with Card Services */
  846. link->next = dev_list;
  847. dev_list = link;
  848. client_reg.dev_info = &dev_info;
  849. client_reg.Version = 0x0210;
  850. client_reg.event_callback_args.client_data = link;
  851. ret = pcmcia_register_client(&link->handle, &client_reg);
  852. if (ret != 0) {
  853. cs_error(link->handle, RegisterClient, ret);
  854. SYM53C500_detach(link);
  855. return NULL;
  856. }
  857. return link;
  858. } /* SYM53C500_attach */
  859. MODULE_AUTHOR("Bob Tracy <rct@frus.com>");
  860. MODULE_DESCRIPTION("SYM53C500 PCMCIA SCSI driver");
  861. MODULE_LICENSE("GPL");
  862. static struct pcmcia_device_id sym53c500_ids[] = {
  863. PCMCIA_DEVICE_PROD_ID12("BASICS by New Media Corporation", "SCSI Sym53C500", 0x23c78a9d, 0x0099e7f7),
  864. PCMCIA_DEVICE_PROD_ID12("New Media Corporation", "SCSI Bus Toaster Sym53C500", 0x085a850b, 0x45432eb8),
  865. PCMCIA_DEVICE_PROD_ID2("SCSI9000", 0x21648f44),
  866. PCMCIA_DEVICE_NULL,
  867. };
  868. MODULE_DEVICE_TABLE(pcmcia, sym53c500_ids);
  869. static struct pcmcia_driver sym53c500_cs_driver = {
  870. .owner = THIS_MODULE,
  871. .drv = {
  872. .name = "sym53c500_cs",
  873. },
  874. .attach = SYM53C500_attach,
  875. .event = SYM53C500_event,
  876. .detach = SYM53C500_detach,
  877. .id_table = sym53c500_ids,
  878. };
  879. static int __init
  880. init_sym53c500_cs(void)
  881. {
  882. return pcmcia_register_driver(&sym53c500_cs_driver);
  883. }
  884. static void __exit
  885. exit_sym53c500_cs(void)
  886. {
  887. pcmcia_unregister_driver(&sym53c500_cs_driver);
  888. }
  889. module_init(init_sym53c500_cs);
  890. module_exit(exit_sym53c500_cs);