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