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