RDMA: Handle PD allocations by IB/core
[linux-2.6-block.git] / drivers / infiniband / hw / i40iw / i40iw_utils.c
CommitLineData
4e9042e6
FL
1/*******************************************************************************
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,
20c61f7e 62 u32 *ip_addr,
4e9042e6
FL
63 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/**
0c5d5155 140 * i40iw_inetaddr_event - system notifier for ipv4 addr events
4e9042e6
FL
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;
20c61f7e 155 u32 local_ipaddr;
e5e74b61 156 u32 action = I40IW_ARP_ADD;
4e9042e6
FL
157
158 hdl = i40iw_find_netdev(event_netdev);
159 if (!hdl)
160 return NOTIFY_DONE;
161
162 iwdev = &hdl->device;
47fb3c16 163 if (iwdev->init_state < IP_ADDR_REGISTERED || iwdev->closing)
871a8623
SS
164 return NOTIFY_DONE;
165
4e9042e6
FL
166 netdev = iwdev->ldev->netdev;
167 upper_dev = netdev_master_upper_dev_get(netdev);
168 if (netdev != event_netdev)
169 return NOTIFY_DONE;
170
b965b51c
BVA
171 if (upper_dev) {
172 struct in_device *in;
173
174 rcu_read_lock();
175 in = __in_dev_get_rcu(upper_dev);
176 local_ipaddr = ntohl(in->ifa_list->ifa_address);
177 rcu_read_unlock();
178 } else {
e5e74b61 179 local_ipaddr = ntohl(ifa->ifa_address);
b965b51c 180 }
4e9042e6
FL
181 switch (event) {
182 case NETDEV_DOWN:
e5e74b61
MI
183 action = I40IW_ARP_DELETE;
184 /* Fall through */
4e9042e6 185 case NETDEV_UP:
e5e74b61 186 /* Fall through */
4e9042e6 187 case NETDEV_CHANGEADDR:
4e9042e6
FL
188 i40iw_manage_arp_cache(iwdev,
189 netdev->dev_addr,
190 &local_ipaddr,
191 true,
e5e74b61
MI
192 action);
193 i40iw_if_notify(iwdev, netdev, &local_ipaddr, true,
194 (action == I40IW_ARP_ADD) ? true : false);
4e9042e6
FL
195 break;
196 default:
197 break;
198 }
199 return NOTIFY_DONE;
200}
201
202/**
0c5d5155 203 * i40iw_inet6addr_event - system notifier for ipv6 addr events
4e9042e6
FL
204 * @notfier: not used
205 * @event: event for notifier
206 * @ptr: if address
207 */
208int i40iw_inet6addr_event(struct notifier_block *notifier,
209 unsigned long event,
210 void *ptr)
211{
212 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
213 struct net_device *event_netdev = ifa->idev->dev;
214 struct net_device *netdev;
215 struct i40iw_device *iwdev;
216 struct i40iw_handler *hdl;
20c61f7e 217 u32 local_ipaddr6[4];
e5e74b61 218 u32 action = I40IW_ARP_ADD;
4e9042e6
FL
219
220 hdl = i40iw_find_netdev(event_netdev);
221 if (!hdl)
222 return NOTIFY_DONE;
223
224 iwdev = &hdl->device;
47fb3c16 225 if (iwdev->init_state < IP_ADDR_REGISTERED || iwdev->closing)
871a8623
SS
226 return NOTIFY_DONE;
227
4e9042e6
FL
228 netdev = iwdev->ldev->netdev;
229 if (netdev != event_netdev)
230 return NOTIFY_DONE;
231
e5e74b61 232 i40iw_copy_ip_ntohl(local_ipaddr6, ifa->addr.in6_u.u6_addr32);
4e9042e6
FL
233 switch (event) {
234 case NETDEV_DOWN:
e5e74b61
MI
235 action = I40IW_ARP_DELETE;
236 /* Fall through */
4e9042e6
FL
237 case NETDEV_UP:
238 /* Fall through */
239 case NETDEV_CHANGEADDR:
4e9042e6
FL
240 i40iw_manage_arp_cache(iwdev,
241 netdev->dev_addr,
242 local_ipaddr6,
243 false,
e5e74b61
MI
244 action);
245 i40iw_if_notify(iwdev, netdev, local_ipaddr6, false,
246 (action == I40IW_ARP_ADD) ? true : false);
4e9042e6
FL
247 break;
248 default:
249 break;
250 }
251 return NOTIFY_DONE;
252}
253
254/**
0c5d5155 255 * i40iw_net_event - system notifier for netevents
4e9042e6
FL
256 * @notfier: not used
257 * @event: event for notifier
258 * @ptr: neighbor
259 */
260int i40iw_net_event(struct notifier_block *notifier, unsigned long event, void *ptr)
261{
262 struct neighbour *neigh = ptr;
263 struct i40iw_device *iwdev;
264 struct i40iw_handler *iwhdl;
265 __be32 *p;
266 u32 local_ipaddr[4];
267
268 switch (event) {
269 case NETEVENT_NEIGH_UPDATE:
270 iwhdl = i40iw_find_netdev((struct net_device *)neigh->dev);
271 if (!iwhdl)
272 return NOTIFY_DONE;
273 iwdev = &iwhdl->device;
47fb3c16 274 if (iwdev->init_state < IP_ADDR_REGISTERED || iwdev->closing)
871a8623 275 return NOTIFY_DONE;
4e9042e6
FL
276 p = (__be32 *)neigh->primary_key;
277 i40iw_copy_ip_ntohl(local_ipaddr, p);
278 if (neigh->nud_state & NUD_VALID) {
279 i40iw_manage_arp_cache(iwdev,
280 neigh->ha,
281 local_ipaddr,
282 false,
283 I40IW_ARP_ADD);
284
285 } else {
286 i40iw_manage_arp_cache(iwdev,
287 neigh->ha,
288 local_ipaddr,
289 false,
290 I40IW_ARP_DELETE);
291 }
292 break;
293 default:
294 break;
295 }
296 return NOTIFY_DONE;
297}
298
0c5d5155
SS
299/**
300 * i40iw_netdevice_event - system notifier for netdev events
301 * @notfier: not used
302 * @event: event for notifier
303 * @ptr: netdev
304 */
305int i40iw_netdevice_event(struct notifier_block *notifier,
306 unsigned long event,
307 void *ptr)
308{
309 struct net_device *event_netdev;
310 struct net_device *netdev;
311 struct i40iw_device *iwdev;
312 struct i40iw_handler *hdl;
313
314 event_netdev = netdev_notifier_info_to_dev(ptr);
315
316 hdl = i40iw_find_netdev(event_netdev);
317 if (!hdl)
318 return NOTIFY_DONE;
319
320 iwdev = &hdl->device;
321 if (iwdev->init_state < RDMA_DEV_REGISTERED || iwdev->closing)
322 return NOTIFY_DONE;
323
324 netdev = iwdev->ldev->netdev;
325 if (netdev != event_netdev)
326 return NOTIFY_DONE;
327
328 iwdev->iw_status = 1;
329
330 switch (event) {
331 case NETDEV_DOWN:
332 iwdev->iw_status = 0;
333 /* Fall through */
334 case NETDEV_UP:
335 i40iw_port_ibevent(iwdev);
336 break;
337 default:
338 break;
339 }
340 return NOTIFY_DONE;
341}
342
4e9042e6
FL
343/**
344 * i40iw_get_cqp_request - get cqp struct
345 * @cqp: device cqp ptr
346 * @wait: cqp to be used in wait mode
347 */
348struct i40iw_cqp_request *i40iw_get_cqp_request(struct i40iw_cqp *cqp, bool wait)
349{
350 struct i40iw_cqp_request *cqp_request = NULL;
351 unsigned long flags;
352
353 spin_lock_irqsave(&cqp->req_lock, flags);
354 if (!list_empty(&cqp->cqp_avail_reqs)) {
355 cqp_request = list_entry(cqp->cqp_avail_reqs.next,
356 struct i40iw_cqp_request, list);
357 list_del_init(&cqp_request->list);
358 }
359 spin_unlock_irqrestore(&cqp->req_lock, flags);
360 if (!cqp_request) {
361 cqp_request = kzalloc(sizeof(*cqp_request), GFP_ATOMIC);
362 if (cqp_request) {
363 cqp_request->dynamic = true;
364 INIT_LIST_HEAD(&cqp_request->list);
365 init_waitqueue_head(&cqp_request->waitq);
366 }
367 }
368 if (!cqp_request) {
369 i40iw_pr_err("CQP Request Fail: No Memory");
370 return NULL;
371 }
372
373 if (wait) {
374 atomic_set(&cqp_request->refcount, 2);
375 cqp_request->waiting = true;
376 } else {
377 atomic_set(&cqp_request->refcount, 1);
378 }
379 return cqp_request;
380}
381
382/**
383 * i40iw_free_cqp_request - free cqp request
384 * @cqp: cqp ptr
385 * @cqp_request: to be put back in cqp list
386 */
387void i40iw_free_cqp_request(struct i40iw_cqp *cqp, struct i40iw_cqp_request *cqp_request)
388{
44b99f88 389 struct i40iw_device *iwdev = container_of(cqp, struct i40iw_device, cqp);
4e9042e6
FL
390 unsigned long flags;
391
392 if (cqp_request->dynamic) {
393 kfree(cqp_request);
394 } else {
395 cqp_request->request_done = false;
396 cqp_request->callback_fcn = NULL;
397 cqp_request->waiting = false;
398
399 spin_lock_irqsave(&cqp->req_lock, flags);
400 list_add_tail(&cqp_request->list, &cqp->cqp_avail_reqs);
401 spin_unlock_irqrestore(&cqp->req_lock, flags);
402 }
44b99f88 403 wake_up(&iwdev->close_wq);
4e9042e6
FL
404}
405
406/**
407 * i40iw_put_cqp_request - dec ref count and free if 0
408 * @cqp: cqp ptr
409 * @cqp_request: to be put back in cqp list
410 */
411void i40iw_put_cqp_request(struct i40iw_cqp *cqp,
412 struct i40iw_cqp_request *cqp_request)
413{
414 if (atomic_dec_and_test(&cqp_request->refcount))
415 i40iw_free_cqp_request(cqp, cqp_request);
416}
417
44b99f88
SS
418/**
419 * i40iw_free_pending_cqp_request -free pending cqp request objs
420 * @cqp: cqp ptr
421 * @cqp_request: to be put back in cqp list
422 */
423static void i40iw_free_pending_cqp_request(struct i40iw_cqp *cqp,
424 struct i40iw_cqp_request *cqp_request)
425{
426 struct i40iw_device *iwdev = container_of(cqp, struct i40iw_device, cqp);
427
428 if (cqp_request->waiting) {
429 cqp_request->compl_info.error = true;
430 cqp_request->request_done = true;
431 wake_up(&cqp_request->waitq);
432 }
433 i40iw_put_cqp_request(cqp, cqp_request);
434 wait_event_timeout(iwdev->close_wq,
435 !atomic_read(&cqp_request->refcount),
436 1000);
437}
438
439/**
440 * i40iw_cleanup_pending_cqp_op - clean-up cqp with no completions
441 * @iwdev: iwarp device
442 */
443void i40iw_cleanup_pending_cqp_op(struct i40iw_device *iwdev)
444{
445 struct i40iw_sc_dev *dev = &iwdev->sc_dev;
446 struct i40iw_cqp *cqp = &iwdev->cqp;
447 struct i40iw_cqp_request *cqp_request = NULL;
448 struct cqp_commands_info *pcmdinfo = NULL;
449 u32 i, pending_work, wqe_idx;
450
451 pending_work = I40IW_RING_WORK_AVAILABLE(cqp->sc_cqp.sq_ring);
452 wqe_idx = I40IW_RING_GETCURRENT_TAIL(cqp->sc_cqp.sq_ring);
453 for (i = 0; i < pending_work; i++) {
454 cqp_request = (struct i40iw_cqp_request *)(unsigned long)cqp->scratch_array[wqe_idx];
455 if (cqp_request)
456 i40iw_free_pending_cqp_request(cqp, cqp_request);
457 wqe_idx = (wqe_idx + 1) % I40IW_RING_GETSIZE(cqp->sc_cqp.sq_ring);
458 }
459
460 while (!list_empty(&dev->cqp_cmd_head)) {
461 pcmdinfo = (struct cqp_commands_info *)i40iw_remove_head(&dev->cqp_cmd_head);
462 cqp_request = container_of(pcmdinfo, struct i40iw_cqp_request, info);
463 if (cqp_request)
464 i40iw_free_pending_cqp_request(cqp, cqp_request);
465 }
466}
467
4e9042e6
FL
468/**
469 * i40iw_free_qp - callback after destroy cqp completes
470 * @cqp_request: cqp request for destroy qp
471 * @num: not used
472 */
473static void i40iw_free_qp(struct i40iw_cqp_request *cqp_request, u32 num)
474{
475 struct i40iw_sc_qp *qp = (struct i40iw_sc_qp *)cqp_request->param;
476 struct i40iw_qp *iwqp = (struct i40iw_qp *)qp->back_qp;
477 struct i40iw_device *iwdev;
478 u32 qp_num = iwqp->ibqp.qp_num;
479
480 iwdev = iwqp->iwdev;
481
482 i40iw_rem_pdusecount(iwqp->iwpd, iwdev);
483 i40iw_free_qp_resources(iwdev, iwqp, qp_num);
d5965934 484 i40iw_rem_devusecount(iwdev);
4e9042e6
FL
485}
486
487/**
488 * i40iw_wait_event - wait for completion
489 * @iwdev: iwarp device
490 * @cqp_request: cqp request to wait
491 */
492static int i40iw_wait_event(struct i40iw_device *iwdev,
493 struct i40iw_cqp_request *cqp_request)
494{
495 struct cqp_commands_info *info = &cqp_request->info;
496 struct i40iw_cqp *iwcqp = &iwdev->cqp;
d26875b4 497 struct i40iw_cqp_timeout cqp_timeout;
4e9042e6
FL
498 bool cqp_error = false;
499 int err_code = 0;
d26875b4
SS
500 memset(&cqp_timeout, 0, sizeof(cqp_timeout));
501 cqp_timeout.compl_cqp_cmds = iwdev->sc_dev.cqp_cmd_stats[OP_COMPLETED_COMMANDS];
502 do {
503 if (wait_event_timeout(cqp_request->waitq,
504 cqp_request->request_done, CQP_COMPL_WAIT_TIME))
505 break;
506
507 i40iw_check_cqp_progress(&cqp_timeout, &iwdev->sc_dev);
508
509 if (cqp_timeout.count < CQP_TIMEOUT_THRESHOLD)
510 continue;
511
512 i40iw_pr_err("error cqp command 0x%x timed out", info->cqp_cmd);
4e9042e6 513 err_code = -ETIME;
78300cf8
HO
514 if (!iwdev->reset) {
515 iwdev->reset = true;
516 i40iw_request_reset(iwdev);
517 }
4e9042e6 518 goto done;
d26875b4 519 } while (1);
4e9042e6
FL
520 cqp_error = cqp_request->compl_info.error;
521 if (cqp_error) {
522 i40iw_pr_err("error cqp command 0x%x completion maj = 0x%x min=0x%x\n",
523 info->cqp_cmd, cqp_request->compl_info.maj_err_code,
524 cqp_request->compl_info.min_err_code);
525 err_code = -EPROTO;
526 goto done;
527 }
528done:
529 i40iw_put_cqp_request(iwcqp, cqp_request);
530 return err_code;
531}
532
533/**
534 * i40iw_handle_cqp_op - process cqp command
535 * @iwdev: iwarp device
536 * @cqp_request: cqp request to process
537 */
538enum i40iw_status_code i40iw_handle_cqp_op(struct i40iw_device *iwdev,
539 struct i40iw_cqp_request
540 *cqp_request)
541{
542 struct i40iw_sc_dev *dev = &iwdev->sc_dev;
543 enum i40iw_status_code status;
544 struct cqp_commands_info *info = &cqp_request->info;
545 int err_code = 0;
546
78300cf8
HO
547 if (iwdev->reset) {
548 i40iw_free_cqp_request(&iwdev->cqp, cqp_request);
549 return I40IW_ERR_CQP_COMPL_ERROR;
550 }
551
4e9042e6
FL
552 status = i40iw_process_cqp_cmd(dev, info);
553 if (status) {
554 i40iw_pr_err("error cqp command 0x%x failed\n", info->cqp_cmd);
555 i40iw_free_cqp_request(&iwdev->cqp, cqp_request);
556 return status;
557 }
558 if (cqp_request->waiting)
559 err_code = i40iw_wait_event(iwdev, cqp_request);
560 if (err_code)
561 status = I40IW_ERR_CQP_COMPL_ERROR;
562 return status;
563}
564
d5965934
MI
565/**
566 * i40iw_add_devusecount - add dev refcount
567 * @iwdev: dev for refcount
568 */
569void i40iw_add_devusecount(struct i40iw_device *iwdev)
570{
571 atomic64_inc(&iwdev->use_count);
572}
573
574/**
575 * i40iw_rem_devusecount - decrement refcount for dev
576 * @iwdev: device
577 */
578void i40iw_rem_devusecount(struct i40iw_device *iwdev)
579{
580 if (!atomic64_dec_and_test(&iwdev->use_count))
581 return;
582 wake_up(&iwdev->close_wq);
583}
584
4e9042e6
FL
585/**
586 * i40iw_add_pdusecount - add pd refcount
587 * @iwpd: pd for refcount
588 */
589void i40iw_add_pdusecount(struct i40iw_pd *iwpd)
590{
591 atomic_inc(&iwpd->usecount);
592}
593
594/**
595 * i40iw_rem_pdusecount - decrement refcount for pd and free if 0
596 * @iwpd: pd for refcount
597 * @iwdev: iwarp device
598 */
599void i40iw_rem_pdusecount(struct i40iw_pd *iwpd, struct i40iw_device *iwdev)
600{
601 if (!atomic_dec_and_test(&iwpd->usecount))
602 return;
603 i40iw_free_resource(iwdev, iwdev->allocated_pds, iwpd->sc_pd.pd_id);
4e9042e6
FL
604}
605
606/**
607 * i40iw_add_ref - add refcount for qp
608 * @ibqp: iqarp qp
609 */
610void i40iw_add_ref(struct ib_qp *ibqp)
611{
612 struct i40iw_qp *iwqp = (struct i40iw_qp *)ibqp;
613
614 atomic_inc(&iwqp->refcount);
615}
616
617/**
618 * i40iw_rem_ref - rem refcount for qp and free if 0
619 * @ibqp: iqarp qp
620 */
621void i40iw_rem_ref(struct ib_qp *ibqp)
622{
623 struct i40iw_qp *iwqp;
624 enum i40iw_status_code status;
625 struct i40iw_cqp_request *cqp_request;
626 struct cqp_commands_info *cqp_info;
627 struct i40iw_device *iwdev;
628 u32 qp_num;
996abf0a 629 unsigned long flags;
4e9042e6
FL
630
631 iwqp = to_iwqp(ibqp);
996abf0a
IM
632 iwdev = iwqp->iwdev;
633 spin_lock_irqsave(&iwdev->qptable_lock, flags);
634 if (!atomic_dec_and_test(&iwqp->refcount)) {
635 spin_unlock_irqrestore(&iwdev->qptable_lock, flags);
4e9042e6 636 return;
996abf0a 637 }
4e9042e6 638
4e9042e6
FL
639 qp_num = iwqp->ibqp.qp_num;
640 iwdev->qp_table[qp_num] = NULL;
996abf0a 641 spin_unlock_irqrestore(&iwdev->qptable_lock, flags);
4e9042e6
FL
642 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
643 if (!cqp_request)
644 return;
645
646 cqp_request->callback_fcn = i40iw_free_qp;
647 cqp_request->param = (void *)&iwqp->sc_qp;
648 cqp_info = &cqp_request->info;
649 cqp_info->cqp_cmd = OP_QP_DESTROY;
650 cqp_info->post_sq = 1;
651 cqp_info->in.u.qp_destroy.qp = &iwqp->sc_qp;
652 cqp_info->in.u.qp_destroy.scratch = (uintptr_t)cqp_request;
653 cqp_info->in.u.qp_destroy.remove_hash_idx = true;
654 status = i40iw_handle_cqp_op(iwdev, cqp_request);
b5e452a0
SS
655 if (!status)
656 return;
657
658 i40iw_rem_pdusecount(iwqp->iwpd, iwdev);
659 i40iw_free_qp_resources(iwdev, iwqp, qp_num);
660 i40iw_rem_devusecount(iwdev);
4e9042e6
FL
661}
662
663/**
664 * i40iw_get_qp - get qp address
665 * @device: iwarp device
666 * @qpn: qp number
667 */
668struct ib_qp *i40iw_get_qp(struct ib_device *device, int qpn)
669{
670 struct i40iw_device *iwdev = to_iwdev(device);
671
672 if ((qpn < IW_FIRST_QPN) || (qpn >= iwdev->max_qp))
673 return NULL;
674
675 return &iwdev->qp_table[qpn]->ibqp;
676}
677
678/**
679 * i40iw_debug_buf - print debug msg and buffer is mask set
680 * @dev: hardware control device structure
681 * @mask: mask to compare if to print debug buffer
682 * @buf: points buffer addr
683 * @size: saize of buffer to print
684 */
685void i40iw_debug_buf(struct i40iw_sc_dev *dev,
686 enum i40iw_debug_flag mask,
687 char *desc,
688 u64 *buf,
689 u32 size)
690{
691 u32 i;
692
693 if (!(dev->debug_mask & mask))
694 return;
695 i40iw_debug(dev, mask, "%s\n", desc);
696 i40iw_debug(dev, mask, "starting address virt=%p phy=%llxh\n", buf,
697 (unsigned long long)virt_to_phys(buf));
698
699 for (i = 0; i < size; i += 8)
700 i40iw_debug(dev, mask, "index %03d val: %016llx\n", i, buf[i / 8]);
701}
702
703/**
704 * i40iw_get_hw_addr - return hw addr
705 * @par: points to shared dev
706 */
707u8 __iomem *i40iw_get_hw_addr(void *par)
708{
709 struct i40iw_sc_dev *dev = (struct i40iw_sc_dev *)par;
710
711 return dev->hw->hw_addr;
712}
713
714/**
715 * i40iw_remove_head - return head entry and remove from list
716 * @list: list for entry
717 */
718void *i40iw_remove_head(struct list_head *list)
719{
720 struct list_head *entry;
721
722 if (list_empty(list))
723 return NULL;
724
725 entry = (void *)list->next;
726 list_del(entry);
727 return (void *)entry;
728}
729
730/**
731 * i40iw_allocate_dma_mem - Memory alloc helper fn
732 * @hw: pointer to the HW structure
733 * @mem: ptr to mem struct to fill out
734 * @size: size of memory requested
735 * @alignment: what to align the allocation to
736 */
737enum i40iw_status_code i40iw_allocate_dma_mem(struct i40iw_hw *hw,
738 struct i40iw_dma_mem *mem,
739 u64 size,
740 u32 alignment)
741{
742 struct pci_dev *pcidev = (struct pci_dev *)hw->dev_context;
743
744 if (!mem)
745 return I40IW_ERR_PARAM;
746 mem->size = ALIGN(size, alignment);
750afb08
LC
747 mem->va = dma_alloc_coherent(&pcidev->dev, mem->size,
748 (dma_addr_t *)&mem->pa, GFP_KERNEL);
4e9042e6
FL
749 if (!mem->va)
750 return I40IW_ERR_NO_MEMORY;
751 return 0;
752}
753
754/**
755 * i40iw_free_dma_mem - Memory free helper fn
756 * @hw: pointer to the HW structure
757 * @mem: ptr to mem struct to free
758 */
759void i40iw_free_dma_mem(struct i40iw_hw *hw, struct i40iw_dma_mem *mem)
760{
761 struct pci_dev *pcidev = (struct pci_dev *)hw->dev_context;
762
763 if (!mem || !mem->va)
764 return;
765
766 dma_free_coherent(&pcidev->dev, mem->size,
767 mem->va, (dma_addr_t)mem->pa);
768 mem->va = NULL;
769}
770
771/**
772 * i40iw_allocate_virt_mem - virtual memory alloc helper fn
773 * @hw: pointer to the HW structure
774 * @mem: ptr to mem struct to fill out
775 * @size: size of memory requested
776 */
777enum i40iw_status_code i40iw_allocate_virt_mem(struct i40iw_hw *hw,
778 struct i40iw_virt_mem *mem,
779 u32 size)
780{
781 if (!mem)
782 return I40IW_ERR_PARAM;
783
784 mem->size = size;
785 mem->va = kzalloc(size, GFP_KERNEL);
786
787 if (mem->va)
788 return 0;
789 else
790 return I40IW_ERR_NO_MEMORY;
791}
792
793/**
794 * i40iw_free_virt_mem - virtual memory free helper fn
795 * @hw: pointer to the HW structure
796 * @mem: ptr to mem struct to free
797 */
798enum i40iw_status_code i40iw_free_virt_mem(struct i40iw_hw *hw,
799 struct i40iw_virt_mem *mem)
800{
801 if (!mem)
802 return I40IW_ERR_PARAM;
7eaf8313
MI
803 /*
804 * mem->va points to the parent of mem, so both mem and mem->va
805 * can not be touched once mem->va is freed
806 */
4e9042e6 807 kfree(mem->va);
4e9042e6
FL
808 return 0;
809}
810
811/**
812 * i40iw_cqp_sds_cmd - create cqp command for sd
813 * @dev: hardware control device structure
814 * @sd_info: information for sd cqp
815 *
816 */
817enum i40iw_status_code i40iw_cqp_sds_cmd(struct i40iw_sc_dev *dev,
818 struct i40iw_update_sds_info *sdinfo)
819{
820 enum i40iw_status_code status;
821 struct i40iw_cqp_request *cqp_request;
822 struct cqp_commands_info *cqp_info;
823 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
824
825 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
826 if (!cqp_request)
827 return I40IW_ERR_NO_MEMORY;
828 cqp_info = &cqp_request->info;
829 memcpy(&cqp_info->in.u.update_pe_sds.info, sdinfo,
830 sizeof(cqp_info->in.u.update_pe_sds.info));
831 cqp_info->cqp_cmd = OP_UPDATE_PE_SDS;
832 cqp_info->post_sq = 1;
833 cqp_info->in.u.update_pe_sds.dev = dev;
834 cqp_info->in.u.update_pe_sds.scratch = (uintptr_t)cqp_request;
835 status = i40iw_handle_cqp_op(iwdev, cqp_request);
836 if (status)
837 i40iw_pr_err("CQP-OP Update SD's fail");
838 return status;
839}
840
0fc2dc58
HO
841/**
842 * i40iw_qp_suspend_resume - cqp command for suspend/resume
843 * @dev: hardware control device structure
844 * @qp: hardware control qp
845 * @suspend: flag if suspend or resume
846 */
847void i40iw_qp_suspend_resume(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp, bool suspend)
848{
849 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
850 struct i40iw_cqp_request *cqp_request;
851 struct i40iw_sc_cqp *cqp = dev->cqp;
852 struct cqp_commands_info *cqp_info;
853 enum i40iw_status_code status;
854
855 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
856 if (!cqp_request)
857 return;
858
859 cqp_info = &cqp_request->info;
860 cqp_info->cqp_cmd = (suspend) ? OP_SUSPEND : OP_RESUME;
861 cqp_info->in.u.suspend_resume.cqp = cqp;
862 cqp_info->in.u.suspend_resume.qp = qp;
863 cqp_info->in.u.suspend_resume.scratch = (uintptr_t)cqp_request;
864 status = i40iw_handle_cqp_op(iwdev, cqp_request);
865 if (status)
866 i40iw_pr_err("CQP-OP QP Suspend/Resume fail");
867}
868
4e9042e6
FL
869/**
870 * i40iw_term_modify_qp - modify qp for term message
871 * @qp: hardware control qp
872 * @next_state: qp's next state
873 * @term: terminate code
874 * @term_len: length
875 */
876void i40iw_term_modify_qp(struct i40iw_sc_qp *qp, u8 next_state, u8 term, u8 term_len)
877{
878 struct i40iw_qp *iwqp;
879
880 iwqp = (struct i40iw_qp *)qp->back_qp;
881 i40iw_next_iw_state(iwqp, next_state, 0, term, term_len);
882};
883
884/**
885 * i40iw_terminate_done - after terminate is completed
886 * @qp: hardware control qp
887 * @timeout_occurred: indicates if terminate timer expired
888 */
889void i40iw_terminate_done(struct i40iw_sc_qp *qp, int timeout_occurred)
890{
891 struct i40iw_qp *iwqp;
892 u32 next_iwarp_state = I40IW_QP_STATE_ERROR;
893 u8 hte = 0;
894 bool first_time;
895 unsigned long flags;
896
897 iwqp = (struct i40iw_qp *)qp->back_qp;
898 spin_lock_irqsave(&iwqp->lock, flags);
899 if (iwqp->hte_added) {
900 iwqp->hte_added = 0;
901 hte = 1;
902 }
903 first_time = !(qp->term_flags & I40IW_TERM_DONE);
904 qp->term_flags |= I40IW_TERM_DONE;
905 spin_unlock_irqrestore(&iwqp->lock, flags);
906 if (first_time) {
907 if (!timeout_occurred)
908 i40iw_terminate_del_timer(qp);
909 else
910 next_iwarp_state = I40IW_QP_STATE_CLOSING;
911
912 i40iw_next_iw_state(iwqp, next_iwarp_state, hte, 0, 0);
913 i40iw_cm_disconn(iwqp);
914 }
915}
916
917/**
918 * i40iw_terminate_imeout - timeout happened
919 * @context: points to iwarp qp
920 */
2ec46d68 921static void i40iw_terminate_timeout(struct timer_list *t)
4e9042e6 922{
2ec46d68 923 struct i40iw_qp *iwqp = from_timer(iwqp, t, terminate_timer);
4e9042e6
FL
924 struct i40iw_sc_qp *qp = (struct i40iw_sc_qp *)&iwqp->sc_qp;
925
926 i40iw_terminate_done(qp, 1);
d627b506 927 i40iw_rem_ref(&iwqp->ibqp);
4e9042e6
FL
928}
929
930/**
931 * i40iw_terminate_start_timer - start terminate timeout
932 * @qp: hardware control qp
933 */
934void i40iw_terminate_start_timer(struct i40iw_sc_qp *qp)
935{
936 struct i40iw_qp *iwqp;
937
938 iwqp = (struct i40iw_qp *)qp->back_qp;
d627b506 939 i40iw_add_ref(&iwqp->ibqp);
2ec46d68 940 timer_setup(&iwqp->terminate_timer, i40iw_terminate_timeout, 0);
4e9042e6 941 iwqp->terminate_timer.expires = jiffies + HZ;
4e9042e6
FL
942 add_timer(&iwqp->terminate_timer);
943}
944
945/**
946 * i40iw_terminate_del_timer - delete terminate timeout
947 * @qp: hardware control qp
948 */
949void i40iw_terminate_del_timer(struct i40iw_sc_qp *qp)
950{
951 struct i40iw_qp *iwqp;
952
953 iwqp = (struct i40iw_qp *)qp->back_qp;
d627b506
SS
954 if (del_timer(&iwqp->terminate_timer))
955 i40iw_rem_ref(&iwqp->ibqp);
4e9042e6
FL
956}
957
958/**
959 * i40iw_cqp_generic_worker - generic worker for cqp
960 * @work: work pointer
961 */
962static void i40iw_cqp_generic_worker(struct work_struct *work)
963{
964 struct i40iw_virtchnl_work_info *work_info =
965 &((struct virtchnl_work *)work)->work_info;
966
967 if (work_info->worker_vf_dev)
968 work_info->callback_fcn(work_info->worker_vf_dev);
969}
970
971/**
972 * i40iw_cqp_spawn_worker - spawn worket thread
973 * @iwdev: device struct pointer
974 * @work_info: work request info
975 * @iw_vf_idx: virtual function index
976 */
977void i40iw_cqp_spawn_worker(struct i40iw_sc_dev *dev,
978 struct i40iw_virtchnl_work_info *work_info,
979 u32 iw_vf_idx)
980{
981 struct virtchnl_work *work;
982 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
983
984 work = &iwdev->virtchnl_w[iw_vf_idx];
985 memcpy(&work->work_info, work_info, sizeof(*work_info));
986 INIT_WORK(&work->work, i40iw_cqp_generic_worker);
987 queue_work(iwdev->virtchnl_wq, &work->work);
988}
989
990/**
991 * i40iw_cqp_manage_hmc_fcn_worker -
992 * @work: work pointer for hmc info
993 */
994static void i40iw_cqp_manage_hmc_fcn_worker(struct work_struct *work)
995{
996 struct i40iw_cqp_request *cqp_request =
997 ((struct virtchnl_work *)work)->cqp_request;
998 struct i40iw_ccq_cqe_info ccq_cqe_info;
999 struct i40iw_hmc_fcn_info *hmcfcninfo =
1000 &cqp_request->info.in.u.manage_hmc_pm.info;
1001 struct i40iw_device *iwdev =
1002 (struct i40iw_device *)cqp_request->info.in.u.manage_hmc_pm.dev->back_dev;
1003
1004 ccq_cqe_info.cqp = NULL;
1005 ccq_cqe_info.maj_err_code = cqp_request->compl_info.maj_err_code;
1006 ccq_cqe_info.min_err_code = cqp_request->compl_info.min_err_code;
1007 ccq_cqe_info.op_code = cqp_request->compl_info.op_code;
1008 ccq_cqe_info.op_ret_val = cqp_request->compl_info.op_ret_val;
1009 ccq_cqe_info.scratch = 0;
1010 ccq_cqe_info.error = cqp_request->compl_info.error;
1011 hmcfcninfo->callback_fcn(cqp_request->info.in.u.manage_hmc_pm.dev,
1012 hmcfcninfo->cqp_callback_param, &ccq_cqe_info);
1013 i40iw_put_cqp_request(&iwdev->cqp, cqp_request);
1014}
1015
1016/**
1017 * i40iw_cqp_manage_hmc_fcn_callback - called function after cqp completion
1018 * @cqp_request: cqp request info struct for hmc fun
1019 * @unused: unused param of callback
1020 */
1021static void i40iw_cqp_manage_hmc_fcn_callback(struct i40iw_cqp_request *cqp_request,
1022 u32 unused)
1023{
1024 struct virtchnl_work *work;
1025 struct i40iw_hmc_fcn_info *hmcfcninfo =
1026 &cqp_request->info.in.u.manage_hmc_pm.info;
1027 struct i40iw_device *iwdev =
1028 (struct i40iw_device *)cqp_request->info.in.u.manage_hmc_pm.dev->
1029 back_dev;
1030
1031 if (hmcfcninfo && hmcfcninfo->callback_fcn) {
1032 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s1\n", __func__);
1033 atomic_inc(&cqp_request->refcount);
1034 work = &iwdev->virtchnl_w[hmcfcninfo->iw_vf_idx];
1035 work->cqp_request = cqp_request;
1036 INIT_WORK(&work->work, i40iw_cqp_manage_hmc_fcn_worker);
1037 queue_work(iwdev->virtchnl_wq, &work->work);
1038 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s2\n", __func__);
1039 } else {
1040 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s: Something wrong\n", __func__);
1041 }
1042}
1043
1044/**
1045 * i40iw_cqp_manage_hmc_fcn_cmd - issue cqp command to manage hmc
1046 * @dev: hardware control device structure
1047 * @hmcfcninfo: info for hmc
1048 */
1049enum i40iw_status_code i40iw_cqp_manage_hmc_fcn_cmd(struct i40iw_sc_dev *dev,
1050 struct i40iw_hmc_fcn_info *hmcfcninfo)
1051{
1052 enum i40iw_status_code status;
1053 struct i40iw_cqp_request *cqp_request;
1054 struct cqp_commands_info *cqp_info;
1055 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1056
1057 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_HMC, "%s\n", __func__);
1058 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
1059 if (!cqp_request)
1060 return I40IW_ERR_NO_MEMORY;
1061 cqp_info = &cqp_request->info;
1062 cqp_request->callback_fcn = i40iw_cqp_manage_hmc_fcn_callback;
1063 cqp_request->param = hmcfcninfo;
1064 memcpy(&cqp_info->in.u.manage_hmc_pm.info, hmcfcninfo,
1065 sizeof(*hmcfcninfo));
1066 cqp_info->in.u.manage_hmc_pm.dev = dev;
1067 cqp_info->cqp_cmd = OP_MANAGE_HMC_PM_FUNC_TABLE;
1068 cqp_info->post_sq = 1;
1069 cqp_info->in.u.manage_hmc_pm.scratch = (uintptr_t)cqp_request;
1070 status = i40iw_handle_cqp_op(iwdev, cqp_request);
1071 if (status)
1072 i40iw_pr_err("CQP-OP Manage HMC fail");
1073 return status;
1074}
1075
1076/**
1077 * i40iw_cqp_query_fpm_values_cmd - send cqp command for fpm
1078 * @iwdev: function device struct
1079 * @values_mem: buffer for fpm
1080 * @hmc_fn_id: function id for fpm
1081 */
1082enum i40iw_status_code i40iw_cqp_query_fpm_values_cmd(struct i40iw_sc_dev *dev,
1083 struct i40iw_dma_mem *values_mem,
1084 u8 hmc_fn_id)
1085{
1086 enum i40iw_status_code status;
1087 struct i40iw_cqp_request *cqp_request;
1088 struct cqp_commands_info *cqp_info;
1089 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1090
1091 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1092 if (!cqp_request)
1093 return I40IW_ERR_NO_MEMORY;
1094 cqp_info = &cqp_request->info;
1095 cqp_request->param = NULL;
1096 cqp_info->in.u.query_fpm_values.cqp = dev->cqp;
1097 cqp_info->in.u.query_fpm_values.fpm_values_pa = values_mem->pa;
1098 cqp_info->in.u.query_fpm_values.fpm_values_va = values_mem->va;
1099 cqp_info->in.u.query_fpm_values.hmc_fn_id = hmc_fn_id;
1100 cqp_info->cqp_cmd = OP_QUERY_FPM_VALUES;
1101 cqp_info->post_sq = 1;
1102 cqp_info->in.u.query_fpm_values.scratch = (uintptr_t)cqp_request;
1103 status = i40iw_handle_cqp_op(iwdev, cqp_request);
1104 if (status)
1105 i40iw_pr_err("CQP-OP Query FPM fail");
1106 return status;
1107}
1108
1109/**
1110 * i40iw_cqp_commit_fpm_values_cmd - commit fpm values in hw
1111 * @dev: hardware control device structure
1112 * @values_mem: buffer with fpm values
1113 * @hmc_fn_id: function id for fpm
1114 */
1115enum i40iw_status_code i40iw_cqp_commit_fpm_values_cmd(struct i40iw_sc_dev *dev,
1116 struct i40iw_dma_mem *values_mem,
1117 u8 hmc_fn_id)
1118{
1119 enum i40iw_status_code status;
1120 struct i40iw_cqp_request *cqp_request;
1121 struct cqp_commands_info *cqp_info;
1122 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1123
1124 cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1125 if (!cqp_request)
1126 return I40IW_ERR_NO_MEMORY;
1127 cqp_info = &cqp_request->info;
1128 cqp_request->param = NULL;
1129 cqp_info->in.u.commit_fpm_values.cqp = dev->cqp;
1130 cqp_info->in.u.commit_fpm_values.fpm_values_pa = values_mem->pa;
1131 cqp_info->in.u.commit_fpm_values.fpm_values_va = values_mem->va;
1132 cqp_info->in.u.commit_fpm_values.hmc_fn_id = hmc_fn_id;
1133 cqp_info->cqp_cmd = OP_COMMIT_FPM_VALUES;
1134 cqp_info->post_sq = 1;
1135 cqp_info->in.u.commit_fpm_values.scratch = (uintptr_t)cqp_request;
1136 status = i40iw_handle_cqp_op(iwdev, cqp_request);
1137 if (status)
1138 i40iw_pr_err("CQP-OP Commit FPM fail");
1139 return status;
1140}
1141
1142/**
1143 * i40iw_vf_wait_vchnl_resp - wait for channel msg
1144 * @iwdev: function's device struct
1145 */
1146enum i40iw_status_code i40iw_vf_wait_vchnl_resp(struct i40iw_sc_dev *dev)
1147{
1148 struct i40iw_device *iwdev = dev->back_dev;
4e9042e6
FL
1149 int timeout_ret;
1150
1151 i40iw_debug(dev, I40IW_DEBUG_VIRT, "%s[%u] dev %p, iwdev %p\n",
1152 __func__, __LINE__, dev, iwdev);
f69c3331
IM
1153
1154 atomic_set(&iwdev->vchnl_msgs, 2);
4e9042e6
FL
1155 timeout_ret = wait_event_timeout(iwdev->vchnl_waitq,
1156 (atomic_read(&iwdev->vchnl_msgs) == 1),
1157 I40IW_VCHNL_EVENT_TIMEOUT);
1158 atomic_dec(&iwdev->vchnl_msgs);
1159 if (!timeout_ret) {
1160 i40iw_pr_err("virt channel completion timeout = 0x%x\n", timeout_ret);
f69c3331
IM
1161 atomic_set(&iwdev->vchnl_msgs, 0);
1162 dev->vchnl_up = false;
1163 return I40IW_ERR_TIMEOUT;
4e9042e6 1164 }
f69c3331
IM
1165 wake_up(&dev->vf_reqs);
1166 return 0;
4e9042e6
FL
1167}
1168
d6f7bbcc
HO
1169/**
1170 * i40iw_cqp_cq_create_cmd - create a cq for the cqp
1171 * @dev: device pointer
1172 * @cq: pointer to created cq
1173 */
1174enum i40iw_status_code i40iw_cqp_cq_create_cmd(struct i40iw_sc_dev *dev,
1175 struct i40iw_sc_cq *cq)
1176{
1177 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1178 struct i40iw_cqp *iwcqp = &iwdev->cqp;
1179 struct i40iw_cqp_request *cqp_request;
1180 struct cqp_commands_info *cqp_info;
1181 enum i40iw_status_code status;
1182
1183 cqp_request = i40iw_get_cqp_request(iwcqp, true);
1184 if (!cqp_request)
1185 return I40IW_ERR_NO_MEMORY;
1186
1187 cqp_info = &cqp_request->info;
1188 cqp_info->cqp_cmd = OP_CQ_CREATE;
1189 cqp_info->post_sq = 1;
1190 cqp_info->in.u.cq_create.cq = cq;
1191 cqp_info->in.u.cq_create.scratch = (uintptr_t)cqp_request;
1192 status = i40iw_handle_cqp_op(iwdev, cqp_request);
1193 if (status)
1194 i40iw_pr_err("CQP-OP Create QP fail");
1195
1196 return status;
1197}
1198
1199/**
1200 * i40iw_cqp_qp_create_cmd - create a qp for the cqp
1201 * @dev: device pointer
1202 * @qp: pointer to created qp
1203 */
1204enum i40iw_status_code i40iw_cqp_qp_create_cmd(struct i40iw_sc_dev *dev,
1205 struct i40iw_sc_qp *qp)
1206{
1207 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1208 struct i40iw_cqp *iwcqp = &iwdev->cqp;
1209 struct i40iw_cqp_request *cqp_request;
1210 struct cqp_commands_info *cqp_info;
1211 struct i40iw_create_qp_info *qp_info;
1212 enum i40iw_status_code status;
1213
1214 cqp_request = i40iw_get_cqp_request(iwcqp, true);
1215 if (!cqp_request)
1216 return I40IW_ERR_NO_MEMORY;
1217
1218 cqp_info = &cqp_request->info;
1219 qp_info = &cqp_request->info.in.u.qp_create.info;
1220
1221 memset(qp_info, 0, sizeof(*qp_info));
1222
1223 qp_info->cq_num_valid = true;
1224 qp_info->next_iwarp_state = I40IW_QP_STATE_RTS;
1225
1226 cqp_info->cqp_cmd = OP_QP_CREATE;
1227 cqp_info->post_sq = 1;
1228 cqp_info->in.u.qp_create.qp = qp;
1229 cqp_info->in.u.qp_create.scratch = (uintptr_t)cqp_request;
1230 status = i40iw_handle_cqp_op(iwdev, cqp_request);
1231 if (status)
1232 i40iw_pr_err("CQP-OP QP create fail");
1233 return status;
1234}
1235
1236/**
1237 * i40iw_cqp_cq_destroy_cmd - destroy the cqp cq
1238 * @dev: device pointer
1239 * @cq: pointer to cq
1240 */
1241void i40iw_cqp_cq_destroy_cmd(struct i40iw_sc_dev *dev, struct i40iw_sc_cq *cq)
1242{
1243 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1244
1245 i40iw_cq_wq_destroy(iwdev, cq);
1246}
1247
1248/**
1249 * i40iw_cqp_qp_destroy_cmd - destroy the cqp
1250 * @dev: device pointer
1251 * @qp: pointer to qp
1252 */
1253void i40iw_cqp_qp_destroy_cmd(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
1254{
1255 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1256 struct i40iw_cqp *iwcqp = &iwdev->cqp;
1257 struct i40iw_cqp_request *cqp_request;
1258 struct cqp_commands_info *cqp_info;
1259 enum i40iw_status_code status;
1260
1261 cqp_request = i40iw_get_cqp_request(iwcqp, true);
1262 if (!cqp_request)
1263 return;
1264
1265 cqp_info = &cqp_request->info;
1266 memset(cqp_info, 0, sizeof(*cqp_info));
1267
1268 cqp_info->cqp_cmd = OP_QP_DESTROY;
1269 cqp_info->post_sq = 1;
1270 cqp_info->in.u.qp_destroy.qp = qp;
1271 cqp_info->in.u.qp_destroy.scratch = (uintptr_t)cqp_request;
1272 cqp_info->in.u.qp_destroy.remove_hash_idx = true;
1273 status = i40iw_handle_cqp_op(iwdev, cqp_request);
1274 if (status)
1275 i40iw_pr_err("CQP QP_DESTROY fail");
1276}
1277
1278
4e9042e6
FL
1279/**
1280 * i40iw_ieq_mpa_crc_ae - generate AE for crc error
1281 * @dev: hardware control device structure
1282 * @qp: hardware control qp
1283 */
1284void i40iw_ieq_mpa_crc_ae(struct i40iw_sc_dev *dev, struct i40iw_sc_qp *qp)
1285{
a8b9234b 1286 struct i40iw_gen_ae_info info;
4e9042e6
FL
1287 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1288
1289 i40iw_debug(dev, I40IW_DEBUG_AEQ, "%s entered\n", __func__);
4e9042e6 1290 info.ae_code = I40IW_AE_LLP_RECEIVED_MPA_CRC_ERROR;
a8b9234b
HO
1291 info.ae_source = I40IW_AE_SOURCE_RQ;
1292 i40iw_gen_ae(iwdev, qp, &info, false);
4e9042e6
FL
1293}
1294
1295/**
1296 * i40iw_init_hash_desc - initialize hash for crc calculation
1297 * @desc: cryption type
1298 */
34abf9ed 1299enum i40iw_status_code i40iw_init_hash_desc(struct shash_desc **desc)
4e9042e6 1300{
34abf9ed
TN
1301 struct crypto_shash *tfm;
1302 struct shash_desc *tdesc;
1303
1304 tfm = crypto_alloc_shash("crc32c", 0, 0);
1305 if (IS_ERR(tfm))
1306 return I40IW_ERR_MPA_CRC;
1307
1308 tdesc = kzalloc(sizeof(*tdesc) + crypto_shash_descsize(tfm),
1309 GFP_KERNEL);
1310 if (!tdesc) {
1311 crypto_free_shash(tfm);
4e9042e6 1312 return I40IW_ERR_MPA_CRC;
34abf9ed
TN
1313 }
1314 tdesc->tfm = tfm;
1315 *desc = tdesc;
1316
4e9042e6
FL
1317 return 0;
1318}
1319
1320/**
1321 * i40iw_free_hash_desc - free hash desc
1322 * @desc: to be freed
1323 */
34abf9ed 1324void i40iw_free_hash_desc(struct shash_desc *desc)
4e9042e6 1325{
34abf9ed
TN
1326 if (desc) {
1327 crypto_free_shash(desc->tfm);
1328 kfree(desc);
1329 }
4e9042e6
FL
1330}
1331
1332/**
1333 * i40iw_alloc_query_fpm_buf - allocate buffer for fpm
1334 * @dev: hardware control device structure
1335 * @mem: buffer ptr for fpm to be allocated
1336 * @return: memory allocation status
1337 */
1338enum i40iw_status_code i40iw_alloc_query_fpm_buf(struct i40iw_sc_dev *dev,
1339 struct i40iw_dma_mem *mem)
1340{
1341 enum i40iw_status_code status;
1342 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1343
1344 status = i40iw_obj_aligned_mem(iwdev, mem, I40IW_QUERY_FPM_BUF_SIZE,
1345 I40IW_FPM_QUERY_BUF_ALIGNMENT_MASK);
1346 return status;
1347}
1348
1349/**
1350 * i40iw_ieq_check_mpacrc - check if mpa crc is OK
1351 * @desc: desc for hash
1352 * @addr: address of buffer for crc
1353 * @length: length of buffer
1354 * @value: value to be compared
1355 */
34abf9ed 1356enum i40iw_status_code i40iw_ieq_check_mpacrc(struct shash_desc *desc,
4e9042e6
FL
1357 void *addr,
1358 u32 length,
1359 u32 value)
1360{
4e9042e6
FL
1361 u32 crc = 0;
1362 int ret;
1363 enum i40iw_status_code ret_code = 0;
1364
34abf9ed
TN
1365 crypto_shash_init(desc);
1366 ret = crypto_shash_update(desc, addr, length);
4e9042e6 1367 if (!ret)
34abf9ed 1368 crypto_shash_final(desc, (u8 *)&crc);
4e9042e6
FL
1369 if (crc != value) {
1370 i40iw_pr_err("mpa crc check fail\n");
1371 ret_code = I40IW_ERR_MPA_CRC;
1372 }
1373 return ret_code;
1374}
1375
1376/**
1377 * i40iw_ieq_get_qp - get qp based on quad in puda buffer
1378 * @dev: hardware control device structure
1379 * @buf: receive puda buffer on exception q
1380 */
1381struct i40iw_sc_qp *i40iw_ieq_get_qp(struct i40iw_sc_dev *dev,
1382 struct i40iw_puda_buf *buf)
1383{
1384 struct i40iw_device *iwdev = (struct i40iw_device *)dev->back_dev;
1385 struct i40iw_qp *iwqp;
1386 struct i40iw_cm_node *cm_node;
1387 u32 loc_addr[4], rem_addr[4];
1388 u16 loc_port, rem_port;
1389 struct ipv6hdr *ip6h;
1390 struct iphdr *iph = (struct iphdr *)buf->iph;
1391 struct tcphdr *tcph = (struct tcphdr *)buf->tcph;
1392
1393 if (iph->version == 4) {
1394 memset(loc_addr, 0, sizeof(loc_addr));
1395 loc_addr[0] = ntohl(iph->daddr);
1396 memset(rem_addr, 0, sizeof(rem_addr));
1397 rem_addr[0] = ntohl(iph->saddr);
1398 } else {
1399 ip6h = (struct ipv6hdr *)buf->iph;
1400 i40iw_copy_ip_ntohl(loc_addr, ip6h->daddr.in6_u.u6_addr32);
1401 i40iw_copy_ip_ntohl(rem_addr, ip6h->saddr.in6_u.u6_addr32);
1402 }
1403 loc_port = ntohs(tcph->dest);
1404 rem_port = ntohs(tcph->source);
1405
1406 cm_node = i40iw_find_node(&iwdev->cm_core, rem_port, rem_addr, loc_port,
7de8b357 1407 loc_addr, false, true);
4e9042e6
FL
1408 if (!cm_node)
1409 return NULL;
1410 iwqp = cm_node->iwqp;
1411 return &iwqp->sc_qp;
1412}
1413
1414/**
1415 * i40iw_ieq_update_tcpip_info - update tcpip in the buffer
1416 * @buf: puda to update
1417 * @length: length of buffer
1418 * @seqnum: seq number for tcp
1419 */
1420void i40iw_ieq_update_tcpip_info(struct i40iw_puda_buf *buf, u16 length, u32 seqnum)
1421{
1422 struct tcphdr *tcph;
1423 struct iphdr *iph;
1424 u16 iphlen;
1425 u16 packetsize;
1426 u8 *addr = (u8 *)buf->mem.va;
1427
1428 iphlen = (buf->ipv4) ? 20 : 40;
1429 iph = (struct iphdr *)(addr + buf->maclen);
1430 tcph = (struct tcphdr *)(addr + buf->maclen + iphlen);
1431 packetsize = length + buf->tcphlen + iphlen;
1432
1433 iph->tot_len = htons(packetsize);
1434 tcph->seq = htonl(seqnum);
1435}
1436
1437/**
1438 * i40iw_puda_get_tcpip_info - get tcpip info from puda buffer
1439 * @info: to get information
1440 * @buf: puda buffer
1441 */
1442enum i40iw_status_code i40iw_puda_get_tcpip_info(struct i40iw_puda_completion_info *info,
1443 struct i40iw_puda_buf *buf)
1444{
1445 struct iphdr *iph;
1446 struct ipv6hdr *ip6h;
1447 struct tcphdr *tcph;
1448 u16 iphlen;
1449 u16 pkt_len;
1450 u8 *mem = (u8 *)buf->mem.va;
1451 struct ethhdr *ethh = (struct ethhdr *)buf->mem.va;
1452
1453 if (ethh->h_proto == htons(0x8100)) {
1454 info->vlan_valid = true;
1455 buf->vlan_id = ntohs(((struct vlan_ethhdr *)ethh)->h_vlan_TCI) & VLAN_VID_MASK;
1456 }
1457 buf->maclen = (info->vlan_valid) ? 18 : 14;
1458 iphlen = (info->l3proto) ? 40 : 20;
1459 buf->ipv4 = (info->l3proto) ? false : true;
1460 buf->iph = mem + buf->maclen;
1461 iph = (struct iphdr *)buf->iph;
1462
1463 buf->tcph = buf->iph + iphlen;
1464 tcph = (struct tcphdr *)buf->tcph;
1465
1466 if (buf->ipv4) {
1467 pkt_len = ntohs(iph->tot_len);
1468 } else {
1469 ip6h = (struct ipv6hdr *)buf->iph;
1470 pkt_len = ntohs(ip6h->payload_len) + iphlen;
1471 }
1472
1473 buf->totallen = pkt_len + buf->maclen;
1474
7581e96c 1475 if (info->payload_len < buf->totallen) {
4e9042e6
FL
1476 i40iw_pr_err("payload_len = 0x%x totallen expected0x%x\n",
1477 info->payload_len, buf->totallen);
1478 return I40IW_ERR_INVALID_SIZE;
1479 }
1480
1481 buf->tcphlen = (tcph->doff) << 2;
1482 buf->datalen = pkt_len - iphlen - buf->tcphlen;
1483 buf->data = (buf->datalen) ? buf->tcph + buf->tcphlen : NULL;
1484 buf->hdrlen = buf->maclen + iphlen + buf->tcphlen;
1485 buf->seqnum = ntohl(tcph->seq);
1486 return 0;
1487}
1488
1489/**
1490 * i40iw_hw_stats_timeout - Stats timer-handler which updates all HW stats
d6f7bbcc 1491 * @vsi: pointer to the vsi structure
4e9042e6 1492 */
605cbb2c 1493static void i40iw_hw_stats_timeout(struct timer_list *t)
4e9042e6 1494{
605cbb2c
KC
1495 struct i40iw_vsi_pestat *pf_devstat = from_timer(pf_devstat, t,
1496 stats_timer);
1497 struct i40iw_sc_vsi *sc_vsi = pf_devstat->vsi;
d6f7bbcc 1498 struct i40iw_sc_dev *pf_dev = sc_vsi->dev;
d6f7bbcc 1499 struct i40iw_vsi_pestat *vf_devstat = NULL;
4e9042e6
FL
1500 u16 iw_vf_idx;
1501 unsigned long flags;
1502
1503 /*PF*/
d6f7bbcc
HO
1504 i40iw_hw_stats_read_all(pf_devstat, &pf_devstat->hw_stats);
1505
4e9042e6 1506 for (iw_vf_idx = 0; iw_vf_idx < I40IW_MAX_PE_ENABLED_VF_COUNT; iw_vf_idx++) {
d6f7bbcc 1507 spin_lock_irqsave(&pf_devstat->lock, flags);
4e9042e6
FL
1508 if (pf_dev->vf_dev[iw_vf_idx]) {
1509 if (pf_dev->vf_dev[iw_vf_idx]->stats_initialized) {
d6f7bbcc
HO
1510 vf_devstat = &pf_dev->vf_dev[iw_vf_idx]->pestat;
1511 i40iw_hw_stats_read_all(vf_devstat, &vf_devstat->hw_stats);
4e9042e6
FL
1512 }
1513 }
d6f7bbcc 1514 spin_unlock_irqrestore(&pf_devstat->lock, flags);
4e9042e6
FL
1515 }
1516
1517 mod_timer(&pf_devstat->stats_timer,
1518 jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
1519}
1520
1521/**
1522 * i40iw_hw_stats_start_timer - Start periodic stats timer
d6f7bbcc 1523 * @vsi: pointer to the vsi structure
4e9042e6 1524 */
d6f7bbcc 1525void i40iw_hw_stats_start_timer(struct i40iw_sc_vsi *vsi)
4e9042e6 1526{
d6f7bbcc 1527 struct i40iw_vsi_pestat *devstat = vsi->pestat;
4e9042e6 1528
605cbb2c 1529 timer_setup(&devstat->stats_timer, i40iw_hw_stats_timeout, 0);
4e9042e6
FL
1530 mod_timer(&devstat->stats_timer,
1531 jiffies + msecs_to_jiffies(STATS_TIMER_DELAY));
1532}
1533
1534/**
d6f7bbcc
HO
1535 * i40iw_hw_stats_stop_timer - Delete periodic stats timer
1536 * @vsi: pointer to the vsi structure
4e9042e6 1537 */
d6f7bbcc 1538void i40iw_hw_stats_stop_timer(struct i40iw_sc_vsi *vsi)
4e9042e6 1539{
d6f7bbcc 1540 struct i40iw_vsi_pestat *devstat = vsi->pestat;
4e9042e6
FL
1541
1542 del_timer_sync(&devstat->stats_timer);
1543}