PageRenderTime 108ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/scsi/aacraid/sa.c

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