NCR_D700.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* -*- mode: c; c-basic-offset: 8 -*- */
  2. /* NCR Dual 700 MCA SCSI Driver
  3. *
  4. * Copyright (C) 2001 by James.Bottomley@HansenPartnership.com
  5. **-----------------------------------------------------------------------------
  6. **
  7. ** This program is free software; you can redistribute it and/or modify
  8. ** it under the terms of the GNU General Public License as published by
  9. ** the Free Software Foundation; either version 2 of the License, or
  10. ** (at your option) any later version.
  11. **
  12. ** This program is distributed in the hope that it will be useful,
  13. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ** GNU General Public License for more details.
  16. **
  17. ** You should have received a copy of the GNU General Public License
  18. ** along with this program; if not, write to the Free Software
  19. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. **
  21. **-----------------------------------------------------------------------------
  22. */
  23. /* Notes:
  24. *
  25. * Most of the work is done in the chip specific module, 53c700.o
  26. *
  27. * TODO List:
  28. *
  29. * 1. Extract the SCSI ID from the voyager CMOS table (necessary to
  30. * support multi-host environments.
  31. *
  32. * */
  33. /* CHANGELOG
  34. *
  35. * Version 2.2
  36. *
  37. * Added mca_set_adapter_name().
  38. *
  39. * Version 2.1
  40. *
  41. * Modularise the driver into a Board piece (this file) and a chip
  42. * piece 53c700.[ch] and 53c700.scr, added module options. You can
  43. * now specify the scsi id by the parameters
  44. *
  45. * NCR_D700=slot:<n> [siop:<n>] id:<n> ....
  46. *
  47. * They need to be comma separated if compiled into the kernel
  48. *
  49. * Version 2.0
  50. *
  51. * Initial implementation of TCQ (Tag Command Queueing). TCQ is full
  52. * featured and uses the clock algorithm to keep track of outstanding
  53. * tags and guard against individual tag starvation. Also fixed a bug
  54. * in all of the 1.x versions where the D700_data_residue() function
  55. * was returning results off by 32 bytes (and thus causing the same 32
  56. * bytes to be written twice corrupting the data block). It turns out
  57. * the 53c700 only has a 6 bit DBC and DFIFO registers not 7 bit ones
  58. * like the 53c710 (The 710 is the only data manual still available,
  59. * which I'd been using to program the 700).
  60. *
  61. * Version 1.2
  62. *
  63. * Much improved message handling engine
  64. *
  65. * Version 1.1
  66. *
  67. * Add code to handle selection reasonably correctly. By the time we
  68. * get the selection interrupt, we've already responded, but drop off the
  69. * bus and hope the selector will go away.
  70. *
  71. * Version 1.0:
  72. *
  73. * Initial release. Fully functional except for procfs and tag
  74. * command queueing. Has only been tested on cards with 53c700-66
  75. * chips and only single ended. Features are
  76. *
  77. * 1. Synchronous data transfers to offset 8 (limit of 700-66) and
  78. * 100ns (10MHz) limit of SCSI-2
  79. *
  80. * 2. Disconnection and reselection
  81. *
  82. * Testing:
  83. *
  84. * I've only really tested this with the 700-66 chip, but have done
  85. * soak tests in multi-device environments to verify that
  86. * disconnections and reselections are being processed correctly.
  87. * */
  88. #define NCR_D700_VERSION "2.2"
  89. #include <linux/blkdev.h>
  90. #include <linux/interrupt.h>
  91. #include <linux/kernel.h>
  92. #include <linux/module.h>
  93. #include <linux/mca.h>
  94. #include <linux/slab.h>
  95. #include <asm/io.h>
  96. #include <scsi/scsi_host.h>
  97. #include <scsi/scsi_device.h>
  98. #include <scsi/scsi_transport.h>
  99. #include <scsi/scsi_transport_spi.h>
  100. #include "53c700.h"
  101. #include "NCR_D700.h"
  102. static char *NCR_D700; /* command line from insmod */
  103. MODULE_AUTHOR("James Bottomley");
  104. MODULE_DESCRIPTION("NCR Dual700 SCSI Driver");
  105. MODULE_LICENSE("GPL");
  106. module_param(NCR_D700, charp, 0);
  107. static __u8 id_array[2*(MCA_MAX_SLOT_NR + 1)] =
  108. { [0 ... 2*(MCA_MAX_SLOT_NR + 1)-1] = 7 };
  109. #ifdef MODULE
  110. #define ARG_SEP ' '
  111. #else
  112. #define ARG_SEP ','
  113. #endif
  114. static int __init
  115. param_setup(char *string)
  116. {
  117. char *pos = string, *next;
  118. int slot = -1, siop = -1;
  119. while(pos != NULL && (next = strchr(pos, ':')) != NULL) {
  120. int val = (int)simple_strtoul(++next, NULL, 0);
  121. if(!strncmp(pos, "slot:", 5))
  122. slot = val;
  123. else if(!strncmp(pos, "siop:", 5))
  124. siop = val;
  125. else if(!strncmp(pos, "id:", 3)) {
  126. if(slot == -1) {
  127. printk(KERN_WARNING "NCR D700: Must specify slot for id parameter\n");
  128. } else if(slot > MCA_MAX_SLOT_NR) {
  129. printk(KERN_WARNING "NCR D700: Illegal slot %d for id %d\n", slot, val);
  130. } else {
  131. if(siop != 0 && siop != 1) {
  132. id_array[slot*2] = val;
  133. id_array[slot*2 + 1] =val;
  134. } else {
  135. id_array[slot*2 + siop] = val;
  136. }
  137. }
  138. }
  139. if((pos = strchr(pos, ARG_SEP)) != NULL)
  140. pos++;
  141. }
  142. return 1;
  143. }
  144. /* Host template. The 53c700 routine NCR_700_detect will
  145. * fill in all of the missing routines */
  146. static struct scsi_host_template NCR_D700_driver_template = {
  147. .module = THIS_MODULE,
  148. .name = "NCR Dual 700 MCA",
  149. .proc_name = "NCR_D700",
  150. .this_id = 7,
  151. };
  152. /* We needs this helper because we have two hosts per struct device */
  153. struct NCR_D700_private {
  154. struct device *dev;
  155. struct Scsi_Host *hosts[2];
  156. char name[30];
  157. char pad;
  158. };
  159. static int
  160. NCR_D700_probe_one(struct NCR_D700_private *p, int siop, int irq,
  161. int slot, u32 region, int differential)
  162. {
  163. struct NCR_700_Host_Parameters *hostdata;
  164. struct Scsi_Host *host;
  165. int ret;
  166. hostdata = kzalloc(sizeof(*hostdata), GFP_KERNEL);
  167. if (!hostdata) {
  168. printk(KERN_ERR "NCR D700: SIOP%d: Failed to allocate host"
  169. "data, detatching\n", siop);
  170. return -ENOMEM;
  171. }
  172. if (!request_region(region, 64, "NCR_D700")) {
  173. printk(KERN_ERR "NCR D700: Failed to reserve IO region 0x%x\n",
  174. region);
  175. ret = -ENODEV;
  176. goto region_failed;
  177. }
  178. /* Fill in the three required pieces of hostdata */
  179. hostdata->base = ioport_map(region, 64);
  180. hostdata->differential = (((1<<siop) & differential) != 0);
  181. hostdata->clock = NCR_D700_CLOCK_MHZ;
  182. hostdata->burst_length = 8;
  183. /* and register the siop */
  184. host = NCR_700_detect(&NCR_D700_driver_template, hostdata, p->dev);
  185. if (!host) {
  186. ret = -ENOMEM;
  187. goto detect_failed;
  188. }
  189. p->hosts[siop] = host;
  190. /* FIXME: read this from SUS */
  191. host->this_id = id_array[slot * 2 + siop];
  192. host->irq = irq;
  193. host->base = region;
  194. scsi_scan_host(host);
  195. return 0;
  196. detect_failed:
  197. release_region(region, 64);
  198. region_failed:
  199. kfree(hostdata);
  200. return ret;
  201. }
  202. static irqreturn_t
  203. NCR_D700_intr(int irq, void *data)
  204. {
  205. struct NCR_D700_private *p = (struct NCR_D700_private *)data;
  206. int i, found = 0;
  207. for (i = 0; i < 2; i++)
  208. if (p->hosts[i] &&
  209. NCR_700_intr(irq, p->hosts[i]) == IRQ_HANDLED)
  210. found++;
  211. return found ? IRQ_HANDLED : IRQ_NONE;
  212. }
  213. /* Detect a D700 card. Note, because of the setup --- the chips are
  214. * essentially connectecd to the MCA bus independently, it is easier
  215. * to set them up as two separate host adapters, rather than one
  216. * adapter with two channels */
  217. static int
  218. NCR_D700_probe(struct device *dev)
  219. {
  220. struct NCR_D700_private *p;
  221. int differential;
  222. static int banner = 1;
  223. struct mca_device *mca_dev = to_mca_device(dev);
  224. int slot = mca_dev->slot;
  225. int found = 0;
  226. int irq, i;
  227. int pos3j, pos3k, pos3a, pos3b, pos4;
  228. __u32 base_addr, offset_addr;
  229. /* enable board interrupt */
  230. pos4 = mca_device_read_pos(mca_dev, 4);
  231. pos4 |= 0x4;
  232. mca_device_write_pos(mca_dev, 4, pos4);
  233. mca_device_write_pos(mca_dev, 6, 9);
  234. pos3j = mca_device_read_pos(mca_dev, 3);
  235. mca_device_write_pos(mca_dev, 6, 10);
  236. pos3k = mca_device_read_pos(mca_dev, 3);
  237. mca_device_write_pos(mca_dev, 6, 0);
  238. pos3a = mca_device_read_pos(mca_dev, 3);
  239. mca_device_write_pos(mca_dev, 6, 1);
  240. pos3b = mca_device_read_pos(mca_dev, 3);
  241. base_addr = ((pos3j << 8) | pos3k) & 0xfffffff0;
  242. offset_addr = ((pos3a << 8) | pos3b) & 0xffffff70;
  243. irq = (pos4 & 0x3) + 11;
  244. if(irq >= 13)
  245. irq++;
  246. if(banner) {
  247. printk(KERN_NOTICE "NCR D700: Driver Version " NCR_D700_VERSION "\n"
  248. "NCR D700: Copyright (c) 2001 by James.Bottomley@HansenPartnership.com\n"
  249. "NCR D700:\n");
  250. banner = 0;
  251. }
  252. /* now do the bus related transforms */
  253. irq = mca_device_transform_irq(mca_dev, irq);
  254. base_addr = mca_device_transform_ioport(mca_dev, base_addr);
  255. offset_addr = mca_device_transform_ioport(mca_dev, offset_addr);
  256. printk(KERN_NOTICE "NCR D700: found in slot %d irq = %d I/O base = 0x%x\n", slot, irq, offset_addr);
  257. /*outb(BOARD_RESET, base_addr);*/
  258. /* clear any pending interrupts */
  259. (void)inb(base_addr + 0x08);
  260. /* get modctl, used later for setting diff bits */
  261. switch(differential = (inb(base_addr + 0x08) >> 6)) {
  262. case 0x00:
  263. /* only SIOP1 differential */
  264. differential = 0x02;
  265. break;
  266. case 0x01:
  267. /* Both SIOPs differential */
  268. differential = 0x03;
  269. break;
  270. case 0x03:
  271. /* No SIOPs differential */
  272. differential = 0x00;
  273. break;
  274. default:
  275. printk(KERN_ERR "D700: UNEXPECTED DIFFERENTIAL RESULT 0x%02x\n",
  276. differential);
  277. differential = 0x00;
  278. break;
  279. }
  280. p = kzalloc(sizeof(*p), GFP_KERNEL);
  281. if (!p)
  282. return -ENOMEM;
  283. p->dev = dev;
  284. snprintf(p->name, sizeof(p->name), "D700(%s)", dev_name(dev));
  285. if (request_irq(irq, NCR_D700_intr, IRQF_SHARED, p->name, p)) {
  286. printk(KERN_ERR "D700: request_irq failed\n");
  287. kfree(p);
  288. return -EBUSY;
  289. }
  290. /* plumb in both 700 chips */
  291. for (i = 0; i < 2; i++) {
  292. int err;
  293. if ((err = NCR_D700_probe_one(p, i, irq, slot,
  294. offset_addr + (0x80 * i),
  295. differential)) != 0)
  296. printk("D700: SIOP%d: probe failed, error = %d\n",
  297. i, err);
  298. else
  299. found++;
  300. }
  301. if (!found) {
  302. kfree(p);
  303. return -ENODEV;
  304. }
  305. mca_device_set_claim(mca_dev, 1);
  306. mca_device_set_name(mca_dev, "NCR_D700");
  307. dev_set_drvdata(dev, p);
  308. return 0;
  309. }
  310. static void
  311. NCR_D700_remove_one(struct Scsi_Host *host)
  312. {
  313. scsi_remove_host(host);
  314. NCR_700_release(host);
  315. kfree((struct NCR_700_Host_Parameters *)host->hostdata[0]);
  316. free_irq(host->irq, host);
  317. release_region(host->base, 64);
  318. }
  319. static int
  320. NCR_D700_remove(struct device *dev)
  321. {
  322. struct NCR_D700_private *p = dev_get_drvdata(dev);
  323. int i;
  324. for (i = 0; i < 2; i++)
  325. NCR_D700_remove_one(p->hosts[i]);
  326. kfree(p);
  327. return 0;
  328. }
  329. static short NCR_D700_id_table[] = { NCR_D700_MCA_ID, 0 };
  330. static struct mca_driver NCR_D700_driver = {
  331. .id_table = NCR_D700_id_table,
  332. .driver = {
  333. .name = "NCR_D700",
  334. .bus = &mca_bus_type,
  335. .probe = NCR_D700_probe,
  336. .remove = NCR_D700_remove,
  337. },
  338. };
  339. static int __init NCR_D700_init(void)
  340. {
  341. #ifdef MODULE
  342. if (NCR_D700)
  343. param_setup(NCR_D700);
  344. #endif
  345. return mca_register_driver(&NCR_D700_driver);
  346. }
  347. static void __exit NCR_D700_exit(void)
  348. {
  349. mca_unregister_driver(&NCR_D700_driver);
  350. }
  351. module_init(NCR_D700_init);
  352. module_exit(NCR_D700_exit);
  353. __setup("NCR_D700=", param_setup);