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