sa.c 9.9 KB

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