90b435cd77ecef7d59ea8d7b715e6e75159f14f1
[cascardo/linux.git] / drivers / net / ethernet / intel / i40e / i40e_client.c
1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Driver
4  * Copyright(c) 2013 - 2015 Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  ******************************************************************************/
26
27 #include <linux/list.h>
28 #include <linux/errno.h>
29
30 #include "i40e.h"
31 #include "i40e_prototype.h"
32 #include "i40e_client.h"
33
34 static const char i40e_client_interface_version_str[] = I40E_CLIENT_VERSION_STR;
35
36 static LIST_HEAD(i40e_devices);
37 static DEFINE_MUTEX(i40e_device_mutex);
38
39 static LIST_HEAD(i40e_clients);
40 static DEFINE_MUTEX(i40e_client_mutex);
41
42 static LIST_HEAD(i40e_client_instances);
43 static DEFINE_MUTEX(i40e_client_instance_mutex);
44
45 static int i40e_client_virtchnl_send(struct i40e_info *ldev,
46                                      struct i40e_client *client,
47                                      u32 vf_id, u8 *msg, u16 len);
48
49 static int i40e_client_setup_qvlist(struct i40e_info *ldev,
50                                     struct i40e_client *client,
51                                     struct i40e_qvlist_info *qvlist_info);
52
53 static void i40e_client_request_reset(struct i40e_info *ldev,
54                                       struct i40e_client *client,
55                                       u32 reset_level);
56
57 static int i40e_client_update_vsi_ctxt(struct i40e_info *ldev,
58                                        struct i40e_client *client,
59                                        bool is_vf, u32 vf_id,
60                                        u32 flag, u32 valid_flag);
61
62 static struct i40e_ops i40e_lan_ops = {
63         .virtchnl_send = i40e_client_virtchnl_send,
64         .setup_qvlist = i40e_client_setup_qvlist,
65         .request_reset = i40e_client_request_reset,
66         .update_vsi_ctxt = i40e_client_update_vsi_ctxt,
67 };
68
69 /**
70  * i40e_client_type_to_vsi_type - convert client type to vsi type
71  * @client_type: the i40e_client type
72  *
73  * returns the related vsi type value
74  **/
75 static
76 enum i40e_vsi_type i40e_client_type_to_vsi_type(enum i40e_client_type type)
77 {
78         switch (type) {
79         case I40E_CLIENT_IWARP:
80                 return I40E_VSI_IWARP;
81
82         case I40E_CLIENT_VMDQ2:
83                 return I40E_VSI_VMDQ2;
84
85         default:
86                 pr_err("i40e: Client type unknown\n");
87                 return I40E_VSI_TYPE_UNKNOWN;
88         }
89 }
90
91 /**
92  * i40e_client_get_params - Get the params that can change at runtime
93  * @vsi: the VSI with the message
94  * @param: clinet param struct
95  *
96  **/
97 static
98 int i40e_client_get_params(struct i40e_vsi *vsi, struct i40e_params *params)
99 {
100         struct i40e_dcbx_config *dcb_cfg = &vsi->back->hw.local_dcbx_config;
101         int i = 0;
102
103         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
104                 u8 tc = dcb_cfg->etscfg.prioritytable[i];
105                 u16 qs_handle;
106
107                 /* If TC is not enabled for VSI use TC0 for UP */
108                 if (!(vsi->tc_config.enabled_tc & BIT(tc)))
109                         tc = 0;
110
111                 qs_handle = le16_to_cpu(vsi->info.qs_handle[tc]);
112                 params->qos.prio_qos[i].tc = tc;
113                 params->qos.prio_qos[i].qs_handle = qs_handle;
114                 if (qs_handle == I40E_AQ_VSI_QS_HANDLE_INVALID) {
115                         dev_err(&vsi->back->pdev->dev, "Invalid queue set handle for TC = %d, vsi id = %d\n",
116                                 tc, vsi->id);
117                         return -EINVAL;
118                 }
119         }
120
121         params->mtu = vsi->netdev->mtu;
122         return 0;
123 }
124
125 /**
126  * i40e_notify_client_of_vf_msg - call the client vf message callback
127  * @vsi: the VSI with the message
128  * @vf_id: the absolute VF id that sent the message
129  * @msg: message buffer
130  * @len: length of the message
131  *
132  * If there is a client to this VSI, call the client
133  **/
134 void
135 i40e_notify_client_of_vf_msg(struct i40e_vsi *vsi, u32 vf_id, u8 *msg, u16 len)
136 {
137         struct i40e_client_instance *cdev;
138
139         if (!vsi)
140                 return;
141         mutex_lock(&i40e_client_instance_mutex);
142         list_for_each_entry(cdev, &i40e_client_instances, list) {
143                 if (cdev->lan_info.pf == vsi->back) {
144                         if (!cdev->client ||
145                             !cdev->client->ops ||
146                             !cdev->client->ops->virtchnl_receive) {
147                                 dev_dbg(&vsi->back->pdev->dev,
148                                         "Cannot locate client instance virtual channel receive routine\n");
149                                 continue;
150                         }
151                         cdev->client->ops->virtchnl_receive(&cdev->lan_info,
152                                                             cdev->client,
153                                                             vf_id, msg, len);
154                 }
155         }
156         mutex_unlock(&i40e_client_instance_mutex);
157 }
158
159 /**
160  * i40e_notify_client_of_l2_param_changes - call the client notify callback
161  * @vsi: the VSI with l2 param changes
162  *
163  * If there is a client to this VSI, call the client
164  **/
165 void i40e_notify_client_of_l2_param_changes(struct i40e_vsi *vsi)
166 {
167         struct i40e_client_instance *cdev;
168         struct i40e_params params;
169
170         if (!vsi)
171                 return;
172         memset(&params, 0, sizeof(params));
173         i40e_client_get_params(vsi, &params);
174         mutex_lock(&i40e_client_instance_mutex);
175         list_for_each_entry(cdev, &i40e_client_instances, list) {
176                 if (cdev->lan_info.pf == vsi->back) {
177                         if (!cdev->client ||
178                             !cdev->client->ops ||
179                             !cdev->client->ops->l2_param_change) {
180                                 dev_dbg(&vsi->back->pdev->dev,
181                                         "Cannot locate client instance l2_param_change routine\n");
182                                 continue;
183                         }
184                         cdev->lan_info.params = params;
185                         cdev->client->ops->l2_param_change(&cdev->lan_info,
186                                                            cdev->client,
187                                                            &params);
188                 }
189         }
190         mutex_unlock(&i40e_client_instance_mutex);
191 }
192
193 /**
194  * i40e_notify_client_of_netdev_open - call the client open callback
195  * @vsi: the VSI with netdev opened
196  *
197  * If there is a client to this netdev, call the client with open
198  **/
199 void i40e_notify_client_of_netdev_open(struct i40e_vsi *vsi)
200 {
201         struct i40e_client_instance *cdev;
202
203         if (!vsi)
204                 return;
205         mutex_lock(&i40e_client_instance_mutex);
206         list_for_each_entry(cdev, &i40e_client_instances, list) {
207                 if (cdev->lan_info.netdev == vsi->netdev) {
208                         if (!cdev->client ||
209                             !cdev->client->ops || !cdev->client->ops->open) {
210                                 dev_dbg(&vsi->back->pdev->dev,
211                                         "Cannot locate client instance open routine\n");
212                                 continue;
213                         }
214                         cdev->client->ops->open(&cdev->lan_info, cdev->client);
215                 }
216         }
217         mutex_unlock(&i40e_client_instance_mutex);
218 }
219
220 /**
221  * i40e_client_release_qvlist
222  * @ldev: pointer to L2 context.
223  *
224  **/
225 static void i40e_client_release_qvlist(struct i40e_info *ldev)
226 {
227         struct i40e_qvlist_info *qvlist_info = ldev->qvlist_info;
228         u32 i;
229
230         if (!ldev->qvlist_info)
231                 return;
232
233         for (i = 0; i < qvlist_info->num_vectors; i++) {
234                 struct i40e_pf *pf = ldev->pf;
235                 struct i40e_qv_info *qv_info;
236                 u32 reg_idx;
237
238                 qv_info = &qvlist_info->qv_info[i];
239                 if (!qv_info)
240                         continue;
241                 reg_idx = I40E_PFINT_LNKLSTN(qv_info->v_idx - 1);
242                 wr32(&pf->hw, reg_idx, I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
243         }
244         kfree(ldev->qvlist_info);
245         ldev->qvlist_info = NULL;
246 }
247
248 /**
249  * i40e_notify_client_of_netdev_close - call the client close callback
250  * @vsi: the VSI with netdev closed
251  * @reset: true when close called due to a reset pending
252  *
253  * If there is a client to this netdev, call the client with close
254  **/
255 void i40e_notify_client_of_netdev_close(struct i40e_vsi *vsi, bool reset)
256 {
257         struct i40e_client_instance *cdev;
258
259         if (!vsi)
260                 return;
261         mutex_lock(&i40e_client_instance_mutex);
262         list_for_each_entry(cdev, &i40e_client_instances, list) {
263                 if (cdev->lan_info.netdev == vsi->netdev) {
264                         if (!cdev->client ||
265                             !cdev->client->ops || !cdev->client->ops->close) {
266                                 dev_dbg(&vsi->back->pdev->dev,
267                                         "Cannot locate client instance close routine\n");
268                                 continue;
269                         }
270                         cdev->client->ops->close(&cdev->lan_info, cdev->client,
271                                                  reset);
272                         i40e_client_release_qvlist(&cdev->lan_info);
273                 }
274         }
275         mutex_unlock(&i40e_client_instance_mutex);
276 }
277
278 /**
279  * i40e_notify_client_of_vf_reset - call the client vf reset callback
280  * @pf: PF device pointer
281  * @vf_id: asolute id of VF being reset
282  *
283  * If there is a client attached to this PF, notify when a VF is reset
284  **/
285 void i40e_notify_client_of_vf_reset(struct i40e_pf *pf, u32 vf_id)
286 {
287         struct i40e_client_instance *cdev;
288
289         if (!pf)
290                 return;
291         mutex_lock(&i40e_client_instance_mutex);
292         list_for_each_entry(cdev, &i40e_client_instances, list) {
293                 if (cdev->lan_info.pf == pf) {
294                         if (!cdev->client ||
295                             !cdev->client->ops ||
296                             !cdev->client->ops->vf_reset) {
297                                 dev_dbg(&pf->pdev->dev,
298                                         "Cannot locate client instance VF reset routine\n");
299                                 continue;
300                         }
301                         cdev->client->ops->vf_reset(&cdev->lan_info,
302                                                     cdev->client, vf_id);
303                 }
304         }
305         mutex_unlock(&i40e_client_instance_mutex);
306 }
307
308 /**
309  * i40e_notify_client_of_vf_enable - call the client vf notification callback
310  * @pf: PF device pointer
311  * @num_vfs: the number of VFs currently enabled, 0 for disable
312  *
313  * If there is a client attached to this PF, call its VF notification routine
314  **/
315 void i40e_notify_client_of_vf_enable(struct i40e_pf *pf, u32 num_vfs)
316 {
317         struct i40e_client_instance *cdev;
318
319         if (!pf)
320                 return;
321         mutex_lock(&i40e_client_instance_mutex);
322         list_for_each_entry(cdev, &i40e_client_instances, list) {
323                 if (cdev->lan_info.pf == pf) {
324                         if (!cdev->client ||
325                             !cdev->client->ops ||
326                             !cdev->client->ops->vf_enable) {
327                                 dev_dbg(&pf->pdev->dev,
328                                         "Cannot locate client instance VF enable routine\n");
329                                 continue;
330                         }
331                         cdev->client->ops->vf_enable(&cdev->lan_info,
332                                                      cdev->client, num_vfs);
333                 }
334         }
335         mutex_unlock(&i40e_client_instance_mutex);
336 }
337
338 /**
339  * i40e_vf_client_capable - ask the client if it likes the specified VF
340  * @pf: PF device pointer
341  * @vf_id: the VF in question
342  *
343  * If there is a client of the specified type attached to this PF, call
344  * its vf_capable routine
345  **/
346 int i40e_vf_client_capable(struct i40e_pf *pf, u32 vf_id,
347                            enum i40e_client_type type)
348 {
349         struct i40e_client_instance *cdev;
350         int capable = false;
351
352         if (!pf)
353                 return false;
354         mutex_lock(&i40e_client_instance_mutex);
355         list_for_each_entry(cdev, &i40e_client_instances, list) {
356                 if (cdev->lan_info.pf == pf) {
357                         if (!cdev->client ||
358                             !cdev->client->ops ||
359                             !cdev->client->ops->vf_capable ||
360                             !(cdev->client->type == type)) {
361                                 dev_dbg(&pf->pdev->dev,
362                                         "Cannot locate client instance VF capability routine\n");
363                                 continue;
364                         }
365                         capable = cdev->client->ops->vf_capable(&cdev->lan_info,
366                                                                 cdev->client,
367                                                                 vf_id);
368                         break;
369                 }
370         }
371         mutex_unlock(&i40e_client_instance_mutex);
372         return capable;
373 }
374
375 /**
376  * i40e_vsi_lookup - finds a matching VSI from the PF list starting at start_vsi
377  * @pf: board private structure
378  * @type: vsi type
379  * @start_vsi: a VSI pointer from where to start the search
380  *
381  * Returns non NULL on success or NULL for failure
382  **/
383 struct i40e_vsi *i40e_vsi_lookup(struct i40e_pf *pf,
384                                  enum i40e_vsi_type type,
385                                  struct i40e_vsi *start_vsi)
386 {
387         struct i40e_vsi *vsi;
388         int i = 0;
389
390         if (start_vsi) {
391                 for (i = 0; i < pf->num_alloc_vsi; i++) {
392                         vsi = pf->vsi[i];
393                         if (vsi == start_vsi)
394                                 break;
395                 }
396         }
397         for (; i < pf->num_alloc_vsi; i++) {
398                 vsi = pf->vsi[i];
399                 if (vsi && vsi->type == type)
400                         return vsi;
401         }
402
403         return NULL;
404 }
405
406 /**
407  * i40e_client_add_instance - add a client instance struct to the instance list
408  * @pf: pointer to the board struct
409  * @client: pointer to a client struct in the client list.
410  *
411  * Returns cdev ptr on success, NULL on failure
412  **/
413 static
414 struct i40e_client_instance *i40e_client_add_instance(struct i40e_pf *pf,
415                                                       struct i40e_client *client)
416 {
417         struct i40e_client_instance *cdev;
418         struct netdev_hw_addr *mac = NULL;
419         struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
420
421         mutex_lock(&i40e_client_instance_mutex);
422         list_for_each_entry(cdev, &i40e_client_instances, list) {
423                 if ((cdev->lan_info.pf == pf) && (cdev->client == client)) {
424                         cdev = NULL;
425                         goto out;
426                 }
427         }
428         cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
429         if (!cdev)
430                 goto out;
431
432         cdev->lan_info.pf = (void *)pf;
433         cdev->lan_info.netdev = vsi->netdev;
434         cdev->lan_info.pcidev = pf->pdev;
435         cdev->lan_info.fid = pf->hw.pf_id;
436         cdev->lan_info.ftype = I40E_CLIENT_FTYPE_PF;
437         cdev->lan_info.hw_addr = pf->hw.hw_addr;
438         cdev->lan_info.ops = &i40e_lan_ops;
439         cdev->lan_info.version.major = I40E_CLIENT_VERSION_MAJOR;
440         cdev->lan_info.version.minor = I40E_CLIENT_VERSION_MINOR;
441         cdev->lan_info.version.build = I40E_CLIENT_VERSION_BUILD;
442         cdev->lan_info.fw_maj_ver = pf->hw.aq.fw_maj_ver;
443         cdev->lan_info.fw_min_ver = pf->hw.aq.fw_min_ver;
444         cdev->lan_info.fw_build = pf->hw.aq.fw_build;
445         set_bit(__I40E_CLIENT_INSTANCE_NONE, &cdev->state);
446
447         if (i40e_client_get_params(vsi, &cdev->lan_info.params)) {
448                 kfree(cdev);
449                 cdev = NULL;
450                 goto out;
451         }
452
453         cdev->lan_info.msix_count = pf->num_iwarp_msix;
454         cdev->lan_info.msix_entries = &pf->msix_entries[pf->iwarp_base_vector];
455
456         mac = list_first_entry(&cdev->lan_info.netdev->dev_addrs.list,
457                                struct netdev_hw_addr, list);
458         if (mac)
459                 ether_addr_copy(cdev->lan_info.lanmac, mac->addr);
460         else
461                 dev_err(&pf->pdev->dev, "MAC address list is empty!\n");
462
463         cdev->client = client;
464         INIT_LIST_HEAD(&cdev->list);
465         list_add(&cdev->list, &i40e_client_instances);
466 out:
467         mutex_unlock(&i40e_client_instance_mutex);
468         return cdev;
469 }
470
471 /**
472  * i40e_client_del_instance - removes a client instance from the list
473  * @pf: pointer to the board struct
474  *
475  * Returns 0 on success or non-0 on error
476  **/
477 static
478 int i40e_client_del_instance(struct i40e_pf *pf, struct i40e_client *client)
479 {
480         struct i40e_client_instance *cdev, *tmp;
481         int ret = -ENODEV;
482
483         mutex_lock(&i40e_client_instance_mutex);
484         list_for_each_entry_safe(cdev, tmp, &i40e_client_instances, list) {
485                 if ((cdev->lan_info.pf != pf) || (cdev->client != client))
486                         continue;
487
488                 dev_info(&pf->pdev->dev, "Deleted instance of Client %s, of dev %d bus=0x%02x func=0x%02x)\n",
489                          client->name, pf->hw.pf_id,
490                          pf->hw.bus.device, pf->hw.bus.func);
491                 list_del(&cdev->list);
492                 kfree(cdev);
493                 ret = 0;
494                 break;
495         }
496         mutex_unlock(&i40e_client_instance_mutex);
497         return ret;
498 }
499
500 /**
501  * i40e_client_subtask - client maintenance work
502  * @pf: board private structure
503  **/
504 void i40e_client_subtask(struct i40e_pf *pf)
505 {
506         struct i40e_client_instance *cdev;
507         struct i40e_client *client;
508         int ret = 0;
509
510         if (!(pf->flags & I40E_FLAG_SERVICE_CLIENT_REQUESTED))
511                 return;
512         pf->flags &= ~I40E_FLAG_SERVICE_CLIENT_REQUESTED;
513
514         /* If we're down or resetting, just bail */
515         if (test_bit(__I40E_DOWN, &pf->state) ||
516             test_bit(__I40E_CONFIG_BUSY, &pf->state))
517                 return;
518
519         /* Check client state and instantiate client if client registered */
520         mutex_lock(&i40e_client_mutex);
521         list_for_each_entry(client, &i40e_clients, list) {
522                 /* first check client is registered */
523                 if (!test_bit(__I40E_CLIENT_REGISTERED, &client->state))
524                         continue;
525
526                 /* Do we also need the LAN VSI to be up, to create instance */
527                 if (!(client->flags & I40E_CLIENT_FLAGS_LAUNCH_ON_PROBE)) {
528                         /* check if L2 VSI is up, if not we are not ready */
529                         if (test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
530                                 continue;
531                 }
532
533                 /* Add the client instance to the instance list */
534                 cdev = i40e_client_add_instance(pf, client);
535                 if (!cdev)
536                         continue;
537
538                 /* Also up the ref_cnt of no. of instances of this client */
539                 atomic_inc(&client->ref_cnt);
540                 dev_info(&pf->pdev->dev, "Added instance of Client %s to PF%d bus=0x%02x func=0x%02x\n",
541                          client->name, pf->hw.pf_id,
542                          pf->hw.bus.device, pf->hw.bus.func);
543
544                 mutex_lock(&i40e_client_instance_mutex);
545                 /* Send an Open request to the client */
546                 atomic_inc(&cdev->ref_cnt);
547                 if (client->ops && client->ops->open)
548                         ret = client->ops->open(&cdev->lan_info, client);
549                 atomic_dec(&cdev->ref_cnt);
550                 if (!ret) {
551                         set_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
552                 } else {
553                         /* remove client instance */
554                         i40e_client_del_instance(pf, client);
555                         atomic_dec(&client->ref_cnt);
556                         continue;
557                 }
558                 mutex_unlock(&i40e_client_instance_mutex);
559         }
560         mutex_unlock(&i40e_client_mutex);
561 }
562
563 /**
564  * i40e_lan_add_device - add a lan device struct to the list of lan devices
565  * @pf: pointer to the board struct
566  *
567  * Returns 0 on success or none 0 on error
568  **/
569 int i40e_lan_add_device(struct i40e_pf *pf)
570 {
571         struct i40e_device *ldev;
572         int ret = 0;
573
574         mutex_lock(&i40e_device_mutex);
575         list_for_each_entry(ldev, &i40e_devices, list) {
576                 if (ldev->pf == pf) {
577                         ret = -EEXIST;
578                         goto out;
579                 }
580         }
581         ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
582         if (!ldev) {
583                 ret = -ENOMEM;
584                 goto out;
585         }
586         ldev->pf = pf;
587         INIT_LIST_HEAD(&ldev->list);
588         list_add(&ldev->list, &i40e_devices);
589         dev_info(&pf->pdev->dev, "Added LAN device PF%d bus=0x%02x func=0x%02x\n",
590                  pf->hw.pf_id, pf->hw.bus.device, pf->hw.bus.func);
591
592         /* Since in some cases register may have happened before a device gets
593          * added, we can schedule a subtask to go initiate the clients.
594          */
595         pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
596         i40e_service_event_schedule(pf);
597
598 out:
599         mutex_unlock(&i40e_device_mutex);
600         return ret;
601 }
602
603 /**
604  * i40e_lan_del_device - removes a lan device from the device list
605  * @pf: pointer to the board struct
606  *
607  * Returns 0 on success or non-0 on error
608  **/
609 int i40e_lan_del_device(struct i40e_pf *pf)
610 {
611         struct i40e_device *ldev, *tmp;
612         int ret = -ENODEV;
613
614         mutex_lock(&i40e_device_mutex);
615         list_for_each_entry_safe(ldev, tmp, &i40e_devices, list) {
616                 if (ldev->pf == pf) {
617                         dev_info(&pf->pdev->dev, "Deleted LAN device PF%d bus=0x%02x func=0x%02x\n",
618                                  pf->hw.pf_id, pf->hw.bus.device,
619                                  pf->hw.bus.func);
620                         list_del(&ldev->list);
621                         kfree(ldev);
622                         ret = 0;
623                         break;
624                 }
625         }
626
627         mutex_unlock(&i40e_device_mutex);
628         return ret;
629 }
630
631 /**
632  * i40e_client_release - release client specific resources
633  * @client: pointer to the registered client
634  *
635  * Return 0 on success or < 0 on error
636  **/
637 static int i40e_client_release(struct i40e_client *client)
638 {
639         struct i40e_client_instance *cdev, *tmp;
640         struct i40e_pf *pf = NULL;
641         int ret = 0;
642
643         LIST_HEAD(cdevs_tmp);
644
645         mutex_lock(&i40e_client_instance_mutex);
646         list_for_each_entry_safe(cdev, tmp, &i40e_client_instances, list) {
647                 if (strncmp(cdev->client->name, client->name,
648                             I40E_CLIENT_STR_LENGTH))
649                         continue;
650                 if (test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state)) {
651                         if (atomic_read(&cdev->ref_cnt) > 0) {
652                                 ret = I40E_ERR_NOT_READY;
653                                 goto out;
654                         }
655                         pf = (struct i40e_pf *)cdev->lan_info.pf;
656                         if (client->ops && client->ops->close)
657                                 client->ops->close(&cdev->lan_info, client,
658                                                    false);
659                         i40e_client_release_qvlist(&cdev->lan_info);
660                         clear_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
661
662                         dev_warn(&pf->pdev->dev,
663                                  "Client %s instance for PF id %d closed\n",
664                                  client->name, pf->hw.pf_id);
665                 }
666                 /* delete the client instance from the list */
667                 list_move(&cdev->list, &cdevs_tmp);
668                 atomic_dec(&client->ref_cnt);
669                 dev_info(&pf->pdev->dev, "Deleted client instance of Client %s\n",
670                          client->name);
671         }
672 out:
673         mutex_unlock(&i40e_client_instance_mutex);
674
675         /* free the client device and release its vsi */
676         list_for_each_entry_safe(cdev, tmp, &cdevs_tmp, list) {
677                 kfree(cdev);
678         }
679         return ret;
680 }
681
682 /**
683  * i40e_client_prepare - prepare client specific resources
684  * @client: pointer to the registered client
685  *
686  * Return 0 on success or < 0 on error
687  **/
688 static int i40e_client_prepare(struct i40e_client *client)
689 {
690         struct i40e_device *ldev;
691         struct i40e_pf *pf;
692         int ret = 0;
693
694         mutex_lock(&i40e_device_mutex);
695         list_for_each_entry(ldev, &i40e_devices, list) {
696                 pf = ldev->pf;
697                 /* Start the client subtask */
698                 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
699                 i40e_service_event_schedule(pf);
700         }
701         mutex_unlock(&i40e_device_mutex);
702         return ret;
703 }
704
705 /**
706  * i40e_client_virtchnl_send - TBD
707  * @ldev: pointer to L2 context
708  * @client: Client pointer
709  * @vf_id: absolute VF identifier
710  * @msg: message buffer
711  * @len: length of message buffer
712  *
713  * Return 0 on success or < 0 on error
714  **/
715 static int i40e_client_virtchnl_send(struct i40e_info *ldev,
716                                      struct i40e_client *client,
717                                      u32 vf_id, u8 *msg, u16 len)
718 {
719         struct i40e_pf *pf = ldev->pf;
720         struct i40e_hw *hw = &pf->hw;
721         i40e_status err;
722
723         err = i40e_aq_send_msg_to_vf(hw, vf_id, I40E_VIRTCHNL_OP_IWARP,
724                                      0, msg, len, NULL);
725         if (err)
726                 dev_err(&pf->pdev->dev, "Unable to send iWarp message to VF, error %d, aq status %d\n",
727                         err, hw->aq.asq_last_status);
728
729         return err;
730 }
731
732 /**
733  * i40e_client_setup_qvlist
734  * @ldev: pointer to L2 context.
735  * @client: Client pointer.
736  * @qv_info: queue and vector list
737  *
738  * Return 0 on success or < 0 on error
739  **/
740 static int i40e_client_setup_qvlist(struct i40e_info *ldev,
741                                     struct i40e_client *client,
742                                     struct i40e_qvlist_info *qvlist_info)
743 {
744         struct i40e_pf *pf = ldev->pf;
745         struct i40e_hw *hw = &pf->hw;
746         struct i40e_qv_info *qv_info;
747         u32 v_idx, i, reg_idx, reg;
748         u32 size;
749
750         size = sizeof(struct i40e_qvlist_info) +
751                (sizeof(struct i40e_qv_info) * (qvlist_info->num_vectors - 1));
752         ldev->qvlist_info = kzalloc(size, GFP_KERNEL);
753         ldev->qvlist_info->num_vectors = qvlist_info->num_vectors;
754
755         for (i = 0; i < qvlist_info->num_vectors; i++) {
756                 qv_info = &qvlist_info->qv_info[i];
757                 if (!qv_info)
758                         continue;
759                 v_idx = qv_info->v_idx;
760
761                 /* Validate vector id belongs to this client */
762                 if ((v_idx >= (pf->iwarp_base_vector + pf->num_iwarp_msix)) ||
763                     (v_idx < pf->iwarp_base_vector))
764                         goto err;
765
766                 ldev->qvlist_info->qv_info[i] = *qv_info;
767                 reg_idx = I40E_PFINT_LNKLSTN(v_idx - 1);
768
769                 if (qv_info->ceq_idx == I40E_QUEUE_INVALID_IDX) {
770                         /* Special case - No CEQ mapped on this vector */
771                         wr32(hw, reg_idx, I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
772                 } else {
773                         reg = (qv_info->ceq_idx &
774                                I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK) |
775                                (I40E_QUEUE_TYPE_PE_CEQ <<
776                                I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
777                         wr32(hw, reg_idx, reg);
778
779                         reg = (I40E_PFINT_CEQCTL_CAUSE_ENA_MASK |
780                                (v_idx << I40E_PFINT_CEQCTL_MSIX_INDX_SHIFT) |
781                                (qv_info->itr_idx <<
782                                 I40E_PFINT_CEQCTL_ITR_INDX_SHIFT) |
783                                (I40E_QUEUE_END_OF_LIST <<
784                                 I40E_PFINT_CEQCTL_NEXTQ_INDX_SHIFT));
785                         wr32(hw, I40E_PFINT_CEQCTL(qv_info->ceq_idx), reg);
786                 }
787                 if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
788                         reg = (I40E_PFINT_AEQCTL_CAUSE_ENA_MASK |
789                                (v_idx << I40E_PFINT_AEQCTL_MSIX_INDX_SHIFT) |
790                                (qv_info->itr_idx <<
791                                 I40E_PFINT_AEQCTL_ITR_INDX_SHIFT));
792
793                         wr32(hw, I40E_PFINT_AEQCTL, reg);
794                 }
795         }
796         /* Mitigate sync problems with iwarp VF driver */
797         i40e_flush(hw);
798         return 0;
799 err:
800         kfree(ldev->qvlist_info);
801         ldev->qvlist_info = NULL;
802         return -EINVAL;
803 }
804
805 /**
806  * i40e_client_request_reset
807  * @ldev: pointer to L2 context.
808  * @client: Client pointer.
809  * @level: reset level
810  **/
811 static void i40e_client_request_reset(struct i40e_info *ldev,
812                                       struct i40e_client *client,
813                                       u32 reset_level)
814 {
815         struct i40e_pf *pf = ldev->pf;
816
817         switch (reset_level) {
818         case I40E_CLIENT_RESET_LEVEL_PF:
819                 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
820                 break;
821         case I40E_CLIENT_RESET_LEVEL_CORE:
822                 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
823                 break;
824         default:
825                 dev_warn(&pf->pdev->dev,
826                          "Client %s instance for PF id %d request an unsupported reset: %d.\n",
827                          client->name, pf->hw.pf_id, reset_level);
828                 break;
829         }
830
831         i40e_service_event_schedule(pf);
832 }
833
834 /**
835  * i40e_client_update_vsi_ctxt
836  * @ldev: pointer to L2 context.
837  * @client: Client pointer.
838  * @is_vf: if this for the VF
839  * @vf_id: if is_vf true this carries the vf_id
840  * @flag: Any device level setting that needs to be done for PE
841  * @valid_flag: Bits in this match up and enable changing of flag bits
842  *
843  * Return 0 on success or < 0 on error
844  **/
845 static int i40e_client_update_vsi_ctxt(struct i40e_info *ldev,
846                                        struct i40e_client *client,
847                                        bool is_vf, u32 vf_id,
848                                        u32 flag, u32 valid_flag)
849 {
850         struct i40e_pf *pf = ldev->pf;
851         struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
852         struct i40e_vsi_context ctxt;
853         bool update = true;
854         i40e_status err;
855
856         /* TODO: for now do not allow setting VF's VSI setting */
857         if (is_vf)
858                 return -EINVAL;
859
860         ctxt.seid = pf->main_vsi_seid;
861         ctxt.pf_num = pf->hw.pf_id;
862         err = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
863         ctxt.flags = I40E_AQ_VSI_TYPE_PF;
864         if (err) {
865                 dev_info(&pf->pdev->dev,
866                          "couldn't get PF vsi config, err %s aq_err %s\n",
867                          i40e_stat_str(&pf->hw, err),
868                          i40e_aq_str(&pf->hw,
869                                      pf->hw.aq.asq_last_status));
870                 return -ENOENT;
871         }
872
873         if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE) &&
874             (flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE)) {
875                 ctxt.info.valid_sections =
876                         cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
877                 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
878         } else if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE) &&
879                   !(flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE)) {
880                 ctxt.info.valid_sections =
881                         cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
882                 ctxt.info.queueing_opt_flags &= ~I40E_AQ_VSI_QUE_OPT_TCP_ENA;
883         } else {
884                 update = false;
885                 dev_warn(&pf->pdev->dev,
886                          "Client %s instance for PF id %d request an unsupported Config: %x.\n",
887                          client->name, pf->hw.pf_id, flag);
888         }
889
890         if (update) {
891                 err = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
892                 if (err) {
893                         dev_info(&pf->pdev->dev,
894                                  "update VSI ctxt for PE failed, err %s aq_err %s\n",
895                                  i40e_stat_str(&pf->hw, err),
896                                  i40e_aq_str(&pf->hw,
897                                              pf->hw.aq.asq_last_status));
898                 }
899         }
900         return err;
901 }
902
903 /**
904  * i40e_register_client - Register a i40e client driver with the L2 driver
905  * @client: pointer to the i40e_client struct
906  *
907  * Returns 0 on success or non-0 on error
908  **/
909 int i40e_register_client(struct i40e_client *client)
910 {
911         int ret = 0;
912         enum i40e_vsi_type vsi_type;
913
914         if (!client) {
915                 ret = -EIO;
916                 goto out;
917         }
918
919         if (strlen(client->name) == 0) {
920                 pr_info("i40e: Failed to register client with no name\n");
921                 ret = -EIO;
922                 goto out;
923         }
924
925         mutex_lock(&i40e_client_mutex);
926         if (i40e_client_is_registered(client)) {
927                 pr_info("i40e: Client %s has already been registered!\n",
928                         client->name);
929                 mutex_unlock(&i40e_client_mutex);
930                 ret = -EEXIST;
931                 goto out;
932         }
933
934         if ((client->version.major != I40E_CLIENT_VERSION_MAJOR) ||
935             (client->version.minor != I40E_CLIENT_VERSION_MINOR)) {
936                 pr_info("i40e: Failed to register client %s due to mismatched client interface version\n",
937                         client->name);
938                 pr_info("Client is using version: %02d.%02d.%02d while LAN driver supports %s\n",
939                         client->version.major, client->version.minor,
940                         client->version.build,
941                         i40e_client_interface_version_str);
942                 mutex_unlock(&i40e_client_mutex);
943                 ret = -EIO;
944                 goto out;
945         }
946
947         vsi_type = i40e_client_type_to_vsi_type(client->type);
948         if (vsi_type == I40E_VSI_TYPE_UNKNOWN) {
949                 pr_info("i40e: Failed to register client %s due to unknown client type %d\n",
950                         client->name, client->type);
951                 mutex_unlock(&i40e_client_mutex);
952                 ret = -EIO;
953                 goto out;
954         }
955         list_add(&client->list, &i40e_clients);
956         set_bit(__I40E_CLIENT_REGISTERED, &client->state);
957         mutex_unlock(&i40e_client_mutex);
958
959         if (i40e_client_prepare(client)) {
960                 ret = -EIO;
961                 goto out;
962         }
963
964         pr_info("i40e: Registered client %s with return code %d\n",
965                 client->name, ret);
966 out:
967         return ret;
968 }
969 EXPORT_SYMBOL(i40e_register_client);
970
971 /**
972  * i40e_unregister_client - Unregister a i40e client driver with the L2 driver
973  * @client: pointer to the i40e_client struct
974  *
975  * Returns 0 on success or non-0 on error
976  **/
977 int i40e_unregister_client(struct i40e_client *client)
978 {
979         int ret = 0;
980
981         /* When a unregister request comes through we would have to send
982          * a close for each of the client instances that were opened.
983          * client_release function is called to handle this.
984          */
985         mutex_lock(&i40e_client_mutex);
986         if (!client || i40e_client_release(client)) {
987                 ret = -EIO;
988                 goto out;
989         }
990
991         /* TODO: check if device is in reset, or if that matters? */
992         if (!i40e_client_is_registered(client)) {
993                 pr_info("i40e: Client %s has not been registered\n",
994                         client->name);
995                 mutex_unlock(&i40e_client_mutex);
996                 ret = -ENODEV;
997                 goto out;
998         }
999         if (atomic_read(&client->ref_cnt) == 0) {
1000                 clear_bit(__I40E_CLIENT_REGISTERED, &client->state);
1001                 list_del(&client->list);
1002                 pr_info("i40e: Unregistered client %s with return code %d\n",
1003                         client->name, ret);
1004         } else {
1005                 ret = I40E_ERR_NOT_READY;
1006                 pr_err("i40e: Client %s failed unregister - client has open instances\n",
1007                        client->name);
1008         }
1009
1010 out:
1011         mutex_unlock(&i40e_client_mutex);
1012         return ret;
1013 }
1014 EXPORT_SYMBOL(i40e_unregister_client);