uwb: Use kcalloc instead of kzalloc to allocate array
[zen-stable.git] / drivers / infiniband / hw / nes / nes_mgt.c
blobb3b2a240c6e9471410ea3b4a6286849186744c92
1 /*
2 * Copyright (c) 2006 - 2009 Intel-NE, Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
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"
43 atomic_t pau_qps_created;
44 atomic_t pau_qps_destroyed;
46 static void nes_replenish_mgt_rq(struct nes_vnic_mgt *mgtvnic)
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;
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);
70 return;
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;
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;
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;
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);
113 break;
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;
123 * nes_mgt_rq_wqes_timeout
125 static void nes_mgt_rq_wqes_timeout(unsigned long parm)
127 struct nes_vnic_mgt *mgtvnic = (struct nes_vnic_mgt *)parm;
129 atomic_set(&mgtvnic->rx_skb_timer_running, 0);
130 if (atomic_read(&mgtvnic->rx_skbs_needed))
131 nes_replenish_mgt_rq(mgtvnic);
135 * nes_mgt_free_skb - unmap and free skb
137 static void nes_mgt_free_skb(struct nes_device *nesdev, struct sk_buff *skb, u32 dir)
139 struct nes_rskb_cb *cb;
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);
148 * nes_download_callback - handle download completions
150 static void nes_download_callback(struct nes_device *nesdev, struct nes_cqp_request *cqp_request)
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;
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);
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);
172 * nes_get_seq - Get the seq, ack_seq and window from the packet
174 static u32 nes_get_seq(struct sk_buff *skb, u32 *ack, u16 *wnd, u32 *fin_rcvd, u32 *rst_rcvd)
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));
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);
188 * nes_get_next_skb - Get the next skb based on where current skb is in the queue
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)
194 u32 seq;
195 bool processacks;
196 struct sk_buff *old_skb;
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;
212 while (1) {
213 seq = nes_get_seq(skb, ack, wnd, fin_rcvd, rst_rcvd);
214 if (seq == nextseq) {
215 if (skb->len || processacks)
216 break;
217 } else if (after(seq, nextseq)) {
218 goto out;
221 if (skb->next == (struct sk_buff *)&nesqp->pau_list)
222 goto out;
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);
230 return skb;
232 out:
233 return NULL;
237 * get_fpdu_info - Find the next complete fpdu and return its fragments.
239 static int get_fpdu_info(struct nes_device *nesdev, struct nes_qp *nesqp,
240 struct pau_fpdu_info **pau_fpdu_info)
242 struct sk_buff *skb;
243 struct iphdr *iph;
244 struct tcphdr *tcph;
245 struct nes_rskb_cb *cb;
246 struct pau_fpdu_info *fpdu_info = NULL;
247 struct pau_fpdu_frag frags[MAX_FPDU_FRAGS];
248 unsigned long flags;
249 u32 fpdu_len = 0;
250 u32 tmp_len;
251 int frag_cnt = 0;
252 u32 tot_len;
253 u32 frag_tot;
254 u32 ack;
255 u32 fin_rcvd;
256 u32 rst_rcvd;
257 u16 wnd;
258 int i;
259 int rc = 0;
261 *pau_fpdu_info = NULL;
263 spin_lock_irqsave(&nesqp->pau_lock, flags);
264 skb = nes_get_next_skb(nesdev, nesqp, NULL, nesqp->pau_rcv_nxt, &ack, &wnd, &fin_rcvd, &rst_rcvd);
265 if (!skb) {
266 spin_unlock_irqrestore(&nesqp->pau_lock, flags);
267 goto out;
269 cb = (struct nes_rskb_cb *)&skb->cb[0];
270 if (skb->len) {
271 fpdu_len = be16_to_cpu(*(__be16 *) skb->data) + MPA_FRAMING;
272 fpdu_len = (fpdu_len + 3) & 0xfffffffc;
273 tmp_len = fpdu_len;
275 /* See if we have all of the fpdu */
276 frag_tot = 0;
277 memset(&frags, 0, sizeof frags);
278 for (i = 0; i < MAX_FPDU_FRAGS; i++) {
279 frags[i].physaddr = cb->busaddr;
280 frags[i].physaddr += skb->data - cb->data_start;
281 frags[i].frag_len = min(tmp_len, skb->len);
282 frags[i].skb = skb;
283 frags[i].cmplt = (skb->len == frags[i].frag_len);
284 frag_tot += frags[i].frag_len;
285 frag_cnt++;
287 tmp_len -= frags[i].frag_len;
288 if (tmp_len == 0)
289 break;
291 skb = nes_get_next_skb(nesdev, nesqp, skb,
292 nesqp->pau_rcv_nxt + frag_tot, &ack, &wnd, &fin_rcvd, &rst_rcvd);
293 if (!skb) {
294 spin_unlock_irqrestore(&nesqp->pau_lock, flags);
295 goto out;
296 } else if (rst_rcvd) {
297 /* rst received in the middle of fpdu */
298 for (; i >= 0; i--) {
299 skb_unlink(frags[i].skb, &nesqp->pau_list);
300 nes_mgt_free_skb(nesdev, frags[i].skb, PCI_DMA_TODEVICE);
302 cb = (struct nes_rskb_cb *)&skb->cb[0];
303 frags[0].physaddr = cb->busaddr;
304 frags[0].physaddr += skb->data - cb->data_start;
305 frags[0].frag_len = skb->len;
306 frags[0].skb = skb;
307 frags[0].cmplt = true;
308 frag_cnt = 1;
309 break;
312 cb = (struct nes_rskb_cb *)&skb->cb[0];
314 } else {
315 /* no data */
316 frags[0].physaddr = cb->busaddr;
317 frags[0].frag_len = 0;
318 frags[0].skb = skb;
319 frags[0].cmplt = true;
320 frag_cnt = 1;
323 spin_unlock_irqrestore(&nesqp->pau_lock, flags);
325 /* Found one */
326 fpdu_info = kzalloc(sizeof(*fpdu_info), GFP_ATOMIC);
327 if (fpdu_info == NULL) {
328 nes_debug(NES_DBG_PAU, "Failed to alloc a fpdu_info.\n");
329 rc = -ENOMEM;
330 goto out;
333 fpdu_info->cqp_request = nes_get_cqp_request(nesdev);
334 if (fpdu_info->cqp_request == NULL) {
335 nes_debug(NES_DBG_PAU, "Failed to get a cqp_request.\n");
336 rc = -ENOMEM;
337 goto out;
340 cb = (struct nes_rskb_cb *)&frags[0].skb->cb[0];
341 iph = (struct iphdr *)(cb->data_start + ETH_HLEN);
342 tcph = (struct tcphdr *)(((char *)iph) + (4 * iph->ihl));
343 fpdu_info->hdr_len = (((unsigned char *)tcph) + 4 * (tcph->doff)) - cb->data_start;
344 fpdu_info->data_len = fpdu_len;
345 tot_len = fpdu_info->hdr_len + fpdu_len - ETH_HLEN;
347 if (frags[0].cmplt) {
348 fpdu_info->hdr_pbase = cb->busaddr;
349 fpdu_info->hdr_vbase = NULL;
350 } else {
351 fpdu_info->hdr_vbase = pci_alloc_consistent(nesdev->pcidev,
352 fpdu_info->hdr_len, &fpdu_info->hdr_pbase);
353 if (!fpdu_info->hdr_vbase) {
354 nes_debug(NES_DBG_PAU, "Unable to allocate memory for pau first frag\n");
355 rc = -ENOMEM;
356 goto out;
359 /* Copy hdrs, adjusting len and seqnum */
360 memcpy(fpdu_info->hdr_vbase, cb->data_start, fpdu_info->hdr_len);
361 iph = (struct iphdr *)(fpdu_info->hdr_vbase + ETH_HLEN);
362 tcph = (struct tcphdr *)(((char *)iph) + (4 * iph->ihl));
365 iph->tot_len = cpu_to_be16(tot_len);
366 iph->saddr = cpu_to_be32(0x7f000001);
368 tcph->seq = cpu_to_be32(nesqp->pau_rcv_nxt);
369 tcph->ack_seq = cpu_to_be32(ack);
370 tcph->window = cpu_to_be16(wnd);
372 nesqp->pau_rcv_nxt += fpdu_len + fin_rcvd;
374 memcpy(fpdu_info->frags, frags, sizeof(fpdu_info->frags));
375 fpdu_info->frag_cnt = frag_cnt;
376 fpdu_info->nesqp = nesqp;
377 *pau_fpdu_info = fpdu_info;
379 /* Update skb's for next pass */
380 for (i = 0; i < frag_cnt; i++) {
381 cb = (struct nes_rskb_cb *)&frags[i].skb->cb[0];
382 skb_pull(frags[i].skb, frags[i].frag_len);
384 if (frags[i].skb->len == 0) {
385 /* Pull skb off the list - it will be freed in the callback */
386 spin_lock_irqsave(&nesqp->pau_lock, flags);
387 skb_unlink(frags[i].skb, &nesqp->pau_list);
388 spin_unlock_irqrestore(&nesqp->pau_lock, flags);
389 } else {
390 /* Last skb still has data so update the seq */
391 iph = (struct iphdr *)(cb->data_start + ETH_HLEN);
392 tcph = (struct tcphdr *)(((char *)iph) + (4 * iph->ihl));
393 tcph->seq = cpu_to_be32(nesqp->pau_rcv_nxt);
397 out:
398 if (rc) {
399 if (fpdu_info) {
400 if (fpdu_info->cqp_request)
401 nes_put_cqp_request(nesdev, fpdu_info->cqp_request);
402 kfree(fpdu_info);
405 return rc;
409 * forward_fpdu - send complete fpdus, one at a time
411 static int forward_fpdus(struct nes_vnic *nesvnic, struct nes_qp *nesqp)
413 struct nes_device *nesdev = nesvnic->nesdev;
414 struct pau_fpdu_info *fpdu_info;
415 struct nes_hw_cqp_wqe *cqp_wqe;
416 struct nes_cqp_request *cqp_request;
417 u64 u64tmp;
418 u32 u32tmp;
419 int rc;
421 while (1) {
422 rc = get_fpdu_info(nesdev, nesqp, &fpdu_info);
423 if (fpdu_info == NULL)
424 return rc;
426 cqp_request = fpdu_info->cqp_request;
427 cqp_wqe = &cqp_request->cqp_wqe;
428 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
429 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_DL_OPCODE_IDX,
430 NES_CQP_DOWNLOAD_SEGMENT |
431 (((u32)nesvnic->logical_port) << NES_CQP_OP_LOGICAL_PORT_SHIFT));
433 u32tmp = fpdu_info->hdr_len << 16;
434 u32tmp |= fpdu_info->hdr_len + (u32)fpdu_info->data_len;
435 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_DL_LENGTH_0_TOTAL_IDX,
436 u32tmp);
438 u32tmp = (fpdu_info->frags[1].frag_len << 16) | fpdu_info->frags[0].frag_len;
439 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_LENGTH_2_1_IDX,
440 u32tmp);
442 u32tmp = (fpdu_info->frags[3].frag_len << 16) | fpdu_info->frags[2].frag_len;
443 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_LENGTH_4_3_IDX,
444 u32tmp);
446 u64tmp = (u64)fpdu_info->hdr_pbase;
447 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG0_LOW_IDX,
448 lower_32_bits(u64tmp));
449 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG0_HIGH_IDX,
450 upper_32_bits(u64tmp >> 32));
452 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG1_LOW_IDX,
453 lower_32_bits(fpdu_info->frags[0].physaddr));
454 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG1_HIGH_IDX,
455 upper_32_bits(fpdu_info->frags[0].physaddr));
457 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG2_LOW_IDX,
458 lower_32_bits(fpdu_info->frags[1].physaddr));
459 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG2_HIGH_IDX,
460 upper_32_bits(fpdu_info->frags[1].physaddr));
462 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG3_LOW_IDX,
463 lower_32_bits(fpdu_info->frags[2].physaddr));
464 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG3_HIGH_IDX,
465 upper_32_bits(fpdu_info->frags[2].physaddr));
467 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG4_LOW_IDX,
468 lower_32_bits(fpdu_info->frags[3].physaddr));
469 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_NIC_SQ_WQE_FRAG4_HIGH_IDX,
470 upper_32_bits(fpdu_info->frags[3].physaddr));
472 cqp_request->cqp_callback_pointer = fpdu_info;
473 cqp_request->callback = 1;
474 cqp_request->cqp_callback = nes_download_callback;
476 atomic_set(&cqp_request->refcount, 1);
477 nes_post_cqp_request(nesdev, cqp_request);
480 return 0;
483 static void process_fpdus(struct nes_vnic *nesvnic, struct nes_qp *nesqp)
485 int again = 1;
486 unsigned long flags;
488 do {
489 /* Ignore rc - if it failed, tcp retries will cause it to try again */
490 forward_fpdus(nesvnic, nesqp);
492 spin_lock_irqsave(&nesqp->pau_lock, flags);
493 if (nesqp->pau_pending) {
494 nesqp->pau_pending = 0;
495 } else {
496 nesqp->pau_busy = 0;
497 again = 0;
500 spin_unlock_irqrestore(&nesqp->pau_lock, flags);
501 } while (again);
505 * queue_fpdus - Handle fpdu's that hw passed up to sw
507 static void queue_fpdus(struct sk_buff *skb, struct nes_vnic *nesvnic, struct nes_qp *nesqp)
509 struct sk_buff *tmpskb;
510 struct nes_rskb_cb *cb;
511 struct iphdr *iph;
512 struct tcphdr *tcph;
513 unsigned char *tcph_end;
514 u32 rcv_nxt;
515 u32 rcv_wnd;
516 u32 seqnum;
517 u32 len;
518 bool process_it = false;
519 unsigned long flags;
521 /* Move data ptr to after tcp header */
522 iph = (struct iphdr *)skb->data;
523 tcph = (struct tcphdr *)(((char *)iph) + (4 * iph->ihl));
524 seqnum = be32_to_cpu(tcph->seq);
525 tcph_end = (((char *)tcph) + (4 * tcph->doff));
527 len = be16_to_cpu(iph->tot_len);
528 if (skb->len > len)
529 skb_trim(skb, len);
530 skb_pull(skb, tcph_end - skb->data);
532 /* Initialize tracking values */
533 cb = (struct nes_rskb_cb *)&skb->cb[0];
534 cb->seqnum = seqnum;
536 /* Make sure data is in the receive window */
537 rcv_nxt = nesqp->pau_rcv_nxt;
538 rcv_wnd = le32_to_cpu(nesqp->nesqp_context->rcv_wnd);
539 if (!between(seqnum, rcv_nxt, (rcv_nxt + rcv_wnd))) {
540 nes_mgt_free_skb(nesvnic->nesdev, skb, PCI_DMA_TODEVICE);
541 nes_rem_ref_cm_node(nesqp->cm_node);
542 return;
545 spin_lock_irqsave(&nesqp->pau_lock, flags);
547 if (nesqp->pau_busy)
548 nesqp->pau_pending = 1;
549 else
550 nesqp->pau_busy = 1;
552 /* Queue skb by sequence number */
553 if (skb_queue_len(&nesqp->pau_list) == 0) {
554 skb_queue_head(&nesqp->pau_list, skb);
555 } else {
556 tmpskb = nesqp->pau_list.next;
557 while (tmpskb != (struct sk_buff *)&nesqp->pau_list) {
558 cb = (struct nes_rskb_cb *)&tmpskb->cb[0];
559 if (before(seqnum, cb->seqnum))
560 break;
561 tmpskb = tmpskb->next;
563 skb_insert(tmpskb, skb, &nesqp->pau_list);
565 if (nesqp->pau_state == PAU_READY)
566 process_it = true;
567 spin_unlock_irqrestore(&nesqp->pau_lock, flags);
569 if (process_it)
570 process_fpdus(nesvnic, nesqp);
572 return;
576 * mgt_thread - Handle mgt skbs in a safe context
578 static int mgt_thread(void *context)
580 struct nes_vnic *nesvnic = context;
581 struct sk_buff *skb;
582 struct nes_rskb_cb *cb;
584 while (!kthread_should_stop()) {
585 wait_event_interruptible(nesvnic->mgt_wait_queue,
586 skb_queue_len(&nesvnic->mgt_skb_list) || kthread_should_stop());
587 while ((skb_queue_len(&nesvnic->mgt_skb_list)) && !kthread_should_stop()) {
588 skb = skb_dequeue(&nesvnic->mgt_skb_list);
589 cb = (struct nes_rskb_cb *)&skb->cb[0];
590 cb->data_start = skb->data - ETH_HLEN;
591 cb->busaddr = pci_map_single(nesvnic->nesdev->pcidev, cb->data_start,
592 nesvnic->max_frame_size, PCI_DMA_TODEVICE);
593 queue_fpdus(skb, nesvnic, cb->nesqp);
597 /* Closing down so delete any entries on the queue */
598 while (skb_queue_len(&nesvnic->mgt_skb_list)) {
599 skb = skb_dequeue(&nesvnic->mgt_skb_list);
600 cb = (struct nes_rskb_cb *)&skb->cb[0];
601 nes_rem_ref_cm_node(cb->nesqp->cm_node);
602 dev_kfree_skb_any(skb);
604 return 0;
608 * nes_queue_skbs - Queue skb so it can be handled in a thread context
610 void nes_queue_mgt_skbs(struct sk_buff *skb, struct nes_vnic *nesvnic, struct nes_qp *nesqp)
612 struct nes_rskb_cb *cb;
614 cb = (struct nes_rskb_cb *)&skb->cb[0];
615 cb->nesqp = nesqp;
616 skb_queue_tail(&nesvnic->mgt_skb_list, skb);
617 wake_up_interruptible(&nesvnic->mgt_wait_queue);
620 void nes_destroy_pau_qp(struct nes_device *nesdev, struct nes_qp *nesqp)
622 struct sk_buff *skb;
623 unsigned long flags;
624 atomic_inc(&pau_qps_destroyed);
626 /* Free packets that have not yet been forwarded */
627 /* Lock is acquired by skb_dequeue when removing the skb */
628 spin_lock_irqsave(&nesqp->pau_lock, flags);
629 while (skb_queue_len(&nesqp->pau_list)) {
630 skb = skb_dequeue(&nesqp->pau_list);
631 nes_mgt_free_skb(nesdev, skb, PCI_DMA_TODEVICE);
632 nes_rem_ref_cm_node(nesqp->cm_node);
634 spin_unlock_irqrestore(&nesqp->pau_lock, flags);
637 static void nes_chg_qh_handler(struct nes_device *nesdev, struct nes_cqp_request *cqp_request)
639 struct pau_qh_chg *qh_chg = cqp_request->cqp_callback_pointer;
640 struct nes_cqp_request *new_request;
641 struct nes_hw_cqp_wqe *cqp_wqe;
642 struct nes_adapter *nesadapter;
643 struct nes_qp *nesqp;
644 struct nes_v4_quad nes_quad;
645 u32 crc_value;
646 u64 u64temp;
648 nesadapter = nesdev->nesadapter;
649 nesqp = qh_chg->nesqp;
651 /* Should we handle the bad completion */
652 if (cqp_request->major_code) {
653 printk(KERN_ERR PFX "Invalid cqp_request major_code=0x%x\n",
654 cqp_request->major_code);
655 WARN_ON(1);
658 switch (nesqp->pau_state) {
659 case PAU_DEL_QH:
660 /* Old hash code deleted, now set the new one */
661 nesqp->pau_state = PAU_ADD_LB_QH;
662 new_request = nes_get_cqp_request(nesdev);
663 if (new_request == NULL) {
664 nes_debug(NES_DBG_PAU, "Failed to get a new_request.\n");
665 WARN_ON(1);
666 return;
669 memset(&nes_quad, 0, sizeof(nes_quad));
670 nes_quad.DstIpAdrIndex =
671 cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) << 24);
672 nes_quad.SrcIpadr = cpu_to_be32(0x7f000001);
673 nes_quad.TcpPorts[0] = swab16(nesqp->nesqp_context->tcpPorts[1]);
674 nes_quad.TcpPorts[1] = swab16(nesqp->nesqp_context->tcpPorts[0]);
676 /* Produce hash key */
677 crc_value = get_crc_value(&nes_quad);
678 nesqp->hte_index = cpu_to_be32(crc_value ^ 0xffffffff);
679 nes_debug(NES_DBG_PAU, "new HTE Index = 0x%08X, CRC = 0x%08X\n",
680 nesqp->hte_index, nesqp->hte_index & nesadapter->hte_index_mask);
682 nesqp->hte_index &= nesadapter->hte_index_mask;
683 nesqp->nesqp_context->hte_index = cpu_to_le32(nesqp->hte_index);
684 nesqp->nesqp_context->ip0 = cpu_to_le32(0x7f000001);
685 nesqp->nesqp_context->rcv_nxt = cpu_to_le32(nesqp->pau_rcv_nxt);
687 cqp_wqe = &new_request->cqp_wqe;
688 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
689 set_wqe_32bit_value(cqp_wqe->wqe_words,
690 NES_CQP_WQE_OPCODE_IDX, NES_CQP_MANAGE_QUAD_HASH |
691 NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_CONTEXT_VALID | NES_CQP_QP_IWARP_STATE_RTS);
692 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
693 u64temp = (u64)nesqp->nesqp_context_pbase;
694 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
696 nes_debug(NES_DBG_PAU, "Waiting for CQP completion for adding the quad hash.\n");
698 new_request->cqp_callback_pointer = qh_chg;
699 new_request->callback = 1;
700 new_request->cqp_callback = nes_chg_qh_handler;
701 atomic_set(&new_request->refcount, 1);
702 nes_post_cqp_request(nesdev, new_request);
703 break;
705 case PAU_ADD_LB_QH:
706 /* Start processing the queued fpdu's */
707 nesqp->pau_state = PAU_READY;
708 process_fpdus(qh_chg->nesvnic, qh_chg->nesqp);
709 kfree(qh_chg);
710 break;
715 * nes_change_quad_hash
717 static int nes_change_quad_hash(struct nes_device *nesdev,
718 struct nes_vnic *nesvnic, struct nes_qp *nesqp)
720 struct nes_cqp_request *cqp_request = NULL;
721 struct pau_qh_chg *qh_chg = NULL;
722 u64 u64temp;
723 struct nes_hw_cqp_wqe *cqp_wqe;
724 int ret = 0;
726 cqp_request = nes_get_cqp_request(nesdev);
727 if (cqp_request == NULL) {
728 nes_debug(NES_DBG_PAU, "Failed to get a cqp_request.\n");
729 ret = -ENOMEM;
730 goto chg_qh_err;
733 qh_chg = kmalloc(sizeof *qh_chg, GFP_ATOMIC);
734 if (qh_chg == NULL) {
735 nes_debug(NES_DBG_PAU, "Failed to get a cqp_request.\n");
736 ret = -ENOMEM;
737 goto chg_qh_err;
739 qh_chg->nesdev = nesdev;
740 qh_chg->nesvnic = nesvnic;
741 qh_chg->nesqp = nesqp;
742 nesqp->pau_state = PAU_DEL_QH;
744 cqp_wqe = &cqp_request->cqp_wqe;
745 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
746 set_wqe_32bit_value(cqp_wqe->wqe_words,
747 NES_CQP_WQE_OPCODE_IDX, NES_CQP_MANAGE_QUAD_HASH | NES_CQP_QP_DEL_HTE |
748 NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_CONTEXT_VALID | NES_CQP_QP_IWARP_STATE_RTS);
749 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
750 u64temp = (u64)nesqp->nesqp_context_pbase;
751 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
753 nes_debug(NES_DBG_PAU, "Waiting for CQP completion for deleting the quad hash.\n");
755 cqp_request->cqp_callback_pointer = qh_chg;
756 cqp_request->callback = 1;
757 cqp_request->cqp_callback = nes_chg_qh_handler;
758 atomic_set(&cqp_request->refcount, 1);
759 nes_post_cqp_request(nesdev, cqp_request);
761 return ret;
763 chg_qh_err:
764 kfree(qh_chg);
765 if (cqp_request)
766 nes_put_cqp_request(nesdev, cqp_request);
767 return ret;
771 * nes_mgt_ce_handler
772 * This management code deals with any packed and unaligned (pau) fpdu's
773 * that the hardware cannot handle.
775 static void nes_mgt_ce_handler(struct nes_device *nesdev, struct nes_hw_nic_cq *cq)
777 struct nes_vnic_mgt *mgtvnic = container_of(cq, struct nes_vnic_mgt, mgt_cq);
778 struct nes_adapter *nesadapter = nesdev->nesadapter;
779 u32 head;
780 u32 cq_size;
781 u32 cqe_count = 0;
782 u32 cqe_misc;
783 u32 qp_id = 0;
784 u32 skbs_needed;
785 unsigned long context;
786 struct nes_qp *nesqp;
787 struct sk_buff *rx_skb;
788 struct nes_rskb_cb *cb;
790 head = cq->cq_head;
791 cq_size = cq->cq_size;
793 while (1) {
794 cqe_misc = le32_to_cpu(cq->cq_vbase[head].cqe_words[NES_NIC_CQE_MISC_IDX]);
795 if (!(cqe_misc & NES_NIC_CQE_VALID))
796 break;
798 nesqp = NULL;
799 if (cqe_misc & NES_NIC_CQE_ACCQP_VALID) {
800 qp_id = le32_to_cpu(cq->cq_vbase[head].cqe_words[NES_NIC_CQE_ACCQP_ID_IDX]);
801 qp_id &= 0x001fffff;
802 if (qp_id < nesadapter->max_qp) {
803 context = (unsigned long)nesadapter->qp_table[qp_id - NES_FIRST_QPN];
804 nesqp = (struct nes_qp *)context;
808 if (nesqp) {
809 if (nesqp->pau_mode == false) {
810 nesqp->pau_mode = true; /* First time for this qp */
811 nesqp->pau_rcv_nxt = le32_to_cpu(
812 cq->cq_vbase[head].cqe_words[NES_NIC_CQE_HASH_RCVNXT]);
813 skb_queue_head_init(&nesqp->pau_list);
814 spin_lock_init(&nesqp->pau_lock);
815 atomic_inc(&pau_qps_created);
816 nes_change_quad_hash(nesdev, mgtvnic->nesvnic, nesqp);
819 rx_skb = mgtvnic->mgt.rx_skb[mgtvnic->mgt.rq_tail];
820 rx_skb->len = 0;
821 skb_put(rx_skb, cqe_misc & 0x0000ffff);
822 rx_skb->protocol = eth_type_trans(rx_skb, mgtvnic->nesvnic->netdev);
823 cb = (struct nes_rskb_cb *)&rx_skb->cb[0];
824 pci_unmap_single(nesdev->pcidev, cb->busaddr, cb->maplen, PCI_DMA_FROMDEVICE);
825 cb->busaddr = 0;
826 mgtvnic->mgt.rq_tail++;
827 mgtvnic->mgt.rq_tail &= mgtvnic->mgt.rq_size - 1;
829 nes_add_ref_cm_node(nesqp->cm_node);
830 nes_queue_mgt_skbs(rx_skb, mgtvnic->nesvnic, nesqp);
831 } else {
832 printk(KERN_ERR PFX "Invalid QP %d for packed/unaligned handling\n", qp_id);
835 cq->cq_vbase[head].cqe_words[NES_NIC_CQE_MISC_IDX] = 0;
836 cqe_count++;
837 if (++head >= cq_size)
838 head = 0;
840 if (cqe_count == 255) {
841 /* Replenish mgt CQ */
842 nes_write32(nesdev->regs + NES_CQE_ALLOC, cq->cq_number | (cqe_count << 16));
843 nesdev->currcq_count += cqe_count;
844 cqe_count = 0;
847 skbs_needed = atomic_inc_return(&mgtvnic->rx_skbs_needed);
848 if (skbs_needed > (mgtvnic->mgt.rq_size >> 1))
849 nes_replenish_mgt_rq(mgtvnic);
852 cq->cq_head = head;
853 nes_write32(nesdev->regs + NES_CQE_ALLOC, NES_CQE_ALLOC_NOTIFY_NEXT |
854 cq->cq_number | (cqe_count << 16));
855 nes_read32(nesdev->regs + NES_CQE_ALLOC);
856 nesdev->currcq_count += cqe_count;
860 * nes_init_mgt_qp
862 int nes_init_mgt_qp(struct nes_device *nesdev, struct net_device *netdev, struct nes_vnic *nesvnic)
864 struct nes_vnic_mgt *mgtvnic;
865 u32 counter;
866 void *vmem;
867 dma_addr_t pmem;
868 struct nes_hw_cqp_wqe *cqp_wqe;
869 u32 cqp_head;
870 unsigned long flags;
871 struct nes_hw_nic_qp_context *mgt_context;
872 u64 u64temp;
873 struct nes_hw_nic_rq_wqe *mgt_rqe;
874 struct sk_buff *skb;
875 u32 wqe_count;
876 struct nes_rskb_cb *cb;
877 u32 mgt_mem_size;
878 void *mgt_vbase;
879 dma_addr_t mgt_pbase;
880 int i;
881 int ret;
883 /* Allocate space the all mgt QPs once */
884 mgtvnic = kzalloc(NES_MGT_QP_COUNT * sizeof(struct nes_vnic_mgt), GFP_KERNEL);
885 if (mgtvnic == NULL) {
886 nes_debug(NES_DBG_INIT, "Unable to allocate memory for mgt structure\n");
887 return -ENOMEM;
890 /* Allocate fragment, RQ, and CQ; Reuse CEQ based on the PCI function */
891 /* We are not sending from this NIC so sq is not allocated */
892 mgt_mem_size = 256 +
893 (NES_MGT_WQ_COUNT * sizeof(struct nes_hw_nic_rq_wqe)) +
894 (NES_MGT_WQ_COUNT * sizeof(struct nes_hw_nic_cqe)) +
895 sizeof(struct nes_hw_nic_qp_context);
896 mgt_mem_size = (mgt_mem_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
897 mgt_vbase = pci_alloc_consistent(nesdev->pcidev, NES_MGT_QP_COUNT * mgt_mem_size, &mgt_pbase);
898 if (!mgt_vbase) {
899 kfree(mgtvnic);
900 nes_debug(NES_DBG_INIT, "Unable to allocate memory for mgt host descriptor rings\n");
901 return -ENOMEM;
904 nesvnic->mgt_mem_size = NES_MGT_QP_COUNT * mgt_mem_size;
905 nesvnic->mgt_vbase = mgt_vbase;
906 nesvnic->mgt_pbase = mgt_pbase;
908 skb_queue_head_init(&nesvnic->mgt_skb_list);
909 init_waitqueue_head(&nesvnic->mgt_wait_queue);
910 nesvnic->mgt_thread = kthread_run(mgt_thread, nesvnic, "nes_mgt_thread");
912 for (i = 0; i < NES_MGT_QP_COUNT; i++) {
913 mgtvnic->nesvnic = nesvnic;
914 mgtvnic->mgt.qp_id = nesdev->mac_index + NES_MGT_QP_OFFSET + i;
915 memset(mgt_vbase, 0, mgt_mem_size);
916 nes_debug(NES_DBG_INIT, "Allocated mgt QP structures at %p (phys = %016lX), size = %u.\n",
917 mgt_vbase, (unsigned long)mgt_pbase, mgt_mem_size);
919 vmem = (void *)(((unsigned long)mgt_vbase + (256 - 1)) &
920 ~(unsigned long)(256 - 1));
921 pmem = (dma_addr_t)(((unsigned long long)mgt_pbase + (256 - 1)) &
922 ~(unsigned long long)(256 - 1));
924 spin_lock_init(&mgtvnic->mgt.rq_lock);
926 /* setup the RQ */
927 mgtvnic->mgt.rq_vbase = vmem;
928 mgtvnic->mgt.rq_pbase = pmem;
929 mgtvnic->mgt.rq_head = 0;
930 mgtvnic->mgt.rq_tail = 0;
931 mgtvnic->mgt.rq_size = NES_MGT_WQ_COUNT;
933 /* setup the CQ */
934 vmem += (NES_MGT_WQ_COUNT * sizeof(struct nes_hw_nic_rq_wqe));
935 pmem += (NES_MGT_WQ_COUNT * sizeof(struct nes_hw_nic_rq_wqe));
937 mgtvnic->mgt_cq.cq_number = mgtvnic->mgt.qp_id;
938 mgtvnic->mgt_cq.cq_vbase = vmem;
939 mgtvnic->mgt_cq.cq_pbase = pmem;
940 mgtvnic->mgt_cq.cq_head = 0;
941 mgtvnic->mgt_cq.cq_size = NES_MGT_WQ_COUNT;
943 mgtvnic->mgt_cq.ce_handler = nes_mgt_ce_handler;
945 /* Send CreateCQ request to CQP */
946 spin_lock_irqsave(&nesdev->cqp.lock, flags);
947 cqp_head = nesdev->cqp.sq_head;
949 cqp_wqe = &nesdev->cqp.sq_vbase[cqp_head];
950 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
952 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] = cpu_to_le32(
953 NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
954 ((u32)mgtvnic->mgt_cq.cq_size << 16));
955 cqp_wqe->wqe_words[NES_CQP_WQE_ID_IDX] = cpu_to_le32(
956 mgtvnic->mgt_cq.cq_number | ((u32)nesdev->ceq_index << 16));
957 u64temp = (u64)mgtvnic->mgt_cq.cq_pbase;
958 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
959 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
960 u64temp = (unsigned long)&mgtvnic->mgt_cq;
961 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] = cpu_to_le32((u32)(u64temp >> 1));
962 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
963 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
964 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
966 if (++cqp_head >= nesdev->cqp.sq_size)
967 cqp_head = 0;
968 cqp_wqe = &nesdev->cqp.sq_vbase[cqp_head];
969 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
971 /* Send CreateQP request to CQP */
972 mgt_context = (void *)(&mgtvnic->mgt_cq.cq_vbase[mgtvnic->mgt_cq.cq_size]);
973 mgt_context->context_words[NES_NIC_CTX_MISC_IDX] =
974 cpu_to_le32((u32)NES_MGT_CTX_SIZE |
975 ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 12));
976 nes_debug(NES_DBG_INIT, "RX_WINDOW_BUFFER_PAGE_TABLE_SIZE = 0x%08X, RX_WINDOW_BUFFER_SIZE = 0x%08X\n",
977 nes_read_indexed(nesdev, NES_IDX_RX_WINDOW_BUFFER_PAGE_TABLE_SIZE),
978 nes_read_indexed(nesdev, NES_IDX_RX_WINDOW_BUFFER_SIZE));
979 if (nes_read_indexed(nesdev, NES_IDX_RX_WINDOW_BUFFER_SIZE) != 0)
980 mgt_context->context_words[NES_NIC_CTX_MISC_IDX] |= cpu_to_le32(NES_NIC_BACK_STORE);
982 u64temp = (u64)mgtvnic->mgt.rq_pbase;
983 mgt_context->context_words[NES_NIC_CTX_SQ_LOW_IDX] = cpu_to_le32((u32)u64temp);
984 mgt_context->context_words[NES_NIC_CTX_SQ_HIGH_IDX] = cpu_to_le32((u32)(u64temp >> 32));
985 u64temp = (u64)mgtvnic->mgt.rq_pbase;
986 mgt_context->context_words[NES_NIC_CTX_RQ_LOW_IDX] = cpu_to_le32((u32)u64temp);
987 mgt_context->context_words[NES_NIC_CTX_RQ_HIGH_IDX] = cpu_to_le32((u32)(u64temp >> 32));
989 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] = cpu_to_le32(NES_CQP_CREATE_QP |
990 NES_CQP_QP_TYPE_NIC);
991 cqp_wqe->wqe_words[NES_CQP_WQE_ID_IDX] = cpu_to_le32(mgtvnic->mgt.qp_id);
992 u64temp = (u64)mgtvnic->mgt_cq.cq_pbase +
993 (mgtvnic->mgt_cq.cq_size * sizeof(struct nes_hw_nic_cqe));
994 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
996 if (++cqp_head >= nesdev->cqp.sq_size)
997 cqp_head = 0;
998 nesdev->cqp.sq_head = cqp_head;
1000 barrier();
1002 /* Ring doorbell (2 WQEs) */
1003 nes_write32(nesdev->regs + NES_WQE_ALLOC, 0x02800000 | nesdev->cqp.qp_id);
1005 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1006 nes_debug(NES_DBG_INIT, "Waiting for create MGT QP%u to complete.\n",
1007 mgtvnic->mgt.qp_id);
1009 ret = wait_event_timeout(nesdev->cqp.waitq, (nesdev->cqp.sq_tail == cqp_head),
1010 NES_EVENT_TIMEOUT);
1011 nes_debug(NES_DBG_INIT, "Create MGT QP%u completed, wait_event_timeout ret = %u.\n",
1012 mgtvnic->mgt.qp_id, ret);
1013 if (!ret) {
1014 nes_debug(NES_DBG_INIT, "MGT QP%u create timeout expired\n", mgtvnic->mgt.qp_id);
1015 if (i == 0) {
1016 pci_free_consistent(nesdev->pcidev, nesvnic->mgt_mem_size, nesvnic->mgt_vbase,
1017 nesvnic->mgt_pbase);
1018 kfree(mgtvnic);
1019 } else {
1020 nes_destroy_mgt(nesvnic);
1022 return -EIO;
1025 /* Populate the RQ */
1026 for (counter = 0; counter < (NES_MGT_WQ_COUNT - 1); counter++) {
1027 skb = dev_alloc_skb(nesvnic->max_frame_size);
1028 if (!skb) {
1029 nes_debug(NES_DBG_INIT, "%s: out of memory for receive skb\n", netdev->name);
1030 return -ENOMEM;
1033 skb->dev = netdev;
1035 pmem = pci_map_single(nesdev->pcidev, skb->data,
1036 nesvnic->max_frame_size, PCI_DMA_FROMDEVICE);
1037 cb = (struct nes_rskb_cb *)&skb->cb[0];
1038 cb->busaddr = pmem;
1039 cb->maplen = nesvnic->max_frame_size;
1041 mgt_rqe = &mgtvnic->mgt.rq_vbase[counter];
1042 mgt_rqe->wqe_words[NES_NIC_RQ_WQE_LENGTH_1_0_IDX] = cpu_to_le32((u32)nesvnic->max_frame_size);
1043 mgt_rqe->wqe_words[NES_NIC_RQ_WQE_LENGTH_3_2_IDX] = 0;
1044 mgt_rqe->wqe_words[NES_NIC_RQ_WQE_FRAG0_LOW_IDX] = cpu_to_le32((u32)pmem);
1045 mgt_rqe->wqe_words[NES_NIC_RQ_WQE_FRAG0_HIGH_IDX] = cpu_to_le32((u32)((u64)pmem >> 32));
1046 mgtvnic->mgt.rx_skb[counter] = skb;
1049 init_timer(&mgtvnic->rq_wqes_timer);
1050 mgtvnic->rq_wqes_timer.function = nes_mgt_rq_wqes_timeout;
1051 mgtvnic->rq_wqes_timer.data = (unsigned long)mgtvnic;
1053 wqe_count = NES_MGT_WQ_COUNT - 1;
1054 mgtvnic->mgt.rq_head = wqe_count;
1055 barrier();
1056 do {
1057 counter = min(wqe_count, ((u32)255));
1058 wqe_count -= counter;
1059 nes_write32(nesdev->regs + NES_WQE_ALLOC, (counter << 24) | mgtvnic->mgt.qp_id);
1060 } while (wqe_count);
1062 nes_write32(nesdev->regs + NES_CQE_ALLOC, NES_CQE_ALLOC_NOTIFY_NEXT |
1063 mgtvnic->mgt_cq.cq_number);
1064 nes_read32(nesdev->regs + NES_CQE_ALLOC);
1066 mgt_vbase += mgt_mem_size;
1067 mgt_pbase += mgt_mem_size;
1068 nesvnic->mgtvnic[i] = mgtvnic++;
1070 return 0;
1074 void nes_destroy_mgt(struct nes_vnic *nesvnic)
1076 struct nes_device *nesdev = nesvnic->nesdev;
1077 struct nes_vnic_mgt *mgtvnic;
1078 struct nes_vnic_mgt *first_mgtvnic;
1079 unsigned long flags;
1080 struct nes_hw_cqp_wqe *cqp_wqe;
1081 u32 cqp_head;
1082 struct sk_buff *rx_skb;
1083 int i;
1084 int ret;
1086 kthread_stop(nesvnic->mgt_thread);
1088 /* Free remaining NIC receive buffers */
1089 first_mgtvnic = nesvnic->mgtvnic[0];
1090 for (i = 0; i < NES_MGT_QP_COUNT; i++) {
1091 mgtvnic = nesvnic->mgtvnic[i];
1092 if (mgtvnic == NULL)
1093 continue;
1095 while (mgtvnic->mgt.rq_head != mgtvnic->mgt.rq_tail) {
1096 rx_skb = mgtvnic->mgt.rx_skb[mgtvnic->mgt.rq_tail];
1097 nes_mgt_free_skb(nesdev, rx_skb, PCI_DMA_FROMDEVICE);
1098 mgtvnic->mgt.rq_tail++;
1099 mgtvnic->mgt.rq_tail &= (mgtvnic->mgt.rq_size - 1);
1102 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1104 /* Destroy NIC QP */
1105 cqp_head = nesdev->cqp.sq_head;
1106 cqp_wqe = &nesdev->cqp.sq_vbase[cqp_head];
1107 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1109 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
1110 (NES_CQP_DESTROY_QP | NES_CQP_QP_TYPE_NIC));
1111 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1112 mgtvnic->mgt.qp_id);
1114 if (++cqp_head >= nesdev->cqp.sq_size)
1115 cqp_head = 0;
1117 cqp_wqe = &nesdev->cqp.sq_vbase[cqp_head];
1119 /* Destroy NIC CQ */
1120 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1121 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
1122 (NES_CQP_DESTROY_CQ | ((u32)mgtvnic->mgt_cq.cq_size << 16)));
1123 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1124 (mgtvnic->mgt_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1126 if (++cqp_head >= nesdev->cqp.sq_size)
1127 cqp_head = 0;
1129 nesdev->cqp.sq_head = cqp_head;
1130 barrier();
1132 /* Ring doorbell (2 WQEs) */
1133 nes_write32(nesdev->regs + NES_WQE_ALLOC, 0x02800000 | nesdev->cqp.qp_id);
1135 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1136 nes_debug(NES_DBG_SHUTDOWN, "Waiting for CQP, cqp_head=%u, cqp.sq_head=%u,"
1137 " cqp.sq_tail=%u, cqp.sq_size=%u\n",
1138 cqp_head, nesdev->cqp.sq_head,
1139 nesdev->cqp.sq_tail, nesdev->cqp.sq_size);
1141 ret = wait_event_timeout(nesdev->cqp.waitq, (nesdev->cqp.sq_tail == cqp_head),
1142 NES_EVENT_TIMEOUT);
1144 nes_debug(NES_DBG_SHUTDOWN, "Destroy MGT QP returned, wait_event_timeout ret = %u, cqp_head=%u,"
1145 " cqp.sq_head=%u, cqp.sq_tail=%u\n",
1146 ret, cqp_head, nesdev->cqp.sq_head, nesdev->cqp.sq_tail);
1147 if (!ret)
1148 nes_debug(NES_DBG_SHUTDOWN, "MGT QP%u destroy timeout expired\n",
1149 mgtvnic->mgt.qp_id);
1151 nesvnic->mgtvnic[i] = NULL;
1154 if (nesvnic->mgt_vbase) {
1155 pci_free_consistent(nesdev->pcidev, nesvnic->mgt_mem_size, nesvnic->mgt_vbase,
1156 nesvnic->mgt_pbase);
1157 nesvnic->mgt_vbase = NULL;
1158 nesvnic->mgt_pbase = 0;
1161 kfree(first_mgtvnic);