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;
157 hdl = i40iw_find_netdev(event_netdev);
161 iwdev = &hdl->device;
162 netdev = iwdev->ldev->netdev;
163 upper_dev = netdev_master_upper_dev_get(netdev);
164 if (netdev != event_netdev)
171 ((struct in_device *)upper_dev->ip_ptr)->ifa_list->ifa_address;
173 local_ipaddr = ifa->ifa_address;
174 local_ipaddr = ntohl(local_ipaddr);
175 i40iw_manage_arp_cache(iwdev,
184 ((struct in_device *)upper_dev->ip_ptr)->ifa_list->ifa_address;
186 local_ipaddr = ifa->ifa_address;
187 local_ipaddr = ntohl(local_ipaddr);
188 i40iw_manage_arp_cache(iwdev,
194 case NETDEV_CHANGEADDR:
195 /* Add the address to the IP table */
198 ((struct in_device *)upper_dev->ip_ptr)->ifa_list->ifa_address;
200 local_ipaddr = ifa->ifa_address;
202 local_ipaddr = ntohl(local_ipaddr);
203 i40iw_manage_arp_cache(iwdev,
216 * i40iw_inet6addr_event - system notifier for ipv6 netdev events
218 * @event: event for notifier
221 int i40iw_inet6addr_event(struct notifier_block *notifier,
225 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
226 struct net_device *event_netdev = ifa->idev->dev;
227 struct net_device *netdev;
228 struct i40iw_device *iwdev;
229 struct i40iw_handler *hdl;
230 __be32 local_ipaddr6[4];
232 hdl = i40iw_find_netdev(event_netdev);
236 iwdev = &hdl->device;
237 netdev = iwdev->ldev->netdev;
238 if (netdev != event_netdev)
243 i40iw_copy_ip_ntohl(local_ipaddr6, ifa->addr.in6_u.u6_addr32);
244 i40iw_manage_arp_cache(iwdev,
252 case NETDEV_CHANGEADDR:
253 i40iw_copy_ip_ntohl(local_ipaddr6, ifa->addr.in6_u.u6_addr32);
254 i40iw_manage_arp_cache(iwdev,
267 * i40iw_net_event - system notifier for net events
269 * @event: event for notifier
272 int i40iw_net_event(struct notifier_block *notifier, unsigned long event, void *ptr)
274 struct neighbour *neigh = ptr;
275 struct i40iw_device *iwdev;
276 struct i40iw_handler *iwhdl;
281 case NETEVENT_NEIGH_UPDATE:
282 iwhdl = i40iw_find_netdev((struct net_device *)neigh->dev);
285 iwdev = &iwhdl->device;
286 p = (__be32 *)neigh->primary_key;
287 i40iw_copy_ip_ntohl(local_ipaddr, p);
288 if (neigh->nud_state & NUD_VALID) {
289 i40iw_manage_arp_cache(iwdev,
296 i40iw_manage_arp_cache(iwdev,
310 * i40iw_get_cqp_request - get cqp struct
311 * @cqp: device cqp ptr
312 * @wait: cqp to be used in wait mode
314 struct i40iw_cqp_request *i40iw_get_cqp_request(struct i40iw_cqp *cqp, bool wait)
316 struct i40iw_cqp_request *cqp_request = NULL;
319 spin_lock_irqsave(&cqp->req_lock, flags);
320 if (!list_empty(&cqp->cqp_avail_reqs)) {
321 cqp_request = list_entry(cqp->cqp_avail_reqs.next,
322 struct i40iw_cqp_request, list);
323 list_del_init(&cqp_request->list);
325 spin_unlock_irqrestore(&cqp->req_lock, flags);
327 cqp_request = kzalloc(sizeof(*cqp_request), GFP_ATOMIC);
329 cqp_request->dynamic = true;
330 INIT_LIST_HEAD(&cqp_request->list);
331 init_waitqueue_head(&cqp_request->waitq);
335 i40iw_pr_err("CQP Request Fail: No Memory");
340 atomic_set(&cqp_request->refcount, 2);
341 cqp_request->waiting = true;
343 atomic_set(&cqp_request->refcount, 1);
349 * i40iw_free_cqp_request - free cqp request
351 * @cqp_request: to be put back in cqp list
353 void i40iw_free_cqp_request(struct i40iw_cqp *cqp, struct i40iw_cqp_request *cqp_request)
357 if (cqp_request->dynamic) {
360 cqp_request->request_done = false;
361 cqp_request->callback_fcn = NULL;
362 cqp_request->waiting = false;
364 spin_lock_irqsave(&cqp->req_lock, flags);
365 list_add_tail(&cqp_request->list, &cqp->cqp_avail_reqs);
366 spin_unlock_irqrestore(&cqp->req_lock, flags);
371 * i40iw_put_cqp_request - dec ref count and free if 0
373 * @cqp_request: to be put back in cqp list
375 void i40iw_put_cqp_request(struct i40iw_cqp *cqp,
376 struct i40iw_cqp_request *cqp_request)
378 if (atomic_dec_and_test(&cqp_request->refcount))
379 i40iw_free_cqp_request(cqp, cqp_request);
383 * i40iw_free_qp - callback after destroy cqp completes
384 * @cqp_request: cqp request for destroy qp
387 static void i40iw_free_qp(struct i40iw_cqp_request *cqp_request, u32 num)
389 struct i40iw_sc_qp *qp = (struct i40iw_sc_qp *)cqp_request->param;
390 struct i40iw_qp *iwqp = (struct i40iw_qp *)qp->back_qp;
391 struct i40iw_device *iwdev;
392 u32 qp_num = iwqp->ibqp.qp_num;
396 i40iw_rem_pdusecount(iwqp->iwpd, iwdev);
397 i40iw_free_qp_resources(iwdev, iwqp, qp_num);
401 * i40iw_wait_event - wait for completion
402 * @iwdev: iwarp device
403 * @cqp_request: cqp request to wait
405 static int i40iw_wait_event(struct i40iw_device *iwdev,
406 struct i40iw_cqp_request *cqp_request)
408 struct cqp_commands_info *info = &cqp_request->info;
409 struct i40iw_cqp *iwcqp = &iwdev->cqp;
410 bool cqp_error = false;
414 timeout_ret = wait_event_timeout(cqp_request->waitq,
415 cqp_request->request_done,
416 I40IW_EVENT_TIMEOUT);
418 i40iw_pr_err("error cqp command 0x%x timed out ret = %d\n",
419 info->cqp_cmd, timeout_ret);
421 i40iw_request_reset(iwdev);
424 cqp_error = cqp_request->compl_info.error;
426 i40iw_pr_err("error cqp command 0x%x completion maj = 0x%x min=0x%x\n",
427 info->cqp_cmd, cqp_request->compl_info.maj_err_code,
428 cqp_request->compl_info.min_err_code);
433 i40iw_put_cqp_request(iwcqp, cqp_request);
438 * i40iw_handle_cqp_op - process cqp command
439 * @iwdev: iwarp device
440 * @cqp_request: cqp request to process
442 enum i40iw_status_code i40iw_handle_cqp_op(struct i40iw_device *iwdev,
443 struct i40iw_cqp_request
446 struct i40iw_sc_dev *dev = &iwdev->sc_dev;
447 enum i40iw_status_code status;
448 struct cqp_commands_info *info = &cqp_request->info;
451 status = i40iw_process_cqp_cmd(dev, info);
453 i40iw_pr_err("error cqp command 0x%x failed\n", info->cqp_cmd);
454 i40iw_free_cqp_request(&iwdev->cqp, cqp_request);
457 if (cqp_request->waiting)
458 err_code = i40iw_wait_event(iwdev, cqp_request);
460 status = I40IW_ERR_CQP_COMPL_ERROR;
465 * i40iw_add_pdusecount - add pd refcount
466 * @iwpd: pd for refcount
468 void i40iw_add_pdusecount(struct i40iw_pd *iwpd)
470 atomic_inc(&iwpd->usecount);
474 * i40iw_rem_pdusecount - decrement refcount for pd and free if 0
475 * @iwpd: pd for refcount
476 * @iwdev: iwarp device
478 void i40iw_rem_pdusecount(struct i40iw_pd *iwpd, struct i40iw_device *iwdev)
480 if (!atomic_dec_and_test(&iwpd->usecount))
482 i40iw_free_resource(iwdev, iwdev->allocated_pds, iwpd->sc_pd.pd_id);
487 * i40iw_add_ref - add refcount for qp
490 void i40iw_add_ref(struct ib_qp *ibqp)
492 struct i40iw_qp *iwqp = (struct i40iw_qp *)ibqp;
494 atomic_inc(&iwqp->refcount);
498 * i40iw_rem_ref - rem refcount for qp and free if 0
501 void i40iw_rem_ref(struct ib_qp *ibqp)
503 struct i40iw_qp *iwqp;
504 enum i40iw_status_code status;
505 struct i40iw_cqp_request *cqp_request;
506 struct cqp_commands_info *cqp_info;
507 struct i40iw_device *iwdev;
511 iwqp = to_iwqp(ibqp);
513 spin_lock_irqsave(&iwdev->qptable_lock, flags);
514 if (!atomic_dec_and_test(&iwqp->refcount)) {
515 spin_unlock_irqrestore(&iwdev->qptable_lock, flags);
519 qp_num = iwqp->ibqp.qp_num;
520 iwdev->qp_table[qp_num] = NULL;
521 spin_unlock_irqrestore(&iwdev->qptable_lock, flags);
522 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
526 cqp_request->callback_fcn = i40iw_free_qp;
527 cqp_request->param = (void *)&iwqp->sc_qp;
528 cqp_info = &cqp_request->info;
529 cqp_info->cqp_cmd = OP_QP_DESTROY;
530 cqp_info->post_sq = 1;
531 cqp_info->in.u.qp_destroy.qp = &iwqp->sc_qp;
532 cqp_info->in.u.qp_destroy.scratch = (uintptr_t)cqp_request;
533 cqp_info->in.u.qp_destroy.remove_hash_idx = true;
534 status = i40iw_handle_cqp_op(iwdev, cqp_request);
536 i40iw_pr_err("CQP-OP Destroy QP fail");
540 * i40iw_get_qp - get qp address
541 * @device: iwarp device
544 struct ib_qp *i40iw_get_qp(struct ib_device *device, int qpn)
546 struct i40iw_device *iwdev = to_iwdev(device);
548 if ((qpn < IW_FIRST_QPN) || (qpn >= iwdev->max_qp))
551 return &iwdev->qp_table[qpn]->ibqp;
555 * i40iw_debug_buf - print debug msg and buffer is mask set
556 * @dev: hardware control device structure
557 * @mask: mask to compare if to print debug buffer
558 * @buf: points buffer addr
559 * @size: saize of buffer to print
561 void i40iw_debug_buf(struct i40iw_sc_dev *dev,
562 enum i40iw_debug_flag mask,
569 if (!(dev->debug_mask & mask))
571 i40iw_debug(dev, mask, "%s\n", desc);
572 i40iw_debug(dev, mask, "starting address virt=%p phy=%llxh\n", buf,
573 (unsigned long long)virt_to_phys(buf));
575 for (i = 0; i < size; i += 8)
576 i40iw_debug(dev, mask, "index %03d val: %016llx\n", i, buf[i / 8]);
580 * i40iw_get_hw_addr - return hw addr
581 * @par: points to shared dev
583 u8 __iomem *i40iw_get_hw_addr(void *par)
585 struct i40iw_sc_dev *dev = (struct i40iw_sc_dev *)par;
587 return dev->hw->hw_addr;
591 * i40iw_remove_head - return head entry and remove from list
592 * @list: list for entry
594 void *i40iw_remove_head(struct list_head *list)
596 struct list_head *entry;
598 if (list_empty(list))
601 entry = (void *)list->next;
603 return (void *)entry;
607 * i40iw_allocate_dma_mem - Memory alloc helper fn
608 * @hw: pointer to the HW structure
609 * @mem: ptr to mem struct to fill out
610 * @size: size of memory requested
611 * @alignment: what to align the allocation to
613 enum i40iw_status_code i40iw_allocate_dma_mem(struct i40iw_hw *hw,
614 struct i40iw_dma_mem *mem,
618 struct pci_dev *pcidev = (struct pci_dev *)hw->dev_context;
621 return I40IW_ERR_PARAM;
622 mem->size = ALIGN(size, alignment);
623 mem->va = dma_zalloc_coherent(&pcidev->dev, mem->size,
624 (dma_addr_t *)&mem->pa, GFP_KERNEL);
626 return I40IW_ERR_NO_MEMORY;
631 * i40iw_free_dma_mem - Memory free helper fn
632 * @hw: pointer to the HW structure
633 * @mem: ptr to mem struct to free
635 void i40iw_free_dma_mem(struct i40iw_hw *hw, struct i40iw_dma_mem *mem)
637 struct pci_dev *pcidev = (struct pci_dev *)hw->dev_context;
639 if (!mem || !mem->va)
642 dma_free_coherent(&pcidev->dev, mem->size,
643 mem->va, (dma_addr_t)mem->pa);
648 * i40iw_allocate_virt_mem - virtual memory alloc helper fn
649 * @hw: pointer to the HW structure
650 * @mem: ptr to mem struct to fill out
651 * @size: size of memory requested
653 enum i40iw_status_code i40iw_allocate_virt_mem(struct i40iw_hw *hw,
654 struct i40iw_virt_mem *mem,
658 return I40IW_ERR_PARAM;
661 mem->va = kzalloc(size, GFP_KERNEL);
666 return I40IW_ERR_NO_MEMORY;
670 * i40iw_free_virt_mem - virtual memory free helper fn
671 * @hw: pointer to the HW structure
672 * @mem: ptr to mem struct to free
674 enum i40iw_status_code i40iw_free_virt_mem(struct i40iw_hw *hw,
675 struct i40iw_virt_mem *mem)
678 return I40IW_ERR_PARAM;
685 * i40iw_cqp_sds_cmd - create cqp command for sd
686 * @dev: hardware control device structure
687 * @sd_info: information for sd cqp
690 enum i40iw_status_code i40iw_cqp_sds_cmd(struct i40iw_sc_dev *dev,
691 struct i40iw_update_sds_info *sdinfo)
693 enum i40iw_status_code status;
694 struct i40iw_cqp_request *cqp_request;
695 struct cqp_commands_info *cqp_info;
696 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
698 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
700 return I40IW_ERR_NO_MEMORY;
701 cqp_info = &cqp_request->info;
702 memcpy(&cqp_info->in.u.update_pe_sds.info, sdinfo,
703 sizeof(cqp_info->in.u.update_pe_sds.info));
704 cqp_info->cqp_cmd = OP_UPDATE_PE_SDS;
705 cqp_info->post_sq = 1;
706 cqp_info->in.u.update_pe_sds.dev = dev;
707 cqp_info->in.u.update_pe_sds.scratch = (uintptr_t)cqp_request;
708 status = i40iw_handle_cqp_op(iwdev, cqp_request);
710 i40iw_pr_err("CQP-OP Update SD's fail");
715 * i40iw_term_modify_qp - modify qp for term message
716 * @qp: hardware control qp
717 * @next_state: qp's next state
718 * @term: terminate code
721 void i40iw_term_modify_qp(struct i40iw_sc_qp *qp, u8 next_state, u8 term, u8 term_len)
723 struct i40iw_qp *iwqp;
725 iwqp = (struct i40iw_qp *)qp->back_qp;
726 i40iw_next_iw_state(iwqp, next_state, 0, term, term_len);
730 * i40iw_terminate_done - after terminate is completed
731 * @qp: hardware control qp
732 * @timeout_occurred: indicates if terminate timer expired
734 void i40iw_terminate_done(struct i40iw_sc_qp *qp, int timeout_occurred)
736 struct i40iw_qp *iwqp;
737 u32 next_iwarp_state = I40IW_QP_STATE_ERROR;
742 iwqp = (struct i40iw_qp *)qp->back_qp;
743 spin_lock_irqsave(&iwqp->lock, flags);
744 if (iwqp->hte_added) {
748 first_time = !(qp->term_flags & I40IW_TERM_DONE);
749 qp->term_flags |= I40IW_TERM_DONE;
750 spin_unlock_irqrestore(&iwqp->lock, flags);
752 if (!timeout_occurred)
753 i40iw_terminate_del_timer(qp);
755 next_iwarp_state = I40IW_QP_STATE_CLOSING;
757 i40iw_next_iw_state(iwqp, next_iwarp_state, hte, 0, 0);
758 i40iw_cm_disconn(iwqp);
763 * i40iw_terminate_imeout - timeout happened
764 * @context: points to iwarp qp
766 static void i40iw_terminate_timeout(unsigned long context)
768 struct i40iw_qp *iwqp = (struct i40iw_qp *)context;
769 struct i40iw_sc_qp *qp = (struct i40iw_sc_qp *)&iwqp->sc_qp;
771 i40iw_terminate_done(qp, 1);
775 * i40iw_terminate_start_timer - start terminate timeout
776 * @qp: hardware control qp
778 void i40iw_terminate_start_timer(struct i40iw_sc_qp *qp)
780 struct i40iw_qp *iwqp;
782 iwqp = (struct i40iw_qp *)qp->back_qp;
783 init_timer(&iwqp->terminate_timer);
784 iwqp->terminate_timer.function = i40iw_terminate_timeout;
785 iwqp->terminate_timer.expires = jiffies + HZ;
786 iwqp->terminate_timer.data = (unsigned long)iwqp;
787 add_timer(&iwqp->terminate_timer);
791 * i40iw_terminate_del_timer - delete terminate timeout
792 * @qp: hardware control qp
794 void i40iw_terminate_del_timer(struct i40iw_sc_qp *qp)
796 struct i40iw_qp *iwqp;
798 iwqp = (struct i40iw_qp *)qp->back_qp;
799 del_timer(&iwqp->terminate_timer);
803 * i40iw_cqp_generic_worker - generic worker for cqp
804 * @work: work pointer
806 static void i40iw_cqp_generic_worker(struct work_struct *work)
808 struct i40iw_virtchnl_work_info *work_info =
809 &((struct virtchnl_work *)work)->work_info;
811 if (work_info->worker_vf_dev)
812 work_info->callback_fcn(work_info->worker_vf_dev);
816 * i40iw_cqp_spawn_worker - spawn worket thread
817 * @iwdev: device struct pointer
818 * @work_info: work request info
819 * @iw_vf_idx: virtual function index
821 void i40iw_cqp_spawn_worker(struct i40iw_sc_dev *dev,
822 struct i40iw_virtchnl_work_info *work_info,
825 struct virtchnl_work *work;
826 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
828 work = &iwdev->virtchnl_w[iw_vf_idx];
829 memcpy(&work->work_info, work_info, sizeof(*work_info));
830 INIT_WORK(&work->work, i40iw_cqp_generic_worker);
831 queue_work(iwdev->virtchnl_wq, &work->work);
835 * i40iw_cqp_manage_hmc_fcn_worker -
836 * @work: work pointer for hmc info
838 static void i40iw_cqp_manage_hmc_fcn_worker(struct work_struct *work)
840 struct i40iw_cqp_request *cqp_request =
841 ((struct virtchnl_work *)work)->cqp_request;
842 struct i40iw_ccq_cqe_info ccq_cqe_info;
843 struct i40iw_hmc_fcn_info *hmcfcninfo =
844 &cqp_request->info.in.u.manage_hmc_pm.info;
845 struct i40iw_device *iwdev =
846 (struct i40iw_device *)cqp_request->info.in.u.manage_hmc_pm.dev->back_dev;
848 ccq_cqe_info.cqp = NULL;
849 ccq_cqe_info.maj_err_code = cqp_request->compl_info.maj_err_code;
850 ccq_cqe_info.min_err_code = cqp_request->compl_info.min_err_code;
851 ccq_cqe_info.op_code = cqp_request->compl_info.op_code;
852 ccq_cqe_info.op_ret_val = cqp_request->compl_info.op_ret_val;
853 ccq_cqe_info.scratch = 0;
854 ccq_cqe_info.error = cqp_request->compl_info.error;
855 hmcfcninfo->callback_fcn(cqp_request->info.in.u.manage_hmc_pm.dev,
856 hmcfcninfo->cqp_callback_param, &ccq_cqe_info);
857 i40iw_put_cqp_request(&iwdev->cqp, cqp_request);
861 * i40iw_cqp_manage_hmc_fcn_callback - called function after cqp completion
862 * @cqp_request: cqp request info struct for hmc fun
863 * @unused: unused param of callback
865 static void i40iw_cqp_manage_hmc_fcn_callback(struct i40iw_cqp_request *cqp_request,
868 struct virtchnl_work *work;
869 struct i40iw_hmc_fcn_info *hmcfcninfo =
870 &cqp_request->info.in.u.manage_hmc_pm.info;
871 struct i40iw_device *iwdev =
872 (struct i40iw_device *)cqp_request->info.in.u.manage_hmc_pm.dev->
875 if (hmcfcninfo && hmcfcninfo->callback_fcn) {
876 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s1\n", __func__);
877 atomic_inc(&cqp_request->refcount);
878 work = &iwdev->virtchnl_w[hmcfcninfo->iw_vf_idx];
879 work->cqp_request = cqp_request;
880 INIT_WORK(&work->work, i40iw_cqp_manage_hmc_fcn_worker);
881 queue_work(iwdev->virtchnl_wq, &work->work);
882 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s2\n", __func__);
884 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s: Something wrong\n", __func__);
889 * i40iw_cqp_manage_hmc_fcn_cmd - issue cqp command to manage hmc
890 * @dev: hardware control device structure
891 * @hmcfcninfo: info for hmc
893 enum i40iw_status_code i40iw_cqp_manage_hmc_fcn_cmd(struct i40iw_sc_dev *dev,
894 struct i40iw_hmc_fcn_info *hmcfcninfo)
896 enum i40iw_status_code status;
897 struct i40iw_cqp_request *cqp_request;
898 struct cqp_commands_info *cqp_info;
899 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
901 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s\n", __func__);
902 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
904 return I40IW_ERR_NO_MEMORY;
905 cqp_info = &cqp_request->info;
906 cqp_request->callback_fcn = i40iw_cqp_manage_hmc_fcn_callback;
907 cqp_request->param = hmcfcninfo;
908 memcpy(&cqp_info->in.u.manage_hmc_pm.info, hmcfcninfo,
909 sizeof(*hmcfcninfo));
910 cqp_info->in.u.manage_hmc_pm.dev = dev;
911 cqp_info->cqp_cmd = OP_MANAGE_HMC_PM_FUNC_TABLE;
912 cqp_info->post_sq = 1;
913 cqp_info->in.u.manage_hmc_pm.scratch = (uintptr_t)cqp_request;
914 status = i40iw_handle_cqp_op(iwdev, cqp_request);
916 i40iw_pr_err("CQP-OP Manage HMC fail");
921 * i40iw_cqp_query_fpm_values_cmd - send cqp command for fpm
922 * @iwdev: function device struct
923 * @values_mem: buffer for fpm
924 * @hmc_fn_id: function id for fpm
926 enum i40iw_status_code i40iw_cqp_query_fpm_values_cmd(struct i40iw_sc_dev *dev,
927 struct i40iw_dma_mem *values_mem,
930 enum i40iw_status_code status;
931 struct i40iw_cqp_request *cqp_request;
932 struct cqp_commands_info *cqp_info;
933 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
935 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
937 return I40IW_ERR_NO_MEMORY;
938 cqp_info = &cqp_request->info;
939 cqp_request->param = NULL;
940 cqp_info->in.u.query_fpm_values.cqp = dev->cqp;
941 cqp_info->in.u.query_fpm_values.fpm_values_pa = values_mem->pa;
942 cqp_info->in.u.query_fpm_values.fpm_values_va = values_mem->va;
943 cqp_info->in.u.query_fpm_values.hmc_fn_id = hmc_fn_id;
944 cqp_info->cqp_cmd = OP_QUERY_FPM_VALUES;
945 cqp_info->post_sq = 1;
946 cqp_info->in.u.query_fpm_values.scratch = (uintptr_t)cqp_request;
947 status = i40iw_handle_cqp_op(iwdev, cqp_request);
949 i40iw_pr_err("CQP-OP Query FPM fail");
954 * i40iw_cqp_commit_fpm_values_cmd - commit fpm values in hw
955 * @dev: hardware control device structure
956 * @values_mem: buffer with fpm values
957 * @hmc_fn_id: function id for fpm
959 enum i40iw_status_code i40iw_cqp_commit_fpm_values_cmd(struct i40iw_sc_dev *dev,
960 struct i40iw_dma_mem *values_mem,
963 enum i40iw_status_code status;
964 struct i40iw_cqp_request *cqp_request;
965 struct cqp_commands_info *cqp_info;
966 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
968 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
970 return I40IW_ERR_NO_MEMORY;
971 cqp_info = &cqp_request->info;
972 cqp_request->param = NULL;
973 cqp_info->in.u.commit_fpm_values.cqp = dev->cqp;
974 cqp_info->in.u.commit_fpm_values.fpm_values_pa = values_mem->pa;
975 cqp_info->in.u.commit_fpm_values.fpm_values_va = values_mem->va;
976 cqp_info->in.u.commit_fpm_values.hmc_fn_id = hmc_fn_id;
977 cqp_info->cqp_cmd = OP_COMMIT_FPM_VALUES;
978 cqp_info->post_sq = 1;
979 cqp_info->in.u.commit_fpm_values.scratch = (uintptr_t)cqp_request;
980 status = i40iw_handle_cqp_op(iwdev, cqp_request);
982 i40iw_pr_err("CQP-OP Commit FPM fail");
987 * i40iw_vf_wait_vchnl_resp - wait for channel msg
988 * @iwdev: function's device struct
990 enum i40iw_status_code i40iw_vf_wait_vchnl_resp(struct i40iw_sc_dev *dev)
992 struct i40iw_device *iwdev = dev->back_dev;
995 i40iw_debug(dev, I40IW_DEBUG_VIRT, "%s[%u] dev %p, iwdev %p\n",
996 __func__, __LINE__, dev, iwdev);
998 atomic_set(&iwdev->vchnl_msgs, 2);
999 timeout_ret = wait_event_timeout(iwdev->vchnl_waitq,
1000 (atomic_read(&iwdev->vchnl_msgs) == 1),
1001 I40IW_VCHNL_EVENT_TIMEOUT);
1002 atomic_dec(&iwdev->vchnl_msgs);
1004 i40iw_pr_err("virt channel completion timeout = 0x%x\n", timeout_ret);
1005 atomic_set(&iwdev->vchnl_msgs, 0);
1006 dev->vchnl_up = false;
1007 return I40IW_ERR_TIMEOUT;
1009 wake_up(&dev->vf_reqs);
1014 * i40iw_ieq_mpa_crc_ae - generate AE for crc error
1015 * @dev: hardware control device structure
1016 * @qp: hardware control qp
1018 void i40iw_ieq_mpa_crc_ae(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
1020 struct i40iw_qp_flush_info info;
1021 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1023 i40iw_debug(dev, I40IW_DEBUG_AEQ, "%s entered\n", __func__);
1024 memset(&info, 0, sizeof(info));
1025 info.ae_code = I40IW_AE_LLP_RECEIVED_MPA_CRC_ERROR;
1026 info.generate_ae = true;
1027 info.ae_source = 0x3;
1028 (void)i40iw_hw_flush_wqes(iwdev, qp, &info, false);
1032 * i40iw_init_hash_desc - initialize hash for crc calculation
1033 * @desc: cryption type
1035 enum i40iw_status_code i40iw_init_hash_desc(struct shash_desc **desc)
1037 struct crypto_shash *tfm;
1038 struct shash_desc *tdesc;
1040 tfm = crypto_alloc_shash("crc32c", 0, 0);
1042 return I40IW_ERR_MPA_CRC;
1044 tdesc = kzalloc(sizeof(*tdesc) + crypto_shash_descsize(tfm),
1047 crypto_free_shash(tfm);
1048 return I40IW_ERR_MPA_CRC;
1057 * i40iw_free_hash_desc - free hash desc
1058 * @desc: to be freed
1060 void i40iw_free_hash_desc(struct shash_desc *desc)
1063 crypto_free_shash(desc->tfm);
1069 * i40iw_alloc_query_fpm_buf - allocate buffer for fpm
1070 * @dev: hardware control device structure
1071 * @mem: buffer ptr for fpm to be allocated
1072 * @return: memory allocation status
1074 enum i40iw_status_code i40iw_alloc_query_fpm_buf(struct i40iw_sc_dev *dev,
1075 struct i40iw_dma_mem *mem)
1077 enum i40iw_status_code status;
1078 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1080 status = i40iw_obj_aligned_mem(iwdev, mem, I40IW_QUERY_FPM_BUF_SIZE,
1081 I40IW_FPM_QUERY_BUF_ALIGNMENT_MASK);
1086 * i40iw_ieq_check_mpacrc - check if mpa crc is OK
1087 * @desc: desc for hash
1088 * @addr: address of buffer for crc
1089 * @length: length of buffer
1090 * @value: value to be compared
1092 enum i40iw_status_code i40iw_ieq_check_mpacrc(struct shash_desc *desc,
1099 enum i40iw_status_code ret_code = 0;
1101 crypto_shash_init(desc);
1102 ret = crypto_shash_update(desc, addr, length);
1104 crypto_shash_final(desc, (u8 *)&crc);
1106 i40iw_pr_err("mpa crc check fail\n");
1107 ret_code = I40IW_ERR_MPA_CRC;
1113 * i40iw_ieq_get_qp - get qp based on quad in puda buffer
1114 * @dev: hardware control device structure
1115 * @buf: receive puda buffer on exception q
1117 struct i40iw_sc_qp *i40iw_ieq_get_qp(struct i40iw_sc_dev *dev,
1118 struct i40iw_puda_buf *buf)
1120 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1121 struct i40iw_qp *iwqp;
1122 struct i40iw_cm_node *cm_node;
1123 u32 loc_addr[4], rem_addr[4];
1124 u16 loc_port, rem_port;
1125 struct ipv6hdr *ip6h;
1126 struct iphdr *iph = (struct iphdr *)buf->iph;
1127 struct tcphdr *tcph = (struct tcphdr *)buf->tcph;
1129 if (iph->version == 4) {
1130 memset(loc_addr, 0, sizeof(loc_addr));
1131 loc_addr[0] = ntohl(iph->daddr);
1132 memset(rem_addr, 0, sizeof(rem_addr));
1133 rem_addr[0] = ntohl(iph->saddr);
1135 ip6h = (struct ipv6hdr *)buf->iph;
1136 i40iw_copy_ip_ntohl(loc_addr, ip6h->daddr.in6_u.u6_addr32);
1137 i40iw_copy_ip_ntohl(rem_addr, ip6h->saddr.in6_u.u6_addr32);
1139 loc_port = ntohs(tcph->dest);
1140 rem_port = ntohs(tcph->source);
1142 cm_node = i40iw_find_node(&iwdev->cm_core, rem_port, rem_addr, loc_port,
1146 iwqp = cm_node->iwqp;
1147 return &iwqp->sc_qp;
1151 * i40iw_ieq_update_tcpip_info - update tcpip in the buffer
1152 * @buf: puda to update
1153 * @length: length of buffer
1154 * @seqnum: seq number for tcp
1156 void i40iw_ieq_update_tcpip_info(struct i40iw_puda_buf *buf, u16 length, u32 seqnum)
1158 struct tcphdr *tcph;
1162 u8 *addr = (u8 *)buf->mem.va;
1164 iphlen = (buf->ipv4) ? 20 : 40;
1165 iph = (struct iphdr *)(addr + buf->maclen);
1166 tcph = (struct tcphdr *)(addr + buf->maclen + iphlen);
1167 packetsize = length + buf->tcphlen + iphlen;
1169 iph->tot_len = htons(packetsize);
1170 tcph->seq = htonl(seqnum);
1174 * i40iw_puda_get_tcpip_info - get tcpip info from puda buffer
1175 * @info: to get information
1178 enum i40iw_status_code i40iw_puda_get_tcpip_info(struct i40iw_puda_completion_info *info,
1179 struct i40iw_puda_buf *buf)
1182 struct ipv6hdr *ip6h;
1183 struct tcphdr *tcph;
1186 u8 *mem = (u8 *)buf->mem.va;
1187 struct ethhdr *ethh = (struct ethhdr *)buf->mem.va;
1189 if (ethh->h_proto == htons(0x8100)) {
1190 info->vlan_valid = true;
1191 buf->vlan_id = ntohs(((struct vlan_ethhdr *)ethh)->h_vlan_TCI) & VLAN_VID_MASK;
1193 buf->maclen = (info->vlan_valid) ? 18 : 14;
1194 iphlen = (info->l3proto) ? 40 : 20;
1195 buf->ipv4 = (info->l3proto) ? false : true;
1196 buf->iph = mem + buf->maclen;
1197 iph = (struct iphdr *)buf->iph;
1199 buf->tcph = buf->iph + iphlen;
1200 tcph = (struct tcphdr *)buf->tcph;
1203 pkt_len = ntohs(iph->tot_len);
1205 ip6h = (struct ipv6hdr *)buf->iph;
1206 pkt_len = ntohs(ip6h->payload_len) + iphlen;
1209 buf->totallen = pkt_len + buf->maclen;
1211 if (info->payload_len < buf->totallen - 4) {
1212 i40iw_pr_err("payload_len = 0x%x totallen expected0x%x\n",
1213 info->payload_len, buf->totallen);
1214 return I40IW_ERR_INVALID_SIZE;
1217 buf->tcphlen = (tcph->doff) << 2;
1218 buf->datalen = pkt_len - iphlen - buf->tcphlen;
1219 buf->data = (buf->datalen) ? buf->tcph + buf->tcphlen : NULL;
1220 buf->hdrlen = buf->maclen + iphlen + buf->tcphlen;
1221 buf->seqnum = ntohl(tcph->seq);
1226 * i40iw_hw_stats_timeout - Stats timer-handler which updates all HW stats
1227 * @dev: hardware control device structure
1229 static void i40iw_hw_stats_timeout(unsigned long dev)
1231 struct i40iw_sc_dev *pf_dev = (struct i40iw_sc_dev *)dev;
1232 struct i40iw_dev_pestat *pf_devstat = &pf_dev->dev_pestat;
1233 struct i40iw_dev_pestat *vf_devstat = NULL;
1235 unsigned long flags;
1238 pf_devstat->ops.iw_hw_stat_read_all(pf_devstat, &pf_devstat->hw_stats);
1239 for (iw_vf_idx = 0; iw_vf_idx < I40IW_MAX_PE_ENABLED_VF_COUNT; iw_vf_idx++) {
1240 spin_lock_irqsave(&pf_devstat->stats_lock, flags);
1241 if (pf_dev->vf_dev[iw_vf_idx]) {
1242 if (pf_dev->vf_dev[iw_vf_idx]->stats_initialized) {
1243 vf_devstat = &pf_dev->vf_dev[iw_vf_idx]->dev_pestat;
1244 vf_devstat->ops.iw_hw_stat_read_all(vf_devstat, &vf_devstat->hw_stats);
1247 spin_unlock_irqrestore(&pf_devstat->stats_lock, flags);
1250 mod_timer(&pf_devstat->stats_timer,
1251 jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
1255 * i40iw_hw_stats_start_timer - Start periodic stats timer
1256 * @dev: hardware control device structure
1258 void i40iw_hw_stats_start_timer(struct i40iw_sc_dev *dev)
1260 struct i40iw_dev_pestat *devstat = &dev->dev_pestat;
1262 init_timer(&devstat->stats_timer);
1263 devstat->stats_timer.function = i40iw_hw_stats_timeout;
1264 devstat->stats_timer.data = (unsigned long)dev;
1265 mod_timer(&devstat->stats_timer,
1266 jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
1270 * i40iw_hw_stats_del_timer - Delete periodic stats timer
1271 * @dev: hardware control device structure
1273 void i40iw_hw_stats_del_timer(struct i40iw_sc_dev *dev)
1275 struct i40iw_dev_pestat *devstat = &dev->dev_pestat;
1277 del_timer_sync(&devstat->stats_timer);