src.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. * src.c
  27. *
  28. * Abstract: Hardware Device Interface for PMC SRC based controllers
  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/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 <scsi/scsi_host.h>
  43. #include "aacraid.h"
  44. static irqreturn_t aac_src_intr_message(int irq, void *dev_id)
  45. {
  46. struct aac_dev *dev = dev_id;
  47. unsigned long bellbits, bellbits_shifted;
  48. int our_interrupt = 0;
  49. int isFastResponse;
  50. u32 index, handle;
  51. bellbits = src_readl(dev, MUnit.ODR_R);
  52. if (bellbits & PmDoorBellResponseSent) {
  53. bellbits = PmDoorBellResponseSent;
  54. /* handle async. status */
  55. our_interrupt = 1;
  56. index = dev->host_rrq_idx;
  57. if (dev->host_rrq[index] == 0) {
  58. u32 old_index = index;
  59. /* adjust index */
  60. do {
  61. index++;
  62. if (index == dev->scsi_host_ptr->can_queue +
  63. AAC_NUM_MGT_FIB)
  64. index = 0;
  65. if (dev->host_rrq[index] != 0)
  66. break;
  67. } while (index != old_index);
  68. dev->host_rrq_idx = index;
  69. }
  70. for (;;) {
  71. isFastResponse = 0;
  72. /* remove toggle bit (31) */
  73. handle = (dev->host_rrq[index] & 0x7fffffff);
  74. /* check fast response bit (30) */
  75. if (handle & 0x40000000)
  76. isFastResponse = 1;
  77. handle &= 0x0000ffff;
  78. if (handle == 0)
  79. break;
  80. aac_intr_normal(dev, handle-1, 0, isFastResponse, NULL);
  81. dev->host_rrq[index++] = 0;
  82. if (index == dev->scsi_host_ptr->can_queue +
  83. AAC_NUM_MGT_FIB)
  84. index = 0;
  85. dev->host_rrq_idx = index;
  86. }
  87. } else {
  88. bellbits_shifted = (bellbits >> SRC_ODR_SHIFT);
  89. if (bellbits_shifted & DoorBellAifPending) {
  90. our_interrupt = 1;
  91. /* handle AIF */
  92. aac_intr_normal(dev, 0, 2, 0, NULL);
  93. }
  94. }
  95. if (our_interrupt) {
  96. src_writel(dev, MUnit.ODR_C, bellbits);
  97. return IRQ_HANDLED;
  98. }
  99. return IRQ_NONE;
  100. }
  101. /**
  102. * aac_src_disable_interrupt - Disable interrupts
  103. * @dev: Adapter
  104. */
  105. static void aac_src_disable_interrupt(struct aac_dev *dev)
  106. {
  107. src_writel(dev, MUnit.OIMR, dev->OIMR = 0xffffffff);
  108. }
  109. /**
  110. * aac_src_enable_interrupt_message - Enable interrupts
  111. * @dev: Adapter
  112. */
  113. static void aac_src_enable_interrupt_message(struct aac_dev *dev)
  114. {
  115. src_writel(dev, MUnit.OIMR, dev->OIMR = 0xfffffff8);
  116. }
  117. /**
  118. * src_sync_cmd - send a command and wait
  119. * @dev: Adapter
  120. * @command: Command to execute
  121. * @p1: first parameter
  122. * @ret: adapter status
  123. *
  124. * This routine will send a synchronous command to the adapter and wait
  125. * for its completion.
  126. */
  127. static int src_sync_cmd(struct aac_dev *dev, u32 command,
  128. u32 p1, u32 p2, u32 p3, u32 p4, u32 p5, u32 p6,
  129. u32 *status, u32 * r1, u32 * r2, u32 * r3, u32 * r4)
  130. {
  131. unsigned long start;
  132. int ok;
  133. /*
  134. * Write the command into Mailbox 0
  135. */
  136. writel(command, &dev->IndexRegs->Mailbox[0]);
  137. /*
  138. * Write the parameters into Mailboxes 1 - 6
  139. */
  140. writel(p1, &dev->IndexRegs->Mailbox[1]);
  141. writel(p2, &dev->IndexRegs->Mailbox[2]);
  142. writel(p3, &dev->IndexRegs->Mailbox[3]);
  143. writel(p4, &dev->IndexRegs->Mailbox[4]);
  144. /*
  145. * Clear the synch command doorbell to start on a clean slate.
  146. */
  147. src_writel(dev, MUnit.ODR_C, OUTBOUNDDOORBELL_0 << SRC_ODR_SHIFT);
  148. /*
  149. * Disable doorbell interrupts
  150. */
  151. src_writel(dev, MUnit.OIMR, dev->OIMR = 0xffffffff);
  152. /*
  153. * Force the completion of the mask register write before issuing
  154. * the interrupt.
  155. */
  156. src_readl(dev, MUnit.OIMR);
  157. /*
  158. * Signal that there is a new synch command
  159. */
  160. src_writel(dev, MUnit.IDR, INBOUNDDOORBELL_0 << SRC_IDR_SHIFT);
  161. ok = 0;
  162. start = jiffies;
  163. /*
  164. * Wait up to 30 seconds
  165. */
  166. while (time_before(jiffies, start+30*HZ)) {
  167. /* Delay 5 microseconds to let Mon960 get info. */
  168. udelay(5);
  169. /* Mon960 will set doorbell0 bit
  170. * when it has completed the command
  171. */
  172. if ((src_readl(dev, MUnit.ODR_R) >> SRC_ODR_SHIFT) & OUTBOUNDDOORBELL_0) {
  173. /* Clear the doorbell */
  174. src_writel(dev,
  175. MUnit.ODR_C,
  176. OUTBOUNDDOORBELL_0 << SRC_ODR_SHIFT);
  177. ok = 1;
  178. break;
  179. }
  180. /* Yield the processor in case we are slow */
  181. msleep(1);
  182. }
  183. if (unlikely(ok != 1)) {
  184. /* Restore interrupt mask even though we timed out */
  185. aac_adapter_enable_int(dev);
  186. return -ETIMEDOUT;
  187. }
  188. /* Pull the synch status from Mailbox 0 */
  189. if (status)
  190. *status = readl(&dev->IndexRegs->Mailbox[0]);
  191. if (r1)
  192. *r1 = readl(&dev->IndexRegs->Mailbox[1]);
  193. if (r2)
  194. *r2 = readl(&dev->IndexRegs->Mailbox[2]);
  195. if (r3)
  196. *r3 = readl(&dev->IndexRegs->Mailbox[3]);
  197. if (r4)
  198. *r4 = readl(&dev->IndexRegs->Mailbox[4]);
  199. /* Clear the synch command doorbell */
  200. src_writel(dev, MUnit.ODR_C, OUTBOUNDDOORBELL_0 << SRC_ODR_SHIFT);
  201. /* Restore interrupt mask */
  202. aac_adapter_enable_int(dev);
  203. return 0;
  204. }
  205. /**
  206. * aac_src_interrupt_adapter - interrupt adapter
  207. * @dev: Adapter
  208. *
  209. * Send an interrupt to the i960 and breakpoint it.
  210. */
  211. static void aac_src_interrupt_adapter(struct aac_dev *dev)
  212. {
  213. src_sync_cmd(dev, BREAKPOINT_REQUEST,
  214. 0, 0, 0, 0, 0, 0,
  215. NULL, NULL, NULL, NULL, NULL);
  216. }
  217. /**
  218. * aac_src_notify_adapter - send an event to the adapter
  219. * @dev: Adapter
  220. * @event: Event to send
  221. *
  222. * Notify the i960 that something it probably cares about has
  223. * happened.
  224. */
  225. static void aac_src_notify_adapter(struct aac_dev *dev, u32 event)
  226. {
  227. switch (event) {
  228. case AdapNormCmdQue:
  229. src_writel(dev, MUnit.ODR_C,
  230. INBOUNDDOORBELL_1 << SRC_ODR_SHIFT);
  231. break;
  232. case HostNormRespNotFull:
  233. src_writel(dev, MUnit.ODR_C,
  234. INBOUNDDOORBELL_4 << SRC_ODR_SHIFT);
  235. break;
  236. case AdapNormRespQue:
  237. src_writel(dev, MUnit.ODR_C,
  238. INBOUNDDOORBELL_2 << SRC_ODR_SHIFT);
  239. break;
  240. case HostNormCmdNotFull:
  241. src_writel(dev, MUnit.ODR_C,
  242. INBOUNDDOORBELL_3 << SRC_ODR_SHIFT);
  243. break;
  244. case FastIo:
  245. src_writel(dev, MUnit.ODR_C,
  246. INBOUNDDOORBELL_6 << SRC_ODR_SHIFT);
  247. break;
  248. case AdapPrintfDone:
  249. src_writel(dev, MUnit.ODR_C,
  250. INBOUNDDOORBELL_5 << SRC_ODR_SHIFT);
  251. break;
  252. default:
  253. BUG();
  254. break;
  255. }
  256. }
  257. /**
  258. * aac_src_start_adapter - activate adapter
  259. * @dev: Adapter
  260. *
  261. * Start up processing on an i960 based AAC adapter
  262. */
  263. static void aac_src_start_adapter(struct aac_dev *dev)
  264. {
  265. struct aac_init *init;
  266. init = dev->init;
  267. init->HostElapsedSeconds = cpu_to_le32(get_seconds());
  268. /* We can only use a 32 bit address here */
  269. src_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS, (u32)(ulong)dev->init_pa,
  270. 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL);
  271. }
  272. /**
  273. * aac_src_check_health
  274. * @dev: device to check if healthy
  275. *
  276. * Will attempt to determine if the specified adapter is alive and
  277. * capable of handling requests, returning 0 if alive.
  278. */
  279. static int aac_src_check_health(struct aac_dev *dev)
  280. {
  281. u32 status = src_readl(dev, MUnit.OMR);
  282. /*
  283. * Check to see if the board failed any self tests.
  284. */
  285. if (unlikely(status & SELF_TEST_FAILED))
  286. return -1;
  287. /*
  288. * Check to see if the board panic'd.
  289. */
  290. if (unlikely(status & KERNEL_PANIC))
  291. return (status >> 16) & 0xFF;
  292. /*
  293. * Wait for the adapter to be up and running.
  294. */
  295. if (unlikely(!(status & KERNEL_UP_AND_RUNNING)))
  296. return -3;
  297. /*
  298. * Everything is OK
  299. */
  300. return 0;
  301. }
  302. /**
  303. * aac_src_deliver_message
  304. * @fib: fib to issue
  305. *
  306. * Will send a fib, returning 0 if successful.
  307. */
  308. static int aac_src_deliver_message(struct fib *fib)
  309. {
  310. struct aac_dev *dev = fib->dev;
  311. struct aac_queue *q = &dev->queues->queue[AdapNormCmdQueue];
  312. unsigned long qflags;
  313. u32 fibsize;
  314. u64 address;
  315. struct aac_fib_xporthdr *pFibX;
  316. spin_lock_irqsave(q->lock, qflags);
  317. q->numpending++;
  318. spin_unlock_irqrestore(q->lock, qflags);
  319. /* Calculate the amount to the fibsize bits */
  320. fibsize = (sizeof(struct aac_fib_xporthdr) +
  321. fib->hw_fib_va->header.Size + 127) / 128 - 1;
  322. if (fibsize > (ALIGN32 - 1))
  323. fibsize = ALIGN32 - 1;
  324. /* Fill XPORT header */
  325. pFibX = (struct aac_fib_xporthdr *)
  326. ((unsigned char *)fib->hw_fib_va -
  327. sizeof(struct aac_fib_xporthdr));
  328. pFibX->Handle = fib->hw_fib_va->header.SenderData + 1;
  329. pFibX->HostAddress = fib->hw_fib_pa;
  330. pFibX->Size = fib->hw_fib_va->header.Size;
  331. address = fib->hw_fib_pa - (u64)sizeof(struct aac_fib_xporthdr);
  332. src_writel(dev, MUnit.IQ_H, (u32)(address >> 32));
  333. src_writel(dev, MUnit.IQ_L, (u32)(address & 0xffffffff) + fibsize);
  334. return 0;
  335. }
  336. /**
  337. * aac_src_ioremap
  338. * @size: mapping resize request
  339. *
  340. */
  341. static int aac_src_ioremap(struct aac_dev *dev, u32 size)
  342. {
  343. if (!size) {
  344. iounmap(dev->regs.src.bar0);
  345. dev->regs.src.bar0 = NULL;
  346. iounmap(dev->base);
  347. dev->base = NULL;
  348. return 0;
  349. }
  350. dev->regs.src.bar1 = ioremap(pci_resource_start(dev->pdev, 2),
  351. AAC_MIN_SRC_BAR1_SIZE);
  352. dev->base = NULL;
  353. if (dev->regs.src.bar1 == NULL)
  354. return -1;
  355. dev->base = dev->regs.src.bar0 = ioremap(dev->scsi_host_ptr->base,
  356. size);
  357. if (dev->base == NULL) {
  358. iounmap(dev->regs.src.bar1);
  359. dev->regs.src.bar1 = NULL;
  360. return -1;
  361. }
  362. dev->IndexRegs = &((struct src_registers __iomem *)
  363. dev->base)->IndexRegs;
  364. return 0;
  365. }
  366. static int aac_src_restart_adapter(struct aac_dev *dev, int bled)
  367. {
  368. u32 var, reset_mask;
  369. if (bled >= 0) {
  370. if (bled)
  371. printk(KERN_ERR "%s%d: adapter kernel panic'd %x.\n",
  372. dev->name, dev->id, bled);
  373. bled = aac_adapter_sync_cmd(dev, IOP_RESET_ALWAYS,
  374. 0, 0, 0, 0, 0, 0, &var, &reset_mask, NULL, NULL, NULL);
  375. if (bled || (var != 0x00000001))
  376. bled = -EINVAL;
  377. if (dev->supplement_adapter_info.SupportedOptions2 &
  378. AAC_OPTION_DOORBELL_RESET) {
  379. src_writel(dev, MUnit.IDR, reset_mask);
  380. msleep(5000); /* Delay 5 seconds */
  381. }
  382. }
  383. if (src_readl(dev, MUnit.OMR) & KERNEL_PANIC)
  384. return -ENODEV;
  385. if (startup_timeout < 300)
  386. startup_timeout = 300;
  387. return 0;
  388. }
  389. /**
  390. * aac_src_select_comm - Select communications method
  391. * @dev: Adapter
  392. * @comm: communications method
  393. */
  394. int aac_src_select_comm(struct aac_dev *dev, int comm)
  395. {
  396. switch (comm) {
  397. case AAC_COMM_MESSAGE:
  398. dev->a_ops.adapter_enable_int = aac_src_enable_interrupt_message;
  399. dev->a_ops.adapter_intr = aac_src_intr_message;
  400. dev->a_ops.adapter_deliver = aac_src_deliver_message;
  401. break;
  402. default:
  403. return 1;
  404. }
  405. return 0;
  406. }
  407. /**
  408. * aac_src_init - initialize an Cardinal Frey Bar card
  409. * @dev: device to configure
  410. *
  411. */
  412. int aac_src_init(struct aac_dev *dev)
  413. {
  414. unsigned long start;
  415. unsigned long status;
  416. int restart = 0;
  417. int instance = dev->id;
  418. const char *name = dev->name;
  419. dev->a_ops.adapter_ioremap = aac_src_ioremap;
  420. dev->a_ops.adapter_comm = aac_src_select_comm;
  421. dev->base_size = AAC_MIN_SRC_BAR0_SIZE;
  422. if (aac_adapter_ioremap(dev, dev->base_size)) {
  423. printk(KERN_WARNING "%s: unable to map adapter.\n", name);
  424. goto error_iounmap;
  425. }
  426. /* Failure to reset here is an option ... */
  427. dev->a_ops.adapter_sync_cmd = src_sync_cmd;
  428. dev->a_ops.adapter_enable_int = aac_src_disable_interrupt;
  429. if ((aac_reset_devices || reset_devices) &&
  430. !aac_src_restart_adapter(dev, 0))
  431. ++restart;
  432. /*
  433. * Check to see if the board panic'd while booting.
  434. */
  435. status = src_readl(dev, MUnit.OMR);
  436. if (status & KERNEL_PANIC) {
  437. if (aac_src_restart_adapter(dev, aac_src_check_health(dev)))
  438. goto error_iounmap;
  439. ++restart;
  440. }
  441. /*
  442. * Check to see if the board failed any self tests.
  443. */
  444. status = src_readl(dev, MUnit.OMR);
  445. if (status & SELF_TEST_FAILED) {
  446. printk(KERN_ERR "%s%d: adapter self-test failed.\n",
  447. dev->name, instance);
  448. goto error_iounmap;
  449. }
  450. /*
  451. * Check to see if the monitor panic'd while booting.
  452. */
  453. if (status & MONITOR_PANIC) {
  454. printk(KERN_ERR "%s%d: adapter monitor panic.\n",
  455. dev->name, instance);
  456. goto error_iounmap;
  457. }
  458. start = jiffies;
  459. /*
  460. * Wait for the adapter to be up and running. Wait up to 3 minutes
  461. */
  462. while (!((status = src_readl(dev, MUnit.OMR)) &
  463. KERNEL_UP_AND_RUNNING)) {
  464. if ((restart &&
  465. (status & (KERNEL_PANIC|SELF_TEST_FAILED|MONITOR_PANIC))) ||
  466. time_after(jiffies, start+HZ*startup_timeout)) {
  467. printk(KERN_ERR "%s%d: adapter kernel failed to start, init status = %lx.\n",
  468. dev->name, instance, status);
  469. goto error_iounmap;
  470. }
  471. if (!restart &&
  472. ((status & (KERNEL_PANIC|SELF_TEST_FAILED|MONITOR_PANIC)) ||
  473. time_after(jiffies, start + HZ *
  474. ((startup_timeout > 60)
  475. ? (startup_timeout - 60)
  476. : (startup_timeout / 2))))) {
  477. if (likely(!aac_src_restart_adapter(dev,
  478. aac_src_check_health(dev))))
  479. start = jiffies;
  480. ++restart;
  481. }
  482. msleep(1);
  483. }
  484. if (restart && aac_commit)
  485. aac_commit = 1;
  486. /*
  487. * Fill in the common function dispatch table.
  488. */
  489. dev->a_ops.adapter_interrupt = aac_src_interrupt_adapter;
  490. dev->a_ops.adapter_disable_int = aac_src_disable_interrupt;
  491. dev->a_ops.adapter_notify = aac_src_notify_adapter;
  492. dev->a_ops.adapter_sync_cmd = src_sync_cmd;
  493. dev->a_ops.adapter_check_health = aac_src_check_health;
  494. dev->a_ops.adapter_restart = aac_src_restart_adapter;
  495. /*
  496. * First clear out all interrupts. Then enable the one's that we
  497. * can handle.
  498. */
  499. aac_adapter_comm(dev, AAC_COMM_MESSAGE);
  500. aac_adapter_disable_int(dev);
  501. src_writel(dev, MUnit.ODR_C, 0xffffffff);
  502. aac_adapter_enable_int(dev);
  503. if (aac_init_adapter(dev) == NULL)
  504. goto error_iounmap;
  505. if (dev->comm_interface != AAC_COMM_MESSAGE_TYPE1)
  506. goto error_iounmap;
  507. dev->msi = aac_msi && !pci_enable_msi(dev->pdev);
  508. if (request_irq(dev->pdev->irq, dev->a_ops.adapter_intr,
  509. IRQF_SHARED|IRQF_DISABLED, "aacraid", dev) < 0) {
  510. if (dev->msi)
  511. pci_disable_msi(dev->pdev);
  512. printk(KERN_ERR "%s%d: Interrupt unavailable.\n",
  513. name, instance);
  514. goto error_iounmap;
  515. }
  516. dev->dbg_base = pci_resource_start(dev->pdev, 2);
  517. dev->dbg_base_mapped = dev->regs.src.bar1;
  518. dev->dbg_size = AAC_MIN_SRC_BAR1_SIZE;
  519. aac_adapter_enable_int(dev);
  520. /*
  521. * Tell the adapter that all is configured, and it can
  522. * start accepting requests
  523. */
  524. aac_src_start_adapter(dev);
  525. return 0;
  526. error_iounmap:
  527. return -1;
  528. }