rx.c 14 KB

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