sa.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Adaptec AAC series RAID controller driver
  3. * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
  4. *
  5. * based on the old aacraid driver that is..
  6. * Adaptec aacraid device driver for Linux.
  7. *
  8. * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2, or (at your option)
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; see the file COPYING. If not, write to
  22. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * Module Name:
  25. * sa.c
  26. *
  27. * Abstract: Drawbridge specific support functions
  28. *
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/init.h>
  32. #include <linux/types.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/slab.h>
  35. #include <linux/blkdev.h>
  36. #include <linux/delay.h>
  37. #include <linux/completion.h>
  38. #include <linux/time.h>
  39. #include <linux/interrupt.h>
  40. #include <asm/semaphore.h>
  41. #include <scsi/scsi_host.h>
  42. #include "aacraid.h"
  43. static irqreturn_t aac_sa_intr(int irq, void *dev_id)
  44. {
  45. struct aac_dev *dev = dev_id;
  46. unsigned short intstat, mask;
  47. intstat = sa_readw(dev, DoorbellReg_p);
  48. /*
  49. * Read mask and invert because drawbridge is reversed.
  50. * This allows us to only service interrupts that have been enabled.
  51. */
  52. mask = ~(sa_readw(dev, SaDbCSR.PRISETIRQMASK));
  53. /* Check to see if this is our interrupt. If it isn't just return */
  54. if (intstat & mask) {
  55. if (intstat & PrintfReady) {
  56. aac_printf(dev, sa_readl(dev, Mailbox5));
  57. sa_writew(dev, DoorbellClrReg_p, PrintfReady); /* clear PrintfReady */
  58. sa_writew(dev, DoorbellReg_s, PrintfDone);
  59. } else if (intstat & DOORBELL_1) { // dev -> Host Normal Command Ready
  60. sa_writew(dev, DoorbellClrReg_p, DOORBELL_1);
  61. aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
  62. } else if (intstat & DOORBELL_2) { // dev -> Host Normal Response Ready
  63. sa_writew(dev, DoorbellClrReg_p, DOORBELL_2);
  64. aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
  65. } else if (intstat & DOORBELL_3) { // dev -> Host Normal Command Not Full
  66. sa_writew(dev, DoorbellClrReg_p, DOORBELL_3);
  67. } else if (intstat & DOORBELL_4) { // dev -> Host Normal Response Not Full
  68. sa_writew(dev, DoorbellClrReg_p, DOORBELL_4);
  69. }
  70. return IRQ_HANDLED;
  71. }
  72. return IRQ_NONE;
  73. }
  74. /**
  75. * aac_sa_disable_interrupt - disable interrupt
  76. * @dev: Which adapter to enable.
  77. */
  78. static void aac_sa_disable_interrupt (struct aac_dev *dev)
  79. {
  80. sa_writew(dev, SaDbCSR.PRISETIRQMASK, 0xffff);
  81. }
  82. /**
  83. * aac_sa_enable_interrupt - enable interrupt
  84. * @dev: Which adapter to enable.
  85. */
  86. static void aac_sa_enable_interrupt (struct aac_dev *dev)
  87. {
  88. sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, (PrintfReady | DOORBELL_1 |
  89. DOORBELL_2 | DOORBELL_3 | DOORBELL_4));
  90. }
  91. /**
  92. * aac_sa_notify_adapter - handle adapter notification
  93. * @dev: Adapter that notification is for
  94. * @event: Event to notidy
  95. *
  96. * Notify the adapter of an event
  97. */
  98. static void aac_sa_notify_adapter(struct aac_dev *dev, u32 event)
  99. {
  100. switch (event) {
  101. case AdapNormCmdQue:
  102. sa_writew(dev, DoorbellReg_s,DOORBELL_1);
  103. break;
  104. case HostNormRespNotFull:
  105. sa_writew(dev, DoorbellReg_s,DOORBELL_4);
  106. break;
  107. case AdapNormRespQue:
  108. sa_writew(dev, DoorbellReg_s,DOORBELL_2);
  109. break;
  110. case HostNormCmdNotFull:
  111. sa_writew(dev, DoorbellReg_s,DOORBELL_3);
  112. break;
  113. case HostShutdown:
  114. /*
  115. sa_sync_cmd(dev, HOST_CRASHING, 0, 0, 0, 0, 0, 0,
  116. NULL, NULL, NULL, NULL, NULL);
  117. */
  118. break;
  119. case FastIo:
  120. sa_writew(dev, DoorbellReg_s,DOORBELL_6);
  121. break;
  122. case AdapPrintfDone:
  123. sa_writew(dev, DoorbellReg_s,DOORBELL_5);
  124. break;
  125. default:
  126. BUG();
  127. break;
  128. }
  129. }
  130. /**
  131. * sa_sync_cmd - send a command and wait
  132. * @dev: Adapter
  133. * @command: Command to execute
  134. * @p1: first parameter
  135. * @ret: adapter status
  136. *
  137. * This routine will send a synchronous command to the adapter and wait
  138. * for its completion.
  139. */
  140. static int sa_sync_cmd(struct aac_dev *dev, u32 command,
  141. u32 p1, u32 p2, u32 p3, u32 p4, u32 p5, u32 p6,
  142. u32 *ret, u32 *r1, u32 *r2, u32 *r3, u32 *r4)
  143. {
  144. unsigned long start;
  145. int ok;
  146. /*
  147. * Write the Command into Mailbox 0
  148. */
  149. sa_writel(dev, Mailbox0, command);
  150. /*
  151. * Write the parameters into Mailboxes 1 - 4
  152. */
  153. sa_writel(dev, Mailbox1, p1);
  154. sa_writel(dev, Mailbox2, p2);
  155. sa_writel(dev, Mailbox3, p3);
  156. sa_writel(dev, Mailbox4, p4);
  157. /*
  158. * Clear the synch command doorbell to start on a clean slate.
  159. */
  160. sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
  161. /*
  162. * Signal that there is a new synch command
  163. */
  164. sa_writew(dev, DoorbellReg_s, DOORBELL_0);
  165. ok = 0;
  166. start = jiffies;
  167. while(time_before(jiffies, start+30*HZ))
  168. {
  169. /*
  170. * Delay 5uS so that the monitor gets access
  171. */
  172. udelay(5);
  173. /*
  174. * Mon110 will set doorbell0 bit when it has
  175. * completed the command.
  176. */
  177. if(sa_readw(dev, DoorbellReg_p) & DOORBELL_0) {
  178. ok = 1;
  179. break;
  180. }
  181. msleep(1);
  182. }
  183. if (ok != 1)
  184. return -ETIMEDOUT;
  185. /*
  186. * Clear the synch command doorbell.
  187. */
  188. sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
  189. /*
  190. * Pull the synch status from Mailbox 0.
  191. */
  192. if (ret)
  193. *ret = sa_readl(dev, Mailbox0);
  194. if (r1)
  195. *r1 = sa_readl(dev, Mailbox1);
  196. if (r2)
  197. *r2 = sa_readl(dev, Mailbox2);
  198. if (r3)
  199. *r3 = sa_readl(dev, Mailbox3);
  200. if (r4)
  201. *r4 = sa_readl(dev, Mailbox4);
  202. return 0;
  203. }
  204. /**
  205. * aac_sa_interrupt_adapter - interrupt an adapter
  206. * @dev: Which adapter to enable.
  207. *
  208. * Breakpoint an adapter.
  209. */
  210. static void aac_sa_interrupt_adapter (struct aac_dev *dev)
  211. {
  212. sa_sync_cmd(dev, BREAKPOINT_REQUEST, 0, 0, 0, 0, 0, 0,
  213. NULL, NULL, NULL, NULL, NULL);
  214. }
  215. /**
  216. * aac_sa_start_adapter - activate adapter
  217. * @dev: Adapter
  218. *
  219. * Start up processing on an ARM based AAC adapter
  220. */
  221. static void aac_sa_start_adapter(struct aac_dev *dev)
  222. {
  223. struct aac_init *init;
  224. /*
  225. * Fill in the remaining pieces of the init.
  226. */
  227. init = dev->init;
  228. init->HostElapsedSeconds = cpu_to_le32(get_seconds());
  229. /* We can only use a 32 bit address here */
  230. sa_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS,
  231. (u32)(ulong)dev->init_pa, 0, 0, 0, 0, 0,
  232. NULL, NULL, NULL, NULL, NULL);
  233. }
  234. /**
  235. * aac_sa_check_health
  236. * @dev: device to check if healthy
  237. *
  238. * Will attempt to determine if the specified adapter is alive and
  239. * capable of handling requests, returning 0 if alive.
  240. */
  241. static int aac_sa_check_health(struct aac_dev *dev)
  242. {
  243. long status = sa_readl(dev, Mailbox7);
  244. /*
  245. * Check to see if the board failed any self tests.
  246. */
  247. if (status & SELF_TEST_FAILED)
  248. return -1;
  249. /*
  250. * Check to see if the board panic'd while booting.
  251. */
  252. if (status & KERNEL_PANIC)
  253. return -2;
  254. /*
  255. * Wait for the adapter to be up and running. Wait up to 3 minutes
  256. */
  257. if (!(status & KERNEL_UP_AND_RUNNING))
  258. return -3;
  259. /*
  260. * Everything is OK
  261. */
  262. return 0;
  263. }
  264. /**
  265. * aac_sa_ioremap
  266. * @size: mapping resize request
  267. *
  268. */
  269. static int aac_sa_ioremap(struct aac_dev * dev, u32 size)
  270. {
  271. if (!size) {
  272. iounmap(dev->regs.sa);
  273. return 0;
  274. }
  275. dev->base = dev->regs.sa = ioremap(dev->scsi_host_ptr->base, size);
  276. return (dev->base == NULL) ? -1 : 0;
  277. }
  278. /**
  279. * aac_sa_init - initialize an ARM based AAC card
  280. * @dev: device to configure
  281. *
  282. * Allocate and set up resources for the ARM based AAC variants. The
  283. * device_interface in the commregion will be allocated and linked
  284. * to the comm region.
  285. */
  286. int aac_sa_init(struct aac_dev *dev)
  287. {
  288. unsigned long start;
  289. unsigned long status;
  290. int instance;
  291. const char *name;
  292. instance = dev->id;
  293. name = dev->name;
  294. if (aac_sa_ioremap(dev, dev->base_size)) {
  295. printk(KERN_WARNING "%s: unable to map adapter.\n", name);
  296. goto error_iounmap;
  297. }
  298. /*
  299. * Check to see if the board failed any self tests.
  300. */
  301. if (sa_readl(dev, Mailbox7) & SELF_TEST_FAILED) {
  302. printk(KERN_WARNING "%s%d: adapter self-test failed.\n", name, instance);
  303. goto error_iounmap;
  304. }
  305. /*
  306. * Check to see if the board panic'd while booting.
  307. */
  308. if (sa_readl(dev, Mailbox7) & KERNEL_PANIC) {
  309. printk(KERN_WARNING "%s%d: adapter kernel panic'd.\n", name, instance);
  310. goto error_iounmap;
  311. }
  312. start = jiffies;
  313. /*
  314. * Wait for the adapter to be up and running. Wait up to 3 minutes.
  315. */
  316. while (!(sa_readl(dev, Mailbox7) & KERNEL_UP_AND_RUNNING)) {
  317. if (time_after(jiffies, start+startup_timeout*HZ)) {
  318. status = sa_readl(dev, Mailbox7);
  319. printk(KERN_WARNING "%s%d: adapter kernel failed to start, init status = %lx.\n",
  320. name, instance, status);
  321. goto error_iounmap;
  322. }
  323. msleep(1);
  324. }
  325. /*
  326. * Fill in the function dispatch table.
  327. */
  328. dev->a_ops.adapter_interrupt = aac_sa_interrupt_adapter;
  329. dev->a_ops.adapter_disable_int = aac_sa_disable_interrupt;
  330. dev->a_ops.adapter_enable_int = aac_sa_enable_interrupt;
  331. dev->a_ops.adapter_notify = aac_sa_notify_adapter;
  332. dev->a_ops.adapter_sync_cmd = sa_sync_cmd;
  333. dev->a_ops.adapter_check_health = aac_sa_check_health;
  334. dev->a_ops.adapter_intr = aac_sa_intr;
  335. dev->a_ops.adapter_ioremap = aac_sa_ioremap;
  336. /*
  337. * First clear out all interrupts. Then enable the one's that
  338. * we can handle.
  339. */
  340. aac_adapter_disable_int(dev);
  341. aac_adapter_enable_int(dev);
  342. if(aac_init_adapter(dev) == NULL)
  343. goto error_irq;
  344. if (request_irq(dev->scsi_host_ptr->irq, dev->a_ops.adapter_intr,
  345. IRQF_SHARED|IRQF_DISABLED,
  346. "aacraid", (void *)dev ) < 0) {
  347. printk(KERN_WARNING "%s%d: Interrupt unavailable.\n",
  348. name, instance);
  349. goto error_iounmap;
  350. }
  351. aac_adapter_enable_int(dev);
  352. /*
  353. * Tell the adapter that all is configure, and it can start
  354. * accepting requests
  355. */
  356. aac_sa_start_adapter(dev);
  357. return 0;
  358. error_irq:
  359. aac_sa_disable_interrupt(dev);
  360. free_irq(dev->scsi_host_ptr->irq, (void *)dev);
  361. error_iounmap:
  362. return -1;
  363. }