card_ddcb.c 37 KB

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