card_ddcb.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /**
  2. * IBM Accelerator Family 'GenWQE'
  3. *
  4. * (C) Copyright IBM Corp. 2013
  5. *
  6. * Author: Frank Haverkamp <haver@linux.vnet.ibm.com>
  7. * Author: Joerg-Stephan Vogt <jsvogt@de.ibm.com>
  8. * Author: Michael Jung <mijung@gmx.net>
  9. * Author: Michael Ruettger <michael@ibmra.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License (version 2 only)
  13. * as published by the Free Software Foundation.
  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. /*
  21. * Device Driver Control Block (DDCB) queue support. Definition of
  22. * interrupt handlers for queue support as well as triggering the
  23. * health monitor code in case of problems. The current hardware uses
  24. * an MSI interrupt which is shared between error handling and
  25. * functional code.
  26. */
  27. #include <linux/types.h>
  28. #include <linux/module.h>
  29. #include <linux/sched.h>
  30. #include <linux/wait.h>
  31. #include <linux/pci.h>
  32. #include <linux/string.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/delay.h>
  35. #include <linux/module.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/crc-itu-t.h>
  38. #include "card_base.h"
  39. #include "card_ddcb.h"
  40. /*
  41. * N: next DDCB, this is where the next DDCB will be put.
  42. * A: active DDCB, this is where the code will look for the next completion.
  43. * x: DDCB is enqueued, we are waiting for its completion.
  44. * Situation (1): Empty queue
  45. * +---+---+---+---+---+---+---+---+
  46. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  47. * | | | | | | | | |
  48. * +---+---+---+---+---+---+---+---+
  49. * A/N
  50. * enqueued_ddcbs = A - N = 2 - 2 = 0
  51. *
  52. * Situation (2): Wrapped, N > A
  53. * +---+---+---+---+---+---+---+---+
  54. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  55. * | | | x | x | | | | |
  56. * +---+---+---+---+---+---+---+---+
  57. * A N
  58. * enqueued_ddcbs = N - A = 4 - 2 = 2
  59. *
  60. * Situation (3): Queue wrapped, A > N
  61. * +---+---+---+---+---+---+---+---+
  62. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  63. * | x | x | | | x | x | x | x |
  64. * +---+---+---+---+---+---+---+---+
  65. * N A
  66. * enqueued_ddcbs = queue_max - (A - N) = 8 - (4 - 2) = 6
  67. *
  68. * Situation (4a): Queue full N > A
  69. * +---+---+---+---+---+---+---+---+
  70. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  71. * | x | x | x | x | x | x | x | |
  72. * +---+---+---+---+---+---+---+---+
  73. * A N
  74. *
  75. * enqueued_ddcbs = N - A = 7 - 0 = 7
  76. *
  77. * Situation (4a): Queue full A > N
  78. * +---+---+---+---+---+---+---+---+
  79. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  80. * | x | x | x | | x | x | x | x |
  81. * +---+---+---+---+---+---+---+---+
  82. * N A
  83. * enqueued_ddcbs = queue_max - (A - N) = 8 - (4 - 3) = 7
  84. */
  85. static int queue_empty(struct ddcb_queue *queue)
  86. {
  87. return queue->ddcb_next == queue->ddcb_act;
  88. }
  89. static int queue_enqueued_ddcbs(struct ddcb_queue *queue)
  90. {
  91. if (queue->ddcb_next >= queue->ddcb_act)
  92. return queue->ddcb_next - queue->ddcb_act;
  93. return queue->ddcb_max - (queue->ddcb_act - queue->ddcb_next);
  94. }
  95. static int queue_free_ddcbs(struct ddcb_queue *queue)
  96. {
  97. int free_ddcbs = queue->ddcb_max - queue_enqueued_ddcbs(queue) - 1;
  98. if (WARN_ON_ONCE(free_ddcbs < 0)) { /* must never ever happen! */
  99. return 0;
  100. }
  101. return free_ddcbs;
  102. }
  103. /*
  104. * Use of the PRIV field in the DDCB for queue debugging:
  105. *
  106. * (1) Trying to get rid of a DDCB which saw a timeout:
  107. * pddcb->priv[6] = 0xcc; # cleared
  108. *
  109. * (2) Append a DDCB via NEXT bit:
  110. * pddcb->priv[7] = 0xaa; # appended
  111. *
  112. * (3) DDCB needed tapping:
  113. * pddcb->priv[7] = 0xbb; # tapped
  114. *
  115. * (4) DDCB marked as correctly finished:
  116. * pddcb->priv[6] = 0xff; # finished
  117. */
  118. static inline void ddcb_mark_tapped(struct ddcb *pddcb)
  119. {
  120. pddcb->priv[7] = 0xbb; /* tapped */
  121. }
  122. static inline void ddcb_mark_appended(struct ddcb *pddcb)
  123. {
  124. pddcb->priv[7] = 0xaa; /* appended */
  125. }
  126. static inline void ddcb_mark_cleared(struct ddcb *pddcb)
  127. {
  128. pddcb->priv[6] = 0xcc; /* cleared */
  129. }
  130. static inline void ddcb_mark_finished(struct ddcb *pddcb)
  131. {
  132. pddcb->priv[6] = 0xff; /* finished */
  133. }
  134. static inline void ddcb_mark_unused(struct ddcb *pddcb)
  135. {
  136. pddcb->priv_64 = cpu_to_be64(0); /* not tapped */
  137. }
  138. /**
  139. * genwqe_crc16() - Generate 16-bit crc as required for DDCBs
  140. * @buff: pointer to data buffer
  141. * @len: length of data for calculation
  142. * @init: initial crc (0xffff at start)
  143. *
  144. * Polynomial = x^16 + x^12 + x^5 + 1 (0x1021)
  145. * Example: 4 bytes 0x01 0x02 0x03 0x04 with init = 0xffff
  146. * should result in a crc16 of 0x89c3
  147. *
  148. * Return: crc16 checksum in big endian format !
  149. */
  150. static inline u16 genwqe_crc16(const u8 *buff, size_t len, u16 init)
  151. {
  152. return crc_itu_t(init, buff, len);
  153. }
  154. static void print_ddcb_info(struct genwqe_dev *cd, struct ddcb_queue *queue)
  155. {
  156. int i;
  157. struct ddcb *pddcb;
  158. unsigned long flags;
  159. struct pci_dev *pci_dev = cd->pci_dev;
  160. spin_lock_irqsave(&cd->print_lock, flags);
  161. dev_info(&pci_dev->dev,
  162. "DDCB list for card #%d (ddcb_act=%d / ddcb_next=%d):\n",
  163. cd->card_idx, queue->ddcb_act, queue->ddcb_next);
  164. pddcb = queue->ddcb_vaddr;
  165. for (i = 0; i < queue->ddcb_max; i++) {
  166. dev_err(&pci_dev->dev,
  167. " %c %-3d: RETC=%03x SEQ=%04x HSI=%02X SHI=%02x PRIV=%06llx CMD=%03x\n",
  168. i == queue->ddcb_act ? '>' : ' ',
  169. i,
  170. be16_to_cpu(pddcb->retc_16),
  171. be16_to_cpu(pddcb->seqnum_16),
  172. pddcb->hsi,
  173. pddcb->shi,
  174. be64_to_cpu(pddcb->priv_64),
  175. pddcb->cmd);
  176. pddcb++;
  177. }
  178. spin_unlock_irqrestore(&cd->print_lock, flags);
  179. }
  180. struct genwqe_ddcb_cmd *ddcb_requ_alloc(void)
  181. {
  182. struct ddcb_requ *req;
  183. req = kzalloc(sizeof(*req), GFP_KERNEL);
  184. if (!req)
  185. return NULL;
  186. return &req->cmd;
  187. }
  188. void ddcb_requ_free(struct genwqe_ddcb_cmd *cmd)
  189. {
  190. struct ddcb_requ *req = container_of(cmd, struct ddcb_requ, cmd);
  191. kfree(req);
  192. }
  193. static inline enum genwqe_requ_state ddcb_requ_get_state(struct ddcb_requ *req)
  194. {
  195. return req->req_state;
  196. }
  197. static inline void ddcb_requ_set_state(struct ddcb_requ *req,
  198. enum genwqe_requ_state new_state)
  199. {
  200. req->req_state = new_state;
  201. }
  202. static inline int ddcb_requ_collect_debug_data(struct ddcb_requ *req)
  203. {
  204. return req->cmd.ddata_addr != 0x0;
  205. }
  206. /**
  207. * ddcb_requ_finished() - Returns the hardware state of the associated DDCB
  208. * @cd: pointer to genwqe device descriptor
  209. * @req: DDCB work request
  210. *
  211. * Status of ddcb_requ mirrors this hardware state, but is copied in
  212. * the ddcb_requ on interrupt/polling function. The lowlevel code
  213. * should check the hardware state directly, the higher level code
  214. * should check the copy.
  215. *
  216. * This function will also return true if the state of the queue is
  217. * not GENWQE_CARD_USED. This enables us to purge all DDCBs in the
  218. * shutdown case.
  219. */
  220. static int ddcb_requ_finished(struct genwqe_dev *cd, struct ddcb_requ *req)
  221. {
  222. return (ddcb_requ_get_state(req) == GENWQE_REQU_FINISHED) ||
  223. (cd->card_state != GENWQE_CARD_USED);
  224. }
  225. /**
  226. * enqueue_ddcb() - Enqueue a DDCB
  227. * @cd: pointer to genwqe device descriptor
  228. * @queue: queue this operation should be done on
  229. * @ddcb_no: pointer to ddcb number being tapped
  230. *
  231. * Start execution of DDCB by tapping or append to queue via NEXT
  232. * bit. This is done by an atomic 'compare and swap' instruction and
  233. * checking SHI and HSI of the previous DDCB.
  234. *
  235. * This function must only be called with ddcb_lock held.
  236. *
  237. * Return: 1 if new DDCB is appended to previous
  238. * 2 if DDCB queue is tapped via register/simulation
  239. */
  240. #define RET_DDCB_APPENDED 1
  241. #define RET_DDCB_TAPPED 2
  242. static int enqueue_ddcb(struct genwqe_dev *cd, struct ddcb_queue *queue,
  243. struct ddcb *pddcb, int ddcb_no)
  244. {
  245. unsigned int try;
  246. int prev_no;
  247. struct ddcb *prev_ddcb;
  248. __be32 old, new, icrc_hsi_shi;
  249. u64 num;
  250. /*
  251. * For performance checks a Dispatch Timestamp can be put into
  252. * DDCB It is supposed to use the SLU's free running counter,
  253. * but this requires PCIe cycles.
  254. */
  255. ddcb_mark_unused(pddcb);
  256. /* check previous DDCB if already fetched */
  257. prev_no = (ddcb_no == 0) ? queue->ddcb_max - 1 : ddcb_no - 1;
  258. prev_ddcb = &queue->ddcb_vaddr[prev_no];
  259. /*
  260. * It might have happened that the HSI.FETCHED bit is
  261. * set. Retry in this case. Therefore I expect maximum 2 times
  262. * trying.
  263. */
  264. ddcb_mark_appended(pddcb);
  265. for (try = 0; try < 2; try++) {
  266. old = prev_ddcb->icrc_hsi_shi_32; /* read SHI/HSI in BE32 */
  267. /* try to append via NEXT bit if prev DDCB is not completed */
  268. if ((old & DDCB_COMPLETED_BE32) != 0x00000000)
  269. break;
  270. new = (old | DDCB_NEXT_BE32);
  271. wmb(); /* need to ensure write ordering */
  272. icrc_hsi_shi = cmpxchg(&prev_ddcb->icrc_hsi_shi_32, old, new);
  273. if (icrc_hsi_shi == old)
  274. return RET_DDCB_APPENDED; /* appended to queue */
  275. }
  276. /* Queue must be re-started by updating QUEUE_OFFSET */
  277. ddcb_mark_tapped(pddcb);
  278. num = (u64)ddcb_no << 8;
  279. wmb(); /* need to ensure write ordering */
  280. __genwqe_writeq(cd, queue->IO_QUEUE_OFFSET, num); /* start queue */
  281. return RET_DDCB_TAPPED;
  282. }
  283. /**
  284. * copy_ddcb_results() - Copy output state from real DDCB to request
  285. *
  286. * Copy DDCB ASV to request struct. There is no endian
  287. * conversion made, since data structure in ASV is still
  288. * unknown here.
  289. *
  290. * This is needed by:
  291. * - genwqe_purge_ddcb()
  292. * - genwqe_check_ddcb_queue()
  293. */
  294. static void copy_ddcb_results(struct ddcb_requ *req, int ddcb_no)
  295. {
  296. struct ddcb_queue *queue = req->queue;
  297. struct ddcb *pddcb = &queue->ddcb_vaddr[req->num];
  298. memcpy(&req->cmd.asv[0], &pddcb->asv[0], DDCB_ASV_LENGTH);
  299. /* copy status flags of the variant part */
  300. req->cmd.vcrc = be16_to_cpu(pddcb->vcrc_16);
  301. req->cmd.deque_ts = be64_to_cpu(pddcb->deque_ts_64);
  302. req->cmd.cmplt_ts = be64_to_cpu(pddcb->cmplt_ts_64);
  303. req->cmd.attn = be16_to_cpu(pddcb->attn_16);
  304. req->cmd.progress = be32_to_cpu(pddcb->progress_32);
  305. req->cmd.retc = be16_to_cpu(pddcb->retc_16);
  306. if (ddcb_requ_collect_debug_data(req)) {
  307. int prev_no = (ddcb_no == 0) ?
  308. queue->ddcb_max - 1 : ddcb_no - 1;
  309. struct ddcb *prev_pddcb = &queue->ddcb_vaddr[prev_no];
  310. memcpy(&req->debug_data.ddcb_finished, pddcb,
  311. sizeof(req->debug_data.ddcb_finished));
  312. memcpy(&req->debug_data.ddcb_prev, prev_pddcb,
  313. sizeof(req->debug_data.ddcb_prev));
  314. }
  315. }
  316. /**
  317. * genwqe_check_ddcb_queue() - Checks DDCB queue for completed work equests.
  318. * @cd: pointer to genwqe device descriptor
  319. *
  320. * Return: Number of DDCBs which were finished
  321. */
  322. static int genwqe_check_ddcb_queue(struct genwqe_dev *cd,
  323. struct ddcb_queue *queue)
  324. {
  325. unsigned long flags;
  326. int ddcbs_finished = 0;
  327. struct pci_dev *pci_dev = cd->pci_dev;
  328. spin_lock_irqsave(&queue->ddcb_lock, flags);
  329. /* FIXME avoid soft locking CPU */
  330. while (!queue_empty(queue) && (ddcbs_finished < queue->ddcb_max)) {
  331. struct ddcb *pddcb;
  332. struct ddcb_requ *req;
  333. u16 vcrc, vcrc_16, retc_16;
  334. pddcb = &queue->ddcb_vaddr[queue->ddcb_act];
  335. if ((pddcb->icrc_hsi_shi_32 & DDCB_COMPLETED_BE32) ==
  336. 0x00000000)
  337. goto go_home; /* not completed, continue waiting */
  338. wmb(); /* Add sync to decouple prev. read operations */
  339. /* Note: DDCB could be purged */
  340. req = queue->ddcb_req[queue->ddcb_act];
  341. if (req == NULL) {
  342. /* this occurs if DDCB is purged, not an error */
  343. /* Move active DDCB further; Nothing to do anymore. */
  344. goto pick_next_one;
  345. }
  346. /*
  347. * HSI=0x44 (fetched and completed), but RETC is
  348. * 0x101, or even worse 0x000.
  349. *
  350. * In case of seeing the queue in inconsistent state
  351. * we read the errcnts and the queue status to provide
  352. * a trigger for our PCIe analyzer stop capturing.
  353. */
  354. retc_16 = be16_to_cpu(pddcb->retc_16);
  355. if ((pddcb->hsi == 0x44) && (retc_16 <= 0x101)) {
  356. u64 errcnts, status;
  357. u64 ddcb_offs = (u64)pddcb - (u64)queue->ddcb_vaddr;
  358. errcnts = __genwqe_readq(cd, queue->IO_QUEUE_ERRCNTS);
  359. status = __genwqe_readq(cd, queue->IO_QUEUE_STATUS);
  360. dev_err(&pci_dev->dev,
  361. "[%s] SEQN=%04x HSI=%02x RETC=%03x Q_ERRCNTS=%016llx Q_STATUS=%016llx DDCB_DMA_ADDR=%016llx\n",
  362. __func__, be16_to_cpu(pddcb->seqnum_16),
  363. pddcb->hsi, retc_16, errcnts, status,
  364. queue->ddcb_daddr + ddcb_offs);
  365. }
  366. copy_ddcb_results(req, queue->ddcb_act);
  367. queue->ddcb_req[queue->ddcb_act] = NULL; /* take from queue */
  368. dev_dbg(&pci_dev->dev, "FINISHED DDCB#%d\n", req->num);
  369. genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
  370. ddcb_mark_finished(pddcb);
  371. /* calculate CRC_16 to see if VCRC is correct */
  372. vcrc = genwqe_crc16(pddcb->asv,
  373. VCRC_LENGTH(req->cmd.asv_length),
  374. 0xffff);
  375. vcrc_16 = be16_to_cpu(pddcb->vcrc_16);
  376. if (vcrc != vcrc_16) {
  377. printk_ratelimited(KERN_ERR
  378. "%s %s: err: wrong VCRC pre=%02x vcrc_len=%d bytes vcrc_data=%04x is not vcrc_card=%04x\n",
  379. GENWQE_DEVNAME, dev_name(&pci_dev->dev),
  380. pddcb->pre, VCRC_LENGTH(req->cmd.asv_length),
  381. vcrc, vcrc_16);
  382. }
  383. ddcb_requ_set_state(req, GENWQE_REQU_FINISHED);
  384. queue->ddcbs_completed++;
  385. queue->ddcbs_in_flight--;
  386. /* wake up process waiting for this DDCB, and
  387. processes on the busy queue */
  388. wake_up_interruptible(&queue->ddcb_waitqs[queue->ddcb_act]);
  389. wake_up_interruptible(&queue->busy_waitq);
  390. pick_next_one:
  391. queue->ddcb_act = (queue->ddcb_act + 1) % queue->ddcb_max;
  392. ddcbs_finished++;
  393. }
  394. go_home:
  395. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  396. return ddcbs_finished;
  397. }
  398. /**
  399. * __genwqe_wait_ddcb(): Waits until DDCB is completed
  400. * @cd: pointer to genwqe device descriptor
  401. * @req: pointer to requsted DDCB parameters
  402. *
  403. * The Service Layer will update the RETC in DDCB when processing is
  404. * pending or done.
  405. *
  406. * Return: > 0 remaining jiffies, DDCB completed
  407. * -ETIMEDOUT when timeout
  408. * -ERESTARTSYS when ^C
  409. * -EINVAL when unknown error condition
  410. *
  411. * When an error is returned the called needs to ensure that
  412. * purge_ddcb() is being called to get the &req removed from the
  413. * queue.
  414. */
  415. int __genwqe_wait_ddcb(struct genwqe_dev *cd, struct ddcb_requ *req)
  416. {
  417. int rc;
  418. unsigned int ddcb_no;
  419. struct ddcb_queue *queue;
  420. struct pci_dev *pci_dev = cd->pci_dev;
  421. if (req == NULL)
  422. return -EINVAL;
  423. queue = req->queue;
  424. if (queue == NULL)
  425. return -EINVAL;
  426. ddcb_no = req->num;
  427. if (ddcb_no >= queue->ddcb_max)
  428. return -EINVAL;
  429. rc = wait_event_interruptible_timeout(queue->ddcb_waitqs[ddcb_no],
  430. ddcb_requ_finished(cd, req),
  431. genwqe_ddcb_software_timeout * HZ);
  432. /*
  433. * We need to distinguish 3 cases here:
  434. * 1. rc == 0 timeout occured
  435. * 2. rc == -ERESTARTSYS signal received
  436. * 3. rc > 0 remaining jiffies condition is true
  437. */
  438. if (rc == 0) {
  439. struct ddcb_queue *queue = req->queue;
  440. struct ddcb *pddcb;
  441. /*
  442. * Timeout may be caused by long task switching time.
  443. * When timeout happens, check if the request has
  444. * meanwhile completed.
  445. */
  446. genwqe_check_ddcb_queue(cd, req->queue);
  447. if (ddcb_requ_finished(cd, req))
  448. return rc;
  449. dev_err(&pci_dev->dev,
  450. "[%s] err: DDCB#%d timeout rc=%d state=%d req @ %p\n",
  451. __func__, req->num, rc, ddcb_requ_get_state(req),
  452. req);
  453. dev_err(&pci_dev->dev,
  454. "[%s] IO_QUEUE_STATUS=0x%016llx\n", __func__,
  455. __genwqe_readq(cd, queue->IO_QUEUE_STATUS));
  456. pddcb = &queue->ddcb_vaddr[req->num];
  457. genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
  458. print_ddcb_info(cd, req->queue);
  459. return -ETIMEDOUT;
  460. } else if (rc == -ERESTARTSYS) {
  461. return rc;
  462. /*
  463. * EINTR: Stops the application
  464. * ERESTARTSYS: Restartable systemcall; called again
  465. */
  466. } else if (rc < 0) {
  467. dev_err(&pci_dev->dev,
  468. "[%s] err: DDCB#%d unknown result (rc=%d) %d!\n",
  469. __func__, req->num, rc, ddcb_requ_get_state(req));
  470. return -EINVAL;
  471. }
  472. /* Severe error occured. Driver is forced to stop operation */
  473. if (cd->card_state != GENWQE_CARD_USED) {
  474. dev_err(&pci_dev->dev,
  475. "[%s] err: DDCB#%d forced to stop (rc=%d)\n",
  476. __func__, req->num, rc);
  477. return -EIO;
  478. }
  479. return rc;
  480. }
  481. /**
  482. * get_next_ddcb() - Get next available DDCB
  483. * @cd: pointer to genwqe device descriptor
  484. *
  485. * DDCB's content is completely cleared but presets for PRE and
  486. * SEQNUM. This function must only be called when ddcb_lock is held.
  487. *
  488. * Return: NULL if no empty DDCB available otherwise ptr to next DDCB.
  489. */
  490. static struct ddcb *get_next_ddcb(struct genwqe_dev *cd,
  491. struct ddcb_queue *queue,
  492. int *num)
  493. {
  494. u64 *pu64;
  495. struct ddcb *pddcb;
  496. if (queue_free_ddcbs(queue) == 0) /* queue is full */
  497. return NULL;
  498. /* find new ddcb */
  499. pddcb = &queue->ddcb_vaddr[queue->ddcb_next];
  500. /* if it is not completed, we are not allowed to use it */
  501. /* barrier(); */
  502. if ((pddcb->icrc_hsi_shi_32 & DDCB_COMPLETED_BE32) == 0x00000000)
  503. return NULL;
  504. *num = queue->ddcb_next; /* internal DDCB number */
  505. queue->ddcb_next = (queue->ddcb_next + 1) % queue->ddcb_max;
  506. /* clear important DDCB fields */
  507. pu64 = (u64 *)pddcb;
  508. pu64[0] = 0ULL; /* offs 0x00 (ICRC,HSI,SHI,...) */
  509. pu64[1] = 0ULL; /* offs 0x01 (ACFUNC,CMD...) */
  510. /* destroy previous results in ASV */
  511. pu64[0x80/8] = 0ULL; /* offs 0x80 (ASV + 0) */
  512. pu64[0x88/8] = 0ULL; /* offs 0x88 (ASV + 0x08) */
  513. pu64[0x90/8] = 0ULL; /* offs 0x90 (ASV + 0x10) */
  514. pu64[0x98/8] = 0ULL; /* offs 0x98 (ASV + 0x18) */
  515. pu64[0xd0/8] = 0ULL; /* offs 0xd0 (RETC,ATTN...) */
  516. pddcb->pre = DDCB_PRESET_PRE; /* 128 */
  517. pddcb->seqnum_16 = cpu_to_be16(queue->ddcb_seq++);
  518. return pddcb;
  519. }
  520. /**
  521. * __genwqe_purge_ddcb() - Remove a DDCB from the workqueue
  522. * @cd: genwqe device descriptor
  523. * @req: DDCB request
  524. *
  525. * This will fail when the request was already FETCHED. In this case
  526. * we need to wait until it is finished. Else the DDCB can be
  527. * reused. This function also ensures that the request data structure
  528. * is removed from ddcb_req[].
  529. *
  530. * Do not forget to call this function when genwqe_wait_ddcb() fails,
  531. * such that the request gets really removed from ddcb_req[].
  532. *
  533. * Return: 0 success
  534. */
  535. int __genwqe_purge_ddcb(struct genwqe_dev *cd, struct ddcb_requ *req)
  536. {
  537. struct ddcb *pddcb = NULL;
  538. unsigned int t;
  539. unsigned long flags;
  540. struct ddcb_queue *queue = req->queue;
  541. struct pci_dev *pci_dev = cd->pci_dev;
  542. u64 queue_status;
  543. __be32 icrc_hsi_shi = 0x0000;
  544. __be32 old, new;
  545. /* unsigned long flags; */
  546. if (genwqe_ddcb_software_timeout <= 0) {
  547. dev_err(&pci_dev->dev,
  548. "[%s] err: software timeout is not set!\n", __func__);
  549. return -EFAULT;
  550. }
  551. pddcb = &queue->ddcb_vaddr[req->num];
  552. for (t = 0; t < genwqe_ddcb_software_timeout * 10; t++) {
  553. spin_lock_irqsave(&queue->ddcb_lock, flags);
  554. /* Check if req was meanwhile finished */
  555. if (ddcb_requ_get_state(req) == GENWQE_REQU_FINISHED)
  556. goto go_home;
  557. /* try to set PURGE bit if FETCHED/COMPLETED are not set */
  558. old = pddcb->icrc_hsi_shi_32; /* read SHI/HSI in BE32 */
  559. if ((old & DDCB_FETCHED_BE32) == 0x00000000) {
  560. new = (old | DDCB_PURGE_BE32);
  561. icrc_hsi_shi = cmpxchg(&pddcb->icrc_hsi_shi_32,
  562. old, new);
  563. if (icrc_hsi_shi == old)
  564. goto finish_ddcb;
  565. }
  566. /* normal finish with HSI bit */
  567. barrier();
  568. icrc_hsi_shi = pddcb->icrc_hsi_shi_32;
  569. if (icrc_hsi_shi & DDCB_COMPLETED_BE32)
  570. goto finish_ddcb;
  571. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  572. /*
  573. * Here the check_ddcb() function will most likely
  574. * discover this DDCB to be finished some point in
  575. * time. It will mark the req finished and free it up
  576. * in the list.
  577. */
  578. copy_ddcb_results(req, req->num); /* for the failing case */
  579. msleep(100); /* sleep for 1/10 second and try again */
  580. continue;
  581. finish_ddcb:
  582. copy_ddcb_results(req, req->num);
  583. ddcb_requ_set_state(req, GENWQE_REQU_FINISHED);
  584. queue->ddcbs_in_flight--;
  585. queue->ddcb_req[req->num] = NULL; /* delete from array */
  586. ddcb_mark_cleared(pddcb);
  587. /* Move active DDCB further; Nothing to do here anymore. */
  588. /*
  589. * We need to ensure that there is at least one free
  590. * DDCB in the queue. To do that, we must update
  591. * ddcb_act only if the COMPLETED bit is set for the
  592. * DDCB we are working on else we treat that DDCB even
  593. * if we PURGED it as occupied (hardware is supposed
  594. * to set the COMPLETED bit yet!).
  595. */
  596. icrc_hsi_shi = pddcb->icrc_hsi_shi_32;
  597. if ((icrc_hsi_shi & DDCB_COMPLETED_BE32) &&
  598. (queue->ddcb_act == req->num)) {
  599. queue->ddcb_act = ((queue->ddcb_act + 1) %
  600. queue->ddcb_max);
  601. }
  602. go_home:
  603. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  604. return 0;
  605. }
  606. /*
  607. * If the card is dead and the queue is forced to stop, we
  608. * might see this in the queue status register.
  609. */
  610. queue_status = __genwqe_readq(cd, queue->IO_QUEUE_STATUS);
  611. dev_dbg(&pci_dev->dev, "UN/FINISHED DDCB#%d\n", req->num);
  612. genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
  613. dev_err(&pci_dev->dev,
  614. "[%s] err: DDCB#%d not purged and not completed after %d seconds QSTAT=%016llx!!\n",
  615. __func__, req->num, genwqe_ddcb_software_timeout,
  616. queue_status);
  617. print_ddcb_info(cd, req->queue);
  618. return -EFAULT;
  619. }
  620. int genwqe_init_debug_data(struct genwqe_dev *cd, struct genwqe_debug_data *d)
  621. {
  622. int len;
  623. struct pci_dev *pci_dev = cd->pci_dev;
  624. if (d == NULL) {
  625. dev_err(&pci_dev->dev,
  626. "[%s] err: invalid memory for debug data!\n",
  627. __func__);
  628. return -EFAULT;
  629. }
  630. len = sizeof(d->driver_version);
  631. snprintf(d->driver_version, len, "%s", DRV_VERSION);
  632. d->slu_unitcfg = cd->slu_unitcfg;
  633. d->app_unitcfg = cd->app_unitcfg;
  634. return 0;
  635. }
  636. /**
  637. * __genwqe_enqueue_ddcb() - Enqueue a DDCB
  638. * @cd: pointer to genwqe device descriptor
  639. * @req: pointer to DDCB execution request
  640. * @f_flags: file mode: blocking, non-blocking
  641. *
  642. * Return: 0 if enqueuing succeeded
  643. * -EIO if card is unusable/PCIe problems
  644. * -EBUSY if enqueuing failed
  645. */
  646. int __genwqe_enqueue_ddcb(struct genwqe_dev *cd, struct ddcb_requ *req,
  647. unsigned int f_flags)
  648. {
  649. struct ddcb *pddcb;
  650. unsigned long flags;
  651. struct ddcb_queue *queue;
  652. struct pci_dev *pci_dev = cd->pci_dev;
  653. u16 icrc;
  654. retry:
  655. if (cd->card_state != GENWQE_CARD_USED) {
  656. printk_ratelimited(KERN_ERR
  657. "%s %s: [%s] Card is unusable/PCIe problem Req#%d\n",
  658. GENWQE_DEVNAME, dev_name(&pci_dev->dev),
  659. __func__, req->num);
  660. return -EIO;
  661. }
  662. queue = req->queue = &cd->queue;
  663. /* FIXME circumvention to improve performance when no irq is
  664. * there.
  665. */
  666. if (genwqe_polling_enabled)
  667. genwqe_check_ddcb_queue(cd, queue);
  668. /*
  669. * It must be ensured to process all DDCBs in successive
  670. * order. Use a lock here in order to prevent nested DDCB
  671. * enqueuing.
  672. */
  673. spin_lock_irqsave(&queue->ddcb_lock, flags);
  674. pddcb = get_next_ddcb(cd, queue, &req->num); /* get ptr and num */
  675. if (pddcb == NULL) {
  676. int rc;
  677. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  678. if (f_flags & O_NONBLOCK) {
  679. queue->return_on_busy++;
  680. return -EBUSY;
  681. }
  682. queue->wait_on_busy++;
  683. rc = wait_event_interruptible(queue->busy_waitq,
  684. queue_free_ddcbs(queue) != 0);
  685. dev_dbg(&pci_dev->dev, "[%s] waiting for free DDCB: rc=%d\n",
  686. __func__, rc);
  687. if (rc == -ERESTARTSYS)
  688. return rc; /* interrupted by a signal */
  689. goto retry;
  690. }
  691. if (queue->ddcb_req[req->num] != NULL) {
  692. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  693. dev_err(&pci_dev->dev,
  694. "[%s] picked DDCB %d with req=%p still in use!!\n",
  695. __func__, req->num, req);
  696. return -EFAULT;
  697. }
  698. ddcb_requ_set_state(req, GENWQE_REQU_ENQUEUED);
  699. queue->ddcb_req[req->num] = req;
  700. pddcb->cmdopts_16 = cpu_to_be16(req->cmd.cmdopts);
  701. pddcb->cmd = req->cmd.cmd;
  702. pddcb->acfunc = req->cmd.acfunc; /* functional unit */
  703. /*
  704. * We know that we can get retc 0x104 with CRC error, do not
  705. * stop the queue in those cases for this command. XDIR = 1
  706. * does not work for old SLU versions.
  707. *
  708. * Last bitstream with the old XDIR behavior had SLU_ID
  709. * 0x34199.
  710. */
  711. if ((cd->slu_unitcfg & 0xFFFF0ull) > 0x34199ull)
  712. pddcb->xdir = 0x1;
  713. else
  714. pddcb->xdir = 0x0;
  715. pddcb->psp = (((req->cmd.asiv_length / 8) << 4) |
  716. ((req->cmd.asv_length / 8)));
  717. pddcb->disp_ts_64 = cpu_to_be64(req->cmd.disp_ts);
  718. /*
  719. * If copying the whole DDCB_ASIV_LENGTH is impacting
  720. * performance we need to change it to
  721. * req->cmd.asiv_length. But simulation benefits from some
  722. * non-architectured bits behind the architectured content.
  723. *
  724. * How much data is copied depends on the availability of the
  725. * ATS field, which was introduced late. If the ATS field is
  726. * supported ASIV is 8 bytes shorter than it used to be. Since
  727. * the ATS field is copied too, the code should do exactly
  728. * what it did before, but I wanted to make copying of the ATS
  729. * field very explicit.
  730. */
  731. if (genwqe_get_slu_id(cd) <= 0x2) {
  732. memcpy(&pddcb->__asiv[0], /* destination */
  733. &req->cmd.__asiv[0], /* source */
  734. DDCB_ASIV_LENGTH); /* req->cmd.asiv_length */
  735. } else {
  736. pddcb->n.ats_64 = cpu_to_be64(req->cmd.ats);
  737. memcpy(&pddcb->n.asiv[0], /* destination */
  738. &req->cmd.asiv[0], /* source */
  739. DDCB_ASIV_LENGTH_ATS); /* req->cmd.asiv_length */
  740. }
  741. pddcb->icrc_hsi_shi_32 = cpu_to_be32(0x00000000); /* for crc */
  742. /*
  743. * Calculate CRC_16 for corresponding range PSP(7:4). Include
  744. * empty 4 bytes prior to the data.
  745. */
  746. icrc = genwqe_crc16((const u8 *)pddcb,
  747. ICRC_LENGTH(req->cmd.asiv_length), 0xffff);
  748. pddcb->icrc_hsi_shi_32 = cpu_to_be32((u32)icrc << 16);
  749. /* enable DDCB completion irq */
  750. if (!genwqe_polling_enabled)
  751. pddcb->icrc_hsi_shi_32 |= DDCB_INTR_BE32;
  752. dev_dbg(&pci_dev->dev, "INPUT DDCB#%d\n", req->num);
  753. genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
  754. if (ddcb_requ_collect_debug_data(req)) {
  755. /* use the kernel copy of debug data. copying back to
  756. user buffer happens later */
  757. genwqe_init_debug_data(cd, &req->debug_data);
  758. memcpy(&req->debug_data.ddcb_before, pddcb,
  759. sizeof(req->debug_data.ddcb_before));
  760. }
  761. enqueue_ddcb(cd, queue, pddcb, req->num);
  762. queue->ddcbs_in_flight++;
  763. if (queue->ddcbs_in_flight > queue->ddcbs_max_in_flight)
  764. queue->ddcbs_max_in_flight = queue->ddcbs_in_flight;
  765. ddcb_requ_set_state(req, GENWQE_REQU_TAPPED);
  766. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  767. wake_up_interruptible(&cd->queue_waitq);
  768. return 0;
  769. }
  770. /**
  771. * __genwqe_execute_raw_ddcb() - Setup and execute DDCB
  772. * @cd: pointer to genwqe device descriptor
  773. * @req: user provided DDCB request
  774. * @f_flags: file mode: blocking, non-blocking
  775. */
  776. int __genwqe_execute_raw_ddcb(struct genwqe_dev *cd,
  777. struct genwqe_ddcb_cmd *cmd,
  778. unsigned int f_flags)
  779. {
  780. int rc = 0;
  781. struct pci_dev *pci_dev = cd->pci_dev;
  782. struct ddcb_requ *req = container_of(cmd, struct ddcb_requ, cmd);
  783. if (cmd->asiv_length > DDCB_ASIV_LENGTH) {
  784. dev_err(&pci_dev->dev, "[%s] err: wrong asiv_length of %d\n",
  785. __func__, cmd->asiv_length);
  786. return -EINVAL;
  787. }
  788. if (cmd->asv_length > DDCB_ASV_LENGTH) {
  789. dev_err(&pci_dev->dev, "[%s] err: wrong asv_length of %d\n",
  790. __func__, cmd->asiv_length);
  791. return -EINVAL;
  792. }
  793. rc = __genwqe_enqueue_ddcb(cd, req, f_flags);
  794. if (rc != 0)
  795. return rc;
  796. rc = __genwqe_wait_ddcb(cd, req);
  797. if (rc < 0) /* error or signal interrupt */
  798. goto err_exit;
  799. if (ddcb_requ_collect_debug_data(req)) {
  800. if (copy_to_user((struct genwqe_debug_data __user *)
  801. (unsigned long)cmd->ddata_addr,
  802. &req->debug_data,
  803. sizeof(struct genwqe_debug_data)))
  804. return -EFAULT;
  805. }
  806. /*
  807. * Higher values than 0x102 indicate completion with faults,
  808. * lower values than 0x102 indicate processing faults. Note
  809. * that DDCB might have been purged. E.g. Cntl+C.
  810. */
  811. if (cmd->retc != DDCB_RETC_COMPLETE) {
  812. /* This might happen e.g. flash read, and needs to be
  813. handled by the upper layer code. */
  814. rc = -EBADMSG; /* not processed/error retc */
  815. }
  816. return rc;
  817. err_exit:
  818. __genwqe_purge_ddcb(cd, req);
  819. if (ddcb_requ_collect_debug_data(req)) {
  820. if (copy_to_user((struct genwqe_debug_data __user *)
  821. (unsigned long)cmd->ddata_addr,
  822. &req->debug_data,
  823. sizeof(struct genwqe_debug_data)))
  824. return -EFAULT;
  825. }
  826. return rc;
  827. }
  828. /**
  829. * genwqe_next_ddcb_ready() - Figure out if the next DDCB is already finished
  830. *
  831. * We use this as condition for our wait-queue code.
  832. */
  833. static int genwqe_next_ddcb_ready(struct genwqe_dev *cd)
  834. {
  835. unsigned long flags;
  836. struct ddcb *pddcb;
  837. struct ddcb_queue *queue = &cd->queue;
  838. spin_lock_irqsave(&queue->ddcb_lock, flags);
  839. if (queue_empty(queue)) { /* emtpy queue */
  840. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  841. return 0;
  842. }
  843. pddcb = &queue->ddcb_vaddr[queue->ddcb_act];
  844. if (pddcb->icrc_hsi_shi_32 & DDCB_COMPLETED_BE32) { /* ddcb ready */
  845. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  846. return 1;
  847. }
  848. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  849. return 0;
  850. }
  851. /**
  852. * genwqe_ddcbs_in_flight() - Check how many DDCBs are in flight
  853. *
  854. * Keep track on the number of DDCBs which ware currently in the
  855. * queue. This is needed for statistics as well as conditon if we want
  856. * to wait or better do polling in case of no interrupts available.
  857. */
  858. int genwqe_ddcbs_in_flight(struct genwqe_dev *cd)
  859. {
  860. unsigned long flags;
  861. int ddcbs_in_flight = 0;
  862. struct ddcb_queue *queue = &cd->queue;
  863. spin_lock_irqsave(&queue->ddcb_lock, flags);
  864. ddcbs_in_flight += queue->ddcbs_in_flight;
  865. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  866. return ddcbs_in_flight;
  867. }
  868. static int setup_ddcb_queue(struct genwqe_dev *cd, struct ddcb_queue *queue)
  869. {
  870. int rc, i;
  871. struct ddcb *pddcb;
  872. u64 val64;
  873. unsigned int queue_size;
  874. struct pci_dev *pci_dev = cd->pci_dev;
  875. if (genwqe_ddcb_max < 2)
  876. return -EINVAL;
  877. queue_size = roundup(genwqe_ddcb_max * sizeof(struct ddcb), PAGE_SIZE);
  878. queue->ddcbs_in_flight = 0; /* statistics */
  879. queue->ddcbs_max_in_flight = 0;
  880. queue->ddcbs_completed = 0;
  881. queue->return_on_busy = 0;
  882. queue->wait_on_busy = 0;
  883. queue->ddcb_seq = 0x100; /* start sequence number */
  884. queue->ddcb_max = genwqe_ddcb_max; /* module parameter */
  885. queue->ddcb_vaddr = __genwqe_alloc_consistent(cd, queue_size,
  886. &queue->ddcb_daddr);
  887. if (queue->ddcb_vaddr == NULL) {
  888. dev_err(&pci_dev->dev,
  889. "[%s] **err: could not allocate DDCB **\n", __func__);
  890. return -ENOMEM;
  891. }
  892. memset(queue->ddcb_vaddr, 0, queue_size);
  893. queue->ddcb_req = kzalloc(sizeof(struct ddcb_requ *) *
  894. queue->ddcb_max, GFP_KERNEL);
  895. if (!queue->ddcb_req) {
  896. rc = -ENOMEM;
  897. goto free_ddcbs;
  898. }
  899. queue->ddcb_waitqs = kzalloc(sizeof(wait_queue_head_t) *
  900. queue->ddcb_max, GFP_KERNEL);
  901. if (!queue->ddcb_waitqs) {
  902. rc = -ENOMEM;
  903. goto free_requs;
  904. }
  905. for (i = 0; i < queue->ddcb_max; i++) {
  906. pddcb = &queue->ddcb_vaddr[i]; /* DDCBs */
  907. pddcb->icrc_hsi_shi_32 = DDCB_COMPLETED_BE32;
  908. pddcb->retc_16 = cpu_to_be16(0xfff);
  909. queue->ddcb_req[i] = NULL; /* requests */
  910. init_waitqueue_head(&queue->ddcb_waitqs[i]); /* waitqueues */
  911. }
  912. queue->ddcb_act = 0;
  913. queue->ddcb_next = 0; /* queue is empty */
  914. spin_lock_init(&queue->ddcb_lock);
  915. init_waitqueue_head(&queue->busy_waitq);
  916. val64 = ((u64)(queue->ddcb_max - 1) << 8); /* lastptr */
  917. __genwqe_writeq(cd, queue->IO_QUEUE_CONFIG, 0x07); /* iCRC/vCRC */
  918. __genwqe_writeq(cd, queue->IO_QUEUE_SEGMENT, queue->ddcb_daddr);
  919. __genwqe_writeq(cd, queue->IO_QUEUE_INITSQN, queue->ddcb_seq);
  920. __genwqe_writeq(cd, queue->IO_QUEUE_WRAP, val64);
  921. return 0;
  922. free_requs:
  923. kfree(queue->ddcb_req);
  924. queue->ddcb_req = NULL;
  925. free_ddcbs:
  926. __genwqe_free_consistent(cd, queue_size, queue->ddcb_vaddr,
  927. queue->ddcb_daddr);
  928. queue->ddcb_vaddr = NULL;
  929. queue->ddcb_daddr = 0ull;
  930. return -ENODEV;
  931. }
  932. static int ddcb_queue_initialized(struct ddcb_queue *queue)
  933. {
  934. return queue->ddcb_vaddr != NULL;
  935. }
  936. static void free_ddcb_queue(struct genwqe_dev *cd, struct ddcb_queue *queue)
  937. {
  938. unsigned int queue_size;
  939. queue_size = roundup(queue->ddcb_max * sizeof(struct ddcb), PAGE_SIZE);
  940. kfree(queue->ddcb_req);
  941. queue->ddcb_req = NULL;
  942. if (queue->ddcb_vaddr) {
  943. __genwqe_free_consistent(cd, queue_size, queue->ddcb_vaddr,
  944. queue->ddcb_daddr);
  945. queue->ddcb_vaddr = NULL;
  946. queue->ddcb_daddr = 0ull;
  947. }
  948. }
  949. static irqreturn_t genwqe_pf_isr(int irq, void *dev_id)
  950. {
  951. u64 gfir;
  952. struct genwqe_dev *cd = (struct genwqe_dev *)dev_id;
  953. struct pci_dev *pci_dev = cd->pci_dev;
  954. /*
  955. * In case of fatal FIR error the queue is stopped, such that
  956. * we can safely check it without risking anything.
  957. */
  958. cd->irqs_processed++;
  959. wake_up_interruptible(&cd->queue_waitq);
  960. /*
  961. * Checking for errors before kicking the queue might be
  962. * safer, but slower for the good-case ... See above.
  963. */
  964. gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
  965. if (((gfir & GFIR_ERR_TRIGGER) != 0x0) &&
  966. !pci_channel_offline(pci_dev)) {
  967. if (cd->use_platform_recovery) {
  968. /*
  969. * Since we use raw accessors, EEH errors won't be
  970. * detected by the platform until we do a non-raw
  971. * MMIO or config space read
  972. */
  973. readq(cd->mmio + IO_SLC_CFGREG_GFIR);
  974. /* Don't do anything if the PCI channel is frozen */
  975. if (pci_channel_offline(pci_dev))
  976. goto exit;
  977. }
  978. wake_up_interruptible(&cd->health_waitq);
  979. /*
  980. * By default GFIRs causes recovery actions. This
  981. * count is just for debug when recovery is masked.
  982. */
  983. dev_err_ratelimited(&pci_dev->dev,
  984. "[%s] GFIR=%016llx\n",
  985. __func__, gfir);
  986. }
  987. exit:
  988. return IRQ_HANDLED;
  989. }
  990. static irqreturn_t genwqe_vf_isr(int irq, void *dev_id)
  991. {
  992. struct genwqe_dev *cd = (struct genwqe_dev *)dev_id;
  993. cd->irqs_processed++;
  994. wake_up_interruptible(&cd->queue_waitq);
  995. return IRQ_HANDLED;
  996. }
  997. /**
  998. * genwqe_card_thread() - Work thread for the DDCB queue
  999. *
  1000. * The idea is to check if there are DDCBs in processing. If there are
  1001. * some finished DDCBs, we process them and wakeup the
  1002. * requestors. Otherwise we give other processes time using
  1003. * cond_resched().
  1004. */
  1005. static int genwqe_card_thread(void *data)
  1006. {
  1007. int should_stop = 0, rc = 0;
  1008. struct genwqe_dev *cd = (struct genwqe_dev *)data;
  1009. while (!kthread_should_stop()) {
  1010. genwqe_check_ddcb_queue(cd, &cd->queue);
  1011. if (genwqe_polling_enabled) {
  1012. rc = wait_event_interruptible_timeout(
  1013. cd->queue_waitq,
  1014. genwqe_ddcbs_in_flight(cd) ||
  1015. (should_stop = kthread_should_stop()), 1);
  1016. } else {
  1017. rc = wait_event_interruptible_timeout(
  1018. cd->queue_waitq,
  1019. genwqe_next_ddcb_ready(cd) ||
  1020. (should_stop = kthread_should_stop()), HZ);
  1021. }
  1022. if (should_stop)
  1023. break;
  1024. /*
  1025. * Avoid soft lockups on heavy loads; we do not want
  1026. * to disable our interrupts.
  1027. */
  1028. cond_resched();
  1029. }
  1030. return 0;
  1031. }
  1032. /**
  1033. * genwqe_setup_service_layer() - Setup DDCB queue
  1034. * @cd: pointer to genwqe device descriptor
  1035. *
  1036. * Allocate DDCBs. Configure Service Layer Controller (SLC).
  1037. *
  1038. * Return: 0 success
  1039. */
  1040. int genwqe_setup_service_layer(struct genwqe_dev *cd)
  1041. {
  1042. int rc;
  1043. struct ddcb_queue *queue;
  1044. struct pci_dev *pci_dev = cd->pci_dev;
  1045. if (genwqe_is_privileged(cd)) {
  1046. rc = genwqe_card_reset(cd);
  1047. if (rc < 0) {
  1048. dev_err(&pci_dev->dev,
  1049. "[%s] err: reset failed.\n", __func__);
  1050. return rc;
  1051. }
  1052. genwqe_read_softreset(cd);
  1053. }
  1054. queue = &cd->queue;
  1055. queue->IO_QUEUE_CONFIG = IO_SLC_QUEUE_CONFIG;
  1056. queue->IO_QUEUE_STATUS = IO_SLC_QUEUE_STATUS;
  1057. queue->IO_QUEUE_SEGMENT = IO_SLC_QUEUE_SEGMENT;
  1058. queue->IO_QUEUE_INITSQN = IO_SLC_QUEUE_INITSQN;
  1059. queue->IO_QUEUE_OFFSET = IO_SLC_QUEUE_OFFSET;
  1060. queue->IO_QUEUE_WRAP = IO_SLC_QUEUE_WRAP;
  1061. queue->IO_QUEUE_WTIME = IO_SLC_QUEUE_WTIME;
  1062. queue->IO_QUEUE_ERRCNTS = IO_SLC_QUEUE_ERRCNTS;
  1063. queue->IO_QUEUE_LRW = IO_SLC_QUEUE_LRW;
  1064. rc = setup_ddcb_queue(cd, queue);
  1065. if (rc != 0) {
  1066. rc = -ENODEV;
  1067. goto err_out;
  1068. }
  1069. init_waitqueue_head(&cd->queue_waitq);
  1070. cd->card_thread = kthread_run(genwqe_card_thread, cd,
  1071. GENWQE_DEVNAME "%d_thread",
  1072. cd->card_idx);
  1073. if (IS_ERR(cd->card_thread)) {
  1074. rc = PTR_ERR(cd->card_thread);
  1075. cd->card_thread = NULL;
  1076. goto stop_free_queue;
  1077. }
  1078. rc = genwqe_set_interrupt_capability(cd, GENWQE_MSI_IRQS);
  1079. if (rc)
  1080. goto stop_kthread;
  1081. /*
  1082. * We must have all wait-queues initialized when we enable the
  1083. * interrupts. Otherwise we might crash if we get an early
  1084. * irq.
  1085. */
  1086. init_waitqueue_head(&cd->health_waitq);
  1087. if (genwqe_is_privileged(cd)) {
  1088. rc = request_irq(pci_dev->irq, genwqe_pf_isr, IRQF_SHARED,
  1089. GENWQE_DEVNAME, cd);
  1090. } else {
  1091. rc = request_irq(pci_dev->irq, genwqe_vf_isr, IRQF_SHARED,
  1092. GENWQE_DEVNAME, cd);
  1093. }
  1094. if (rc < 0) {
  1095. dev_err(&pci_dev->dev, "irq %d not free.\n", pci_dev->irq);
  1096. goto stop_irq_cap;
  1097. }
  1098. cd->card_state = GENWQE_CARD_USED;
  1099. return 0;
  1100. stop_irq_cap:
  1101. genwqe_reset_interrupt_capability(cd);
  1102. stop_kthread:
  1103. kthread_stop(cd->card_thread);
  1104. cd->card_thread = NULL;
  1105. stop_free_queue:
  1106. free_ddcb_queue(cd, queue);
  1107. err_out:
  1108. return rc;
  1109. }
  1110. /**
  1111. * queue_wake_up_all() - Handles fatal error case
  1112. *
  1113. * The PCI device got unusable and we have to stop all pending
  1114. * requests as fast as we can. The code after this must purge the
  1115. * DDCBs in question and ensure that all mappings are freed.
  1116. */
  1117. static int queue_wake_up_all(struct genwqe_dev *cd)
  1118. {
  1119. unsigned int i;
  1120. unsigned long flags;
  1121. struct ddcb_queue *queue = &cd->queue;
  1122. spin_lock_irqsave(&queue->ddcb_lock, flags);
  1123. for (i = 0; i < queue->ddcb_max; i++)
  1124. wake_up_interruptible(&queue->ddcb_waitqs[queue->ddcb_act]);
  1125. wake_up_interruptible(&queue->busy_waitq);
  1126. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  1127. return 0;
  1128. }
  1129. /**
  1130. * genwqe_finish_queue() - Remove any genwqe devices and user-interfaces
  1131. *
  1132. * Relies on the pre-condition that there are no users of the card
  1133. * device anymore e.g. with open file-descriptors.
  1134. *
  1135. * This function must be robust enough to be called twice.
  1136. */
  1137. int genwqe_finish_queue(struct genwqe_dev *cd)
  1138. {
  1139. int i, rc = 0, in_flight;
  1140. int waitmax = genwqe_ddcb_software_timeout;
  1141. struct pci_dev *pci_dev = cd->pci_dev;
  1142. struct ddcb_queue *queue = &cd->queue;
  1143. if (!ddcb_queue_initialized(queue))
  1144. return 0;
  1145. /* Do not wipe out the error state. */
  1146. if (cd->card_state == GENWQE_CARD_USED)
  1147. cd->card_state = GENWQE_CARD_UNUSED;
  1148. /* Wake up all requests in the DDCB queue such that they
  1149. should be removed nicely. */
  1150. queue_wake_up_all(cd);
  1151. /* We must wait to get rid of the DDCBs in flight */
  1152. for (i = 0; i < waitmax; i++) {
  1153. in_flight = genwqe_ddcbs_in_flight(cd);
  1154. if (in_flight == 0)
  1155. break;
  1156. dev_dbg(&pci_dev->dev,
  1157. " DEBUG [%d/%d] waiting for queue to get empty: %d requests!\n",
  1158. i, waitmax, in_flight);
  1159. /*
  1160. * Severe severe error situation: The card itself has
  1161. * 16 DDCB queues, each queue has e.g. 32 entries,
  1162. * each DDBC has a hardware timeout of currently 250
  1163. * msec but the PFs have a hardware timeout of 8 sec
  1164. * ... so I take something large.
  1165. */
  1166. msleep(1000);
  1167. }
  1168. if (i == waitmax) {
  1169. dev_err(&pci_dev->dev, " [%s] err: queue is not empty!!\n",
  1170. __func__);
  1171. rc = -EIO;
  1172. }
  1173. return rc;
  1174. }
  1175. /**
  1176. * genwqe_release_service_layer() - Shutdown DDCB queue
  1177. * @cd: genwqe device descriptor
  1178. *
  1179. * This function must be robust enough to be called twice.
  1180. */
  1181. int genwqe_release_service_layer(struct genwqe_dev *cd)
  1182. {
  1183. struct pci_dev *pci_dev = cd->pci_dev;
  1184. if (!ddcb_queue_initialized(&cd->queue))
  1185. return 1;
  1186. free_irq(pci_dev->irq, cd);
  1187. genwqe_reset_interrupt_capability(cd);
  1188. if (cd->card_thread != NULL) {
  1189. kthread_stop(cd->card_thread);
  1190. cd->card_thread = NULL;
  1191. }
  1192. free_ddcb_queue(cd, &cd->queue);
  1193. return 0;
  1194. }