card_ddcb.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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/sched.h>
  29. #include <linux/wait.h>
  30. #include <linux/pci.h>
  31. #include <linux/string.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/delay.h>
  34. #include <linux/module.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/crc-itu-t.h>
  37. #include "card_base.h"
  38. #include "card_ddcb.h"
  39. /*
  40. * N: next DDCB, this is where the next DDCB will be put.
  41. * A: active DDCB, this is where the code will look for the next completion.
  42. * x: DDCB is enqueued, we are waiting for its completion.
  43. * Situation (1): Empty queue
  44. * +---+---+---+---+---+---+---+---+
  45. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  46. * | | | | | | | | |
  47. * +---+---+---+---+---+---+---+---+
  48. * A/N
  49. * enqueued_ddcbs = A - N = 2 - 2 = 0
  50. *
  51. * Situation (2): Wrapped, N > A
  52. * +---+---+---+---+---+---+---+---+
  53. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  54. * | | | x | x | | | | |
  55. * +---+---+---+---+---+---+---+---+
  56. * A N
  57. * enqueued_ddcbs = N - A = 4 - 2 = 2
  58. *
  59. * Situation (3): Queue wrapped, A > N
  60. * +---+---+---+---+---+---+---+---+
  61. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  62. * | x | x | | | x | x | x | x |
  63. * +---+---+---+---+---+---+---+---+
  64. * N A
  65. * enqueued_ddcbs = queue_max - (A - N) = 8 - (4 - 2) = 6
  66. *
  67. * Situation (4a): Queue full N > A
  68. * +---+---+---+---+---+---+---+---+
  69. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  70. * | x | x | x | x | x | x | x | |
  71. * +---+---+---+---+---+---+---+---+
  72. * A N
  73. *
  74. * enqueued_ddcbs = N - A = 7 - 0 = 7
  75. *
  76. * Situation (4a): Queue full A > N
  77. * +---+---+---+---+---+---+---+---+
  78. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  79. * | x | x | x | | x | x | x | x |
  80. * +---+---+---+---+---+---+---+---+
  81. * N A
  82. * enqueued_ddcbs = queue_max - (A - N) = 8 - (4 - 3) = 7
  83. */
  84. static int queue_empty(struct ddcb_queue *queue)
  85. {
  86. return queue->ddcb_next == queue->ddcb_act;
  87. }
  88. static int queue_enqueued_ddcbs(struct ddcb_queue *queue)
  89. {
  90. if (queue->ddcb_next >= queue->ddcb_act)
  91. return queue->ddcb_next - queue->ddcb_act;
  92. return queue->ddcb_max - (queue->ddcb_act - queue->ddcb_next);
  93. }
  94. static int queue_free_ddcbs(struct ddcb_queue *queue)
  95. {
  96. int free_ddcbs = queue->ddcb_max - queue_enqueued_ddcbs(queue) - 1;
  97. if (WARN_ON_ONCE(free_ddcbs < 0)) { /* must never ever happen! */
  98. return 0;
  99. }
  100. return free_ddcbs;
  101. }
  102. /*
  103. * Use of the PRIV field in the DDCB for queue debugging:
  104. *
  105. * (1) Trying to get rid of a DDCB which saw a timeout:
  106. * pddcb->priv[6] = 0xcc; # cleared
  107. *
  108. * (2) Append a DDCB via NEXT bit:
  109. * pddcb->priv[7] = 0xaa; # appended
  110. *
  111. * (3) DDCB needed tapping:
  112. * pddcb->priv[7] = 0xbb; # tapped
  113. *
  114. * (4) DDCB marked as correctly finished:
  115. * pddcb->priv[6] = 0xff; # finished
  116. */
  117. static inline void ddcb_mark_tapped(struct ddcb *pddcb)
  118. {
  119. pddcb->priv[7] = 0xbb; /* tapped */
  120. }
  121. static inline void ddcb_mark_appended(struct ddcb *pddcb)
  122. {
  123. pddcb->priv[7] = 0xaa; /* appended */
  124. }
  125. static inline void ddcb_mark_cleared(struct ddcb *pddcb)
  126. {
  127. pddcb->priv[6] = 0xcc; /* cleared */
  128. }
  129. static inline void ddcb_mark_finished(struct ddcb *pddcb)
  130. {
  131. pddcb->priv[6] = 0xff; /* finished */
  132. }
  133. static inline void ddcb_mark_unused(struct ddcb *pddcb)
  134. {
  135. pddcb->priv_64 = cpu_to_be64(0); /* not tapped */
  136. }
  137. /**
  138. * genwqe_crc16() - Generate 16-bit crc as required for DDCBs
  139. * @buff: pointer to data buffer
  140. * @len: length of data for calculation
  141. * @init: initial crc (0xffff at start)
  142. *
  143. * Polynomial = x^16 + x^12 + x^5 + 1 (0x1021)
  144. * Example: 4 bytes 0x01 0x02 0x03 0x04 with init = 0xffff
  145. * should result in a crc16 of 0x89c3
  146. *
  147. * Return: crc16 checksum in big endian format !
  148. */
  149. static inline u16 genwqe_crc16(const u8 *buff, size_t len, u16 init)
  150. {
  151. return crc_itu_t(init, buff, len);
  152. }
  153. static void print_ddcb_info(struct genwqe_dev *cd, struct ddcb_queue *queue)
  154. {
  155. int i;
  156. struct ddcb *pddcb;
  157. unsigned long flags;
  158. struct pci_dev *pci_dev = cd->pci_dev;
  159. spin_lock_irqsave(&cd->print_lock, flags);
  160. dev_info(&pci_dev->dev,
  161. "DDCB list for card #%d (ddcb_act=%d / ddcb_next=%d):\n",
  162. cd->card_idx, queue->ddcb_act, queue->ddcb_next);
  163. pddcb = queue->ddcb_vaddr;
  164. for (i = 0; i < queue->ddcb_max; i++) {
  165. dev_err(&pci_dev->dev,
  166. " %c %-3d: RETC=%03x SEQ=%04x HSI=%02X SHI=%02x PRIV=%06llx CMD=%03x\n",
  167. i == queue->ddcb_act ? '>' : ' ',
  168. i,
  169. be16_to_cpu(pddcb->retc_16),
  170. be16_to_cpu(pddcb->seqnum_16),
  171. pddcb->hsi,
  172. pddcb->shi,
  173. be64_to_cpu(pddcb->priv_64),
  174. pddcb->cmd);
  175. pddcb++;
  176. }
  177. spin_unlock_irqrestore(&cd->print_lock, flags);
  178. }
  179. struct genwqe_ddcb_cmd *ddcb_requ_alloc(void)
  180. {
  181. struct ddcb_requ *req;
  182. req = kzalloc(sizeof(*req), GFP_KERNEL);
  183. if (!req)
  184. return NULL;
  185. return &req->cmd;
  186. }
  187. void ddcb_requ_free(struct genwqe_ddcb_cmd *cmd)
  188. {
  189. struct ddcb_requ *req = container_of(cmd, struct ddcb_requ, cmd);
  190. kfree(req);
  191. }
  192. static inline enum genwqe_requ_state ddcb_requ_get_state(struct ddcb_requ *req)
  193. {
  194. return req->req_state;
  195. }
  196. static inline void ddcb_requ_set_state(struct ddcb_requ *req,
  197. enum genwqe_requ_state new_state)
  198. {
  199. req->req_state = new_state;
  200. }
  201. static inline int ddcb_requ_collect_debug_data(struct ddcb_requ *req)
  202. {
  203. return req->cmd.ddata_addr != 0x0;
  204. }
  205. /**
  206. * ddcb_requ_finished() - Returns the hardware state of the associated DDCB
  207. * @cd: pointer to genwqe device descriptor
  208. * @req: DDCB work request
  209. *
  210. * Status of ddcb_requ mirrors this hardware state, but is copied in
  211. * the ddcb_requ on interrupt/polling function. The lowlevel code
  212. * should check the hardware state directly, the higher level code
  213. * should check the copy.
  214. *
  215. * This function will also return true if the state of the queue is
  216. * not GENWQE_CARD_USED. This enables us to purge all DDCBs in the
  217. * shutdown case.
  218. */
  219. static int ddcb_requ_finished(struct genwqe_dev *cd, struct ddcb_requ *req)
  220. {
  221. return (ddcb_requ_get_state(req) == GENWQE_REQU_FINISHED) ||
  222. (cd->card_state != GENWQE_CARD_USED);
  223. }
  224. /**
  225. * enqueue_ddcb() - Enqueue a DDCB
  226. * @cd: pointer to genwqe device descriptor
  227. * @queue: queue this operation should be done on
  228. * @ddcb_no: pointer to ddcb number being tapped
  229. *
  230. * Start execution of DDCB by tapping or append to queue via NEXT
  231. * bit. This is done by an atomic 'compare and swap' instruction and
  232. * checking SHI and HSI of the previous DDCB.
  233. *
  234. * This function must only be called with ddcb_lock held.
  235. *
  236. * Return: 1 if new DDCB is appended to previous
  237. * 2 if DDCB queue is tapped via register/simulation
  238. */
  239. #define RET_DDCB_APPENDED 1
  240. #define RET_DDCB_TAPPED 2
  241. static int enqueue_ddcb(struct genwqe_dev *cd, struct ddcb_queue *queue,
  242. struct ddcb *pddcb, int ddcb_no)
  243. {
  244. unsigned int try;
  245. int prev_no;
  246. struct ddcb *prev_ddcb;
  247. __be32 old, new, icrc_hsi_shi;
  248. u64 num;
  249. /*
  250. * For performance checks a Dispatch Timestamp can be put into
  251. * DDCB It is supposed to use the SLU's free running counter,
  252. * but this requires PCIe cycles.
  253. */
  254. ddcb_mark_unused(pddcb);
  255. /* check previous DDCB if already fetched */
  256. prev_no = (ddcb_no == 0) ? queue->ddcb_max - 1 : ddcb_no - 1;
  257. prev_ddcb = &queue->ddcb_vaddr[prev_no];
  258. /*
  259. * It might have happened that the HSI.FETCHED bit is
  260. * set. Retry in this case. Therefore I expect maximum 2 times
  261. * trying.
  262. */
  263. ddcb_mark_appended(pddcb);
  264. for (try = 0; try < 2; try++) {
  265. old = prev_ddcb->icrc_hsi_shi_32; /* read SHI/HSI in BE32 */
  266. /* try to append via NEXT bit if prev DDCB is not completed */
  267. if ((old & DDCB_COMPLETED_BE32) != 0x00000000)
  268. break;
  269. new = (old | DDCB_NEXT_BE32);
  270. wmb(); /* need to ensure write ordering */
  271. icrc_hsi_shi = cmpxchg(&prev_ddcb->icrc_hsi_shi_32, old, new);
  272. if (icrc_hsi_shi == old)
  273. return RET_DDCB_APPENDED; /* appended to queue */
  274. }
  275. /* Queue must be re-started by updating QUEUE_OFFSET */
  276. ddcb_mark_tapped(pddcb);
  277. num = (u64)ddcb_no << 8;
  278. wmb(); /* need to ensure write ordering */
  279. __genwqe_writeq(cd, queue->IO_QUEUE_OFFSET, num); /* start queue */
  280. return RET_DDCB_TAPPED;
  281. }
  282. /**
  283. * copy_ddcb_results() - Copy output state from real DDCB to request
  284. *
  285. * Copy DDCB ASV to request struct. There is no endian
  286. * conversion made, since data structure in ASV is still
  287. * unknown here.
  288. *
  289. * This is needed by:
  290. * - genwqe_purge_ddcb()
  291. * - genwqe_check_ddcb_queue()
  292. */
  293. static void copy_ddcb_results(struct ddcb_requ *req, int ddcb_no)
  294. {
  295. struct ddcb_queue *queue = req->queue;
  296. struct ddcb *pddcb = &queue->ddcb_vaddr[req->num];
  297. memcpy(&req->cmd.asv[0], &pddcb->asv[0], DDCB_ASV_LENGTH);
  298. /* copy status flags of the variant part */
  299. req->cmd.vcrc = be16_to_cpu(pddcb->vcrc_16);
  300. req->cmd.deque_ts = be64_to_cpu(pddcb->deque_ts_64);
  301. req->cmd.cmplt_ts = be64_to_cpu(pddcb->cmplt_ts_64);
  302. req->cmd.attn = be16_to_cpu(pddcb->attn_16);
  303. req->cmd.progress = be32_to_cpu(pddcb->progress_32);
  304. req->cmd.retc = be16_to_cpu(pddcb->retc_16);
  305. if (ddcb_requ_collect_debug_data(req)) {
  306. int prev_no = (ddcb_no == 0) ?
  307. queue->ddcb_max - 1 : ddcb_no - 1;
  308. struct ddcb *prev_pddcb = &queue->ddcb_vaddr[prev_no];
  309. memcpy(&req->debug_data.ddcb_finished, pddcb,
  310. sizeof(req->debug_data.ddcb_finished));
  311. memcpy(&req->debug_data.ddcb_prev, prev_pddcb,
  312. sizeof(req->debug_data.ddcb_prev));
  313. }
  314. }
  315. /**
  316. * genwqe_check_ddcb_queue() - Checks DDCB queue for completed work equests.
  317. * @cd: pointer to genwqe device descriptor
  318. *
  319. * Return: Number of DDCBs which were finished
  320. */
  321. static int genwqe_check_ddcb_queue(struct genwqe_dev *cd,
  322. struct ddcb_queue *queue)
  323. {
  324. unsigned long flags;
  325. int ddcbs_finished = 0;
  326. struct pci_dev *pci_dev = cd->pci_dev;
  327. spin_lock_irqsave(&queue->ddcb_lock, flags);
  328. /* FIXME avoid soft locking CPU */
  329. while (!queue_empty(queue) && (ddcbs_finished < queue->ddcb_max)) {
  330. struct ddcb *pddcb;
  331. struct ddcb_requ *req;
  332. u16 vcrc, vcrc_16, retc_16;
  333. pddcb = &queue->ddcb_vaddr[queue->ddcb_act];
  334. if ((pddcb->icrc_hsi_shi_32 & DDCB_COMPLETED_BE32) ==
  335. 0x00000000)
  336. goto go_home; /* not completed, continue waiting */
  337. wmb(); /* Add sync to decouple prev. read operations */
  338. /* Note: DDCB could be purged */
  339. req = queue->ddcb_req[queue->ddcb_act];
  340. if (req == NULL) {
  341. /* this occurs if DDCB is purged, not an error */
  342. /* Move active DDCB further; Nothing to do anymore. */
  343. goto pick_next_one;
  344. }
  345. /*
  346. * HSI=0x44 (fetched and completed), but RETC is
  347. * 0x101, or even worse 0x000.
  348. *
  349. * In case of seeing the queue in inconsistent state
  350. * we read the errcnts and the queue status to provide
  351. * a trigger for our PCIe analyzer stop capturing.
  352. */
  353. retc_16 = be16_to_cpu(pddcb->retc_16);
  354. if ((pddcb->hsi == 0x44) && (retc_16 <= 0x101)) {
  355. u64 errcnts, status;
  356. u64 ddcb_offs = (u64)pddcb - (u64)queue->ddcb_vaddr;
  357. errcnts = __genwqe_readq(cd, queue->IO_QUEUE_ERRCNTS);
  358. status = __genwqe_readq(cd, queue->IO_QUEUE_STATUS);
  359. dev_err(&pci_dev->dev,
  360. "[%s] SEQN=%04x HSI=%02x RETC=%03x Q_ERRCNTS=%016llx Q_STATUS=%016llx DDCB_DMA_ADDR=%016llx\n",
  361. __func__, be16_to_cpu(pddcb->seqnum_16),
  362. pddcb->hsi, retc_16, errcnts, status,
  363. queue->ddcb_daddr + ddcb_offs);
  364. }
  365. copy_ddcb_results(req, queue->ddcb_act);
  366. queue->ddcb_req[queue->ddcb_act] = NULL; /* take from queue */
  367. dev_dbg(&pci_dev->dev, "FINISHED DDCB#%d\n", req->num);
  368. genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
  369. ddcb_mark_finished(pddcb);
  370. /* calculate CRC_16 to see if VCRC is correct */
  371. vcrc = genwqe_crc16(pddcb->asv,
  372. VCRC_LENGTH(req->cmd.asv_length),
  373. 0xffff);
  374. vcrc_16 = be16_to_cpu(pddcb->vcrc_16);
  375. if (vcrc != vcrc_16) {
  376. printk_ratelimited(KERN_ERR
  377. "%s %s: err: wrong VCRC pre=%02x vcrc_len=%d bytes vcrc_data=%04x is not vcrc_card=%04x\n",
  378. GENWQE_DEVNAME, dev_name(&pci_dev->dev),
  379. pddcb->pre, VCRC_LENGTH(req->cmd.asv_length),
  380. vcrc, vcrc_16);
  381. }
  382. ddcb_requ_set_state(req, GENWQE_REQU_FINISHED);
  383. queue->ddcbs_completed++;
  384. queue->ddcbs_in_flight--;
  385. /* wake up process waiting for this DDCB, and
  386. processes on the busy queue */
  387. wake_up_interruptible(&queue->ddcb_waitqs[queue->ddcb_act]);
  388. wake_up_interruptible(&queue->busy_waitq);
  389. pick_next_one:
  390. queue->ddcb_act = (queue->ddcb_act + 1) % queue->ddcb_max;
  391. ddcbs_finished++;
  392. }
  393. go_home:
  394. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  395. return ddcbs_finished;
  396. }
  397. /**
  398. * __genwqe_wait_ddcb(): Waits until DDCB is completed
  399. * @cd: pointer to genwqe device descriptor
  400. * @req: pointer to requsted DDCB parameters
  401. *
  402. * The Service Layer will update the RETC in DDCB when processing is
  403. * pending or done.
  404. *
  405. * Return: > 0 remaining jiffies, DDCB completed
  406. * -ETIMEDOUT when timeout
  407. * -ERESTARTSYS when ^C
  408. * -EINVAL when unknown error condition
  409. *
  410. * When an error is returned the called needs to ensure that
  411. * purge_ddcb() is being called to get the &req removed from the
  412. * queue.
  413. */
  414. int __genwqe_wait_ddcb(struct genwqe_dev *cd, struct ddcb_requ *req)
  415. {
  416. int rc;
  417. unsigned int ddcb_no;
  418. struct ddcb_queue *queue;
  419. struct pci_dev *pci_dev = cd->pci_dev;
  420. if (req == NULL)
  421. return -EINVAL;
  422. queue = req->queue;
  423. if (queue == NULL)
  424. return -EINVAL;
  425. ddcb_no = req->num;
  426. if (ddcb_no >= queue->ddcb_max)
  427. return -EINVAL;
  428. rc = wait_event_interruptible_timeout(queue->ddcb_waitqs[ddcb_no],
  429. ddcb_requ_finished(cd, req),
  430. GENWQE_DDCB_SOFTWARE_TIMEOUT * HZ);
  431. /*
  432. * We need to distinguish 3 cases here:
  433. * 1. rc == 0 timeout occured
  434. * 2. rc == -ERESTARTSYS signal received
  435. * 3. rc > 0 remaining jiffies condition is true
  436. */
  437. if (rc == 0) {
  438. struct ddcb_queue *queue = req->queue;
  439. struct ddcb *pddcb;
  440. /*
  441. * Timeout may be caused by long task switching time.
  442. * When timeout happens, check if the request has
  443. * meanwhile completed.
  444. */
  445. genwqe_check_ddcb_queue(cd, req->queue);
  446. if (ddcb_requ_finished(cd, req))
  447. return rc;
  448. dev_err(&pci_dev->dev,
  449. "[%s] err: DDCB#%d timeout rc=%d state=%d req @ %p\n",
  450. __func__, req->num, rc, ddcb_requ_get_state(req),
  451. req);
  452. dev_err(&pci_dev->dev,
  453. "[%s] IO_QUEUE_STATUS=0x%016llx\n", __func__,
  454. __genwqe_readq(cd, queue->IO_QUEUE_STATUS));
  455. pddcb = &queue->ddcb_vaddr[req->num];
  456. genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
  457. print_ddcb_info(cd, req->queue);
  458. return -ETIMEDOUT;
  459. } else if (rc == -ERESTARTSYS) {
  460. return rc;
  461. /*
  462. * EINTR: Stops the application
  463. * ERESTARTSYS: Restartable systemcall; called again
  464. */
  465. } else if (rc < 0) {
  466. dev_err(&pci_dev->dev,
  467. "[%s] err: DDCB#%d unknown result (rc=%d) %d!\n",
  468. __func__, req->num, rc, ddcb_requ_get_state(req));
  469. return -EINVAL;
  470. }
  471. /* Severe error occured. Driver is forced to stop operation */
  472. if (cd->card_state != GENWQE_CARD_USED) {
  473. dev_err(&pci_dev->dev,
  474. "[%s] err: DDCB#%d forced to stop (rc=%d)\n",
  475. __func__, req->num, rc);
  476. return -EIO;
  477. }
  478. return rc;
  479. }
  480. /**
  481. * get_next_ddcb() - Get next available DDCB
  482. * @cd: pointer to genwqe device descriptor
  483. *
  484. * DDCB's content is completely cleared but presets for PRE and
  485. * SEQNUM. This function must only be called when ddcb_lock is held.
  486. *
  487. * Return: NULL if no empty DDCB available otherwise ptr to next DDCB.
  488. */
  489. static struct ddcb *get_next_ddcb(struct genwqe_dev *cd,
  490. struct ddcb_queue *queue,
  491. int *num)
  492. {
  493. u64 *pu64;
  494. struct ddcb *pddcb;
  495. if (queue_free_ddcbs(queue) == 0) /* queue is full */
  496. return NULL;
  497. /* find new ddcb */
  498. pddcb = &queue->ddcb_vaddr[queue->ddcb_next];
  499. /* if it is not completed, we are not allowed to use it */
  500. /* barrier(); */
  501. if ((pddcb->icrc_hsi_shi_32 & DDCB_COMPLETED_BE32) == 0x00000000)
  502. return NULL;
  503. *num = queue->ddcb_next; /* internal DDCB number */
  504. queue->ddcb_next = (queue->ddcb_next + 1) % queue->ddcb_max;
  505. /* clear important DDCB fields */
  506. pu64 = (u64 *)pddcb;
  507. pu64[0] = 0ULL; /* offs 0x00 (ICRC,HSI,SHI,...) */
  508. pu64[1] = 0ULL; /* offs 0x01 (ACFUNC,CMD...) */
  509. /* destroy previous results in ASV */
  510. pu64[0x80/8] = 0ULL; /* offs 0x80 (ASV + 0) */
  511. pu64[0x88/8] = 0ULL; /* offs 0x88 (ASV + 0x08) */
  512. pu64[0x90/8] = 0ULL; /* offs 0x90 (ASV + 0x10) */
  513. pu64[0x98/8] = 0ULL; /* offs 0x98 (ASV + 0x18) */
  514. pu64[0xd0/8] = 0ULL; /* offs 0xd0 (RETC,ATTN...) */
  515. pddcb->pre = DDCB_PRESET_PRE; /* 128 */
  516. pddcb->seqnum_16 = cpu_to_be16(queue->ddcb_seq++);
  517. return pddcb;
  518. }
  519. /**
  520. * __genwqe_purge_ddcb() - Remove a DDCB from the workqueue
  521. * @cd: genwqe device descriptor
  522. * @req: DDCB request
  523. *
  524. * This will fail when the request was already FETCHED. In this case
  525. * we need to wait until it is finished. Else the DDCB can be
  526. * reused. This function also ensures that the request data structure
  527. * is removed from ddcb_req[].
  528. *
  529. * Do not forget to call this function when genwqe_wait_ddcb() fails,
  530. * such that the request gets really removed from ddcb_req[].
  531. *
  532. * Return: 0 success
  533. */
  534. int __genwqe_purge_ddcb(struct genwqe_dev *cd, struct ddcb_requ *req)
  535. {
  536. struct ddcb *pddcb = NULL;
  537. unsigned int t;
  538. unsigned long flags;
  539. struct ddcb_queue *queue = req->queue;
  540. struct pci_dev *pci_dev = cd->pci_dev;
  541. u64 queue_status;
  542. __be32 icrc_hsi_shi = 0x0000;
  543. __be32 old, new;
  544. /* unsigned long flags; */
  545. if (GENWQE_DDCB_SOFTWARE_TIMEOUT <= 0) {
  546. dev_err(&pci_dev->dev,
  547. "[%s] err: software timeout is not set!\n", __func__);
  548. return -EFAULT;
  549. }
  550. pddcb = &queue->ddcb_vaddr[req->num];
  551. for (t = 0; t < GENWQE_DDCB_SOFTWARE_TIMEOUT * 10; t++) {
  552. spin_lock_irqsave(&queue->ddcb_lock, flags);
  553. /* Check if req was meanwhile finished */
  554. if (ddcb_requ_get_state(req) == GENWQE_REQU_FINISHED)
  555. goto go_home;
  556. /* try to set PURGE bit if FETCHED/COMPLETED are not set */
  557. old = pddcb->icrc_hsi_shi_32; /* read SHI/HSI in BE32 */
  558. if ((old & DDCB_FETCHED_BE32) == 0x00000000) {
  559. new = (old | DDCB_PURGE_BE32);
  560. icrc_hsi_shi = cmpxchg(&pddcb->icrc_hsi_shi_32,
  561. old, new);
  562. if (icrc_hsi_shi == old)
  563. goto finish_ddcb;
  564. }
  565. /* normal finish with HSI bit */
  566. barrier();
  567. icrc_hsi_shi = pddcb->icrc_hsi_shi_32;
  568. if (icrc_hsi_shi & DDCB_COMPLETED_BE32)
  569. goto finish_ddcb;
  570. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  571. /*
  572. * Here the check_ddcb() function will most likely
  573. * discover this DDCB to be finished some point in
  574. * time. It will mark the req finished and free it up
  575. * in the list.
  576. */
  577. copy_ddcb_results(req, req->num); /* for the failing case */
  578. msleep(100); /* sleep for 1/10 second and try again */
  579. continue;
  580. finish_ddcb:
  581. copy_ddcb_results(req, req->num);
  582. ddcb_requ_set_state(req, GENWQE_REQU_FINISHED);
  583. queue->ddcbs_in_flight--;
  584. queue->ddcb_req[req->num] = NULL; /* delete from array */
  585. ddcb_mark_cleared(pddcb);
  586. /* Move active DDCB further; Nothing to do here anymore. */
  587. /*
  588. * We need to ensure that there is at least one free
  589. * DDCB in the queue. To do that, we must update
  590. * ddcb_act only if the COMPLETED bit is set for the
  591. * DDCB we are working on else we treat that DDCB even
  592. * if we PURGED it as occupied (hardware is supposed
  593. * to set the COMPLETED bit yet!).
  594. */
  595. icrc_hsi_shi = pddcb->icrc_hsi_shi_32;
  596. if ((icrc_hsi_shi & DDCB_COMPLETED_BE32) &&
  597. (queue->ddcb_act == req->num)) {
  598. queue->ddcb_act = ((queue->ddcb_act + 1) %
  599. queue->ddcb_max);
  600. }
  601. go_home:
  602. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  603. return 0;
  604. }
  605. /*
  606. * If the card is dead and the queue is forced to stop, we
  607. * might see this in the queue status register.
  608. */
  609. queue_status = __genwqe_readq(cd, queue->IO_QUEUE_STATUS);
  610. dev_dbg(&pci_dev->dev, "UN/FINISHED DDCB#%d\n", req->num);
  611. genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
  612. dev_err(&pci_dev->dev,
  613. "[%s] err: DDCB#%d not purged and not completed after %d seconds QSTAT=%016llx!!\n",
  614. __func__, req->num, GENWQE_DDCB_SOFTWARE_TIMEOUT,
  615. queue_status);
  616. print_ddcb_info(cd, req->queue);
  617. return -EFAULT;
  618. }
  619. int genwqe_init_debug_data(struct genwqe_dev *cd, struct genwqe_debug_data *d)
  620. {
  621. int len;
  622. struct pci_dev *pci_dev = cd->pci_dev;
  623. if (d == NULL) {
  624. dev_err(&pci_dev->dev,
  625. "[%s] err: invalid memory for debug data!\n",
  626. __func__);
  627. return -EFAULT;
  628. }
  629. len = sizeof(d->driver_version);
  630. snprintf(d->driver_version, len, "%s", DRV_VERSION);
  631. d->slu_unitcfg = cd->slu_unitcfg;
  632. d->app_unitcfg = cd->app_unitcfg;
  633. return 0;
  634. }
  635. /**
  636. * __genwqe_enqueue_ddcb() - Enqueue a DDCB
  637. * @cd: pointer to genwqe device descriptor
  638. * @req: pointer to DDCB execution request
  639. * @f_flags: file mode: blocking, non-blocking
  640. *
  641. * Return: 0 if enqueuing succeeded
  642. * -EIO if card is unusable/PCIe problems
  643. * -EBUSY if enqueuing failed
  644. */
  645. int __genwqe_enqueue_ddcb(struct genwqe_dev *cd, struct ddcb_requ *req,
  646. unsigned int f_flags)
  647. {
  648. struct ddcb *pddcb;
  649. unsigned long flags;
  650. struct ddcb_queue *queue;
  651. struct pci_dev *pci_dev = cd->pci_dev;
  652. u16 icrc;
  653. retry:
  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. int rc;
  676. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  677. if (f_flags & O_NONBLOCK) {
  678. queue->return_on_busy++;
  679. return -EBUSY;
  680. }
  681. queue->wait_on_busy++;
  682. rc = wait_event_interruptible(queue->busy_waitq,
  683. queue_free_ddcbs(queue) != 0);
  684. dev_dbg(&pci_dev->dev, "[%s] waiting for free DDCB: rc=%d\n",
  685. __func__, rc);
  686. if (rc == -ERESTARTSYS)
  687. return rc; /* interrupted by a signal */
  688. goto retry;
  689. }
  690. if (queue->ddcb_req[req->num] != NULL) {
  691. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  692. dev_err(&pci_dev->dev,
  693. "[%s] picked DDCB %d with req=%p still in use!!\n",
  694. __func__, req->num, req);
  695. return -EFAULT;
  696. }
  697. ddcb_requ_set_state(req, GENWQE_REQU_ENQUEUED);
  698. queue->ddcb_req[req->num] = req;
  699. pddcb->cmdopts_16 = cpu_to_be16(req->cmd.cmdopts);
  700. pddcb->cmd = req->cmd.cmd;
  701. pddcb->acfunc = req->cmd.acfunc; /* functional unit */
  702. /*
  703. * We know that we can get retc 0x104 with CRC error, do not
  704. * stop the queue in those cases for this command. XDIR = 1
  705. * does not work for old SLU versions.
  706. *
  707. * Last bitstream with the old XDIR behavior had SLU_ID
  708. * 0x34199.
  709. */
  710. if ((cd->slu_unitcfg & 0xFFFF0ull) > 0x34199ull)
  711. pddcb->xdir = 0x1;
  712. else
  713. pddcb->xdir = 0x0;
  714. pddcb->psp = (((req->cmd.asiv_length / 8) << 4) |
  715. ((req->cmd.asv_length / 8)));
  716. pddcb->disp_ts_64 = cpu_to_be64(req->cmd.disp_ts);
  717. /*
  718. * If copying the whole DDCB_ASIV_LENGTH is impacting
  719. * performance we need to change it to
  720. * req->cmd.asiv_length. But simulation benefits from some
  721. * non-architectured bits behind the architectured content.
  722. *
  723. * How much data is copied depends on the availability of the
  724. * ATS field, which was introduced late. If the ATS field is
  725. * supported ASIV is 8 bytes shorter than it used to be. Since
  726. * the ATS field is copied too, the code should do exactly
  727. * what it did before, but I wanted to make copying of the ATS
  728. * field very explicit.
  729. */
  730. if (genwqe_get_slu_id(cd) <= 0x2) {
  731. memcpy(&pddcb->__asiv[0], /* destination */
  732. &req->cmd.__asiv[0], /* source */
  733. DDCB_ASIV_LENGTH); /* req->cmd.asiv_length */
  734. } else {
  735. pddcb->n.ats_64 = cpu_to_be64(req->cmd.ats);
  736. memcpy(&pddcb->n.asiv[0], /* destination */
  737. &req->cmd.asiv[0], /* source */
  738. DDCB_ASIV_LENGTH_ATS); /* req->cmd.asiv_length */
  739. }
  740. pddcb->icrc_hsi_shi_32 = cpu_to_be32(0x00000000); /* for crc */
  741. /*
  742. * Calculate CRC_16 for corresponding range PSP(7:4). Include
  743. * empty 4 bytes prior to the data.
  744. */
  745. icrc = genwqe_crc16((const u8 *)pddcb,
  746. ICRC_LENGTH(req->cmd.asiv_length), 0xffff);
  747. pddcb->icrc_hsi_shi_32 = cpu_to_be32((u32)icrc << 16);
  748. /* enable DDCB completion irq */
  749. if (!GENWQE_POLLING_ENABLED)
  750. pddcb->icrc_hsi_shi_32 |= DDCB_INTR_BE32;
  751. dev_dbg(&pci_dev->dev, "INPUT DDCB#%d\n", req->num);
  752. genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
  753. if (ddcb_requ_collect_debug_data(req)) {
  754. /* use the kernel copy of debug data. copying back to
  755. user buffer happens later */
  756. genwqe_init_debug_data(cd, &req->debug_data);
  757. memcpy(&req->debug_data.ddcb_before, pddcb,
  758. sizeof(req->debug_data.ddcb_before));
  759. }
  760. enqueue_ddcb(cd, queue, pddcb, req->num);
  761. queue->ddcbs_in_flight++;
  762. if (queue->ddcbs_in_flight > queue->ddcbs_max_in_flight)
  763. queue->ddcbs_max_in_flight = queue->ddcbs_in_flight;
  764. ddcb_requ_set_state(req, GENWQE_REQU_TAPPED);
  765. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  766. wake_up_interruptible(&cd->queue_waitq);
  767. return 0;
  768. }
  769. /**
  770. * __genwqe_execute_raw_ddcb() - Setup and execute DDCB
  771. * @cd: pointer to genwqe device descriptor
  772. * @req: user provided DDCB request
  773. * @f_flags: file mode: blocking, non-blocking
  774. */
  775. int __genwqe_execute_raw_ddcb(struct genwqe_dev *cd,
  776. struct genwqe_ddcb_cmd *cmd,
  777. unsigned int f_flags)
  778. {
  779. int rc = 0;
  780. struct pci_dev *pci_dev = cd->pci_dev;
  781. struct ddcb_requ *req = container_of(cmd, struct ddcb_requ, cmd);
  782. if (cmd->asiv_length > DDCB_ASIV_LENGTH) {
  783. dev_err(&pci_dev->dev, "[%s] err: wrong asiv_length of %d\n",
  784. __func__, cmd->asiv_length);
  785. return -EINVAL;
  786. }
  787. if (cmd->asv_length > DDCB_ASV_LENGTH) {
  788. dev_err(&pci_dev->dev, "[%s] err: wrong asv_length of %d\n",
  789. __func__, cmd->asiv_length);
  790. return -EINVAL;
  791. }
  792. rc = __genwqe_enqueue_ddcb(cd, req, f_flags);
  793. if (rc != 0)
  794. return rc;
  795. rc = __genwqe_wait_ddcb(cd, req);
  796. if (rc < 0) /* error or signal interrupt */
  797. goto err_exit;
  798. if (ddcb_requ_collect_debug_data(req)) {
  799. if (copy_to_user((struct genwqe_debug_data __user *)
  800. (unsigned long)cmd->ddata_addr,
  801. &req->debug_data,
  802. sizeof(struct genwqe_debug_data)))
  803. return -EFAULT;
  804. }
  805. /*
  806. * Higher values than 0x102 indicate completion with faults,
  807. * lower values than 0x102 indicate processing faults. Note
  808. * that DDCB might have been purged. E.g. Cntl+C.
  809. */
  810. if (cmd->retc != DDCB_RETC_COMPLETE) {
  811. /* This might happen e.g. flash read, and needs to be
  812. handled by the upper layer code. */
  813. rc = -EBADMSG; /* not processed/error retc */
  814. }
  815. return rc;
  816. err_exit:
  817. __genwqe_purge_ddcb(cd, req);
  818. if (ddcb_requ_collect_debug_data(req)) {
  819. if (copy_to_user((struct genwqe_debug_data __user *)
  820. (unsigned long)cmd->ddata_addr,
  821. &req->debug_data,
  822. sizeof(struct genwqe_debug_data)))
  823. return -EFAULT;
  824. }
  825. return rc;
  826. }
  827. /**
  828. * genwqe_next_ddcb_ready() - Figure out if the next DDCB is already finished
  829. *
  830. * We use this as condition for our wait-queue code.
  831. */
  832. static int genwqe_next_ddcb_ready(struct genwqe_dev *cd)
  833. {
  834. unsigned long flags;
  835. struct ddcb *pddcb;
  836. struct ddcb_queue *queue = &cd->queue;
  837. spin_lock_irqsave(&queue->ddcb_lock, flags);
  838. if (queue_empty(queue)) { /* emtpy queue */
  839. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  840. return 0;
  841. }
  842. pddcb = &queue->ddcb_vaddr[queue->ddcb_act];
  843. if (pddcb->icrc_hsi_shi_32 & DDCB_COMPLETED_BE32) { /* ddcb ready */
  844. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  845. return 1;
  846. }
  847. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  848. return 0;
  849. }
  850. /**
  851. * genwqe_ddcbs_in_flight() - Check how many DDCBs are in flight
  852. *
  853. * Keep track on the number of DDCBs which ware currently in the
  854. * queue. This is needed for statistics as well as conditon if we want
  855. * to wait or better do polling in case of no interrupts available.
  856. */
  857. int genwqe_ddcbs_in_flight(struct genwqe_dev *cd)
  858. {
  859. unsigned long flags;
  860. int ddcbs_in_flight = 0;
  861. struct ddcb_queue *queue = &cd->queue;
  862. spin_lock_irqsave(&queue->ddcb_lock, flags);
  863. ddcbs_in_flight += queue->ddcbs_in_flight;
  864. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  865. return ddcbs_in_flight;
  866. }
  867. static int setup_ddcb_queue(struct genwqe_dev *cd, struct ddcb_queue *queue)
  868. {
  869. int rc, i;
  870. struct ddcb *pddcb;
  871. u64 val64;
  872. unsigned int queue_size;
  873. struct pci_dev *pci_dev = cd->pci_dev;
  874. if (GENWQE_DDCB_MAX < 2)
  875. return -EINVAL;
  876. queue_size = roundup(GENWQE_DDCB_MAX * sizeof(struct ddcb), PAGE_SIZE);
  877. queue->ddcbs_in_flight = 0; /* statistics */
  878. queue->ddcbs_max_in_flight = 0;
  879. queue->ddcbs_completed = 0;
  880. queue->return_on_busy = 0;
  881. queue->wait_on_busy = 0;
  882. queue->ddcb_seq = 0x100; /* start sequence number */
  883. queue->ddcb_max = GENWQE_DDCB_MAX;
  884. queue->ddcb_vaddr = __genwqe_alloc_consistent(cd, queue_size,
  885. &queue->ddcb_daddr);
  886. if (queue->ddcb_vaddr == NULL) {
  887. dev_err(&pci_dev->dev,
  888. "[%s] **err: could not allocate DDCB **\n", __func__);
  889. return -ENOMEM;
  890. }
  891. queue->ddcb_req = kcalloc(queue->ddcb_max, sizeof(struct ddcb_requ *),
  892. GFP_KERNEL);
  893. if (!queue->ddcb_req) {
  894. rc = -ENOMEM;
  895. goto free_ddcbs;
  896. }
  897. queue->ddcb_waitqs = kcalloc(queue->ddcb_max,
  898. sizeof(wait_queue_head_t),
  899. 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. }