Input: wm97xx: add new AC97 bus support
[sfrench/cifs-2.6.git] / drivers / infiniband / hw / i40iw / i40iw_main.c
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/ip.h>
40 #include <linux/tcp.h>
41 #include <linux/if_vlan.h>
42 #include <net/addrconf.h>
43
44 #include "i40iw.h"
45 #include "i40iw_register.h"
46 #include <net/netevent.h>
47 #define CLIENT_IW_INTERFACE_VERSION_MAJOR 0
48 #define CLIENT_IW_INTERFACE_VERSION_MINOR 01
49 #define CLIENT_IW_INTERFACE_VERSION_BUILD 00
50
51 #define DRV_VERSION_MAJOR 0
52 #define DRV_VERSION_MINOR 5
53 #define DRV_VERSION_BUILD 123
54 #define DRV_VERSION     __stringify(DRV_VERSION_MAJOR) "."              \
55         __stringify(DRV_VERSION_MINOR) "." __stringify(DRV_VERSION_BUILD)
56
57 static int push_mode;
58 module_param(push_mode, int, 0644);
59 MODULE_PARM_DESC(push_mode, "Low latency mode: 0=disabled (default), 1=enabled)");
60
61 static int debug;
62 module_param(debug, int, 0644);
63 MODULE_PARM_DESC(debug, "debug flags: 0=disabled (default), 0x7fffffff=all");
64
65 static int resource_profile;
66 module_param(resource_profile, int, 0644);
67 MODULE_PARM_DESC(resource_profile,
68                  "Resource Profile: 0=no VF RDMA support (default), 1=Weighted VF, 2=Even Distribution");
69
70 static int max_rdma_vfs = 32;
71 module_param(max_rdma_vfs, int, 0644);
72 MODULE_PARM_DESC(max_rdma_vfs, "Maximum VF count: 0-32 32=default");
73 static int mpa_version = 2;
74 module_param(mpa_version, int, 0644);
75 MODULE_PARM_DESC(mpa_version, "MPA version to be used in MPA Req/Resp 1 or 2");
76
77 MODULE_AUTHOR("Intel Corporation, <e1000-rdma@lists.sourceforge.net>");
78 MODULE_DESCRIPTION("Intel(R) Ethernet Connection X722 iWARP RDMA Driver");
79 MODULE_LICENSE("Dual BSD/GPL");
80
81 static struct i40e_client i40iw_client;
82 static char i40iw_client_name[I40E_CLIENT_STR_LENGTH] = "i40iw";
83
84 static LIST_HEAD(i40iw_handlers);
85 static spinlock_t i40iw_handler_lock;
86
87 static enum i40iw_status_code i40iw_virtchnl_send(struct i40iw_sc_dev *dev,
88                                                   u32 vf_id, u8 *msg, u16 len);
89
90 static struct notifier_block i40iw_inetaddr_notifier = {
91         .notifier_call = i40iw_inetaddr_event
92 };
93
94 static struct notifier_block i40iw_inetaddr6_notifier = {
95         .notifier_call = i40iw_inet6addr_event
96 };
97
98 static struct notifier_block i40iw_net_notifier = {
99         .notifier_call = i40iw_net_event
100 };
101
102 static atomic_t i40iw_notifiers_registered;
103
104 /**
105  * i40iw_find_i40e_handler - find a handler given a client info
106  * @ldev: pointer to a client info
107  */
108 static struct i40iw_handler *i40iw_find_i40e_handler(struct i40e_info *ldev)
109 {
110         struct i40iw_handler *hdl;
111         unsigned long flags;
112
113         spin_lock_irqsave(&i40iw_handler_lock, flags);
114         list_for_each_entry(hdl, &i40iw_handlers, list) {
115                 if (hdl->ldev.netdev == ldev->netdev) {
116                         spin_unlock_irqrestore(&i40iw_handler_lock, flags);
117                         return hdl;
118                 }
119         }
120         spin_unlock_irqrestore(&i40iw_handler_lock, flags);
121         return NULL;
122 }
123
124 /**
125  * i40iw_find_netdev - find a handler given a netdev
126  * @netdev: pointer to net_device
127  */
128 struct i40iw_handler *i40iw_find_netdev(struct net_device *netdev)
129 {
130         struct i40iw_handler *hdl;
131         unsigned long flags;
132
133         spin_lock_irqsave(&i40iw_handler_lock, flags);
134         list_for_each_entry(hdl, &i40iw_handlers, list) {
135                 if (hdl->ldev.netdev == netdev) {
136                         spin_unlock_irqrestore(&i40iw_handler_lock, flags);
137                         return hdl;
138                 }
139         }
140         spin_unlock_irqrestore(&i40iw_handler_lock, flags);
141         return NULL;
142 }
143
144 /**
145  * i40iw_add_handler - add a handler to the list
146  * @hdl: handler to be added to the handler list
147  */
148 static void i40iw_add_handler(struct i40iw_handler *hdl)
149 {
150         unsigned long flags;
151
152         spin_lock_irqsave(&i40iw_handler_lock, flags);
153         list_add(&hdl->list, &i40iw_handlers);
154         spin_unlock_irqrestore(&i40iw_handler_lock, flags);
155 }
156
157 /**
158  * i40iw_del_handler - delete a handler from the list
159  * @hdl: handler to be deleted from the handler list
160  */
161 static int i40iw_del_handler(struct i40iw_handler *hdl)
162 {
163         unsigned long flags;
164
165         spin_lock_irqsave(&i40iw_handler_lock, flags);
166         list_del(&hdl->list);
167         spin_unlock_irqrestore(&i40iw_handler_lock, flags);
168         return 0;
169 }
170
171 /**
172  * i40iw_enable_intr - set up device interrupts
173  * @dev: hardware control device structure
174  * @msix_id: id of the interrupt to be enabled
175  */
176 static void i40iw_enable_intr(struct i40iw_sc_dev *dev, u32 msix_id)
177 {
178         u32 val;
179
180         val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
181                 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
182                 (3 << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
183         if (dev->is_pf)
184                 i40iw_wr32(dev->hw, I40E_PFINT_DYN_CTLN(msix_id - 1), val);
185         else
186                 i40iw_wr32(dev->hw, I40E_VFINT_DYN_CTLN1(msix_id - 1), val);
187 }
188
189 /**
190  * i40iw_dpc - tasklet for aeq and ceq 0
191  * @data: iwarp device
192  */
193 static void i40iw_dpc(unsigned long data)
194 {
195         struct i40iw_device *iwdev = (struct i40iw_device *)data;
196
197         if (iwdev->msix_shared)
198                 i40iw_process_ceq(iwdev, iwdev->ceqlist);
199         i40iw_process_aeq(iwdev);
200         i40iw_enable_intr(&iwdev->sc_dev, iwdev->iw_msixtbl[0].idx);
201 }
202
203 /**
204  * i40iw_ceq_dpc - dpc handler for CEQ
205  * @data: data points to CEQ
206  */
207 static void i40iw_ceq_dpc(unsigned long data)
208 {
209         struct i40iw_ceq *iwceq = (struct i40iw_ceq *)data;
210         struct i40iw_device *iwdev = iwceq->iwdev;
211
212         i40iw_process_ceq(iwdev, iwceq);
213         i40iw_enable_intr(&iwdev->sc_dev, iwceq->msix_idx);
214 }
215
216 /**
217  * i40iw_irq_handler - interrupt handler for aeq and ceq0
218  * @irq: Interrupt request number
219  * @data: iwarp device
220  */
221 static irqreturn_t i40iw_irq_handler(int irq, void *data)
222 {
223         struct i40iw_device *iwdev = (struct i40iw_device *)data;
224
225         tasklet_schedule(&iwdev->dpc_tasklet);
226         return IRQ_HANDLED;
227 }
228
229 /**
230  * i40iw_destroy_cqp  - destroy control qp
231  * @iwdev: iwarp device
232  * @create_done: 1 if cqp create poll was success
233  *
234  * Issue destroy cqp request and
235  * free the resources associated with the cqp
236  */
237 static void i40iw_destroy_cqp(struct i40iw_device *iwdev, bool free_hwcqp)
238 {
239         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
240         struct i40iw_cqp *cqp = &iwdev->cqp;
241
242         if (free_hwcqp)
243                 dev->cqp_ops->cqp_destroy(dev->cqp);
244
245         i40iw_cleanup_pending_cqp_op(iwdev);
246
247         i40iw_free_dma_mem(dev->hw, &cqp->sq);
248         kfree(cqp->scratch_array);
249         iwdev->cqp.scratch_array = NULL;
250
251         kfree(cqp->cqp_requests);
252         cqp->cqp_requests = NULL;
253 }
254
255 /**
256  * i40iw_disable_irqs - disable device interrupts
257  * @dev: hardware control device structure
258  * @msic_vec: msix vector to disable irq
259  * @dev_id: parameter to pass to free_irq (used during irq setup)
260  *
261  * The function is called when destroying aeq/ceq
262  */
263 static void i40iw_disable_irq(struct i40iw_sc_dev *dev,
264                               struct i40iw_msix_vector *msix_vec,
265                               void *dev_id)
266 {
267         if (dev->is_pf)
268                 i40iw_wr32(dev->hw, I40E_PFINT_DYN_CTLN(msix_vec->idx - 1), 0);
269         else
270                 i40iw_wr32(dev->hw, I40E_VFINT_DYN_CTLN1(msix_vec->idx - 1), 0);
271         irq_set_affinity_hint(msix_vec->irq, NULL);
272         free_irq(msix_vec->irq, dev_id);
273 }
274
275 /**
276  * i40iw_destroy_aeq - destroy aeq
277  * @iwdev: iwarp device
278  *
279  * Issue a destroy aeq request and
280  * free the resources associated with the aeq
281  * The function is called during driver unload
282  */
283 static void i40iw_destroy_aeq(struct i40iw_device *iwdev)
284 {
285         enum i40iw_status_code status = I40IW_ERR_NOT_READY;
286         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
287         struct i40iw_aeq *aeq = &iwdev->aeq;
288
289         if (!iwdev->msix_shared)
290                 i40iw_disable_irq(dev, iwdev->iw_msixtbl, (void *)iwdev);
291         if (iwdev->reset)
292                 goto exit;
293
294         if (!dev->aeq_ops->aeq_destroy(&aeq->sc_aeq, 0, 1))
295                 status = dev->aeq_ops->aeq_destroy_done(&aeq->sc_aeq);
296         if (status)
297                 i40iw_pr_err("destroy aeq failed %d\n", status);
298
299 exit:
300         i40iw_free_dma_mem(dev->hw, &aeq->mem);
301 }
302
303 /**
304  * i40iw_destroy_ceq - destroy ceq
305  * @iwdev: iwarp device
306  * @iwceq: ceq to be destroyed
307  *
308  * Issue a destroy ceq request and
309  * free the resources associated with the ceq
310  */
311 static void i40iw_destroy_ceq(struct i40iw_device *iwdev,
312                               struct i40iw_ceq *iwceq)
313 {
314         enum i40iw_status_code status;
315         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
316
317         if (iwdev->reset)
318                 goto exit;
319
320         status = dev->ceq_ops->ceq_destroy(&iwceq->sc_ceq, 0, 1);
321         if (status) {
322                 i40iw_pr_err("ceq destroy command failed %d\n", status);
323                 goto exit;
324         }
325
326         status = dev->ceq_ops->cceq_destroy_done(&iwceq->sc_ceq);
327         if (status)
328                 i40iw_pr_err("ceq destroy completion failed %d\n", status);
329 exit:
330         i40iw_free_dma_mem(dev->hw, &iwceq->mem);
331 }
332
333 /**
334  * i40iw_dele_ceqs - destroy all ceq's
335  * @iwdev: iwarp device
336  *
337  * Go through all of the device ceq's and for each ceq
338  * disable the ceq interrupt and destroy the ceq
339  */
340 static void i40iw_dele_ceqs(struct i40iw_device *iwdev)
341 {
342         u32 i = 0;
343         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
344         struct i40iw_ceq *iwceq = iwdev->ceqlist;
345         struct i40iw_msix_vector *msix_vec = iwdev->iw_msixtbl;
346
347         if (iwdev->msix_shared) {
348                 i40iw_disable_irq(dev, msix_vec, (void *)iwdev);
349                 i40iw_destroy_ceq(iwdev, iwceq);
350                 iwceq++;
351                 i++;
352         }
353
354         for (msix_vec++; i < iwdev->ceqs_count; i++, msix_vec++, iwceq++) {
355                 i40iw_disable_irq(dev, msix_vec, (void *)iwceq);
356                 i40iw_destroy_ceq(iwdev, iwceq);
357         }
358 }
359
360 /**
361  * i40iw_destroy_ccq - destroy control cq
362  * @iwdev: iwarp device
363  *
364  * Issue destroy ccq request and
365  * free the resources associated with the ccq
366  */
367 static void i40iw_destroy_ccq(struct i40iw_device *iwdev)
368 {
369         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
370         struct i40iw_ccq *ccq = &iwdev->ccq;
371         enum i40iw_status_code status = 0;
372
373         if (!iwdev->reset)
374                 status = dev->ccq_ops->ccq_destroy(dev->ccq, 0, true);
375         if (status)
376                 i40iw_pr_err("ccq destroy failed %d\n", status);
377         i40iw_free_dma_mem(dev->hw, &ccq->mem_cq);
378 }
379
380 /* types of hmc objects */
381 static enum i40iw_hmc_rsrc_type iw_hmc_obj_types[] = {
382         I40IW_HMC_IW_QP,
383         I40IW_HMC_IW_CQ,
384         I40IW_HMC_IW_HTE,
385         I40IW_HMC_IW_ARP,
386         I40IW_HMC_IW_APBVT_ENTRY,
387         I40IW_HMC_IW_MR,
388         I40IW_HMC_IW_XF,
389         I40IW_HMC_IW_XFFL,
390         I40IW_HMC_IW_Q1,
391         I40IW_HMC_IW_Q1FL,
392         I40IW_HMC_IW_TIMER,
393 };
394
395 /**
396  * i40iw_close_hmc_objects_type - delete hmc objects of a given type
397  * @iwdev: iwarp device
398  * @obj_type: the hmc object type to be deleted
399  * @is_pf: true if the function is PF otherwise false
400  * @reset: true if called before reset
401  */
402 static void i40iw_close_hmc_objects_type(struct i40iw_sc_dev *dev,
403                                          enum i40iw_hmc_rsrc_type obj_type,
404                                          struct i40iw_hmc_info *hmc_info,
405                                          bool is_pf,
406                                          bool reset)
407 {
408         struct i40iw_hmc_del_obj_info info;
409
410         memset(&info, 0, sizeof(info));
411         info.hmc_info = hmc_info;
412         info.rsrc_type = obj_type;
413         info.count = hmc_info->hmc_obj[obj_type].cnt;
414         info.is_pf = is_pf;
415         if (dev->hmc_ops->del_hmc_object(dev, &info, reset))
416                 i40iw_pr_err("del obj of type %d failed\n", obj_type);
417 }
418
419 /**
420  * i40iw_del_hmc_objects - remove all device hmc objects
421  * @dev: iwarp device
422  * @hmc_info: hmc_info to free
423  * @is_pf: true if hmc_info belongs to PF, not vf nor allocated
424  *         by PF on behalf of VF
425  * @reset: true if called before reset
426  */
427 static void i40iw_del_hmc_objects(struct i40iw_sc_dev *dev,
428                                   struct i40iw_hmc_info *hmc_info,
429                                   bool is_pf,
430                                   bool reset)
431 {
432         unsigned int i;
433
434         for (i = 0; i < IW_HMC_OBJ_TYPE_NUM; i++)
435                 i40iw_close_hmc_objects_type(dev, iw_hmc_obj_types[i], hmc_info, is_pf, reset);
436 }
437
438 /**
439  * i40iw_ceq_handler - interrupt handler for ceq
440  * @data: ceq pointer
441  */
442 static irqreturn_t i40iw_ceq_handler(int irq, void *data)
443 {
444         struct i40iw_ceq *iwceq = (struct i40iw_ceq *)data;
445
446         if (iwceq->irq != irq)
447                 i40iw_pr_err("expected irq = %d received irq = %d\n", iwceq->irq, irq);
448         tasklet_schedule(&iwceq->dpc_tasklet);
449         return IRQ_HANDLED;
450 }
451
452 /**
453  * i40iw_create_hmc_obj_type - create hmc object of a given type
454  * @dev: hardware control device structure
455  * @info: information for the hmc object to create
456  */
457 static enum i40iw_status_code i40iw_create_hmc_obj_type(struct i40iw_sc_dev *dev,
458                                                         struct i40iw_hmc_create_obj_info *info)
459 {
460         return dev->hmc_ops->create_hmc_object(dev, info);
461 }
462
463 /**
464  * i40iw_create_hmc_objs - create all hmc objects for the device
465  * @iwdev: iwarp device
466  * @is_pf: true if the function is PF otherwise false
467  *
468  * Create the device hmc objects and allocate hmc pages
469  * Return 0 if successful, otherwise clean up and return error
470  */
471 static enum i40iw_status_code i40iw_create_hmc_objs(struct i40iw_device *iwdev,
472                                                     bool is_pf)
473 {
474         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
475         struct i40iw_hmc_create_obj_info info;
476         enum i40iw_status_code status;
477         int i;
478
479         memset(&info, 0, sizeof(info));
480         info.hmc_info = dev->hmc_info;
481         info.is_pf = is_pf;
482         info.entry_type = iwdev->sd_type;
483         for (i = 0; i < IW_HMC_OBJ_TYPE_NUM; i++) {
484                 info.rsrc_type = iw_hmc_obj_types[i];
485                 info.count = dev->hmc_info->hmc_obj[info.rsrc_type].cnt;
486                 status = i40iw_create_hmc_obj_type(dev, &info);
487                 if (status) {
488                         i40iw_pr_err("create obj type %d status = %d\n",
489                                      iw_hmc_obj_types[i], status);
490                         break;
491                 }
492         }
493         if (!status)
494                 return (dev->cqp_misc_ops->static_hmc_pages_allocated(dev->cqp, 0,
495                                                                       dev->hmc_fn_id,
496                                                                       true, true));
497
498         while (i) {
499                 i--;
500                 /* destroy the hmc objects of a given type */
501                 i40iw_close_hmc_objects_type(dev,
502                                              iw_hmc_obj_types[i],
503                                              dev->hmc_info,
504                                              is_pf,
505                                              false);
506         }
507         return status;
508 }
509
510 /**
511  * i40iw_obj_aligned_mem - get aligned memory from device allocated memory
512  * @iwdev: iwarp device
513  * @memptr: points to the memory addresses
514  * @size: size of memory needed
515  * @mask: mask for the aligned memory
516  *
517  * Get aligned memory of the requested size and
518  * update the memptr to point to the new aligned memory
519  * Return 0 if successful, otherwise return no memory error
520  */
521 enum i40iw_status_code i40iw_obj_aligned_mem(struct i40iw_device *iwdev,
522                                              struct i40iw_dma_mem *memptr,
523                                              u32 size,
524                                              u32 mask)
525 {
526         unsigned long va, newva;
527         unsigned long extra;
528
529         va = (unsigned long)iwdev->obj_next.va;
530         newva = va;
531         if (mask)
532                 newva = ALIGN(va, (mask + 1));
533         extra = newva - va;
534         memptr->va = (u8 *)va + extra;
535         memptr->pa = iwdev->obj_next.pa + extra;
536         memptr->size = size;
537         if ((memptr->va + size) > (iwdev->obj_mem.va + iwdev->obj_mem.size))
538                 return I40IW_ERR_NO_MEMORY;
539
540         iwdev->obj_next.va = memptr->va + size;
541         iwdev->obj_next.pa = memptr->pa + size;
542         return 0;
543 }
544
545 /**
546  * i40iw_create_cqp - create control qp
547  * @iwdev: iwarp device
548  *
549  * Return 0, if the cqp and all the resources associated with it
550  * are successfully created, otherwise return error
551  */
552 static enum i40iw_status_code i40iw_create_cqp(struct i40iw_device *iwdev)
553 {
554         enum i40iw_status_code status;
555         u32 sqsize = I40IW_CQP_SW_SQSIZE_2048;
556         struct i40iw_dma_mem mem;
557         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
558         struct i40iw_cqp_init_info cqp_init_info;
559         struct i40iw_cqp *cqp = &iwdev->cqp;
560         u16 maj_err, min_err;
561         int i;
562
563         cqp->cqp_requests = kcalloc(sqsize, sizeof(*cqp->cqp_requests), GFP_KERNEL);
564         if (!cqp->cqp_requests)
565                 return I40IW_ERR_NO_MEMORY;
566         cqp->scratch_array = kcalloc(sqsize, sizeof(*cqp->scratch_array), GFP_KERNEL);
567         if (!cqp->scratch_array) {
568                 kfree(cqp->cqp_requests);
569                 return I40IW_ERR_NO_MEMORY;
570         }
571         dev->cqp = &cqp->sc_cqp;
572         dev->cqp->dev = dev;
573         memset(&cqp_init_info, 0, sizeof(cqp_init_info));
574         status = i40iw_allocate_dma_mem(dev->hw, &cqp->sq,
575                                         (sizeof(struct i40iw_cqp_sq_wqe) * sqsize),
576                                         I40IW_CQP_ALIGNMENT);
577         if (status)
578                 goto exit;
579         status = i40iw_obj_aligned_mem(iwdev, &mem, sizeof(struct i40iw_cqp_ctx),
580                                        I40IW_HOST_CTX_ALIGNMENT_MASK);
581         if (status)
582                 goto exit;
583         dev->cqp->host_ctx_pa = mem.pa;
584         dev->cqp->host_ctx = mem.va;
585         /* populate the cqp init info */
586         cqp_init_info.dev = dev;
587         cqp_init_info.sq_size = sqsize;
588         cqp_init_info.sq = cqp->sq.va;
589         cqp_init_info.sq_pa = cqp->sq.pa;
590         cqp_init_info.host_ctx_pa = mem.pa;
591         cqp_init_info.host_ctx = mem.va;
592         cqp_init_info.hmc_profile = iwdev->resource_profile;
593         cqp_init_info.enabled_vf_count = iwdev->max_rdma_vfs;
594         cqp_init_info.scratch_array = cqp->scratch_array;
595         status = dev->cqp_ops->cqp_init(dev->cqp, &cqp_init_info);
596         if (status) {
597                 i40iw_pr_err("cqp init status %d\n", status);
598                 goto exit;
599         }
600         status = dev->cqp_ops->cqp_create(dev->cqp, &maj_err, &min_err);
601         if (status) {
602                 i40iw_pr_err("cqp create status %d maj_err %d min_err %d\n",
603                              status, maj_err, min_err);
604                 goto exit;
605         }
606         spin_lock_init(&cqp->req_lock);
607         INIT_LIST_HEAD(&cqp->cqp_avail_reqs);
608         INIT_LIST_HEAD(&cqp->cqp_pending_reqs);
609         /* init the waitq of the cqp_requests and add them to the list */
610         for (i = 0; i < I40IW_CQP_SW_SQSIZE_2048; i++) {
611                 init_waitqueue_head(&cqp->cqp_requests[i].waitq);
612                 list_add_tail(&cqp->cqp_requests[i].list, &cqp->cqp_avail_reqs);
613         }
614         return 0;
615 exit:
616         /* clean up the created resources */
617         i40iw_destroy_cqp(iwdev, false);
618         return status;
619 }
620
621 /**
622  * i40iw_create_ccq - create control cq
623  * @iwdev: iwarp device
624  *
625  * Return 0, if the ccq and the resources associated with it
626  * are successfully created, otherwise return error
627  */
628 static enum i40iw_status_code i40iw_create_ccq(struct i40iw_device *iwdev)
629 {
630         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
631         struct i40iw_dma_mem mem;
632         enum i40iw_status_code status;
633         struct i40iw_ccq_init_info info;
634         struct i40iw_ccq *ccq = &iwdev->ccq;
635
636         memset(&info, 0, sizeof(info));
637         dev->ccq = &ccq->sc_cq;
638         dev->ccq->dev = dev;
639         info.dev = dev;
640         ccq->shadow_area.size = sizeof(struct i40iw_cq_shadow_area);
641         ccq->mem_cq.size = sizeof(struct i40iw_cqe) * IW_CCQ_SIZE;
642         status = i40iw_allocate_dma_mem(dev->hw, &ccq->mem_cq,
643                                         ccq->mem_cq.size, I40IW_CQ0_ALIGNMENT);
644         if (status)
645                 goto exit;
646         status = i40iw_obj_aligned_mem(iwdev, &mem, ccq->shadow_area.size,
647                                        I40IW_SHADOWAREA_MASK);
648         if (status)
649                 goto exit;
650         ccq->sc_cq.back_cq = (void *)ccq;
651         /* populate the ccq init info */
652         info.cq_base = ccq->mem_cq.va;
653         info.cq_pa = ccq->mem_cq.pa;
654         info.num_elem = IW_CCQ_SIZE;
655         info.shadow_area = mem.va;
656         info.shadow_area_pa = mem.pa;
657         info.ceqe_mask = false;
658         info.ceq_id_valid = true;
659         info.shadow_read_threshold = 16;
660         status = dev->ccq_ops->ccq_init(dev->ccq, &info);
661         if (!status)
662                 status = dev->ccq_ops->ccq_create(dev->ccq, 0, true, true);
663 exit:
664         if (status)
665                 i40iw_free_dma_mem(dev->hw, &ccq->mem_cq);
666         return status;
667 }
668
669 /**
670  * i40iw_configure_ceq_vector - set up the msix interrupt vector for ceq
671  * @iwdev: iwarp device
672  * @msix_vec: interrupt vector information
673  * @iwceq: ceq associated with the vector
674  * @ceq_id: the id number of the iwceq
675  *
676  * Allocate interrupt resources and enable irq handling
677  * Return 0 if successful, otherwise return error
678  */
679 static enum i40iw_status_code i40iw_configure_ceq_vector(struct i40iw_device *iwdev,
680                                                          struct i40iw_ceq *iwceq,
681                                                          u32 ceq_id,
682                                                          struct i40iw_msix_vector *msix_vec)
683 {
684         enum i40iw_status_code status;
685         cpumask_t mask;
686
687         if (iwdev->msix_shared && !ceq_id) {
688                 tasklet_init(&iwdev->dpc_tasklet, i40iw_dpc, (unsigned long)iwdev);
689                 status = request_irq(msix_vec->irq, i40iw_irq_handler, 0, "AEQCEQ", iwdev);
690         } else {
691                 tasklet_init(&iwceq->dpc_tasklet, i40iw_ceq_dpc, (unsigned long)iwceq);
692                 status = request_irq(msix_vec->irq, i40iw_ceq_handler, 0, "CEQ", iwceq);
693         }
694
695         cpumask_clear(&mask);
696         cpumask_set_cpu(msix_vec->cpu_affinity, &mask);
697         irq_set_affinity_hint(msix_vec->irq, &mask);
698
699         if (status) {
700                 i40iw_pr_err("ceq irq config fail\n");
701                 return I40IW_ERR_CONFIG;
702         }
703         msix_vec->ceq_id = ceq_id;
704
705         return 0;
706 }
707
708 /**
709  * i40iw_create_ceq - create completion event queue
710  * @iwdev: iwarp device
711  * @iwceq: pointer to the ceq resources to be created
712  * @ceq_id: the id number of the iwceq
713  *
714  * Return 0, if the ceq and the resources associated with it
715  * are successfully created, otherwise return error
716  */
717 static enum i40iw_status_code i40iw_create_ceq(struct i40iw_device *iwdev,
718                                                struct i40iw_ceq *iwceq,
719                                                u32 ceq_id)
720 {
721         enum i40iw_status_code status;
722         struct i40iw_ceq_init_info info;
723         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
724         u64 scratch;
725
726         memset(&info, 0, sizeof(info));
727         info.ceq_id = ceq_id;
728         iwceq->iwdev = iwdev;
729         iwceq->mem.size = sizeof(struct i40iw_ceqe) *
730                 iwdev->sc_dev.hmc_info->hmc_obj[I40IW_HMC_IW_CQ].cnt;
731         status = i40iw_allocate_dma_mem(dev->hw, &iwceq->mem, iwceq->mem.size,
732                                         I40IW_CEQ_ALIGNMENT);
733         if (status)
734                 goto exit;
735         info.ceq_id = ceq_id;
736         info.ceqe_base = iwceq->mem.va;
737         info.ceqe_pa = iwceq->mem.pa;
738
739         info.elem_cnt = iwdev->sc_dev.hmc_info->hmc_obj[I40IW_HMC_IW_CQ].cnt;
740         iwceq->sc_ceq.ceq_id = ceq_id;
741         info.dev = dev;
742         scratch = (uintptr_t)&iwdev->cqp.sc_cqp;
743         status = dev->ceq_ops->ceq_init(&iwceq->sc_ceq, &info);
744         if (!status)
745                 status = dev->ceq_ops->cceq_create(&iwceq->sc_ceq, scratch);
746
747 exit:
748         if (status)
749                 i40iw_free_dma_mem(dev->hw, &iwceq->mem);
750         return status;
751 }
752
753 void i40iw_request_reset(struct i40iw_device *iwdev)
754 {
755         struct i40e_info *ldev = iwdev->ldev;
756
757         ldev->ops->request_reset(ldev, iwdev->client, 1);
758 }
759
760 /**
761  * i40iw_setup_ceqs - manage the device ceq's and their interrupt resources
762  * @iwdev: iwarp device
763  * @ldev: i40e lan device
764  *
765  * Allocate a list for all device completion event queues
766  * Create the ceq's and configure their msix interrupt vectors
767  * Return 0, if at least one ceq is successfully set up, otherwise return error
768  */
769 static enum i40iw_status_code i40iw_setup_ceqs(struct i40iw_device *iwdev,
770                                                struct i40e_info *ldev)
771 {
772         u32 i;
773         u32 ceq_id;
774         struct i40iw_ceq *iwceq;
775         struct i40iw_msix_vector *msix_vec;
776         enum i40iw_status_code status = 0;
777         u32 num_ceqs;
778
779         if (ldev && ldev->ops && ldev->ops->setup_qvlist) {
780                 status = ldev->ops->setup_qvlist(ldev, &i40iw_client,
781                                                  iwdev->iw_qvlist);
782                 if (status)
783                         goto exit;
784         } else {
785                 status = I40IW_ERR_BAD_PTR;
786                 goto exit;
787         }
788
789         num_ceqs = min(iwdev->msix_count, iwdev->sc_dev.hmc_fpm_misc.max_ceqs);
790         iwdev->ceqlist = kcalloc(num_ceqs, sizeof(*iwdev->ceqlist), GFP_KERNEL);
791         if (!iwdev->ceqlist) {
792                 status = I40IW_ERR_NO_MEMORY;
793                 goto exit;
794         }
795         i = (iwdev->msix_shared) ? 0 : 1;
796         for (ceq_id = 0; i < num_ceqs; i++, ceq_id++) {
797                 iwceq = &iwdev->ceqlist[ceq_id];
798                 status = i40iw_create_ceq(iwdev, iwceq, ceq_id);
799                 if (status) {
800                         i40iw_pr_err("create ceq status = %d\n", status);
801                         break;
802                 }
803
804                 msix_vec = &iwdev->iw_msixtbl[i];
805                 iwceq->irq = msix_vec->irq;
806                 iwceq->msix_idx = msix_vec->idx;
807                 status = i40iw_configure_ceq_vector(iwdev, iwceq, ceq_id, msix_vec);
808                 if (status) {
809                         i40iw_destroy_ceq(iwdev, iwceq);
810                         break;
811                 }
812                 i40iw_enable_intr(&iwdev->sc_dev, msix_vec->idx);
813                 iwdev->ceqs_count++;
814         }
815
816 exit:
817         if (status) {
818                 if (!iwdev->ceqs_count) {
819                         kfree(iwdev->ceqlist);
820                         iwdev->ceqlist = NULL;
821                 } else {
822                         status = 0;
823                 }
824         }
825         return status;
826 }
827
828 /**
829  * i40iw_configure_aeq_vector - set up the msix vector for aeq
830  * @iwdev: iwarp device
831  *
832  * Allocate interrupt resources and enable irq handling
833  * Return 0 if successful, otherwise return error
834  */
835 static enum i40iw_status_code i40iw_configure_aeq_vector(struct i40iw_device *iwdev)
836 {
837         struct i40iw_msix_vector *msix_vec = iwdev->iw_msixtbl;
838         u32 ret = 0;
839
840         if (!iwdev->msix_shared) {
841                 tasklet_init(&iwdev->dpc_tasklet, i40iw_dpc, (unsigned long)iwdev);
842                 ret = request_irq(msix_vec->irq, i40iw_irq_handler, 0, "i40iw", iwdev);
843         }
844         if (ret) {
845                 i40iw_pr_err("aeq irq config fail\n");
846                 return I40IW_ERR_CONFIG;
847         }
848
849         return 0;
850 }
851
852 /**
853  * i40iw_create_aeq - create async event queue
854  * @iwdev: iwarp device
855  *
856  * Return 0, if the aeq and the resources associated with it
857  * are successfully created, otherwise return error
858  */
859 static enum i40iw_status_code i40iw_create_aeq(struct i40iw_device *iwdev)
860 {
861         enum i40iw_status_code status;
862         struct i40iw_aeq_init_info info;
863         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
864         struct i40iw_aeq *aeq = &iwdev->aeq;
865         u64 scratch = 0;
866         u32 aeq_size;
867
868         aeq_size = 2 * iwdev->sc_dev.hmc_info->hmc_obj[I40IW_HMC_IW_QP].cnt +
869                 iwdev->sc_dev.hmc_info->hmc_obj[I40IW_HMC_IW_CQ].cnt;
870         memset(&info, 0, sizeof(info));
871         aeq->mem.size = sizeof(struct i40iw_sc_aeqe) * aeq_size;
872         status = i40iw_allocate_dma_mem(dev->hw, &aeq->mem, aeq->mem.size,
873                                         I40IW_AEQ_ALIGNMENT);
874         if (status)
875                 goto exit;
876
877         info.aeqe_base = aeq->mem.va;
878         info.aeq_elem_pa = aeq->mem.pa;
879         info.elem_cnt = aeq_size;
880         info.dev = dev;
881         status = dev->aeq_ops->aeq_init(&aeq->sc_aeq, &info);
882         if (status)
883                 goto exit;
884         status = dev->aeq_ops->aeq_create(&aeq->sc_aeq, scratch, 1);
885         if (!status)
886                 status = dev->aeq_ops->aeq_create_done(&aeq->sc_aeq);
887 exit:
888         if (status)
889                 i40iw_free_dma_mem(dev->hw, &aeq->mem);
890         return status;
891 }
892
893 /**
894  * i40iw_setup_aeq - set up the device aeq
895  * @iwdev: iwarp device
896  *
897  * Create the aeq and configure its msix interrupt vector
898  * Return 0 if successful, otherwise return error
899  */
900 static enum i40iw_status_code i40iw_setup_aeq(struct i40iw_device *iwdev)
901 {
902         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
903         enum i40iw_status_code status;
904
905         status = i40iw_create_aeq(iwdev);
906         if (status)
907                 return status;
908
909         status = i40iw_configure_aeq_vector(iwdev);
910         if (status) {
911                 i40iw_destroy_aeq(iwdev);
912                 return status;
913         }
914
915         if (!iwdev->msix_shared)
916                 i40iw_enable_intr(dev, iwdev->iw_msixtbl[0].idx);
917         return 0;
918 }
919
920 /**
921  * i40iw_initialize_ilq - create iwarp local queue for cm
922  * @iwdev: iwarp device
923  *
924  * Return 0 if successful, otherwise return error
925  */
926 static enum i40iw_status_code i40iw_initialize_ilq(struct i40iw_device *iwdev)
927 {
928         struct i40iw_puda_rsrc_info info;
929         enum i40iw_status_code status;
930
931         memset(&info, 0, sizeof(info));
932         info.type = I40IW_PUDA_RSRC_TYPE_ILQ;
933         info.cq_id = 1;
934         info.qp_id = 0;
935         info.count = 1;
936         info.pd_id = 1;
937         info.sq_size = 8192;
938         info.rq_size = 8192;
939         info.buf_size = 1024;
940         info.tx_buf_cnt = 16384;
941         info.receive = i40iw_receive_ilq;
942         info.xmit_complete = i40iw_free_sqbuf;
943         status = i40iw_puda_create_rsrc(&iwdev->vsi, &info);
944         if (status)
945                 i40iw_pr_err("ilq create fail\n");
946         return status;
947 }
948
949 /**
950  * i40iw_initialize_ieq - create iwarp exception queue
951  * @iwdev: iwarp device
952  *
953  * Return 0 if successful, otherwise return error
954  */
955 static enum i40iw_status_code i40iw_initialize_ieq(struct i40iw_device *iwdev)
956 {
957         struct i40iw_puda_rsrc_info info;
958         enum i40iw_status_code status;
959
960         memset(&info, 0, sizeof(info));
961         info.type = I40IW_PUDA_RSRC_TYPE_IEQ;
962         info.cq_id = 2;
963         info.qp_id = iwdev->sc_dev.exception_lan_queue;
964         info.count = 1;
965         info.pd_id = 2;
966         info.sq_size = 8192;
967         info.rq_size = 8192;
968         info.buf_size = 2048;
969         info.tx_buf_cnt = 16384;
970         status = i40iw_puda_create_rsrc(&iwdev->vsi, &info);
971         if (status)
972                 i40iw_pr_err("ieq create fail\n");
973         return status;
974 }
975
976 /**
977  * i40iw_hmc_setup - create hmc objects for the device
978  * @iwdev: iwarp device
979  *
980  * Set up the device private memory space for the number and size of
981  * the hmc objects and create the objects
982  * Return 0 if successful, otherwise return error
983  */
984 static enum i40iw_status_code i40iw_hmc_setup(struct i40iw_device *iwdev)
985 {
986         enum i40iw_status_code status;
987
988         iwdev->sd_type = I40IW_SD_TYPE_DIRECT;
989         status = i40iw_config_fpm_values(&iwdev->sc_dev, IW_CFG_FPM_QP_COUNT);
990         if (status)
991                 goto exit;
992         status = i40iw_create_hmc_objs(iwdev, true);
993         if (status)
994                 goto exit;
995         iwdev->init_state = HMC_OBJS_CREATED;
996 exit:
997         return status;
998 }
999
1000 /**
1001  * i40iw_del_init_mem - deallocate memory resources
1002  * @iwdev: iwarp device
1003  */
1004 static void i40iw_del_init_mem(struct i40iw_device *iwdev)
1005 {
1006         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
1007
1008         i40iw_free_dma_mem(&iwdev->hw, &iwdev->obj_mem);
1009         kfree(dev->hmc_info->sd_table.sd_entry);
1010         dev->hmc_info->sd_table.sd_entry = NULL;
1011         kfree(iwdev->mem_resources);
1012         iwdev->mem_resources = NULL;
1013         kfree(iwdev->ceqlist);
1014         iwdev->ceqlist = NULL;
1015         kfree(iwdev->iw_msixtbl);
1016         iwdev->iw_msixtbl = NULL;
1017         kfree(iwdev->hmc_info_mem);
1018         iwdev->hmc_info_mem = NULL;
1019 }
1020
1021 /**
1022  * i40iw_del_macip_entry - remove a mac ip address entry from the hw table
1023  * @iwdev: iwarp device
1024  * @idx: the index of the mac ip address to delete
1025  */
1026 static void i40iw_del_macip_entry(struct i40iw_device *iwdev, u8 idx)
1027 {
1028         struct i40iw_cqp *iwcqp = &iwdev->cqp;
1029         struct i40iw_cqp_request *cqp_request;
1030         struct cqp_commands_info *cqp_info;
1031         enum i40iw_status_code status = 0;
1032
1033         cqp_request = i40iw_get_cqp_request(iwcqp, true);
1034         if (!cqp_request) {
1035                 i40iw_pr_err("cqp_request memory failed\n");
1036                 return;
1037         }
1038         cqp_info = &cqp_request->info;
1039         cqp_info->cqp_cmd = OP_DELETE_LOCAL_MAC_IPADDR_ENTRY;
1040         cqp_info->post_sq = 1;
1041         cqp_info->in.u.del_local_mac_ipaddr_entry.cqp = &iwcqp->sc_cqp;
1042         cqp_info->in.u.del_local_mac_ipaddr_entry.scratch = (uintptr_t)cqp_request;
1043         cqp_info->in.u.del_local_mac_ipaddr_entry.entry_idx = idx;
1044         cqp_info->in.u.del_local_mac_ipaddr_entry.ignore_ref_count = 0;
1045         status = i40iw_handle_cqp_op(iwdev, cqp_request);
1046         if (status)
1047                 i40iw_pr_err("CQP-OP Del MAC Ip entry fail");
1048 }
1049
1050 /**
1051  * i40iw_add_mac_ipaddr_entry - add a mac ip address entry to the hw table
1052  * @iwdev: iwarp device
1053  * @mac_addr: pointer to mac address
1054  * @idx: the index of the mac ip address to add
1055  */
1056 static enum i40iw_status_code i40iw_add_mac_ipaddr_entry(struct i40iw_device *iwdev,
1057                                                          u8 *mac_addr,
1058                                                          u8 idx)
1059 {
1060         struct i40iw_local_mac_ipaddr_entry_info *info;
1061         struct i40iw_cqp *iwcqp = &iwdev->cqp;
1062         struct i40iw_cqp_request *cqp_request;
1063         struct cqp_commands_info *cqp_info;
1064         enum i40iw_status_code status = 0;
1065
1066         cqp_request = i40iw_get_cqp_request(iwcqp, true);
1067         if (!cqp_request) {
1068                 i40iw_pr_err("cqp_request memory failed\n");
1069                 return I40IW_ERR_NO_MEMORY;
1070         }
1071
1072         cqp_info = &cqp_request->info;
1073
1074         cqp_info->post_sq = 1;
1075         info = &cqp_info->in.u.add_local_mac_ipaddr_entry.info;
1076         ether_addr_copy(info->mac_addr, mac_addr);
1077         info->entry_idx = idx;
1078         cqp_info->in.u.add_local_mac_ipaddr_entry.scratch = (uintptr_t)cqp_request;
1079         cqp_info->cqp_cmd = OP_ADD_LOCAL_MAC_IPADDR_ENTRY;
1080         cqp_info->in.u.add_local_mac_ipaddr_entry.cqp = &iwcqp->sc_cqp;
1081         cqp_info->in.u.add_local_mac_ipaddr_entry.scratch = (uintptr_t)cqp_request;
1082         status = i40iw_handle_cqp_op(iwdev, cqp_request);
1083         if (status)
1084                 i40iw_pr_err("CQP-OP Add MAC Ip entry fail");
1085         return status;
1086 }
1087
1088 /**
1089  * i40iw_alloc_local_mac_ipaddr_entry - allocate a mac ip address entry
1090  * @iwdev: iwarp device
1091  * @mac_ip_tbl_idx: the index of the new mac ip address
1092  *
1093  * Allocate a mac ip address entry and update the mac_ip_tbl_idx
1094  * to hold the index of the newly created mac ip address
1095  * Return 0 if successful, otherwise return error
1096  */
1097 static enum i40iw_status_code i40iw_alloc_local_mac_ipaddr_entry(struct i40iw_device *iwdev,
1098                                                                  u16 *mac_ip_tbl_idx)
1099 {
1100         struct i40iw_cqp *iwcqp = &iwdev->cqp;
1101         struct i40iw_cqp_request *cqp_request;
1102         struct cqp_commands_info *cqp_info;
1103         enum i40iw_status_code status = 0;
1104
1105         cqp_request = i40iw_get_cqp_request(iwcqp, true);
1106         if (!cqp_request) {
1107                 i40iw_pr_err("cqp_request memory failed\n");
1108                 return I40IW_ERR_NO_MEMORY;
1109         }
1110
1111         /* increment refcount, because we need the cqp request ret value */
1112         atomic_inc(&cqp_request->refcount);
1113
1114         cqp_info = &cqp_request->info;
1115         cqp_info->cqp_cmd = OP_ALLOC_LOCAL_MAC_IPADDR_ENTRY;
1116         cqp_info->post_sq = 1;
1117         cqp_info->in.u.alloc_local_mac_ipaddr_entry.cqp = &iwcqp->sc_cqp;
1118         cqp_info->in.u.alloc_local_mac_ipaddr_entry.scratch = (uintptr_t)cqp_request;
1119         status = i40iw_handle_cqp_op(iwdev, cqp_request);
1120         if (!status)
1121                 *mac_ip_tbl_idx = cqp_request->compl_info.op_ret_val;
1122         else
1123                 i40iw_pr_err("CQP-OP Alloc MAC Ip entry fail");
1124         /* decrement refcount and free the cqp request, if no longer used */
1125         i40iw_put_cqp_request(iwcqp, cqp_request);
1126         return status;
1127 }
1128
1129 /**
1130  * i40iw_alloc_set_mac_ipaddr - set up a mac ip address table entry
1131  * @iwdev: iwarp device
1132  * @macaddr: pointer to mac address
1133  *
1134  * Allocate a mac ip address entry and add it to the hw table
1135  * Return 0 if successful, otherwise return error
1136  */
1137 static enum i40iw_status_code i40iw_alloc_set_mac_ipaddr(struct i40iw_device *iwdev,
1138                                                          u8 *macaddr)
1139 {
1140         enum i40iw_status_code status;
1141
1142         status = i40iw_alloc_local_mac_ipaddr_entry(iwdev, &iwdev->mac_ip_table_idx);
1143         if (!status) {
1144                 status = i40iw_add_mac_ipaddr_entry(iwdev, macaddr,
1145                                                     (u8)iwdev->mac_ip_table_idx);
1146                 if (status)
1147                         i40iw_del_macip_entry(iwdev, (u8)iwdev->mac_ip_table_idx);
1148         }
1149         return status;
1150 }
1151
1152 /**
1153  * i40iw_add_ipv6_addr - add ipv6 address to the hw arp table
1154  * @iwdev: iwarp device
1155  */
1156 static void i40iw_add_ipv6_addr(struct i40iw_device *iwdev)
1157 {
1158         struct net_device *ip_dev;
1159         struct inet6_dev *idev;
1160         struct inet6_ifaddr *ifp, *tmp;
1161         u32 local_ipaddr6[4];
1162
1163         rcu_read_lock();
1164         for_each_netdev_rcu(&init_net, ip_dev) {
1165                 if ((((rdma_vlan_dev_vlan_id(ip_dev) < 0xFFFF) &&
1166                       (rdma_vlan_dev_real_dev(ip_dev) == iwdev->netdev)) ||
1167                      (ip_dev == iwdev->netdev)) && (ip_dev->flags & IFF_UP)) {
1168                         idev = __in6_dev_get(ip_dev);
1169                         if (!idev) {
1170                                 i40iw_pr_err("ipv6 inet device not found\n");
1171                                 break;
1172                         }
1173                         list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
1174                                 i40iw_pr_info("IP=%pI6, vlan_id=%d, MAC=%pM\n", &ifp->addr,
1175                                               rdma_vlan_dev_vlan_id(ip_dev), ip_dev->dev_addr);
1176                                 i40iw_copy_ip_ntohl(local_ipaddr6,
1177                                                     ifp->addr.in6_u.u6_addr32);
1178                                 i40iw_manage_arp_cache(iwdev,
1179                                                        ip_dev->dev_addr,
1180                                                        local_ipaddr6,
1181                                                        false,
1182                                                        I40IW_ARP_ADD);
1183                         }
1184                 }
1185         }
1186         rcu_read_unlock();
1187 }
1188
1189 /**
1190  * i40iw_add_ipv4_addr - add ipv4 address to the hw arp table
1191  * @iwdev: iwarp device
1192  */
1193 static void i40iw_add_ipv4_addr(struct i40iw_device *iwdev)
1194 {
1195         struct net_device *dev;
1196         struct in_device *idev;
1197         bool got_lock = true;
1198         u32 ip_addr;
1199
1200         if (!rtnl_trylock())
1201                 got_lock = false;
1202
1203         for_each_netdev(&init_net, dev) {
1204                 if ((((rdma_vlan_dev_vlan_id(dev) < 0xFFFF) &&
1205                       (rdma_vlan_dev_real_dev(dev) == iwdev->netdev)) ||
1206                     (dev == iwdev->netdev)) && (dev->flags & IFF_UP)) {
1207                         idev = in_dev_get(dev);
1208                         for_ifa(idev) {
1209                                 i40iw_debug(&iwdev->sc_dev, I40IW_DEBUG_CM,
1210                                             "IP=%pI4, vlan_id=%d, MAC=%pM\n", &ifa->ifa_address,
1211                                              rdma_vlan_dev_vlan_id(dev), dev->dev_addr);
1212
1213                                 ip_addr = ntohl(ifa->ifa_address);
1214                                 i40iw_manage_arp_cache(iwdev,
1215                                                        dev->dev_addr,
1216                                                        &ip_addr,
1217                                                        true,
1218                                                        I40IW_ARP_ADD);
1219                         }
1220                         endfor_ifa(idev);
1221                         in_dev_put(idev);
1222                 }
1223         }
1224         if (got_lock)
1225                 rtnl_unlock();
1226 }
1227
1228 /**
1229  * i40iw_add_mac_ip - add mac and ip addresses
1230  * @iwdev: iwarp device
1231  *
1232  * Create and add a mac ip address entry to the hw table and
1233  * ipv4/ipv6 addresses to the arp cache
1234  * Return 0 if successful, otherwise return error
1235  */
1236 static enum i40iw_status_code i40iw_add_mac_ip(struct i40iw_device *iwdev)
1237 {
1238         struct net_device *netdev = iwdev->netdev;
1239         enum i40iw_status_code status;
1240
1241         status = i40iw_alloc_set_mac_ipaddr(iwdev, (u8 *)netdev->dev_addr);
1242         if (status)
1243                 return status;
1244         i40iw_add_ipv4_addr(iwdev);
1245         i40iw_add_ipv6_addr(iwdev);
1246         return 0;
1247 }
1248
1249 /**
1250  * i40iw_wait_pe_ready - Check if firmware is ready
1251  * @hw: provides access to registers
1252  */
1253 static void i40iw_wait_pe_ready(struct i40iw_hw *hw)
1254 {
1255         u32 statusfw;
1256         u32 statuscpu0;
1257         u32 statuscpu1;
1258         u32 statuscpu2;
1259         u32 retrycount = 0;
1260
1261         do {
1262                 statusfw = i40iw_rd32(hw, I40E_GLPE_FWLDSTATUS);
1263                 i40iw_pr_info("[%04d] fm load status[x%04X]\n", __LINE__, statusfw);
1264                 statuscpu0 = i40iw_rd32(hw, I40E_GLPE_CPUSTATUS0);
1265                 i40iw_pr_info("[%04d] CSR_CQP status[x%04X]\n", __LINE__, statuscpu0);
1266                 statuscpu1 = i40iw_rd32(hw, I40E_GLPE_CPUSTATUS1);
1267                 i40iw_pr_info("[%04d] I40E_GLPE_CPUSTATUS1 status[x%04X]\n",
1268                               __LINE__, statuscpu1);
1269                 statuscpu2 = i40iw_rd32(hw, I40E_GLPE_CPUSTATUS2);
1270                 i40iw_pr_info("[%04d] I40E_GLPE_CPUSTATUS2 status[x%04X]\n",
1271                               __LINE__, statuscpu2);
1272                 if ((statuscpu0 == 0x80) && (statuscpu1 == 0x80) && (statuscpu2 == 0x80))
1273                         break;  /* SUCCESS */
1274                 mdelay(1000);
1275                 retrycount++;
1276         } while (retrycount < 14);
1277         i40iw_wr32(hw, 0xb4040, 0x4C104C5);
1278 }
1279
1280 /**
1281  * i40iw_initialize_dev - initialize device
1282  * @iwdev: iwarp device
1283  * @ldev: lan device information
1284  *
1285  * Allocate memory for the hmc objects and initialize iwdev
1286  * Return 0 if successful, otherwise clean up the resources
1287  * and return error
1288  */
1289 static enum i40iw_status_code i40iw_initialize_dev(struct i40iw_device *iwdev,
1290                                                    struct i40e_info *ldev)
1291 {
1292         enum i40iw_status_code status;
1293         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
1294         struct i40iw_device_init_info info;
1295         struct i40iw_vsi_init_info vsi_info;
1296         struct i40iw_dma_mem mem;
1297         struct i40iw_l2params l2params;
1298         u32 size;
1299         struct i40iw_vsi_stats_info stats_info;
1300         u16 last_qset = I40IW_NO_QSET;
1301         u16 qset;
1302         u32 i;
1303
1304         memset(&l2params, 0, sizeof(l2params));
1305         memset(&info, 0, sizeof(info));
1306         size = sizeof(struct i40iw_hmc_pble_rsrc) + sizeof(struct i40iw_hmc_info) +
1307                                 (sizeof(struct i40iw_hmc_obj_info) * I40IW_HMC_IW_MAX);
1308         iwdev->hmc_info_mem = kzalloc(size, GFP_KERNEL);
1309         if (!iwdev->hmc_info_mem)
1310                 return I40IW_ERR_NO_MEMORY;
1311
1312         iwdev->pble_rsrc = (struct i40iw_hmc_pble_rsrc *)iwdev->hmc_info_mem;
1313         dev->hmc_info = &iwdev->hw.hmc;
1314         dev->hmc_info->hmc_obj = (struct i40iw_hmc_obj_info *)(iwdev->pble_rsrc + 1);
1315         status = i40iw_obj_aligned_mem(iwdev, &mem, I40IW_QUERY_FPM_BUF_SIZE,
1316                                        I40IW_FPM_QUERY_BUF_ALIGNMENT_MASK);
1317         if (status)
1318                 goto error;
1319         info.fpm_query_buf_pa = mem.pa;
1320         info.fpm_query_buf = mem.va;
1321         status = i40iw_obj_aligned_mem(iwdev, &mem, I40IW_COMMIT_FPM_BUF_SIZE,
1322                                        I40IW_FPM_COMMIT_BUF_ALIGNMENT_MASK);
1323         if (status)
1324                 goto error;
1325         info.fpm_commit_buf_pa = mem.pa;
1326         info.fpm_commit_buf = mem.va;
1327         info.hmc_fn_id = ldev->fid;
1328         info.is_pf = (ldev->ftype) ? false : true;
1329         info.bar0 = ldev->hw_addr;
1330         info.hw = &iwdev->hw;
1331         info.debug_mask = debug;
1332         l2params.mss =
1333                 (ldev->params.mtu) ? ldev->params.mtu - I40IW_MTU_TO_MSS : I40IW_DEFAULT_MSS;
1334         for (i = 0; i < I40E_CLIENT_MAX_USER_PRIORITY; i++) {
1335                 qset = ldev->params.qos.prio_qos[i].qs_handle;
1336                 l2params.qs_handle_list[i] = qset;
1337                 if (last_qset == I40IW_NO_QSET)
1338                         last_qset = qset;
1339                 else if ((qset != last_qset) && (qset != I40IW_NO_QSET))
1340                         iwdev->dcb = true;
1341         }
1342         i40iw_pr_info("DCB is set/clear = %d\n", iwdev->dcb);
1343         info.exception_lan_queue = 1;
1344         info.vchnl_send = i40iw_virtchnl_send;
1345         status = i40iw_device_init(&iwdev->sc_dev, &info);
1346
1347         if (status)
1348                 goto error;
1349         memset(&vsi_info, 0, sizeof(vsi_info));
1350         vsi_info.dev = &iwdev->sc_dev;
1351         vsi_info.back_vsi = (void *)iwdev;
1352         vsi_info.params = &l2params;
1353         i40iw_sc_vsi_init(&iwdev->vsi, &vsi_info);
1354
1355         if (dev->is_pf) {
1356                 memset(&stats_info, 0, sizeof(stats_info));
1357                 stats_info.fcn_id = ldev->fid;
1358                 stats_info.pestat = kzalloc(sizeof(*stats_info.pestat), GFP_KERNEL);
1359                 if (!stats_info.pestat) {
1360                         status = I40IW_ERR_NO_MEMORY;
1361                         goto error;
1362                 }
1363                 stats_info.stats_initialize = true;
1364                 if (stats_info.pestat)
1365                         i40iw_vsi_stats_init(&iwdev->vsi, &stats_info);
1366         }
1367         return status;
1368 error:
1369         kfree(iwdev->hmc_info_mem);
1370         iwdev->hmc_info_mem = NULL;
1371         return status;
1372 }
1373
1374 /**
1375  * i40iw_register_notifiers - register tcp ip notifiers
1376  */
1377 static void i40iw_register_notifiers(void)
1378 {
1379         if (atomic_inc_return(&i40iw_notifiers_registered) == 1) {
1380                 register_inetaddr_notifier(&i40iw_inetaddr_notifier);
1381                 register_inet6addr_notifier(&i40iw_inetaddr6_notifier);
1382                 register_netevent_notifier(&i40iw_net_notifier);
1383         }
1384 }
1385
1386 /**
1387  * i40iw_save_msix_info - copy msix vector information to iwarp device
1388  * @iwdev: iwarp device
1389  * @ldev: lan device information
1390  *
1391  * Allocate iwdev msix table and copy the ldev msix info to the table
1392  * Return 0 if successful, otherwise return error
1393  */
1394 static enum i40iw_status_code i40iw_save_msix_info(struct i40iw_device *iwdev,
1395                                                    struct i40e_info *ldev)
1396 {
1397         struct i40e_qvlist_info *iw_qvlist;
1398         struct i40e_qv_info *iw_qvinfo;
1399         u32 ceq_idx;
1400         u32 i;
1401         u32 size;
1402
1403         iwdev->msix_count = ldev->msix_count;
1404
1405         size = sizeof(struct i40iw_msix_vector) * iwdev->msix_count;
1406         size += sizeof(struct i40e_qvlist_info);
1407         size +=  sizeof(struct i40e_qv_info) * iwdev->msix_count - 1;
1408         iwdev->iw_msixtbl = kzalloc(size, GFP_KERNEL);
1409
1410         if (!iwdev->iw_msixtbl)
1411                 return I40IW_ERR_NO_MEMORY;
1412         iwdev->iw_qvlist = (struct i40e_qvlist_info *)(&iwdev->iw_msixtbl[iwdev->msix_count]);
1413         iw_qvlist = iwdev->iw_qvlist;
1414         iw_qvinfo = iw_qvlist->qv_info;
1415         iw_qvlist->num_vectors = iwdev->msix_count;
1416         if (iwdev->msix_count <= num_online_cpus())
1417                 iwdev->msix_shared = true;
1418         for (i = 0, ceq_idx = 0; i < iwdev->msix_count; i++, iw_qvinfo++) {
1419                 iwdev->iw_msixtbl[i].idx = ldev->msix_entries[i].entry;
1420                 iwdev->iw_msixtbl[i].irq = ldev->msix_entries[i].vector;
1421                 iwdev->iw_msixtbl[i].cpu_affinity = ceq_idx;
1422                 if (i == 0) {
1423                         iw_qvinfo->aeq_idx = 0;
1424                         if (iwdev->msix_shared)
1425                                 iw_qvinfo->ceq_idx = ceq_idx++;
1426                         else
1427                                 iw_qvinfo->ceq_idx = I40E_QUEUE_INVALID_IDX;
1428                 } else {
1429                         iw_qvinfo->aeq_idx = I40E_QUEUE_INVALID_IDX;
1430                         iw_qvinfo->ceq_idx = ceq_idx++;
1431                 }
1432                 iw_qvinfo->itr_idx = 3;
1433                 iw_qvinfo->v_idx = iwdev->iw_msixtbl[i].idx;
1434         }
1435         return 0;
1436 }
1437
1438 /**
1439  * i40iw_deinit_device - clean up the device resources
1440  * @iwdev: iwarp device
1441  *
1442  * Destroy the ib device interface, remove the mac ip entry and ipv4/ipv6 addresses,
1443  * destroy the device queues and free the pble and the hmc objects
1444  */
1445 static void i40iw_deinit_device(struct i40iw_device *iwdev)
1446 {
1447         struct i40e_info *ldev = iwdev->ldev;
1448
1449         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
1450
1451         i40iw_pr_info("state = %d\n", iwdev->init_state);
1452         if (iwdev->param_wq)
1453                 destroy_workqueue(iwdev->param_wq);
1454
1455         switch (iwdev->init_state) {
1456         case RDMA_DEV_REGISTERED:
1457                 iwdev->iw_status = 0;
1458                 i40iw_port_ibevent(iwdev);
1459                 i40iw_destroy_rdma_device(iwdev->iwibdev);
1460                 /* fallthrough */
1461         case IP_ADDR_REGISTERED:
1462                 if (!iwdev->reset)
1463                         i40iw_del_macip_entry(iwdev, (u8)iwdev->mac_ip_table_idx);
1464                 /* fallthrough */
1465         case INET_NOTIFIER:
1466                 if (!atomic_dec_return(&i40iw_notifiers_registered)) {
1467                         unregister_netevent_notifier(&i40iw_net_notifier);
1468                         unregister_inetaddr_notifier(&i40iw_inetaddr_notifier);
1469                         unregister_inet6addr_notifier(&i40iw_inetaddr6_notifier);
1470                 }
1471                 /* fallthrough */
1472         case PBLE_CHUNK_MEM:
1473                 i40iw_destroy_pble_pool(dev, iwdev->pble_rsrc);
1474                 /* fallthrough */
1475         case CEQ_CREATED:
1476                 i40iw_dele_ceqs(iwdev);
1477                 /* fallthrough */
1478         case AEQ_CREATED:
1479                 i40iw_destroy_aeq(iwdev);
1480                 /* fallthrough */
1481         case IEQ_CREATED:
1482                 i40iw_puda_dele_resources(&iwdev->vsi, I40IW_PUDA_RSRC_TYPE_IEQ, iwdev->reset);
1483                 /* fallthrough */
1484         case ILQ_CREATED:
1485                 i40iw_puda_dele_resources(&iwdev->vsi, I40IW_PUDA_RSRC_TYPE_ILQ, iwdev->reset);
1486                 /* fallthrough */
1487         case CCQ_CREATED:
1488                 i40iw_destroy_ccq(iwdev);
1489                 /* fallthrough */
1490         case HMC_OBJS_CREATED:
1491                 i40iw_del_hmc_objects(dev, dev->hmc_info, true, iwdev->reset);
1492                 /* fallthrough */
1493         case CQP_CREATED:
1494                 i40iw_destroy_cqp(iwdev, true);
1495                 /* fallthrough */
1496         case INITIAL_STATE:
1497                 i40iw_cleanup_cm_core(&iwdev->cm_core);
1498                 if (iwdev->vsi.pestat) {
1499                         i40iw_vsi_stats_free(&iwdev->vsi);
1500                         kfree(iwdev->vsi.pestat);
1501                 }
1502                 i40iw_del_init_mem(iwdev);
1503                 break;
1504         case INVALID_STATE:
1505                 /* fallthrough */
1506         default:
1507                 i40iw_pr_err("bad init_state = %d\n", iwdev->init_state);
1508                 break;
1509         }
1510
1511         i40iw_del_handler(i40iw_find_i40e_handler(ldev));
1512         kfree(iwdev->hdl);
1513 }
1514
1515 /**
1516  * i40iw_setup_init_state - set up the initial device struct
1517  * @hdl: handler for iwarp device - one per instance
1518  * @ldev: lan device information
1519  * @client: iwarp client information, provided during registration
1520  *
1521  * Initialize the iwarp device and its hdl information
1522  * using the ldev and client information
1523  * Return 0 if successful, otherwise return error
1524  */
1525 static enum i40iw_status_code i40iw_setup_init_state(struct i40iw_handler *hdl,
1526                                                      struct i40e_info *ldev,
1527                                                      struct i40e_client *client)
1528 {
1529         struct i40iw_device *iwdev = &hdl->device;
1530         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
1531         enum i40iw_status_code status;
1532
1533         memcpy(&hdl->ldev, ldev, sizeof(*ldev));
1534         if (resource_profile == 1)
1535                 resource_profile = 2;
1536
1537         iwdev->mpa_version = mpa_version;
1538         iwdev->resource_profile = (resource_profile < I40IW_HMC_PROFILE_EQUAL) ?
1539             (u8)resource_profile + I40IW_HMC_PROFILE_DEFAULT :
1540             I40IW_HMC_PROFILE_DEFAULT;
1541         iwdev->max_rdma_vfs =
1542                 (iwdev->resource_profile != I40IW_HMC_PROFILE_DEFAULT) ?  max_rdma_vfs : 0;
1543         iwdev->max_enabled_vfs = iwdev->max_rdma_vfs;
1544         iwdev->netdev = ldev->netdev;
1545         hdl->client = client;
1546         if (!ldev->ftype)
1547                 iwdev->db_start = pci_resource_start(ldev->pcidev, 0) + I40IW_DB_ADDR_OFFSET;
1548         else
1549                 iwdev->db_start = pci_resource_start(ldev->pcidev, 0) + I40IW_VF_DB_ADDR_OFFSET;
1550
1551         status = i40iw_save_msix_info(iwdev, ldev);
1552         if (status)
1553                 goto exit;
1554         iwdev->hw.dev_context = (void *)ldev->pcidev;
1555         iwdev->hw.hw_addr = ldev->hw_addr;
1556         status = i40iw_allocate_dma_mem(&iwdev->hw,
1557                                         &iwdev->obj_mem, 8192, 4096);
1558         if (status)
1559                 goto exit;
1560         iwdev->obj_next = iwdev->obj_mem;
1561         iwdev->push_mode = push_mode;
1562
1563         init_waitqueue_head(&iwdev->vchnl_waitq);
1564         init_waitqueue_head(&dev->vf_reqs);
1565         init_waitqueue_head(&iwdev->close_wq);
1566
1567         status = i40iw_initialize_dev(iwdev, ldev);
1568 exit:
1569         if (status) {
1570                 kfree(iwdev->iw_msixtbl);
1571                 i40iw_free_dma_mem(dev->hw, &iwdev->obj_mem);
1572                 iwdev->iw_msixtbl = NULL;
1573         }
1574         return status;
1575 }
1576
1577 /**
1578  * i40iw_get_used_rsrc - determine resources used internally
1579  * @iwdev: iwarp device
1580  *
1581  * Called after internal allocations
1582  */
1583 static void i40iw_get_used_rsrc(struct i40iw_device *iwdev)
1584 {
1585         iwdev->used_pds = find_next_zero_bit(iwdev->allocated_pds, iwdev->max_pd, 0);
1586         iwdev->used_qps = find_next_zero_bit(iwdev->allocated_qps, iwdev->max_qp, 0);
1587         iwdev->used_cqs = find_next_zero_bit(iwdev->allocated_cqs, iwdev->max_cq, 0);
1588         iwdev->used_mrs = find_next_zero_bit(iwdev->allocated_mrs, iwdev->max_mr, 0);
1589 }
1590
1591 /**
1592  * i40iw_open - client interface operation open for iwarp/uda device
1593  * @ldev: lan device information
1594  * @client: iwarp client information, provided during registration
1595  *
1596  * Called by the lan driver during the processing of client register
1597  * Create device resources, set up queues, pble and hmc objects and
1598  * register the device with the ib verbs interface
1599  * Return 0 if successful, otherwise return error
1600  */
1601 static int i40iw_open(struct i40e_info *ldev, struct i40e_client *client)
1602 {
1603         struct i40iw_device *iwdev;
1604         struct i40iw_sc_dev *dev;
1605         enum i40iw_status_code status;
1606         struct i40iw_handler *hdl;
1607
1608         hdl = i40iw_find_netdev(ldev->netdev);
1609         if (hdl)
1610                 return 0;
1611
1612         hdl = kzalloc(sizeof(*hdl), GFP_KERNEL);
1613         if (!hdl)
1614                 return -ENOMEM;
1615         iwdev = &hdl->device;
1616         iwdev->hdl = hdl;
1617         dev = &iwdev->sc_dev;
1618         i40iw_setup_cm_core(iwdev);
1619
1620         dev->back_dev = (void *)iwdev;
1621         iwdev->ldev = &hdl->ldev;
1622         iwdev->client = client;
1623         mutex_init(&iwdev->pbl_mutex);
1624         i40iw_add_handler(hdl);
1625
1626         do {
1627                 status = i40iw_setup_init_state(hdl, ldev, client);
1628                 if (status)
1629                         break;
1630                 iwdev->init_state = INITIAL_STATE;
1631                 if (dev->is_pf)
1632                         i40iw_wait_pe_ready(dev->hw);
1633                 status = i40iw_create_cqp(iwdev);
1634                 if (status)
1635                         break;
1636                 iwdev->init_state = CQP_CREATED;
1637                 status = i40iw_hmc_setup(iwdev);
1638                 if (status)
1639                         break;
1640                 status = i40iw_create_ccq(iwdev);
1641                 if (status)
1642                         break;
1643                 iwdev->init_state = CCQ_CREATED;
1644                 status = i40iw_initialize_ilq(iwdev);
1645                 if (status)
1646                         break;
1647                 iwdev->init_state = ILQ_CREATED;
1648                 status = i40iw_initialize_ieq(iwdev);
1649                 if (status)
1650                         break;
1651                 iwdev->init_state = IEQ_CREATED;
1652                 status = i40iw_setup_aeq(iwdev);
1653                 if (status)
1654                         break;
1655                 iwdev->init_state = AEQ_CREATED;
1656                 status = i40iw_setup_ceqs(iwdev, ldev);
1657                 if (status)
1658                         break;
1659                 iwdev->init_state = CEQ_CREATED;
1660                 status = i40iw_initialize_hw_resources(iwdev);
1661                 if (status)
1662                         break;
1663                 i40iw_get_used_rsrc(iwdev);
1664                 dev->ccq_ops->ccq_arm(dev->ccq);
1665                 status = i40iw_hmc_init_pble(&iwdev->sc_dev, iwdev->pble_rsrc);
1666                 if (status)
1667                         break;
1668                 iwdev->init_state = PBLE_CHUNK_MEM;
1669                 iwdev->virtchnl_wq = alloc_ordered_workqueue("iwvch", WQ_MEM_RECLAIM);
1670                 i40iw_register_notifiers();
1671                 iwdev->init_state = INET_NOTIFIER;
1672                 status = i40iw_add_mac_ip(iwdev);
1673                 if (status)
1674                         break;
1675                 iwdev->init_state = IP_ADDR_REGISTERED;
1676                 if (i40iw_register_rdma_device(iwdev)) {
1677                         i40iw_pr_err("register rdma device fail\n");
1678                         break;
1679                 };
1680
1681                 iwdev->init_state = RDMA_DEV_REGISTERED;
1682                 iwdev->iw_status = 1;
1683                 i40iw_port_ibevent(iwdev);
1684                 iwdev->param_wq = alloc_ordered_workqueue("l2params", WQ_MEM_RECLAIM);
1685                 if(iwdev->param_wq == NULL)
1686                         break;
1687                 i40iw_pr_info("i40iw_open completed\n");
1688                 return 0;
1689         } while (0);
1690
1691         i40iw_pr_err("status = %d last completion = %d\n", status, iwdev->init_state);
1692         i40iw_deinit_device(iwdev);
1693         return -ERESTART;
1694 }
1695
1696 /**
1697  * i40iw_l2params_worker - worker for l2 params change
1698  * @work: work pointer for l2 params
1699  */
1700 static void i40iw_l2params_worker(struct work_struct *work)
1701 {
1702         struct l2params_work *dwork =
1703             container_of(work, struct l2params_work, work);
1704         struct i40iw_device *iwdev = dwork->iwdev;
1705
1706         i40iw_change_l2params(&iwdev->vsi, &dwork->l2params);
1707         atomic_dec(&iwdev->params_busy);
1708         kfree(work);
1709 }
1710
1711 /**
1712  * i40iw_l2param_change - handle qs handles for qos and mss change
1713  * @ldev: lan device information
1714  * @client: client for paramater change
1715  * @params: new parameters from L2
1716  */
1717 static void i40iw_l2param_change(struct i40e_info *ldev, struct i40e_client *client,
1718                                  struct i40e_params *params)
1719 {
1720         struct i40iw_handler *hdl;
1721         struct i40iw_l2params *l2params;
1722         struct l2params_work *work;
1723         struct i40iw_device *iwdev;
1724         int i;
1725
1726         hdl = i40iw_find_i40e_handler(ldev);
1727         if (!hdl)
1728                 return;
1729
1730         iwdev = &hdl->device;
1731
1732         if (atomic_read(&iwdev->params_busy))
1733                 return;
1734
1735
1736         work = kzalloc(sizeof(*work), GFP_ATOMIC);
1737         if (!work)
1738                 return;
1739
1740         atomic_inc(&iwdev->params_busy);
1741
1742         work->iwdev = iwdev;
1743         l2params = &work->l2params;
1744         for (i = 0; i < I40E_CLIENT_MAX_USER_PRIORITY; i++)
1745                 l2params->qs_handle_list[i] = params->qos.prio_qos[i].qs_handle;
1746
1747         l2params->mss = (params->mtu) ? params->mtu - I40IW_MTU_TO_MSS : iwdev->vsi.mss;
1748
1749         INIT_WORK(&work->work, i40iw_l2params_worker);
1750         queue_work(iwdev->param_wq, &work->work);
1751 }
1752
1753 /**
1754  * i40iw_close - client interface operation close for iwarp/uda device
1755  * @ldev: lan device information
1756  * @client: client to close
1757  *
1758  * Called by the lan driver during the processing of client unregister
1759  * Destroy and clean up the driver resources
1760  */
1761 static void i40iw_close(struct i40e_info *ldev, struct i40e_client *client, bool reset)
1762 {
1763         struct i40iw_device *iwdev;
1764         struct i40iw_handler *hdl;
1765
1766         hdl = i40iw_find_i40e_handler(ldev);
1767         if (!hdl)
1768                 return;
1769
1770         iwdev = &hdl->device;
1771         iwdev->closing = true;
1772
1773         if (reset)
1774                 iwdev->reset = true;
1775
1776         i40iw_cm_disconnect_all(iwdev);
1777         destroy_workqueue(iwdev->virtchnl_wq);
1778         i40iw_deinit_device(iwdev);
1779 }
1780
1781 /**
1782  * i40iw_vf_reset - process VF reset
1783  * @ldev: lan device information
1784  * @client: client interface instance
1785  * @vf_id: virtual function id
1786  *
1787  * Called when a VF is reset by the PF
1788  * Destroy and clean up the VF resources
1789  */
1790 static void i40iw_vf_reset(struct i40e_info *ldev, struct i40e_client *client, u32 vf_id)
1791 {
1792         struct i40iw_handler *hdl;
1793         struct i40iw_sc_dev *dev;
1794         struct i40iw_hmc_fcn_info hmc_fcn_info;
1795         struct i40iw_virt_mem vf_dev_mem;
1796         struct i40iw_vfdev *tmp_vfdev;
1797         unsigned int i;
1798         unsigned long flags;
1799         struct i40iw_device *iwdev;
1800
1801         hdl = i40iw_find_i40e_handler(ldev);
1802         if (!hdl)
1803                 return;
1804
1805         dev = &hdl->device.sc_dev;
1806         iwdev = (struct i40iw_device *)dev->back_dev;
1807
1808         for (i = 0; i < I40IW_MAX_PE_ENABLED_VF_COUNT; i++) {
1809                 if (!dev->vf_dev[i] || (dev->vf_dev[i]->vf_id != vf_id))
1810                         continue;
1811                 /* free all resources allocated on behalf of vf */
1812                 tmp_vfdev = dev->vf_dev[i];
1813                 spin_lock_irqsave(&iwdev->vsi.pestat->lock, flags);
1814                 dev->vf_dev[i] = NULL;
1815                 spin_unlock_irqrestore(&iwdev->vsi.pestat->lock, flags);
1816                 i40iw_del_hmc_objects(dev, &tmp_vfdev->hmc_info, false, false);
1817                 /* remove vf hmc function */
1818                 memset(&hmc_fcn_info, 0, sizeof(hmc_fcn_info));
1819                 hmc_fcn_info.vf_id = vf_id;
1820                 hmc_fcn_info.iw_vf_idx = tmp_vfdev->iw_vf_idx;
1821                 hmc_fcn_info.free_fcn = true;
1822                 i40iw_cqp_manage_hmc_fcn_cmd(dev, &hmc_fcn_info);
1823                 /* free vf_dev */
1824                 vf_dev_mem.va = tmp_vfdev;
1825                 vf_dev_mem.size = sizeof(struct i40iw_vfdev) +
1826                                         sizeof(struct i40iw_hmc_obj_info) * I40IW_HMC_IW_MAX;
1827                 i40iw_free_virt_mem(dev->hw, &vf_dev_mem);
1828                 break;
1829         }
1830 }
1831
1832 /**
1833  * i40iw_vf_enable - enable a number of VFs
1834  * @ldev: lan device information
1835  * @client: client interface instance
1836  * @num_vfs: number of VFs for the PF
1837  *
1838  * Called when the number of VFs changes
1839  */
1840 static void i40iw_vf_enable(struct i40e_info *ldev,
1841                             struct i40e_client *client,
1842                             u32 num_vfs)
1843 {
1844         struct i40iw_handler *hdl;
1845
1846         hdl = i40iw_find_i40e_handler(ldev);
1847         if (!hdl)
1848                 return;
1849
1850         if (num_vfs > I40IW_MAX_PE_ENABLED_VF_COUNT)
1851                 hdl->device.max_enabled_vfs = I40IW_MAX_PE_ENABLED_VF_COUNT;
1852         else
1853                 hdl->device.max_enabled_vfs = num_vfs;
1854 }
1855
1856 /**
1857  * i40iw_vf_capable - check if VF capable
1858  * @ldev: lan device information
1859  * @client: client interface instance
1860  * @vf_id: virtual function id
1861  *
1862  * Return 1 if a VF slot is available or if VF is already RDMA enabled
1863  * Return 0 otherwise
1864  */
1865 static int i40iw_vf_capable(struct i40e_info *ldev,
1866                             struct i40e_client *client,
1867                             u32 vf_id)
1868 {
1869         struct i40iw_handler *hdl;
1870         struct i40iw_sc_dev *dev;
1871         unsigned int i;
1872
1873         hdl = i40iw_find_i40e_handler(ldev);
1874         if (!hdl)
1875                 return 0;
1876
1877         dev = &hdl->device.sc_dev;
1878
1879         for (i = 0; i < hdl->device.max_enabled_vfs; i++) {
1880                 if (!dev->vf_dev[i] || (dev->vf_dev[i]->vf_id == vf_id))
1881                         return 1;
1882         }
1883
1884         return 0;
1885 }
1886
1887 /**
1888  * i40iw_virtchnl_receive - receive a message through the virtual channel
1889  * @ldev: lan device information
1890  * @client: client interface instance
1891  * @vf_id: virtual function id associated with the message
1892  * @msg: message buffer pointer
1893  * @len: length of the message
1894  *
1895  * Invoke virtual channel receive operation for the given msg
1896  * Return 0 if successful, otherwise return error
1897  */
1898 static int i40iw_virtchnl_receive(struct i40e_info *ldev,
1899                                   struct i40e_client *client,
1900                                   u32 vf_id,
1901                                   u8 *msg,
1902                                   u16 len)
1903 {
1904         struct i40iw_handler *hdl;
1905         struct i40iw_sc_dev *dev;
1906         struct i40iw_device *iwdev;
1907         int ret_code = I40IW_NOT_SUPPORTED;
1908
1909         if (!len || !msg)
1910                 return I40IW_ERR_PARAM;
1911
1912         hdl = i40iw_find_i40e_handler(ldev);
1913         if (!hdl)
1914                 return I40IW_ERR_PARAM;
1915
1916         dev = &hdl->device.sc_dev;
1917         iwdev = dev->back_dev;
1918
1919         if (dev->vchnl_if.vchnl_recv) {
1920                 ret_code = dev->vchnl_if.vchnl_recv(dev, vf_id, msg, len);
1921                 if (!dev->is_pf) {
1922                         atomic_dec(&iwdev->vchnl_msgs);
1923                         wake_up(&iwdev->vchnl_waitq);
1924                 }
1925         }
1926         return ret_code;
1927 }
1928
1929 /**
1930  * i40iw_vf_clear_to_send - wait to send virtual channel message
1931  * @dev: iwarp device *
1932  * Wait for until virtual channel is clear
1933  * before sending the next message
1934  *
1935  * Returns false if error
1936  * Returns true if clear to send
1937  */
1938 bool i40iw_vf_clear_to_send(struct i40iw_sc_dev *dev)
1939 {
1940         struct i40iw_device *iwdev;
1941         wait_queue_entry_t wait;
1942
1943         iwdev = dev->back_dev;
1944
1945         if (!wq_has_sleeper(&dev->vf_reqs) &&
1946             (atomic_read(&iwdev->vchnl_msgs) == 0))
1947                 return true; /* virtual channel is clear */
1948
1949         init_wait(&wait);
1950         add_wait_queue_exclusive(&dev->vf_reqs, &wait);
1951
1952         if (!wait_event_timeout(dev->vf_reqs,
1953                                 (atomic_read(&iwdev->vchnl_msgs) == 0),
1954                                 I40IW_VCHNL_EVENT_TIMEOUT))
1955                 dev->vchnl_up = false;
1956
1957         remove_wait_queue(&dev->vf_reqs, &wait);
1958
1959         return dev->vchnl_up;
1960 }
1961
1962 /**
1963  * i40iw_virtchnl_send - send a message through the virtual channel
1964  * @dev: iwarp device
1965  * @vf_id: virtual function id associated with the message
1966  * @msg: virtual channel message buffer pointer
1967  * @len: length of the message
1968  *
1969  * Invoke virtual channel send operation for the given msg
1970  * Return 0 if successful, otherwise return error
1971  */
1972 static enum i40iw_status_code i40iw_virtchnl_send(struct i40iw_sc_dev *dev,
1973                                                   u32 vf_id,
1974                                                   u8 *msg,
1975                                                   u16 len)
1976 {
1977         struct i40iw_device *iwdev;
1978         struct i40e_info *ldev;
1979
1980         if (!dev || !dev->back_dev)
1981                 return I40IW_ERR_BAD_PTR;
1982
1983         iwdev = dev->back_dev;
1984         ldev = iwdev->ldev;
1985
1986         if (ldev && ldev->ops && ldev->ops->virtchnl_send)
1987                 return ldev->ops->virtchnl_send(ldev, &i40iw_client, vf_id, msg, len);
1988         return I40IW_ERR_BAD_PTR;
1989 }
1990
1991 /* client interface functions */
1992 static const struct i40e_client_ops i40e_ops = {
1993         .open = i40iw_open,
1994         .close = i40iw_close,
1995         .l2_param_change = i40iw_l2param_change,
1996         .virtchnl_receive = i40iw_virtchnl_receive,
1997         .vf_reset = i40iw_vf_reset,
1998         .vf_enable = i40iw_vf_enable,
1999         .vf_capable = i40iw_vf_capable
2000 };
2001
2002 /**
2003  * i40iw_init_module - driver initialization function
2004  *
2005  * First function to call when the driver is loaded
2006  * Register the driver as i40e client and port mapper client
2007  */
2008 static int __init i40iw_init_module(void)
2009 {
2010         int ret;
2011
2012         memset(&i40iw_client, 0, sizeof(i40iw_client));
2013         i40iw_client.version.major = CLIENT_IW_INTERFACE_VERSION_MAJOR;
2014         i40iw_client.version.minor = CLIENT_IW_INTERFACE_VERSION_MINOR;
2015         i40iw_client.version.build = CLIENT_IW_INTERFACE_VERSION_BUILD;
2016         i40iw_client.ops = &i40e_ops;
2017         memcpy(i40iw_client.name, i40iw_client_name, I40E_CLIENT_STR_LENGTH);
2018         i40iw_client.type = I40E_CLIENT_IWARP;
2019         spin_lock_init(&i40iw_handler_lock);
2020         ret = i40e_register_client(&i40iw_client);
2021         return ret;
2022 }
2023
2024 /**
2025  * i40iw_exit_module - driver exit clean up function
2026  *
2027  * The function is called just before the driver is unloaded
2028  * Unregister the driver as i40e client and port mapper client
2029  */
2030 static void __exit i40iw_exit_module(void)
2031 {
2032         i40e_unregister_client(&i40iw_client);
2033 }
2034
2035 module_init(i40iw_init_module);
2036 module_exit(i40iw_exit_module);