blob: 58151280828d72c8462f516453a8b18639e4bfc1 [file] [log] [blame]
Faisal Latif4e9042e2016-01-20 13:40:08 -06001/*******************************************************************************
2*
3* Copyright (c) 2015-2016 Intel Corporation. All rights reserved.
4*
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:
10*
11* Redistribution and use in source and binary forms, with or
12* without modification, are permitted provided that the following
13* conditions are met:
14*
15* - Redistributions of source code must retain the above
16* copyright notice, this list of conditions and the following
17* disclaimer.
18*
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.
23*
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
31* SOFTWARE.
32*
33*******************************************************************************/
34
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>
43#include <linux/in.h>
44#include <linux/ip.h>
45#include <linux/tcp.h>
46#include <linux/init.h>
47#include <linux/io.h>
48#include <asm/irq.h>
49#include <asm/byteorder.h>
50#include <net/netevent.h>
51#include <net/neighbour.h>
52#include "i40iw.h"
53
54/**
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
60 */
61int i40iw_arp_table(struct i40iw_device *iwdev,
Ismail, Mustafa20c61f72016-04-18 10:33:07 -050062 u32 *ip_addr,
Faisal Latif4e9042e2016-01-20 13:40:08 -060063 bool ipv4,
64 u8 *mac_addr,
65 u32 action)
66{
67 int arp_index;
68 int err;
69 u32 ip[4];
70
71 if (ipv4) {
72 memset(ip, 0, sizeof(ip));
73 ip[0] = *ip_addr;
74 } else {
75 memcpy(ip, ip_addr, sizeof(ip));
76 }
77
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)
80 break;
81 switch (action) {
82 case I40IW_ARP_ADD:
83 if (arp_index != iwdev->arp_table_size)
84 return -1;
85
86 arp_index = 0;
87 err = i40iw_alloc_resource(iwdev, iwdev->allocated_arps,
88 iwdev->arp_table_size,
89 (u32 *)&arp_index,
90 &iwdev->next_arp_index);
91
92 if (err)
93 return err;
94
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);
97 break;
98 case I40IW_ARP_RESOLVE:
99 if (arp_index == iwdev->arp_table_size)
100 return -1;
101 break;
102 case I40IW_ARP_DELETE:
103 if (arp_index == iwdev->arp_table_size)
104 return -1;
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);
109 break;
110 default:
111 return -1;
112 }
113 return arp_index;
114}
115
116/**
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
121 */
122inline void i40iw_wr32(struct i40iw_hw *hw, u32 reg, u32 value)
123{
124 writel(value, hw->hw_addr + reg);
125}
126
127/**
128 * i40iw_rd32 - read a 32 bit hw register
129 * @hw: hardware information including registers
130 * @reg: register offset
131 *
132 * Return value of register content
133 */
134inline u32 i40iw_rd32(struct i40iw_hw *hw, u32 reg)
135{
136 return readl(hw->hw_addr + reg);
137}
138
139/**
140 * i40iw_inetaddr_event - system notifier for netdev events
141 * @notfier: not used
142 * @event: event for notifier
143 * @ptr: if address
144 */
145int i40iw_inetaddr_event(struct notifier_block *notifier,
146 unsigned long event,
147 void *ptr)
148{
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;
Ismail, Mustafa20c61f72016-04-18 10:33:07 -0500155 u32 local_ipaddr;
Faisal Latif4e9042e2016-01-20 13:40:08 -0600156
157 hdl = i40iw_find_netdev(event_netdev);
158 if (!hdl)
159 return NOTIFY_DONE;
160
161 iwdev = &hdl->device;
162 netdev = iwdev->ldev->netdev;
163 upper_dev = netdev_master_upper_dev_get(netdev);
164 if (netdev != event_netdev)
165 return NOTIFY_DONE;
166
167 switch (event) {
168 case NETDEV_DOWN:
169 if (upper_dev)
Ismail, Mustafa20c61f72016-04-18 10:33:07 -0500170 local_ipaddr = ntohl(
171 ((struct in_device *)upper_dev->ip_ptr)->ifa_list->ifa_address);
Faisal Latif4e9042e2016-01-20 13:40:08 -0600172 else
Ismail, Mustafa20c61f72016-04-18 10:33:07 -0500173 local_ipaddr = ntohl(ifa->ifa_address);
Faisal Latif4e9042e2016-01-20 13:40:08 -0600174 i40iw_manage_arp_cache(iwdev,
175 netdev->dev_addr,
176 &local_ipaddr,
177 true,
178 I40IW_ARP_DELETE);
179 return NOTIFY_OK;
180 case NETDEV_UP:
181 if (upper_dev)
Ismail, Mustafa20c61f72016-04-18 10:33:07 -0500182 local_ipaddr = ntohl(
183 ((struct in_device *)upper_dev->ip_ptr)->ifa_list->ifa_address);
Faisal Latif4e9042e2016-01-20 13:40:08 -0600184 else
Ismail, Mustafa20c61f72016-04-18 10:33:07 -0500185 local_ipaddr = ntohl(ifa->ifa_address);
Faisal Latif4e9042e2016-01-20 13:40:08 -0600186 i40iw_manage_arp_cache(iwdev,
187 netdev->dev_addr,
188 &local_ipaddr,
189 true,
190 I40IW_ARP_ADD);
191 break;
192 case NETDEV_CHANGEADDR:
193 /* Add the address to the IP table */
194 if (upper_dev)
Ismail, Mustafa20c61f72016-04-18 10:33:07 -0500195 local_ipaddr = ntohl(
196 ((struct in_device *)upper_dev->ip_ptr)->ifa_list->ifa_address);
Faisal Latif4e9042e2016-01-20 13:40:08 -0600197 else
Ismail, Mustafa20c61f72016-04-18 10:33:07 -0500198 local_ipaddr = ntohl(ifa->ifa_address);
Faisal Latif4e9042e2016-01-20 13:40:08 -0600199
Faisal Latif4e9042e2016-01-20 13:40:08 -0600200 i40iw_manage_arp_cache(iwdev,
201 netdev->dev_addr,
202 &local_ipaddr,
203 true,
204 I40IW_ARP_ADD);
205 break;
206 default:
207 break;
208 }
209 return NOTIFY_DONE;
210}
211
212/**
213 * i40iw_inet6addr_event - system notifier for ipv6 netdev events
214 * @notfier: not used
215 * @event: event for notifier
216 * @ptr: if address
217 */
218int i40iw_inet6addr_event(struct notifier_block *notifier,
219 unsigned long event,
220 void *ptr)
221{
222 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
223 struct net_device *event_netdev = ifa->idev->dev;
224 struct net_device *netdev;
225 struct i40iw_device *iwdev;
226 struct i40iw_handler *hdl;
Ismail, Mustafa20c61f72016-04-18 10:33:07 -0500227 u32 local_ipaddr6[4];
Faisal Latif4e9042e2016-01-20 13:40:08 -0600228
229 hdl = i40iw_find_netdev(event_netdev);
230 if (!hdl)
231 return NOTIFY_DONE;
232
233 iwdev = &hdl->device;
234 netdev = iwdev->ldev->netdev;
235 if (netdev != event_netdev)
236 return NOTIFY_DONE;
237
238 switch (event) {
239 case NETDEV_DOWN:
240 i40iw_copy_ip_ntohl(local_ipaddr6, ifa->addr.in6_u.u6_addr32);
241 i40iw_manage_arp_cache(iwdev,
242 netdev->dev_addr,
243 local_ipaddr6,
244 false,
245 I40IW_ARP_DELETE);
246 return NOTIFY_OK;
247 case NETDEV_UP:
248 /* Fall through */
249 case NETDEV_CHANGEADDR:
250 i40iw_copy_ip_ntohl(local_ipaddr6, ifa->addr.in6_u.u6_addr32);
251 i40iw_manage_arp_cache(iwdev,
252 netdev->dev_addr,
253 local_ipaddr6,
254 false,
255 I40IW_ARP_ADD);
256 break;
257 default:
258 break;
259 }
260 return NOTIFY_DONE;
261}
262
263/**
264 * i40iw_net_event - system notifier for net events
265 * @notfier: not used
266 * @event: event for notifier
267 * @ptr: neighbor
268 */
269int i40iw_net_event(struct notifier_block *notifier, unsigned long event, void *ptr)
270{
271 struct neighbour *neigh = ptr;
272 struct i40iw_device *iwdev;
273 struct i40iw_handler *iwhdl;
274 __be32 *p;
275 u32 local_ipaddr[4];
276
277 switch (event) {
278 case NETEVENT_NEIGH_UPDATE:
279 iwhdl = i40iw_find_netdev((struct net_device *)neigh->dev);
280 if (!iwhdl)
281 return NOTIFY_DONE;
282 iwdev = &iwhdl->device;
283 p = (__be32 *)neigh->primary_key;
284 i40iw_copy_ip_ntohl(local_ipaddr, p);
285 if (neigh->nud_state & NUD_VALID) {
286 i40iw_manage_arp_cache(iwdev,
287 neigh->ha,
288 local_ipaddr,
289 false,
290 I40IW_ARP_ADD);
291
292 } else {
293 i40iw_manage_arp_cache(iwdev,
294 neigh->ha,
295 local_ipaddr,
296 false,
297 I40IW_ARP_DELETE);
298 }
299 break;
300 default:
301 break;
302 }
303 return NOTIFY_DONE;
304}
305
306/**
307 * i40iw_get_cqp_request - get cqp struct
308 * @cqp: device cqp ptr
309 * @wait: cqp to be used in wait mode
310 */
311struct i40iw_cqp_request *i40iw_get_cqp_request(struct i40iw_cqp *cqp, bool wait)
312{
313 struct i40iw_cqp_request *cqp_request = NULL;
314 unsigned long flags;
315
316 spin_lock_irqsave(&cqp->req_lock, flags);
317 if (!list_empty(&cqp->cqp_avail_reqs)) {
318 cqp_request = list_entry(cqp->cqp_avail_reqs.next,
319 struct i40iw_cqp_request, list);
320 list_del_init(&cqp_request->list);
321 }
322 spin_unlock_irqrestore(&cqp->req_lock, flags);
323 if (!cqp_request) {
324 cqp_request = kzalloc(sizeof(*cqp_request), GFP_ATOMIC);
325 if (cqp_request) {
326 cqp_request->dynamic = true;
327 INIT_LIST_HEAD(&cqp_request->list);
328 init_waitqueue_head(&cqp_request->waitq);
329 }
330 }
331 if (!cqp_request) {
332 i40iw_pr_err("CQP Request Fail: No Memory");
333 return NULL;
334 }
335
336 if (wait) {
337 atomic_set(&cqp_request->refcount, 2);
338 cqp_request->waiting = true;
339 } else {
340 atomic_set(&cqp_request->refcount, 1);
341 }
342 return cqp_request;
343}
344
345/**
346 * i40iw_free_cqp_request - free cqp request
347 * @cqp: cqp ptr
348 * @cqp_request: to be put back in cqp list
349 */
350void i40iw_free_cqp_request(struct i40iw_cqp *cqp, struct i40iw_cqp_request *cqp_request)
351{
352 unsigned long flags;
353
354 if (cqp_request->dynamic) {
355 kfree(cqp_request);
356 } else {
357 cqp_request->request_done = false;
358 cqp_request->callback_fcn = NULL;
359 cqp_request->waiting = false;
360
361 spin_lock_irqsave(&cqp->req_lock, flags);
362 list_add_tail(&cqp_request->list, &cqp->cqp_avail_reqs);
363 spin_unlock_irqrestore(&cqp->req_lock, flags);
364 }
365}
366
367/**
368 * i40iw_put_cqp_request - dec ref count and free if 0
369 * @cqp: cqp ptr
370 * @cqp_request: to be put back in cqp list
371 */
372void i40iw_put_cqp_request(struct i40iw_cqp *cqp,
373 struct i40iw_cqp_request *cqp_request)
374{
375 if (atomic_dec_and_test(&cqp_request->refcount))
376 i40iw_free_cqp_request(cqp, cqp_request);
377}
378
379/**
380 * i40iw_free_qp - callback after destroy cqp completes
381 * @cqp_request: cqp request for destroy qp
382 * @num: not used
383 */
384static void i40iw_free_qp(struct i40iw_cqp_request *cqp_request, u32 num)
385{
386 struct i40iw_sc_qp *qp = (struct i40iw_sc_qp *)cqp_request->param;
387 struct i40iw_qp *iwqp = (struct i40iw_qp *)qp->back_qp;
388 struct i40iw_device *iwdev;
389 u32 qp_num = iwqp->ibqp.qp_num;
390
391 iwdev = iwqp->iwdev;
392
393 i40iw_rem_pdusecount(iwqp->iwpd, iwdev);
394 i40iw_free_qp_resources(iwdev, iwqp, qp_num);
Mustafa Ismaild5965932016-11-30 14:59:26 -0600395 i40iw_rem_devusecount(iwdev);
Faisal Latif4e9042e2016-01-20 13:40:08 -0600396}
397
398/**
399 * i40iw_wait_event - wait for completion
400 * @iwdev: iwarp device
401 * @cqp_request: cqp request to wait
402 */
403static int i40iw_wait_event(struct i40iw_device *iwdev,
404 struct i40iw_cqp_request *cqp_request)
405{
406 struct cqp_commands_info *info = &cqp_request->info;
407 struct i40iw_cqp *iwcqp = &iwdev->cqp;
408 bool cqp_error = false;
409 int err_code = 0;
410 int timeout_ret = 0;
411
412 timeout_ret = wait_event_timeout(cqp_request->waitq,
413 cqp_request->request_done,
414 I40IW_EVENT_TIMEOUT);
415 if (!timeout_ret) {
416 i40iw_pr_err("error cqp command 0x%x timed out ret = %d\n",
417 info->cqp_cmd, timeout_ret);
418 err_code = -ETIME;
419 i40iw_request_reset(iwdev);
420 goto done;
421 }
422 cqp_error = cqp_request->compl_info.error;
423 if (cqp_error) {
424 i40iw_pr_err("error cqp command 0x%x completion maj = 0x%x min=0x%x\n",
425 info->cqp_cmd, cqp_request->compl_info.maj_err_code,
426 cqp_request->compl_info.min_err_code);
427 err_code = -EPROTO;
428 goto done;
429 }
430done:
431 i40iw_put_cqp_request(iwcqp, cqp_request);
432 return err_code;
433}
434
435/**
436 * i40iw_handle_cqp_op - process cqp command
437 * @iwdev: iwarp device
438 * @cqp_request: cqp request to process
439 */
440enum i40iw_status_code i40iw_handle_cqp_op(struct i40iw_device *iwdev,
441 struct i40iw_cqp_request
442 *cqp_request)
443{
444 struct i40iw_sc_dev *dev = &iwdev->sc_dev;
445 enum i40iw_status_code status;
446 struct cqp_commands_info *info = &cqp_request->info;
447 int err_code = 0;
448
449 status = i40iw_process_cqp_cmd(dev, info);
450 if (status) {
451 i40iw_pr_err("error cqp command 0x%x failed\n", info->cqp_cmd);
452 i40iw_free_cqp_request(&iwdev->cqp, cqp_request);
453 return status;
454 }
455 if (cqp_request->waiting)
456 err_code = i40iw_wait_event(iwdev, cqp_request);
457 if (err_code)
458 status = I40IW_ERR_CQP_COMPL_ERROR;
459 return status;
460}
461
462/**
Mustafa Ismaild5965932016-11-30 14:59:26 -0600463 * i40iw_add_devusecount - add dev refcount
464 * @iwdev: dev for refcount
465 */
466void i40iw_add_devusecount(struct i40iw_device *iwdev)
467{
468 atomic64_inc(&iwdev->use_count);
469}
470
471/**
472 * i40iw_rem_devusecount - decrement refcount for dev
473 * @iwdev: device
474 */
475void i40iw_rem_devusecount(struct i40iw_device *iwdev)
476{
477 if (!atomic64_dec_and_test(&iwdev->use_count))
478 return;
479 wake_up(&iwdev->close_wq);
480}
481
482/**
Faisal Latif4e9042e2016-01-20 13:40:08 -0600483 * i40iw_add_pdusecount - add pd refcount
484 * @iwpd: pd for refcount
485 */
486void i40iw_add_pdusecount(struct i40iw_pd *iwpd)
487{
488 atomic_inc(&iwpd->usecount);
489}
490
491/**
492 * i40iw_rem_pdusecount - decrement refcount for pd and free if 0
493 * @iwpd: pd for refcount
494 * @iwdev: iwarp device
495 */
496void i40iw_rem_pdusecount(struct i40iw_pd *iwpd, struct i40iw_device *iwdev)
497{
498 if (!atomic_dec_and_test(&iwpd->usecount))
499 return;
500 i40iw_free_resource(iwdev, iwdev->allocated_pds, iwpd->sc_pd.pd_id);
501 kfree(iwpd);
502}
503
504/**
505 * i40iw_add_ref - add refcount for qp
506 * @ibqp: iqarp qp
507 */
508void i40iw_add_ref(struct ib_qp *ibqp)
509{
510 struct i40iw_qp *iwqp = (struct i40iw_qp *)ibqp;
511
512 atomic_inc(&iwqp->refcount);
513}
514
515/**
516 * i40iw_rem_ref - rem refcount for qp and free if 0
517 * @ibqp: iqarp qp
518 */
519void i40iw_rem_ref(struct ib_qp *ibqp)
520{
521 struct i40iw_qp *iwqp;
522 enum i40iw_status_code status;
523 struct i40iw_cqp_request *cqp_request;
524 struct cqp_commands_info *cqp_info;
525 struct i40iw_device *iwdev;
526 u32 qp_num;
Ismail, Mustafa996abf0a2016-04-18 10:32:59 -0500527 unsigned long flags;
Faisal Latif4e9042e2016-01-20 13:40:08 -0600528
529 iwqp = to_iwqp(ibqp);
Faisal Latif4e9042e2016-01-20 13:40:08 -0600530 iwdev = iwqp->iwdev;
Ismail, Mustafa996abf0a2016-04-18 10:32:59 -0500531 spin_lock_irqsave(&iwdev->qptable_lock, flags);
532 if (!atomic_dec_and_test(&iwqp->refcount)) {
533 spin_unlock_irqrestore(&iwdev->qptable_lock, flags);
534 return;
535 }
536
Faisal Latif4e9042e2016-01-20 13:40:08 -0600537 qp_num = iwqp->ibqp.qp_num;
538 iwdev->qp_table[qp_num] = NULL;
Ismail, Mustafa996abf0a2016-04-18 10:32:59 -0500539 spin_unlock_irqrestore(&iwdev->qptable_lock, flags);
Faisal Latif4e9042e2016-01-20 13:40:08 -0600540 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
541 if (!cqp_request)
542 return;
543
544 cqp_request->callback_fcn = i40iw_free_qp;
545 cqp_request->param = (void *)&iwqp->sc_qp;
546 cqp_info = &cqp_request->info;
547 cqp_info->cqp_cmd = OP_QP_DESTROY;
548 cqp_info->post_sq = 1;
549 cqp_info->in.u.qp_destroy.qp = &iwqp->sc_qp;
550 cqp_info->in.u.qp_destroy.scratch = (uintptr_t)cqp_request;
551 cqp_info->in.u.qp_destroy.remove_hash_idx = true;
552 status = i40iw_handle_cqp_op(iwdev, cqp_request);
553 if (status)
554 i40iw_pr_err("CQP-OP Destroy QP fail");
555}
556
557/**
558 * i40iw_get_qp - get qp address
559 * @device: iwarp device
560 * @qpn: qp number
561 */
562struct ib_qp *i40iw_get_qp(struct ib_device *device, int qpn)
563{
564 struct i40iw_device *iwdev = to_iwdev(device);
565
566 if ((qpn < IW_FIRST_QPN) || (qpn >= iwdev->max_qp))
567 return NULL;
568
569 return &iwdev->qp_table[qpn]->ibqp;
570}
571
572/**
573 * i40iw_debug_buf - print debug msg and buffer is mask set
574 * @dev: hardware control device structure
575 * @mask: mask to compare if to print debug buffer
576 * @buf: points buffer addr
577 * @size: saize of buffer to print
578 */
579void i40iw_debug_buf(struct i40iw_sc_dev *dev,
580 enum i40iw_debug_flag mask,
581 char *desc,
582 u64 *buf,
583 u32 size)
584{
585 u32 i;
586
587 if (!(dev->debug_mask & mask))
588 return;
589 i40iw_debug(dev, mask, "%s\n", desc);
590 i40iw_debug(dev, mask, "starting address virt=%p phy=%llxh\n", buf,
591 (unsigned long long)virt_to_phys(buf));
592
593 for (i = 0; i < size; i += 8)
594 i40iw_debug(dev, mask, "index %03d val: %016llx\n", i, buf[i / 8]);
595}
596
597/**
598 * i40iw_get_hw_addr - return hw addr
599 * @par: points to shared dev
600 */
601u8 __iomem *i40iw_get_hw_addr(void *par)
602{
603 struct i40iw_sc_dev *dev = (struct i40iw_sc_dev *)par;
604
605 return dev->hw->hw_addr;
606}
607
608/**
609 * i40iw_remove_head - return head entry and remove from list
610 * @list: list for entry
611 */
612void *i40iw_remove_head(struct list_head *list)
613{
614 struct list_head *entry;
615
616 if (list_empty(list))
617 return NULL;
618
619 entry = (void *)list->next;
620 list_del(entry);
621 return (void *)entry;
622}
623
624/**
625 * i40iw_allocate_dma_mem - Memory alloc helper fn
626 * @hw: pointer to the HW structure
627 * @mem: ptr to mem struct to fill out
628 * @size: size of memory requested
629 * @alignment: what to align the allocation to
630 */
631enum i40iw_status_code i40iw_allocate_dma_mem(struct i40iw_hw *hw,
632 struct i40iw_dma_mem *mem,
633 u64 size,
634 u32 alignment)
635{
636 struct pci_dev *pcidev = (struct pci_dev *)hw->dev_context;
637
638 if (!mem)
639 return I40IW_ERR_PARAM;
640 mem->size = ALIGN(size, alignment);
641 mem->va = dma_zalloc_coherent(&pcidev->dev, mem->size,
642 (dma_addr_t *)&mem->pa, GFP_KERNEL);
643 if (!mem->va)
644 return I40IW_ERR_NO_MEMORY;
645 return 0;
646}
647
648/**
649 * i40iw_free_dma_mem - Memory free helper fn
650 * @hw: pointer to the HW structure
651 * @mem: ptr to mem struct to free
652 */
653void i40iw_free_dma_mem(struct i40iw_hw *hw, struct i40iw_dma_mem *mem)
654{
655 struct pci_dev *pcidev = (struct pci_dev *)hw->dev_context;
656
657 if (!mem || !mem->va)
658 return;
659
660 dma_free_coherent(&pcidev->dev, mem->size,
661 mem->va, (dma_addr_t)mem->pa);
662 mem->va = NULL;
663}
664
665/**
666 * i40iw_allocate_virt_mem - virtual memory alloc helper fn
667 * @hw: pointer to the HW structure
668 * @mem: ptr to mem struct to fill out
669 * @size: size of memory requested
670 */
671enum i40iw_status_code i40iw_allocate_virt_mem(struct i40iw_hw *hw,
672 struct i40iw_virt_mem *mem,
673 u32 size)
674{
675 if (!mem)
676 return I40IW_ERR_PARAM;
677
678 mem->size = size;
679 mem->va = kzalloc(size, GFP_KERNEL);
680
681 if (mem->va)
682 return 0;
683 else
684 return I40IW_ERR_NO_MEMORY;
685}
686
687/**
688 * i40iw_free_virt_mem - virtual memory free helper fn
689 * @hw: pointer to the HW structure
690 * @mem: ptr to mem struct to free
691 */
692enum i40iw_status_code i40iw_free_virt_mem(struct i40iw_hw *hw,
693 struct i40iw_virt_mem *mem)
694{
695 if (!mem)
696 return I40IW_ERR_PARAM;
Mustafa Ismail7eaf8312016-08-22 19:01:47 -0500697 /*
698 * mem->va points to the parent of mem, so both mem and mem->va
699 * can not be touched once mem->va is freed
700 */
Faisal Latif4e9042e2016-01-20 13:40:08 -0600701 kfree(mem->va);
Faisal Latif4e9042e2016-01-20 13:40:08 -0600702 return 0;
703}
704
705/**
706 * i40iw_cqp_sds_cmd - create cqp command for sd
707 * @dev: hardware control device structure
708 * @sd_info: information for sd cqp
709 *
710 */
711enum i40iw_status_code i40iw_cqp_sds_cmd(struct i40iw_sc_dev *dev,
712 struct i40iw_update_sds_info *sdinfo)
713{
714 enum i40iw_status_code status;
715 struct i40iw_cqp_request *cqp_request;
716 struct cqp_commands_info *cqp_info;
717 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
718
719 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
720 if (!cqp_request)
721 return I40IW_ERR_NO_MEMORY;
722 cqp_info = &cqp_request->info;
723 memcpy(&cqp_info->in.u.update_pe_sds.info, sdinfo,
724 sizeof(cqp_info->in.u.update_pe_sds.info));
725 cqp_info->cqp_cmd = OP_UPDATE_PE_SDS;
726 cqp_info->post_sq = 1;
727 cqp_info->in.u.update_pe_sds.dev = dev;
728 cqp_info->in.u.update_pe_sds.scratch = (uintptr_t)cqp_request;
729 status = i40iw_handle_cqp_op(iwdev, cqp_request);
730 if (status)
731 i40iw_pr_err("CQP-OP Update SD's fail");
732 return status;
733}
734
735/**
Henry Orosco0fc2dc52016-10-10 21:12:10 -0500736 * i40iw_qp_suspend_resume - cqp command for suspend/resume
737 * @dev: hardware control device structure
738 * @qp: hardware control qp
739 * @suspend: flag if suspend or resume
740 */
741void i40iw_qp_suspend_resume(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp, bool suspend)
742{
743 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
744 struct i40iw_cqp_request *cqp_request;
745 struct i40iw_sc_cqp *cqp = dev->cqp;
746 struct cqp_commands_info *cqp_info;
747 enum i40iw_status_code status;
748
749 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
750 if (!cqp_request)
751 return;
752
753 cqp_info = &cqp_request->info;
754 cqp_info->cqp_cmd = (suspend) ? OP_SUSPEND : OP_RESUME;
755 cqp_info->in.u.suspend_resume.cqp = cqp;
756 cqp_info->in.u.suspend_resume.qp = qp;
757 cqp_info->in.u.suspend_resume.scratch = (uintptr_t)cqp_request;
758 status = i40iw_handle_cqp_op(iwdev, cqp_request);
759 if (status)
760 i40iw_pr_err("CQP-OP QP Suspend/Resume fail");
761}
762
763/**
764 * i40iw_qp_mss_modify - modify mss for qp
765 * @dev: hardware control device structure
766 * @qp: hardware control qp
767 */
768void i40iw_qp_mss_modify(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
769{
770 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
771 struct i40iw_qp *iwqp = (struct i40iw_qp *)qp->back_qp;
772 struct i40iw_modify_qp_info info;
773
774 memset(&info, 0, sizeof(info));
775 info.mss_change = true;
776 info.new_mss = dev->mss;
777 i40iw_hw_modify_qp(iwdev, iwqp, &info, false);
778}
779
780/**
Faisal Latif4e9042e2016-01-20 13:40:08 -0600781 * i40iw_term_modify_qp - modify qp for term message
782 * @qp: hardware control qp
783 * @next_state: qp's next state
784 * @term: terminate code
785 * @term_len: length
786 */
787void i40iw_term_modify_qp(struct i40iw_sc_qp *qp, u8 next_state, u8 term, u8 term_len)
788{
789 struct i40iw_qp *iwqp;
790
791 iwqp = (struct i40iw_qp *)qp->back_qp;
792 i40iw_next_iw_state(iwqp, next_state, 0, term, term_len);
793};
794
795/**
796 * i40iw_terminate_done - after terminate is completed
797 * @qp: hardware control qp
798 * @timeout_occurred: indicates if terminate timer expired
799 */
800void i40iw_terminate_done(struct i40iw_sc_qp *qp, int timeout_occurred)
801{
802 struct i40iw_qp *iwqp;
803 u32 next_iwarp_state = I40IW_QP_STATE_ERROR;
804 u8 hte = 0;
805 bool first_time;
806 unsigned long flags;
807
808 iwqp = (struct i40iw_qp *)qp->back_qp;
809 spin_lock_irqsave(&iwqp->lock, flags);
810 if (iwqp->hte_added) {
811 iwqp->hte_added = 0;
812 hte = 1;
813 }
814 first_time = !(qp->term_flags & I40IW_TERM_DONE);
815 qp->term_flags |= I40IW_TERM_DONE;
816 spin_unlock_irqrestore(&iwqp->lock, flags);
817 if (first_time) {
818 if (!timeout_occurred)
819 i40iw_terminate_del_timer(qp);
820 else
821 next_iwarp_state = I40IW_QP_STATE_CLOSING;
822
823 i40iw_next_iw_state(iwqp, next_iwarp_state, hte, 0, 0);
824 i40iw_cm_disconn(iwqp);
825 }
826}
827
828/**
829 * i40iw_terminate_imeout - timeout happened
830 * @context: points to iwarp qp
831 */
832static void i40iw_terminate_timeout(unsigned long context)
833{
834 struct i40iw_qp *iwqp = (struct i40iw_qp *)context;
835 struct i40iw_sc_qp *qp = (struct i40iw_sc_qp *)&iwqp->sc_qp;
836
837 i40iw_terminate_done(qp, 1);
838}
839
840/**
841 * i40iw_terminate_start_timer - start terminate timeout
842 * @qp: hardware control qp
843 */
844void i40iw_terminate_start_timer(struct i40iw_sc_qp *qp)
845{
846 struct i40iw_qp *iwqp;
847
848 iwqp = (struct i40iw_qp *)qp->back_qp;
849 init_timer(&iwqp->terminate_timer);
850 iwqp->terminate_timer.function = i40iw_terminate_timeout;
851 iwqp->terminate_timer.expires = jiffies + HZ;
852 iwqp->terminate_timer.data = (unsigned long)iwqp;
853 add_timer(&iwqp->terminate_timer);
854}
855
856/**
857 * i40iw_terminate_del_timer - delete terminate timeout
858 * @qp: hardware control qp
859 */
860void i40iw_terminate_del_timer(struct i40iw_sc_qp *qp)
861{
862 struct i40iw_qp *iwqp;
863
864 iwqp = (struct i40iw_qp *)qp->back_qp;
865 del_timer(&iwqp->terminate_timer);
866}
867
868/**
869 * i40iw_cqp_generic_worker - generic worker for cqp
870 * @work: work pointer
871 */
872static void i40iw_cqp_generic_worker(struct work_struct *work)
873{
874 struct i40iw_virtchnl_work_info *work_info =
875 &((struct virtchnl_work *)work)->work_info;
876
877 if (work_info->worker_vf_dev)
878 work_info->callback_fcn(work_info->worker_vf_dev);
879}
880
881/**
882 * i40iw_cqp_spawn_worker - spawn worket thread
883 * @iwdev: device struct pointer
884 * @work_info: work request info
885 * @iw_vf_idx: virtual function index
886 */
887void i40iw_cqp_spawn_worker(struct i40iw_sc_dev *dev,
888 struct i40iw_virtchnl_work_info *work_info,
889 u32 iw_vf_idx)
890{
891 struct virtchnl_work *work;
892 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
893
894 work = &iwdev->virtchnl_w[iw_vf_idx];
895 memcpy(&work->work_info, work_info, sizeof(*work_info));
896 INIT_WORK(&work->work, i40iw_cqp_generic_worker);
897 queue_work(iwdev->virtchnl_wq, &work->work);
898}
899
900/**
901 * i40iw_cqp_manage_hmc_fcn_worker -
902 * @work: work pointer for hmc info
903 */
904static void i40iw_cqp_manage_hmc_fcn_worker(struct work_struct *work)
905{
906 struct i40iw_cqp_request *cqp_request =
907 ((struct virtchnl_work *)work)->cqp_request;
908 struct i40iw_ccq_cqe_info ccq_cqe_info;
909 struct i40iw_hmc_fcn_info *hmcfcninfo =
910 &cqp_request->info.in.u.manage_hmc_pm.info;
911 struct i40iw_device *iwdev =
912 (struct i40iw_device *)cqp_request->info.in.u.manage_hmc_pm.dev->back_dev;
913
914 ccq_cqe_info.cqp = NULL;
915 ccq_cqe_info.maj_err_code = cqp_request->compl_info.maj_err_code;
916 ccq_cqe_info.min_err_code = cqp_request->compl_info.min_err_code;
917 ccq_cqe_info.op_code = cqp_request->compl_info.op_code;
918 ccq_cqe_info.op_ret_val = cqp_request->compl_info.op_ret_val;
919 ccq_cqe_info.scratch = 0;
920 ccq_cqe_info.error = cqp_request->compl_info.error;
921 hmcfcninfo->callback_fcn(cqp_request->info.in.u.manage_hmc_pm.dev,
922 hmcfcninfo->cqp_callback_param, &ccq_cqe_info);
923 i40iw_put_cqp_request(&iwdev->cqp, cqp_request);
924}
925
926/**
927 * i40iw_cqp_manage_hmc_fcn_callback - called function after cqp completion
928 * @cqp_request: cqp request info struct for hmc fun
929 * @unused: unused param of callback
930 */
931static void i40iw_cqp_manage_hmc_fcn_callback(struct i40iw_cqp_request *cqp_request,
932 u32 unused)
933{
934 struct virtchnl_work *work;
935 struct i40iw_hmc_fcn_info *hmcfcninfo =
936 &cqp_request->info.in.u.manage_hmc_pm.info;
937 struct i40iw_device *iwdev =
938 (struct i40iw_device *)cqp_request->info.in.u.manage_hmc_pm.dev->
939 back_dev;
940
941 if (hmcfcninfo && hmcfcninfo->callback_fcn) {
942 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s1\n", __func__);
943 atomic_inc(&cqp_request->refcount);
944 work = &iwdev->virtchnl_w[hmcfcninfo->iw_vf_idx];
945 work->cqp_request = cqp_request;
946 INIT_WORK(&work->work, i40iw_cqp_manage_hmc_fcn_worker);
947 queue_work(iwdev->virtchnl_wq, &work->work);
948 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s2\n", __func__);
949 } else {
950 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s: Something wrong\n", __func__);
951 }
952}
953
954/**
955 * i40iw_cqp_manage_hmc_fcn_cmd - issue cqp command to manage hmc
956 * @dev: hardware control device structure
957 * @hmcfcninfo: info for hmc
958 */
959enum i40iw_status_code i40iw_cqp_manage_hmc_fcn_cmd(struct i40iw_sc_dev *dev,
960 struct i40iw_hmc_fcn_info *hmcfcninfo)
961{
962 enum i40iw_status_code status;
963 struct i40iw_cqp_request *cqp_request;
964 struct cqp_commands_info *cqp_info;
965 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
966
967 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s\n", __func__);
968 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
969 if (!cqp_request)
970 return I40IW_ERR_NO_MEMORY;
971 cqp_info = &cqp_request->info;
972 cqp_request->callback_fcn = i40iw_cqp_manage_hmc_fcn_callback;
973 cqp_request->param = hmcfcninfo;
974 memcpy(&cqp_info->in.u.manage_hmc_pm.info, hmcfcninfo,
975 sizeof(*hmcfcninfo));
976 cqp_info->in.u.manage_hmc_pm.dev = dev;
977 cqp_info->cqp_cmd = OP_MANAGE_HMC_PM_FUNC_TABLE;
978 cqp_info->post_sq = 1;
979 cqp_info->in.u.manage_hmc_pm.scratch = (uintptr_t)cqp_request;
980 status = i40iw_handle_cqp_op(iwdev, cqp_request);
981 if (status)
982 i40iw_pr_err("CQP-OP Manage HMC fail");
983 return status;
984}
985
986/**
987 * i40iw_cqp_query_fpm_values_cmd - send cqp command for fpm
988 * @iwdev: function device struct
989 * @values_mem: buffer for fpm
990 * @hmc_fn_id: function id for fpm
991 */
992enum i40iw_status_code i40iw_cqp_query_fpm_values_cmd(struct i40iw_sc_dev *dev,
993 struct i40iw_dma_mem *values_mem,
994 u8 hmc_fn_id)
995{
996 enum i40iw_status_code status;
997 struct i40iw_cqp_request *cqp_request;
998 struct cqp_commands_info *cqp_info;
999 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1000
1001 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1002 if (!cqp_request)
1003 return I40IW_ERR_NO_MEMORY;
1004 cqp_info = &cqp_request->info;
1005 cqp_request->param = NULL;
1006 cqp_info->in.u.query_fpm_values.cqp = dev->cqp;
1007 cqp_info->in.u.query_fpm_values.fpm_values_pa = values_mem->pa;
1008 cqp_info->in.u.query_fpm_values.fpm_values_va = values_mem->va;
1009 cqp_info->in.u.query_fpm_values.hmc_fn_id = hmc_fn_id;
1010 cqp_info->cqp_cmd = OP_QUERY_FPM_VALUES;
1011 cqp_info->post_sq = 1;
1012 cqp_info->in.u.query_fpm_values.scratch = (uintptr_t)cqp_request;
1013 status = i40iw_handle_cqp_op(iwdev, cqp_request);
1014 if (status)
1015 i40iw_pr_err("CQP-OP Query FPM fail");
1016 return status;
1017}
1018
1019/**
1020 * i40iw_cqp_commit_fpm_values_cmd - commit fpm values in hw
1021 * @dev: hardware control device structure
1022 * @values_mem: buffer with fpm values
1023 * @hmc_fn_id: function id for fpm
1024 */
1025enum i40iw_status_code i40iw_cqp_commit_fpm_values_cmd(struct i40iw_sc_dev *dev,
1026 struct i40iw_dma_mem *values_mem,
1027 u8 hmc_fn_id)
1028{
1029 enum i40iw_status_code status;
1030 struct i40iw_cqp_request *cqp_request;
1031 struct cqp_commands_info *cqp_info;
1032 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1033
1034 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1035 if (!cqp_request)
1036 return I40IW_ERR_NO_MEMORY;
1037 cqp_info = &cqp_request->info;
1038 cqp_request->param = NULL;
1039 cqp_info->in.u.commit_fpm_values.cqp = dev->cqp;
1040 cqp_info->in.u.commit_fpm_values.fpm_values_pa = values_mem->pa;
1041 cqp_info->in.u.commit_fpm_values.fpm_values_va = values_mem->va;
1042 cqp_info->in.u.commit_fpm_values.hmc_fn_id = hmc_fn_id;
1043 cqp_info->cqp_cmd = OP_COMMIT_FPM_VALUES;
1044 cqp_info->post_sq = 1;
1045 cqp_info->in.u.commit_fpm_values.scratch = (uintptr_t)cqp_request;
1046 status = i40iw_handle_cqp_op(iwdev, cqp_request);
1047 if (status)
1048 i40iw_pr_err("CQP-OP Commit FPM fail");
1049 return status;
1050}
1051
1052/**
1053 * i40iw_vf_wait_vchnl_resp - wait for channel msg
1054 * @iwdev: function's device struct
1055 */
1056enum i40iw_status_code i40iw_vf_wait_vchnl_resp(struct i40iw_sc_dev *dev)
1057{
1058 struct i40iw_device *iwdev = dev->back_dev;
Faisal Latif4e9042e2016-01-20 13:40:08 -06001059 int timeout_ret;
1060
1061 i40iw_debug(dev, I40IW_DEBUG_VIRT, "%s[%u] dev %p, iwdev %p\n",
1062 __func__, __LINE__, dev, iwdev);
Ismail, Mustafaf69c3332016-04-18 10:33:03 -05001063
1064 atomic_set(&iwdev->vchnl_msgs, 2);
Faisal Latif4e9042e2016-01-20 13:40:08 -06001065 timeout_ret = wait_event_timeout(iwdev->vchnl_waitq,
1066 (atomic_read(&iwdev->vchnl_msgs) == 1),
1067 I40IW_VCHNL_EVENT_TIMEOUT);
1068 atomic_dec(&iwdev->vchnl_msgs);
1069 if (!timeout_ret) {
1070 i40iw_pr_err("virt channel completion timeout = 0x%x\n", timeout_ret);
Ismail, Mustafaf69c3332016-04-18 10:33:03 -05001071 atomic_set(&iwdev->vchnl_msgs, 0);
1072 dev->vchnl_up = false;
1073 return I40IW_ERR_TIMEOUT;
Faisal Latif4e9042e2016-01-20 13:40:08 -06001074 }
Ismail, Mustafaf69c3332016-04-18 10:33:03 -05001075 wake_up(&dev->vf_reqs);
1076 return 0;
Faisal Latif4e9042e2016-01-20 13:40:08 -06001077}
1078
1079/**
1080 * i40iw_ieq_mpa_crc_ae - generate AE for crc error
1081 * @dev: hardware control device structure
1082 * @qp: hardware control qp
1083 */
1084void i40iw_ieq_mpa_crc_ae(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
1085{
1086 struct i40iw_qp_flush_info info;
1087 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1088
1089 i40iw_debug(dev, I40IW_DEBUG_AEQ, "%s entered\n", __func__);
1090 memset(&info, 0, sizeof(info));
1091 info.ae_code = I40IW_AE_LLP_RECEIVED_MPA_CRC_ERROR;
1092 info.generate_ae = true;
1093 info.ae_source = 0x3;
1094 (void)i40iw_hw_flush_wqes(iwdev, qp, &info, false);
1095}
1096
1097/**
1098 * i40iw_init_hash_desc - initialize hash for crc calculation
1099 * @desc: cryption type
1100 */
Tatyana Nikolova34abf9e2016-03-18 10:38:33 -05001101enum i40iw_status_code i40iw_init_hash_desc(struct shash_desc **desc)
Faisal Latif4e9042e2016-01-20 13:40:08 -06001102{
Tatyana Nikolova34abf9e2016-03-18 10:38:33 -05001103 struct crypto_shash *tfm;
1104 struct shash_desc *tdesc;
1105
1106 tfm = crypto_alloc_shash("crc32c", 0, 0);
1107 if (IS_ERR(tfm))
Faisal Latif4e9042e2016-01-20 13:40:08 -06001108 return I40IW_ERR_MPA_CRC;
Tatyana Nikolova34abf9e2016-03-18 10:38:33 -05001109
1110 tdesc = kzalloc(sizeof(*tdesc) + crypto_shash_descsize(tfm),
1111 GFP_KERNEL);
1112 if (!tdesc) {
1113 crypto_free_shash(tfm);
1114 return I40IW_ERR_MPA_CRC;
1115 }
1116 tdesc->tfm = tfm;
1117 *desc = tdesc;
1118
Faisal Latif4e9042e2016-01-20 13:40:08 -06001119 return 0;
1120}
1121
1122/**
1123 * i40iw_free_hash_desc - free hash desc
1124 * @desc: to be freed
1125 */
Tatyana Nikolova34abf9e2016-03-18 10:38:33 -05001126void i40iw_free_hash_desc(struct shash_desc *desc)
Faisal Latif4e9042e2016-01-20 13:40:08 -06001127{
Tatyana Nikolova34abf9e2016-03-18 10:38:33 -05001128 if (desc) {
1129 crypto_free_shash(desc->tfm);
1130 kfree(desc);
1131 }
Faisal Latif4e9042e2016-01-20 13:40:08 -06001132}
1133
1134/**
1135 * i40iw_alloc_query_fpm_buf - allocate buffer for fpm
1136 * @dev: hardware control device structure
1137 * @mem: buffer ptr for fpm to be allocated
1138 * @return: memory allocation status
1139 */
1140enum i40iw_status_code i40iw_alloc_query_fpm_buf(struct i40iw_sc_dev *dev,
1141 struct i40iw_dma_mem *mem)
1142{
1143 enum i40iw_status_code status;
1144 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1145
1146 status = i40iw_obj_aligned_mem(iwdev, mem, I40IW_QUERY_FPM_BUF_SIZE,
1147 I40IW_FPM_QUERY_BUF_ALIGNMENT_MASK);
1148 return status;
1149}
1150
1151/**
1152 * i40iw_ieq_check_mpacrc - check if mpa crc is OK
1153 * @desc: desc for hash
1154 * @addr: address of buffer for crc
1155 * @length: length of buffer
1156 * @value: value to be compared
1157 */
Tatyana Nikolova34abf9e2016-03-18 10:38:33 -05001158enum i40iw_status_code i40iw_ieq_check_mpacrc(struct shash_desc *desc,
Faisal Latif4e9042e2016-01-20 13:40:08 -06001159 void *addr,
1160 u32 length,
1161 u32 value)
1162{
Faisal Latif4e9042e2016-01-20 13:40:08 -06001163 u32 crc = 0;
1164 int ret;
1165 enum i40iw_status_code ret_code = 0;
1166
Tatyana Nikolova34abf9e2016-03-18 10:38:33 -05001167 crypto_shash_init(desc);
1168 ret = crypto_shash_update(desc, addr, length);
Faisal Latif4e9042e2016-01-20 13:40:08 -06001169 if (!ret)
Tatyana Nikolova34abf9e2016-03-18 10:38:33 -05001170 crypto_shash_final(desc, (u8 *)&crc);
Faisal Latif4e9042e2016-01-20 13:40:08 -06001171 if (crc != value) {
1172 i40iw_pr_err("mpa crc check fail\n");
1173 ret_code = I40IW_ERR_MPA_CRC;
1174 }
1175 return ret_code;
1176}
1177
1178/**
1179 * i40iw_ieq_get_qp - get qp based on quad in puda buffer
1180 * @dev: hardware control device structure
1181 * @buf: receive puda buffer on exception q
1182 */
1183struct i40iw_sc_qp *i40iw_ieq_get_qp(struct i40iw_sc_dev *dev,
1184 struct i40iw_puda_buf *buf)
1185{
1186 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1187 struct i40iw_qp *iwqp;
1188 struct i40iw_cm_node *cm_node;
1189 u32 loc_addr[4], rem_addr[4];
1190 u16 loc_port, rem_port;
1191 struct ipv6hdr *ip6h;
1192 struct iphdr *iph = (struct iphdr *)buf->iph;
1193 struct tcphdr *tcph = (struct tcphdr *)buf->tcph;
1194
1195 if (iph->version == 4) {
1196 memset(loc_addr, 0, sizeof(loc_addr));
1197 loc_addr[0] = ntohl(iph->daddr);
1198 memset(rem_addr, 0, sizeof(rem_addr));
1199 rem_addr[0] = ntohl(iph->saddr);
1200 } else {
1201 ip6h = (struct ipv6hdr *)buf->iph;
1202 i40iw_copy_ip_ntohl(loc_addr, ip6h->daddr.in6_u.u6_addr32);
1203 i40iw_copy_ip_ntohl(rem_addr, ip6h->saddr.in6_u.u6_addr32);
1204 }
1205 loc_port = ntohs(tcph->dest);
1206 rem_port = ntohs(tcph->source);
1207
1208 cm_node = i40iw_find_node(&iwdev->cm_core, rem_port, rem_addr, loc_port,
1209 loc_addr, false);
1210 if (!cm_node)
1211 return NULL;
1212 iwqp = cm_node->iwqp;
1213 return &iwqp->sc_qp;
1214}
1215
1216/**
1217 * i40iw_ieq_update_tcpip_info - update tcpip in the buffer
1218 * @buf: puda to update
1219 * @length: length of buffer
1220 * @seqnum: seq number for tcp
1221 */
1222void i40iw_ieq_update_tcpip_info(struct i40iw_puda_buf *buf, u16 length, u32 seqnum)
1223{
1224 struct tcphdr *tcph;
1225 struct iphdr *iph;
1226 u16 iphlen;
1227 u16 packetsize;
1228 u8 *addr = (u8 *)buf->mem.va;
1229
1230 iphlen = (buf->ipv4) ? 20 : 40;
1231 iph = (struct iphdr *)(addr + buf->maclen);
1232 tcph = (struct tcphdr *)(addr + buf->maclen + iphlen);
1233 packetsize = length + buf->tcphlen + iphlen;
1234
1235 iph->tot_len = htons(packetsize);
1236 tcph->seq = htonl(seqnum);
1237}
1238
1239/**
1240 * i40iw_puda_get_tcpip_info - get tcpip info from puda buffer
1241 * @info: to get information
1242 * @buf: puda buffer
1243 */
1244enum i40iw_status_code i40iw_puda_get_tcpip_info(struct i40iw_puda_completion_info *info,
1245 struct i40iw_puda_buf *buf)
1246{
1247 struct iphdr *iph;
1248 struct ipv6hdr *ip6h;
1249 struct tcphdr *tcph;
1250 u16 iphlen;
1251 u16 pkt_len;
1252 u8 *mem = (u8 *)buf->mem.va;
1253 struct ethhdr *ethh = (struct ethhdr *)buf->mem.va;
1254
1255 if (ethh->h_proto == htons(0x8100)) {
1256 info->vlan_valid = true;
1257 buf->vlan_id = ntohs(((struct vlan_ethhdr *)ethh)->h_vlan_TCI) & VLAN_VID_MASK;
1258 }
1259 buf->maclen = (info->vlan_valid) ? 18 : 14;
1260 iphlen = (info->l3proto) ? 40 : 20;
1261 buf->ipv4 = (info->l3proto) ? false : true;
1262 buf->iph = mem + buf->maclen;
1263 iph = (struct iphdr *)buf->iph;
1264
1265 buf->tcph = buf->iph + iphlen;
1266 tcph = (struct tcphdr *)buf->tcph;
1267
1268 if (buf->ipv4) {
1269 pkt_len = ntohs(iph->tot_len);
1270 } else {
1271 ip6h = (struct ipv6hdr *)buf->iph;
1272 pkt_len = ntohs(ip6h->payload_len) + iphlen;
1273 }
1274
1275 buf->totallen = pkt_len + buf->maclen;
1276
Henry Orosco7581e962016-10-19 15:33:32 -05001277 if (info->payload_len < buf->totallen) {
Faisal Latif4e9042e2016-01-20 13:40:08 -06001278 i40iw_pr_err("payload_len = 0x%x totallen expected0x%x\n",
1279 info->payload_len, buf->totallen);
1280 return I40IW_ERR_INVALID_SIZE;
1281 }
1282
1283 buf->tcphlen = (tcph->doff) << 2;
1284 buf->datalen = pkt_len - iphlen - buf->tcphlen;
1285 buf->data = (buf->datalen) ? buf->tcph + buf->tcphlen : NULL;
1286 buf->hdrlen = buf->maclen + iphlen + buf->tcphlen;
1287 buf->seqnum = ntohl(tcph->seq);
1288 return 0;
1289}
1290
1291/**
1292 * i40iw_hw_stats_timeout - Stats timer-handler which updates all HW stats
1293 * @dev: hardware control device structure
1294 */
1295static void i40iw_hw_stats_timeout(unsigned long dev)
1296{
1297 struct i40iw_sc_dev *pf_dev = (struct i40iw_sc_dev *)dev;
1298 struct i40iw_dev_pestat *pf_devstat = &pf_dev->dev_pestat;
1299 struct i40iw_dev_pestat *vf_devstat = NULL;
1300 u16 iw_vf_idx;
1301 unsigned long flags;
1302
1303 /*PF*/
1304 pf_devstat->ops.iw_hw_stat_read_all(pf_devstat, &pf_devstat->hw_stats);
1305 for (iw_vf_idx = 0; iw_vf_idx < I40IW_MAX_PE_ENABLED_VF_COUNT; iw_vf_idx++) {
1306 spin_lock_irqsave(&pf_devstat->stats_lock, flags);
1307 if (pf_dev->vf_dev[iw_vf_idx]) {
1308 if (pf_dev->vf_dev[iw_vf_idx]->stats_initialized) {
1309 vf_devstat = &pf_dev->vf_dev[iw_vf_idx]->dev_pestat;
1310 vf_devstat->ops.iw_hw_stat_read_all(vf_devstat, &vf_devstat->hw_stats);
1311 }
1312 }
1313 spin_unlock_irqrestore(&pf_devstat->stats_lock, flags);
1314 }
1315
1316 mod_timer(&pf_devstat->stats_timer,
1317 jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
1318}
1319
1320/**
1321 * i40iw_hw_stats_start_timer - Start periodic stats timer
1322 * @dev: hardware control device structure
1323 */
1324void i40iw_hw_stats_start_timer(struct i40iw_sc_dev *dev)
1325{
1326 struct i40iw_dev_pestat *devstat = &dev->dev_pestat;
1327
1328 init_timer(&devstat->stats_timer);
1329 devstat->stats_timer.function = i40iw_hw_stats_timeout;
1330 devstat->stats_timer.data = (unsigned long)dev;
1331 mod_timer(&devstat->stats_timer,
1332 jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
1333}
1334
1335/**
1336 * i40iw_hw_stats_del_timer - Delete periodic stats timer
1337 * @dev: hardware control device structure
1338 */
1339void i40iw_hw_stats_del_timer(struct i40iw_sc_dev *dev)
1340{
1341 struct i40iw_dev_pestat *devstat = &dev->dev_pestat;
1342
1343 del_timer_sync(&devstat->stats_timer);
1344}