dpcsup.c 12 KB

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