• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2006 - 2011 Intel-NE, Inc.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33 
34 #include <linux/skbuff.h>
35 #include <linux/etherdevice.h>
36 #include <linux/kthread.h>
37 #include <linux/ip.h>
38 #include <linux/tcp.h>
39 #include <net/tcp.h>
40 #include "nes.h"
41 #include "nes_mgt.h"
42 
43 atomic_t pau_qps_created;
44 atomic_t pau_qps_destroyed;
45 
nes_replenish_mgt_rq(struct nes_vnic_mgt * mgtvnic)46 static void nes_replenish_mgt_rq(struct nes_vnic_mgt *mgtvnic)
47 {
48 	unsigned long flags;
49 	dma_addr_t bus_address;
50 	struct sk_buff *skb;
51 	struct nes_hw_nic_rq_wqe *nic_rqe;
52 	struct nes_hw_mgt *nesmgt;
53 	struct nes_device *nesdev;
54 	struct nes_rskb_cb *cb;
55 	u32 rx_wqes_posted = 0;
56 
57 	nesmgt = &mgtvnic->mgt;
58 	nesdev = mgtvnic->nesvnic->nesdev;
59 	spin_lock_irqsave(&nesmgt->rq_lock, flags);
60 	if (nesmgt->replenishing_rq != 0) {
61 		if (((nesmgt->rq_size - 1) == atomic_read(&mgtvnic->rx_skbs_needed)) &&
62 		    (atomic_read(&mgtvnic->rx_skb_timer_running) == 0)) {
63 			atomic_set(&mgtvnic->rx_skb_timer_running, 1);
64 			spin_unlock_irqrestore(&nesmgt->rq_lock, flags);
65 			mgtvnic->rq_wqes_timer.expires = jiffies + (HZ / 2);      /* 1/2 second */
66 			add_timer(&mgtvnic->rq_wqes_timer);
67 		} else {
68 			spin_unlock_irqrestore(&nesmgt->rq_lock, flags);
69 		}
70 		return;
71 	}
72 	nesmgt->replenishing_rq = 1;
73 	spin_unlock_irqrestore(&nesmgt->rq_lock, flags);
74 	do {
75 		skb = dev_alloc_skb(mgtvnic->nesvnic->max_frame_size);
76 		if (skb) {
77 			skb->dev = mgtvnic->nesvnic->netdev;
78 
79 			bus_address = pci_map_single(nesdev->pcidev,
80 						     skb->data, mgtvnic->nesvnic->max_frame_size, PCI_DMA_FROMDEVICE);
81 			cb = (struct nes_rskb_cb *)&skb->cb[0];
82 			cb->busaddr = bus_address;
83 			cb->maplen = mgtvnic->nesvnic->max_frame_size;
84 
85 			nic_rqe = &nesmgt->rq_vbase[mgtvnic->mgt.rq_head];
86 			nic_rqe->wqe_words[NES_NIC_RQ_WQE_LENGTH_1_0_IDX] =
87 				cpu_to_le32(mgtvnic->nesvnic->max_frame_size);
88 			nic_rqe->wqe_words[NES_NIC_RQ_WQE_LENGTH_3_2_IDX] = 0;
89 			nic_rqe->wqe_words[NES_NIC_RQ_WQE_FRAG0_LOW_IDX] =
90 				cpu_to_le32((u32)bus_address);
91 			nic_rqe->wqe_words[NES_NIC_RQ_WQE_FRAG0_HIGH_IDX] =
92 				cpu_to_le32((u32)((u64)bus_address >> 32));
93 			nesmgt->rx_skb[nesmgt->rq_head] = skb;
94 			nesmgt->rq_head++;
95 			nesmgt->rq_head &= nesmgt->rq_size - 1;
96 			atomic_dec(&mgtvnic->rx_skbs_needed);
97 			barrier();
98 			if (++rx_wqes_posted == 255) {
99 				nes_write32(nesdev->regs + NES_WQE_ALLOC, (rx_wqes_posted << 24) | nesmgt->qp_id);
100 				rx_wqes_posted = 0;
101 			}
102 		} else {
103 			spin_lock_irqsave(&nesmgt->rq_lock, flags);
104 			if (((nesmgt->rq_size - 1) == atomic_read(&mgtvnic->rx_skbs_needed)) &&
105 			    (atomic_read(&mgtvnic->rx_skb_timer_running) == 0)) {
106 				atomic_set(&mgtvnic->rx_skb_timer_running, 1);
107 				spin_unlock_irqrestore(&nesmgt->rq_lock, flags);
108 				mgtvnic->rq_wqes_timer.expires = jiffies + (HZ / 2);      /* 1/2 second */
109 				add_timer(&mgtvnic->rq_wqes_timer);
110 			} else {
111 				spin_unlock_irqrestore(&nesmgt->rq_lock, flags);
112 			}
113 			break;
114 		}
115 	} while (atomic_read(&mgtvnic->rx_skbs_needed));
116 	barrier();
117 	if (rx_wqes_posted)
118 		nes_write32(nesdev->regs + NES_WQE_ALLOC, (rx_wqes_posted << 24) | nesmgt->qp_id);
119 	nesmgt->replenishing_rq = 0;
120 }
121 
122 /**
123  * nes_mgt_rq_wqes_timeout
124  */
nes_mgt_rq_wqes_timeout(unsigned long parm)125 static void nes_mgt_rq_wqes_timeout(unsigned long parm)
126 {
127 	struct nes_vnic_mgt *mgtvnic = (struct nes_vnic_mgt *)parm;
128 
129 	atomic_set(&mgtvnic->rx_skb_timer_running, 0);
130 	if (atomic_read(&mgtvnic->rx_skbs_needed))
131 		nes_replenish_mgt_rq(mgtvnic);
132 }
133 
134 /**
135  * nes_mgt_free_skb - unmap and free skb
136  */
nes_mgt_free_skb(struct nes_device * nesdev,struct sk_buff * skb,u32 dir)137 static void nes_mgt_free_skb(struct nes_device *nesdev, struct sk_buff *skb, u32 dir)
138 {
139 	struct nes_rskb_cb *cb;
140 
141 	cb = (struct nes_rskb_cb *)&skb->cb[0];
142 	pci_unmap_single(nesdev->pcidev, cb->busaddr, cb->maplen, dir);
143 	cb->busaddr = 0;
144 	dev_kfree_skb_any(skb);
145 }
146 
147 /**
148  * nes_download_callback - handle download completions
149  */
nes_download_callback(struct nes_device * nesdev,struct nes_cqp_request * cqp_request)150 static void nes_download_callback(struct nes_device *nesdev, struct nes_cqp_request *cqp_request)
151 {
152 	struct pau_fpdu_info *fpdu_info = cqp_request->cqp_callback_pointer;
153 	struct nes_qp *nesqp = fpdu_info->nesqp;
154 	struct sk_buff *skb;
155 	int i;
156 
157 	for (i = 0; i < fpdu_info->frag_cnt; i++) {
158 		skb = fpdu_info->frags[i].skb;
159 		if (fpdu_info->frags[i].cmplt) {
160 			nes_mgt_free_skb(nesdev, skb, PCI_DMA_TODEVICE);
161 			nes_rem_ref_cm_node(nesqp->cm_node);
162 		}
163 	}
164 
165 	if (fpdu_info->hdr_vbase)
166 		pci_free_consistent(nesdev->pcidev, fpdu_info->hdr_len,
167 				    fpdu_info->hdr_vbase, fpdu_info->hdr_pbase);
168 	kfree(fpdu_info);
169 }
170 
171 /**
172  * nes_get_seq - Get the seq, ack_seq and window from the packet
173  */
nes_get_seq(struct sk_buff * skb,u32 * ack,u16 * wnd,u32 * fin_rcvd,u32 * rst_rcvd)174 static u32 nes_get_seq(struct sk_buff *skb, u32 *ack, u16 *wnd, u32 *fin_rcvd, u32 *rst_rcvd)
175 {
176 	struct nes_rskb_cb *cb = (struct nes_rskb_cb *)&skb->cb[0];
177 	struct iphdr *iph = (struct iphdr *)(cb->data_start + ETH_HLEN);
178 	struct tcphdr *tcph = (struct tcphdr *)(((char *)iph) + (4 * iph->ihl));
179 
180 	*ack = be32_to_cpu(tcph->ack_seq);
181 	*wnd = be16_to_cpu(tcph->window);
182 	*fin_rcvd = tcph->fin;
183 	*rst_rcvd = tcph->rst;
184 	return be32_to_cpu(tcph->seq);
185 }
186 
187 /**
188  * nes_get_next_skb - Get the next skb based on where current skb is in the queue
189  */
nes_get_next_skb(struct nes_device * nesdev,struct nes_qp * nesqp,struct sk_buff * skb,u32 nextseq,u32 * ack,u16 * wnd,u32 * fin_rcvd,u32 * rst_rcvd)190 static struct sk_buff *nes_get_next_skb(struct nes_device *nesdev, struct nes_qp *nesqp,
191 					struct sk_buff *skb, u32 nextseq, u32 *ack,
192 					u16 *wnd, u32 *fin_rcvd, u32 *rst_rcvd)
193 {
194 	u32 seq;
195 	bool processacks;
196 	struct sk_buff *old_skb;
197 
198 	if (skb) {
199 		/* Continue processing fpdu */
200 		if (skb->next == (struct sk_buff *)&nesqp->pau_list)
201 			goto out;
202 		skb = skb->next;
203 		processacks = false;
204 	} else {
205 		/* Starting a new one */
206 		if (skb_queue_empty(&nesqp->pau_list))
207 			goto out;
208 		skb = skb_peek(&nesqp->pau_list);
209 		processacks = true;
210 	}
211 
212 	while (1) {
213 		if (skb_queue_empty(&nesqp->pau_list))
214 			goto out;
215 
216 		seq = nes_get_seq(skb, ack, wnd, fin_rcvd, rst_rcvd);
217 		if (seq == nextseq) {
218 			if (skb->len || processacks)
219 				break;
220 		} else if (after(seq, nextseq)) {
221 			goto out;
222 		}
223 
224 		old_skb = skb;
225 		skb = skb->next;
226 		skb_unlink(old_skb, &nesqp->pau_list);
227 		nes_mgt_free_skb(nesdev, old_skb, PCI_DMA_TODEVICE);
228 		nes_rem_ref_cm_node(nesqp->cm_node);
229 		if (skb == (struct sk_buff *)&nesqp->pau_list)
230 			goto out;
231 	}
232 	return skb;
233 
234 out:
235 	return NULL;
236 }
237 
238 /**
239  * get_fpdu_info - Find the next complete fpdu and return its fragments.
240  */
get_fpdu_info(struct nes_device * nesdev,struct nes_qp * nesqp,struct pau_fpdu_info ** pau_fpdu_info)241 static int get_fpdu_info(struct nes_device *nesdev, struct nes_qp *nesqp,
242 			 struct pau_fpdu_info **pau_fpdu_info)
243 {
244 	struct sk_buff *skb;
245 	struct iphdr *iph;
246 	struct tcphdr *tcph;
247 	struct nes_rskb_cb *cb;
248 	struct pau_fpdu_info *fpdu_info = NULL;
249 	struct pau_fpdu_frag frags[MAX_FPDU_FRAGS];
250 	u32 fpdu_len = 0;
251 	u32 tmp_len;
252 	int frag_cnt = 0;
253 	u32 tot_len;
254 	u32 frag_tot;
255 	u32 ack;
256 	u32 fin_rcvd;
257 	u32 rst_rcvd;
258 	u16 wnd;
259 	int i;
260 	int rc = 0;
261 
262 	*pau_fpdu_info = NULL;
263 
264 	skb = nes_get_next_skb(nesdev, nesqp, NULL, nesqp->pau_rcv_nxt, &ack, &wnd, &fin_rcvd, &rst_rcvd);
265 	if (!skb)
266 		goto out;
267 
268 	cb = (struct nes_rskb_cb *)&skb->cb[0];
269 	if (skb->len) {
270 		fpdu_len = be16_to_cpu(*(__be16 *) skb->data) + MPA_FRAMING;
271 		fpdu_len = (fpdu_len + 3) & 0xfffffffc;
272 		tmp_len = fpdu_len;
273 
274 		/* See if we have all of the fpdu */
275 		frag_tot = 0;
276 		memset(&frags, 0, sizeof frags);
277 		for (i = 0; i < MAX_FPDU_FRAGS; i++) {
278 			frags[i].physaddr = cb->busaddr;
279 			frags[i].physaddr += skb->data - cb->data_start;
280 			frags[i].frag_len = min(tmp_len, skb->len);
281 			frags[i].skb = skb;
282 			frags[i].cmplt = (skb->len == frags[i].frag_len);
283 			frag_tot += frags[i].frag_len;
284 			frag_cnt++;
285 
286 			tmp_len -= frags[i].frag_len;
287 			if (tmp_len == 0)
288 				break;
289 
290 			skb = nes_get_next_skb(nesdev, nesqp, skb,
291 					       nesqp->pau_rcv_nxt + frag_tot, &ack, &wnd, &fin_rcvd, &rst_rcvd);
292 			if (!skb)
293 				goto out;
294 			if (rst_rcvd) {
295 				/* rst received in the middle of fpdu */
296 				for (; i >= 0; i--) {
297 					skb_unlink(frags[i].skb, &nesqp->pau_list);
298 					nes_mgt_free_skb(nesdev, frags[i].skb, PCI_DMA_TODEVICE);
299 				}
300 				cb = (struct nes_rskb_cb *)&skb->cb[0];
301 				frags[0].physaddr = cb->busaddr;
302 				frags[0].physaddr += skb->data - cb->data_start;
303 				frags[0].frag_len = skb->len;
304 				frags[0].skb = skb;
305 				frags[0].cmplt = true;
306 				frag_cnt = 1;
307 				break;
308 			}
309 
310 			cb = (struct nes_rskb_cb *)&skb->cb[0];
311 		}
312 	} else {
313 		/* no data */
314 		frags[0].physaddr = cb->busaddr;
315 		frags[0].frag_len = 0;
316 		frags[0].skb = skb;
317 		frags[0].cmplt = true;
318 		frag_cnt = 1;
319 	}
320 
321 	/* Found one */
322 	fpdu_info = kzalloc(sizeof(*fpdu_info), GFP_ATOMIC);
323 	if (!fpdu_info) {
324 		rc = -ENOMEM;
325 		goto out;
326 	}
327 
328 	fpdu_info->cqp_request = nes_get_cqp_request(nesdev);
329 	if (fpdu_info->cqp_request == NULL) {
330 		nes_debug(NES_DBG_PAU, "Failed to get a cqp_request.\n");
331 		rc = -ENOMEM;
332 		goto out;
333 	}
334 
335 	cb = (struct nes_rskb_cb *)&frags[0].skb->cb[0];
336 	iph = (struct iphdr *)(cb->data_start + ETH_HLEN);
337 	tcph = (struct tcphdr *)(((char *)iph) + (4 * iph->ihl));
338 	fpdu_info->hdr_len = (((unsigned char *)tcph) + 4 * (tcph->doff)) - cb->data_start;
339 	fpdu_info->data_len = fpdu_len;
340 	tot_len = fpdu_info->hdr_len + fpdu_len - ETH_HLEN;
341 
342 	if (frags[0].cmplt) {
343 		fpdu_info->hdr_pbase = cb->busaddr;
344 		fpdu_info->hdr_vbase = NULL;
345 	} else {
346 		fpdu_info->hdr_vbase = pci_alloc_consistent(nesdev->pcidev,
347 							    fpdu_info->hdr_len, &fpdu_info->hdr_pbase);
348 		if (!fpdu_info->hdr_vbase) {
349 			nes_debug(NES_DBG_PAU, "Unable to allocate memory for pau first frag\n");
350 			rc = -ENOMEM;
351 			goto out;
352 		}
353 
354 		/* Copy hdrs, adjusting len and seqnum */
355 		memcpy(fpdu_info->hdr_vbase, cb->data_start, fpdu_info->hdr_len);
356 		iph = (struct iphdr *)(fpdu_info->hdr_vbase + ETH_HLEN);
357 		tcph = (struct tcphdr *)(((char *)iph) + (4 * iph->ihl));
358 	}
359 
360 	iph->tot_len = cpu_to_be16(tot_len);
361 	iph->saddr = cpu_to_be32(0x7f000001);
362 
363 	tcph->seq = cpu_to_be32(nesqp->pau_rcv_nxt);
364 	tcph->ack_seq = cpu_to_be32(ack);
365 	tcph->window = cpu_to_be16(wnd);
366 
367 	nesqp->pau_rcv_nxt += fpdu_len + fin_rcvd;
368 
369 	memcpy(fpdu_info->frags, frags, sizeof(fpdu_info->frags));
370 	fpdu_info->frag_cnt = frag_cnt;
371 	fpdu_info->nesqp = nesqp;
372 	*pau_fpdu_info = fpdu_info;
373 
374 	/* Update skb's for next pass */
375 	for (i = 0; i < frag_cnt; i++) {
376 		cb = (struct nes_rskb_cb *)&frags[i].skb->cb[0];
377 		skb_pull(frags[i].skb, frags[i].frag_len);
378 
379 		if (frags[i].skb->len == 0) {
380 			/* Pull skb off the list - it will be freed in the callback */
381 			if (!skb_queue_empty(&nesqp->pau_list))
382 				skb_unlink(frags[i].skb, &nesqp->pau_list);
383 		} else {
384 			/* Last skb still has data so update the seq */
385 			iph = (struct iphdr *)(cb->data_start + ETH_HLEN);
386 			tcph = (struct tcphdr *)(((char *)iph) + (4 * iph->ihl));
387 			tcph->seq = cpu_to_be32(nesqp->pau_rcv_nxt);
388 		}
389 	}
390 
391 out:
392 	if (rc) {
393 		if (fpdu_info) {
394 			if (fpdu_info->cqp_request)
395 				nes_put_cqp_request(nesdev, fpdu_info->cqp_request);
396 			kfree(fpdu_info);
397 		}
398 	}
399 	return rc;
400 }
401 
402 /**
403  * forward_fpdu - send complete fpdus, one at a time
404  */
forward_fpdus(struct nes_vnic * nesvnic,struct nes_qp * nesqp)405 static int forward_fpdus(struct nes_vnic *nesvnic, struct nes_qp *nesqp)
406 {
407 	struct nes_device *nesdev = nesvnic->nesdev;
408 	struct pau_fpdu_info *fpdu_info;
409 	struct nes_hw_cqp_wqe *cqp_wqe;
410 	struct nes_cqp_request *cqp_request;
411 	unsigned long flags;
412 	u64 u64tmp;
413 	u32 u32tmp;
414 	int rc;
415 
416 	while (1) {
417 		spin_lock_irqsave(&nesqp->pau_lock, flags);
418 		rc = get_fpdu_info(nesdev, nesqp, &fpdu_info);
419 		if (rc || (fpdu_info == NULL)) {
420 			spin_unlock_irqrestore(&nesqp->pau_lock, flags);
421 			return rc;
422 		}
423 
424 		cqp_request = fpdu_info->cqp_request;
425 		cqp_wqe = &cqp_request->cqp_wqe;
426 		nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
427 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_DL_OPCODE_IDX,
428 				    NES_CQP_DOWNLOAD_SEGMENT |
429 				    (((u32)nesvnic->logical_port) << NES_CQP_OP_LOGICAL_PORT_SHIFT));
430 
431 		u32tmp = fpdu_info->hdr_len << 16;
432 		u32tmp |= fpdu_info->hdr_len + (u32)fpdu_info->data_len;
433 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_DL_LENGTH_0_TOTAL_IDX,
434 				    u32tmp);
435 
436 		u32tmp = (fpdu_info->frags[1].frag_len << 16) | fpdu_info->frags[0].frag_len;
437 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_LENGTH_2_1_IDX,
438 				    u32tmp);
439 
440 		u32tmp = (fpdu_info->frags[3].frag_len << 16) | fpdu_info->frags[2].frag_len;
441 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_LENGTH_4_3_IDX,
442 				    u32tmp);
443 
444 		u64tmp = (u64)fpdu_info->hdr_pbase;
445 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG0_LOW_IDX,
446 				    lower_32_bits(u64tmp));
447 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG0_HIGH_IDX,
448 				    upper_32_bits(u64tmp));
449 
450 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG1_LOW_IDX,
451 				    lower_32_bits(fpdu_info->frags[0].physaddr));
452 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG1_HIGH_IDX,
453 				    upper_32_bits(fpdu_info->frags[0].physaddr));
454 
455 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG2_LOW_IDX,
456 				    lower_32_bits(fpdu_info->frags[1].physaddr));
457 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG2_HIGH_IDX,
458 				    upper_32_bits(fpdu_info->frags[1].physaddr));
459 
460 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG3_LOW_IDX,
461 				    lower_32_bits(fpdu_info->frags[2].physaddr));
462 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG3_HIGH_IDX,
463 				    upper_32_bits(fpdu_info->frags[2].physaddr));
464 
465 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG4_LOW_IDX,
466 				    lower_32_bits(fpdu_info->frags[3].physaddr));
467 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG4_HIGH_IDX,
468 				    upper_32_bits(fpdu_info->frags[3].physaddr));
469 
470 		cqp_request->cqp_callback_pointer = fpdu_info;
471 		cqp_request->callback = 1;
472 		cqp_request->cqp_callback = nes_download_callback;
473 
474 		atomic_set(&cqp_request->refcount, 1);
475 		nes_post_cqp_request(nesdev, cqp_request);
476 		spin_unlock_irqrestore(&nesqp->pau_lock, flags);
477 	}
478 
479 	return 0;
480 }
481 
process_fpdus(struct nes_vnic * nesvnic,struct nes_qp * nesqp)482 static void process_fpdus(struct nes_vnic *nesvnic, struct nes_qp *nesqp)
483 {
484 	int again = 1;
485 	unsigned long flags;
486 
487 	do {
488 		/* Ignore rc - if it failed, tcp retries will cause it to try again */
489 		forward_fpdus(nesvnic, nesqp);
490 
491 		spin_lock_irqsave(&nesqp->pau_lock, flags);
492 		if (nesqp->pau_pending) {
493 			nesqp->pau_pending = 0;
494 		} else {
495 			nesqp->pau_busy = 0;
496 			again = 0;
497 		}
498 
499 		spin_unlock_irqrestore(&nesqp->pau_lock, flags);
500 	} while (again);
501 }
502 
503 /**
504  * queue_fpdus - Handle fpdu's that hw passed up to sw
505  */
queue_fpdus(struct sk_buff * skb,struct nes_vnic * nesvnic,struct nes_qp * nesqp)506 static void queue_fpdus(struct sk_buff *skb, struct nes_vnic *nesvnic, struct nes_qp *nesqp)
507 {
508 	struct sk_buff *tmpskb;
509 	struct nes_rskb_cb *cb;
510 	struct iphdr *iph;
511 	struct tcphdr *tcph;
512 	unsigned char *tcph_end;
513 	u32 rcv_nxt;
514 	u32 rcv_wnd;
515 	u32 seqnum;
516 	u32 len;
517 	bool process_it = false;
518 	unsigned long flags;
519 
520 	/* Move data ptr to after tcp header */
521 	iph = (struct iphdr *)skb->data;
522 	tcph = (struct tcphdr *)(((char *)iph) + (4 * iph->ihl));
523 	seqnum = be32_to_cpu(tcph->seq);
524 	tcph_end = (((char *)tcph) + (4 * tcph->doff));
525 
526 	len = be16_to_cpu(iph->tot_len);
527 	if (skb->len > len)
528 		skb_trim(skb, len);
529 	skb_pull(skb, tcph_end - skb->data);
530 
531 	/* Initialize tracking values */
532 	cb = (struct nes_rskb_cb *)&skb->cb[0];
533 	cb->seqnum = seqnum;
534 
535 	/* Make sure data is in the receive window */
536 	rcv_nxt = nesqp->pau_rcv_nxt;
537 	rcv_wnd = le32_to_cpu(nesqp->nesqp_context->rcv_wnd);
538 	if (!between(seqnum, rcv_nxt, (rcv_nxt + rcv_wnd))) {
539 		nes_mgt_free_skb(nesvnic->nesdev, skb, PCI_DMA_TODEVICE);
540 		nes_rem_ref_cm_node(nesqp->cm_node);
541 		return;
542 	}
543 
544 	spin_lock_irqsave(&nesqp->pau_lock, flags);
545 
546 	if (nesqp->pau_busy)
547 		nesqp->pau_pending = 1;
548 	else
549 		nesqp->pau_busy = 1;
550 
551 	/* Queue skb by sequence number */
552 	if (skb_queue_len(&nesqp->pau_list) == 0) {
553 		skb_queue_head(&nesqp->pau_list, skb);
554 	} else {
555 		tmpskb = nesqp->pau_list.next;
556 		while (tmpskb != (struct sk_buff *)&nesqp->pau_list) {
557 			cb = (struct nes_rskb_cb *)&tmpskb->cb[0];
558 			if (before(seqnum, cb->seqnum))
559 				break;
560 			tmpskb = tmpskb->next;
561 		}
562 		skb_insert(tmpskb, skb, &nesqp->pau_list);
563 	}
564 	if (nesqp->pau_state == PAU_READY)
565 		process_it = true;
566 	spin_unlock_irqrestore(&nesqp->pau_lock, flags);
567 
568 	if (process_it)
569 		process_fpdus(nesvnic, nesqp);
570 
571 	return;
572 }
573 
574 /**
575  * mgt_thread - Handle mgt skbs in a safe context
576  */
mgt_thread(void * context)577 static int mgt_thread(void *context)
578 {
579 	struct nes_vnic *nesvnic = context;
580 	struct sk_buff *skb;
581 	struct nes_rskb_cb *cb;
582 
583 	while (!kthread_should_stop()) {
584 		wait_event_interruptible(nesvnic->mgt_wait_queue,
585 					 skb_queue_len(&nesvnic->mgt_skb_list) || kthread_should_stop());
586 		while ((skb_queue_len(&nesvnic->mgt_skb_list)) && !kthread_should_stop()) {
587 			skb = skb_dequeue(&nesvnic->mgt_skb_list);
588 			cb = (struct nes_rskb_cb *)&skb->cb[0];
589 			cb->data_start = skb->data - ETH_HLEN;
590 			cb->busaddr = pci_map_single(nesvnic->nesdev->pcidev, cb->data_start,
591 						     nesvnic->max_frame_size, PCI_DMA_TODEVICE);
592 			queue_fpdus(skb, nesvnic, cb->nesqp);
593 		}
594 	}
595 
596 	/* Closing down so delete any entries on the queue */
597 	while (skb_queue_len(&nesvnic->mgt_skb_list)) {
598 		skb = skb_dequeue(&nesvnic->mgt_skb_list);
599 		cb = (struct nes_rskb_cb *)&skb->cb[0];
600 		nes_rem_ref_cm_node(cb->nesqp->cm_node);
601 		dev_kfree_skb_any(skb);
602 	}
603 	return 0;
604 }
605 
606 /**
607  * nes_queue_skbs - Queue skb so it can be handled in a thread context
608  */
nes_queue_mgt_skbs(struct sk_buff * skb,struct nes_vnic * nesvnic,struct nes_qp * nesqp)609 void nes_queue_mgt_skbs(struct sk_buff *skb, struct nes_vnic *nesvnic, struct nes_qp *nesqp)
610 {
611 	struct nes_rskb_cb *cb;
612 
613 	cb = (struct nes_rskb_cb *)&skb->cb[0];
614 	cb->nesqp = nesqp;
615 	skb_queue_tail(&nesvnic->mgt_skb_list, skb);
616 	wake_up_interruptible(&nesvnic->mgt_wait_queue);
617 }
618 
nes_destroy_pau_qp(struct nes_device * nesdev,struct nes_qp * nesqp)619 void nes_destroy_pau_qp(struct nes_device *nesdev, struct nes_qp *nesqp)
620 {
621 	struct sk_buff *skb;
622 	unsigned long flags;
623 	atomic_inc(&pau_qps_destroyed);
624 
625 	/* Free packets that have not yet been forwarded */
626 	/* Lock is acquired by skb_dequeue when removing the skb */
627 	spin_lock_irqsave(&nesqp->pau_lock, flags);
628 	while (skb_queue_len(&nesqp->pau_list)) {
629 		skb = skb_dequeue(&nesqp->pau_list);
630 		nes_mgt_free_skb(nesdev, skb, PCI_DMA_TODEVICE);
631 		nes_rem_ref_cm_node(nesqp->cm_node);
632 	}
633 	spin_unlock_irqrestore(&nesqp->pau_lock, flags);
634 }
635 
nes_chg_qh_handler(struct nes_device * nesdev,struct nes_cqp_request * cqp_request)636 static void nes_chg_qh_handler(struct nes_device *nesdev, struct nes_cqp_request *cqp_request)
637 {
638 	struct pau_qh_chg *qh_chg = cqp_request->cqp_callback_pointer;
639 	struct nes_cqp_request *new_request;
640 	struct nes_hw_cqp_wqe *cqp_wqe;
641 	struct nes_adapter *nesadapter;
642 	struct nes_qp *nesqp;
643 	struct nes_v4_quad nes_quad;
644 	u32 crc_value;
645 	u64 u64temp;
646 
647 	nesadapter = nesdev->nesadapter;
648 	nesqp = qh_chg->nesqp;
649 
650 	/* Should we handle the bad completion */
651 	if (cqp_request->major_code)
652 		WARN(1, PFX "Invalid cqp_request major_code=0x%x\n",
653 		       cqp_request->major_code);
654 
655 	switch (nesqp->pau_state) {
656 	case PAU_DEL_QH:
657 		/* Old hash code deleted, now set the new one */
658 		nesqp->pau_state = PAU_ADD_LB_QH;
659 		new_request = nes_get_cqp_request(nesdev);
660 		if (new_request == NULL) {
661 			nes_debug(NES_DBG_PAU, "Failed to get a new_request.\n");
662 			WARN_ON(1);
663 			return;
664 		}
665 
666 		memset(&nes_quad, 0, sizeof(nes_quad));
667 		nes_quad.DstIpAdrIndex =
668 			cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
669 		nes_quad.SrcIpadr = cpu_to_be32(0x7f000001);
670 		nes_quad.TcpPorts[0] = swab16(nesqp->nesqp_context->tcpPorts[1]);
671 		nes_quad.TcpPorts[1] = swab16(nesqp->nesqp_context->tcpPorts[0]);
672 
673 		/* Produce hash key */
674 		crc_value = get_crc_value(&nes_quad);
675 		nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
676 		nes_debug(NES_DBG_PAU, "new HTE Index = 0x%08X, CRC = 0x%08X\n",
677 			  nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
678 
679 		nesqp->hte_index &= nesadapter->hte_index_mask;
680 		nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
681 		nesqp->nesqp_context->ip0 = cpu_to_le32(0x7f000001);
682 		nesqp->nesqp_context->rcv_nxt = cpu_to_le32(nesqp->pau_rcv_nxt);
683 
684 		cqp_wqe = &new_request->cqp_wqe;
685 		nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
686 		set_wqe_32bit_value(cqp_wqe->wqe_words,
687 				    NES_CQP_WQE_OPCODE_IDX, NES_CQP_MANAGE_QUAD_HASH |
688 				    NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_CONTEXT_VALID | NES_CQP_QP_IWARP_STATE_RTS);
689 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
690 		u64temp = (u64)nesqp->nesqp_context_pbase;
691 		set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
692 
693 		nes_debug(NES_DBG_PAU, "Waiting for CQP completion for adding the quad hash.\n");
694 
695 		new_request->cqp_callback_pointer = qh_chg;
696 		new_request->callback = 1;
697 		new_request->cqp_callback = nes_chg_qh_handler;
698 		atomic_set(&new_request->refcount, 1);
699 		nes_post_cqp_request(nesdev, new_request);
700 		break;
701 
702 	case PAU_ADD_LB_QH:
703 		/* Start processing the queued fpdu's */
704 		nesqp->pau_state = PAU_READY;
705 		process_fpdus(qh_chg->nesvnic, qh_chg->nesqp);
706 		kfree(qh_chg);
707 		break;
708 	}
709 }
710 
711 /**
712  * nes_change_quad_hash
713  */
nes_change_quad_hash(struct nes_device * nesdev,struct nes_vnic * nesvnic,struct nes_qp * nesqp)714 static int nes_change_quad_hash(struct nes_device *nesdev,
715 				struct nes_vnic *nesvnic, struct nes_qp *nesqp)
716 {
717 	struct nes_cqp_request *cqp_request = NULL;
718 	struct pau_qh_chg *qh_chg = NULL;
719 	u64 u64temp;
720 	struct nes_hw_cqp_wqe *cqp_wqe;
721 	int ret = 0;
722 
723 	cqp_request = nes_get_cqp_request(nesdev);
724 	if (cqp_request == NULL) {
725 		nes_debug(NES_DBG_PAU, "Failed to get a cqp_request.\n");
726 		ret = -ENOMEM;
727 		goto chg_qh_err;
728 	}
729 
730 	qh_chg = kmalloc(sizeof *qh_chg, GFP_ATOMIC);
731 	if (!qh_chg) {
732 		ret = -ENOMEM;
733 		goto chg_qh_err;
734 	}
735 	qh_chg->nesdev = nesdev;
736 	qh_chg->nesvnic = nesvnic;
737 	qh_chg->nesqp = nesqp;
738 	nesqp->pau_state = PAU_DEL_QH;
739 
740 	cqp_wqe = &cqp_request->cqp_wqe;
741 	nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
742 	set_wqe_32bit_value(cqp_wqe->wqe_words,
743 			    NES_CQP_WQE_OPCODE_IDX, NES_CQP_MANAGE_QUAD_HASH | NES_CQP_QP_DEL_HTE |
744 			    NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_CONTEXT_VALID | NES_CQP_QP_IWARP_STATE_RTS);
745 	set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
746 	u64temp = (u64)nesqp->nesqp_context_pbase;
747 	set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
748 
749 	nes_debug(NES_DBG_PAU, "Waiting for CQP completion for deleting the quad hash.\n");
750 
751 	cqp_request->cqp_callback_pointer = qh_chg;
752 	cqp_request->callback = 1;
753 	cqp_request->cqp_callback = nes_chg_qh_handler;
754 	atomic_set(&cqp_request->refcount, 1);
755 	nes_post_cqp_request(nesdev, cqp_request);
756 
757 	return ret;
758 
759 chg_qh_err:
760 	kfree(qh_chg);
761 	if (cqp_request)
762 		nes_put_cqp_request(nesdev, cqp_request);
763 	return ret;
764 }
765 
766 /**
767  * nes_mgt_ce_handler
768  * This management code deals with any packed and unaligned (pau) fpdu's
769  * that the hardware cannot handle.
770  */
nes_mgt_ce_handler(struct nes_device * nesdev,struct nes_hw_nic_cq * cq)771 static void nes_mgt_ce_handler(struct nes_device *nesdev, struct nes_hw_nic_cq *cq)
772 {
773 	struct nes_vnic_mgt *mgtvnic = container_of(cq, struct nes_vnic_mgt, mgt_cq);
774 	struct nes_adapter *nesadapter = nesdev->nesadapter;
775 	u32 head;
776 	u32 cq_size;
777 	u32 cqe_count = 0;
778 	u32 cqe_misc;
779 	u32 qp_id = 0;
780 	u32 skbs_needed;
781 	unsigned long context;
782 	struct nes_qp *nesqp;
783 	struct sk_buff *rx_skb;
784 	struct nes_rskb_cb *cb;
785 
786 	head = cq->cq_head;
787 	cq_size = cq->cq_size;
788 
789 	while (1) {
790 		cqe_misc = le32_to_cpu(cq->cq_vbase[head].cqe_words[NES_NIC_CQE_MISC_IDX]);
791 		if (!(cqe_misc & NES_NIC_CQE_VALID))
792 			break;
793 
794 		nesqp = NULL;
795 		if (cqe_misc & NES_NIC_CQE_ACCQP_VALID) {
796 			qp_id = le32_to_cpu(cq->cq_vbase[head].cqe_words[NES_NIC_CQE_ACCQP_ID_IDX]);
797 			qp_id &= 0x001fffff;
798 			if (qp_id < nesadapter->max_qp) {
799 				context = (unsigned long)nesadapter->qp_table[qp_id - NES_FIRST_QPN];
800 				nesqp = (struct nes_qp *)context;
801 			}
802 		}
803 
804 		if (nesqp) {
805 			if (nesqp->pau_mode == false) {
806 				nesqp->pau_mode = true; /* First time for this qp */
807 				nesqp->pau_rcv_nxt = le32_to_cpu(
808 					cq->cq_vbase[head].cqe_words[NES_NIC_CQE_HASH_RCVNXT]);
809 				skb_queue_head_init(&nesqp->pau_list);
810 				spin_lock_init(&nesqp->pau_lock);
811 				atomic_inc(&pau_qps_created);
812 				nes_change_quad_hash(nesdev, mgtvnic->nesvnic, nesqp);
813 			}
814 
815 			rx_skb = mgtvnic->mgt.rx_skb[mgtvnic->mgt.rq_tail];
816 			rx_skb->len = 0;
817 			skb_put(rx_skb, cqe_misc & 0x0000ffff);
818 			rx_skb->protocol = eth_type_trans(rx_skb, mgtvnic->nesvnic->netdev);
819 			cb = (struct nes_rskb_cb *)&rx_skb->cb[0];
820 			pci_unmap_single(nesdev->pcidev, cb->busaddr, cb->maplen, PCI_DMA_FROMDEVICE);
821 			cb->busaddr = 0;
822 			mgtvnic->mgt.rq_tail++;
823 			mgtvnic->mgt.rq_tail &= mgtvnic->mgt.rq_size - 1;
824 
825 			nes_add_ref_cm_node(nesqp->cm_node);
826 			nes_queue_mgt_skbs(rx_skb, mgtvnic->nesvnic, nesqp);
827 		} else {
828 			printk(KERN_ERR PFX "Invalid QP %d for packed/unaligned handling\n", qp_id);
829 		}
830 
831 		cq->cq_vbase[head].cqe_words[NES_NIC_CQE_MISC_IDX] = 0;
832 		cqe_count++;
833 		if (++head >= cq_size)
834 			head = 0;
835 
836 		if (cqe_count == 255) {
837 			/* Replenish mgt CQ */
838 			nes_write32(nesdev->regs + NES_CQE_ALLOC, cq->cq_number | (cqe_count << 16));
839 			nesdev->currcq_count += cqe_count;
840 			cqe_count = 0;
841 		}
842 
843 		skbs_needed = atomic_inc_return(&mgtvnic->rx_skbs_needed);
844 		if (skbs_needed > (mgtvnic->mgt.rq_size >> 1))
845 			nes_replenish_mgt_rq(mgtvnic);
846 	}
847 
848 	cq->cq_head = head;
849 	nes_write32(nesdev->regs + NES_CQE_ALLOC, NES_CQE_ALLOC_NOTIFY_NEXT |
850 		    cq->cq_number | (cqe_count << 16));
851 	nes_read32(nesdev->regs + NES_CQE_ALLOC);
852 	nesdev->currcq_count += cqe_count;
853 }
854 
855 /**
856  * nes_init_mgt_qp
857  */
nes_init_mgt_qp(struct nes_device * nesdev,struct net_device * netdev,struct nes_vnic * nesvnic)858 int nes_init_mgt_qp(struct nes_device *nesdev, struct net_device *netdev, struct nes_vnic *nesvnic)
859 {
860 	struct nes_vnic_mgt *mgtvnic;
861 	u32 counter;
862 	void *vmem;
863 	dma_addr_t pmem;
864 	struct nes_hw_cqp_wqe *cqp_wqe;
865 	u32 cqp_head;
866 	unsigned long flags;
867 	struct nes_hw_nic_qp_context *mgt_context;
868 	u64 u64temp;
869 	struct nes_hw_nic_rq_wqe *mgt_rqe;
870 	struct sk_buff *skb;
871 	u32 wqe_count;
872 	struct nes_rskb_cb *cb;
873 	u32 mgt_mem_size;
874 	void *mgt_vbase;
875 	dma_addr_t mgt_pbase;
876 	int i;
877 	int ret;
878 
879 	/* Allocate space the all mgt QPs once */
880 	mgtvnic = kzalloc(NES_MGT_QP_COUNT * sizeof(struct nes_vnic_mgt), GFP_KERNEL);
881 	if (!mgtvnic)
882 		return -ENOMEM;
883 
884 	/* Allocate fragment, RQ, and CQ; Reuse CEQ based on the PCI function */
885 	/* We are not sending from this NIC so sq is not allocated */
886 	mgt_mem_size = 256 +
887 		       (NES_MGT_WQ_COUNT * sizeof(struct nes_hw_nic_rq_wqe)) +
888 		       (NES_MGT_WQ_COUNT * sizeof(struct nes_hw_nic_cqe)) +
889 		       sizeof(struct nes_hw_nic_qp_context);
890 	mgt_mem_size = (mgt_mem_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
891 	mgt_vbase = pci_alloc_consistent(nesdev->pcidev, NES_MGT_QP_COUNT * mgt_mem_size, &mgt_pbase);
892 	if (!mgt_vbase) {
893 		kfree(mgtvnic);
894 		nes_debug(NES_DBG_INIT, "Unable to allocate memory for mgt host descriptor rings\n");
895 		return -ENOMEM;
896 	}
897 
898 	nesvnic->mgt_mem_size = NES_MGT_QP_COUNT * mgt_mem_size;
899 	nesvnic->mgt_vbase = mgt_vbase;
900 	nesvnic->mgt_pbase = mgt_pbase;
901 
902 	skb_queue_head_init(&nesvnic->mgt_skb_list);
903 	init_waitqueue_head(&nesvnic->mgt_wait_queue);
904 	nesvnic->mgt_thread = kthread_run(mgt_thread, nesvnic, "nes_mgt_thread");
905 
906 	for (i = 0; i < NES_MGT_QP_COUNT; i++) {
907 		mgtvnic->nesvnic = nesvnic;
908 		mgtvnic->mgt.qp_id = nesdev->mac_index + NES_MGT_QP_OFFSET + i;
909 		memset(mgt_vbase, 0, mgt_mem_size);
910 		nes_debug(NES_DBG_INIT, "Allocated mgt QP structures at %p (phys = %016lX), size = %u.\n",
911 			  mgt_vbase, (unsigned long)mgt_pbase, mgt_mem_size);
912 
913 		vmem = (void *)(((unsigned long)mgt_vbase + (256 - 1)) &
914 				~(unsigned long)(256 - 1));
915 		pmem = (dma_addr_t)(((unsigned long long)mgt_pbase + (256 - 1)) &
916 				    ~(unsigned long long)(256 - 1));
917 
918 		spin_lock_init(&mgtvnic->mgt.rq_lock);
919 
920 		/* setup the RQ */
921 		mgtvnic->mgt.rq_vbase = vmem;
922 		mgtvnic->mgt.rq_pbase = pmem;
923 		mgtvnic->mgt.rq_head = 0;
924 		mgtvnic->mgt.rq_tail = 0;
925 		mgtvnic->mgt.rq_size = NES_MGT_WQ_COUNT;
926 
927 		/* setup the CQ */
928 		vmem += (NES_MGT_WQ_COUNT * sizeof(struct nes_hw_nic_rq_wqe));
929 		pmem += (NES_MGT_WQ_COUNT * sizeof(struct nes_hw_nic_rq_wqe));
930 
931 		mgtvnic->mgt_cq.cq_number = mgtvnic->mgt.qp_id;
932 		mgtvnic->mgt_cq.cq_vbase = vmem;
933 		mgtvnic->mgt_cq.cq_pbase = pmem;
934 		mgtvnic->mgt_cq.cq_head = 0;
935 		mgtvnic->mgt_cq.cq_size = NES_MGT_WQ_COUNT;
936 
937 		mgtvnic->mgt_cq.ce_handler = nes_mgt_ce_handler;
938 
939 		/* Send CreateCQ request to CQP */
940 		spin_lock_irqsave(&nesdev->cqp.lock, flags);
941 		cqp_head = nesdev->cqp.sq_head;
942 
943 		cqp_wqe = &nesdev->cqp.sq_vbase[cqp_head];
944 		nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
945 
946 		cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] = cpu_to_le32(
947 			NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
948 			((u32)mgtvnic->mgt_cq.cq_size << 16));
949 		cqp_wqe->wqe_words[NES_CQP_WQE_ID_IDX] = cpu_to_le32(
950 			mgtvnic->mgt_cq.cq_number | ((u32)nesdev->ceq_index << 16));
951 		u64temp = (u64)mgtvnic->mgt_cq.cq_pbase;
952 		set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
953 		cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
954 		u64temp = (unsigned long)&mgtvnic->mgt_cq;
955 		cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] = cpu_to_le32((u32)(u64temp >> 1));
956 		cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
957 			cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
958 		cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
959 
960 		if (++cqp_head >= nesdev->cqp.sq_size)
961 			cqp_head = 0;
962 		cqp_wqe = &nesdev->cqp.sq_vbase[cqp_head];
963 		nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
964 
965 		/* Send CreateQP request to CQP */
966 		mgt_context = (void *)(&mgtvnic->mgt_cq.cq_vbase[mgtvnic->mgt_cq.cq_size]);
967 		mgt_context->context_words[NES_NIC_CTX_MISC_IDX] =
968 			cpu_to_le32((u32)NES_MGT_CTX_SIZE |
969 				    ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 12));
970 		nes_debug(NES_DBG_INIT, "RX_WINDOW_BUFFER_PAGE_TABLE_SIZE = 0x%08X, RX_WINDOW_BUFFER_SIZE = 0x%08X\n",
971 			  nes_read_indexed(nesdev, NES_IDX_RX_WINDOW_BUFFER_PAGE_TABLE_SIZE),
972 			  nes_read_indexed(nesdev, NES_IDX_RX_WINDOW_BUFFER_SIZE));
973 		if (nes_read_indexed(nesdev, NES_IDX_RX_WINDOW_BUFFER_SIZE) != 0)
974 			mgt_context->context_words[NES_NIC_CTX_MISC_IDX] |= cpu_to_le32(NES_NIC_BACK_STORE);
975 
976 		u64temp = (u64)mgtvnic->mgt.rq_pbase;
977 		mgt_context->context_words[NES_NIC_CTX_SQ_LOW_IDX] = cpu_to_le32((u32)u64temp);
978 		mgt_context->context_words[NES_NIC_CTX_SQ_HIGH_IDX] = cpu_to_le32((u32)(u64temp >> 32));
979 		u64temp = (u64)mgtvnic->mgt.rq_pbase;
980 		mgt_context->context_words[NES_NIC_CTX_RQ_LOW_IDX] = cpu_to_le32((u32)u64temp);
981 		mgt_context->context_words[NES_NIC_CTX_RQ_HIGH_IDX] = cpu_to_le32((u32)(u64temp >> 32));
982 
983 		cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] = cpu_to_le32(NES_CQP_CREATE_QP |
984 									 NES_CQP_QP_TYPE_NIC);
985 		cqp_wqe->wqe_words[NES_CQP_WQE_ID_IDX] = cpu_to_le32(mgtvnic->mgt.qp_id);
986 		u64temp = (u64)mgtvnic->mgt_cq.cq_pbase +
987 			  (mgtvnic->mgt_cq.cq_size * sizeof(struct nes_hw_nic_cqe));
988 		set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
989 
990 		if (++cqp_head >= nesdev->cqp.sq_size)
991 			cqp_head = 0;
992 		nesdev->cqp.sq_head = cqp_head;
993 
994 		barrier();
995 
996 		/* Ring doorbell (2 WQEs) */
997 		nes_write32(nesdev->regs + NES_WQE_ALLOC, 0x02800000 | nesdev->cqp.qp_id);
998 
999 		spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1000 		nes_debug(NES_DBG_INIT, "Waiting for create MGT QP%u to complete.\n",
1001 			  mgtvnic->mgt.qp_id);
1002 
1003 		ret = wait_event_timeout(nesdev->cqp.waitq, (nesdev->cqp.sq_tail == cqp_head),
1004 					 NES_EVENT_TIMEOUT);
1005 		nes_debug(NES_DBG_INIT, "Create MGT QP%u completed, wait_event_timeout ret = %u.\n",
1006 			  mgtvnic->mgt.qp_id, ret);
1007 		if (!ret) {
1008 			nes_debug(NES_DBG_INIT, "MGT QP%u create timeout expired\n", mgtvnic->mgt.qp_id);
1009 			if (i == 0) {
1010 				pci_free_consistent(nesdev->pcidev, nesvnic->mgt_mem_size, nesvnic->mgt_vbase,
1011 						    nesvnic->mgt_pbase);
1012 				kfree(mgtvnic);
1013 			} else {
1014 				nes_destroy_mgt(nesvnic);
1015 			}
1016 			return -EIO;
1017 		}
1018 
1019 		/* Populate the RQ */
1020 		for (counter = 0; counter < (NES_MGT_WQ_COUNT - 1); counter++) {
1021 			skb = dev_alloc_skb(nesvnic->max_frame_size);
1022 			if (!skb) {
1023 				nes_debug(NES_DBG_INIT, "%s: out of memory for receive skb\n", netdev->name);
1024 				return -ENOMEM;
1025 			}
1026 
1027 			skb->dev = netdev;
1028 
1029 			pmem = pci_map_single(nesdev->pcidev, skb->data,
1030 					      nesvnic->max_frame_size, PCI_DMA_FROMDEVICE);
1031 			cb = (struct nes_rskb_cb *)&skb->cb[0];
1032 			cb->busaddr = pmem;
1033 			cb->maplen = nesvnic->max_frame_size;
1034 
1035 			mgt_rqe = &mgtvnic->mgt.rq_vbase[counter];
1036 			mgt_rqe->wqe_words[NES_NIC_RQ_WQE_LENGTH_1_0_IDX] = cpu_to_le32((u32)nesvnic->max_frame_size);
1037 			mgt_rqe->wqe_words[NES_NIC_RQ_WQE_LENGTH_3_2_IDX] = 0;
1038 			mgt_rqe->wqe_words[NES_NIC_RQ_WQE_FRAG0_LOW_IDX] = cpu_to_le32((u32)pmem);
1039 			mgt_rqe->wqe_words[NES_NIC_RQ_WQE_FRAG0_HIGH_IDX] = cpu_to_le32((u32)((u64)pmem >> 32));
1040 			mgtvnic->mgt.rx_skb[counter] = skb;
1041 		}
1042 
1043 		setup_timer(&mgtvnic->rq_wqes_timer, nes_mgt_rq_wqes_timeout,
1044 			    (unsigned long)mgtvnic);
1045 
1046 		wqe_count = NES_MGT_WQ_COUNT - 1;
1047 		mgtvnic->mgt.rq_head = wqe_count;
1048 		barrier();
1049 		do {
1050 			counter = min(wqe_count, ((u32)255));
1051 			wqe_count -= counter;
1052 			nes_write32(nesdev->regs + NES_WQE_ALLOC, (counter << 24) | mgtvnic->mgt.qp_id);
1053 		} while (wqe_count);
1054 
1055 		nes_write32(nesdev->regs + NES_CQE_ALLOC, NES_CQE_ALLOC_NOTIFY_NEXT |
1056 			    mgtvnic->mgt_cq.cq_number);
1057 		nes_read32(nesdev->regs + NES_CQE_ALLOC);
1058 
1059 		mgt_vbase += mgt_mem_size;
1060 		mgt_pbase += mgt_mem_size;
1061 		nesvnic->mgtvnic[i] = mgtvnic++;
1062 	}
1063 	return 0;
1064 }
1065 
1066 
nes_destroy_mgt(struct nes_vnic * nesvnic)1067 void nes_destroy_mgt(struct nes_vnic *nesvnic)
1068 {
1069 	struct nes_device *nesdev = nesvnic->nesdev;
1070 	struct nes_vnic_mgt *mgtvnic;
1071 	struct nes_vnic_mgt *first_mgtvnic;
1072 	unsigned long flags;
1073 	struct nes_hw_cqp_wqe *cqp_wqe;
1074 	u32 cqp_head;
1075 	struct sk_buff *rx_skb;
1076 	int i;
1077 	int ret;
1078 
1079 	kthread_stop(nesvnic->mgt_thread);
1080 
1081 	/* Free remaining NIC receive buffers */
1082 	first_mgtvnic = nesvnic->mgtvnic[0];
1083 	for (i = 0; i < NES_MGT_QP_COUNT; i++) {
1084 		mgtvnic = nesvnic->mgtvnic[i];
1085 		if (mgtvnic == NULL)
1086 			continue;
1087 
1088 		while (mgtvnic->mgt.rq_head != mgtvnic->mgt.rq_tail) {
1089 			rx_skb = mgtvnic->mgt.rx_skb[mgtvnic->mgt.rq_tail];
1090 			nes_mgt_free_skb(nesdev, rx_skb, PCI_DMA_FROMDEVICE);
1091 			mgtvnic->mgt.rq_tail++;
1092 			mgtvnic->mgt.rq_tail &= (mgtvnic->mgt.rq_size - 1);
1093 		}
1094 
1095 		spin_lock_irqsave(&nesdev->cqp.lock, flags);
1096 
1097 		/* Destroy NIC QP */
1098 		cqp_head = nesdev->cqp.sq_head;
1099 		cqp_wqe = &nesdev->cqp.sq_vbase[cqp_head];
1100 		nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1101 
1102 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
1103 				    (NES_CQP_DESTROY_QP | NES_CQP_QP_TYPE_NIC));
1104 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1105 				    mgtvnic->mgt.qp_id);
1106 
1107 		if (++cqp_head >= nesdev->cqp.sq_size)
1108 			cqp_head = 0;
1109 
1110 		cqp_wqe = &nesdev->cqp.sq_vbase[cqp_head];
1111 
1112 		/* Destroy NIC CQ */
1113 		nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1114 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
1115 				    (NES_CQP_DESTROY_CQ | ((u32)mgtvnic->mgt_cq.cq_size << 16)));
1116 		set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1117 				    (mgtvnic->mgt_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1118 
1119 		if (++cqp_head >= nesdev->cqp.sq_size)
1120 			cqp_head = 0;
1121 
1122 		nesdev->cqp.sq_head = cqp_head;
1123 		barrier();
1124 
1125 		/* Ring doorbell (2 WQEs) */
1126 		nes_write32(nesdev->regs + NES_WQE_ALLOC, 0x02800000 | nesdev->cqp.qp_id);
1127 
1128 		spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1129 		nes_debug(NES_DBG_SHUTDOWN, "Waiting for CQP, cqp_head=%u, cqp.sq_head=%u,"
1130 			  " cqp.sq_tail=%u, cqp.sq_size=%u\n",
1131 			  cqp_head, nesdev->cqp.sq_head,
1132 			  nesdev->cqp.sq_tail, nesdev->cqp.sq_size);
1133 
1134 		ret = wait_event_timeout(nesdev->cqp.waitq, (nesdev->cqp.sq_tail == cqp_head),
1135 					 NES_EVENT_TIMEOUT);
1136 
1137 		nes_debug(NES_DBG_SHUTDOWN, "Destroy MGT QP returned, wait_event_timeout ret = %u, cqp_head=%u,"
1138 			  " cqp.sq_head=%u, cqp.sq_tail=%u\n",
1139 			  ret, cqp_head, nesdev->cqp.sq_head, nesdev->cqp.sq_tail);
1140 		if (!ret)
1141 			nes_debug(NES_DBG_SHUTDOWN, "MGT QP%u destroy timeout expired\n",
1142 				  mgtvnic->mgt.qp_id);
1143 
1144 		nesvnic->mgtvnic[i] = NULL;
1145 	}
1146 
1147 	if (nesvnic->mgt_vbase) {
1148 		pci_free_consistent(nesdev->pcidev, nesvnic->mgt_mem_size, nesvnic->mgt_vbase,
1149 				    nesvnic->mgt_pbase);
1150 		nesvnic->mgt_vbase = NULL;
1151 		nesvnic->mgt_pbase = 0;
1152 	}
1153 
1154 	kfree(first_mgtvnic);
1155 }
1156