rx.c 12 KB

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