src.c 14 KB

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