qlcnic: VF-PF communication channel implementation
[pandora-kernel.git] / drivers / net / ethernet / qlogic / qlcnic / qlcnic_sriov_pf.c
1 /*
2  * QLogic qlcnic NIC Driver
3  * Copyright (c) 2009-2013 QLogic Corporation
4  *
5  * See LICENSE.qlcnic for copyright and licensing details.
6  */
7
8 #include "qlcnic_sriov.h"
9 #include "qlcnic.h"
10 #include <linux/types.h>
11
12 #define QLCNIC_SRIOV_VF_MAX_MAC 1
13
14 static int qlcnic_sriov_pf_get_vport_handle(struct qlcnic_adapter *, u8);
15
16 struct qlcnic_sriov_cmd_handler {
17         int (*fn) (struct qlcnic_bc_trans *, struct qlcnic_cmd_args *);
18 };
19
20 static int qlcnic_sriov_pf_set_vport_info(struct qlcnic_adapter *adapter,
21                                           struct qlcnic_info *npar_info,
22                                           u16 vport_id)
23 {
24         struct qlcnic_cmd_args cmd;
25         int err;
26
27         if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_SET_NIC_INFO))
28                 return -ENOMEM;
29
30         cmd.req.arg[1] = (vport_id << 16) | 0x1;
31         cmd.req.arg[2] = npar_info->bit_offsets;
32         cmd.req.arg[2] |= npar_info->min_tx_bw << 16;
33         cmd.req.arg[3] = npar_info->max_tx_bw | (npar_info->max_tx_ques << 16);
34         cmd.req.arg[4] = npar_info->max_tx_mac_filters;
35         cmd.req.arg[4] |= npar_info->max_rx_mcast_mac_filters << 16;
36         cmd.req.arg[5] = npar_info->max_rx_ucast_mac_filters |
37                          (npar_info->max_rx_ip_addr << 16);
38         cmd.req.arg[6] = npar_info->max_rx_lro_flow |
39                          (npar_info->max_rx_status_rings << 16);
40         cmd.req.arg[7] = npar_info->max_rx_buf_rings |
41                          (npar_info->max_rx_ques << 16);
42         cmd.req.arg[8] = npar_info->max_tx_vlan_keys;
43         cmd.req.arg[8] |= npar_info->max_local_ipv6_addrs << 16;
44         cmd.req.arg[9] = npar_info->max_remote_ipv6_addrs;
45
46         err = qlcnic_issue_cmd(adapter, &cmd);
47         if (err)
48                 dev_err(&adapter->pdev->dev,
49                         "Failed to set vport info, err=%d\n", err);
50
51         qlcnic_free_mbx_args(&cmd);
52         return err;
53 }
54
55 static int qlcnic_sriov_pf_cal_res_limit(struct qlcnic_adapter *adapter,
56                                          struct qlcnic_info *info, u16 func)
57 {
58         struct qlcnic_sriov *sriov = adapter->ahw->sriov;
59         struct qlcnic_resources *res = &sriov->ff_max;
60         int ret = -EIO, vpid;
61         u32 temp, num_vf_macs, num_vfs, max;
62
63         vpid = qlcnic_sriov_pf_get_vport_handle(adapter, func);
64         if (vpid < 0)
65                 return -EINVAL;
66
67         num_vfs = sriov->num_vfs;
68         max = num_vfs + 1;
69         info->bit_offsets = 0xffff;
70         info->min_tx_bw = 0;
71         info->max_tx_bw = MAX_BW;
72         info->max_tx_ques = res->num_tx_queues / max;
73         info->max_rx_mcast_mac_filters = res->num_rx_mcast_mac_filters;
74         num_vf_macs = QLCNIC_SRIOV_VF_MAX_MAC;
75
76         if (adapter->ahw->pci_func == func) {
77                 temp = res->num_rx_mcast_mac_filters - (num_vfs * num_vf_macs);
78                 info->max_rx_ucast_mac_filters = temp;
79                 temp = res->num_tx_mac_filters - (num_vfs * num_vf_macs);
80                 info->max_tx_mac_filters = temp;
81         } else {
82                 info->max_rx_ucast_mac_filters = num_vf_macs;
83                 info->max_tx_mac_filters = num_vf_macs;
84         }
85
86         info->max_rx_ip_addr = res->num_destip / max;
87         info->max_rx_status_rings = res->num_rx_status_rings / max;
88         info->max_rx_buf_rings = res->num_rx_buf_rings / max;
89         info->max_rx_ques = res->num_rx_queues / max;
90         info->max_rx_lro_flow = res->num_lro_flows_supported / max;
91         info->max_tx_vlan_keys = res->num_txvlan_keys;
92         info->max_local_ipv6_addrs = res->max_local_ipv6_addrs;
93         info->max_remote_ipv6_addrs = res->max_remote_ipv6_addrs;
94
95         ret = qlcnic_sriov_pf_set_vport_info(adapter, info, vpid);
96         if (ret)
97                 return ret;
98
99         return 0;
100 }
101
102 static void qlcnic_sriov_pf_set_ff_max_res(struct qlcnic_adapter *adapter,
103                                            struct qlcnic_info *info)
104 {
105         struct qlcnic_resources *ff_max = &adapter->ahw->sriov->ff_max;
106
107         ff_max->num_tx_mac_filters = info->max_tx_mac_filters;
108         ff_max->num_rx_ucast_mac_filters = info->max_rx_ucast_mac_filters;
109         ff_max->num_rx_mcast_mac_filters = info->max_rx_mcast_mac_filters;
110         ff_max->num_txvlan_keys = info->max_tx_vlan_keys;
111         ff_max->num_rx_queues = info->max_rx_ques;
112         ff_max->num_tx_queues = info->max_tx_ques;
113         ff_max->num_lro_flows_supported = info->max_rx_lro_flow;
114         ff_max->num_destip = info->max_rx_ip_addr;
115         ff_max->num_rx_buf_rings = info->max_rx_buf_rings;
116         ff_max->num_rx_status_rings = info->max_rx_status_rings;
117         ff_max->max_remote_ipv6_addrs = info->max_remote_ipv6_addrs;
118         ff_max->max_local_ipv6_addrs = info->max_local_ipv6_addrs;
119 }
120
121 static int qlcnic_sriov_get_pf_info(struct qlcnic_adapter *adapter,
122                                     struct qlcnic_info *npar_info)
123 {
124         int err;
125         struct qlcnic_cmd_args cmd;
126
127         if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_GET_NIC_INFO))
128                 return -ENOMEM;
129
130         cmd.req.arg[1] = 0x2;
131         err = qlcnic_issue_cmd(adapter, &cmd);
132         if (err) {
133                 dev_err(&adapter->pdev->dev,
134                         "Failed to get PF info, err=%d\n", err);
135                 goto out;
136         }
137
138         npar_info->total_pf = cmd.rsp.arg[2] & 0xff;
139         npar_info->total_rss_engines = (cmd.rsp.arg[2] >> 8) & 0xff;
140         npar_info->max_vports = MSW(cmd.rsp.arg[2]);
141         npar_info->max_tx_ques =  LSW(cmd.rsp.arg[3]);
142         npar_info->max_tx_mac_filters = MSW(cmd.rsp.arg[3]);
143         npar_info->max_rx_mcast_mac_filters = LSW(cmd.rsp.arg[4]);
144         npar_info->max_rx_ucast_mac_filters = MSW(cmd.rsp.arg[4]);
145         npar_info->max_rx_ip_addr = LSW(cmd.rsp.arg[5]);
146         npar_info->max_rx_lro_flow = MSW(cmd.rsp.arg[5]);
147         npar_info->max_rx_status_rings = LSW(cmd.rsp.arg[6]);
148         npar_info->max_rx_buf_rings = MSW(cmd.rsp.arg[6]);
149         npar_info->max_rx_ques = LSW(cmd.rsp.arg[7]);
150         npar_info->max_tx_vlan_keys = MSW(cmd.rsp.arg[7]);
151         npar_info->max_local_ipv6_addrs = LSW(cmd.rsp.arg[8]);
152         npar_info->max_remote_ipv6_addrs = MSW(cmd.rsp.arg[8]);
153
154         dev_info(&adapter->pdev->dev,
155                  "\n\ttotal_pf: %d,\n"
156                  "\n\ttotal_rss_engines: %d max_vports: %d max_tx_ques %d,\n"
157                  "\tmax_tx_mac_filters: %d max_rx_mcast_mac_filters: %d,\n"
158                  "\tmax_rx_ucast_mac_filters: 0x%x, max_rx_ip_addr: %d,\n"
159                  "\tmax_rx_lro_flow: %d max_rx_status_rings: %d,\n"
160                  "\tmax_rx_buf_rings: %d, max_rx_ques: %d, max_tx_vlan_keys %d\n"
161                  "\tmax_local_ipv6_addrs: %d, max_remote_ipv6_addrs: %d\n",
162                  npar_info->total_pf, npar_info->total_rss_engines,
163                  npar_info->max_vports, npar_info->max_tx_ques,
164                  npar_info->max_tx_mac_filters,
165                  npar_info->max_rx_mcast_mac_filters,
166                  npar_info->max_rx_ucast_mac_filters, npar_info->max_rx_ip_addr,
167                  npar_info->max_rx_lro_flow, npar_info->max_rx_status_rings,
168                  npar_info->max_rx_buf_rings, npar_info->max_rx_ques,
169                  npar_info->max_tx_vlan_keys, npar_info->max_local_ipv6_addrs,
170                  npar_info->max_remote_ipv6_addrs);
171
172 out:
173         qlcnic_free_mbx_args(&cmd);
174         return err;
175 }
176
177 static void qlcnic_sriov_pf_reset_vport_handle(struct qlcnic_adapter *adapter,
178                                                u8 func)
179 {
180         struct qlcnic_sriov  *sriov = adapter->ahw->sriov;
181         struct qlcnic_vport *vp;
182         int index;
183
184         if (adapter->ahw->pci_func == func) {
185                 sriov->vp_handle = 0;
186         } else {
187                 index = qlcnic_sriov_func_to_index(adapter, func);
188                 if (index < 0)
189                         return;
190                 vp = sriov->vf_info[index].vp;
191                 vp->handle = 0;
192         }
193 }
194
195 static void qlcnic_sriov_pf_set_vport_handle(struct qlcnic_adapter *adapter,
196                                              u16 vport_handle, u8 func)
197 {
198         struct qlcnic_sriov  *sriov = adapter->ahw->sriov;
199         struct qlcnic_vport *vp;
200         int index;
201
202         if (adapter->ahw->pci_func == func) {
203                 sriov->vp_handle = vport_handle;
204         } else {
205                 index = qlcnic_sriov_func_to_index(adapter, func);
206                 if (index < 0)
207                         return;
208                 vp = sriov->vf_info[index].vp;
209                 vp->handle = vport_handle;
210         }
211 }
212
213 static int qlcnic_sriov_pf_get_vport_handle(struct qlcnic_adapter *adapter,
214                                             u8 func)
215 {
216         struct qlcnic_sriov  *sriov = adapter->ahw->sriov;
217         struct qlcnic_vf_info *vf_info;
218         int index;
219
220         if (adapter->ahw->pci_func == func) {
221                 return sriov->vp_handle;
222         } else {
223                 index = qlcnic_sriov_func_to_index(adapter, func);
224                 if (index >= 0) {
225                         vf_info = &sriov->vf_info[index];
226                         return vf_info->vp->handle;
227                 }
228         }
229
230         return -EINVAL;
231 }
232
233 static int qlcnic_sriov_pf_config_vport(struct qlcnic_adapter *adapter,
234                                         u8 flag, u16 func)
235 {
236         struct qlcnic_cmd_args cmd;
237         int ret;
238         int vpid;
239
240         if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_CONFIG_VPORT))
241                 return -ENOMEM;
242
243         if (flag) {
244                 cmd.req.arg[3] = func << 8;
245         } else {
246                 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, func);
247                 if (vpid < 0) {
248                         ret = -EINVAL;
249                         goto out;
250                 }
251                 cmd.req.arg[3] = ((vpid & 0xffff) << 8) | 1;
252         }
253
254         ret = qlcnic_issue_cmd(adapter, &cmd);
255         if (ret) {
256                 dev_err(&adapter->pdev->dev,
257                         "Failed %s vport, err %d for func 0x%x\n",
258                         (flag ? "enable" : "disable"), ret, func);
259                 goto out;
260         }
261
262         if (flag) {
263                 vpid = cmd.rsp.arg[2] & 0xffff;
264                 qlcnic_sriov_pf_set_vport_handle(adapter, vpid, func);
265         } else {
266                 qlcnic_sriov_pf_reset_vport_handle(adapter, func);
267         }
268
269 out:
270         qlcnic_free_mbx_args(&cmd);
271         return ret;
272 }
273
274 static int qlcnic_sriov_pf_cfg_eswitch(struct qlcnic_adapter *adapter,
275                                        u8 func, u8 enable)
276 {
277         struct qlcnic_cmd_args cmd;
278         int err = -EIO;
279
280         if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_TOGGLE_ESWITCH))
281                 return -ENOMEM;
282
283         cmd.req.arg[0] |= (3 << 29);
284         cmd.req.arg[1] = ((func & 0xf) << 2) | BIT_6 | BIT_1;
285         if (enable)
286                 cmd.req.arg[1] |= BIT_0;
287
288         err = qlcnic_issue_cmd(adapter, &cmd);
289
290         if (err != QLCNIC_RCODE_SUCCESS) {
291                 dev_err(&adapter->pdev->dev,
292                         "Failed to enable sriov eswitch%d\n", err);
293                 err = -EIO;
294         }
295
296         qlcnic_free_mbx_args(&cmd);
297         return err;
298 }
299
300 void qlcnic_sriov_pf_cleanup(struct qlcnic_adapter *adapter)
301 {
302         u8 func = adapter->ahw->pci_func;
303
304         if (!qlcnic_sriov_enable_check(adapter))
305                 return;
306
307         qlcnic_sriov_cfg_bc_intr(adapter, 0);
308         qlcnic_sriov_pf_config_vport(adapter, 0, func);
309         qlcnic_sriov_pf_cfg_eswitch(adapter, func, 0);
310         __qlcnic_sriov_cleanup(adapter);
311         adapter->ahw->op_mode = QLCNIC_MGMT_FUNC;
312         clear_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
313 }
314
315 void qlcnic_sriov_pf_disable(struct qlcnic_adapter *adapter)
316 {
317         if (!qlcnic_sriov_pf_check(adapter))
318                 return;
319
320         if (!qlcnic_sriov_enable_check(adapter))
321                 return;
322
323         pci_disable_sriov(adapter->pdev);
324         netdev_info(adapter->netdev,
325                     "SR-IOV is disabled successfully on port %d\n",
326                     adapter->portnum);
327 }
328
329 static int qlcnic_pci_sriov_disable(struct qlcnic_adapter *adapter)
330 {
331         struct net_device *netdev = adapter->netdev;
332
333         if (netif_running(netdev))
334                 __qlcnic_down(adapter, netdev);
335
336         qlcnic_sriov_pf_disable(adapter);
337
338         qlcnic_sriov_pf_cleanup(adapter);
339
340         /* After disabling SRIOV re-init the driver in default mode
341            configure opmode based on op_mode of function
342          */
343         if (qlcnic_83xx_configure_opmode(adapter))
344                 return -EIO;
345
346         if (netif_running(netdev))
347                 __qlcnic_up(adapter, netdev);
348
349         return 0;
350 }
351
352 static int qlcnic_sriov_pf_init(struct qlcnic_adapter *adapter)
353 {
354         struct qlcnic_hardware_context *ahw = adapter->ahw;
355         struct qlcnic_info nic_info, pf_info, vp_info;
356         int err;
357         u8 func = ahw->pci_func;
358
359         if (!qlcnic_sriov_enable_check(adapter))
360                 return 0;
361
362         err = qlcnic_sriov_pf_cfg_eswitch(adapter, func, 1);
363         if (err)
364                 goto clear_sriov_enable;
365
366         err = qlcnic_sriov_pf_config_vport(adapter, 1, func);
367         if (err)
368                 goto disable_eswitch;
369
370         err = qlcnic_sriov_get_pf_info(adapter, &pf_info);
371         if (err)
372                 goto delete_vport;
373
374         qlcnic_sriov_pf_set_ff_max_res(adapter, &pf_info);
375
376         err = qlcnic_get_nic_info(adapter, &nic_info, func);
377         if (err)
378                 goto delete_vport;
379
380         err = qlcnic_sriov_pf_cal_res_limit(adapter, &vp_info, func);
381         if (err)
382                 goto delete_vport;
383
384         err = qlcnic_sriov_cfg_bc_intr(adapter, 1);
385         if (err)
386                 goto delete_vport;
387
388         ahw->physical_port = (u8) nic_info.phys_port;
389         ahw->switch_mode = nic_info.switch_mode;
390         ahw->max_mtu = nic_info.max_mtu;
391         ahw->capabilities = nic_info.capabilities;
392         ahw->nic_mode = QLC_83XX_SRIOV_MODE;
393         return err;
394
395 delete_vport:
396         qlcnic_sriov_pf_config_vport(adapter, 0, func);
397
398 disable_eswitch:
399         qlcnic_sriov_pf_cfg_eswitch(adapter, func, 0);
400
401 clear_sriov_enable:
402         __qlcnic_sriov_cleanup(adapter);
403         adapter->ahw->op_mode = QLCNIC_MGMT_FUNC;
404         clear_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
405         return err;
406 }
407
408 static int qlcnic_sriov_pf_enable(struct qlcnic_adapter *adapter, int num_vfs)
409 {
410         int err;
411
412         if (!qlcnic_sriov_enable_check(adapter))
413                 return 0;
414
415         err = pci_enable_sriov(adapter->pdev, num_vfs);
416         if (err)
417                 qlcnic_sriov_pf_cleanup(adapter);
418
419         return err;
420 }
421
422 static int __qlcnic_pci_sriov_enable(struct qlcnic_adapter *adapter,
423                                      int num_vfs)
424 {
425         int err = 0;
426
427         set_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
428         adapter->ahw->op_mode = QLCNIC_SRIOV_PF_FUNC;
429
430         if (qlcnic_sriov_init(adapter, num_vfs)) {
431                 clear_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
432                 adapter->ahw->op_mode = QLCNIC_MGMT_FUNC;
433                 return -EIO;
434         }
435
436         if (qlcnic_sriov_pf_init(adapter))
437                 return -EIO;
438
439         err = qlcnic_sriov_pf_enable(adapter, num_vfs);
440         return err;
441 }
442
443 static int qlcnic_pci_sriov_enable(struct qlcnic_adapter *adapter, int num_vfs)
444 {
445         struct net_device *netdev = adapter->netdev;
446         int err;
447
448         if (!(adapter->flags & QLCNIC_MSIX_ENABLED)) {
449                 netdev_err(netdev,
450                            "SR-IOV cannot be enabled, when legacy interrupts are enabled\n");
451                 return -EIO;
452         }
453
454         if (netif_running(netdev))
455                 __qlcnic_down(adapter, netdev);
456
457         err = __qlcnic_pci_sriov_enable(adapter, num_vfs);
458         if (err) {
459                 netdev_info(netdev, "Failed to enable SR-IOV on port %d\n",
460                             adapter->portnum);
461
462                 if (qlcnic_83xx_configure_opmode(adapter))
463                         goto error;
464         } else {
465                 netdev_info(adapter->netdev,
466                             "SR-IOV is enabled successfully on port %d\n",
467                             adapter->portnum);
468         }
469         if (netif_running(netdev))
470                 __qlcnic_up(adapter, netdev);
471
472 error:
473         return err;
474 }
475
476 int qlcnic_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
477 {
478         struct qlcnic_adapter *adapter = pci_get_drvdata(dev);
479         int err;
480
481         if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
482                 return -EBUSY;
483
484         if (num_vfs == 0)
485                 err = qlcnic_pci_sriov_disable(adapter);
486         else
487                 err = qlcnic_pci_sriov_enable(adapter, num_vfs);
488
489         clear_bit(__QLCNIC_RESETTING, &adapter->state);
490         return err;
491 }
492
493 static int qlcnic_sriov_set_vf_vport_info(struct qlcnic_adapter *adapter,
494                                           u16 func)
495 {
496         struct qlcnic_info defvp_info;
497         int err;
498
499         err = qlcnic_sriov_pf_cal_res_limit(adapter, &defvp_info, func);
500         if (err)
501                 return -EIO;
502
503         return 0;
504 }
505
506 static int qlcnic_sriov_pf_channel_cfg_cmd(struct qlcnic_bc_trans *trans,
507                                            struct qlcnic_cmd_args *cmd)
508 {
509         struct qlcnic_vf_info *vf = trans->vf;
510         struct qlcnic_adapter *adapter = vf->adapter;
511         int err;
512         u16 func = vf->pci_func;
513
514         cmd->rsp.arg[0] = trans->req_hdr->cmd_op;
515         cmd->rsp.arg[0] |= (1 << 16);
516
517         if (trans->req_hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT) {
518                 err = qlcnic_sriov_pf_config_vport(adapter, 1, func);
519                 if (!err) {
520                         err = qlcnic_sriov_set_vf_vport_info(adapter, func);
521                         if (err)
522                                 qlcnic_sriov_pf_config_vport(adapter, 0, func);
523                 }
524         } else {
525                 err = qlcnic_sriov_pf_config_vport(adapter, 0, func);
526         }
527
528         if (err)
529                 goto err_out;
530
531         cmd->rsp.arg[0] |= (1 << 25);
532
533         if (trans->req_hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT)
534                 set_bit(QLC_BC_VF_STATE, &vf->state);
535         else
536                 clear_bit(QLC_BC_VF_STATE, &vf->state);
537
538         return err;
539
540 err_out:
541         cmd->rsp.arg[0] |= (2 << 25);
542         return err;
543 }
544
545 static const struct qlcnic_sriov_cmd_handler qlcnic_pf_bc_cmd_hdlr[] = {
546         [QLCNIC_BC_CMD_CHANNEL_INIT] = {&qlcnic_sriov_pf_channel_cfg_cmd},
547         [QLCNIC_BC_CMD_CHANNEL_TERM] = {&qlcnic_sriov_pf_channel_cfg_cmd},
548 };
549
550 void qlcnic_sriov_pf_process_bc_cmd(struct qlcnic_adapter *adapter,
551                                     struct qlcnic_bc_trans *trans,
552                                     struct qlcnic_cmd_args *cmd)
553 {
554         u8 size, cmd_op;
555
556         cmd_op = trans->req_hdr->cmd_op;
557
558         if (trans->req_hdr->op_type == QLC_BC_CMD) {
559                 size = ARRAY_SIZE(qlcnic_pf_bc_cmd_hdlr);
560                 if (cmd_op < size) {
561                         qlcnic_pf_bc_cmd_hdlr[cmd_op].fn(trans, cmd);
562                         return;
563                 }
564         }
565
566         cmd->rsp.arg[0] |= (0x9 << 25);
567 }