NCR_D700.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 <asm/io.h>
  95. #include <scsi/scsi_host.h>
  96. #include <scsi/scsi_device.h>
  97. #include <scsi/scsi_transport.h>
  98. #include <scsi/scsi_transport_spi.h>
  99. #include "53c700.h"
  100. #include "NCR_D700.h"
  101. static char *NCR_D700; /* command line from insmod */
  102. MODULE_AUTHOR("James Bottomley");
  103. MODULE_DESCRIPTION("NCR Dual700 SCSI Driver");
  104. MODULE_LICENSE("GPL");
  105. module_param(NCR_D700, charp, 0);
  106. static __u8 __devinitdata id_array[2*(MCA_MAX_SLOT_NR + 1)] =
  107. { [0 ... 2*(MCA_MAX_SLOT_NR + 1)-1] = 7 };
  108. #ifdef MODULE
  109. #define ARG_SEP ' '
  110. #else
  111. #define ARG_SEP ','
  112. #endif
  113. static int __init
  114. param_setup(char *string)
  115. {
  116. char *pos = string, *next;
  117. int slot = -1, siop = -1;
  118. while(pos != NULL && (next = strchr(pos, ':')) != NULL) {
  119. int val = (int)simple_strtoul(++next, NULL, 0);
  120. if(!strncmp(pos, "slot:", 5))
  121. slot = val;
  122. else if(!strncmp(pos, "siop:", 5))
  123. siop = val;
  124. else if(!strncmp(pos, "id:", 3)) {
  125. if(slot == -1) {
  126. printk(KERN_WARNING "NCR D700: Must specify slot for id parameter\n");
  127. } else if(slot > MCA_MAX_SLOT_NR) {
  128. printk(KERN_WARNING "NCR D700: Illegal slot %d for id %d\n", slot, val);
  129. } else {
  130. if(siop != 0 && siop != 1) {
  131. id_array[slot*2] = val;
  132. id_array[slot*2 + 1] =val;
  133. } else {
  134. id_array[slot*2 + siop] = val;
  135. }
  136. }
  137. }
  138. if((pos = strchr(pos, ARG_SEP)) != NULL)
  139. pos++;
  140. }
  141. return 1;
  142. }
  143. /* Host template. The 53c700 routine NCR_700_detect will
  144. * fill in all of the missing routines */
  145. static struct scsi_host_template NCR_D700_driver_template = {
  146. .module = THIS_MODULE,
  147. .name = "NCR Dual 700 MCA",
  148. .proc_name = "NCR_D700",
  149. .this_id = 7,
  150. };
  151. /* We needs this helper because we have two hosts per struct device */
  152. struct NCR_D700_private {
  153. struct device *dev;
  154. struct Scsi_Host *hosts[2];
  155. char name[30];
  156. char pad;
  157. };
  158. static int __devinit
  159. NCR_D700_probe_one(struct NCR_D700_private *p, int siop, int irq,
  160. int slot, u32 region, int differential)
  161. {
  162. struct NCR_700_Host_Parameters *hostdata;
  163. struct Scsi_Host *host;
  164. int ret;
  165. hostdata = kzalloc(sizeof(*hostdata), GFP_KERNEL);
  166. if (!hostdata) {
  167. printk(KERN_ERR "NCR D700: SIOP%d: Failed to allocate host"
  168. "data, detatching\n", siop);
  169. return -ENOMEM;
  170. }
  171. if (!request_region(region, 64, "NCR_D700")) {
  172. printk(KERN_ERR "NCR D700: Failed to reserve IO region 0x%x\n",
  173. region);
  174. ret = -ENODEV;
  175. goto region_failed;
  176. }
  177. /* Fill in the three required pieces of hostdata */
  178. hostdata->base = ioport_map(region, 64);
  179. hostdata->differential = (((1<<siop) & differential) != 0);
  180. hostdata->clock = NCR_D700_CLOCK_MHZ;
  181. hostdata->burst_length = 8;
  182. /* and register the siop */
  183. host = NCR_700_detect(&NCR_D700_driver_template, hostdata, p->dev);
  184. if (!host) {
  185. ret = -ENOMEM;
  186. goto detect_failed;
  187. }
  188. p->hosts[siop] = host;
  189. /* FIXME: read this from SUS */
  190. host->this_id = id_array[slot * 2 + siop];
  191. host->irq = irq;
  192. host->base = region;
  193. scsi_scan_host(host);
  194. return 0;
  195. detect_failed:
  196. release_region(region, 64);
  197. region_failed:
  198. kfree(hostdata);
  199. return ret;
  200. }
  201. static int
  202. NCR_D700_intr(int irq, void *data)
  203. {
  204. struct NCR_D700_private *p = (struct NCR_D700_private *)data;
  205. int i, found = 0;
  206. for (i = 0; i < 2; i++)
  207. if (p->hosts[i] &&
  208. NCR_700_intr(irq, p->hosts[i]) == IRQ_HANDLED)
  209. found++;
  210. return found ? IRQ_HANDLED : IRQ_NONE;
  211. }
  212. /* Detect a D700 card. Note, because of the setup --- the chips are
  213. * essentially connectecd to the MCA bus independently, it is easier
  214. * to set them up as two separate host adapters, rather than one
  215. * adapter with two channels */
  216. static int __devinit
  217. NCR_D700_probe(struct device *dev)
  218. {
  219. struct NCR_D700_private *p;
  220. int differential;
  221. static int banner = 1;
  222. struct mca_device *mca_dev = to_mca_device(dev);
  223. int slot = mca_dev->slot;
  224. int found = 0;
  225. int irq, i;
  226. int pos3j, pos3k, pos3a, pos3b, pos4;
  227. __u32 base_addr, offset_addr;
  228. /* enable board interrupt */
  229. pos4 = mca_device_read_pos(mca_dev, 4);
  230. pos4 |= 0x4;
  231. mca_device_write_pos(mca_dev, 4, pos4);
  232. mca_device_write_pos(mca_dev, 6, 9);
  233. pos3j = mca_device_read_pos(mca_dev, 3);
  234. mca_device_write_pos(mca_dev, 6, 10);
  235. pos3k = mca_device_read_pos(mca_dev, 3);
  236. mca_device_write_pos(mca_dev, 6, 0);
  237. pos3a = mca_device_read_pos(mca_dev, 3);
  238. mca_device_write_pos(mca_dev, 6, 1);
  239. pos3b = mca_device_read_pos(mca_dev, 3);
  240. base_addr = ((pos3j << 8) | pos3k) & 0xfffffff0;
  241. offset_addr = ((pos3a << 8) | pos3b) & 0xffffff70;
  242. irq = (pos4 & 0x3) + 11;
  243. if(irq >= 13)
  244. irq++;
  245. if(banner) {
  246. printk(KERN_NOTICE "NCR D700: Driver Version " NCR_D700_VERSION "\n"
  247. "NCR D700: Copyright (c) 2001 by James.Bottomley@HansenPartnership.com\n"
  248. "NCR D700:\n");
  249. banner = 0;
  250. }
  251. /* now do the bus related transforms */
  252. irq = mca_device_transform_irq(mca_dev, irq);
  253. base_addr = mca_device_transform_ioport(mca_dev, base_addr);
  254. offset_addr = mca_device_transform_ioport(mca_dev, offset_addr);
  255. printk(KERN_NOTICE "NCR D700: found in slot %d irq = %d I/O base = 0x%x\n", slot, irq, offset_addr);
  256. /*outb(BOARD_RESET, base_addr);*/
  257. /* clear any pending interrupts */
  258. (void)inb(base_addr + 0x08);
  259. /* get modctl, used later for setting diff bits */
  260. switch(differential = (inb(base_addr + 0x08) >> 6)) {
  261. case 0x00:
  262. /* only SIOP1 differential */
  263. differential = 0x02;
  264. break;
  265. case 0x01:
  266. /* Both SIOPs differential */
  267. differential = 0x03;
  268. break;
  269. case 0x03:
  270. /* No SIOPs differential */
  271. differential = 0x00;
  272. break;
  273. default:
  274. printk(KERN_ERR "D700: UNEXPECTED DIFFERENTIAL RESULT 0x%02x\n",
  275. differential);
  276. differential = 0x00;
  277. break;
  278. }
  279. p = kzalloc(sizeof(*p), GFP_KERNEL);
  280. if (!p)
  281. return -ENOMEM;
  282. p->dev = dev;
  283. snprintf(p->name, sizeof(p->name), "D700(%s)", dev->bus_id);
  284. if (request_irq(irq, NCR_D700_intr, IRQF_SHARED, p->name, p)) {
  285. printk(KERN_ERR "D700: request_irq failed\n");
  286. kfree(p);
  287. return -EBUSY;
  288. }
  289. /* plumb in both 700 chips */
  290. for (i = 0; i < 2; i++) {
  291. int err;
  292. if ((err = NCR_D700_probe_one(p, i, irq, slot,
  293. offset_addr + (0x80 * i),
  294. differential)) != 0)
  295. printk("D700: SIOP%d: probe failed, error = %d\n",
  296. i, err);
  297. else
  298. found++;
  299. }
  300. if (!found) {
  301. kfree(p);
  302. return -ENODEV;
  303. }
  304. mca_device_set_claim(mca_dev, 1);
  305. mca_device_set_name(mca_dev, "NCR_D700");
  306. dev_set_drvdata(dev, p);
  307. return 0;
  308. }
  309. static void __devexit
  310. NCR_D700_remove_one(struct Scsi_Host *host)
  311. {
  312. scsi_remove_host(host);
  313. NCR_700_release(host);
  314. kfree((struct NCR_700_Host_Parameters *)host->hostdata[0]);
  315. free_irq(host->irq, host);
  316. release_region(host->base, 64);
  317. }
  318. static int __devexit
  319. NCR_D700_remove(struct device *dev)
  320. {
  321. struct NCR_D700_private *p = dev_get_drvdata(dev);
  322. int i;
  323. for (i = 0; i < 2; i++)
  324. NCR_D700_remove_one(p->hosts[i]);
  325. kfree(p);
  326. return 0;
  327. }
  328. static short NCR_D700_id_table[] = { NCR_D700_MCA_ID, 0 };
  329. static struct mca_driver NCR_D700_driver = {
  330. .id_table = NCR_D700_id_table,
  331. .driver = {
  332. .name = "NCR_D700",
  333. .bus = &mca_bus_type,
  334. .probe = NCR_D700_probe,
  335. .remove = __devexit_p(NCR_D700_remove),
  336. },
  337. };
  338. static int __init NCR_D700_init(void)
  339. {
  340. #ifdef MODULE
  341. if (NCR_D700)
  342. param_setup(NCR_D700);
  343. #endif
  344. return mca_register_driver(&NCR_D700_driver);
  345. }
  346. static void __exit NCR_D700_exit(void)
  347. {
  348. mca_unregister_driver(&NCR_D700_driver);
  349. }
  350. module_init(NCR_D700_init);
  351. module_exit(NCR_D700_exit);
  352. __setup("NCR_D700=", param_setup);