rx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * Adaptec AAC series RAID controller driver
  3. * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
  4. *
  5. * based on the old aacraid driver that is..
  6. * Adaptec aacraid device driver for Linux.
  7. *
  8. * Copyright (c) 2000 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. * rx.c
  26. *
  27. * Abstract: Hardware miniport for Drawbridge specific hardware functions.
  28. *
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/init.h>
  32. #include <linux/types.h>
  33. #include <linux/sched.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 <asm/semaphore.h>
  43. #include <scsi/scsi_host.h>
  44. #include "aacraid.h"
  45. static irqreturn_t aac_rx_intr_producer(int irq, void *dev_id)
  46. {
  47. struct aac_dev *dev = dev_id;
  48. unsigned long bellbits;
  49. u8 intstat = rx_readb(dev, MUnit.OISR);
  50. /*
  51. * Read mask and invert because drawbridge is reversed.
  52. * This allows us to only service interrupts that have
  53. * been enabled.
  54. * Check to see if this is our interrupt. If it isn't just return
  55. */
  56. if (intstat & ~(dev->OIMR)) {
  57. bellbits = rx_readl(dev, OutboundDoorbellReg);
  58. if (bellbits & DoorBellPrintfReady) {
  59. aac_printf(dev, readl (&dev->IndexRegs->Mailbox[5]));
  60. rx_writel(dev, MUnit.ODR,DoorBellPrintfReady);
  61. rx_writel(dev, InboundDoorbellReg,DoorBellPrintfDone);
  62. }
  63. else if (bellbits & DoorBellAdapterNormCmdReady) {
  64. rx_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdReady);
  65. aac_command_normal(&dev->queues->queue[HostNormCmdQueue]);
  66. }
  67. else if (bellbits & DoorBellAdapterNormRespReady) {
  68. rx_writel(dev, MUnit.ODR,DoorBellAdapterNormRespReady);
  69. aac_response_normal(&dev->queues->queue[HostNormRespQueue]);
  70. }
  71. else if (bellbits & DoorBellAdapterNormCmdNotFull) {
  72. rx_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
  73. }
  74. else if (bellbits & DoorBellAdapterNormRespNotFull) {
  75. rx_writel(dev, MUnit.ODR, DoorBellAdapterNormCmdNotFull);
  76. rx_writel(dev, MUnit.ODR, DoorBellAdapterNormRespNotFull);
  77. }
  78. return IRQ_HANDLED;
  79. }
  80. return IRQ_NONE;
  81. }
  82. static irqreturn_t aac_rx_intr_message(int irq, void *dev_id)
  83. {
  84. struct aac_dev *dev = dev_id;
  85. u32 Index = rx_readl(dev, MUnit.OutboundQueue);
  86. if (Index == 0xFFFFFFFFL)
  87. Index = rx_readl(dev, MUnit.OutboundQueue);
  88. if (Index != 0xFFFFFFFFL) {
  89. do {
  90. if (aac_intr_normal(dev, Index)) {
  91. rx_writel(dev, MUnit.OutboundQueue, Index);
  92. rx_writel(dev, MUnit.ODR, DoorBellAdapterNormRespReady);
  93. }
  94. Index = rx_readl(dev, MUnit.OutboundQueue);
  95. } while (Index != 0xFFFFFFFFL);
  96. return IRQ_HANDLED;
  97. }
  98. return IRQ_NONE;
  99. }
  100. /**
  101. * aac_rx_disable_interrupt - Disable interrupts
  102. * @dev: Adapter
  103. */
  104. static void aac_rx_disable_interrupt(struct aac_dev *dev)
  105. {
  106. rx_writeb(dev, MUnit.OIMR, dev->OIMR = 0xff);
  107. }
  108. /**
  109. * aac_rx_enable_interrupt_producer - Enable interrupts
  110. * @dev: Adapter
  111. */
  112. static void aac_rx_enable_interrupt_producer(struct aac_dev *dev)
  113. {
  114. rx_writeb(dev, MUnit.OIMR, dev->OIMR = 0xfb);
  115. }
  116. /**
  117. * aac_rx_enable_interrupt_message - Enable interrupts
  118. * @dev: Adapter
  119. */
  120. static void aac_rx_enable_interrupt_message(struct aac_dev *dev)
  121. {
  122. rx_writeb(dev, MUnit.OIMR, dev->OIMR = 0xf7);
  123. }
  124. /**
  125. * rx_sync_cmd - send a command and wait
  126. * @dev: Adapter
  127. * @command: Command to execute
  128. * @p1: first parameter
  129. * @ret: adapter status
  130. *
  131. * This routine will send a synchronous command to the adapter and wait
  132. * for its completion.
  133. */
  134. static int rx_sync_cmd(struct aac_dev *dev, u32 command,
  135. u32 p1, u32 p2, u32 p3, u32 p4, u32 p5, u32 p6,
  136. u32 *status, u32 * r1, u32 * r2, u32 * r3, u32 * r4)
  137. {
  138. unsigned long start;
  139. int ok;
  140. /*
  141. * Write the command into Mailbox 0
  142. */
  143. writel(command, &dev->IndexRegs->Mailbox[0]);
  144. /*
  145. * Write the parameters into Mailboxes 1 - 6
  146. */
  147. writel(p1, &dev->IndexRegs->Mailbox[1]);
  148. writel(p2, &dev->IndexRegs->Mailbox[2]);
  149. writel(p3, &dev->IndexRegs->Mailbox[3]);
  150. writel(p4, &dev->IndexRegs->Mailbox[4]);
  151. /*
  152. * Clear the synch command doorbell to start on a clean slate.
  153. */
  154. rx_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
  155. /*
  156. * Disable doorbell interrupts
  157. */
  158. rx_writeb(dev, MUnit.OIMR, dev->OIMR = 0xff);
  159. /*
  160. * Force the completion of the mask register write before issuing
  161. * the interrupt.
  162. */
  163. rx_readb (dev, MUnit.OIMR);
  164. /*
  165. * Signal that there is a new synch command
  166. */
  167. rx_writel(dev, InboundDoorbellReg, INBOUNDDOORBELL_0);
  168. ok = 0;
  169. start = jiffies;
  170. /*
  171. * Wait up to 30 seconds
  172. */
  173. while (time_before(jiffies, start+30*HZ))
  174. {
  175. udelay(5); /* Delay 5 microseconds to let Mon960 get info. */
  176. /*
  177. * Mon960 will set doorbell0 bit when it has completed the command.
  178. */
  179. if (rx_readl(dev, OutboundDoorbellReg) & OUTBOUNDDOORBELL_0) {
  180. /*
  181. * Clear the doorbell.
  182. */
  183. rx_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
  184. ok = 1;
  185. break;
  186. }
  187. /*
  188. * Yield the processor in case we are slow
  189. */
  190. msleep(1);
  191. }
  192. if (ok != 1) {
  193. /*
  194. * Restore interrupt mask even though we timed out
  195. */
  196. aac_adapter_enable_int(dev);
  197. return -ETIMEDOUT;
  198. }
  199. /*
  200. * Pull the synch status from Mailbox 0.
  201. */
  202. if (status)
  203. *status = readl(&dev->IndexRegs->Mailbox[0]);
  204. if (r1)
  205. *r1 = readl(&dev->IndexRegs->Mailbox[1]);
  206. if (r2)
  207. *r2 = readl(&dev->IndexRegs->Mailbox[2]);
  208. if (r3)
  209. *r3 = readl(&dev->IndexRegs->Mailbox[3]);
  210. if (r4)
  211. *r4 = readl(&dev->IndexRegs->Mailbox[4]);
  212. /*
  213. * Clear the synch command doorbell.
  214. */
  215. rx_writel(dev, OutboundDoorbellReg, OUTBOUNDDOORBELL_0);
  216. /*
  217. * Restore interrupt mask
  218. */
  219. aac_adapter_enable_int(dev);
  220. return 0;
  221. }
  222. /**
  223. * aac_rx_interrupt_adapter - interrupt adapter
  224. * @dev: Adapter
  225. *
  226. * Send an interrupt to the i960 and breakpoint it.
  227. */
  228. static void aac_rx_interrupt_adapter(struct aac_dev *dev)
  229. {
  230. rx_sync_cmd(dev, BREAKPOINT_REQUEST, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL);
  231. }
  232. /**
  233. * aac_rx_notify_adapter - send an event to the adapter
  234. * @dev: Adapter
  235. * @event: Event to send
  236. *
  237. * Notify the i960 that something it probably cares about has
  238. * happened.
  239. */
  240. static void aac_rx_notify_adapter(struct aac_dev *dev, u32 event)
  241. {
  242. switch (event) {
  243. case AdapNormCmdQue:
  244. rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_1);
  245. break;
  246. case HostNormRespNotFull:
  247. rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_4);
  248. break;
  249. case AdapNormRespQue:
  250. rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_2);
  251. break;
  252. case HostNormCmdNotFull:
  253. rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_3);
  254. break;
  255. case HostShutdown:
  256. break;
  257. case FastIo:
  258. rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_6);
  259. break;
  260. case AdapPrintfDone:
  261. rx_writel(dev, MUnit.IDR,INBOUNDDOORBELL_5);
  262. break;
  263. default:
  264. BUG();
  265. break;
  266. }
  267. }
  268. /**
  269. * aac_rx_start_adapter - activate adapter
  270. * @dev: Adapter
  271. *
  272. * Start up processing on an i960 based AAC adapter
  273. */
  274. void aac_rx_start_adapter(struct aac_dev *dev)
  275. {
  276. struct aac_init *init;
  277. init = dev->init;
  278. init->HostElapsedSeconds = cpu_to_le32(get_seconds());
  279. // We can only use a 32 bit address here
  280. rx_sync_cmd(dev, INIT_STRUCT_BASE_ADDRESS, (u32)(ulong)dev->init_pa,
  281. 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL);
  282. }
  283. /**
  284. * aac_rx_check_health
  285. * @dev: device to check if healthy
  286. *
  287. * Will attempt to determine if the specified adapter is alive and
  288. * capable of handling requests, returning 0 if alive.
  289. */
  290. static int aac_rx_check_health(struct aac_dev *dev)
  291. {
  292. u32 status = rx_readl(dev, MUnit.OMRx[0]);
  293. /*
  294. * Check to see if the board failed any self tests.
  295. */
  296. if (status & SELF_TEST_FAILED)
  297. return -1;
  298. /*
  299. * Check to see if the board panic'd.
  300. */
  301. if (status & KERNEL_PANIC) {
  302. char * buffer;
  303. struct POSTSTATUS {
  304. __le32 Post_Command;
  305. __le32 Post_Address;
  306. } * post;
  307. dma_addr_t paddr, baddr;
  308. int ret;
  309. if ((status & 0xFF000000L) == 0xBC000000L)
  310. return (status >> 16) & 0xFF;
  311. buffer = pci_alloc_consistent(dev->pdev, 512, &baddr);
  312. ret = -2;
  313. if (buffer == NULL)
  314. return ret;
  315. post = pci_alloc_consistent(dev->pdev,
  316. sizeof(struct POSTSTATUS), &paddr);
  317. if (post == NULL) {
  318. pci_free_consistent(dev->pdev, 512, buffer, baddr);
  319. return ret;
  320. }
  321. memset(buffer, 0, 512);
  322. post->Post_Command = cpu_to_le32(COMMAND_POST_RESULTS);
  323. post->Post_Address = cpu_to_le32(baddr);
  324. rx_writel(dev, MUnit.IMRx[0], paddr);
  325. rx_sync_cmd(dev, COMMAND_POST_RESULTS, baddr, 0, 0, 0, 0, 0,
  326. NULL, NULL, NULL, NULL, NULL);
  327. pci_free_consistent(dev->pdev, sizeof(struct POSTSTATUS),
  328. post, paddr);
  329. if ((buffer[0] == '0') && ((buffer[1] == 'x') || (buffer[1] == 'X'))) {
  330. ret = (buffer[2] <= '9') ? (buffer[2] - '0') : (buffer[2] - 'A' + 10);
  331. ret <<= 4;
  332. ret += (buffer[3] <= '9') ? (buffer[3] - '0') : (buffer[3] - 'A' + 10);
  333. }
  334. pci_free_consistent(dev->pdev, 512, buffer, baddr);
  335. return ret;
  336. }
  337. /*
  338. * Wait for the adapter to be up and running.
  339. */
  340. if (!(status & KERNEL_UP_AND_RUNNING))
  341. return -3;
  342. /*
  343. * Everything is OK
  344. */
  345. return 0;
  346. }
  347. /**
  348. * aac_rx_deliver_producer
  349. * @fib: fib to issue
  350. *
  351. * Will send a fib, returning 0 if successful.
  352. */
  353. static int aac_rx_deliver_producer(struct fib * fib)
  354. {
  355. struct aac_dev *dev = fib->dev;
  356. struct aac_queue *q = &dev->queues->queue[AdapNormCmdQueue];
  357. unsigned long qflags;
  358. u32 Index;
  359. unsigned long nointr = 0;
  360. spin_lock_irqsave(q->lock, qflags);
  361. aac_queue_get( dev, &Index, AdapNormCmdQueue, fib->hw_fib, 1, fib, &nointr);
  362. q->numpending++;
  363. *(q->headers.producer) = cpu_to_le32(Index + 1);
  364. spin_unlock_irqrestore(q->lock, qflags);
  365. if (!(nointr & aac_config.irq_mod))
  366. aac_adapter_notify(dev, AdapNormCmdQueue);
  367. return 0;
  368. }
  369. /**
  370. * aac_rx_deliver_message
  371. * @fib: fib to issue
  372. *
  373. * Will send a fib, returning 0 if successful.
  374. */
  375. static int aac_rx_deliver_message(struct fib * fib)
  376. {
  377. struct aac_dev *dev = fib->dev;
  378. struct aac_queue *q = &dev->queues->queue[AdapNormCmdQueue];
  379. unsigned long qflags;
  380. u32 Index;
  381. u64 addr;
  382. volatile void __iomem *device;
  383. unsigned long count = 10000000L; /* 50 seconds */
  384. spin_lock_irqsave(q->lock, qflags);
  385. q->numpending++;
  386. spin_unlock_irqrestore(q->lock, qflags);
  387. for(;;) {
  388. Index = rx_readl(dev, MUnit.InboundQueue);
  389. if (Index == 0xFFFFFFFFL)
  390. Index = rx_readl(dev, MUnit.InboundQueue);
  391. if (Index != 0xFFFFFFFFL)
  392. break;
  393. if (--count == 0) {
  394. spin_lock_irqsave(q->lock, qflags);
  395. q->numpending--;
  396. spin_unlock_irqrestore(q->lock, qflags);
  397. return -ETIMEDOUT;
  398. }
  399. udelay(5);
  400. }
  401. device = dev->base + Index;
  402. addr = fib->hw_fib_pa;
  403. writel((u32)(addr & 0xffffffff), device);
  404. device += sizeof(u32);
  405. writel((u32)(addr >> 32), device);
  406. device += sizeof(u32);
  407. writel(le16_to_cpu(fib->hw_fib->header.Size), device);
  408. rx_writel(dev, MUnit.InboundQueue, Index);
  409. return 0;
  410. }
  411. /**
  412. * aac_rx_ioremap
  413. * @size: mapping resize request
  414. *
  415. */
  416. static int aac_rx_ioremap(struct aac_dev * dev, u32 size)
  417. {
  418. if (!size) {
  419. iounmap(dev->regs.rx);
  420. return 0;
  421. }
  422. dev->base = dev->regs.rx = ioremap(dev->scsi_host_ptr->base, size);
  423. if (dev->base == NULL)
  424. return -1;
  425. dev->IndexRegs = &dev->regs.rx->IndexRegs;
  426. return 0;
  427. }
  428. static int aac_rx_restart_adapter(struct aac_dev *dev)
  429. {
  430. u32 var;
  431. printk(KERN_ERR "%s%d: adapter kernel panic'd.\n",
  432. dev->name, dev->id);
  433. if (aac_rx_check_health(dev) <= 0)
  434. return 1;
  435. if (rx_sync_cmd(dev, IOP_RESET, 0, 0, 0, 0, 0, 0,
  436. &var, NULL, NULL, NULL, NULL))
  437. return 1;
  438. if (var != 0x00000001)
  439. return 1;
  440. if (rx_readl(dev, MUnit.OMRx[0]) & KERNEL_PANIC)
  441. return 1;
  442. return 0;
  443. }
  444. /**
  445. * aac_rx_select_comm - Select communications method
  446. * @dev: Adapter
  447. * @comm: communications method
  448. */
  449. int aac_rx_select_comm(struct aac_dev *dev, int comm)
  450. {
  451. switch (comm) {
  452. case AAC_COMM_PRODUCER:
  453. dev->a_ops.adapter_enable_int = aac_rx_enable_interrupt_producer;
  454. dev->a_ops.adapter_intr = aac_rx_intr_producer;
  455. dev->a_ops.adapter_deliver = aac_rx_deliver_producer;
  456. break;
  457. case AAC_COMM_MESSAGE:
  458. dev->a_ops.adapter_enable_int = aac_rx_enable_interrupt_message;
  459. dev->a_ops.adapter_intr = aac_rx_intr_message;
  460. dev->a_ops.adapter_deliver = aac_rx_deliver_message;
  461. break;
  462. default:
  463. return 1;
  464. }
  465. return 0;
  466. }
  467. /**
  468. * aac_rx_init - initialize an i960 based AAC card
  469. * @dev: device to configure
  470. *
  471. * Allocate and set up resources for the i960 based AAC variants. The
  472. * device_interface in the commregion will be allocated and linked
  473. * to the comm region.
  474. */
  475. int _aac_rx_init(struct aac_dev *dev)
  476. {
  477. unsigned long start;
  478. unsigned long status;
  479. int instance;
  480. const char * name;
  481. instance = dev->id;
  482. name = dev->name;
  483. if (aac_adapter_ioremap(dev, dev->base_size)) {
  484. printk(KERN_WARNING "%s: unable to map adapter.\n", name);
  485. goto error_iounmap;
  486. }
  487. /*
  488. * Check to see if the board panic'd while booting.
  489. */
  490. status = rx_readl(dev, MUnit.OMRx[0]);
  491. if (status & KERNEL_PANIC)
  492. if (aac_rx_restart_adapter(dev))
  493. goto error_iounmap;
  494. /*
  495. * Check to see if the board failed any self tests.
  496. */
  497. status = rx_readl(dev, MUnit.OMRx[0]);
  498. if (status & SELF_TEST_FAILED) {
  499. printk(KERN_ERR "%s%d: adapter self-test failed.\n", dev->name, instance);
  500. goto error_iounmap;
  501. }
  502. /*
  503. * Check to see if the monitor panic'd while booting.
  504. */
  505. if (status & MONITOR_PANIC) {
  506. printk(KERN_ERR "%s%d: adapter monitor panic.\n", dev->name, instance);
  507. goto error_iounmap;
  508. }
  509. start = jiffies;
  510. /*
  511. * Wait for the adapter to be up and running. Wait up to 3 minutes
  512. */
  513. while (!((status = rx_readl(dev, MUnit.OMRx[0])) & KERNEL_UP_AND_RUNNING))
  514. {
  515. if(time_after(jiffies, start+startup_timeout*HZ))
  516. {
  517. printk(KERN_ERR "%s%d: adapter kernel failed to start, init status = %lx.\n",
  518. dev->name, instance, status);
  519. goto error_iounmap;
  520. }
  521. msleep(1);
  522. }
  523. /*
  524. * Fill in the common function dispatch table.
  525. */
  526. dev->a_ops.adapter_interrupt = aac_rx_interrupt_adapter;
  527. dev->a_ops.adapter_disable_int = aac_rx_disable_interrupt;
  528. dev->a_ops.adapter_notify = aac_rx_notify_adapter;
  529. dev->a_ops.adapter_sync_cmd = rx_sync_cmd;
  530. dev->a_ops.adapter_check_health = aac_rx_check_health;
  531. /*
  532. * First clear out all interrupts. Then enable the one's that we
  533. * can handle.
  534. */
  535. aac_adapter_comm(dev, AAC_COMM_PRODUCER);
  536. aac_adapter_disable_int(dev);
  537. rx_writel(dev, MUnit.ODR, 0xffffffff);
  538. aac_adapter_enable_int(dev);
  539. if (aac_init_adapter(dev) == NULL)
  540. goto error_iounmap;
  541. aac_adapter_comm(dev, dev->comm_interface);
  542. if (request_irq(dev->scsi_host_ptr->irq, dev->a_ops.adapter_intr,
  543. IRQF_SHARED|IRQF_DISABLED, "aacraid", dev) < 0) {
  544. printk(KERN_ERR "%s%d: Interrupt unavailable.\n",
  545. name, instance);
  546. goto error_iounmap;
  547. }
  548. aac_adapter_enable_int(dev);
  549. /*
  550. * Tell the adapter that all is configured, and it can
  551. * start accepting requests
  552. */
  553. aac_rx_start_adapter(dev);
  554. return 0;
  555. error_iounmap:
  556. return -1;
  557. }
  558. int aac_rx_init(struct aac_dev *dev)
  559. {
  560. /*
  561. * Fill in the function dispatch table.
  562. */
  563. dev->a_ops.adapter_ioremap = aac_rx_ioremap;
  564. dev->a_ops.adapter_comm = aac_rx_select_comm;
  565. return _aac_rx_init(dev);
  566. }