1 /*******************************************************************************
3 * Copyright (c) 2015-2016 Intel Corporation. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenFabrics.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 *******************************************************************************/
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/netdevice.h>
38 #include <linux/etherdevice.h>
39 #include <linux/ethtool.h>
40 #include <linux/mii.h>
41 #include <linux/if_vlan.h>
42 #include <linux/crc32.h>
45 #include <linux/tcp.h>
46 #include <linux/init.h>
49 #include <asm/byteorder.h>
50 #include <net/netevent.h>
51 #include <net/neighbour.h>
55 * i40iw_arp_table - manage arp table
56 * @iwdev: iwarp device
57 * @ip_addr: ip address for device
58 * @mac_addr: mac address ptr
59 * @action: modify, delete or add
61 int i40iw_arp_table(struct i40iw_device *iwdev,
72 memset(ip, 0, sizeof(ip));
75 memcpy(ip, ip_addr, sizeof(ip));
78 for (arp_index = 0; (u32)arp_index < iwdev->arp_table_size; arp_index++)
79 if (memcmp(iwdev->arp_table[arp_index].ip_addr, ip, sizeof(ip)) == 0)
83 if (arp_index != iwdev->arp_table_size)
87 err = i40iw_alloc_resource(iwdev, iwdev->allocated_arps,
88 iwdev->arp_table_size,
90 &iwdev->next_arp_index);
95 memcpy(iwdev->arp_table[arp_index].ip_addr, ip, sizeof(ip));
96 ether_addr_copy(iwdev->arp_table[arp_index].mac_addr, mac_addr);
98 case I40IW_ARP_RESOLVE:
99 if (arp_index == iwdev->arp_table_size)
102 case I40IW_ARP_DELETE:
103 if (arp_index == iwdev->arp_table_size)
105 memset(iwdev->arp_table[arp_index].ip_addr, 0,
106 sizeof(iwdev->arp_table[arp_index].ip_addr));
107 eth_zero_addr(iwdev->arp_table[arp_index].mac_addr);
108 i40iw_free_resource(iwdev, iwdev->allocated_arps, arp_index);
117 * i40iw_wr32 - write 32 bits to hw register
118 * @hw: hardware information including registers
119 * @reg: register offset
120 * @value: vvalue to write to register
122 inline void i40iw_wr32(struct i40iw_hw *hw, u32 reg, u32 value)
124 writel(value, hw->hw_addr + reg);
128 * i40iw_rd32 - read a 32 bit hw register
129 * @hw: hardware information including registers
130 * @reg: register offset
132 * Return value of register content
134 inline u32 i40iw_rd32(struct i40iw_hw *hw, u32 reg)
136 return readl(hw->hw_addr + reg);
140 * i40iw_inetaddr_event - system notifier for netdev events
142 * @event: event for notifier
145 int i40iw_inetaddr_event(struct notifier_block *notifier,
149 struct in_ifaddr *ifa = ptr;
150 struct net_device *event_netdev = ifa->ifa_dev->dev;
151 struct net_device *netdev;
152 struct net_device *upper_dev;
153 struct i40iw_device *iwdev;
154 struct i40iw_handler *hdl;
156 u32 action = I40IW_ARP_ADD;
158 hdl = i40iw_find_netdev(event_netdev);
162 iwdev = &hdl->device;
163 if (iwdev->init_state < INET_NOTIFIER)
166 netdev = iwdev->ldev->netdev;
167 upper_dev = netdev_master_upper_dev_get(netdev);
168 if (netdev != event_netdev)
172 local_ipaddr = ntohl(
173 ((struct in_device *)upper_dev->ip_ptr)->ifa_list->ifa_address);
175 local_ipaddr = ntohl(ifa->ifa_address);
178 action = I40IW_ARP_DELETE;
182 case NETDEV_CHANGEADDR:
183 i40iw_manage_arp_cache(iwdev,
188 i40iw_if_notify(iwdev, netdev, &local_ipaddr, true,
189 (action == I40IW_ARP_ADD) ? true : false);
198 * i40iw_inet6addr_event - system notifier for ipv6 netdev events
200 * @event: event for notifier
203 int i40iw_inet6addr_event(struct notifier_block *notifier,
207 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
208 struct net_device *event_netdev = ifa->idev->dev;
209 struct net_device *netdev;
210 struct i40iw_device *iwdev;
211 struct i40iw_handler *hdl;
212 u32 local_ipaddr6[4];
213 u32 action = I40IW_ARP_ADD;
215 hdl = i40iw_find_netdev(event_netdev);
219 iwdev = &hdl->device;
220 if (iwdev->init_state < INET_NOTIFIER)
223 netdev = iwdev->ldev->netdev;
224 if (netdev != event_netdev)
227 i40iw_copy_ip_ntohl(local_ipaddr6, ifa->addr.in6_u.u6_addr32);
230 action = I40IW_ARP_DELETE;
234 case NETDEV_CHANGEADDR:
235 i40iw_manage_arp_cache(iwdev,
240 i40iw_if_notify(iwdev, netdev, local_ipaddr6, false,
241 (action == I40IW_ARP_ADD) ? true : false);
250 * i40iw_net_event - system notifier for net events
252 * @event: event for notifier
255 int i40iw_net_event(struct notifier_block *notifier, unsigned long event, void *ptr)
257 struct neighbour *neigh = ptr;
258 struct i40iw_device *iwdev;
259 struct i40iw_handler *iwhdl;
264 case NETEVENT_NEIGH_UPDATE:
265 iwhdl = i40iw_find_netdev((struct net_device *)neigh->dev);
268 iwdev = &iwhdl->device;
269 if (iwdev->init_state < INET_NOTIFIER)
271 p = (__be32 *)neigh->primary_key;
272 i40iw_copy_ip_ntohl(local_ipaddr, p);
273 if (neigh->nud_state & NUD_VALID) {
274 i40iw_manage_arp_cache(iwdev,
281 i40iw_manage_arp_cache(iwdev,
295 * i40iw_get_cqp_request - get cqp struct
296 * @cqp: device cqp ptr
297 * @wait: cqp to be used in wait mode
299 struct i40iw_cqp_request *i40iw_get_cqp_request(struct i40iw_cqp *cqp, bool wait)
301 struct i40iw_cqp_request *cqp_request = NULL;
304 spin_lock_irqsave(&cqp->req_lock, flags);
305 if (!list_empty(&cqp->cqp_avail_reqs)) {
306 cqp_request = list_entry(cqp->cqp_avail_reqs.next,
307 struct i40iw_cqp_request, list);
308 list_del_init(&cqp_request->list);
310 spin_unlock_irqrestore(&cqp->req_lock, flags);
312 cqp_request = kzalloc(sizeof(*cqp_request), GFP_ATOMIC);
314 cqp_request->dynamic = true;
315 INIT_LIST_HEAD(&cqp_request->list);
316 init_waitqueue_head(&cqp_request->waitq);
320 i40iw_pr_err("CQP Request Fail: No Memory");
325 atomic_set(&cqp_request->refcount, 2);
326 cqp_request->waiting = true;
328 atomic_set(&cqp_request->refcount, 1);
334 * i40iw_free_cqp_request - free cqp request
336 * @cqp_request: to be put back in cqp list
338 void i40iw_free_cqp_request(struct i40iw_cqp *cqp, struct i40iw_cqp_request *cqp_request)
342 if (cqp_request->dynamic) {
345 cqp_request->request_done = false;
346 cqp_request->callback_fcn = NULL;
347 cqp_request->waiting = false;
349 spin_lock_irqsave(&cqp->req_lock, flags);
350 list_add_tail(&cqp_request->list, &cqp->cqp_avail_reqs);
351 spin_unlock_irqrestore(&cqp->req_lock, flags);
356 * i40iw_put_cqp_request - dec ref count and free if 0
358 * @cqp_request: to be put back in cqp list
360 void i40iw_put_cqp_request(struct i40iw_cqp *cqp,
361 struct i40iw_cqp_request *cqp_request)
363 if (atomic_dec_and_test(&cqp_request->refcount))
364 i40iw_free_cqp_request(cqp, cqp_request);
368 * i40iw_free_qp - callback after destroy cqp completes
369 * @cqp_request: cqp request for destroy qp
372 static void i40iw_free_qp(struct i40iw_cqp_request *cqp_request, u32 num)
374 struct i40iw_sc_qp *qp = (struct i40iw_sc_qp *)cqp_request->param;
375 struct i40iw_qp *iwqp = (struct i40iw_qp *)qp->back_qp;
376 struct i40iw_device *iwdev;
377 u32 qp_num = iwqp->ibqp.qp_num;
381 i40iw_rem_pdusecount(iwqp->iwpd, iwdev);
382 i40iw_free_qp_resources(iwdev, iwqp, qp_num);
383 i40iw_rem_devusecount(iwdev);
387 * i40iw_wait_event - wait for completion
388 * @iwdev: iwarp device
389 * @cqp_request: cqp request to wait
391 static int i40iw_wait_event(struct i40iw_device *iwdev,
392 struct i40iw_cqp_request *cqp_request)
394 struct cqp_commands_info *info = &cqp_request->info;
395 struct i40iw_cqp *iwcqp = &iwdev->cqp;
396 bool cqp_error = false;
400 timeout_ret = wait_event_timeout(cqp_request->waitq,
401 cqp_request->request_done,
402 I40IW_EVENT_TIMEOUT);
404 i40iw_pr_err("error cqp command 0x%x timed out ret = %d\n",
405 info->cqp_cmd, timeout_ret);
409 i40iw_request_reset(iwdev);
413 cqp_error = cqp_request->compl_info.error;
415 i40iw_pr_err("error cqp command 0x%x completion maj = 0x%x min=0x%x\n",
416 info->cqp_cmd, cqp_request->compl_info.maj_err_code,
417 cqp_request->compl_info.min_err_code);
422 i40iw_put_cqp_request(iwcqp, cqp_request);
427 * i40iw_handle_cqp_op - process cqp command
428 * @iwdev: iwarp device
429 * @cqp_request: cqp request to process
431 enum i40iw_status_code i40iw_handle_cqp_op(struct i40iw_device *iwdev,
432 struct i40iw_cqp_request
435 struct i40iw_sc_dev *dev = &iwdev->sc_dev;
436 enum i40iw_status_code status;
437 struct cqp_commands_info *info = &cqp_request->info;
441 i40iw_free_cqp_request(&iwdev->cqp, cqp_request);
442 return I40IW_ERR_CQP_COMPL_ERROR;
445 status = i40iw_process_cqp_cmd(dev, info);
447 i40iw_pr_err("error cqp command 0x%x failed\n", info->cqp_cmd);
448 i40iw_free_cqp_request(&iwdev->cqp, cqp_request);
451 if (cqp_request->waiting)
452 err_code = i40iw_wait_event(iwdev, cqp_request);
454 status = I40IW_ERR_CQP_COMPL_ERROR;
459 * i40iw_add_devusecount - add dev refcount
460 * @iwdev: dev for refcount
462 void i40iw_add_devusecount(struct i40iw_device *iwdev)
464 atomic64_inc(&iwdev->use_count);
468 * i40iw_rem_devusecount - decrement refcount for dev
471 void i40iw_rem_devusecount(struct i40iw_device *iwdev)
473 if (!atomic64_dec_and_test(&iwdev->use_count))
475 wake_up(&iwdev->close_wq);
479 * i40iw_add_pdusecount - add pd refcount
480 * @iwpd: pd for refcount
482 void i40iw_add_pdusecount(struct i40iw_pd *iwpd)
484 atomic_inc(&iwpd->usecount);
488 * i40iw_rem_pdusecount - decrement refcount for pd and free if 0
489 * @iwpd: pd for refcount
490 * @iwdev: iwarp device
492 void i40iw_rem_pdusecount(struct i40iw_pd *iwpd, struct i40iw_device *iwdev)
494 if (!atomic_dec_and_test(&iwpd->usecount))
496 i40iw_free_resource(iwdev, iwdev->allocated_pds, iwpd->sc_pd.pd_id);
501 * i40iw_add_ref - add refcount for qp
504 void i40iw_add_ref(struct ib_qp *ibqp)
506 struct i40iw_qp *iwqp = (struct i40iw_qp *)ibqp;
508 atomic_inc(&iwqp->refcount);
512 * i40iw_rem_ref - rem refcount for qp and free if 0
515 void i40iw_rem_ref(struct ib_qp *ibqp)
517 struct i40iw_qp *iwqp;
518 enum i40iw_status_code status;
519 struct i40iw_cqp_request *cqp_request;
520 struct cqp_commands_info *cqp_info;
521 struct i40iw_device *iwdev;
525 iwqp = to_iwqp(ibqp);
527 spin_lock_irqsave(&iwdev->qptable_lock, flags);
528 if (!atomic_dec_and_test(&iwqp->refcount)) {
529 spin_unlock_irqrestore(&iwdev->qptable_lock, flags);
533 qp_num = iwqp->ibqp.qp_num;
534 iwdev->qp_table[qp_num] = NULL;
535 spin_unlock_irqrestore(&iwdev->qptable_lock, flags);
536 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
540 cqp_request->callback_fcn = i40iw_free_qp;
541 cqp_request->param = (void *)&iwqp->sc_qp;
542 cqp_info = &cqp_request->info;
543 cqp_info->cqp_cmd = OP_QP_DESTROY;
544 cqp_info->post_sq = 1;
545 cqp_info->in.u.qp_destroy.qp = &iwqp->sc_qp;
546 cqp_info->in.u.qp_destroy.scratch = (uintptr_t)cqp_request;
547 cqp_info->in.u.qp_destroy.remove_hash_idx = true;
548 status = i40iw_handle_cqp_op(iwdev, cqp_request);
550 i40iw_pr_err("CQP-OP Destroy QP fail");
554 * i40iw_get_qp - get qp address
555 * @device: iwarp device
558 struct ib_qp *i40iw_get_qp(struct ib_device *device, int qpn)
560 struct i40iw_device *iwdev = to_iwdev(device);
562 if ((qpn < IW_FIRST_QPN) || (qpn >= iwdev->max_qp))
565 return &iwdev->qp_table[qpn]->ibqp;
569 * i40iw_debug_buf - print debug msg and buffer is mask set
570 * @dev: hardware control device structure
571 * @mask: mask to compare if to print debug buffer
572 * @buf: points buffer addr
573 * @size: saize of buffer to print
575 void i40iw_debug_buf(struct i40iw_sc_dev *dev,
576 enum i40iw_debug_flag mask,
583 if (!(dev->debug_mask & mask))
585 i40iw_debug(dev, mask, "%s\n", desc);
586 i40iw_debug(dev, mask, "starting address virt=%p phy=%llxh\n", buf,
587 (unsigned long long)virt_to_phys(buf));
589 for (i = 0; i < size; i += 8)
590 i40iw_debug(dev, mask, "index %03d val: %016llx\n", i, buf[i / 8]);
594 * i40iw_get_hw_addr - return hw addr
595 * @par: points to shared dev
597 u8 __iomem *i40iw_get_hw_addr(void *par)
599 struct i40iw_sc_dev *dev = (struct i40iw_sc_dev *)par;
601 return dev->hw->hw_addr;
605 * i40iw_remove_head - return head entry and remove from list
606 * @list: list for entry
608 void *i40iw_remove_head(struct list_head *list)
610 struct list_head *entry;
612 if (list_empty(list))
615 entry = (void *)list->next;
617 return (void *)entry;
621 * i40iw_allocate_dma_mem - Memory alloc helper fn
622 * @hw: pointer to the HW structure
623 * @mem: ptr to mem struct to fill out
624 * @size: size of memory requested
625 * @alignment: what to align the allocation to
627 enum i40iw_status_code i40iw_allocate_dma_mem(struct i40iw_hw *hw,
628 struct i40iw_dma_mem *mem,
632 struct pci_dev *pcidev = (struct pci_dev *)hw->dev_context;
635 return I40IW_ERR_PARAM;
636 mem->size = ALIGN(size, alignment);
637 mem->va = dma_zalloc_coherent(&pcidev->dev, mem->size,
638 (dma_addr_t *)&mem->pa, GFP_KERNEL);
640 return I40IW_ERR_NO_MEMORY;
645 * i40iw_free_dma_mem - Memory free helper fn
646 * @hw: pointer to the HW structure
647 * @mem: ptr to mem struct to free
649 void i40iw_free_dma_mem(struct i40iw_hw *hw, struct i40iw_dma_mem *mem)
651 struct pci_dev *pcidev = (struct pci_dev *)hw->dev_context;
653 if (!mem || !mem->va)
656 dma_free_coherent(&pcidev->dev, mem->size,
657 mem->va, (dma_addr_t)mem->pa);
662 * i40iw_allocate_virt_mem - virtual memory alloc helper fn
663 * @hw: pointer to the HW structure
664 * @mem: ptr to mem struct to fill out
665 * @size: size of memory requested
667 enum i40iw_status_code i40iw_allocate_virt_mem(struct i40iw_hw *hw,
668 struct i40iw_virt_mem *mem,
672 return I40IW_ERR_PARAM;
675 mem->va = kzalloc(size, GFP_KERNEL);
680 return I40IW_ERR_NO_MEMORY;
684 * i40iw_free_virt_mem - virtual memory free helper fn
685 * @hw: pointer to the HW structure
686 * @mem: ptr to mem struct to free
688 enum i40iw_status_code i40iw_free_virt_mem(struct i40iw_hw *hw,
689 struct i40iw_virt_mem *mem)
692 return I40IW_ERR_PARAM;
694 * mem->va points to the parent of mem, so both mem and mem->va
695 * can not be touched once mem->va is freed
702 * i40iw_cqp_sds_cmd - create cqp command for sd
703 * @dev: hardware control device structure
704 * @sd_info: information for sd cqp
707 enum i40iw_status_code i40iw_cqp_sds_cmd(struct i40iw_sc_dev *dev,
708 struct i40iw_update_sds_info *sdinfo)
710 enum i40iw_status_code status;
711 struct i40iw_cqp_request *cqp_request;
712 struct cqp_commands_info *cqp_info;
713 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
715 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
717 return I40IW_ERR_NO_MEMORY;
718 cqp_info = &cqp_request->info;
719 memcpy(&cqp_info->in.u.update_pe_sds.info, sdinfo,
720 sizeof(cqp_info->in.u.update_pe_sds.info));
721 cqp_info->cqp_cmd = OP_UPDATE_PE_SDS;
722 cqp_info->post_sq = 1;
723 cqp_info->in.u.update_pe_sds.dev = dev;
724 cqp_info->in.u.update_pe_sds.scratch = (uintptr_t)cqp_request;
725 status = i40iw_handle_cqp_op(iwdev, cqp_request);
727 i40iw_pr_err("CQP-OP Update SD's fail");
732 * i40iw_qp_suspend_resume - cqp command for suspend/resume
733 * @dev: hardware control device structure
734 * @qp: hardware control qp
735 * @suspend: flag if suspend or resume
737 void i40iw_qp_suspend_resume(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp, bool suspend)
739 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
740 struct i40iw_cqp_request *cqp_request;
741 struct i40iw_sc_cqp *cqp = dev->cqp;
742 struct cqp_commands_info *cqp_info;
743 enum i40iw_status_code status;
745 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
749 cqp_info = &cqp_request->info;
750 cqp_info->cqp_cmd = (suspend) ? OP_SUSPEND : OP_RESUME;
751 cqp_info->in.u.suspend_resume.cqp = cqp;
752 cqp_info->in.u.suspend_resume.qp = qp;
753 cqp_info->in.u.suspend_resume.scratch = (uintptr_t)cqp_request;
754 status = i40iw_handle_cqp_op(iwdev, cqp_request);
756 i40iw_pr_err("CQP-OP QP Suspend/Resume fail");
760 * i40iw_qp_mss_modify - modify mss for qp
761 * @dev: hardware control device structure
762 * @qp: hardware control qp
764 void i40iw_qp_mss_modify(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
766 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
767 struct i40iw_qp *iwqp = (struct i40iw_qp *)qp->back_qp;
768 struct i40iw_modify_qp_info info;
770 memset(&info, 0, sizeof(info));
771 info.mss_change = true;
772 info.new_mss = qp->vsi->mss;
773 i40iw_hw_modify_qp(iwdev, iwqp, &info, false);
777 * i40iw_term_modify_qp - modify qp for term message
778 * @qp: hardware control qp
779 * @next_state: qp's next state
780 * @term: terminate code
783 void i40iw_term_modify_qp(struct i40iw_sc_qp *qp, u8 next_state, u8 term, u8 term_len)
785 struct i40iw_qp *iwqp;
787 iwqp = (struct i40iw_qp *)qp->back_qp;
788 i40iw_next_iw_state(iwqp, next_state, 0, term, term_len);
792 * i40iw_terminate_done - after terminate is completed
793 * @qp: hardware control qp
794 * @timeout_occurred: indicates if terminate timer expired
796 void i40iw_terminate_done(struct i40iw_sc_qp *qp, int timeout_occurred)
798 struct i40iw_qp *iwqp;
799 u32 next_iwarp_state = I40IW_QP_STATE_ERROR;
804 iwqp = (struct i40iw_qp *)qp->back_qp;
805 spin_lock_irqsave(&iwqp->lock, flags);
806 if (iwqp->hte_added) {
810 first_time = !(qp->term_flags & I40IW_TERM_DONE);
811 qp->term_flags |= I40IW_TERM_DONE;
812 spin_unlock_irqrestore(&iwqp->lock, flags);
814 if (!timeout_occurred)
815 i40iw_terminate_del_timer(qp);
817 next_iwarp_state = I40IW_QP_STATE_CLOSING;
819 i40iw_next_iw_state(iwqp, next_iwarp_state, hte, 0, 0);
820 i40iw_cm_disconn(iwqp);
825 * i40iw_terminate_imeout - timeout happened
826 * @context: points to iwarp qp
828 static void i40iw_terminate_timeout(unsigned long context)
830 struct i40iw_qp *iwqp = (struct i40iw_qp *)context;
831 struct i40iw_sc_qp *qp = (struct i40iw_sc_qp *)&iwqp->sc_qp;
833 i40iw_terminate_done(qp, 1);
834 i40iw_rem_ref(&iwqp->ibqp);
838 * i40iw_terminate_start_timer - start terminate timeout
839 * @qp: hardware control qp
841 void i40iw_terminate_start_timer(struct i40iw_sc_qp *qp)
843 struct i40iw_qp *iwqp;
845 iwqp = (struct i40iw_qp *)qp->back_qp;
846 i40iw_add_ref(&iwqp->ibqp);
847 setup_timer(&iwqp->terminate_timer, i40iw_terminate_timeout,
848 (unsigned long)iwqp);
849 iwqp->terminate_timer.expires = jiffies + HZ;
850 add_timer(&iwqp->terminate_timer);
854 * i40iw_terminate_del_timer - delete terminate timeout
855 * @qp: hardware control qp
857 void i40iw_terminate_del_timer(struct i40iw_sc_qp *qp)
859 struct i40iw_qp *iwqp;
861 iwqp = (struct i40iw_qp *)qp->back_qp;
862 if (del_timer(&iwqp->terminate_timer))
863 i40iw_rem_ref(&iwqp->ibqp);
867 * i40iw_cqp_generic_worker - generic worker for cqp
868 * @work: work pointer
870 static void i40iw_cqp_generic_worker(struct work_struct *work)
872 struct i40iw_virtchnl_work_info *work_info =
873 &((struct virtchnl_work *)work)->work_info;
875 if (work_info->worker_vf_dev)
876 work_info->callback_fcn(work_info->worker_vf_dev);
880 * i40iw_cqp_spawn_worker - spawn worket thread
881 * @iwdev: device struct pointer
882 * @work_info: work request info
883 * @iw_vf_idx: virtual function index
885 void i40iw_cqp_spawn_worker(struct i40iw_sc_dev *dev,
886 struct i40iw_virtchnl_work_info *work_info,
889 struct virtchnl_work *work;
890 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
892 work = &iwdev->virtchnl_w[iw_vf_idx];
893 memcpy(&work->work_info, work_info, sizeof(*work_info));
894 INIT_WORK(&work->work, i40iw_cqp_generic_worker);
895 queue_work(iwdev->virtchnl_wq, &work->work);
899 * i40iw_cqp_manage_hmc_fcn_worker -
900 * @work: work pointer for hmc info
902 static void i40iw_cqp_manage_hmc_fcn_worker(struct work_struct *work)
904 struct i40iw_cqp_request *cqp_request =
905 ((struct virtchnl_work *)work)->cqp_request;
906 struct i40iw_ccq_cqe_info ccq_cqe_info;
907 struct i40iw_hmc_fcn_info *hmcfcninfo =
908 &cqp_request->info.in.u.manage_hmc_pm.info;
909 struct i40iw_device *iwdev =
910 (struct i40iw_device *)cqp_request->info.in.u.manage_hmc_pm.dev->back_dev;
912 ccq_cqe_info.cqp = NULL;
913 ccq_cqe_info.maj_err_code = cqp_request->compl_info.maj_err_code;
914 ccq_cqe_info.min_err_code = cqp_request->compl_info.min_err_code;
915 ccq_cqe_info.op_code = cqp_request->compl_info.op_code;
916 ccq_cqe_info.op_ret_val = cqp_request->compl_info.op_ret_val;
917 ccq_cqe_info.scratch = 0;
918 ccq_cqe_info.error = cqp_request->compl_info.error;
919 hmcfcninfo->callback_fcn(cqp_request->info.in.u.manage_hmc_pm.dev,
920 hmcfcninfo->cqp_callback_param, &ccq_cqe_info);
921 i40iw_put_cqp_request(&iwdev->cqp, cqp_request);
925 * i40iw_cqp_manage_hmc_fcn_callback - called function after cqp completion
926 * @cqp_request: cqp request info struct for hmc fun
927 * @unused: unused param of callback
929 static void i40iw_cqp_manage_hmc_fcn_callback(struct i40iw_cqp_request *cqp_request,
932 struct virtchnl_work *work;
933 struct i40iw_hmc_fcn_info *hmcfcninfo =
934 &cqp_request->info.in.u.manage_hmc_pm.info;
935 struct i40iw_device *iwdev =
936 (struct i40iw_device *)cqp_request->info.in.u.manage_hmc_pm.dev->
939 if (hmcfcninfo && hmcfcninfo->callback_fcn) {
940 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s1\n", __func__);
941 atomic_inc(&cqp_request->refcount);
942 work = &iwdev->virtchnl_w[hmcfcninfo->iw_vf_idx];
943 work->cqp_request = cqp_request;
944 INIT_WORK(&work->work, i40iw_cqp_manage_hmc_fcn_worker);
945 queue_work(iwdev->virtchnl_wq, &work->work);
946 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s2\n", __func__);
948 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s: Something wrong\n", __func__);
953 * i40iw_cqp_manage_hmc_fcn_cmd - issue cqp command to manage hmc
954 * @dev: hardware control device structure
955 * @hmcfcninfo: info for hmc
957 enum i40iw_status_code i40iw_cqp_manage_hmc_fcn_cmd(struct i40iw_sc_dev *dev,
958 struct i40iw_hmc_fcn_info *hmcfcninfo)
960 enum i40iw_status_code status;
961 struct i40iw_cqp_request *cqp_request;
962 struct cqp_commands_info *cqp_info;
963 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
965 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s\n", __func__);
966 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
968 return I40IW_ERR_NO_MEMORY;
969 cqp_info = &cqp_request->info;
970 cqp_request->callback_fcn = i40iw_cqp_manage_hmc_fcn_callback;
971 cqp_request->param = hmcfcninfo;
972 memcpy(&cqp_info->in.u.manage_hmc_pm.info, hmcfcninfo,
973 sizeof(*hmcfcninfo));
974 cqp_info->in.u.manage_hmc_pm.dev = dev;
975 cqp_info->cqp_cmd = OP_MANAGE_HMC_PM_FUNC_TABLE;
976 cqp_info->post_sq = 1;
977 cqp_info->in.u.manage_hmc_pm.scratch = (uintptr_t)cqp_request;
978 status = i40iw_handle_cqp_op(iwdev, cqp_request);
980 i40iw_pr_err("CQP-OP Manage HMC fail");
985 * i40iw_cqp_query_fpm_values_cmd - send cqp command for fpm
986 * @iwdev: function device struct
987 * @values_mem: buffer for fpm
988 * @hmc_fn_id: function id for fpm
990 enum i40iw_status_code i40iw_cqp_query_fpm_values_cmd(struct i40iw_sc_dev *dev,
991 struct i40iw_dma_mem *values_mem,
994 enum i40iw_status_code status;
995 struct i40iw_cqp_request *cqp_request;
996 struct cqp_commands_info *cqp_info;
997 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
999 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1001 return I40IW_ERR_NO_MEMORY;
1002 cqp_info = &cqp_request->info;
1003 cqp_request->param = NULL;
1004 cqp_info->in.u.query_fpm_values.cqp = dev->cqp;
1005 cqp_info->in.u.query_fpm_values.fpm_values_pa = values_mem->pa;
1006 cqp_info->in.u.query_fpm_values.fpm_values_va = values_mem->va;
1007 cqp_info->in.u.query_fpm_values.hmc_fn_id = hmc_fn_id;
1008 cqp_info->cqp_cmd = OP_QUERY_FPM_VALUES;
1009 cqp_info->post_sq = 1;
1010 cqp_info->in.u.query_fpm_values.scratch = (uintptr_t)cqp_request;
1011 status = i40iw_handle_cqp_op(iwdev, cqp_request);
1013 i40iw_pr_err("CQP-OP Query FPM fail");
1018 * i40iw_cqp_commit_fpm_values_cmd - commit fpm values in hw
1019 * @dev: hardware control device structure
1020 * @values_mem: buffer with fpm values
1021 * @hmc_fn_id: function id for fpm
1023 enum i40iw_status_code i40iw_cqp_commit_fpm_values_cmd(struct i40iw_sc_dev *dev,
1024 struct i40iw_dma_mem *values_mem,
1027 enum i40iw_status_code status;
1028 struct i40iw_cqp_request *cqp_request;
1029 struct cqp_commands_info *cqp_info;
1030 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1032 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1034 return I40IW_ERR_NO_MEMORY;
1035 cqp_info = &cqp_request->info;
1036 cqp_request->param = NULL;
1037 cqp_info->in.u.commit_fpm_values.cqp = dev->cqp;
1038 cqp_info->in.u.commit_fpm_values.fpm_values_pa = values_mem->pa;
1039 cqp_info->in.u.commit_fpm_values.fpm_values_va = values_mem->va;
1040 cqp_info->in.u.commit_fpm_values.hmc_fn_id = hmc_fn_id;
1041 cqp_info->cqp_cmd = OP_COMMIT_FPM_VALUES;
1042 cqp_info->post_sq = 1;
1043 cqp_info->in.u.commit_fpm_values.scratch = (uintptr_t)cqp_request;
1044 status = i40iw_handle_cqp_op(iwdev, cqp_request);
1046 i40iw_pr_err("CQP-OP Commit FPM fail");
1051 * i40iw_vf_wait_vchnl_resp - wait for channel msg
1052 * @iwdev: function's device struct
1054 enum i40iw_status_code i40iw_vf_wait_vchnl_resp(struct i40iw_sc_dev *dev)
1056 struct i40iw_device *iwdev = dev->back_dev;
1059 i40iw_debug(dev, I40IW_DEBUG_VIRT, "%s[%u] dev %p, iwdev %p\n",
1060 __func__, __LINE__, dev, iwdev);
1062 atomic_set(&iwdev->vchnl_msgs, 2);
1063 timeout_ret = wait_event_timeout(iwdev->vchnl_waitq,
1064 (atomic_read(&iwdev->vchnl_msgs) == 1),
1065 I40IW_VCHNL_EVENT_TIMEOUT);
1066 atomic_dec(&iwdev->vchnl_msgs);
1068 i40iw_pr_err("virt channel completion timeout = 0x%x\n", timeout_ret);
1069 atomic_set(&iwdev->vchnl_msgs, 0);
1070 dev->vchnl_up = false;
1071 return I40IW_ERR_TIMEOUT;
1073 wake_up(&dev->vf_reqs);
1078 * i40iw_cqp_cq_create_cmd - create a cq for the cqp
1079 * @dev: device pointer
1080 * @cq: pointer to created cq
1082 enum i40iw_status_code i40iw_cqp_cq_create_cmd(struct i40iw_sc_dev *dev,
1083 struct i40iw_sc_cq *cq)
1085 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1086 struct i40iw_cqp *iwcqp = &iwdev->cqp;
1087 struct i40iw_cqp_request *cqp_request;
1088 struct cqp_commands_info *cqp_info;
1089 enum i40iw_status_code status;
1091 cqp_request = i40iw_get_cqp_request(iwcqp, true);
1093 return I40IW_ERR_NO_MEMORY;
1095 cqp_info = &cqp_request->info;
1096 cqp_info->cqp_cmd = OP_CQ_CREATE;
1097 cqp_info->post_sq = 1;
1098 cqp_info->in.u.cq_create.cq = cq;
1099 cqp_info->in.u.cq_create.scratch = (uintptr_t)cqp_request;
1100 status = i40iw_handle_cqp_op(iwdev, cqp_request);
1102 i40iw_pr_err("CQP-OP Create QP fail");
1108 * i40iw_cqp_qp_create_cmd - create a qp for the cqp
1109 * @dev: device pointer
1110 * @qp: pointer to created qp
1112 enum i40iw_status_code i40iw_cqp_qp_create_cmd(struct i40iw_sc_dev *dev,
1113 struct i40iw_sc_qp *qp)
1115 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1116 struct i40iw_cqp *iwcqp = &iwdev->cqp;
1117 struct i40iw_cqp_request *cqp_request;
1118 struct cqp_commands_info *cqp_info;
1119 struct i40iw_create_qp_info *qp_info;
1120 enum i40iw_status_code status;
1122 cqp_request = i40iw_get_cqp_request(iwcqp, true);
1124 return I40IW_ERR_NO_MEMORY;
1126 cqp_info = &cqp_request->info;
1127 qp_info = &cqp_request->info.in.u.qp_create.info;
1129 memset(qp_info, 0, sizeof(*qp_info));
1131 qp_info->cq_num_valid = true;
1132 qp_info->next_iwarp_state = I40IW_QP_STATE_RTS;
1134 cqp_info->cqp_cmd = OP_QP_CREATE;
1135 cqp_info->post_sq = 1;
1136 cqp_info->in.u.qp_create.qp = qp;
1137 cqp_info->in.u.qp_create.scratch = (uintptr_t)cqp_request;
1138 status = i40iw_handle_cqp_op(iwdev, cqp_request);
1140 i40iw_pr_err("CQP-OP QP create fail");
1145 * i40iw_cqp_cq_destroy_cmd - destroy the cqp cq
1146 * @dev: device pointer
1147 * @cq: pointer to cq
1149 void i40iw_cqp_cq_destroy_cmd(struct i40iw_sc_dev *dev, struct i40iw_sc_cq *cq)
1151 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1153 i40iw_cq_wq_destroy(iwdev, cq);
1157 * i40iw_cqp_qp_destroy_cmd - destroy the cqp
1158 * @dev: device pointer
1159 * @qp: pointer to qp
1161 void i40iw_cqp_qp_destroy_cmd(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
1163 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1164 struct i40iw_cqp *iwcqp = &iwdev->cqp;
1165 struct i40iw_cqp_request *cqp_request;
1166 struct cqp_commands_info *cqp_info;
1167 enum i40iw_status_code status;
1169 cqp_request = i40iw_get_cqp_request(iwcqp, true);
1173 cqp_info = &cqp_request->info;
1174 memset(cqp_info, 0, sizeof(*cqp_info));
1176 cqp_info->cqp_cmd = OP_QP_DESTROY;
1177 cqp_info->post_sq = 1;
1178 cqp_info->in.u.qp_destroy.qp = qp;
1179 cqp_info->in.u.qp_destroy.scratch = (uintptr_t)cqp_request;
1180 cqp_info->in.u.qp_destroy.remove_hash_idx = true;
1181 status = i40iw_handle_cqp_op(iwdev, cqp_request);
1183 i40iw_pr_err("CQP QP_DESTROY fail");
1188 * i40iw_ieq_mpa_crc_ae - generate AE for crc error
1189 * @dev: hardware control device structure
1190 * @qp: hardware control qp
1192 void i40iw_ieq_mpa_crc_ae(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
1194 struct i40iw_qp_flush_info info;
1195 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1197 i40iw_debug(dev, I40IW_DEBUG_AEQ, "%s entered\n", __func__);
1198 memset(&info, 0, sizeof(info));
1199 info.ae_code = I40IW_AE_LLP_RECEIVED_MPA_CRC_ERROR;
1200 info.generate_ae = true;
1201 info.ae_source = 0x3;
1202 (void)i40iw_hw_flush_wqes(iwdev, qp, &info, false);
1206 * i40iw_init_hash_desc - initialize hash for crc calculation
1207 * @desc: cryption type
1209 enum i40iw_status_code i40iw_init_hash_desc(struct shash_desc **desc)
1211 struct crypto_shash *tfm;
1212 struct shash_desc *tdesc;
1214 tfm = crypto_alloc_shash("crc32c", 0, 0);
1216 return I40IW_ERR_MPA_CRC;
1218 tdesc = kzalloc(sizeof(*tdesc) + crypto_shash_descsize(tfm),
1221 crypto_free_shash(tfm);
1222 return I40IW_ERR_MPA_CRC;
1231 * i40iw_free_hash_desc - free hash desc
1232 * @desc: to be freed
1234 void i40iw_free_hash_desc(struct shash_desc *desc)
1237 crypto_free_shash(desc->tfm);
1243 * i40iw_alloc_query_fpm_buf - allocate buffer for fpm
1244 * @dev: hardware control device structure
1245 * @mem: buffer ptr for fpm to be allocated
1246 * @return: memory allocation status
1248 enum i40iw_status_code i40iw_alloc_query_fpm_buf(struct i40iw_sc_dev *dev,
1249 struct i40iw_dma_mem *mem)
1251 enum i40iw_status_code status;
1252 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1254 status = i40iw_obj_aligned_mem(iwdev, mem, I40IW_QUERY_FPM_BUF_SIZE,
1255 I40IW_FPM_QUERY_BUF_ALIGNMENT_MASK);
1260 * i40iw_ieq_check_mpacrc - check if mpa crc is OK
1261 * @desc: desc for hash
1262 * @addr: address of buffer for crc
1263 * @length: length of buffer
1264 * @value: value to be compared
1266 enum i40iw_status_code i40iw_ieq_check_mpacrc(struct shash_desc *desc,
1273 enum i40iw_status_code ret_code = 0;
1275 crypto_shash_init(desc);
1276 ret = crypto_shash_update(desc, addr, length);
1278 crypto_shash_final(desc, (u8 *)&crc);
1280 i40iw_pr_err("mpa crc check fail\n");
1281 ret_code = I40IW_ERR_MPA_CRC;
1287 * i40iw_ieq_get_qp - get qp based on quad in puda buffer
1288 * @dev: hardware control device structure
1289 * @buf: receive puda buffer on exception q
1291 struct i40iw_sc_qp *i40iw_ieq_get_qp(struct i40iw_sc_dev *dev,
1292 struct i40iw_puda_buf *buf)
1294 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1295 struct i40iw_qp *iwqp;
1296 struct i40iw_cm_node *cm_node;
1297 u32 loc_addr[4], rem_addr[4];
1298 u16 loc_port, rem_port;
1299 struct ipv6hdr *ip6h;
1300 struct iphdr *iph = (struct iphdr *)buf->iph;
1301 struct tcphdr *tcph = (struct tcphdr *)buf->tcph;
1303 if (iph->version == 4) {
1304 memset(loc_addr, 0, sizeof(loc_addr));
1305 loc_addr[0] = ntohl(iph->daddr);
1306 memset(rem_addr, 0, sizeof(rem_addr));
1307 rem_addr[0] = ntohl(iph->saddr);
1309 ip6h = (struct ipv6hdr *)buf->iph;
1310 i40iw_copy_ip_ntohl(loc_addr, ip6h->daddr.in6_u.u6_addr32);
1311 i40iw_copy_ip_ntohl(rem_addr, ip6h->saddr.in6_u.u6_addr32);
1313 loc_port = ntohs(tcph->dest);
1314 rem_port = ntohs(tcph->source);
1316 cm_node = i40iw_find_node(&iwdev->cm_core, rem_port, rem_addr, loc_port,
1320 iwqp = cm_node->iwqp;
1321 return &iwqp->sc_qp;
1325 * i40iw_ieq_update_tcpip_info - update tcpip in the buffer
1326 * @buf: puda to update
1327 * @length: length of buffer
1328 * @seqnum: seq number for tcp
1330 void i40iw_ieq_update_tcpip_info(struct i40iw_puda_buf *buf, u16 length, u32 seqnum)
1332 struct tcphdr *tcph;
1336 u8 *addr = (u8 *)buf->mem.va;
1338 iphlen = (buf->ipv4) ? 20 : 40;
1339 iph = (struct iphdr *)(addr + buf->maclen);
1340 tcph = (struct tcphdr *)(addr + buf->maclen + iphlen);
1341 packetsize = length + buf->tcphlen + iphlen;
1343 iph->tot_len = htons(packetsize);
1344 tcph->seq = htonl(seqnum);
1348 * i40iw_puda_get_tcpip_info - get tcpip info from puda buffer
1349 * @info: to get information
1352 enum i40iw_status_code i40iw_puda_get_tcpip_info(struct i40iw_puda_completion_info *info,
1353 struct i40iw_puda_buf *buf)
1356 struct ipv6hdr *ip6h;
1357 struct tcphdr *tcph;
1360 u8 *mem = (u8 *)buf->mem.va;
1361 struct ethhdr *ethh = (struct ethhdr *)buf->mem.va;
1363 if (ethh->h_proto == htons(0x8100)) {
1364 info->vlan_valid = true;
1365 buf->vlan_id = ntohs(((struct vlan_ethhdr *)ethh)->h_vlan_TCI) & VLAN_VID_MASK;
1367 buf->maclen = (info->vlan_valid) ? 18 : 14;
1368 iphlen = (info->l3proto) ? 40 : 20;
1369 buf->ipv4 = (info->l3proto) ? false : true;
1370 buf->iph = mem + buf->maclen;
1371 iph = (struct iphdr *)buf->iph;
1373 buf->tcph = buf->iph + iphlen;
1374 tcph = (struct tcphdr *)buf->tcph;
1377 pkt_len = ntohs(iph->tot_len);
1379 ip6h = (struct ipv6hdr *)buf->iph;
1380 pkt_len = ntohs(ip6h->payload_len) + iphlen;
1383 buf->totallen = pkt_len + buf->maclen;
1385 if (info->payload_len < buf->totallen) {
1386 i40iw_pr_err("payload_len = 0x%x totallen expected0x%x\n",
1387 info->payload_len, buf->totallen);
1388 return I40IW_ERR_INVALID_SIZE;
1391 buf->tcphlen = (tcph->doff) << 2;
1392 buf->datalen = pkt_len - iphlen - buf->tcphlen;
1393 buf->data = (buf->datalen) ? buf->tcph + buf->tcphlen : NULL;
1394 buf->hdrlen = buf->maclen + iphlen + buf->tcphlen;
1395 buf->seqnum = ntohl(tcph->seq);
1400 * i40iw_hw_stats_timeout - Stats timer-handler which updates all HW stats
1401 * @vsi: pointer to the vsi structure
1403 static void i40iw_hw_stats_timeout(unsigned long vsi)
1405 struct i40iw_sc_vsi *sc_vsi = (struct i40iw_sc_vsi *)vsi;
1406 struct i40iw_sc_dev *pf_dev = sc_vsi->dev;
1407 struct i40iw_vsi_pestat *pf_devstat = sc_vsi->pestat;
1408 struct i40iw_vsi_pestat *vf_devstat = NULL;
1410 unsigned long flags;
1413 i40iw_hw_stats_read_all(pf_devstat, &pf_devstat->hw_stats);
1415 for (iw_vf_idx = 0; iw_vf_idx < I40IW_MAX_PE_ENABLED_VF_COUNT; iw_vf_idx++) {
1416 spin_lock_irqsave(&pf_devstat->lock, flags);
1417 if (pf_dev->vf_dev[iw_vf_idx]) {
1418 if (pf_dev->vf_dev[iw_vf_idx]->stats_initialized) {
1419 vf_devstat = &pf_dev->vf_dev[iw_vf_idx]->pestat;
1420 i40iw_hw_stats_read_all(vf_devstat, &vf_devstat->hw_stats);
1423 spin_unlock_irqrestore(&pf_devstat->lock, flags);
1426 mod_timer(&pf_devstat->stats_timer,
1427 jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
1431 * i40iw_hw_stats_start_timer - Start periodic stats timer
1432 * @vsi: pointer to the vsi structure
1434 void i40iw_hw_stats_start_timer(struct i40iw_sc_vsi *vsi)
1436 struct i40iw_vsi_pestat *devstat = vsi->pestat;
1438 setup_timer(&devstat->stats_timer, i40iw_hw_stats_timeout,
1439 (unsigned long)vsi);
1440 mod_timer(&devstat->stats_timer,
1441 jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
1445 * i40iw_hw_stats_stop_timer - Delete periodic stats timer
1446 * @vsi: pointer to the vsi structure
1448 void i40iw_hw_stats_stop_timer(struct i40iw_sc_vsi *vsi)
1450 struct i40iw_vsi_pestat *devstat = vsi->pestat;
1452 del_timer_sync(&devstat->stats_timer);