sa.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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/sched.h>
  34. #include <linux/pci.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/slab.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/delay.h>
  39. #include <linux/completion.h>
  40. #include <linux/time.h>
  41. #include <linux/interrupt.h>
  42. #include <asm/semaphore.h>
  43. #include <scsi/scsi_host.h>
  44. #include "aacraid.h"
  45. static irqreturn_t aac_sa_intr(int irq, void *dev_id, struct pt_regs *regs)
  46. {
  47. struct aac_dev *dev = dev_id;
  48. unsigned short intstat, mask;
  49. intstat = sa_readw(dev, DoorbellReg_p);
  50. /*
  51. * Read mask and invert because drawbridge is reversed.
  52. * This allows us to only service interrupts that have been enabled.
  53. */
  54. mask = ~(sa_readw(dev, SaDbCSR.PRISETIRQMASK));
  55. /* Check to see if this is our interrupt. If it isn't just return */
  56. if (intstat & mask) {
  57. if (intstat & PrintfReady) {
  58. aac_printf(dev, sa_readl(dev, Mailbox5));
  59. sa_writew(dev, DoorbellClrReg_p, PrintfReady); /* clear PrintfReady */
  60. sa_writew(dev, DoorbellReg_s, PrintfDone);
  61. } else if (intstat & DOORBELL_1) { // dev -> Host Normal Command Ready
  62. aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
  63. sa_writew(dev, DoorbellClrReg_p, DOORBELL_1);
  64. } else if (intstat & DOORBELL_2) { // dev -> Host Normal Response Ready
  65. aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
  66. sa_writew(dev, DoorbellClrReg_p, DOORBELL_2);
  67. } else if (intstat & DOORBELL_3) { // dev -> Host Normal Command Not Full
  68. sa_writew(dev, DoorbellClrReg_p, DOORBELL_3);
  69. } else if (intstat & DOORBELL_4) { // dev -> Host Normal Response Not Full
  70. sa_writew(dev, DoorbellClrReg_p, DOORBELL_4);
  71. }
  72. return IRQ_HANDLED;
  73. }
  74. return IRQ_NONE;
  75. }
  76. /**
  77. * aac_sa_notify_adapter - handle adapter notification
  78. * @dev: Adapter that notification is for
  79. * @event: Event to notidy
  80. *
  81. * Notify the adapter of an event
  82. */
  83. static void aac_sa_notify_adapter(struct aac_dev *dev, u32 event)
  84. {
  85. switch (event) {
  86. case AdapNormCmdQue:
  87. sa_writew(dev, DoorbellReg_s,DOORBELL_1);
  88. break;
  89. case HostNormRespNotFull:
  90. sa_writew(dev, DoorbellReg_s,DOORBELL_4);
  91. break;
  92. case AdapNormRespQue:
  93. sa_writew(dev, DoorbellReg_s,DOORBELL_2);
  94. break;
  95. case HostNormCmdNotFull:
  96. sa_writew(dev, DoorbellReg_s,DOORBELL_3);
  97. break;
  98. case HostShutdown:
  99. /*
  100. sa_sync_cmd(dev, HOST_CRASHING, 0, 0, 0, 0, 0, 0,
  101. NULL, NULL, NULL, NULL, NULL);
  102. */
  103. break;
  104. case FastIo:
  105. sa_writew(dev, DoorbellReg_s,DOORBELL_6);
  106. break;
  107. case AdapPrintfDone:
  108. sa_writew(dev, DoorbellReg_s,DOORBELL_5);
  109. break;
  110. default:
  111. BUG();
  112. break;
  113. }
  114. }
  115. /**
  116. * sa_sync_cmd - send a command and wait
  117. * @dev: Adapter
  118. * @command: Command to execute
  119. * @p1: first parameter
  120. * @ret: adapter status
  121. *
  122. * This routine will send a synchronous command to the adapter and wait
  123. * for its completion.
  124. */
  125. static int sa_sync_cmd(struct aac_dev *dev, u32 command,
  126. u32 p1, u32 p2, u32 p3, u32 p4, u32 p5, u32 p6,
  127. u32 *ret, u32 *r1, u32 *r2, u32 *r3, u32 *r4)
  128. {
  129. unsigned long start;
  130. int ok;
  131. /*
  132. * Write the Command into Mailbox 0
  133. */
  134. sa_writel(dev, Mailbox0, command);
  135. /*
  136. * Write the parameters into Mailboxes 1 - 4
  137. */
  138. sa_writel(dev, Mailbox1, p1);
  139. sa_writel(dev, Mailbox2, p2);
  140. sa_writel(dev, Mailbox3, p3);
  141. sa_writel(dev, Mailbox4, p4);
  142. /*
  143. * Clear the synch command doorbell to start on a clean slate.
  144. */
  145. sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
  146. /*
  147. * Signal that there is a new synch command
  148. */
  149. sa_writew(dev, DoorbellReg_s, DOORBELL_0);
  150. ok = 0;
  151. start = jiffies;
  152. while(time_before(jiffies, start+30*HZ))
  153. {
  154. /*
  155. * Delay 5uS so that the monitor gets access
  156. */
  157. udelay(5);
  158. /*
  159. * Mon110 will set doorbell0 bit when it has
  160. * completed the command.
  161. */
  162. if(sa_readw(dev, DoorbellReg_p) & DOORBELL_0) {
  163. ok = 1;
  164. break;
  165. }
  166. set_current_state(TASK_UNINTERRUPTIBLE);
  167. schedule_timeout(1);
  168. }
  169. if (ok != 1)
  170. return -ETIMEDOUT;
  171. /*
  172. * Clear the synch command doorbell.
  173. */
  174. sa_writew(dev, DoorbellClrReg_p, DOORBELL_0);
  175. /*
  176. * Pull the synch status from Mailbox 0.
  177. */
  178. if (ret)
  179. *ret = sa_readl(dev, Mailbox0);
  180. if (r1)
  181. *r1 = sa_readl(dev, Mailbox1);
  182. if (r2)
  183. *r2 = sa_readl(dev, Mailbox2);
  184. if (r3)
  185. *r3 = sa_readl(dev, Mailbox3);
  186. if (r4)
  187. *r4 = sa_readl(dev, Mailbox4);
  188. return 0;
  189. }
  190. /**
  191. * aac_sa_interrupt_adapter - interrupt an adapter
  192. * @dev: Which adapter to enable.
  193. *
  194. * Breakpoint an adapter.
  195. */
  196. static void aac_sa_interrupt_adapter (struct aac_dev *dev)
  197. {
  198. u32 ret;
  199. sa_sync_cmd(dev, BREAKPOINT_REQUEST, 0, 0, 0, 0, 0, 0,
  200. &ret, NULL, NULL, NULL, NULL);
  201. }
  202. /**
  203. * aac_sa_start_adapter - activate adapter
  204. * @dev: Adapter
  205. *
  206. * Start up processing on an ARM based AAC adapter
  207. */
  208. static void aac_sa_start_adapter(struct aac_dev *dev)
  209. {
  210. u32 ret;
  211. struct aac_init *init;
  212. /*
  213. * Fill in the remaining pieces of the init.
  214. */
  215. init = dev->init;
  216. init->HostElapsedSeconds = cpu_to_le32(get_seconds());
  217. /*
  218. * Tell the adapter we are back and up and running so it will scan its command
  219. * queues and enable our interrupts
  220. */
  221. dev->irq_mask = (PrintfReady | DOORBELL_1 | DOORBELL_2 | DOORBELL_3 | DOORBELL_4);
  222. /*
  223. * First clear out all interrupts. Then enable the one's that
  224. * we can handle.
  225. */
  226. sa_writew(dev, SaDbCSR.PRISETIRQMASK, 0xffff);
  227. sa_writew(dev, SaDbCSR.PRICLEARIRQMASK, (PrintfReady | DOORBELL_1 | DOORBELL_2 | DOORBELL_3 | DOORBELL_4));
  228. /* We can only use a 32 bit address here */
  229. sa_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS,
  230. (u32)(ulong)dev->init_pa, 0, 0, 0, 0, 0,
  231. &ret, NULL, NULL, NULL, NULL);
  232. }
  233. /**
  234. * aac_sa_check_health
  235. * @dev: device to check if healthy
  236. *
  237. * Will attempt to determine if the specified adapter is alive and
  238. * capable of handling requests, returning 0 if alive.
  239. */
  240. static int aac_sa_check_health(struct aac_dev *dev)
  241. {
  242. long status = sa_readl(dev, Mailbox7);
  243. /*
  244. * Check to see if the board failed any self tests.
  245. */
  246. if (status & SELF_TEST_FAILED)
  247. return -1;
  248. /*
  249. * Check to see if the board panic'd while booting.
  250. */
  251. if (status & KERNEL_PANIC)
  252. return -2;
  253. /*
  254. * Wait for the adapter to be up and running. Wait up to 3 minutes
  255. */
  256. if (!(status & KERNEL_UP_AND_RUNNING))
  257. return -3;
  258. /*
  259. * Everything is OK
  260. */
  261. return 0;
  262. }
  263. /**
  264. * aac_sa_init - initialize an ARM based AAC card
  265. * @dev: device to configure
  266. *
  267. * Allocate and set up resources for the ARM based AAC variants. The
  268. * device_interface in the commregion will be allocated and linked
  269. * to the comm region.
  270. */
  271. int aac_sa_init(struct aac_dev *dev)
  272. {
  273. unsigned long start;
  274. unsigned long status;
  275. int instance;
  276. const char *name;
  277. instance = dev->id;
  278. name = dev->name;
  279. /*
  280. * Map in the registers from the adapter.
  281. */
  282. if((dev->regs.sa = ioremap((unsigned long)dev->scsi_host_ptr->base, 8192))==NULL)
  283. {
  284. printk(KERN_WARNING "aacraid: unable to map ARM.\n" );
  285. goto error_iounmap;
  286. }
  287. /*
  288. * Check to see if the board failed any self tests.
  289. */
  290. if (sa_readl(dev, Mailbox7) & SELF_TEST_FAILED) {
  291. printk(KERN_WARNING "%s%d: adapter self-test failed.\n", name, instance);
  292. goto error_iounmap;
  293. }
  294. /*
  295. * Check to see if the board panic'd while booting.
  296. */
  297. if (sa_readl(dev, Mailbox7) & KERNEL_PANIC) {
  298. printk(KERN_WARNING "%s%d: adapter kernel panic'd.\n", name, instance);
  299. goto error_iounmap;
  300. }
  301. start = jiffies;
  302. /*
  303. * Wait for the adapter to be up and running. Wait up to 3 minutes.
  304. */
  305. while (!(sa_readl(dev, Mailbox7) & KERNEL_UP_AND_RUNNING)) {
  306. if (time_after(jiffies, start+180*HZ)) {
  307. status = sa_readl(dev, Mailbox7);
  308. printk(KERN_WARNING "%s%d: adapter kernel failed to start, init status = %lx.\n",
  309. name, instance, status);
  310. goto error_iounmap;
  311. }
  312. set_current_state(TASK_UNINTERRUPTIBLE);
  313. schedule_timeout(1);
  314. }
  315. if (request_irq(dev->scsi_host_ptr->irq, aac_sa_intr, SA_SHIRQ|SA_INTERRUPT, "aacraid", (void *)dev ) < 0) {
  316. printk(KERN_WARNING "%s%d: Interrupt unavailable.\n", name, instance);
  317. goto error_iounmap;
  318. }
  319. /*
  320. * Fill in the function dispatch table.
  321. */
  322. dev->a_ops.adapter_interrupt = aac_sa_interrupt_adapter;
  323. dev->a_ops.adapter_notify = aac_sa_notify_adapter;
  324. dev->a_ops.adapter_sync_cmd = sa_sync_cmd;
  325. dev->a_ops.adapter_check_health = aac_sa_check_health;
  326. if(aac_init_adapter(dev) == NULL)
  327. goto error_irq;
  328. /*
  329. * Start any kernel threads needed
  330. */
  331. dev->thread_pid = kernel_thread((int (*)(void *))aac_command_thread, dev, 0);
  332. if (dev->thread_pid < 0) {
  333. printk(KERN_ERR "aacraid: Unable to create command thread.\n");
  334. goto error_kfree;
  335. }
  336. /*
  337. * Tell the adapter that all is configure, and it can start
  338. * accepting requests
  339. */
  340. aac_sa_start_adapter(dev);
  341. return 0;
  342. error_kfree:
  343. kfree(dev->queues);
  344. error_irq:
  345. free_irq(dev->scsi_host_ptr->irq, (void *)dev);
  346. error_iounmap:
  347. iounmap(dev->regs.sa);
  348. return -1;
  349. }