sa.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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-2010 Adaptec, Inc.
  9. * 2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; see the file COPYING. If not, write to
  23. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. * Module Name:
  26. * sa.c
  27. *
  28. * Abstract: Drawbridge specific support functions
  29. *
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/init.h>
  33. #include <linux/types.h>
  34. #include <linux/pci.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/blkdev.h>
  37. #include <linux/delay.h>
  38. #include <linux/completion.h>
  39. #include <linux/time.h>
  40. #include <linux/interrupt.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. static int aac_sa_restart_adapter(struct aac_dev *dev, int bled)
  235. {
  236. return -EINVAL;
  237. }
  238. /**
  239. * aac_sa_check_health
  240. * @dev: device to check if healthy
  241. *
  242. * Will attempt to determine if the specified adapter is alive and
  243. * capable of handling requests, returning 0 if alive.
  244. */
  245. static int aac_sa_check_health(struct aac_dev *dev)
  246. {
  247. long status = sa_readl(dev, Mailbox7);
  248. /*
  249. * Check to see if the board failed any self tests.
  250. */
  251. if (status & SELF_TEST_FAILED)
  252. return -1;
  253. /*
  254. * Check to see if the board panic'd while booting.
  255. */
  256. if (status & KERNEL_PANIC)
  257. return -2;
  258. /*
  259. * Wait for the adapter to be up and running. Wait up to 3 minutes
  260. */
  261. if (!(status & KERNEL_UP_AND_RUNNING))
  262. return -3;
  263. /*
  264. * Everything is OK
  265. */
  266. return 0;
  267. }
  268. /**
  269. * aac_sa_ioremap
  270. * @size: mapping resize request
  271. *
  272. */
  273. static int aac_sa_ioremap(struct aac_dev * dev, u32 size)
  274. {
  275. if (!size) {
  276. iounmap(dev->regs.sa);
  277. return 0;
  278. }
  279. dev->base = dev->regs.sa = ioremap(dev->base_start, size);
  280. return (dev->base == NULL) ? -1 : 0;
  281. }
  282. /**
  283. * aac_sa_init - initialize an ARM based AAC card
  284. * @dev: device to configure
  285. *
  286. * Allocate and set up resources for the ARM based AAC variants. The
  287. * device_interface in the commregion will be allocated and linked
  288. * to the comm region.
  289. */
  290. int aac_sa_init(struct aac_dev *dev)
  291. {
  292. unsigned long start;
  293. unsigned long status;
  294. int instance;
  295. const char *name;
  296. instance = dev->id;
  297. name = dev->name;
  298. if (aac_sa_ioremap(dev, dev->base_size)) {
  299. printk(KERN_WARNING "%s: unable to map adapter.\n", name);
  300. goto error_iounmap;
  301. }
  302. /*
  303. * Check to see if the board failed any self tests.
  304. */
  305. if (sa_readl(dev, Mailbox7) & SELF_TEST_FAILED) {
  306. printk(KERN_WARNING "%s%d: adapter self-test failed.\n", name, instance);
  307. goto error_iounmap;
  308. }
  309. /*
  310. * Check to see if the board panic'd while booting.
  311. */
  312. if (sa_readl(dev, Mailbox7) & KERNEL_PANIC) {
  313. printk(KERN_WARNING "%s%d: adapter kernel panic'd.\n", name, instance);
  314. goto error_iounmap;
  315. }
  316. start = jiffies;
  317. /*
  318. * Wait for the adapter to be up and running. Wait up to 3 minutes.
  319. */
  320. while (!(sa_readl(dev, Mailbox7) & KERNEL_UP_AND_RUNNING)) {
  321. if (time_after(jiffies, start+startup_timeout*HZ)) {
  322. status = sa_readl(dev, Mailbox7);
  323. printk(KERN_WARNING "%s%d: adapter kernel failed to start, init status = %lx.\n",
  324. name, instance, status);
  325. goto error_iounmap;
  326. }
  327. msleep(1);
  328. }
  329. /*
  330. * Fill in the function dispatch table.
  331. */
  332. dev->a_ops.adapter_interrupt = aac_sa_interrupt_adapter;
  333. dev->a_ops.adapter_disable_int = aac_sa_disable_interrupt;
  334. dev->a_ops.adapter_enable_int = aac_sa_enable_interrupt;
  335. dev->a_ops.adapter_notify = aac_sa_notify_adapter;
  336. dev->a_ops.adapter_sync_cmd = sa_sync_cmd;
  337. dev->a_ops.adapter_check_health = aac_sa_check_health;
  338. dev->a_ops.adapter_restart = aac_sa_restart_adapter;
  339. dev->a_ops.adapter_intr = aac_sa_intr;
  340. dev->a_ops.adapter_deliver = aac_rx_deliver_producer;
  341. dev->a_ops.adapter_ioremap = aac_sa_ioremap;
  342. /*
  343. * First clear out all interrupts. Then enable the one's that
  344. * we can handle.
  345. */
  346. aac_adapter_disable_int(dev);
  347. aac_adapter_enable_int(dev);
  348. if(aac_init_adapter(dev) == NULL)
  349. goto error_irq;
  350. dev->sync_mode = 0; /* sync. mode not supported */
  351. if (request_irq(dev->pdev->irq, dev->a_ops.adapter_intr,
  352. IRQF_SHARED, "aacraid", (void *)dev) < 0) {
  353. printk(KERN_WARNING "%s%d: Interrupt unavailable.\n",
  354. name, instance);
  355. goto error_iounmap;
  356. }
  357. dev->dbg_base = dev->base_start;
  358. dev->dbg_base_mapped = dev->base;
  359. dev->dbg_size = dev->base_size;
  360. aac_adapter_enable_int(dev);
  361. /*
  362. * Tell the adapter that all is configure, and it can start
  363. * accepting requests
  364. */
  365. aac_sa_start_adapter(dev);
  366. return 0;
  367. error_irq:
  368. aac_sa_disable_interrupt(dev);
  369. free_irq(dev->pdev->irq, (void *)dev);
  370. error_iounmap:
  371. return -1;
  372. }