card_ddcb.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. /**
  2. * IBM Accelerator Family 'GenWQE'
  3. *
  4. * (C) Copyright IBM Corp. 2013
  5. *
  6. * Author: Frank Haverkamp <haver@linux.vnet.ibm.com>
  7. * Author: Joerg-Stephan Vogt <jsvogt@de.ibm.com>
  8. * Author: Michael Jung <mijung@de.ibm.com>
  9. * Author: Michael Ruettger <michael@ibmra.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License (version 2 only)
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. /*
  21. * Device Driver Control Block (DDCB) queue support. Definition of
  22. * interrupt handlers for queue support as well as triggering the
  23. * health monitor code in case of problems. The current hardware uses
  24. * an MSI interrupt which is shared between error handling and
  25. * functional code.
  26. */
  27. #include <linux/types.h>
  28. #include <linux/module.h>
  29. #include <linux/sched.h>
  30. #include <linux/wait.h>
  31. #include <linux/pci.h>
  32. #include <linux/string.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/delay.h>
  35. #include <linux/module.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/crc-itu-t.h>
  38. #include "card_base.h"
  39. #include "card_ddcb.h"
  40. /*
  41. * N: next DDCB, this is where the next DDCB will be put.
  42. * A: active DDCB, this is where the code will look for the next completion.
  43. * x: DDCB is enqueued, we are waiting for its completion.
  44. * Situation (1): Empty queue
  45. * +---+---+---+---+---+---+---+---+
  46. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  47. * | | | | | | | | |
  48. * +---+---+---+---+---+---+---+---+
  49. * A/N
  50. * enqueued_ddcbs = A - N = 2 - 2 = 0
  51. *
  52. * Situation (2): Wrapped, N > A
  53. * +---+---+---+---+---+---+---+---+
  54. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  55. * | | | x | x | | | | |
  56. * +---+---+---+---+---+---+---+---+
  57. * A N
  58. * enqueued_ddcbs = N - A = 4 - 2 = 2
  59. *
  60. * Situation (3): Queue wrapped, A > N
  61. * +---+---+---+---+---+---+---+---+
  62. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  63. * | x | x | | | x | x | x | x |
  64. * +---+---+---+---+---+---+---+---+
  65. * N A
  66. * enqueued_ddcbs = queue_max - (A - N) = 8 - (4 - 2) = 6
  67. *
  68. * Situation (4a): Queue full N > A
  69. * +---+---+---+---+---+---+---+---+
  70. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  71. * | x | x | x | x | x | x | x | |
  72. * +---+---+---+---+---+---+---+---+
  73. * A N
  74. *
  75. * enqueued_ddcbs = N - A = 7 - 0 = 7
  76. *
  77. * Situation (4a): Queue full A > N
  78. * +---+---+---+---+---+---+---+---+
  79. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  80. * | x | x | x | | x | x | x | x |
  81. * +---+---+---+---+---+---+---+---+
  82. * N A
  83. * enqueued_ddcbs = queue_max - (A - N) = 8 - (4 - 3) = 7
  84. */
  85. static int queue_empty(struct ddcb_queue *queue)
  86. {
  87. return queue->ddcb_next == queue->ddcb_act;
  88. }
  89. static int queue_enqueued_ddcbs(struct ddcb_queue *queue)
  90. {
  91. if (queue->ddcb_next >= queue->ddcb_act)
  92. return queue->ddcb_next - queue->ddcb_act;
  93. return queue->ddcb_max - (queue->ddcb_act - queue->ddcb_next);
  94. }
  95. static int queue_free_ddcbs(struct ddcb_queue *queue)
  96. {
  97. int free_ddcbs = queue->ddcb_max - queue_enqueued_ddcbs(queue) - 1;
  98. if (WARN_ON_ONCE(free_ddcbs < 0)) { /* must never ever happen! */
  99. return 0;
  100. }
  101. return free_ddcbs;
  102. }
  103. /*
  104. * Use of the PRIV field in the DDCB for queue debugging:
  105. *
  106. * (1) Trying to get rid of a DDCB which saw a timeout:
  107. * pddcb->priv[6] = 0xcc; # cleared
  108. *
  109. * (2) Append a DDCB via NEXT bit:
  110. * pddcb->priv[7] = 0xaa; # appended
  111. *
  112. * (3) DDCB needed tapping:
  113. * pddcb->priv[7] = 0xbb; # tapped
  114. *
  115. * (4) DDCB marked as correctly finished:
  116. * pddcb->priv[6] = 0xff; # finished
  117. */
  118. static inline void ddcb_mark_tapped(struct ddcb *pddcb)
  119. {
  120. pddcb->priv[7] = 0xbb; /* tapped */
  121. }
  122. static inline void ddcb_mark_appended(struct ddcb *pddcb)
  123. {
  124. pddcb->priv[7] = 0xaa; /* appended */
  125. }
  126. static inline void ddcb_mark_cleared(struct ddcb *pddcb)
  127. {
  128. pddcb->priv[6] = 0xcc; /* cleared */
  129. }
  130. static inline void ddcb_mark_finished(struct ddcb *pddcb)
  131. {
  132. pddcb->priv[6] = 0xff; /* finished */
  133. }
  134. static inline void ddcb_mark_unused(struct ddcb *pddcb)
  135. {
  136. pddcb->priv_64 = cpu_to_be64(0); /* not tapped */
  137. }
  138. /**
  139. * genwqe_crc16() - Generate 16-bit crc as required for DDCBs
  140. * @buff: pointer to data buffer
  141. * @len: length of data for calculation
  142. * @init: initial crc (0xffff at start)
  143. *
  144. * Polynomial = x^16 + x^12 + x^5 + 1 (0x1021)
  145. * Example: 4 bytes 0x01 0x02 0x03 0x04 with init = 0xffff
  146. * should result in a crc16 of 0x89c3
  147. *
  148. * Return: crc16 checksum in big endian format !
  149. */
  150. static inline u16 genwqe_crc16(const u8 *buff, size_t len, u16 init)
  151. {
  152. return crc_itu_t(init, buff, len);
  153. }
  154. static void print_ddcb_info(struct genwqe_dev *cd, struct ddcb_queue *queue)
  155. {
  156. int i;
  157. struct ddcb *pddcb;
  158. unsigned long flags;
  159. struct pci_dev *pci_dev = cd->pci_dev;
  160. spin_lock_irqsave(&cd->print_lock, flags);
  161. dev_info(&pci_dev->dev,
  162. "DDCB list for card #%d (ddcb_act=%d / ddcb_next=%d):\n",
  163. cd->card_idx, queue->ddcb_act, queue->ddcb_next);
  164. pddcb = queue->ddcb_vaddr;
  165. for (i = 0; i < queue->ddcb_max; i++) {
  166. dev_err(&pci_dev->dev,
  167. " %c %-3d: RETC=%03x SEQ=%04x "
  168. "HSI=%02X SHI=%02x PRIV=%06llx CMD=%03x\n",
  169. i == queue->ddcb_act ? '>' : ' ',
  170. i,
  171. be16_to_cpu(pddcb->retc_16),
  172. be16_to_cpu(pddcb->seqnum_16),
  173. pddcb->hsi,
  174. pddcb->shi,
  175. be64_to_cpu(pddcb->priv_64),
  176. pddcb->cmd);
  177. pddcb++;
  178. }
  179. spin_unlock_irqrestore(&cd->print_lock, flags);
  180. }
  181. struct genwqe_ddcb_cmd *ddcb_requ_alloc(void)
  182. {
  183. struct ddcb_requ *req;
  184. req = kzalloc(sizeof(*req), GFP_ATOMIC);
  185. if (!req)
  186. return NULL;
  187. return &req->cmd;
  188. }
  189. void ddcb_requ_free(struct genwqe_ddcb_cmd *cmd)
  190. {
  191. struct ddcb_requ *req = container_of(cmd, struct ddcb_requ, cmd);
  192. kfree(req);
  193. }
  194. static inline enum genwqe_requ_state ddcb_requ_get_state(struct ddcb_requ *req)
  195. {
  196. return req->req_state;
  197. }
  198. static inline void ddcb_requ_set_state(struct ddcb_requ *req,
  199. enum genwqe_requ_state new_state)
  200. {
  201. req->req_state = new_state;
  202. }
  203. static inline int ddcb_requ_collect_debug_data(struct ddcb_requ *req)
  204. {
  205. return req->cmd.ddata_addr != 0x0;
  206. }
  207. /**
  208. * ddcb_requ_finished() - Returns the hardware state of the associated DDCB
  209. * @cd: pointer to genwqe device descriptor
  210. * @req: DDCB work request
  211. *
  212. * Status of ddcb_requ mirrors this hardware state, but is copied in
  213. * the ddcb_requ on interrupt/polling function. The lowlevel code
  214. * should check the hardware state directly, the higher level code
  215. * should check the copy.
  216. *
  217. * This function will also return true if the state of the queue is
  218. * not GENWQE_CARD_USED. This enables us to purge all DDCBs in the
  219. * shutdown case.
  220. */
  221. static int ddcb_requ_finished(struct genwqe_dev *cd, struct ddcb_requ *req)
  222. {
  223. return (ddcb_requ_get_state(req) == GENWQE_REQU_FINISHED) ||
  224. (cd->card_state != GENWQE_CARD_USED);
  225. }
  226. /**
  227. * enqueue_ddcb() - Enqueue a DDCB
  228. * @cd: pointer to genwqe device descriptor
  229. * @queue: queue this operation should be done on
  230. * @ddcb_no: pointer to ddcb number being tapped
  231. *
  232. * Start execution of DDCB by tapping or append to queue via NEXT
  233. * bit. This is done by an atomic 'compare and swap' instruction and
  234. * checking SHI and HSI of the previous DDCB.
  235. *
  236. * This function must only be called with ddcb_lock held.
  237. *
  238. * Return: 1 if new DDCB is appended to previous
  239. * 2 if DDCB queue is tapped via register/simulation
  240. */
  241. #define RET_DDCB_APPENDED 1
  242. #define RET_DDCB_TAPPED 2
  243. static int enqueue_ddcb(struct genwqe_dev *cd, struct ddcb_queue *queue,
  244. struct ddcb *pddcb, int ddcb_no)
  245. {
  246. unsigned int try;
  247. int prev_no;
  248. struct ddcb *prev_ddcb;
  249. __be32 old, new, icrc_hsi_shi;
  250. u64 num;
  251. /*
  252. * For performance checks a Dispatch Timestamp can be put into
  253. * DDCB It is supposed to use the SLU's free running counter,
  254. * but this requires PCIe cycles.
  255. */
  256. ddcb_mark_unused(pddcb);
  257. /* check previous DDCB if already fetched */
  258. prev_no = (ddcb_no == 0) ? queue->ddcb_max - 1 : ddcb_no - 1;
  259. prev_ddcb = &queue->ddcb_vaddr[prev_no];
  260. /*
  261. * It might have happened that the HSI.FETCHED bit is
  262. * set. Retry in this case. Therefore I expect maximum 2 times
  263. * trying.
  264. */
  265. ddcb_mark_appended(pddcb);
  266. for (try = 0; try < 2; try++) {
  267. old = prev_ddcb->icrc_hsi_shi_32; /* read SHI/HSI in BE32 */
  268. /* try to append via NEXT bit if prev DDCB is not completed */
  269. if ((old & DDCB_COMPLETED_BE32) != 0x00000000)
  270. break;
  271. new = (old | DDCB_NEXT_BE32);
  272. 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. __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. /* Note: DDCB could be purged */
  338. req = queue->ddcb_req[queue->ddcb_act];
  339. if (req == NULL) {
  340. /* this occurs if DDCB is purged, not an error */
  341. /* Move active DDCB further; Nothing to do anymore. */
  342. goto pick_next_one;
  343. }
  344. /*
  345. * HSI=0x44 (fetched and completed), but RETC is
  346. * 0x101, or even worse 0x000.
  347. *
  348. * In case of seeing the queue in inconsistent state
  349. * we read the errcnts and the queue status to provide
  350. * a trigger for our PCIe analyzer stop capturing.
  351. */
  352. retc_16 = be16_to_cpu(pddcb->retc_16);
  353. if ((pddcb->hsi == 0x44) && (retc_16 <= 0x101)) {
  354. u64 errcnts, status;
  355. u64 ddcb_offs = (u64)pddcb - (u64)queue->ddcb_vaddr;
  356. errcnts = __genwqe_readq(cd, queue->IO_QUEUE_ERRCNTS);
  357. status = __genwqe_readq(cd, queue->IO_QUEUE_STATUS);
  358. dev_err(&pci_dev->dev,
  359. "[%s] SEQN=%04x HSI=%02x RETC=%03x "
  360. " Q_ERRCNTS=%016llx Q_STATUS=%016llx\n"
  361. " 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 "
  379. "bytes vcrc_data=%04x is not vcrc_card=%04x\n",
  380. GENWQE_DEVNAME, dev_name(&pci_dev->dev),
  381. pddcb->pre, VCRC_LENGTH(req->cmd.asv_length),
  382. vcrc, vcrc_16);
  383. }
  384. ddcb_requ_set_state(req, GENWQE_REQU_FINISHED);
  385. queue->ddcbs_completed++;
  386. queue->ddcbs_in_flight--;
  387. /* wake up process waiting for this DDCB */
  388. wake_up_interruptible(&queue->ddcb_waitqs[queue->ddcb_act]);
  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 "
  614. "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_VERS_STRING);
  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. *
  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. {
  647. struct ddcb *pddcb;
  648. unsigned long flags;
  649. struct ddcb_queue *queue;
  650. struct pci_dev *pci_dev = cd->pci_dev;
  651. u16 icrc;
  652. if (cd->card_state != GENWQE_CARD_USED) {
  653. printk_ratelimited(KERN_ERR
  654. "%s %s: [%s] Card is unusable/PCIe problem Req#%d\n",
  655. GENWQE_DEVNAME, dev_name(&pci_dev->dev),
  656. __func__, req->num);
  657. return -EIO;
  658. }
  659. queue = req->queue = &cd->queue;
  660. /* FIXME circumvention to improve performance when no irq is
  661. * there.
  662. */
  663. if (genwqe_polling_enabled)
  664. genwqe_check_ddcb_queue(cd, queue);
  665. /*
  666. * It must be ensured to process all DDCBs in successive
  667. * order. Use a lock here in order to prevent nested DDCB
  668. * enqueuing.
  669. */
  670. spin_lock_irqsave(&queue->ddcb_lock, flags);
  671. pddcb = get_next_ddcb(cd, queue, &req->num); /* get ptr and num */
  672. if (pddcb == NULL) {
  673. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  674. queue->busy++;
  675. return -EBUSY;
  676. }
  677. if (queue->ddcb_req[req->num] != NULL) {
  678. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  679. dev_err(&pci_dev->dev,
  680. "[%s] picked DDCB %d with req=%p still in use!!\n",
  681. __func__, req->num, req);
  682. return -EFAULT;
  683. }
  684. ddcb_requ_set_state(req, GENWQE_REQU_ENQUEUED);
  685. queue->ddcb_req[req->num] = req;
  686. pddcb->cmdopts_16 = cpu_to_be16(req->cmd.cmdopts);
  687. pddcb->cmd = req->cmd.cmd;
  688. pddcb->acfunc = req->cmd.acfunc; /* functional unit */
  689. /*
  690. * We know that we can get retc 0x104 with CRC error, do not
  691. * stop the queue in those cases for this command. XDIR = 1
  692. * does not work for old SLU versions.
  693. *
  694. * Last bitstream with the old XDIR behavior had SLU_ID
  695. * 0x34199.
  696. */
  697. if ((cd->slu_unitcfg & 0xFFFF0ull) > 0x34199ull)
  698. pddcb->xdir = 0x1;
  699. else
  700. pddcb->xdir = 0x0;
  701. pddcb->psp = (((req->cmd.asiv_length / 8) << 4) |
  702. ((req->cmd.asv_length / 8)));
  703. pddcb->disp_ts_64 = cpu_to_be64(req->cmd.disp_ts);
  704. /*
  705. * If copying the whole DDCB_ASIV_LENGTH is impacting
  706. * performance we need to change it to
  707. * req->cmd.asiv_length. But simulation benefits from some
  708. * non-architectured bits behind the architectured content.
  709. *
  710. * How much data is copied depends on the availability of the
  711. * ATS field, which was introduced late. If the ATS field is
  712. * supported ASIV is 8 bytes shorter than it used to be. Since
  713. * the ATS field is copied too, the code should do exactly
  714. * what it did before, but I wanted to make copying of the ATS
  715. * field very explicit.
  716. */
  717. if (genwqe_get_slu_id(cd) <= 0x2) {
  718. memcpy(&pddcb->__asiv[0], /* destination */
  719. &req->cmd.__asiv[0], /* source */
  720. DDCB_ASIV_LENGTH); /* req->cmd.asiv_length */
  721. } else {
  722. pddcb->n.ats_64 = cpu_to_be64(req->cmd.ats);
  723. memcpy(&pddcb->n.asiv[0], /* destination */
  724. &req->cmd.asiv[0], /* source */
  725. DDCB_ASIV_LENGTH_ATS); /* req->cmd.asiv_length */
  726. }
  727. pddcb->icrc_hsi_shi_32 = cpu_to_be32(0x00000000); /* for crc */
  728. /*
  729. * Calculate CRC_16 for corresponding range PSP(7:4). Include
  730. * empty 4 bytes prior to the data.
  731. */
  732. icrc = genwqe_crc16((const u8 *)pddcb,
  733. ICRC_LENGTH(req->cmd.asiv_length), 0xffff);
  734. pddcb->icrc_hsi_shi_32 = cpu_to_be32((u32)icrc << 16);
  735. /* enable DDCB completion irq */
  736. if (!genwqe_polling_enabled)
  737. pddcb->icrc_hsi_shi_32 |= DDCB_INTR_BE32;
  738. dev_dbg(&pci_dev->dev, "INPUT DDCB#%d\n", req->num);
  739. genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
  740. if (ddcb_requ_collect_debug_data(req)) {
  741. /* use the kernel copy of debug data. copying back to
  742. user buffer happens later */
  743. genwqe_init_debug_data(cd, &req->debug_data);
  744. memcpy(&req->debug_data.ddcb_before, pddcb,
  745. sizeof(req->debug_data.ddcb_before));
  746. }
  747. enqueue_ddcb(cd, queue, pddcb, req->num);
  748. queue->ddcbs_in_flight++;
  749. if (queue->ddcbs_in_flight > queue->ddcbs_max_in_flight)
  750. queue->ddcbs_max_in_flight = queue->ddcbs_in_flight;
  751. ddcb_requ_set_state(req, GENWQE_REQU_TAPPED);
  752. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  753. wake_up_interruptible(&cd->queue_waitq);
  754. return 0;
  755. }
  756. /**
  757. * __genwqe_execute_raw_ddcb() - Setup and execute DDCB
  758. * @cd: pointer to genwqe device descriptor
  759. * @req: user provided DDCB request
  760. */
  761. int __genwqe_execute_raw_ddcb(struct genwqe_dev *cd,
  762. struct genwqe_ddcb_cmd *cmd)
  763. {
  764. int rc = 0;
  765. struct pci_dev *pci_dev = cd->pci_dev;
  766. struct ddcb_requ *req = container_of(cmd, struct ddcb_requ, cmd);
  767. if (cmd->asiv_length > DDCB_ASIV_LENGTH) {
  768. dev_err(&pci_dev->dev, "[%s] err: wrong asiv_length of %d\n",
  769. __func__, cmd->asiv_length);
  770. return -EINVAL;
  771. }
  772. if (cmd->asv_length > DDCB_ASV_LENGTH) {
  773. dev_err(&pci_dev->dev, "[%s] err: wrong asv_length of %d\n",
  774. __func__, cmd->asiv_length);
  775. return -EINVAL;
  776. }
  777. rc = __genwqe_enqueue_ddcb(cd, req);
  778. if (rc != 0)
  779. return rc;
  780. rc = __genwqe_wait_ddcb(cd, req);
  781. if (rc < 0) /* error or signal interrupt */
  782. goto err_exit;
  783. if (ddcb_requ_collect_debug_data(req)) {
  784. if (copy_to_user((struct genwqe_debug_data __user *)
  785. (unsigned long)cmd->ddata_addr,
  786. &req->debug_data,
  787. sizeof(struct genwqe_debug_data)))
  788. return -EFAULT;
  789. }
  790. /*
  791. * Higher values than 0x102 indicate completion with faults,
  792. * lower values than 0x102 indicate processing faults. Note
  793. * that DDCB might have been purged. E.g. Cntl+C.
  794. */
  795. if (cmd->retc != DDCB_RETC_COMPLETE) {
  796. /* This might happen e.g. flash read, and needs to be
  797. handled by the upper layer code. */
  798. rc = -EBADMSG; /* not processed/error retc */
  799. }
  800. return rc;
  801. err_exit:
  802. __genwqe_purge_ddcb(cd, req);
  803. if (ddcb_requ_collect_debug_data(req)) {
  804. if (copy_to_user((struct genwqe_debug_data __user *)
  805. (unsigned long)cmd->ddata_addr,
  806. &req->debug_data,
  807. sizeof(struct genwqe_debug_data)))
  808. return -EFAULT;
  809. }
  810. return rc;
  811. }
  812. /**
  813. * genwqe_next_ddcb_ready() - Figure out if the next DDCB is already finished
  814. *
  815. * We use this as condition for our wait-queue code.
  816. */
  817. static int genwqe_next_ddcb_ready(struct genwqe_dev *cd)
  818. {
  819. unsigned long flags;
  820. struct ddcb *pddcb;
  821. struct ddcb_queue *queue = &cd->queue;
  822. spin_lock_irqsave(&queue->ddcb_lock, flags);
  823. if (queue_empty(queue)) { /* emtpy queue */
  824. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  825. return 0;
  826. }
  827. pddcb = &queue->ddcb_vaddr[queue->ddcb_act];
  828. if (pddcb->icrc_hsi_shi_32 & DDCB_COMPLETED_BE32) { /* ddcb ready */
  829. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  830. return 1;
  831. }
  832. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  833. return 0;
  834. }
  835. /**
  836. * genwqe_ddcbs_in_flight() - Check how many DDCBs are in flight
  837. *
  838. * Keep track on the number of DDCBs which ware currently in the
  839. * queue. This is needed for statistics as well as conditon if we want
  840. * to wait or better do polling in case of no interrupts available.
  841. */
  842. int genwqe_ddcbs_in_flight(struct genwqe_dev *cd)
  843. {
  844. unsigned long flags;
  845. int ddcbs_in_flight = 0;
  846. struct ddcb_queue *queue = &cd->queue;
  847. spin_lock_irqsave(&queue->ddcb_lock, flags);
  848. ddcbs_in_flight += queue->ddcbs_in_flight;
  849. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  850. return ddcbs_in_flight;
  851. }
  852. static int setup_ddcb_queue(struct genwqe_dev *cd, struct ddcb_queue *queue)
  853. {
  854. int rc, i;
  855. struct ddcb *pddcb;
  856. u64 val64;
  857. unsigned int queue_size;
  858. struct pci_dev *pci_dev = cd->pci_dev;
  859. if (genwqe_ddcb_max < 2)
  860. return -EINVAL;
  861. queue_size = roundup(genwqe_ddcb_max * sizeof(struct ddcb), PAGE_SIZE);
  862. queue->ddcbs_in_flight = 0; /* statistics */
  863. queue->ddcbs_max_in_flight = 0;
  864. queue->ddcbs_completed = 0;
  865. queue->busy = 0;
  866. queue->ddcb_seq = 0x100; /* start sequence number */
  867. queue->ddcb_max = genwqe_ddcb_max; /* module parameter */
  868. queue->ddcb_vaddr = __genwqe_alloc_consistent(cd, queue_size,
  869. &queue->ddcb_daddr);
  870. if (queue->ddcb_vaddr == NULL) {
  871. dev_err(&pci_dev->dev,
  872. "[%s] **err: could not allocate DDCB **\n", __func__);
  873. return -ENOMEM;
  874. }
  875. memset(queue->ddcb_vaddr, 0, queue_size);
  876. queue->ddcb_req = kzalloc(sizeof(struct ddcb_requ *) *
  877. queue->ddcb_max, GFP_KERNEL);
  878. if (!queue->ddcb_req) {
  879. rc = -ENOMEM;
  880. goto free_ddcbs;
  881. }
  882. queue->ddcb_waitqs = kzalloc(sizeof(wait_queue_head_t) *
  883. queue->ddcb_max, GFP_KERNEL);
  884. if (!queue->ddcb_waitqs) {
  885. rc = -ENOMEM;
  886. goto free_requs;
  887. }
  888. for (i = 0; i < queue->ddcb_max; i++) {
  889. pddcb = &queue->ddcb_vaddr[i]; /* DDCBs */
  890. pddcb->icrc_hsi_shi_32 = DDCB_COMPLETED_BE32;
  891. pddcb->retc_16 = cpu_to_be16(0xfff);
  892. queue->ddcb_req[i] = NULL; /* requests */
  893. init_waitqueue_head(&queue->ddcb_waitqs[i]); /* waitqueues */
  894. }
  895. queue->ddcb_act = 0;
  896. queue->ddcb_next = 0; /* queue is empty */
  897. spin_lock_init(&queue->ddcb_lock);
  898. init_waitqueue_head(&queue->ddcb_waitq);
  899. val64 = ((u64)(queue->ddcb_max - 1) << 8); /* lastptr */
  900. __genwqe_writeq(cd, queue->IO_QUEUE_CONFIG, 0x07); /* iCRC/vCRC */
  901. __genwqe_writeq(cd, queue->IO_QUEUE_SEGMENT, queue->ddcb_daddr);
  902. __genwqe_writeq(cd, queue->IO_QUEUE_INITSQN, queue->ddcb_seq);
  903. __genwqe_writeq(cd, queue->IO_QUEUE_WRAP, val64);
  904. return 0;
  905. free_requs:
  906. kfree(queue->ddcb_req);
  907. queue->ddcb_req = NULL;
  908. free_ddcbs:
  909. __genwqe_free_consistent(cd, queue_size, queue->ddcb_vaddr,
  910. queue->ddcb_daddr);
  911. queue->ddcb_vaddr = NULL;
  912. queue->ddcb_daddr = 0ull;
  913. return -ENODEV;
  914. }
  915. static int ddcb_queue_initialized(struct ddcb_queue *queue)
  916. {
  917. return queue->ddcb_vaddr != NULL;
  918. }
  919. static void free_ddcb_queue(struct genwqe_dev *cd, struct ddcb_queue *queue)
  920. {
  921. unsigned int queue_size;
  922. queue_size = roundup(queue->ddcb_max * sizeof(struct ddcb), PAGE_SIZE);
  923. kfree(queue->ddcb_req);
  924. queue->ddcb_req = NULL;
  925. if (queue->ddcb_vaddr) {
  926. __genwqe_free_consistent(cd, queue_size, queue->ddcb_vaddr,
  927. queue->ddcb_daddr);
  928. queue->ddcb_vaddr = NULL;
  929. queue->ddcb_daddr = 0ull;
  930. }
  931. }
  932. static irqreturn_t genwqe_pf_isr(int irq, void *dev_id)
  933. {
  934. u64 gfir;
  935. struct genwqe_dev *cd = (struct genwqe_dev *)dev_id;
  936. struct pci_dev *pci_dev = cd->pci_dev;
  937. /*
  938. * In case of fatal FIR error the queue is stopped, such that
  939. * we can safely check it without risking anything.
  940. */
  941. cd->irqs_processed++;
  942. wake_up_interruptible(&cd->queue_waitq);
  943. /*
  944. * Checking for errors before kicking the queue might be
  945. * safer, but slower for the good-case ... See above.
  946. */
  947. gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
  948. if ((gfir & GFIR_ERR_TRIGGER) != 0x0) {
  949. wake_up_interruptible(&cd->health_waitq);
  950. /*
  951. * By default GFIRs causes recovery actions. This
  952. * count is just for debug when recovery is masked.
  953. */
  954. printk_ratelimited(KERN_ERR
  955. "%s %s: [%s] GFIR=%016llx\n",
  956. GENWQE_DEVNAME, dev_name(&pci_dev->dev),
  957. __func__, gfir);
  958. }
  959. return IRQ_HANDLED;
  960. }
  961. static irqreturn_t genwqe_vf_isr(int irq, void *dev_id)
  962. {
  963. struct genwqe_dev *cd = (struct genwqe_dev *)dev_id;
  964. cd->irqs_processed++;
  965. wake_up_interruptible(&cd->queue_waitq);
  966. return IRQ_HANDLED;
  967. }
  968. /**
  969. * genwqe_card_thread() - Work thread for the DDCB queue
  970. *
  971. * The idea is to check if there are DDCBs in processing. If there are
  972. * some finished DDCBs, we process them and wakeup the
  973. * requestors. Otherwise we give other processes time using
  974. * cond_resched().
  975. */
  976. static int genwqe_card_thread(void *data)
  977. {
  978. int should_stop = 0, rc = 0;
  979. struct genwqe_dev *cd = (struct genwqe_dev *)data;
  980. while (!kthread_should_stop()) {
  981. genwqe_check_ddcb_queue(cd, &cd->queue);
  982. if (genwqe_polling_enabled) {
  983. rc = wait_event_interruptible_timeout(
  984. cd->queue_waitq,
  985. genwqe_ddcbs_in_flight(cd) ||
  986. (should_stop = kthread_should_stop()), 1);
  987. } else {
  988. rc = wait_event_interruptible_timeout(
  989. cd->queue_waitq,
  990. genwqe_next_ddcb_ready(cd) ||
  991. (should_stop = kthread_should_stop()), HZ);
  992. }
  993. if (should_stop)
  994. break;
  995. /*
  996. * Avoid soft lockups on heavy loads; we do not want
  997. * to disable our interrupts.
  998. */
  999. cond_resched();
  1000. }
  1001. return 0;
  1002. }
  1003. /**
  1004. * genwqe_setup_service_layer() - Setup DDCB queue
  1005. * @cd: pointer to genwqe device descriptor
  1006. *
  1007. * Allocate DDCBs. Configure Service Layer Controller (SLC).
  1008. *
  1009. * Return: 0 success
  1010. */
  1011. int genwqe_setup_service_layer(struct genwqe_dev *cd)
  1012. {
  1013. int rc;
  1014. struct ddcb_queue *queue;
  1015. struct pci_dev *pci_dev = cd->pci_dev;
  1016. if (genwqe_is_privileged(cd)) {
  1017. rc = genwqe_card_reset(cd);
  1018. if (rc < 0) {
  1019. dev_err(&pci_dev->dev,
  1020. "[%s] err: reset failed.\n", __func__);
  1021. return rc;
  1022. }
  1023. genwqe_read_softreset(cd);
  1024. }
  1025. queue = &cd->queue;
  1026. queue->IO_QUEUE_CONFIG = IO_SLC_QUEUE_CONFIG;
  1027. queue->IO_QUEUE_STATUS = IO_SLC_QUEUE_STATUS;
  1028. queue->IO_QUEUE_SEGMENT = IO_SLC_QUEUE_SEGMENT;
  1029. queue->IO_QUEUE_INITSQN = IO_SLC_QUEUE_INITSQN;
  1030. queue->IO_QUEUE_OFFSET = IO_SLC_QUEUE_OFFSET;
  1031. queue->IO_QUEUE_WRAP = IO_SLC_QUEUE_WRAP;
  1032. queue->IO_QUEUE_WTIME = IO_SLC_QUEUE_WTIME;
  1033. queue->IO_QUEUE_ERRCNTS = IO_SLC_QUEUE_ERRCNTS;
  1034. queue->IO_QUEUE_LRW = IO_SLC_QUEUE_LRW;
  1035. rc = setup_ddcb_queue(cd, queue);
  1036. if (rc != 0) {
  1037. rc = -ENODEV;
  1038. goto err_out;
  1039. }
  1040. init_waitqueue_head(&cd->queue_waitq);
  1041. cd->card_thread = kthread_run(genwqe_card_thread, cd,
  1042. GENWQE_DEVNAME "%d_thread",
  1043. cd->card_idx);
  1044. if (IS_ERR(cd->card_thread)) {
  1045. rc = PTR_ERR(cd->card_thread);
  1046. cd->card_thread = NULL;
  1047. goto stop_free_queue;
  1048. }
  1049. rc = genwqe_set_interrupt_capability(cd, GENWQE_MSI_IRQS);
  1050. if (rc > 0)
  1051. rc = genwqe_set_interrupt_capability(cd, rc);
  1052. if (rc != 0) {
  1053. rc = -ENODEV;
  1054. goto stop_kthread;
  1055. }
  1056. /*
  1057. * We must have all wait-queues initialized when we enable the
  1058. * interrupts. Otherwise we might crash if we get an early
  1059. * irq.
  1060. */
  1061. init_waitqueue_head(&cd->health_waitq);
  1062. if (genwqe_is_privileged(cd)) {
  1063. rc = request_irq(pci_dev->irq, genwqe_pf_isr, IRQF_SHARED,
  1064. GENWQE_DEVNAME, cd);
  1065. } else {
  1066. rc = request_irq(pci_dev->irq, genwqe_vf_isr, IRQF_SHARED,
  1067. GENWQE_DEVNAME, cd);
  1068. }
  1069. if (rc < 0) {
  1070. dev_err(&pci_dev->dev, "irq %d not free.\n", pci_dev->irq);
  1071. goto stop_irq_cap;
  1072. }
  1073. cd->card_state = GENWQE_CARD_USED;
  1074. return 0;
  1075. stop_irq_cap:
  1076. genwqe_reset_interrupt_capability(cd);
  1077. stop_kthread:
  1078. kthread_stop(cd->card_thread);
  1079. cd->card_thread = NULL;
  1080. stop_free_queue:
  1081. free_ddcb_queue(cd, queue);
  1082. err_out:
  1083. return rc;
  1084. }
  1085. /**
  1086. * queue_wake_up_all() - Handles fatal error case
  1087. *
  1088. * The PCI device got unusable and we have to stop all pending
  1089. * requests as fast as we can. The code after this must purge the
  1090. * DDCBs in question and ensure that all mappings are freed.
  1091. */
  1092. static int queue_wake_up_all(struct genwqe_dev *cd)
  1093. {
  1094. unsigned int i;
  1095. unsigned long flags;
  1096. struct ddcb_queue *queue = &cd->queue;
  1097. spin_lock_irqsave(&queue->ddcb_lock, flags);
  1098. for (i = 0; i < queue->ddcb_max; i++)
  1099. wake_up_interruptible(&queue->ddcb_waitqs[queue->ddcb_act]);
  1100. spin_unlock_irqrestore(&queue->ddcb_lock, flags);
  1101. return 0;
  1102. }
  1103. /**
  1104. * genwqe_finish_queue() - Remove any genwqe devices and user-interfaces
  1105. *
  1106. * Relies on the pre-condition that there are no users of the card
  1107. * device anymore e.g. with open file-descriptors.
  1108. *
  1109. * This function must be robust enough to be called twice.
  1110. */
  1111. int genwqe_finish_queue(struct genwqe_dev *cd)
  1112. {
  1113. int i, rc, in_flight;
  1114. int waitmax = genwqe_ddcb_software_timeout;
  1115. struct pci_dev *pci_dev = cd->pci_dev;
  1116. struct ddcb_queue *queue = &cd->queue;
  1117. if (!ddcb_queue_initialized(queue))
  1118. return 0;
  1119. /* Do not wipe out the error state. */
  1120. if (cd->card_state == GENWQE_CARD_USED)
  1121. cd->card_state = GENWQE_CARD_UNUSED;
  1122. /* Wake up all requests in the DDCB queue such that they
  1123. should be removed nicely. */
  1124. queue_wake_up_all(cd);
  1125. /* We must wait to get rid of the DDCBs in flight */
  1126. for (i = 0; i < waitmax; i++) {
  1127. in_flight = genwqe_ddcbs_in_flight(cd);
  1128. if (in_flight == 0)
  1129. break;
  1130. dev_dbg(&pci_dev->dev,
  1131. " DEBUG [%d/%d] waiting for queue to get empty: "
  1132. "%d requests!\n", i, waitmax, in_flight);
  1133. /*
  1134. * Severe severe error situation: The card itself has
  1135. * 16 DDCB queues, each queue has e.g. 32 entries,
  1136. * each DDBC has a hardware timeout of currently 250
  1137. * msec but the PFs have a hardware timeout of 8 sec
  1138. * ... so I take something large.
  1139. */
  1140. msleep(1000);
  1141. }
  1142. if (i == waitmax) {
  1143. dev_err(&pci_dev->dev, " [%s] err: queue is not empty!!\n",
  1144. __func__);
  1145. rc = -EIO;
  1146. }
  1147. return rc;
  1148. }
  1149. /**
  1150. * genwqe_release_service_layer() - Shutdown DDCB queue
  1151. * @cd: genwqe device descriptor
  1152. *
  1153. * This function must be robust enough to be called twice.
  1154. */
  1155. int genwqe_release_service_layer(struct genwqe_dev *cd)
  1156. {
  1157. struct pci_dev *pci_dev = cd->pci_dev;
  1158. if (!ddcb_queue_initialized(&cd->queue))
  1159. return 1;
  1160. free_irq(pci_dev->irq, cd);
  1161. genwqe_reset_interrupt_capability(cd);
  1162. if (cd->card_thread != NULL) {
  1163. kthread_stop(cd->card_thread);
  1164. cd->card_thread = NULL;
  1165. }
  1166. free_ddcb_queue(cd, &cd->queue);
  1167. return 0;
  1168. }