card_ddcb.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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. queue->ddcb_req = kzalloc(sizeof(struct ddcb_requ *) *
  893. queue->ddcb_max, GFP_KERNEL);
  894. if (!queue->ddcb_req) {
  895. rc = -ENOMEM;
  896. goto free_ddcbs;
  897. }
  898. queue->ddcb_waitqs = kzalloc(sizeof(wait_queue_head_t) *
  899. queue->ddcb_max, GFP_KERNEL);
  900. if (!queue->ddcb_waitqs) {
  901. rc = -ENOMEM;
  902. goto free_requs;
  903. }
  904. for (i = 0; i < queue->ddcb_max; i++) {
  905. pddcb = &queue->ddcb_vaddr[i]; /* DDCBs */
  906. pddcb->icrc_hsi_shi_32 = DDCB_COMPLETED_BE32;
  907. pddcb->retc_16 = cpu_to_be16(0xfff);
  908. queue->ddcb_req[i] = NULL; /* requests */
  909. init_waitqueue_head(&queue->ddcb_waitqs[i]); /* waitqueues */
  910. }
  911. queue->ddcb_act = 0;
  912. queue->ddcb_next = 0; /* queue is empty */
  913. spin_lock_init(&queue->ddcb_lock);
  914. init_waitqueue_head(&queue->busy_waitq);
  915. val64 = ((u64)(queue->ddcb_max - 1) << 8); /* lastptr */
  916. __genwqe_writeq(cd, queue->IO_QUEUE_CONFIG, 0x07); /* iCRC/vCRC */
  917. __genwqe_writeq(cd, queue->IO_QUEUE_SEGMENT, queue->ddcb_daddr);
  918. __genwqe_writeq(cd, queue->IO_QUEUE_INITSQN, queue->ddcb_seq);
  919. __genwqe_writeq(cd, queue->IO_QUEUE_WRAP, val64);
  920. return 0;
  921. free_requs:
  922. kfree(queue->ddcb_req);
  923. queue->ddcb_req = NULL;
  924. free_ddcbs:
  925. __genwqe_free_consistent(cd, queue_size, queue->ddcb_vaddr,
  926. queue->ddcb_daddr);
  927. queue->ddcb_vaddr = NULL;
  928. queue->ddcb_daddr = 0ull;
  929. return -ENODEV;
  930. }
  931. static int ddcb_queue_initialized(struct ddcb_queue *queue)
  932. {
  933. return queue->ddcb_vaddr != NULL;
  934. }
  935. static void free_ddcb_queue(struct genwqe_dev *cd, struct ddcb_queue *queue)
  936. {
  937. unsigned int queue_size;
  938. queue_size = roundup(queue->ddcb_max * sizeof(struct ddcb), PAGE_SIZE);
  939. kfree(queue->ddcb_req);
  940. queue->ddcb_req = NULL;
  941. if (queue->ddcb_vaddr) {
  942. __genwqe_free_consistent(cd, queue_size, queue->ddcb_vaddr,
  943. queue->ddcb_daddr);
  944. queue->ddcb_vaddr = NULL;
  945. queue->ddcb_daddr = 0ull;
  946. }
  947. }
  948. static irqreturn_t genwqe_pf_isr(int irq, void *dev_id)
  949. {
  950. u64 gfir;
  951. struct genwqe_dev *cd = (struct genwqe_dev *)dev_id;
  952. struct pci_dev *pci_dev = cd->pci_dev;
  953. /*
  954. * In case of fatal FIR error the queue is stopped, such that
  955. * we can safely check it without risking anything.
  956. */
  957. cd->irqs_processed++;
  958. wake_up_interruptible(&cd->queue_waitq);
  959. /*
  960. * Checking for errors before kicking the queue might be
  961. * safer, but slower for the good-case ... See above.
  962. */
  963. gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
  964. if (((gfir & GFIR_ERR_TRIGGER) != 0x0) &&
  965. !pci_channel_offline(pci_dev)) {
  966. if (cd->use_platform_recovery) {
  967. /*
  968. * Since we use raw accessors, EEH errors won't be
  969. * detected by the platform until we do a non-raw
  970. * MMIO or config space read
  971. */
  972. readq(cd->mmio + IO_SLC_CFGREG_GFIR);
  973. /* Don't do anything if the PCI channel is frozen */
  974. if (pci_channel_offline(pci_dev))
  975. goto exit;
  976. }
  977. wake_up_interruptible(&cd->health_waitq);
  978. /*
  979. * By default GFIRs causes recovery actions. This
  980. * count is just for debug when recovery is masked.
  981. */
  982. dev_err_ratelimited(&pci_dev->dev,
  983. "[%s] GFIR=%016llx\n",
  984. __func__, gfir);
  985. }
  986. exit:
  987. return IRQ_HANDLED;
  988. }
  989. static irqreturn_t genwqe_vf_isr(int irq, void *dev_id)
  990. {
  991. struct genwqe_dev *cd = (struct genwqe_dev *)dev_id;
  992. cd->irqs_processed++;
  993. wake_up_interruptible(&cd->queue_waitq);
  994. return IRQ_HANDLED;
  995. }
  996. /**
  997. * genwqe_card_thread() - Work thread for the DDCB queue
  998. *
  999. * The idea is to check if there are DDCBs in processing. If there are
  1000. * some finished DDCBs, we process them and wakeup the
  1001. * requestors. Otherwise we give other processes time using
  1002. * cond_resched().
  1003. */
  1004. static int genwqe_card_thread(void *data)
  1005. {
  1006. int should_stop = 0, rc = 0;
  1007. struct genwqe_dev *cd = (struct genwqe_dev *)data;
  1008. while (!kthread_should_stop()) {
  1009. genwqe_check_ddcb_queue(cd, &cd->queue);
  1010. if (genwqe_polling_enabled) {
  1011. rc = wait_event_interruptible_timeout(
  1012. cd->queue_waitq,
  1013. genwqe_ddcbs_in_flight(cd) ||
  1014. (should_stop = kthread_should_stop()), 1);
  1015. } else {
  1016. rc = wait_event_interruptible_timeout(
  1017. cd->queue_waitq,
  1018. genwqe_next_ddcb_ready(cd) ||
  1019. (should_stop = kthread_should_stop()), HZ);
  1020. }
  1021. if (should_stop)
  1022. break;
  1023. /*
  1024. * Avoid soft lockups on heavy loads; we do not want
  1025. * to disable our interrupts.
  1026. */
  1027. cond_resched();
  1028. }
  1029. return 0;
  1030. }
  1031. /**
  1032. * genwqe_setup_service_layer() - Setup DDCB queue
  1033. * @cd: pointer to genwqe device descriptor
  1034. *
  1035. * Allocate DDCBs. Configure Service Layer Controller (SLC).
  1036. *
  1037. * Return: 0 success
  1038. */
  1039. int genwqe_setup_service_layer(struct genwqe_dev *cd)
  1040. {
  1041. int rc;
  1042. struct ddcb_queue *queue;
  1043. struct pci_dev *pci_dev = cd->pci_dev;
  1044. if (genwqe_is_privileged(cd)) {
  1045. rc = genwqe_card_reset(cd);
  1046. if (rc < 0) {
  1047. dev_err(&pci_dev->dev,
  1048. "[%s] err: reset failed.\n", __func__);
  1049. return rc;
  1050. }
  1051. genwqe_read_softreset(cd);
  1052. }
  1053. queue = &cd->queue;
  1054. queue->IO_QUEUE_CONFIG = IO_SLC_QUEUE_CONFIG;
  1055. queue->IO_QUEUE_STATUS = IO_SLC_QUEUE_STATUS;
  1056. queue->IO_QUEUE_SEGMENT = IO_SLC_QUEUE_SEGMENT;
  1057. queue->IO_QUEUE_INITSQN = IO_SLC_QUEUE_INITSQN;
  1058. queue->IO_QUEUE_OFFSET = IO_SLC_QUEUE_OFFSET;
  1059. queue->IO_QUEUE_WRAP = IO_SLC_QUEUE_WRAP;
  1060. queue->IO_QUEUE_WTIME = IO_SLC_QUEUE_WTIME;
  1061. queue->IO_QUEUE_ERRCNTS = IO_SLC_QUEUE_ERRCNTS;
  1062. queue->IO_QUEUE_LRW = IO_SLC_QUEUE_LRW;
  1063. rc = setup_ddcb_queue(cd, queue);
  1064. if (rc != 0) {
  1065. rc = -ENODEV;
  1066. goto err_out;
  1067. }
  1068. init_waitqueue_head(&cd->queue_waitq);
  1069. cd->card_thread = kthread_run(genwqe_card_thread, cd,
  1070. GENWQE_DEVNAME "%d_thread",
  1071. cd->card_idx);
  1072. if (IS_ERR(cd->card_thread)) {
  1073. rc = PTR_ERR(cd->card_thread);
  1074. cd->card_thread = NULL;
  1075. goto stop_free_queue;
  1076. }
  1077. rc = genwqe_set_interrupt_capability(cd, GENWQE_MSI_IRQS);
  1078. if (rc)
  1079. goto stop_kthread;
  1080. /*
  1081. * We must have all wait-queues initialized when we enable the
  1082. * interrupts. Otherwise we might crash if we get an early
  1083. * irq.
  1084. */
  1085. init_waitqueue_head(&cd->health_waitq);
  1086. if (genwqe_is_privileged(cd)) {
  1087. rc = request_irq(pci_dev->irq, genwqe_pf_isr, IRQF_SHARED,
  1088. GENWQE_DEVNAME, cd);
  1089. } else {
  1090. rc = request_irq(pci_dev->irq, genwqe_vf_isr, IRQF_SHARED,
  1091. GENWQE_DEVNAME, cd);
  1092. }
  1093. if (rc < 0) {
  1094. dev_err(&pci_dev->dev, "irq %d not free.\n", pci_dev->irq);
  1095. goto stop_irq_cap;
  1096. }
  1097. cd->card_state = GENWQE_CARD_USED;
  1098. return 0;
  1099. stop_irq_cap:
  1100. genwqe_reset_interrupt_capability(cd);
  1101. stop_kthread:
  1102. kthread_stop(cd->card_thread);
  1103. cd->card_thread = NULL;
  1104. stop_free_queue:
  1105. free_ddcb_queue(cd, queue);
  1106. err_out:
  1107. return rc;
  1108. }
  1109. /**
  1110. * queue_wake_up_all() - Handles fatal error case
  1111. *
  1112. * The PCI device got unusable and we have to stop all pending
  1113. * requests as fast as we can. The code after this must purge the
  1114. * DDCBs in question and ensure that all mappings are freed.
  1115. */
  1116. static int queue_wake_up_all(struct genwqe_dev *cd)
  1117. {
  1118. unsigned int i;
  1119. unsigned long flags;
  1120. struct ddcb_queue *queue = &cd->queue;
  1121. spin_lock_irqsave(&queue->ddcb_lock, flags);
  1122. for (i = 0; i < queue->ddcb_max; i++)
  1123. wake_up_interruptible(&queue->ddcb_waitqs[queue->ddcb_act]);
  1124. wake_up_interruptible(&queue->busy_waitq);
  1125. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  1126. return 0;
  1127. }
  1128. /**
  1129. * genwqe_finish_queue() - Remove any genwqe devices and user-interfaces
  1130. *
  1131. * Relies on the pre-condition that there are no users of the card
  1132. * device anymore e.g. with open file-descriptors.
  1133. *
  1134. * This function must be robust enough to be called twice.
  1135. */
  1136. int genwqe_finish_queue(struct genwqe_dev *cd)
  1137. {
  1138. int i, rc = 0, in_flight;
  1139. int waitmax = genwqe_ddcb_software_timeout;
  1140. struct pci_dev *pci_dev = cd->pci_dev;
  1141. struct ddcb_queue *queue = &cd->queue;
  1142. if (!ddcb_queue_initialized(queue))
  1143. return 0;
  1144. /* Do not wipe out the error state. */
  1145. if (cd->card_state == GENWQE_CARD_USED)
  1146. cd->card_state = GENWQE_CARD_UNUSED;
  1147. /* Wake up all requests in the DDCB queue such that they
  1148. should be removed nicely. */
  1149. queue_wake_up_all(cd);
  1150. /* We must wait to get rid of the DDCBs in flight */
  1151. for (i = 0; i < waitmax; i++) {
  1152. in_flight = genwqe_ddcbs_in_flight(cd);
  1153. if (in_flight == 0)
  1154. break;
  1155. dev_dbg(&pci_dev->dev,
  1156. " DEBUG [%d/%d] waiting for queue to get empty: %d requests!\n",
  1157. i, waitmax, in_flight);
  1158. /*
  1159. * Severe severe error situation: The card itself has
  1160. * 16 DDCB queues, each queue has e.g. 32 entries,
  1161. * each DDBC has a hardware timeout of currently 250
  1162. * msec but the PFs have a hardware timeout of 8 sec
  1163. * ... so I take something large.
  1164. */
  1165. msleep(1000);
  1166. }
  1167. if (i == waitmax) {
  1168. dev_err(&pci_dev->dev, " [%s] err: queue is not empty!!\n",
  1169. __func__);
  1170. rc = -EIO;
  1171. }
  1172. return rc;
  1173. }
  1174. /**
  1175. * genwqe_release_service_layer() - Shutdown DDCB queue
  1176. * @cd: genwqe device descriptor
  1177. *
  1178. * This function must be robust enough to be called twice.
  1179. */
  1180. int genwqe_release_service_layer(struct genwqe_dev *cd)
  1181. {
  1182. struct pci_dev *pci_dev = cd->pci_dev;
  1183. if (!ddcb_queue_initialized(&cd->queue))
  1184. return 1;
  1185. free_irq(pci_dev->irq, cd);
  1186. genwqe_reset_interrupt_capability(cd);
  1187. if (cd->card_thread != NULL) {
  1188. kthread_stop(cd->card_thread);
  1189. cd->card_thread = NULL;
  1190. }
  1191. free_ddcb_queue(cd, &cd->queue);
  1192. return 0;
  1193. }