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