blob: f105750001800f2a0011fcaac24aceaecac3fd4c [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
393 /* Note: DDCB could be purged */
394
395 req = queue->ddcb_req[queue->ddcb_act];
396 if (req == NULL) {
397 /* this occurs if DDCB is purged, not an error */
398 /* Move active DDCB further; Nothing to do anymore. */
399 goto pick_next_one;
400 }
401
402 /*
403 * HSI=0x44 (fetched and completed), but RETC is
404 * 0x101, or even worse 0x000.
405 *
406 * In case of seeing the queue in inconsistent state
407 * we read the errcnts and the queue status to provide
408 * a trigger for our PCIe analyzer stop capturing.
409 */
410 retc_16 = be16_to_cpu(pddcb->retc_16);
411 if ((pddcb->hsi == 0x44) && (retc_16 <= 0x101)) {
412 u64 errcnts, status;
413 u64 ddcb_offs = (u64)pddcb - (u64)queue->ddcb_vaddr;
414
415 errcnts = __genwqe_readq(cd, queue->IO_QUEUE_ERRCNTS);
416 status = __genwqe_readq(cd, queue->IO_QUEUE_STATUS);
417
418 dev_err(&pci_dev->dev,
Frank Haverkampd9c11d42014-09-10 16:37:51 +0200419 "[%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 +0100420 __func__, be16_to_cpu(pddcb->seqnum_16),
421 pddcb->hsi, retc_16, errcnts, status,
422 queue->ddcb_daddr + ddcb_offs);
423 }
424
425 copy_ddcb_results(req, queue->ddcb_act);
426 queue->ddcb_req[queue->ddcb_act] = NULL; /* take from queue */
427
428 dev_dbg(&pci_dev->dev, "FINISHED DDCB#%d\n", req->num);
429 genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
430
431 ddcb_mark_finished(pddcb);
432
433 /* calculate CRC_16 to see if VCRC is correct */
434 vcrc = genwqe_crc16(pddcb->asv,
435 VCRC_LENGTH(req->cmd.asv_length),
436 0xffff);
437 vcrc_16 = be16_to_cpu(pddcb->vcrc_16);
438 if (vcrc != vcrc_16) {
439 printk_ratelimited(KERN_ERR
Frank Haverkampd9c11d42014-09-10 16:37:51 +0200440 "%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 +0100441 GENWQE_DEVNAME, dev_name(&pci_dev->dev),
442 pddcb->pre, VCRC_LENGTH(req->cmd.asv_length),
443 vcrc, vcrc_16);
444 }
445
446 ddcb_requ_set_state(req, GENWQE_REQU_FINISHED);
447 queue->ddcbs_completed++;
448 queue->ddcbs_in_flight--;
449
450 /* wake up process waiting for this DDCB */
451 wake_up_interruptible(&queue->ddcb_waitqs[queue->ddcb_act]);
452
453pick_next_one:
454 queue->ddcb_act = (queue->ddcb_act + 1) % queue->ddcb_max;
455 ddcbs_finished++;
456 }
457
458 go_home:
459 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
460 return ddcbs_finished;
461}
462
463/**
464 * __genwqe_wait_ddcb(): Waits until DDCB is completed
465 * @cd: pointer to genwqe device descriptor
466 * @req: pointer to requsted DDCB parameters
467 *
468 * The Service Layer will update the RETC in DDCB when processing is
469 * pending or done.
470 *
471 * Return: > 0 remaining jiffies, DDCB completed
472 * -ETIMEDOUT when timeout
473 * -ERESTARTSYS when ^C
474 * -EINVAL when unknown error condition
475 *
476 * When an error is returned the called needs to ensure that
477 * purge_ddcb() is being called to get the &req removed from the
478 * queue.
479 */
480int __genwqe_wait_ddcb(struct genwqe_dev *cd, struct ddcb_requ *req)
481{
482 int rc;
483 unsigned int ddcb_no;
484 struct ddcb_queue *queue;
485 struct pci_dev *pci_dev = cd->pci_dev;
486
487 if (req == NULL)
488 return -EINVAL;
489
490 queue = req->queue;
491 if (queue == NULL)
492 return -EINVAL;
493
494 ddcb_no = req->num;
495 if (ddcb_no >= queue->ddcb_max)
496 return -EINVAL;
497
498 rc = wait_event_interruptible_timeout(queue->ddcb_waitqs[ddcb_no],
499 ddcb_requ_finished(cd, req),
500 genwqe_ddcb_software_timeout * HZ);
501
502 /*
503 * We need to distinguish 3 cases here:
504 * 1. rc == 0 timeout occured
505 * 2. rc == -ERESTARTSYS signal received
506 * 3. rc > 0 remaining jiffies condition is true
507 */
508 if (rc == 0) {
509 struct ddcb_queue *queue = req->queue;
510 struct ddcb *pddcb;
511
512 /*
513 * Timeout may be caused by long task switching time.
514 * When timeout happens, check if the request has
515 * meanwhile completed.
516 */
517 genwqe_check_ddcb_queue(cd, req->queue);
518 if (ddcb_requ_finished(cd, req))
519 return rc;
520
521 dev_err(&pci_dev->dev,
522 "[%s] err: DDCB#%d timeout rc=%d state=%d req @ %p\n",
523 __func__, req->num, rc, ddcb_requ_get_state(req),
524 req);
525 dev_err(&pci_dev->dev,
526 "[%s] IO_QUEUE_STATUS=0x%016llx\n", __func__,
527 __genwqe_readq(cd, queue->IO_QUEUE_STATUS));
528
529 pddcb = &queue->ddcb_vaddr[req->num];
530 genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
531
532 print_ddcb_info(cd, req->queue);
533 return -ETIMEDOUT;
534
535 } else if (rc == -ERESTARTSYS) {
536 return rc;
537 /*
538 * EINTR: Stops the application
539 * ERESTARTSYS: Restartable systemcall; called again
540 */
541
542 } else if (rc < 0) {
543 dev_err(&pci_dev->dev,
544 "[%s] err: DDCB#%d unknown result (rc=%d) %d!\n",
545 __func__, req->num, rc, ddcb_requ_get_state(req));
546 return -EINVAL;
547 }
548
549 /* Severe error occured. Driver is forced to stop operation */
550 if (cd->card_state != GENWQE_CARD_USED) {
551 dev_err(&pci_dev->dev,
552 "[%s] err: DDCB#%d forced to stop (rc=%d)\n",
553 __func__, req->num, rc);
554 return -EIO;
555 }
556 return rc;
557}
558
559/**
560 * get_next_ddcb() - Get next available DDCB
561 * @cd: pointer to genwqe device descriptor
562 *
563 * DDCB's content is completely cleared but presets for PRE and
564 * SEQNUM. This function must only be called when ddcb_lock is held.
565 *
566 * Return: NULL if no empty DDCB available otherwise ptr to next DDCB.
567 */
568static struct ddcb *get_next_ddcb(struct genwqe_dev *cd,
569 struct ddcb_queue *queue,
570 int *num)
571{
572 u64 *pu64;
573 struct ddcb *pddcb;
574
575 if (queue_free_ddcbs(queue) == 0) /* queue is full */
576 return NULL;
577
578 /* find new ddcb */
579 pddcb = &queue->ddcb_vaddr[queue->ddcb_next];
580
581 /* if it is not completed, we are not allowed to use it */
582 /* barrier(); */
583 if ((pddcb->icrc_hsi_shi_32 & DDCB_COMPLETED_BE32) == 0x00000000)
584 return NULL;
585
586 *num = queue->ddcb_next; /* internal DDCB number */
587 queue->ddcb_next = (queue->ddcb_next + 1) % queue->ddcb_max;
588
589 /* clear important DDCB fields */
590 pu64 = (u64 *)pddcb;
591 pu64[0] = 0ULL; /* offs 0x00 (ICRC,HSI,SHI,...) */
592 pu64[1] = 0ULL; /* offs 0x01 (ACFUNC,CMD...) */
593
594 /* destroy previous results in ASV */
595 pu64[0x80/8] = 0ULL; /* offs 0x80 (ASV + 0) */
596 pu64[0x88/8] = 0ULL; /* offs 0x88 (ASV + 0x08) */
597 pu64[0x90/8] = 0ULL; /* offs 0x90 (ASV + 0x10) */
598 pu64[0x98/8] = 0ULL; /* offs 0x98 (ASV + 0x18) */
599 pu64[0xd0/8] = 0ULL; /* offs 0xd0 (RETC,ATTN...) */
600
601 pddcb->pre = DDCB_PRESET_PRE; /* 128 */
602 pddcb->seqnum_16 = cpu_to_be16(queue->ddcb_seq++);
603 return pddcb;
604}
605
606/**
607 * __genwqe_purge_ddcb() - Remove a DDCB from the workqueue
608 * @cd: genwqe device descriptor
609 * @req: DDCB request
610 *
611 * This will fail when the request was already FETCHED. In this case
612 * we need to wait until it is finished. Else the DDCB can be
613 * reused. This function also ensures that the request data structure
614 * is removed from ddcb_req[].
615 *
616 * Do not forget to call this function when genwqe_wait_ddcb() fails,
617 * such that the request gets really removed from ddcb_req[].
618 *
619 * Return: 0 success
620 */
621int __genwqe_purge_ddcb(struct genwqe_dev *cd, struct ddcb_requ *req)
622{
623 struct ddcb *pddcb = NULL;
624 unsigned int t;
625 unsigned long flags;
626 struct ddcb_queue *queue = req->queue;
627 struct pci_dev *pci_dev = cd->pci_dev;
Frank Haverkampeaf47222013-12-09 13:30:40 +0100628 u64 queue_status;
Frank Haverkamp58d66ce2013-12-20 16:26:10 +0100629 __be32 icrc_hsi_shi = 0x0000;
630 __be32 old, new;
Frank Haverkampeaf47222013-12-09 13:30:40 +0100631
632 /* unsigned long flags; */
633 if (genwqe_ddcb_software_timeout <= 0) {
634 dev_err(&pci_dev->dev,
635 "[%s] err: software timeout is not set!\n", __func__);
636 return -EFAULT;
637 }
638
639 pddcb = &queue->ddcb_vaddr[req->num];
640
641 for (t = 0; t < genwqe_ddcb_software_timeout * 10; t++) {
642
643 spin_lock_irqsave(&queue->ddcb_lock, flags);
644
645 /* Check if req was meanwhile finished */
646 if (ddcb_requ_get_state(req) == GENWQE_REQU_FINISHED)
647 goto go_home;
648
649 /* try to set PURGE bit if FETCHED/COMPLETED are not set */
650 old = pddcb->icrc_hsi_shi_32; /* read SHI/HSI in BE32 */
651 if ((old & DDCB_FETCHED_BE32) == 0x00000000) {
652
653 new = (old | DDCB_PURGE_BE32);
654 icrc_hsi_shi = cmpxchg(&pddcb->icrc_hsi_shi_32,
655 old, new);
656 if (icrc_hsi_shi == old)
657 goto finish_ddcb;
658 }
659
660 /* normal finish with HSI bit */
661 barrier();
662 icrc_hsi_shi = pddcb->icrc_hsi_shi_32;
663 if (icrc_hsi_shi & DDCB_COMPLETED_BE32)
664 goto finish_ddcb;
665
666 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
667
668 /*
669 * Here the check_ddcb() function will most likely
670 * discover this DDCB to be finished some point in
671 * time. It will mark the req finished and free it up
672 * in the list.
673 */
674
675 copy_ddcb_results(req, req->num); /* for the failing case */
676 msleep(100); /* sleep for 1/10 second and try again */
677 continue;
678
679finish_ddcb:
680 copy_ddcb_results(req, req->num);
681 ddcb_requ_set_state(req, GENWQE_REQU_FINISHED);
682 queue->ddcbs_in_flight--;
683 queue->ddcb_req[req->num] = NULL; /* delete from array */
684 ddcb_mark_cleared(pddcb);
685
686 /* Move active DDCB further; Nothing to do here anymore. */
687
688 /*
689 * We need to ensure that there is at least one free
690 * DDCB in the queue. To do that, we must update
691 * ddcb_act only if the COMPLETED bit is set for the
692 * DDCB we are working on else we treat that DDCB even
693 * if we PURGED it as occupied (hardware is supposed
694 * to set the COMPLETED bit yet!).
695 */
696 icrc_hsi_shi = pddcb->icrc_hsi_shi_32;
697 if ((icrc_hsi_shi & DDCB_COMPLETED_BE32) &&
698 (queue->ddcb_act == req->num)) {
699 queue->ddcb_act = ((queue->ddcb_act + 1) %
700 queue->ddcb_max);
701 }
702go_home:
703 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
704 return 0;
705 }
706
707 /*
708 * If the card is dead and the queue is forced to stop, we
709 * might see this in the queue status register.
710 */
711 queue_status = __genwqe_readq(cd, queue->IO_QUEUE_STATUS);
712
713 dev_dbg(&pci_dev->dev, "UN/FINISHED DDCB#%d\n", req->num);
714 genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
715
716 dev_err(&pci_dev->dev,
Frank Haverkampd9c11d42014-09-10 16:37:51 +0200717 "[%s] err: DDCB#%d not purged and not completed after %d seconds QSTAT=%016llx!!\n",
Frank Haverkampeaf47222013-12-09 13:30:40 +0100718 __func__, req->num, genwqe_ddcb_software_timeout,
719 queue_status);
720
721 print_ddcb_info(cd, req->queue);
722
723 return -EFAULT;
724}
725
726int genwqe_init_debug_data(struct genwqe_dev *cd, struct genwqe_debug_data *d)
727{
728 int len;
729 struct pci_dev *pci_dev = cd->pci_dev;
730
731 if (d == NULL) {
732 dev_err(&pci_dev->dev,
733 "[%s] err: invalid memory for debug data!\n",
734 __func__);
735 return -EFAULT;
736 }
737
738 len = sizeof(d->driver_version);
Frank Haverkamp64df2ec2014-09-10 16:37:47 +0200739 snprintf(d->driver_version, len, "%s", DRV_VERSION);
Frank Haverkampeaf47222013-12-09 13:30:40 +0100740 d->slu_unitcfg = cd->slu_unitcfg;
741 d->app_unitcfg = cd->app_unitcfg;
742 return 0;
743}
744
745/**
746 * __genwqe_enqueue_ddcb() - Enqueue a DDCB
747 * @cd: pointer to genwqe device descriptor
748 * @req: pointer to DDCB execution request
749 *
750 * Return: 0 if enqueuing succeeded
751 * -EIO if card is unusable/PCIe problems
752 * -EBUSY if enqueuing failed
753 */
754int __genwqe_enqueue_ddcb(struct genwqe_dev *cd, struct ddcb_requ *req)
755{
756 struct ddcb *pddcb;
757 unsigned long flags;
758 struct ddcb_queue *queue;
759 struct pci_dev *pci_dev = cd->pci_dev;
760 u16 icrc;
761
762 if (cd->card_state != GENWQE_CARD_USED) {
763 printk_ratelimited(KERN_ERR
764 "%s %s: [%s] Card is unusable/PCIe problem Req#%d\n",
765 GENWQE_DEVNAME, dev_name(&pci_dev->dev),
766 __func__, req->num);
767 return -EIO;
768 }
769
770 queue = req->queue = &cd->queue;
771
772 /* FIXME circumvention to improve performance when no irq is
773 * there.
774 */
775 if (genwqe_polling_enabled)
776 genwqe_check_ddcb_queue(cd, queue);
777
778 /*
779 * It must be ensured to process all DDCBs in successive
780 * order. Use a lock here in order to prevent nested DDCB
781 * enqueuing.
782 */
783 spin_lock_irqsave(&queue->ddcb_lock, flags);
784
785 pddcb = get_next_ddcb(cd, queue, &req->num); /* get ptr and num */
786 if (pddcb == NULL) {
787 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
788 queue->busy++;
789 return -EBUSY;
790 }
791
792 if (queue->ddcb_req[req->num] != NULL) {
793 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
794
795 dev_err(&pci_dev->dev,
796 "[%s] picked DDCB %d with req=%p still in use!!\n",
797 __func__, req->num, req);
798 return -EFAULT;
799 }
800 ddcb_requ_set_state(req, GENWQE_REQU_ENQUEUED);
801 queue->ddcb_req[req->num] = req;
802
803 pddcb->cmdopts_16 = cpu_to_be16(req->cmd.cmdopts);
804 pddcb->cmd = req->cmd.cmd;
805 pddcb->acfunc = req->cmd.acfunc; /* functional unit */
806
807 /*
808 * We know that we can get retc 0x104 with CRC error, do not
809 * stop the queue in those cases for this command. XDIR = 1
810 * does not work for old SLU versions.
811 *
812 * Last bitstream with the old XDIR behavior had SLU_ID
813 * 0x34199.
814 */
815 if ((cd->slu_unitcfg & 0xFFFF0ull) > 0x34199ull)
816 pddcb->xdir = 0x1;
817 else
818 pddcb->xdir = 0x0;
819
820
821 pddcb->psp = (((req->cmd.asiv_length / 8) << 4) |
822 ((req->cmd.asv_length / 8)));
823 pddcb->disp_ts_64 = cpu_to_be64(req->cmd.disp_ts);
824
825 /*
826 * If copying the whole DDCB_ASIV_LENGTH is impacting
827 * performance we need to change it to
828 * req->cmd.asiv_length. But simulation benefits from some
829 * non-architectured bits behind the architectured content.
830 *
831 * How much data is copied depends on the availability of the
832 * ATS field, which was introduced late. If the ATS field is
833 * supported ASIV is 8 bytes shorter than it used to be. Since
834 * the ATS field is copied too, the code should do exactly
835 * what it did before, but I wanted to make copying of the ATS
836 * field very explicit.
837 */
838 if (genwqe_get_slu_id(cd) <= 0x2) {
839 memcpy(&pddcb->__asiv[0], /* destination */
840 &req->cmd.__asiv[0], /* source */
841 DDCB_ASIV_LENGTH); /* req->cmd.asiv_length */
842 } else {
Frank Haverkamp58d66ce2013-12-20 16:26:10 +0100843 pddcb->n.ats_64 = cpu_to_be64(req->cmd.ats);
844 memcpy(&pddcb->n.asiv[0], /* destination */
Frank Haverkampeaf47222013-12-09 13:30:40 +0100845 &req->cmd.asiv[0], /* source */
846 DDCB_ASIV_LENGTH_ATS); /* req->cmd.asiv_length */
847 }
848
849 pddcb->icrc_hsi_shi_32 = cpu_to_be32(0x00000000); /* for crc */
850
851 /*
852 * Calculate CRC_16 for corresponding range PSP(7:4). Include
853 * empty 4 bytes prior to the data.
854 */
855 icrc = genwqe_crc16((const u8 *)pddcb,
856 ICRC_LENGTH(req->cmd.asiv_length), 0xffff);
857 pddcb->icrc_hsi_shi_32 = cpu_to_be32((u32)icrc << 16);
858
859 /* enable DDCB completion irq */
860 if (!genwqe_polling_enabled)
861 pddcb->icrc_hsi_shi_32 |= DDCB_INTR_BE32;
862
863 dev_dbg(&pci_dev->dev, "INPUT DDCB#%d\n", req->num);
864 genwqe_hexdump(pci_dev, pddcb, sizeof(*pddcb));
865
866 if (ddcb_requ_collect_debug_data(req)) {
867 /* use the kernel copy of debug data. copying back to
868 user buffer happens later */
869
870 genwqe_init_debug_data(cd, &req->debug_data);
871 memcpy(&req->debug_data.ddcb_before, pddcb,
872 sizeof(req->debug_data.ddcb_before));
873 }
874
875 enqueue_ddcb(cd, queue, pddcb, req->num);
876 queue->ddcbs_in_flight++;
877
878 if (queue->ddcbs_in_flight > queue->ddcbs_max_in_flight)
879 queue->ddcbs_max_in_flight = queue->ddcbs_in_flight;
880
881 ddcb_requ_set_state(req, GENWQE_REQU_TAPPED);
882 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
883 wake_up_interruptible(&cd->queue_waitq);
884
885 return 0;
886}
887
888/**
889 * __genwqe_execute_raw_ddcb() - Setup and execute DDCB
890 * @cd: pointer to genwqe device descriptor
891 * @req: user provided DDCB request
892 */
893int __genwqe_execute_raw_ddcb(struct genwqe_dev *cd,
894 struct genwqe_ddcb_cmd *cmd)
895{
896 int rc = 0;
897 struct pci_dev *pci_dev = cd->pci_dev;
898 struct ddcb_requ *req = container_of(cmd, struct ddcb_requ, cmd);
899
900 if (cmd->asiv_length > DDCB_ASIV_LENGTH) {
901 dev_err(&pci_dev->dev, "[%s] err: wrong asiv_length of %d\n",
902 __func__, cmd->asiv_length);
903 return -EINVAL;
904 }
905 if (cmd->asv_length > DDCB_ASV_LENGTH) {
906 dev_err(&pci_dev->dev, "[%s] err: wrong asv_length of %d\n",
907 __func__, cmd->asiv_length);
908 return -EINVAL;
909 }
910 rc = __genwqe_enqueue_ddcb(cd, req);
911 if (rc != 0)
912 return rc;
913
914 rc = __genwqe_wait_ddcb(cd, req);
915 if (rc < 0) /* error or signal interrupt */
916 goto err_exit;
917
918 if (ddcb_requ_collect_debug_data(req)) {
Frank Haverkamp58d66ce2013-12-20 16:26:10 +0100919 if (copy_to_user((struct genwqe_debug_data __user *)
920 (unsigned long)cmd->ddata_addr,
Frank Haverkampeaf47222013-12-09 13:30:40 +0100921 &req->debug_data,
922 sizeof(struct genwqe_debug_data)))
923 return -EFAULT;
924 }
925
926 /*
927 * Higher values than 0x102 indicate completion with faults,
928 * lower values than 0x102 indicate processing faults. Note
929 * that DDCB might have been purged. E.g. Cntl+C.
930 */
931 if (cmd->retc != DDCB_RETC_COMPLETE) {
932 /* This might happen e.g. flash read, and needs to be
933 handled by the upper layer code. */
934 rc = -EBADMSG; /* not processed/error retc */
935 }
936
937 return rc;
938
939 err_exit:
940 __genwqe_purge_ddcb(cd, req);
941
942 if (ddcb_requ_collect_debug_data(req)) {
Frank Haverkamp58d66ce2013-12-20 16:26:10 +0100943 if (copy_to_user((struct genwqe_debug_data __user *)
944 (unsigned long)cmd->ddata_addr,
Frank Haverkampeaf47222013-12-09 13:30:40 +0100945 &req->debug_data,
946 sizeof(struct genwqe_debug_data)))
947 return -EFAULT;
948 }
949 return rc;
950}
951
952/**
953 * genwqe_next_ddcb_ready() - Figure out if the next DDCB is already finished
954 *
955 * We use this as condition for our wait-queue code.
956 */
957static int genwqe_next_ddcb_ready(struct genwqe_dev *cd)
958{
959 unsigned long flags;
960 struct ddcb *pddcb;
961 struct ddcb_queue *queue = &cd->queue;
962
963 spin_lock_irqsave(&queue->ddcb_lock, flags);
964
965 if (queue_empty(queue)) { /* emtpy queue */
966 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
967 return 0;
968 }
969
970 pddcb = &queue->ddcb_vaddr[queue->ddcb_act];
971 if (pddcb->icrc_hsi_shi_32 & DDCB_COMPLETED_BE32) { /* ddcb ready */
972 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
973 return 1;
974 }
975
976 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
977 return 0;
978}
979
980/**
981 * genwqe_ddcbs_in_flight() - Check how many DDCBs are in flight
982 *
983 * Keep track on the number of DDCBs which ware currently in the
984 * queue. This is needed for statistics as well as conditon if we want
985 * to wait or better do polling in case of no interrupts available.
986 */
987int genwqe_ddcbs_in_flight(struct genwqe_dev *cd)
988{
989 unsigned long flags;
990 int ddcbs_in_flight = 0;
991 struct ddcb_queue *queue = &cd->queue;
992
993 spin_lock_irqsave(&queue->ddcb_lock, flags);
994 ddcbs_in_flight += queue->ddcbs_in_flight;
995 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
996
997 return ddcbs_in_flight;
998}
999
1000static int setup_ddcb_queue(struct genwqe_dev *cd, struct ddcb_queue *queue)
1001{
1002 int rc, i;
1003 struct ddcb *pddcb;
1004 u64 val64;
1005 unsigned int queue_size;
1006 struct pci_dev *pci_dev = cd->pci_dev;
1007
1008 if (genwqe_ddcb_max < 2)
1009 return -EINVAL;
1010
1011 queue_size = roundup(genwqe_ddcb_max * sizeof(struct ddcb), PAGE_SIZE);
1012
1013 queue->ddcbs_in_flight = 0; /* statistics */
1014 queue->ddcbs_max_in_flight = 0;
1015 queue->ddcbs_completed = 0;
1016 queue->busy = 0;
1017
1018 queue->ddcb_seq = 0x100; /* start sequence number */
1019 queue->ddcb_max = genwqe_ddcb_max; /* module parameter */
1020 queue->ddcb_vaddr = __genwqe_alloc_consistent(cd, queue_size,
1021 &queue->ddcb_daddr);
1022 if (queue->ddcb_vaddr == NULL) {
1023 dev_err(&pci_dev->dev,
1024 "[%s] **err: could not allocate DDCB **\n", __func__);
1025 return -ENOMEM;
1026 }
1027 memset(queue->ddcb_vaddr, 0, queue_size);
1028
1029 queue->ddcb_req = kzalloc(sizeof(struct ddcb_requ *) *
1030 queue->ddcb_max, GFP_KERNEL);
1031 if (!queue->ddcb_req) {
1032 rc = -ENOMEM;
1033 goto free_ddcbs;
1034 }
1035
1036 queue->ddcb_waitqs = kzalloc(sizeof(wait_queue_head_t) *
1037 queue->ddcb_max, GFP_KERNEL);
1038 if (!queue->ddcb_waitqs) {
1039 rc = -ENOMEM;
1040 goto free_requs;
1041 }
1042
1043 for (i = 0; i < queue->ddcb_max; i++) {
1044 pddcb = &queue->ddcb_vaddr[i]; /* DDCBs */
1045 pddcb->icrc_hsi_shi_32 = DDCB_COMPLETED_BE32;
1046 pddcb->retc_16 = cpu_to_be16(0xfff);
1047
1048 queue->ddcb_req[i] = NULL; /* requests */
1049 init_waitqueue_head(&queue->ddcb_waitqs[i]); /* waitqueues */
1050 }
1051
1052 queue->ddcb_act = 0;
1053 queue->ddcb_next = 0; /* queue is empty */
1054
1055 spin_lock_init(&queue->ddcb_lock);
1056 init_waitqueue_head(&queue->ddcb_waitq);
1057
1058 val64 = ((u64)(queue->ddcb_max - 1) << 8); /* lastptr */
1059 __genwqe_writeq(cd, queue->IO_QUEUE_CONFIG, 0x07); /* iCRC/vCRC */
1060 __genwqe_writeq(cd, queue->IO_QUEUE_SEGMENT, queue->ddcb_daddr);
1061 __genwqe_writeq(cd, queue->IO_QUEUE_INITSQN, queue->ddcb_seq);
1062 __genwqe_writeq(cd, queue->IO_QUEUE_WRAP, val64);
1063 return 0;
1064
1065 free_requs:
1066 kfree(queue->ddcb_req);
1067 queue->ddcb_req = NULL;
1068 free_ddcbs:
1069 __genwqe_free_consistent(cd, queue_size, queue->ddcb_vaddr,
1070 queue->ddcb_daddr);
1071 queue->ddcb_vaddr = NULL;
1072 queue->ddcb_daddr = 0ull;
1073 return -ENODEV;
1074
1075}
1076
1077static int ddcb_queue_initialized(struct ddcb_queue *queue)
1078{
1079 return queue->ddcb_vaddr != NULL;
1080}
1081
1082static void free_ddcb_queue(struct genwqe_dev *cd, struct ddcb_queue *queue)
1083{
1084 unsigned int queue_size;
1085
1086 queue_size = roundup(queue->ddcb_max * sizeof(struct ddcb), PAGE_SIZE);
1087
1088 kfree(queue->ddcb_req);
1089 queue->ddcb_req = NULL;
1090
1091 if (queue->ddcb_vaddr) {
1092 __genwqe_free_consistent(cd, queue_size, queue->ddcb_vaddr,
1093 queue->ddcb_daddr);
1094 queue->ddcb_vaddr = NULL;
1095 queue->ddcb_daddr = 0ull;
1096 }
1097}
1098
1099static irqreturn_t genwqe_pf_isr(int irq, void *dev_id)
1100{
1101 u64 gfir;
1102 struct genwqe_dev *cd = (struct genwqe_dev *)dev_id;
1103 struct pci_dev *pci_dev = cd->pci_dev;
1104
1105 /*
1106 * In case of fatal FIR error the queue is stopped, such that
1107 * we can safely check it without risking anything.
1108 */
1109 cd->irqs_processed++;
1110 wake_up_interruptible(&cd->queue_waitq);
1111
1112 /*
1113 * Checking for errors before kicking the queue might be
1114 * safer, but slower for the good-case ... See above.
1115 */
1116 gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
Kleber Sacilotto de Souzafb145452014-06-04 10:57:51 -03001117 if (((gfir & GFIR_ERR_TRIGGER) != 0x0) &&
1118 !pci_channel_offline(pci_dev)) {
1119
1120 if (cd->use_platform_recovery) {
1121 /*
1122 * Since we use raw accessors, EEH errors won't be
1123 * detected by the platform until we do a non-raw
1124 * MMIO or config space read
1125 */
1126 readq(cd->mmio + IO_SLC_CFGREG_GFIR);
1127
1128 /* Don't do anything if the PCI channel is frozen */
1129 if (pci_channel_offline(pci_dev))
1130 goto exit;
1131 }
Frank Haverkampeaf47222013-12-09 13:30:40 +01001132
1133 wake_up_interruptible(&cd->health_waitq);
1134
1135 /*
1136 * By default GFIRs causes recovery actions. This
1137 * count is just for debug when recovery is masked.
1138 */
Kleber Sacilotto de Souzafb145452014-06-04 10:57:51 -03001139 dev_err_ratelimited(&pci_dev->dev,
1140 "[%s] GFIR=%016llx\n",
1141 __func__, gfir);
Frank Haverkampeaf47222013-12-09 13:30:40 +01001142 }
1143
Kleber Sacilotto de Souzafb145452014-06-04 10:57:51 -03001144 exit:
Frank Haverkampeaf47222013-12-09 13:30:40 +01001145 return IRQ_HANDLED;
1146}
1147
1148static irqreturn_t genwqe_vf_isr(int irq, void *dev_id)
1149{
1150 struct genwqe_dev *cd = (struct genwqe_dev *)dev_id;
1151
1152 cd->irqs_processed++;
1153 wake_up_interruptible(&cd->queue_waitq);
1154
1155 return IRQ_HANDLED;
1156}
1157
1158/**
1159 * genwqe_card_thread() - Work thread for the DDCB queue
1160 *
1161 * The idea is to check if there are DDCBs in processing. If there are
1162 * some finished DDCBs, we process them and wakeup the
1163 * requestors. Otherwise we give other processes time using
1164 * cond_resched().
1165 */
1166static int genwqe_card_thread(void *data)
1167{
1168 int should_stop = 0, rc = 0;
1169 struct genwqe_dev *cd = (struct genwqe_dev *)data;
1170
1171 while (!kthread_should_stop()) {
1172
1173 genwqe_check_ddcb_queue(cd, &cd->queue);
1174
1175 if (genwqe_polling_enabled) {
1176 rc = wait_event_interruptible_timeout(
1177 cd->queue_waitq,
1178 genwqe_ddcbs_in_flight(cd) ||
1179 (should_stop = kthread_should_stop()), 1);
1180 } else {
1181 rc = wait_event_interruptible_timeout(
1182 cd->queue_waitq,
1183 genwqe_next_ddcb_ready(cd) ||
1184 (should_stop = kthread_should_stop()), HZ);
1185 }
1186 if (should_stop)
1187 break;
1188
1189 /*
1190 * Avoid soft lockups on heavy loads; we do not want
1191 * to disable our interrupts.
1192 */
1193 cond_resched();
1194 }
1195 return 0;
1196}
1197
1198/**
1199 * genwqe_setup_service_layer() - Setup DDCB queue
1200 * @cd: pointer to genwqe device descriptor
1201 *
1202 * Allocate DDCBs. Configure Service Layer Controller (SLC).
1203 *
1204 * Return: 0 success
1205 */
1206int genwqe_setup_service_layer(struct genwqe_dev *cd)
1207{
1208 int rc;
1209 struct ddcb_queue *queue;
1210 struct pci_dev *pci_dev = cd->pci_dev;
1211
1212 if (genwqe_is_privileged(cd)) {
1213 rc = genwqe_card_reset(cd);
1214 if (rc < 0) {
1215 dev_err(&pci_dev->dev,
1216 "[%s] err: reset failed.\n", __func__);
1217 return rc;
1218 }
1219 genwqe_read_softreset(cd);
1220 }
1221
1222 queue = &cd->queue;
1223 queue->IO_QUEUE_CONFIG = IO_SLC_QUEUE_CONFIG;
1224 queue->IO_QUEUE_STATUS = IO_SLC_QUEUE_STATUS;
1225 queue->IO_QUEUE_SEGMENT = IO_SLC_QUEUE_SEGMENT;
1226 queue->IO_QUEUE_INITSQN = IO_SLC_QUEUE_INITSQN;
1227 queue->IO_QUEUE_OFFSET = IO_SLC_QUEUE_OFFSET;
1228 queue->IO_QUEUE_WRAP = IO_SLC_QUEUE_WRAP;
1229 queue->IO_QUEUE_WTIME = IO_SLC_QUEUE_WTIME;
1230 queue->IO_QUEUE_ERRCNTS = IO_SLC_QUEUE_ERRCNTS;
1231 queue->IO_QUEUE_LRW = IO_SLC_QUEUE_LRW;
1232
1233 rc = setup_ddcb_queue(cd, queue);
1234 if (rc != 0) {
1235 rc = -ENODEV;
1236 goto err_out;
1237 }
1238
1239 init_waitqueue_head(&cd->queue_waitq);
1240 cd->card_thread = kthread_run(genwqe_card_thread, cd,
1241 GENWQE_DEVNAME "%d_thread",
1242 cd->card_idx);
1243 if (IS_ERR(cd->card_thread)) {
1244 rc = PTR_ERR(cd->card_thread);
1245 cd->card_thread = NULL;
1246 goto stop_free_queue;
1247 }
1248
1249 rc = genwqe_set_interrupt_capability(cd, GENWQE_MSI_IRQS);
Frank Haverkamp2d880cc2014-09-10 16:37:49 +02001250 if (rc)
Frank Haverkampeaf47222013-12-09 13:30:40 +01001251 goto stop_kthread;
Frank Haverkampeaf47222013-12-09 13:30:40 +01001252
1253 /*
1254 * We must have all wait-queues initialized when we enable the
1255 * interrupts. Otherwise we might crash if we get an early
1256 * irq.
1257 */
1258 init_waitqueue_head(&cd->health_waitq);
1259
1260 if (genwqe_is_privileged(cd)) {
1261 rc = request_irq(pci_dev->irq, genwqe_pf_isr, IRQF_SHARED,
1262 GENWQE_DEVNAME, cd);
1263 } else {
1264 rc = request_irq(pci_dev->irq, genwqe_vf_isr, IRQF_SHARED,
1265 GENWQE_DEVNAME, cd);
1266 }
1267 if (rc < 0) {
1268 dev_err(&pci_dev->dev, "irq %d not free.\n", pci_dev->irq);
1269 goto stop_irq_cap;
1270 }
1271
1272 cd->card_state = GENWQE_CARD_USED;
1273 return 0;
1274
1275 stop_irq_cap:
1276 genwqe_reset_interrupt_capability(cd);
1277 stop_kthread:
1278 kthread_stop(cd->card_thread);
1279 cd->card_thread = NULL;
1280 stop_free_queue:
1281 free_ddcb_queue(cd, queue);
1282 err_out:
1283 return rc;
1284}
1285
1286/**
1287 * queue_wake_up_all() - Handles fatal error case
1288 *
1289 * The PCI device got unusable and we have to stop all pending
1290 * requests as fast as we can. The code after this must purge the
1291 * DDCBs in question and ensure that all mappings are freed.
1292 */
1293static int queue_wake_up_all(struct genwqe_dev *cd)
1294{
1295 unsigned int i;
1296 unsigned long flags;
1297 struct ddcb_queue *queue = &cd->queue;
1298
1299 spin_lock_irqsave(&queue->ddcb_lock, flags);
1300
1301 for (i = 0; i < queue->ddcb_max; i++)
1302 wake_up_interruptible(&queue->ddcb_waitqs[queue->ddcb_act]);
1303
1304 spin_unlock_irqrestore(&queue->ddcb_lock, flags);
1305
1306 return 0;
1307}
1308
1309/**
1310 * genwqe_finish_queue() - Remove any genwqe devices and user-interfaces
1311 *
1312 * Relies on the pre-condition that there are no users of the card
1313 * device anymore e.g. with open file-descriptors.
1314 *
1315 * This function must be robust enough to be called twice.
1316 */
1317int genwqe_finish_queue(struct genwqe_dev *cd)
1318{
Colin Ian Kingebb2c962014-03-20 15:11:04 +01001319 int i, rc = 0, in_flight;
Frank Haverkampeaf47222013-12-09 13:30:40 +01001320 int waitmax = genwqe_ddcb_software_timeout;
1321 struct pci_dev *pci_dev = cd->pci_dev;
1322 struct ddcb_queue *queue = &cd->queue;
1323
1324 if (!ddcb_queue_initialized(queue))
1325 return 0;
1326
1327 /* Do not wipe out the error state. */
1328 if (cd->card_state == GENWQE_CARD_USED)
1329 cd->card_state = GENWQE_CARD_UNUSED;
1330
1331 /* Wake up all requests in the DDCB queue such that they
1332 should be removed nicely. */
1333 queue_wake_up_all(cd);
1334
1335 /* We must wait to get rid of the DDCBs in flight */
1336 for (i = 0; i < waitmax; i++) {
1337 in_flight = genwqe_ddcbs_in_flight(cd);
1338
1339 if (in_flight == 0)
1340 break;
1341
1342 dev_dbg(&pci_dev->dev,
Frank Haverkampd9c11d42014-09-10 16:37:51 +02001343 " DEBUG [%d/%d] waiting for queue to get empty: %d requests!\n",
1344 i, waitmax, in_flight);
Frank Haverkampeaf47222013-12-09 13:30:40 +01001345
1346 /*
1347 * Severe severe error situation: The card itself has
1348 * 16 DDCB queues, each queue has e.g. 32 entries,
1349 * each DDBC has a hardware timeout of currently 250
1350 * msec but the PFs have a hardware timeout of 8 sec
1351 * ... so I take something large.
1352 */
1353 msleep(1000);
1354 }
1355 if (i == waitmax) {
1356 dev_err(&pci_dev->dev, " [%s] err: queue is not empty!!\n",
1357 __func__);
1358 rc = -EIO;
1359 }
1360 return rc;
1361}
1362
1363/**
1364 * genwqe_release_service_layer() - Shutdown DDCB queue
1365 * @cd: genwqe device descriptor
1366 *
1367 * This function must be robust enough to be called twice.
1368 */
1369int genwqe_release_service_layer(struct genwqe_dev *cd)
1370{
1371 struct pci_dev *pci_dev = cd->pci_dev;
1372
1373 if (!ddcb_queue_initialized(&cd->queue))
1374 return 1;
1375
1376 free_irq(pci_dev->irq, cd);
1377 genwqe_reset_interrupt_capability(cd);
1378
1379 if (cd->card_thread != NULL) {
1380 kthread_stop(cd->card_thread);
1381 cd->card_thread = NULL;
1382 }
1383
1384 free_ddcb_queue(cd, &cd->queue);
1385 return 0;
1386}