dpcsup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
  10. * 2016-2017 Microsemi Corp. (aacraid@microsemi.com)
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * Module Name:
  27. * dpcsup.c
  28. *
  29. * Abstract: All DPC processing routines for the cyclone board occur here.
  30. *
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/init.h>
  35. #include <linux/types.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/slab.h>
  38. #include <linux/completion.h>
  39. #include <linux/blkdev.h>
  40. #include <linux/semaphore.h>
  41. #include "aacraid.h"
  42. /**
  43. * aac_response_normal - Handle command replies
  44. * @q: Queue to read from
  45. *
  46. * This DPC routine will be run when the adapter interrupts us to let us
  47. * know there is a response on our normal priority queue. We will pull off
  48. * all QE there are and wake up all the waiters before exiting. We will
  49. * take a spinlock out on the queue before operating on it.
  50. */
  51. unsigned int aac_response_normal(struct aac_queue * q)
  52. {
  53. struct aac_dev * dev = q->dev;
  54. struct aac_entry *entry;
  55. struct hw_fib * hwfib;
  56. struct fib * fib;
  57. int consumed = 0;
  58. unsigned long flags, mflags;
  59. spin_lock_irqsave(q->lock, flags);
  60. /*
  61. * Keep pulling response QEs off the response queue and waking
  62. * up the waiters until there are no more QEs. We then return
  63. * back to the system. If no response was requested we just
  64. * deallocate the Fib here and continue.
  65. */
  66. while(aac_consumer_get(dev, q, &entry))
  67. {
  68. int fast;
  69. u32 index = le32_to_cpu(entry->addr);
  70. fast = index & 0x01;
  71. fib = &dev->fibs[index >> 2];
  72. hwfib = fib->hw_fib_va;
  73. aac_consumer_free(dev, q, HostNormRespQueue);
  74. /*
  75. * Remove this fib from the Outstanding I/O queue.
  76. * But only if it has not already been timed out.
  77. *
  78. * If the fib has been timed out already, then just
  79. * continue. The caller has already been notified that
  80. * the fib timed out.
  81. */
  82. atomic_dec(&dev->queues->queue[AdapNormCmdQueue].numpending);
  83. if (unlikely(fib->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) {
  84. spin_unlock_irqrestore(q->lock, flags);
  85. aac_fib_complete(fib);
  86. aac_fib_free(fib);
  87. spin_lock_irqsave(q->lock, flags);
  88. continue;
  89. }
  90. spin_unlock_irqrestore(q->lock, flags);
  91. if (fast) {
  92. /*
  93. * Doctor the fib
  94. */
  95. *(__le32 *)hwfib->data = cpu_to_le32(ST_OK);
  96. hwfib->header.XferState |= cpu_to_le32(AdapterProcessed);
  97. fib->flags |= FIB_CONTEXT_FLAG_FASTRESP;
  98. }
  99. FIB_COUNTER_INCREMENT(aac_config.FibRecved);
  100. if (hwfib->header.Command == cpu_to_le16(NuFileSystem))
  101. {
  102. __le32 *pstatus = (__le32 *)hwfib->data;
  103. if (*pstatus & cpu_to_le32(0xffff0000))
  104. *pstatus = cpu_to_le32(ST_OK);
  105. }
  106. if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected | Async))
  107. {
  108. if (hwfib->header.XferState & cpu_to_le32(NoResponseExpected))
  109. FIB_COUNTER_INCREMENT(aac_config.NoResponseRecved);
  110. else
  111. FIB_COUNTER_INCREMENT(aac_config.AsyncRecved);
  112. /*
  113. * NOTE: we cannot touch the fib after this
  114. * call, because it may have been deallocated.
  115. */
  116. fib->callback(fib->callback_data, fib);
  117. } else {
  118. unsigned long flagv;
  119. spin_lock_irqsave(&fib->event_lock, flagv);
  120. if (!fib->done) {
  121. fib->done = 1;
  122. up(&fib->event_wait);
  123. }
  124. spin_unlock_irqrestore(&fib->event_lock, flagv);
  125. spin_lock_irqsave(&dev->manage_lock, mflags);
  126. dev->management_fib_count--;
  127. spin_unlock_irqrestore(&dev->manage_lock, mflags);
  128. FIB_COUNTER_INCREMENT(aac_config.NormalRecved);
  129. if (fib->done == 2) {
  130. spin_lock_irqsave(&fib->event_lock, flagv);
  131. fib->done = 0;
  132. spin_unlock_irqrestore(&fib->event_lock, flagv);
  133. aac_fib_complete(fib);
  134. aac_fib_free(fib);
  135. }
  136. }
  137. consumed++;
  138. spin_lock_irqsave(q->lock, flags);
  139. }
  140. if (consumed > aac_config.peak_fibs)
  141. aac_config.peak_fibs = consumed;
  142. if (consumed == 0)
  143. aac_config.zero_fibs++;
  144. spin_unlock_irqrestore(q->lock, flags);
  145. return 0;
  146. }
  147. /**
  148. * aac_command_normal - handle commands
  149. * @q: queue to process
  150. *
  151. * This DPC routine will be queued when the adapter interrupts us to
  152. * let us know there is a command on our normal priority queue. We will
  153. * pull off all QE there are and wake up all the waiters before exiting.
  154. * We will take a spinlock out on the queue before operating on it.
  155. */
  156. unsigned int aac_command_normal(struct aac_queue *q)
  157. {
  158. struct aac_dev * dev = q->dev;
  159. struct aac_entry *entry;
  160. unsigned long flags;
  161. spin_lock_irqsave(q->lock, flags);
  162. /*
  163. * Keep pulling response QEs off the response queue and waking
  164. * up the waiters until there are no more QEs. We then return
  165. * back to the system.
  166. */
  167. while(aac_consumer_get(dev, q, &entry))
  168. {
  169. struct fib fibctx;
  170. struct hw_fib * hw_fib;
  171. u32 index;
  172. struct fib *fib = &fibctx;
  173. index = le32_to_cpu(entry->addr) / sizeof(struct hw_fib);
  174. hw_fib = &dev->aif_base_va[index];
  175. /*
  176. * Allocate a FIB at all costs. For non queued stuff
  177. * we can just use the stack so we are happy. We need
  178. * a fib object in order to manage the linked lists
  179. */
  180. if (dev->aif_thread)
  181. if((fib = kmalloc(sizeof(struct fib), GFP_ATOMIC)) == NULL)
  182. fib = &fibctx;
  183. memset(fib, 0, sizeof(struct fib));
  184. INIT_LIST_HEAD(&fib->fiblink);
  185. fib->type = FSAFS_NTC_FIB_CONTEXT;
  186. fib->size = sizeof(struct fib);
  187. fib->hw_fib_va = hw_fib;
  188. fib->data = hw_fib->data;
  189. fib->dev = dev;
  190. if (dev->aif_thread && fib != &fibctx) {
  191. list_add_tail(&fib->fiblink, &q->cmdq);
  192. aac_consumer_free(dev, q, HostNormCmdQueue);
  193. wake_up_interruptible(&q->cmdready);
  194. } else {
  195. aac_consumer_free(dev, q, HostNormCmdQueue);
  196. spin_unlock_irqrestore(q->lock, flags);
  197. /*
  198. * Set the status of this FIB
  199. */
  200. *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
  201. aac_fib_adapter_complete(fib, sizeof(u32));
  202. spin_lock_irqsave(q->lock, flags);
  203. }
  204. }
  205. spin_unlock_irqrestore(q->lock, flags);
  206. return 0;
  207. }
  208. /*
  209. *
  210. * aac_aif_callback
  211. * @context: the context set in the fib - here it is scsi cmd
  212. * @fibptr: pointer to the fib
  213. *
  214. * Handles the AIFs - new method (SRC)
  215. *
  216. */
  217. static void aac_aif_callback(void *context, struct fib * fibptr)
  218. {
  219. struct fib *fibctx;
  220. struct aac_dev *dev;
  221. struct aac_aifcmd *cmd;
  222. int status;
  223. fibctx = (struct fib *)context;
  224. BUG_ON(fibptr == NULL);
  225. dev = fibptr->dev;
  226. if ((fibptr->hw_fib_va->header.XferState &
  227. cpu_to_le32(NoMoreAifDataAvailable)) ||
  228. dev->sa_firmware) {
  229. aac_fib_complete(fibptr);
  230. aac_fib_free(fibptr);
  231. return;
  232. }
  233. aac_intr_normal(dev, 0, 1, 0, fibptr->hw_fib_va);
  234. aac_fib_init(fibctx);
  235. cmd = (struct aac_aifcmd *) fib_data(fibctx);
  236. cmd->command = cpu_to_le32(AifReqEvent);
  237. status = aac_fib_send(AifRequest,
  238. fibctx,
  239. sizeof(struct hw_fib)-sizeof(struct aac_fibhdr),
  240. FsaNormal,
  241. 0, 1,
  242. (fib_callback)aac_aif_callback, fibctx);
  243. }
  244. /**
  245. * aac_intr_normal - Handle command replies
  246. * @dev: Device
  247. * @index: completion reference
  248. *
  249. * This DPC routine will be run when the adapter interrupts us to let us
  250. * know there is a response on our normal priority queue. We will pull off
  251. * all QE there are and wake up all the waiters before exiting.
  252. */
  253. unsigned int aac_intr_normal(struct aac_dev *dev, u32 index, int isAif,
  254. int isFastResponse, struct hw_fib *aif_fib)
  255. {
  256. unsigned long mflags;
  257. dprintk((KERN_INFO "aac_intr_normal(%p,%x)\n", dev, index));
  258. if (isAif == 1) { /* AIF - common */
  259. struct hw_fib * hw_fib;
  260. struct fib * fib;
  261. struct aac_queue *q = &dev->queues->queue[HostNormCmdQueue];
  262. unsigned long flags;
  263. /*
  264. * Allocate a FIB. For non queued stuff we can just use
  265. * the stack so we are happy. We need a fib object in order to
  266. * manage the linked lists.
  267. */
  268. if ((!dev->aif_thread)
  269. || (!(fib = kzalloc(sizeof(struct fib),GFP_ATOMIC))))
  270. return 1;
  271. if (!(hw_fib = kzalloc(sizeof(struct hw_fib),GFP_ATOMIC))) {
  272. kfree (fib);
  273. return 1;
  274. }
  275. if (dev->sa_firmware) {
  276. fib->hbacmd_size = index; /* store event type */
  277. } else if (aif_fib != NULL) {
  278. memcpy(hw_fib, aif_fib, sizeof(struct hw_fib));
  279. } else {
  280. memcpy(hw_fib, (struct hw_fib *)
  281. (((uintptr_t)(dev->regs.sa)) + index),
  282. sizeof(struct hw_fib));
  283. }
  284. INIT_LIST_HEAD(&fib->fiblink);
  285. fib->type = FSAFS_NTC_FIB_CONTEXT;
  286. fib->size = sizeof(struct fib);
  287. fib->hw_fib_va = hw_fib;
  288. fib->data = hw_fib->data;
  289. fib->dev = dev;
  290. spin_lock_irqsave(q->lock, flags);
  291. list_add_tail(&fib->fiblink, &q->cmdq);
  292. wake_up_interruptible(&q->cmdready);
  293. spin_unlock_irqrestore(q->lock, flags);
  294. return 1;
  295. } else if (isAif == 2) { /* AIF - new (SRC) */
  296. struct fib *fibctx;
  297. struct aac_aifcmd *cmd;
  298. fibctx = aac_fib_alloc(dev);
  299. if (!fibctx)
  300. return 1;
  301. aac_fib_init(fibctx);
  302. cmd = (struct aac_aifcmd *) fib_data(fibctx);
  303. cmd->command = cpu_to_le32(AifReqEvent);
  304. return aac_fib_send(AifRequest,
  305. fibctx,
  306. sizeof(struct hw_fib)-sizeof(struct aac_fibhdr),
  307. FsaNormal,
  308. 0, 1,
  309. (fib_callback)aac_aif_callback, fibctx);
  310. } else {
  311. struct fib *fib = &dev->fibs[index];
  312. int start_callback = 0;
  313. /*
  314. * Remove this fib from the Outstanding I/O queue.
  315. * But only if it has not already been timed out.
  316. *
  317. * If the fib has been timed out already, then just
  318. * continue. The caller has already been notified that
  319. * the fib timed out.
  320. */
  321. atomic_dec(&dev->queues->queue[AdapNormCmdQueue].numpending);
  322. if (unlikely(fib->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) {
  323. aac_fib_complete(fib);
  324. aac_fib_free(fib);
  325. return 0;
  326. }
  327. FIB_COUNTER_INCREMENT(aac_config.FibRecved);
  328. if (fib->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) {
  329. if (isFastResponse)
  330. fib->flags |= FIB_CONTEXT_FLAG_FASTRESP;
  331. if (fib->callback) {
  332. start_callback = 1;
  333. } else {
  334. unsigned long flagv;
  335. int complete = 0;
  336. dprintk((KERN_INFO "event_wait up\n"));
  337. spin_lock_irqsave(&fib->event_lock, flagv);
  338. if (fib->done == 2) {
  339. fib->done = 1;
  340. complete = 1;
  341. } else {
  342. fib->done = 1;
  343. up(&fib->event_wait);
  344. }
  345. spin_unlock_irqrestore(&fib->event_lock, flagv);
  346. spin_lock_irqsave(&dev->manage_lock, mflags);
  347. dev->management_fib_count--;
  348. spin_unlock_irqrestore(&dev->manage_lock,
  349. mflags);
  350. FIB_COUNTER_INCREMENT(aac_config.NativeRecved);
  351. if (complete)
  352. aac_fib_complete(fib);
  353. }
  354. } else {
  355. struct hw_fib *hwfib = fib->hw_fib_va;
  356. if (isFastResponse) {
  357. /* Doctor the fib */
  358. *(__le32 *)hwfib->data = cpu_to_le32(ST_OK);
  359. hwfib->header.XferState |=
  360. cpu_to_le32(AdapterProcessed);
  361. fib->flags |= FIB_CONTEXT_FLAG_FASTRESP;
  362. }
  363. if (hwfib->header.Command ==
  364. cpu_to_le16(NuFileSystem)) {
  365. __le32 *pstatus = (__le32 *)hwfib->data;
  366. if (*pstatus & cpu_to_le32(0xffff0000))
  367. *pstatus = cpu_to_le32(ST_OK);
  368. }
  369. if (hwfib->header.XferState &
  370. cpu_to_le32(NoResponseExpected | Async)) {
  371. if (hwfib->header.XferState & cpu_to_le32(
  372. NoResponseExpected))
  373. FIB_COUNTER_INCREMENT(
  374. aac_config.NoResponseRecved);
  375. else
  376. FIB_COUNTER_INCREMENT(
  377. aac_config.AsyncRecved);
  378. start_callback = 1;
  379. } else {
  380. unsigned long flagv;
  381. int complete = 0;
  382. dprintk((KERN_INFO "event_wait up\n"));
  383. spin_lock_irqsave(&fib->event_lock, flagv);
  384. if (fib->done == 2) {
  385. fib->done = 1;
  386. complete = 1;
  387. } else {
  388. fib->done = 1;
  389. up(&fib->event_wait);
  390. }
  391. spin_unlock_irqrestore(&fib->event_lock, flagv);
  392. spin_lock_irqsave(&dev->manage_lock, mflags);
  393. dev->management_fib_count--;
  394. spin_unlock_irqrestore(&dev->manage_lock,
  395. mflags);
  396. FIB_COUNTER_INCREMENT(aac_config.NormalRecved);
  397. if (complete)
  398. aac_fib_complete(fib);
  399. }
  400. }
  401. if (start_callback) {
  402. /*
  403. * NOTE: we cannot touch the fib after this
  404. * call, because it may have been deallocated.
  405. */
  406. if (likely(fib->callback && fib->callback_data)) {
  407. fib->callback(fib->callback_data, fib);
  408. } else {
  409. aac_fib_complete(fib);
  410. aac_fib_free(fib);
  411. }
  412. }
  413. return 0;
  414. }
  415. }