1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * QLogic qlcnic NIC Driver
4 * Copyright (c) 2009-2013 QLogic Corporation
5 */
6
7 #include <linux/vmalloc.h>
8 #include <linux/interrupt.h>
9 #include <linux/swab.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/if_vlan.h>
12 #include <net/ip.h>
13 #include <linux/ipv6.h>
14 #include <linux/inetdevice.h>
15 #include <linux/aer.h>
16 #include <linux/log2.h>
17 #include <linux/pci.h>
18 #include <net/vxlan.h>
19
20 #include "qlcnic.h"
21 #include "qlcnic_sriov.h"
22 #include "qlcnic_hw.h"
23
24 MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
25 MODULE_LICENSE("GPL");
26 MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
27 MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
28
29 char qlcnic_driver_name[] = "qlcnic";
30 static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
31 "Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
32
33 static int qlcnic_mac_learn;
34 module_param(qlcnic_mac_learn, int, 0444);
35 MODULE_PARM_DESC(qlcnic_mac_learn,
36 "Mac Filter (0=learning is disabled, 1=Driver learning is enabled, 2=FDB learning is enabled)");
37
38 int qlcnic_use_msi = 1;
39 MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled)");
40 module_param_named(use_msi, qlcnic_use_msi, int, 0444);
41
42 int qlcnic_use_msi_x = 1;
43 MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled)");
44 module_param_named(use_msi_x, qlcnic_use_msi_x, int, 0444);
45
46 int qlcnic_auto_fw_reset = 1;
47 MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled)");
48 module_param_named(auto_fw_reset, qlcnic_auto_fw_reset, int, 0644);
49
50 int qlcnic_load_fw_file;
51 MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file, 2=POST in fast mode, 3= POST in medium mode, 4=POST in slow mode)");
52 module_param_named(load_fw_file, qlcnic_load_fw_file, int, 0444);
53
54 static int qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
55 static void qlcnic_remove(struct pci_dev *pdev);
56 static int qlcnic_open(struct net_device *netdev);
57 static int qlcnic_close(struct net_device *netdev);
58 static void qlcnic_tx_timeout(struct net_device *netdev, unsigned int txqueue);
59 static void qlcnic_attach_work(struct work_struct *work);
60 static void qlcnic_fwinit_work(struct work_struct *work);
61
62 static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
63 static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
64
65 static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
66 static irqreturn_t qlcnic_intr(int irq, void *data);
67 static irqreturn_t qlcnic_msi_intr(int irq, void *data);
68 static irqreturn_t qlcnic_msix_intr(int irq, void *data);
69 static irqreturn_t qlcnic_msix_tx_intr(int irq, void *data);
70
71 static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
72 static int qlcnic_start_firmware(struct qlcnic_adapter *);
73
74 static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter);
75 static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
76 static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
77 static int qlcnic_vlan_rx_add(struct net_device *, __be16, u16);
78 static int qlcnic_vlan_rx_del(struct net_device *, __be16, u16);
79
80 static int qlcnic_82xx_setup_intr(struct qlcnic_adapter *);
81 static void qlcnic_82xx_dev_request_reset(struct qlcnic_adapter *, u32);
82 static irqreturn_t qlcnic_82xx_clear_legacy_intr(struct qlcnic_adapter *);
83 static pci_ers_result_t qlcnic_82xx_io_slot_reset(struct pci_dev *);
84 static int qlcnic_82xx_start_firmware(struct qlcnic_adapter *);
85 static void qlcnic_82xx_io_resume(struct pci_dev *);
86 static void qlcnic_82xx_set_mac_filter_count(struct qlcnic_adapter *);
87 static pci_ers_result_t qlcnic_82xx_io_error_detected(struct pci_dev *,
88 pci_channel_state_t);
qlcnic_vlan_tx_check(struct qlcnic_adapter * adapter)89 static u32 qlcnic_vlan_tx_check(struct qlcnic_adapter *adapter)
90 {
91 struct qlcnic_hardware_context *ahw = adapter->ahw;
92
93 if (adapter->pdev->device == PCI_DEVICE_ID_QLOGIC_QLE824X)
94 return ahw->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX;
95 else
96 return 1;
97 }
98
99 /* PCI Device ID Table */
100 #define ENTRY(device) \
101 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
102 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
103
104 static const struct pci_device_id qlcnic_pci_tbl[] = {
105 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
106 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE834X),
107 ENTRY(PCI_DEVICE_ID_QLOGIC_VF_QLE834X),
108 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE8830),
109 ENTRY(PCI_DEVICE_ID_QLOGIC_VF_QLE8C30),
110 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE844X),
111 ENTRY(PCI_DEVICE_ID_QLOGIC_VF_QLE844X),
112 {0,}
113 };
114
115 MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
116
117
qlcnic_update_cmd_producer(struct qlcnic_host_tx_ring * tx_ring)118 inline void qlcnic_update_cmd_producer(struct qlcnic_host_tx_ring *tx_ring)
119 {
120 writel(tx_ring->producer, tx_ring->crb_cmd_producer);
121 }
122
123 static const u32 msi_tgt_status[8] = {
124 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
125 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
126 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
127 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
128 };
129
130 static const u32 qlcnic_reg_tbl[] = {
131 0x1B20A8, /* PEG_HALT_STAT1 */
132 0x1B20AC, /* PEG_HALT_STAT2 */
133 0x1B20B0, /* FW_HEARTBEAT */
134 0x1B2100, /* LOCK ID */
135 0x1B2128, /* FW_CAPABILITIES */
136 0x1B2138, /* drv active */
137 0x1B2140, /* dev state */
138 0x1B2144, /* drv state */
139 0x1B2148, /* drv scratch */
140 0x1B214C, /* dev partition info */
141 0x1B2174, /* drv idc ver */
142 0x1B2150, /* fw version major */
143 0x1B2154, /* fw version minor */
144 0x1B2158, /* fw version sub */
145 0x1B219C, /* npar state */
146 0x1B21FC, /* FW_IMG_VALID */
147 0x1B2250, /* CMD_PEG_STATE */
148 0x1B233C, /* RCV_PEG_STATE */
149 0x1B23B4, /* ASIC TEMP */
150 0x1B216C, /* FW api */
151 0x1B2170, /* drv op mode */
152 0x13C010, /* flash lock */
153 0x13C014, /* flash unlock */
154 };
155
156 static const struct qlcnic_board_info qlcnic_boards[] = {
157 { PCI_VENDOR_ID_QLOGIC,
158 PCI_DEVICE_ID_QLOGIC_QLE844X,
159 0x0,
160 0x0,
161 "8400 series 10GbE Converged Network Adapter (TCP/IP Networking)" },
162 { PCI_VENDOR_ID_QLOGIC,
163 PCI_DEVICE_ID_QLOGIC_QLE834X,
164 PCI_VENDOR_ID_QLOGIC,
165 0x24e,
166 "8300 Series Dual Port 10GbE Converged Network Adapter "
167 "(TCP/IP Networking)" },
168 { PCI_VENDOR_ID_QLOGIC,
169 PCI_DEVICE_ID_QLOGIC_QLE834X,
170 PCI_VENDOR_ID_QLOGIC,
171 0x243,
172 "8300 Series Single Port 10GbE Converged Network Adapter "
173 "(TCP/IP Networking)" },
174 { PCI_VENDOR_ID_QLOGIC,
175 PCI_DEVICE_ID_QLOGIC_QLE834X,
176 PCI_VENDOR_ID_QLOGIC,
177 0x24a,
178 "8300 Series Dual Port 10GbE Converged Network Adapter "
179 "(TCP/IP Networking)" },
180 { PCI_VENDOR_ID_QLOGIC,
181 PCI_DEVICE_ID_QLOGIC_QLE834X,
182 PCI_VENDOR_ID_QLOGIC,
183 0x246,
184 "8300 Series Dual Port 10GbE Converged Network Adapter "
185 "(TCP/IP Networking)" },
186 { PCI_VENDOR_ID_QLOGIC,
187 PCI_DEVICE_ID_QLOGIC_QLE834X,
188 PCI_VENDOR_ID_QLOGIC,
189 0x252,
190 "8300 Series Dual Port 10GbE Converged Network Adapter "
191 "(TCP/IP Networking)" },
192 { PCI_VENDOR_ID_QLOGIC,
193 PCI_DEVICE_ID_QLOGIC_QLE834X,
194 PCI_VENDOR_ID_QLOGIC,
195 0x26e,
196 "8300 Series Dual Port 10GbE Converged Network Adapter "
197 "(TCP/IP Networking)" },
198 { PCI_VENDOR_ID_QLOGIC,
199 PCI_DEVICE_ID_QLOGIC_QLE834X,
200 PCI_VENDOR_ID_QLOGIC,
201 0x260,
202 "8300 Series Dual Port 10GbE Converged Network Adapter "
203 "(TCP/IP Networking)" },
204 { PCI_VENDOR_ID_QLOGIC,
205 PCI_DEVICE_ID_QLOGIC_QLE834X,
206 PCI_VENDOR_ID_QLOGIC,
207 0x266,
208 "8300 Series Single Port 10GbE Converged Network Adapter "
209 "(TCP/IP Networking)" },
210 { PCI_VENDOR_ID_QLOGIC,
211 PCI_DEVICE_ID_QLOGIC_QLE834X,
212 PCI_VENDOR_ID_QLOGIC,
213 0x269,
214 "8300 Series Dual Port 10GbE Converged Network Adapter "
215 "(TCP/IP Networking)" },
216 { PCI_VENDOR_ID_QLOGIC,
217 PCI_DEVICE_ID_QLOGIC_QLE834X,
218 PCI_VENDOR_ID_QLOGIC,
219 0x271,
220 "8300 Series Dual Port 10GbE Converged Network Adapter "
221 "(TCP/IP Networking)" },
222 { PCI_VENDOR_ID_QLOGIC,
223 PCI_DEVICE_ID_QLOGIC_QLE834X,
224 0x0, 0x0, "8300 Series 1/10GbE Controller" },
225 { PCI_VENDOR_ID_QLOGIC,
226 PCI_DEVICE_ID_QLOGIC_QLE8830,
227 0x0,
228 0x0,
229 "8830 Series 1/10GbE Controller" },
230 { PCI_VENDOR_ID_QLOGIC,
231 PCI_DEVICE_ID_QLOGIC_QLE824X,
232 PCI_VENDOR_ID_QLOGIC,
233 0x203,
234 "8200 Series Single Port 10GbE Converged Network Adapter"
235 "(TCP/IP Networking)" },
236 { PCI_VENDOR_ID_QLOGIC,
237 PCI_DEVICE_ID_QLOGIC_QLE824X,
238 PCI_VENDOR_ID_QLOGIC,
239 0x207,
240 "8200 Series Dual Port 10GbE Converged Network Adapter"
241 "(TCP/IP Networking)" },
242 { PCI_VENDOR_ID_QLOGIC,
243 PCI_DEVICE_ID_QLOGIC_QLE824X,
244 PCI_VENDOR_ID_QLOGIC,
245 0x20b,
246 "3200 Series Dual Port 10Gb Intelligent Ethernet Adapter" },
247 { PCI_VENDOR_ID_QLOGIC,
248 PCI_DEVICE_ID_QLOGIC_QLE824X,
249 PCI_VENDOR_ID_QLOGIC,
250 0x20c,
251 "3200 Series Quad Port 1Gb Intelligent Ethernet Adapter" },
252 { PCI_VENDOR_ID_QLOGIC,
253 PCI_DEVICE_ID_QLOGIC_QLE824X,
254 PCI_VENDOR_ID_QLOGIC,
255 0x20f,
256 "3200 Series Single Port 10Gb Intelligent Ethernet Adapter" },
257 { PCI_VENDOR_ID_QLOGIC,
258 PCI_DEVICE_ID_QLOGIC_QLE824X,
259 0x103c, 0x3733,
260 "NC523SFP 10Gb 2-port Server Adapter" },
261 { PCI_VENDOR_ID_QLOGIC,
262 PCI_DEVICE_ID_QLOGIC_QLE824X,
263 0x103c, 0x3346,
264 "CN1000Q Dual Port Converged Network Adapter" },
265 { PCI_VENDOR_ID_QLOGIC,
266 PCI_DEVICE_ID_QLOGIC_QLE824X,
267 PCI_VENDOR_ID_QLOGIC,
268 0x210,
269 "QME8242-k 10GbE Dual Port Mezzanine Card" },
270 { PCI_VENDOR_ID_QLOGIC,
271 PCI_DEVICE_ID_QLOGIC_QLE824X,
272 0x0, 0x0, "cLOM8214 1/10GbE Controller" },
273 };
274
275 #define NUM_SUPPORTED_BOARDS ARRAY_SIZE(qlcnic_boards)
276
277 static const
278 struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
279
qlcnic_alloc_sds_rings(struct qlcnic_recv_context * recv_ctx,int count)280 int qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
281 {
282 int size = sizeof(struct qlcnic_host_sds_ring) * count;
283
284 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
285
286 return recv_ctx->sds_rings == NULL;
287 }
288
qlcnic_free_sds_rings(struct qlcnic_recv_context * recv_ctx)289 void qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
290 {
291 kfree(recv_ctx->sds_rings);
292 recv_ctx->sds_rings = NULL;
293 }
294
qlcnic_read_mac_addr(struct qlcnic_adapter * adapter)295 int qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
296 {
297 struct net_device *netdev = adapter->netdev;
298 struct pci_dev *pdev = adapter->pdev;
299 u8 mac_addr[ETH_ALEN];
300 int ret;
301
302 ret = qlcnic_get_mac_address(adapter, mac_addr,
303 adapter->ahw->pci_func);
304 if (ret)
305 return ret;
306
307 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
308 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
309
310 /* set station address */
311
312 if (!is_valid_ether_addr(netdev->dev_addr))
313 dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
314 netdev->dev_addr);
315
316 return 0;
317 }
318
qlcnic_delete_adapter_mac(struct qlcnic_adapter * adapter)319 static void qlcnic_delete_adapter_mac(struct qlcnic_adapter *adapter)
320 {
321 struct qlcnic_mac_vlan_list *cur;
322 struct list_head *head;
323
324 list_for_each(head, &adapter->mac_list) {
325 cur = list_entry(head, struct qlcnic_mac_vlan_list, list);
326 if (ether_addr_equal_unaligned(adapter->mac_addr, cur->mac_addr)) {
327 qlcnic_sre_macaddr_change(adapter, cur->mac_addr,
328 0, QLCNIC_MAC_DEL);
329 list_del(&cur->list);
330 kfree(cur);
331 return;
332 }
333 }
334 }
335
qlcnic_set_mac(struct net_device * netdev,void * p)336 static int qlcnic_set_mac(struct net_device *netdev, void *p)
337 {
338 struct qlcnic_adapter *adapter = netdev_priv(netdev);
339 struct sockaddr *addr = p;
340
341 if (qlcnic_sriov_vf_check(adapter))
342 return -EINVAL;
343
344 if ((adapter->flags & QLCNIC_MAC_OVERRIDE_DISABLED))
345 return -EOPNOTSUPP;
346
347 if (!is_valid_ether_addr(addr->sa_data))
348 return -EINVAL;
349
350 if (ether_addr_equal_unaligned(adapter->mac_addr, addr->sa_data) &&
351 ether_addr_equal_unaligned(netdev->dev_addr, addr->sa_data))
352 return 0;
353
354 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
355 netif_device_detach(netdev);
356 qlcnic_napi_disable(adapter);
357 }
358
359 qlcnic_delete_adapter_mac(adapter);
360 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
361 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
362 qlcnic_set_multi(adapter->netdev);
363
364 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
365 netif_device_attach(netdev);
366 qlcnic_napi_enable(adapter);
367 }
368 return 0;
369 }
370
qlcnic_fdb_del(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * netdev,const unsigned char * addr,u16 vid)371 static int qlcnic_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
372 struct net_device *netdev,
373 const unsigned char *addr, u16 vid)
374 {
375 struct qlcnic_adapter *adapter = netdev_priv(netdev);
376 int err = -EOPNOTSUPP;
377
378 if (!adapter->fdb_mac_learn)
379 return ndo_dflt_fdb_del(ndm, tb, netdev, addr, vid);
380
381 if ((adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
382 qlcnic_sriov_check(adapter)) {
383 if (is_unicast_ether_addr(addr)) {
384 err = dev_uc_del(netdev, addr);
385 if (!err)
386 err = qlcnic_nic_del_mac(adapter, addr);
387 } else if (is_multicast_ether_addr(addr)) {
388 err = dev_mc_del(netdev, addr);
389 } else {
390 err = -EINVAL;
391 }
392 }
393 return err;
394 }
395
qlcnic_fdb_add(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * netdev,const unsigned char * addr,u16 vid,u16 flags,struct netlink_ext_ack * extack)396 static int qlcnic_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
397 struct net_device *netdev,
398 const unsigned char *addr, u16 vid, u16 flags,
399 struct netlink_ext_ack *extack)
400 {
401 struct qlcnic_adapter *adapter = netdev_priv(netdev);
402 int err = 0;
403
404 if (!adapter->fdb_mac_learn)
405 return ndo_dflt_fdb_add(ndm, tb, netdev, addr, vid, flags);
406
407 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) &&
408 !qlcnic_sriov_check(adapter)) {
409 pr_info("%s: FDB e-switch is not enabled\n", __func__);
410 return -EOPNOTSUPP;
411 }
412
413 if (ether_addr_equal(addr, adapter->mac_addr))
414 return err;
415
416 if (is_unicast_ether_addr(addr)) {
417 if (netdev_uc_count(netdev) < adapter->ahw->max_uc_count)
418 err = dev_uc_add_excl(netdev, addr);
419 else
420 err = -ENOMEM;
421 } else if (is_multicast_ether_addr(addr)) {
422 err = dev_mc_add_excl(netdev, addr);
423 } else {
424 err = -EINVAL;
425 }
426
427 return err;
428 }
429
qlcnic_fdb_dump(struct sk_buff * skb,struct netlink_callback * ncb,struct net_device * netdev,struct net_device * filter_dev,int * idx)430 static int qlcnic_fdb_dump(struct sk_buff *skb, struct netlink_callback *ncb,
431 struct net_device *netdev,
432 struct net_device *filter_dev, int *idx)
433 {
434 struct qlcnic_adapter *adapter = netdev_priv(netdev);
435 int err = 0;
436
437 if (!adapter->fdb_mac_learn)
438 return ndo_dflt_fdb_dump(skb, ncb, netdev, filter_dev, idx);
439
440 if ((adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
441 qlcnic_sriov_check(adapter))
442 err = ndo_dflt_fdb_dump(skb, ncb, netdev, filter_dev, idx);
443
444 return err;
445 }
446
qlcnic_82xx_cancel_idc_work(struct qlcnic_adapter * adapter)447 static void qlcnic_82xx_cancel_idc_work(struct qlcnic_adapter *adapter)
448 {
449 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
450 usleep_range(10000, 11000);
451
452 if (!adapter->fw_work.work.func)
453 return;
454
455 cancel_delayed_work_sync(&adapter->fw_work);
456 }
457
qlcnic_get_phys_port_id(struct net_device * netdev,struct netdev_phys_item_id * ppid)458 static int qlcnic_get_phys_port_id(struct net_device *netdev,
459 struct netdev_phys_item_id *ppid)
460 {
461 struct qlcnic_adapter *adapter = netdev_priv(netdev);
462 struct qlcnic_hardware_context *ahw = adapter->ahw;
463
464 if (!(adapter->flags & QLCNIC_HAS_PHYS_PORT_ID))
465 return -EOPNOTSUPP;
466
467 ppid->id_len = sizeof(ahw->phys_port_id);
468 memcpy(ppid->id, ahw->phys_port_id, ppid->id_len);
469
470 return 0;
471 }
472
qlcnic_udp_tunnel_sync(struct net_device * dev,unsigned int table)473 static int qlcnic_udp_tunnel_sync(struct net_device *dev, unsigned int table)
474 {
475 struct qlcnic_adapter *adapter = netdev_priv(dev);
476 struct udp_tunnel_info ti;
477 int err;
478
479 udp_tunnel_nic_get_port(dev, table, 0, &ti);
480 if (ti.port) {
481 err = qlcnic_set_vxlan_port(adapter, ntohs(ti.port));
482 if (err)
483 return err;
484 }
485
486 return qlcnic_set_vxlan_parsing(adapter, ntohs(ti.port));
487 }
488
489 static const struct udp_tunnel_nic_info qlcnic_udp_tunnels = {
490 .sync_table = qlcnic_udp_tunnel_sync,
491 .flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP,
492 .tables = {
493 { .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, },
494 },
495 };
496
qlcnic_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)497 static netdev_features_t qlcnic_features_check(struct sk_buff *skb,
498 struct net_device *dev,
499 netdev_features_t features)
500 {
501 features = vlan_features_check(skb, features);
502 return vxlan_features_check(skb, features);
503 }
504
505 static const struct net_device_ops qlcnic_netdev_ops = {
506 .ndo_open = qlcnic_open,
507 .ndo_stop = qlcnic_close,
508 .ndo_start_xmit = qlcnic_xmit_frame,
509 .ndo_get_stats = qlcnic_get_stats,
510 .ndo_validate_addr = eth_validate_addr,
511 .ndo_set_rx_mode = qlcnic_set_multi,
512 .ndo_set_mac_address = qlcnic_set_mac,
513 .ndo_change_mtu = qlcnic_change_mtu,
514 .ndo_fix_features = qlcnic_fix_features,
515 .ndo_set_features = qlcnic_set_features,
516 .ndo_tx_timeout = qlcnic_tx_timeout,
517 .ndo_vlan_rx_add_vid = qlcnic_vlan_rx_add,
518 .ndo_vlan_rx_kill_vid = qlcnic_vlan_rx_del,
519 .ndo_fdb_add = qlcnic_fdb_add,
520 .ndo_fdb_del = qlcnic_fdb_del,
521 .ndo_fdb_dump = qlcnic_fdb_dump,
522 .ndo_get_phys_port_id = qlcnic_get_phys_port_id,
523 .ndo_udp_tunnel_add = udp_tunnel_nic_add_port,
524 .ndo_udp_tunnel_del = udp_tunnel_nic_del_port,
525 .ndo_features_check = qlcnic_features_check,
526 #ifdef CONFIG_QLCNIC_SRIOV
527 .ndo_set_vf_mac = qlcnic_sriov_set_vf_mac,
528 .ndo_set_vf_rate = qlcnic_sriov_set_vf_tx_rate,
529 .ndo_get_vf_config = qlcnic_sriov_get_vf_config,
530 .ndo_set_vf_vlan = qlcnic_sriov_set_vf_vlan,
531 .ndo_set_vf_spoofchk = qlcnic_sriov_set_vf_spoofchk,
532 #endif
533 };
534
535 static const struct net_device_ops qlcnic_netdev_failed_ops = {
536 .ndo_open = qlcnic_open,
537 };
538
539 static struct qlcnic_nic_template qlcnic_ops = {
540 .config_bridged_mode = qlcnic_config_bridged_mode,
541 .config_led = qlcnic_82xx_config_led,
542 .start_firmware = qlcnic_82xx_start_firmware,
543 .request_reset = qlcnic_82xx_dev_request_reset,
544 .cancel_idc_work = qlcnic_82xx_cancel_idc_work,
545 .napi_add = qlcnic_82xx_napi_add,
546 .napi_del = qlcnic_82xx_napi_del,
547 .config_ipaddr = qlcnic_82xx_config_ipaddr,
548 .shutdown = qlcnic_82xx_shutdown,
549 .resume = qlcnic_82xx_resume,
550 .clear_legacy_intr = qlcnic_82xx_clear_legacy_intr,
551 };
552
553 struct qlcnic_nic_template qlcnic_vf_ops = {
554 .config_bridged_mode = qlcnicvf_config_bridged_mode,
555 .config_led = qlcnicvf_config_led,
556 .start_firmware = qlcnicvf_start_firmware
557 };
558
559 static struct qlcnic_hardware_ops qlcnic_hw_ops = {
560 .read_crb = qlcnic_82xx_read_crb,
561 .write_crb = qlcnic_82xx_write_crb,
562 .read_reg = qlcnic_82xx_hw_read_wx_2M,
563 .write_reg = qlcnic_82xx_hw_write_wx_2M,
564 .get_mac_address = qlcnic_82xx_get_mac_address,
565 .setup_intr = qlcnic_82xx_setup_intr,
566 .alloc_mbx_args = qlcnic_82xx_alloc_mbx_args,
567 .mbx_cmd = qlcnic_82xx_issue_cmd,
568 .get_func_no = qlcnic_82xx_get_func_no,
569 .api_lock = qlcnic_82xx_api_lock,
570 .api_unlock = qlcnic_82xx_api_unlock,
571 .add_sysfs = qlcnic_82xx_add_sysfs,
572 .remove_sysfs = qlcnic_82xx_remove_sysfs,
573 .process_lb_rcv_ring_diag = qlcnic_82xx_process_rcv_ring_diag,
574 .create_rx_ctx = qlcnic_82xx_fw_cmd_create_rx_ctx,
575 .create_tx_ctx = qlcnic_82xx_fw_cmd_create_tx_ctx,
576 .del_rx_ctx = qlcnic_82xx_fw_cmd_del_rx_ctx,
577 .del_tx_ctx = qlcnic_82xx_fw_cmd_del_tx_ctx,
578 .setup_link_event = qlcnic_82xx_linkevent_request,
579 .get_nic_info = qlcnic_82xx_get_nic_info,
580 .get_pci_info = qlcnic_82xx_get_pci_info,
581 .set_nic_info = qlcnic_82xx_set_nic_info,
582 .change_macvlan = qlcnic_82xx_sre_macaddr_change,
583 .napi_enable = qlcnic_82xx_napi_enable,
584 .napi_disable = qlcnic_82xx_napi_disable,
585 .config_intr_coal = qlcnic_82xx_config_intr_coalesce,
586 .config_rss = qlcnic_82xx_config_rss,
587 .config_hw_lro = qlcnic_82xx_config_hw_lro,
588 .config_loopback = qlcnic_82xx_set_lb_mode,
589 .clear_loopback = qlcnic_82xx_clear_lb_mode,
590 .config_promisc_mode = qlcnic_82xx_nic_set_promisc,
591 .change_l2_filter = qlcnic_82xx_change_filter,
592 .get_board_info = qlcnic_82xx_get_board_info,
593 .set_mac_filter_count = qlcnic_82xx_set_mac_filter_count,
594 .free_mac_list = qlcnic_82xx_free_mac_list,
595 .read_phys_port_id = qlcnic_82xx_read_phys_port_id,
596 .io_error_detected = qlcnic_82xx_io_error_detected,
597 .io_slot_reset = qlcnic_82xx_io_slot_reset,
598 .io_resume = qlcnic_82xx_io_resume,
599 .get_beacon_state = qlcnic_82xx_get_beacon_state,
600 .enable_sds_intr = qlcnic_82xx_enable_sds_intr,
601 .disable_sds_intr = qlcnic_82xx_disable_sds_intr,
602 .enable_tx_intr = qlcnic_82xx_enable_tx_intr,
603 .disable_tx_intr = qlcnic_82xx_disable_tx_intr,
604 .get_saved_state = qlcnic_82xx_get_saved_state,
605 .set_saved_state = qlcnic_82xx_set_saved_state,
606 .cache_tmpl_hdr_values = qlcnic_82xx_cache_tmpl_hdr_values,
607 .get_cap_size = qlcnic_82xx_get_cap_size,
608 .set_sys_info = qlcnic_82xx_set_sys_info,
609 .store_cap_mask = qlcnic_82xx_store_cap_mask,
610 .encap_rx_offload = qlcnic_82xx_encap_rx_offload,
611 .encap_tx_offload = qlcnic_82xx_encap_tx_offload,
612 };
613
qlcnic_check_multi_tx_capability(struct qlcnic_adapter * adapter)614 static int qlcnic_check_multi_tx_capability(struct qlcnic_adapter *adapter)
615 {
616 struct qlcnic_hardware_context *ahw = adapter->ahw;
617
618 if (qlcnic_82xx_check(adapter) &&
619 (ahw->extra_capability[0] & QLCNIC_FW_CAPABILITY_2_MULTI_TX)) {
620 test_and_set_bit(__QLCNIC_MULTI_TX_UNIQUE, &adapter->state);
621 return 0;
622 } else {
623 return 1;
624 }
625 }
626
qlcnic_max_rings(struct qlcnic_adapter * adapter,u8 ring_cnt,int queue_type)627 static int qlcnic_max_rings(struct qlcnic_adapter *adapter, u8 ring_cnt,
628 int queue_type)
629 {
630 int num_rings, max_rings = QLCNIC_MAX_SDS_RINGS;
631
632 if (queue_type == QLCNIC_RX_QUEUE)
633 max_rings = adapter->max_sds_rings;
634 else if (queue_type == QLCNIC_TX_QUEUE)
635 max_rings = adapter->max_tx_rings;
636
637 num_rings = rounddown_pow_of_two(min_t(int, num_online_cpus(),
638 max_rings));
639
640 if (ring_cnt > num_rings)
641 return num_rings;
642 else
643 return ring_cnt;
644 }
645
qlcnic_set_tx_ring_count(struct qlcnic_adapter * adapter,u8 tx_cnt)646 void qlcnic_set_tx_ring_count(struct qlcnic_adapter *adapter, u8 tx_cnt)
647 {
648 /* 83xx adapter does not have max_tx_rings intialized in probe */
649 if (adapter->max_tx_rings)
650 adapter->drv_tx_rings = qlcnic_max_rings(adapter, tx_cnt,
651 QLCNIC_TX_QUEUE);
652 else
653 adapter->drv_tx_rings = tx_cnt;
654 }
655
qlcnic_set_sds_ring_count(struct qlcnic_adapter * adapter,u8 rx_cnt)656 void qlcnic_set_sds_ring_count(struct qlcnic_adapter *adapter, u8 rx_cnt)
657 {
658 /* 83xx adapter does not have max_sds_rings intialized in probe */
659 if (adapter->max_sds_rings)
660 adapter->drv_sds_rings = qlcnic_max_rings(adapter, rx_cnt,
661 QLCNIC_RX_QUEUE);
662 else
663 adapter->drv_sds_rings = rx_cnt;
664 }
665
qlcnic_setup_tss_rss_intr(struct qlcnic_adapter * adapter)666 int qlcnic_setup_tss_rss_intr(struct qlcnic_adapter *adapter)
667 {
668 struct pci_dev *pdev = adapter->pdev;
669 int num_msix = 0, err = 0, vector;
670
671 adapter->flags &= ~QLCNIC_TSS_RSS;
672
673 if (adapter->drv_tss_rings > 0)
674 num_msix += adapter->drv_tss_rings;
675 else
676 num_msix += adapter->drv_tx_rings;
677
678 if (adapter->drv_rss_rings > 0)
679 num_msix += adapter->drv_rss_rings;
680 else
681 num_msix += adapter->drv_sds_rings;
682
683 if (qlcnic_83xx_check(adapter))
684 num_msix += 1;
685
686 if (!adapter->msix_entries) {
687 adapter->msix_entries = kcalloc(num_msix,
688 sizeof(struct msix_entry),
689 GFP_KERNEL);
690 if (!adapter->msix_entries)
691 return -ENOMEM;
692 }
693
694 for (vector = 0; vector < num_msix; vector++)
695 adapter->msix_entries[vector].entry = vector;
696
697 restore:
698 err = pci_enable_msix_exact(pdev, adapter->msix_entries, num_msix);
699 if (err == -ENOSPC) {
700 if (!adapter->drv_tss_rings && !adapter->drv_rss_rings)
701 return err;
702
703 netdev_info(adapter->netdev,
704 "Unable to allocate %d MSI-X vectors, Available vectors %d\n",
705 num_msix, err);
706
707 num_msix = adapter->drv_tx_rings + adapter->drv_sds_rings;
708
709 /* Set rings to 0 so we can restore original TSS/RSS count */
710 adapter->drv_tss_rings = 0;
711 adapter->drv_rss_rings = 0;
712
713 if (qlcnic_83xx_check(adapter))
714 num_msix += 1;
715
716 netdev_info(adapter->netdev,
717 "Restoring %d Tx, %d SDS rings for total %d vectors.\n",
718 adapter->drv_tx_rings, adapter->drv_sds_rings,
719 num_msix);
720
721 goto restore;
722 } else if (err < 0) {
723 return err;
724 }
725
726 adapter->ahw->num_msix = num_msix;
727 if (adapter->drv_tss_rings > 0)
728 adapter->drv_tx_rings = adapter->drv_tss_rings;
729
730 if (adapter->drv_rss_rings > 0)
731 adapter->drv_sds_rings = adapter->drv_rss_rings;
732
733 return 0;
734 }
735
qlcnic_enable_msix(struct qlcnic_adapter * adapter,u32 num_msix)736 int qlcnic_enable_msix(struct qlcnic_adapter *adapter, u32 num_msix)
737 {
738 struct pci_dev *pdev = adapter->pdev;
739 int err, vector;
740
741 if (!adapter->msix_entries) {
742 adapter->msix_entries = kcalloc(num_msix,
743 sizeof(struct msix_entry),
744 GFP_KERNEL);
745 if (!adapter->msix_entries)
746 return -ENOMEM;
747 }
748
749 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
750
751 if (adapter->ahw->msix_supported) {
752 enable_msix:
753 for (vector = 0; vector < num_msix; vector++)
754 adapter->msix_entries[vector].entry = vector;
755
756 err = pci_enable_msix_range(pdev,
757 adapter->msix_entries, 1, num_msix);
758
759 if (err == num_msix) {
760 adapter->flags |= QLCNIC_MSIX_ENABLED;
761 adapter->ahw->num_msix = num_msix;
762 dev_info(&pdev->dev, "using msi-x interrupts\n");
763 return 0;
764 } else if (err > 0) {
765 pci_disable_msix(pdev);
766
767 dev_info(&pdev->dev,
768 "Unable to allocate %d MSI-X vectors, Available vectors %d\n",
769 num_msix, err);
770
771 if (qlcnic_82xx_check(adapter)) {
772 num_msix = rounddown_pow_of_two(err);
773 if (err < QLCNIC_82XX_MINIMUM_VECTOR)
774 return -ENOSPC;
775 } else {
776 num_msix = rounddown_pow_of_two(err - 1);
777 num_msix += 1;
778 if (err < QLCNIC_83XX_MINIMUM_VECTOR)
779 return -ENOSPC;
780 }
781
782 if (qlcnic_82xx_check(adapter) &&
783 !qlcnic_check_multi_tx(adapter)) {
784 adapter->drv_sds_rings = num_msix;
785 adapter->drv_tx_rings = QLCNIC_SINGLE_RING;
786 } else {
787 /* Distribute vectors equally */
788 adapter->drv_tx_rings = num_msix / 2;
789 adapter->drv_sds_rings = adapter->drv_tx_rings;
790 }
791
792 if (num_msix) {
793 dev_info(&pdev->dev,
794 "Trying to allocate %d MSI-X interrupt vectors\n",
795 num_msix);
796 goto enable_msix;
797 }
798 } else {
799 dev_info(&pdev->dev,
800 "Unable to allocate %d MSI-X vectors, err=%d\n",
801 num_msix, err);
802 return err;
803 }
804 }
805
806 return -EIO;
807 }
808
qlcnic_82xx_calculate_msix_vector(struct qlcnic_adapter * adapter)809 static int qlcnic_82xx_calculate_msix_vector(struct qlcnic_adapter *adapter)
810 {
811 int num_msix;
812
813 num_msix = adapter->drv_sds_rings;
814
815 if (qlcnic_check_multi_tx(adapter))
816 num_msix += adapter->drv_tx_rings;
817 else
818 num_msix += QLCNIC_SINGLE_RING;
819
820 return num_msix;
821 }
822
qlcnic_enable_msi_legacy(struct qlcnic_adapter * adapter)823 static int qlcnic_enable_msi_legacy(struct qlcnic_adapter *adapter)
824 {
825 int err = 0;
826 u32 offset, mask_reg;
827 const struct qlcnic_legacy_intr_set *legacy_intrp;
828 struct qlcnic_hardware_context *ahw = adapter->ahw;
829 struct pci_dev *pdev = adapter->pdev;
830
831 if (qlcnic_use_msi && !pci_enable_msi(pdev)) {
832 adapter->flags |= QLCNIC_MSI_ENABLED;
833 offset = msi_tgt_status[adapter->ahw->pci_func];
834 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter->ahw,
835 offset);
836 dev_info(&pdev->dev, "using msi interrupts\n");
837 adapter->msix_entries[0].vector = pdev->irq;
838 return err;
839 }
840
841 if (qlcnic_use_msi || qlcnic_use_msi_x)
842 return -EOPNOTSUPP;
843
844 legacy_intrp = &legacy_intr[adapter->ahw->pci_func];
845 adapter->ahw->int_vec_bit = legacy_intrp->int_vec_bit;
846 offset = legacy_intrp->tgt_status_reg;
847 adapter->tgt_status_reg = qlcnic_get_ioaddr(ahw, offset);
848 mask_reg = legacy_intrp->tgt_mask_reg;
849 adapter->tgt_mask_reg = qlcnic_get_ioaddr(ahw, mask_reg);
850 adapter->isr_int_vec = qlcnic_get_ioaddr(ahw, ISR_INT_VECTOR);
851 adapter->crb_int_state_reg = qlcnic_get_ioaddr(ahw, ISR_INT_STATE_REG);
852 dev_info(&pdev->dev, "using legacy interrupts\n");
853 adapter->msix_entries[0].vector = pdev->irq;
854 return err;
855 }
856
qlcnic_82xx_setup_intr(struct qlcnic_adapter * adapter)857 static int qlcnic_82xx_setup_intr(struct qlcnic_adapter *adapter)
858 {
859 int num_msix, err = 0;
860
861 if (adapter->flags & QLCNIC_TSS_RSS) {
862 err = qlcnic_setup_tss_rss_intr(adapter);
863 if (err < 0)
864 return err;
865 num_msix = adapter->ahw->num_msix;
866 } else {
867 num_msix = qlcnic_82xx_calculate_msix_vector(adapter);
868
869 err = qlcnic_enable_msix(adapter, num_msix);
870 if (err == -ENOMEM)
871 return err;
872
873 if (!(adapter->flags & QLCNIC_MSIX_ENABLED)) {
874 qlcnic_disable_multi_tx(adapter);
875 adapter->drv_sds_rings = QLCNIC_SINGLE_RING;
876
877 err = qlcnic_enable_msi_legacy(adapter);
878 if (err)
879 return err;
880 }
881 }
882
883 return 0;
884 }
885
qlcnic_82xx_mq_intrpt(struct qlcnic_adapter * adapter,int op_type)886 int qlcnic_82xx_mq_intrpt(struct qlcnic_adapter *adapter, int op_type)
887 {
888 struct qlcnic_hardware_context *ahw = adapter->ahw;
889 int err, i;
890
891 if (qlcnic_check_multi_tx(adapter) &&
892 !ahw->diag_test &&
893 (adapter->flags & QLCNIC_MSIX_ENABLED)) {
894 ahw->intr_tbl =
895 vzalloc(array_size(sizeof(struct qlcnic_intrpt_config),
896 ahw->num_msix));
897 if (!ahw->intr_tbl)
898 return -ENOMEM;
899
900 for (i = 0; i < ahw->num_msix; i++) {
901 ahw->intr_tbl[i].type = QLCNIC_INTRPT_MSIX;
902 ahw->intr_tbl[i].id = i;
903 ahw->intr_tbl[i].src = 0;
904 }
905
906 err = qlcnic_82xx_config_intrpt(adapter, 1);
907 if (err)
908 dev_err(&adapter->pdev->dev,
909 "Failed to configure Interrupt for %d vector\n",
910 ahw->num_msix);
911 return err;
912 }
913
914 return 0;
915 }
916
qlcnic_teardown_intr(struct qlcnic_adapter * adapter)917 void qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
918 {
919 if (adapter->flags & QLCNIC_MSIX_ENABLED)
920 pci_disable_msix(adapter->pdev);
921 if (adapter->flags & QLCNIC_MSI_ENABLED)
922 pci_disable_msi(adapter->pdev);
923
924 kfree(adapter->msix_entries);
925 adapter->msix_entries = NULL;
926
927 if (adapter->ahw->intr_tbl) {
928 vfree(adapter->ahw->intr_tbl);
929 adapter->ahw->intr_tbl = NULL;
930 }
931 }
932
qlcnic_cleanup_pci_map(struct qlcnic_hardware_context * ahw)933 static void qlcnic_cleanup_pci_map(struct qlcnic_hardware_context *ahw)
934 {
935 if (ahw->pci_base0 != NULL)
936 iounmap(ahw->pci_base0);
937 }
938
qlcnic_get_act_pci_func(struct qlcnic_adapter * adapter)939 static int qlcnic_get_act_pci_func(struct qlcnic_adapter *adapter)
940 {
941 struct qlcnic_hardware_context *ahw = adapter->ahw;
942 struct qlcnic_pci_info *pci_info;
943 int ret;
944
945 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED)) {
946 switch (ahw->port_type) {
947 case QLCNIC_GBE:
948 ahw->total_nic_func = QLCNIC_NIU_MAX_GBE_PORTS;
949 break;
950 case QLCNIC_XGBE:
951 ahw->total_nic_func = QLCNIC_NIU_MAX_XG_PORTS;
952 break;
953 }
954 return 0;
955 }
956
957 if (ahw->op_mode == QLCNIC_MGMT_FUNC)
958 return 0;
959
960 pci_info = kcalloc(ahw->max_vnic_func, sizeof(*pci_info), GFP_KERNEL);
961 if (!pci_info)
962 return -ENOMEM;
963
964 ret = qlcnic_get_pci_info(adapter, pci_info);
965 kfree(pci_info);
966 return ret;
967 }
968
qlcnic_port_eswitch_cfg_capability(struct qlcnic_adapter * adapter)969 static bool qlcnic_port_eswitch_cfg_capability(struct qlcnic_adapter *adapter)
970 {
971 bool ret = false;
972
973 if (qlcnic_84xx_check(adapter)) {
974 ret = true;
975 } else if (qlcnic_83xx_check(adapter)) {
976 if (adapter->ahw->extra_capability[0] &
977 QLCNIC_FW_CAPABILITY_2_PER_PORT_ESWITCH_CFG)
978 ret = true;
979 else
980 ret = false;
981 }
982
983 return ret;
984 }
985
qlcnic_init_pci_info(struct qlcnic_adapter * adapter)986 int qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
987 {
988 struct qlcnic_hardware_context *ahw = adapter->ahw;
989 struct qlcnic_pci_info *pci_info;
990 int i, id = 0, ret = 0, j = 0;
991 u16 act_pci_func;
992 u8 pfn;
993
994 pci_info = kcalloc(ahw->max_vnic_func, sizeof(*pci_info), GFP_KERNEL);
995 if (!pci_info)
996 return -ENOMEM;
997
998 ret = qlcnic_get_pci_info(adapter, pci_info);
999 if (ret)
1000 goto err_pci_info;
1001
1002 act_pci_func = ahw->total_nic_func;
1003
1004 adapter->npars = kcalloc(act_pci_func,
1005 sizeof(struct qlcnic_npar_info),
1006 GFP_KERNEL);
1007 if (!adapter->npars) {
1008 ret = -ENOMEM;
1009 goto err_pci_info;
1010 }
1011
1012 adapter->eswitch = kcalloc(QLCNIC_NIU_MAX_XG_PORTS,
1013 sizeof(struct qlcnic_eswitch),
1014 GFP_KERNEL);
1015 if (!adapter->eswitch) {
1016 ret = -ENOMEM;
1017 goto err_npars;
1018 }
1019
1020 for (i = 0; i < ahw->max_vnic_func; i++) {
1021 pfn = pci_info[i].id;
1022
1023 if (pfn >= ahw->max_vnic_func) {
1024 ret = -EINVAL;
1025 dev_err(&adapter->pdev->dev, "%s: Invalid function 0x%x, max 0x%x\n",
1026 __func__, pfn, ahw->max_vnic_func);
1027 goto err_eswitch;
1028 }
1029
1030 if (!pci_info[i].active ||
1031 (pci_info[i].type != QLCNIC_TYPE_NIC))
1032 continue;
1033
1034 if (qlcnic_port_eswitch_cfg_capability(adapter)) {
1035 if (!qlcnic_83xx_set_port_eswitch_status(adapter, pfn,
1036 &id))
1037 adapter->npars[j].eswitch_status = true;
1038 else
1039 continue;
1040 } else {
1041 adapter->npars[j].eswitch_status = true;
1042 }
1043
1044 adapter->npars[j].pci_func = pfn;
1045 adapter->npars[j].active = (u8)pci_info[i].active;
1046 adapter->npars[j].type = (u8)pci_info[i].type;
1047 adapter->npars[j].phy_port = (u8)pci_info[i].default_port;
1048 adapter->npars[j].min_bw = pci_info[i].tx_min_bw;
1049 adapter->npars[j].max_bw = pci_info[i].tx_max_bw;
1050
1051 memcpy(&adapter->npars[j].mac, &pci_info[i].mac, ETH_ALEN);
1052 j++;
1053 }
1054
1055 /* Update eSwitch status for adapters without per port eSwitch
1056 * configuration capability
1057 */
1058 if (!qlcnic_port_eswitch_cfg_capability(adapter)) {
1059 for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
1060 adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
1061 }
1062
1063 kfree(pci_info);
1064 return 0;
1065
1066 err_eswitch:
1067 kfree(adapter->eswitch);
1068 adapter->eswitch = NULL;
1069 err_npars:
1070 kfree(adapter->npars);
1071 adapter->npars = NULL;
1072 err_pci_info:
1073 kfree(pci_info);
1074
1075 return ret;
1076 }
1077
1078 static int
qlcnic_set_function_modes(struct qlcnic_adapter * adapter)1079 qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
1080 {
1081 u8 id;
1082 int ret;
1083 u32 data = QLCNIC_MGMT_FUNC;
1084 struct qlcnic_hardware_context *ahw = adapter->ahw;
1085
1086 ret = qlcnic_api_lock(adapter);
1087 if (ret)
1088 goto err_lock;
1089
1090 id = ahw->pci_func;
1091 data = QLC_SHARED_REG_RD32(adapter, QLCNIC_DRV_OP_MODE);
1092 data = (data & ~QLC_DEV_SET_DRV(0xf, id)) |
1093 QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC, id);
1094 QLC_SHARED_REG_WR32(adapter, QLCNIC_DRV_OP_MODE, data);
1095 qlcnic_api_unlock(adapter);
1096 err_lock:
1097 return ret;
1098 }
1099
qlcnic_check_vf(struct qlcnic_adapter * adapter,const struct pci_device_id * ent)1100 static void qlcnic_check_vf(struct qlcnic_adapter *adapter,
1101 const struct pci_device_id *ent)
1102 {
1103 u32 op_mode, priv_level;
1104
1105 /* Determine FW API version */
1106 adapter->ahw->fw_hal_version = QLC_SHARED_REG_RD32(adapter,
1107 QLCNIC_FW_API);
1108
1109 /* Find PCI function number */
1110 qlcnic_get_func_no(adapter);
1111
1112 /* Determine function privilege level */
1113 op_mode = QLC_SHARED_REG_RD32(adapter, QLCNIC_DRV_OP_MODE);
1114 if (op_mode == QLC_DEV_DRV_DEFAULT)
1115 priv_level = QLCNIC_MGMT_FUNC;
1116 else
1117 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
1118
1119 if (priv_level == QLCNIC_NON_PRIV_FUNC) {
1120 adapter->ahw->op_mode = QLCNIC_NON_PRIV_FUNC;
1121 dev_info(&adapter->pdev->dev,
1122 "HAL Version: %d Non Privileged function\n",
1123 adapter->ahw->fw_hal_version);
1124 adapter->nic_ops = &qlcnic_vf_ops;
1125 } else
1126 adapter->nic_ops = &qlcnic_ops;
1127 }
1128
1129 #define QLCNIC_82XX_BAR0_LENGTH 0x00200000UL
1130 #define QLCNIC_83XX_BAR0_LENGTH 0x4000
qlcnic_get_bar_length(u32 dev_id,ulong * bar)1131 static void qlcnic_get_bar_length(u32 dev_id, ulong *bar)
1132 {
1133 switch (dev_id) {
1134 case PCI_DEVICE_ID_QLOGIC_QLE824X:
1135 *bar = QLCNIC_82XX_BAR0_LENGTH;
1136 break;
1137 case PCI_DEVICE_ID_QLOGIC_QLE834X:
1138 case PCI_DEVICE_ID_QLOGIC_QLE8830:
1139 case PCI_DEVICE_ID_QLOGIC_QLE844X:
1140 case PCI_DEVICE_ID_QLOGIC_VF_QLE834X:
1141 case PCI_DEVICE_ID_QLOGIC_VF_QLE844X:
1142 case PCI_DEVICE_ID_QLOGIC_VF_QLE8C30:
1143 *bar = QLCNIC_83XX_BAR0_LENGTH;
1144 break;
1145 default:
1146 *bar = 0;
1147 }
1148 }
1149
qlcnic_setup_pci_map(struct pci_dev * pdev,struct qlcnic_hardware_context * ahw)1150 static int qlcnic_setup_pci_map(struct pci_dev *pdev,
1151 struct qlcnic_hardware_context *ahw)
1152 {
1153 u32 offset;
1154 void __iomem *mem_ptr0 = NULL;
1155 unsigned long mem_len, pci_len0 = 0, bar0_len;
1156
1157 /* remap phys address */
1158 mem_len = pci_resource_len(pdev, 0);
1159
1160 qlcnic_get_bar_length(pdev->device, &bar0_len);
1161 if (mem_len >= bar0_len) {
1162
1163 mem_ptr0 = pci_ioremap_bar(pdev, 0);
1164 if (mem_ptr0 == NULL) {
1165 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
1166 return -EIO;
1167 }
1168 pci_len0 = mem_len;
1169 } else {
1170 return -EIO;
1171 }
1172
1173 dev_info(&pdev->dev, "%dKB memory map\n", (int)(mem_len >> 10));
1174
1175 ahw->pci_base0 = mem_ptr0;
1176 ahw->pci_len0 = pci_len0;
1177 offset = QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(ahw->pci_func));
1178 qlcnic_get_ioaddr(ahw, offset);
1179
1180 return 0;
1181 }
1182
qlcnic_validate_subsystem_id(struct qlcnic_adapter * adapter,int index)1183 static bool qlcnic_validate_subsystem_id(struct qlcnic_adapter *adapter,
1184 int index)
1185 {
1186 struct pci_dev *pdev = adapter->pdev;
1187 unsigned short subsystem_vendor;
1188 bool ret = true;
1189
1190 subsystem_vendor = pdev->subsystem_vendor;
1191
1192 if (pdev->device == PCI_DEVICE_ID_QLOGIC_QLE824X ||
1193 pdev->device == PCI_DEVICE_ID_QLOGIC_QLE834X) {
1194 if (qlcnic_boards[index].sub_vendor == subsystem_vendor &&
1195 qlcnic_boards[index].sub_device == pdev->subsystem_device)
1196 ret = true;
1197 else
1198 ret = false;
1199 }
1200
1201 return ret;
1202 }
1203
qlcnic_get_board_name(struct qlcnic_adapter * adapter,char * name)1204 static void qlcnic_get_board_name(struct qlcnic_adapter *adapter, char *name)
1205 {
1206 struct pci_dev *pdev = adapter->pdev;
1207 int i, found = 0;
1208
1209 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
1210 if (qlcnic_boards[i].vendor == pdev->vendor &&
1211 qlcnic_boards[i].device == pdev->device &&
1212 qlcnic_validate_subsystem_id(adapter, i)) {
1213 found = 1;
1214 break;
1215 }
1216 }
1217
1218 if (!found)
1219 sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
1220 else
1221 sprintf(name, "%pM: %s" , adapter->mac_addr,
1222 qlcnic_boards[i].short_name);
1223 }
1224
1225 static void
qlcnic_check_options(struct qlcnic_adapter * adapter)1226 qlcnic_check_options(struct qlcnic_adapter *adapter)
1227 {
1228 int err;
1229 u32 fw_major, fw_minor, fw_build, prev_fw_version;
1230 struct pci_dev *pdev = adapter->pdev;
1231 struct qlcnic_hardware_context *ahw = adapter->ahw;
1232 struct qlcnic_fw_dump *fw_dump = &ahw->fw_dump;
1233
1234 prev_fw_version = adapter->fw_version;
1235
1236 fw_major = QLC_SHARED_REG_RD32(adapter, QLCNIC_FW_VERSION_MAJOR);
1237 fw_minor = QLC_SHARED_REG_RD32(adapter, QLCNIC_FW_VERSION_MINOR);
1238 fw_build = QLC_SHARED_REG_RD32(adapter, QLCNIC_FW_VERSION_SUB);
1239
1240 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
1241
1242 err = qlcnic_get_board_info(adapter);
1243 if (err) {
1244 dev_err(&pdev->dev, "Error getting board config info.\n");
1245 return;
1246 }
1247 if (ahw->op_mode != QLCNIC_NON_PRIV_FUNC) {
1248 if (fw_dump->tmpl_hdr == NULL ||
1249 adapter->fw_version > prev_fw_version) {
1250 vfree(fw_dump->tmpl_hdr);
1251 if (!qlcnic_fw_cmd_get_minidump_temp(adapter))
1252 dev_info(&pdev->dev,
1253 "Supports FW dump capability\n");
1254 }
1255 }
1256
1257 dev_info(&pdev->dev, "Driver v%s, firmware v%d.%d.%d\n",
1258 QLCNIC_LINUX_VERSIONID, fw_major, fw_minor, fw_build);
1259
1260 if (adapter->ahw->port_type == QLCNIC_XGBE) {
1261 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
1262 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
1263 adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
1264 } else {
1265 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
1266 adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
1267 }
1268
1269 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
1270 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
1271
1272 } else if (adapter->ahw->port_type == QLCNIC_GBE) {
1273 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
1274 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
1275 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
1276 adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
1277 }
1278
1279 adapter->ahw->msix_supported = !!qlcnic_use_msi_x;
1280
1281 adapter->num_txd = MAX_CMD_DESCRIPTORS;
1282
1283 adapter->max_rds_rings = MAX_RDS_RINGS;
1284 }
1285
1286 static int
qlcnic_initialize_nic(struct qlcnic_adapter * adapter)1287 qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
1288 {
1289 struct qlcnic_info nic_info;
1290 int err = 0;
1291
1292 memset(&nic_info, 0, sizeof(struct qlcnic_info));
1293 err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
1294 if (err)
1295 return err;
1296
1297 adapter->ahw->physical_port = (u8)nic_info.phys_port;
1298 adapter->ahw->switch_mode = nic_info.switch_mode;
1299 adapter->ahw->max_tx_ques = nic_info.max_tx_ques;
1300 adapter->ahw->max_rx_ques = nic_info.max_rx_ques;
1301 adapter->ahw->capabilities = nic_info.capabilities;
1302
1303 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_MORE_CAPS) {
1304 u32 temp;
1305 temp = QLCRD32(adapter, CRB_FW_CAPABILITIES_2, &err);
1306 if (err == -EIO)
1307 return err;
1308 adapter->ahw->extra_capability[0] = temp;
1309 } else {
1310 adapter->ahw->extra_capability[0] = 0;
1311 }
1312
1313 adapter->ahw->max_mac_filters = nic_info.max_mac_filters;
1314 adapter->ahw->max_mtu = nic_info.max_mtu;
1315
1316 if (adapter->ahw->capabilities & BIT_6) {
1317 adapter->flags |= QLCNIC_ESWITCH_ENABLED;
1318 adapter->ahw->nic_mode = QLCNIC_VNIC_MODE;
1319 adapter->max_tx_rings = QLCNIC_MAX_HW_VNIC_TX_RINGS;
1320 adapter->max_sds_rings = QLCNIC_MAX_VNIC_SDS_RINGS;
1321
1322 dev_info(&adapter->pdev->dev, "vNIC mode enabled.\n");
1323 } else {
1324 adapter->ahw->nic_mode = QLCNIC_DEFAULT_MODE;
1325 adapter->max_tx_rings = QLCNIC_MAX_HW_TX_RINGS;
1326 adapter->max_sds_rings = QLCNIC_MAX_SDS_RINGS;
1327 adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
1328 }
1329
1330 return err;
1331 }
1332
qlcnic_set_vlan_config(struct qlcnic_adapter * adapter,struct qlcnic_esw_func_cfg * esw_cfg)1333 void qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
1334 struct qlcnic_esw_func_cfg *esw_cfg)
1335 {
1336 if (esw_cfg->discard_tagged)
1337 adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
1338 else
1339 adapter->flags |= QLCNIC_TAGGING_ENABLED;
1340
1341 if (esw_cfg->vlan_id) {
1342 adapter->rx_pvid = esw_cfg->vlan_id;
1343 adapter->tx_pvid = esw_cfg->vlan_id;
1344 } else {
1345 adapter->rx_pvid = 0;
1346 adapter->tx_pvid = 0;
1347 }
1348 }
1349
1350 static int
qlcnic_vlan_rx_add(struct net_device * netdev,__be16 proto,u16 vid)1351 qlcnic_vlan_rx_add(struct net_device *netdev, __be16 proto, u16 vid)
1352 {
1353 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1354 int err;
1355
1356 if (qlcnic_sriov_vf_check(adapter)) {
1357 err = qlcnic_sriov_cfg_vf_guest_vlan(adapter, vid, 1);
1358 if (err) {
1359 netdev_err(netdev,
1360 "Cannot add VLAN filter for VLAN id %d, err=%d",
1361 vid, err);
1362 return err;
1363 }
1364 }
1365
1366 set_bit(vid, adapter->vlans);
1367 return 0;
1368 }
1369
1370 static int
qlcnic_vlan_rx_del(struct net_device * netdev,__be16 proto,u16 vid)1371 qlcnic_vlan_rx_del(struct net_device *netdev, __be16 proto, u16 vid)
1372 {
1373 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1374 int err;
1375
1376 if (qlcnic_sriov_vf_check(adapter)) {
1377 err = qlcnic_sriov_cfg_vf_guest_vlan(adapter, vid, 0);
1378 if (err) {
1379 netdev_err(netdev,
1380 "Cannot delete VLAN filter for VLAN id %d, err=%d",
1381 vid, err);
1382 return err;
1383 }
1384 }
1385
1386 qlcnic_restore_indev_addr(netdev, NETDEV_DOWN);
1387 clear_bit(vid, adapter->vlans);
1388 return 0;
1389 }
1390
qlcnic_set_eswitch_port_features(struct qlcnic_adapter * adapter,struct qlcnic_esw_func_cfg * esw_cfg)1391 void qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
1392 struct qlcnic_esw_func_cfg *esw_cfg)
1393 {
1394 adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
1395 QLCNIC_PROMISC_DISABLED);
1396
1397 if (esw_cfg->mac_anti_spoof)
1398 adapter->flags |= QLCNIC_MACSPOOF;
1399
1400 if (!esw_cfg->mac_override)
1401 adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
1402
1403 if (!esw_cfg->promisc_mode)
1404 adapter->flags |= QLCNIC_PROMISC_DISABLED;
1405 }
1406
qlcnic_set_eswitch_port_config(struct qlcnic_adapter * adapter)1407 int qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
1408 {
1409 struct qlcnic_esw_func_cfg esw_cfg;
1410
1411 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1412 return 0;
1413
1414 esw_cfg.pci_func = adapter->ahw->pci_func;
1415 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
1416 return -EIO;
1417 qlcnic_set_vlan_config(adapter, &esw_cfg);
1418 qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
1419 qlcnic_set_netdev_features(adapter, &esw_cfg);
1420
1421 return 0;
1422 }
1423
qlcnic_set_netdev_features(struct qlcnic_adapter * adapter,struct qlcnic_esw_func_cfg * esw_cfg)1424 void qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
1425 struct qlcnic_esw_func_cfg *esw_cfg)
1426 {
1427 struct net_device *netdev = adapter->netdev;
1428
1429 if (qlcnic_83xx_check(adapter))
1430 return;
1431
1432 adapter->offload_flags = esw_cfg->offload_flags;
1433 adapter->flags |= QLCNIC_APP_CHANGED_FLAGS;
1434 netdev_update_features(netdev);
1435 adapter->flags &= ~QLCNIC_APP_CHANGED_FLAGS;
1436 }
1437
1438 static int
qlcnic_check_eswitch_mode(struct qlcnic_adapter * adapter)1439 qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
1440 {
1441 u32 op_mode, priv_level;
1442 int err = 0;
1443
1444 err = qlcnic_initialize_nic(adapter);
1445 if (err)
1446 return err;
1447
1448 if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
1449 return 0;
1450
1451 op_mode = QLC_SHARED_REG_RD32(adapter, QLCNIC_DRV_OP_MODE);
1452 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
1453
1454 if (op_mode == QLC_DEV_DRV_DEFAULT)
1455 priv_level = QLCNIC_MGMT_FUNC;
1456 else
1457 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
1458
1459 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
1460 if (priv_level == QLCNIC_MGMT_FUNC) {
1461 adapter->ahw->op_mode = QLCNIC_MGMT_FUNC;
1462 err = qlcnic_init_pci_info(adapter);
1463 if (err)
1464 return err;
1465 /* Set privilege level for other functions */
1466 qlcnic_set_function_modes(adapter);
1467 dev_info(&adapter->pdev->dev,
1468 "HAL Version: %d, Management function\n",
1469 adapter->ahw->fw_hal_version);
1470 } else if (priv_level == QLCNIC_PRIV_FUNC) {
1471 adapter->ahw->op_mode = QLCNIC_PRIV_FUNC;
1472 dev_info(&adapter->pdev->dev,
1473 "HAL Version: %d, Privileged function\n",
1474 adapter->ahw->fw_hal_version);
1475 }
1476 } else {
1477 adapter->ahw->nic_mode = QLCNIC_DEFAULT_MODE;
1478 }
1479
1480 adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
1481
1482 return err;
1483 }
1484
qlcnic_set_default_offload_settings(struct qlcnic_adapter * adapter)1485 int qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
1486 {
1487 struct qlcnic_esw_func_cfg esw_cfg;
1488 struct qlcnic_npar_info *npar;
1489 u8 i;
1490
1491 if (adapter->need_fw_reset)
1492 return 0;
1493
1494 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
1495 if (!adapter->npars[i].eswitch_status)
1496 continue;
1497
1498 memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
1499 esw_cfg.pci_func = adapter->npars[i].pci_func;
1500 esw_cfg.mac_override = BIT_0;
1501 esw_cfg.promisc_mode = BIT_0;
1502 if (qlcnic_82xx_check(adapter)) {
1503 esw_cfg.offload_flags = BIT_0;
1504 if (QLCNIC_IS_TSO_CAPABLE(adapter))
1505 esw_cfg.offload_flags |= (BIT_1 | BIT_2);
1506 }
1507 if (qlcnic_config_switch_port(adapter, &esw_cfg))
1508 return -EIO;
1509 npar = &adapter->npars[i];
1510 npar->pvid = esw_cfg.vlan_id;
1511 npar->mac_override = esw_cfg.mac_override;
1512 npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
1513 npar->discard_tagged = esw_cfg.discard_tagged;
1514 npar->promisc_mode = esw_cfg.promisc_mode;
1515 npar->offload_flags = esw_cfg.offload_flags;
1516 }
1517
1518 return 0;
1519 }
1520
1521
1522 static int
qlcnic_reset_eswitch_config(struct qlcnic_adapter * adapter,struct qlcnic_npar_info * npar,int pci_func)1523 qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
1524 struct qlcnic_npar_info *npar, int pci_func)
1525 {
1526 struct qlcnic_esw_func_cfg esw_cfg;
1527 esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
1528 esw_cfg.pci_func = pci_func;
1529 esw_cfg.vlan_id = npar->pvid;
1530 esw_cfg.mac_override = npar->mac_override;
1531 esw_cfg.discard_tagged = npar->discard_tagged;
1532 esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
1533 esw_cfg.offload_flags = npar->offload_flags;
1534 esw_cfg.promisc_mode = npar->promisc_mode;
1535 if (qlcnic_config_switch_port(adapter, &esw_cfg))
1536 return -EIO;
1537
1538 esw_cfg.op_mode = QLCNIC_ADD_VLAN;
1539 if (qlcnic_config_switch_port(adapter, &esw_cfg))
1540 return -EIO;
1541
1542 return 0;
1543 }
1544
qlcnic_reset_npar_config(struct qlcnic_adapter * adapter)1545 int qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
1546 {
1547 int i, err;
1548 struct qlcnic_npar_info *npar;
1549 struct qlcnic_info nic_info;
1550 u8 pci_func;
1551
1552 if (qlcnic_82xx_check(adapter))
1553 if (!adapter->need_fw_reset)
1554 return 0;
1555
1556 /* Set the NPAR config data after FW reset */
1557 for (i = 0; i < adapter->ahw->total_nic_func; i++) {
1558 npar = &adapter->npars[i];
1559 pci_func = npar->pci_func;
1560 if (!adapter->npars[i].eswitch_status)
1561 continue;
1562
1563 memset(&nic_info, 0, sizeof(struct qlcnic_info));
1564 err = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
1565 if (err)
1566 return err;
1567 nic_info.min_tx_bw = npar->min_bw;
1568 nic_info.max_tx_bw = npar->max_bw;
1569 err = qlcnic_set_nic_info(adapter, &nic_info);
1570 if (err)
1571 return err;
1572
1573 if (npar->enable_pm) {
1574 err = qlcnic_config_port_mirroring(adapter,
1575 npar->dest_npar, 1,
1576 pci_func);
1577 if (err)
1578 return err;
1579 }
1580 err = qlcnic_reset_eswitch_config(adapter, npar, pci_func);
1581 if (err)
1582 return err;
1583 }
1584 return 0;
1585 }
1586
qlcnic_check_npar_opertional(struct qlcnic_adapter * adapter)1587 static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
1588 {
1589 u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
1590 u32 npar_state;
1591
1592 if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
1593 return 0;
1594
1595 npar_state = QLC_SHARED_REG_RD32(adapter,
1596 QLCNIC_CRB_DEV_NPAR_STATE);
1597 while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
1598 msleep(1000);
1599 npar_state = QLC_SHARED_REG_RD32(adapter,
1600 QLCNIC_CRB_DEV_NPAR_STATE);
1601 }
1602 if (!npar_opt_timeo) {
1603 dev_err(&adapter->pdev->dev,
1604 "Waiting for NPAR state to operational timeout\n");
1605 return -EIO;
1606 }
1607 return 0;
1608 }
1609
1610 static int
qlcnic_set_mgmt_operations(struct qlcnic_adapter * adapter)1611 qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
1612 {
1613 int err;
1614
1615 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
1616 adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1617 return 0;
1618
1619 err = qlcnic_set_default_offload_settings(adapter);
1620 if (err)
1621 return err;
1622
1623 err = qlcnic_reset_npar_config(adapter);
1624 if (err)
1625 return err;
1626
1627 qlcnic_dev_set_npar_ready(adapter);
1628
1629 return err;
1630 }
1631
qlcnic_82xx_start_firmware(struct qlcnic_adapter * adapter)1632 static int qlcnic_82xx_start_firmware(struct qlcnic_adapter *adapter)
1633 {
1634 int err;
1635
1636 err = qlcnic_can_start_firmware(adapter);
1637 if (err < 0)
1638 return err;
1639 else if (!err)
1640 goto check_fw_status;
1641
1642 if (qlcnic_load_fw_file)
1643 qlcnic_request_firmware(adapter);
1644 else {
1645 err = qlcnic_check_flash_fw_ver(adapter);
1646 if (err)
1647 goto err_out;
1648
1649 adapter->ahw->fw_type = QLCNIC_FLASH_ROMIMAGE;
1650 }
1651
1652 err = qlcnic_need_fw_reset(adapter);
1653 if (err == 0)
1654 goto check_fw_status;
1655
1656 err = qlcnic_pinit_from_rom(adapter);
1657 if (err)
1658 goto err_out;
1659
1660 err = qlcnic_load_firmware(adapter);
1661 if (err)
1662 goto err_out;
1663
1664 qlcnic_release_firmware(adapter);
1665 QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
1666
1667 check_fw_status:
1668 err = qlcnic_check_fw_status(adapter);
1669 if (err)
1670 goto err_out;
1671
1672 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
1673 qlcnic_idc_debug_info(adapter, 1);
1674 err = qlcnic_check_eswitch_mode(adapter);
1675 if (err) {
1676 dev_err(&adapter->pdev->dev,
1677 "Memory allocation failed for eswitch\n");
1678 goto err_out;
1679 }
1680 err = qlcnic_set_mgmt_operations(adapter);
1681 if (err)
1682 goto err_out;
1683
1684 qlcnic_check_options(adapter);
1685 adapter->need_fw_reset = 0;
1686
1687 qlcnic_release_firmware(adapter);
1688 return 0;
1689
1690 err_out:
1691 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1692 dev_err(&adapter->pdev->dev, "Device state set to failed\n");
1693
1694 qlcnic_release_firmware(adapter);
1695 return err;
1696 }
1697
1698 static int
qlcnic_request_irq(struct qlcnic_adapter * adapter)1699 qlcnic_request_irq(struct qlcnic_adapter *adapter)
1700 {
1701 irq_handler_t handler;
1702 struct qlcnic_host_sds_ring *sds_ring;
1703 struct qlcnic_host_tx_ring *tx_ring;
1704 int err, ring, num_sds_rings;
1705
1706 unsigned long flags = 0;
1707 struct net_device *netdev = adapter->netdev;
1708 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1709
1710 if (adapter->ahw->diag_test == QLCNIC_INTERRUPT_TEST) {
1711 if (qlcnic_82xx_check(adapter))
1712 handler = qlcnic_tmp_intr;
1713 else
1714 handler = qlcnic_83xx_tmp_intr;
1715 if (!QLCNIC_IS_MSI_FAMILY(adapter))
1716 flags |= IRQF_SHARED;
1717
1718 } else {
1719 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1720 handler = qlcnic_msix_intr;
1721 else if (adapter->flags & QLCNIC_MSI_ENABLED)
1722 handler = qlcnic_msi_intr;
1723 else {
1724 flags |= IRQF_SHARED;
1725 if (qlcnic_82xx_check(adapter))
1726 handler = qlcnic_intr;
1727 else
1728 handler = qlcnic_83xx_intr;
1729 }
1730 }
1731 adapter->irq = netdev->irq;
1732
1733 if (adapter->ahw->diag_test != QLCNIC_LOOPBACK_TEST) {
1734 if (qlcnic_82xx_check(adapter) ||
1735 (qlcnic_83xx_check(adapter) &&
1736 (adapter->flags & QLCNIC_MSIX_ENABLED))) {
1737 num_sds_rings = adapter->drv_sds_rings;
1738 for (ring = 0; ring < num_sds_rings; ring++) {
1739 sds_ring = &recv_ctx->sds_rings[ring];
1740 if (qlcnic_82xx_check(adapter) &&
1741 !qlcnic_check_multi_tx(adapter) &&
1742 (ring == (num_sds_rings - 1))) {
1743 if (!(adapter->flags &
1744 QLCNIC_MSIX_ENABLED))
1745 snprintf(sds_ring->name,
1746 sizeof(sds_ring->name),
1747 "qlcnic");
1748 else
1749 snprintf(sds_ring->name,
1750 sizeof(sds_ring->name),
1751 "%s-tx-0-rx-%d",
1752 netdev->name, ring);
1753 } else {
1754 snprintf(sds_ring->name,
1755 sizeof(sds_ring->name),
1756 "%s-rx-%d",
1757 netdev->name, ring);
1758 }
1759 err = request_irq(sds_ring->irq, handler, flags,
1760 sds_ring->name, sds_ring);
1761 if (err)
1762 return err;
1763 }
1764 }
1765 if ((qlcnic_82xx_check(adapter) &&
1766 qlcnic_check_multi_tx(adapter)) ||
1767 (qlcnic_83xx_check(adapter) &&
1768 (adapter->flags & QLCNIC_MSIX_ENABLED) &&
1769 !(adapter->flags & QLCNIC_TX_INTR_SHARED))) {
1770 handler = qlcnic_msix_tx_intr;
1771 for (ring = 0; ring < adapter->drv_tx_rings;
1772 ring++) {
1773 tx_ring = &adapter->tx_ring[ring];
1774 snprintf(tx_ring->name, sizeof(tx_ring->name),
1775 "%s-tx-%d", netdev->name, ring);
1776 err = request_irq(tx_ring->irq, handler, flags,
1777 tx_ring->name, tx_ring);
1778 if (err)
1779 return err;
1780 }
1781 }
1782 }
1783 return 0;
1784 }
1785
1786 static void
qlcnic_free_irq(struct qlcnic_adapter * adapter)1787 qlcnic_free_irq(struct qlcnic_adapter *adapter)
1788 {
1789 int ring;
1790 struct qlcnic_host_sds_ring *sds_ring;
1791 struct qlcnic_host_tx_ring *tx_ring;
1792
1793 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1794
1795 if (adapter->ahw->diag_test != QLCNIC_LOOPBACK_TEST) {
1796 if (qlcnic_82xx_check(adapter) ||
1797 (qlcnic_83xx_check(adapter) &&
1798 (adapter->flags & QLCNIC_MSIX_ENABLED))) {
1799 for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
1800 sds_ring = &recv_ctx->sds_rings[ring];
1801 free_irq(sds_ring->irq, sds_ring);
1802 }
1803 }
1804 if ((qlcnic_83xx_check(adapter) &&
1805 !(adapter->flags & QLCNIC_TX_INTR_SHARED)) ||
1806 (qlcnic_82xx_check(adapter) &&
1807 qlcnic_check_multi_tx(adapter))) {
1808 for (ring = 0; ring < adapter->drv_tx_rings;
1809 ring++) {
1810 tx_ring = &adapter->tx_ring[ring];
1811 if (tx_ring->irq)
1812 free_irq(tx_ring->irq, tx_ring);
1813 }
1814 }
1815 }
1816 }
1817
qlcnic_get_lro_mss_capability(struct qlcnic_adapter * adapter)1818 static void qlcnic_get_lro_mss_capability(struct qlcnic_adapter *adapter)
1819 {
1820 u32 capab = 0;
1821
1822 if (qlcnic_82xx_check(adapter)) {
1823 if (adapter->ahw->extra_capability[0] &
1824 QLCNIC_FW_CAPABILITY_2_LRO_MAX_TCP_SEG)
1825 adapter->flags |= QLCNIC_FW_LRO_MSS_CAP;
1826 } else {
1827 capab = adapter->ahw->capabilities;
1828 if (QLC_83XX_GET_FW_LRO_MSS_CAPABILITY(capab))
1829 adapter->flags |= QLCNIC_FW_LRO_MSS_CAP;
1830 }
1831 }
1832
qlcnic_config_def_intr_coalesce(struct qlcnic_adapter * adapter)1833 static int qlcnic_config_def_intr_coalesce(struct qlcnic_adapter *adapter)
1834 {
1835 struct qlcnic_hardware_context *ahw = adapter->ahw;
1836 int err;
1837
1838 /* Initialize interrupt coalesce parameters */
1839 ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1840
1841 if (qlcnic_83xx_check(adapter)) {
1842 ahw->coal.type = QLCNIC_INTR_COAL_TYPE_RX_TX;
1843 ahw->coal.tx_time_us = QLCNIC_DEF_INTR_COALESCE_TX_TIME_US;
1844 ahw->coal.tx_packets = QLCNIC_DEF_INTR_COALESCE_TX_PACKETS;
1845 ahw->coal.rx_time_us = QLCNIC_DEF_INTR_COALESCE_RX_TIME_US;
1846 ahw->coal.rx_packets = QLCNIC_DEF_INTR_COALESCE_RX_PACKETS;
1847
1848 err = qlcnic_83xx_set_rx_tx_intr_coal(adapter);
1849 } else {
1850 ahw->coal.type = QLCNIC_INTR_COAL_TYPE_RX;
1851 ahw->coal.rx_time_us = QLCNIC_DEF_INTR_COALESCE_RX_TIME_US;
1852 ahw->coal.rx_packets = QLCNIC_DEF_INTR_COALESCE_RX_PACKETS;
1853
1854 err = qlcnic_82xx_set_rx_coalesce(adapter);
1855 }
1856
1857 return err;
1858 }
1859
__qlcnic_up(struct qlcnic_adapter * adapter,struct net_device * netdev)1860 int __qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1861 {
1862 int ring;
1863 struct qlcnic_host_rds_ring *rds_ring;
1864
1865 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1866 return -EIO;
1867
1868 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1869 return 0;
1870
1871 if (qlcnic_set_eswitch_port_config(adapter))
1872 return -EIO;
1873
1874 qlcnic_get_lro_mss_capability(adapter);
1875
1876 if (qlcnic_fw_create_ctx(adapter))
1877 return -EIO;
1878
1879 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1880 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1881 qlcnic_post_rx_buffers(adapter, rds_ring, ring);
1882 }
1883
1884 qlcnic_set_multi(netdev);
1885 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1886
1887 adapter->ahw->linkup = 0;
1888
1889 if (adapter->drv_sds_rings > 1)
1890 qlcnic_config_rss(adapter, 1);
1891
1892 qlcnic_config_def_intr_coalesce(adapter);
1893
1894 if (netdev->features & NETIF_F_LRO)
1895 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1896
1897 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1898 qlcnic_napi_enable(adapter);
1899
1900 qlcnic_linkevent_request(adapter, 1);
1901
1902 adapter->ahw->reset_context = 0;
1903 netif_tx_start_all_queues(netdev);
1904 return 0;
1905 }
1906
qlcnic_up(struct qlcnic_adapter * adapter,struct net_device * netdev)1907 int qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1908 {
1909 int err = 0;
1910
1911 rtnl_lock();
1912 if (netif_running(netdev))
1913 err = __qlcnic_up(adapter, netdev);
1914 rtnl_unlock();
1915
1916 return err;
1917 }
1918
__qlcnic_down(struct qlcnic_adapter * adapter,struct net_device * netdev)1919 void __qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1920 {
1921 int ring;
1922
1923 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1924 return;
1925
1926 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1927 return;
1928
1929 smp_mb();
1930 netif_carrier_off(netdev);
1931 adapter->ahw->linkup = 0;
1932 netif_tx_disable(netdev);
1933
1934 qlcnic_free_mac_list(adapter);
1935
1936 if (adapter->fhash.fnum)
1937 qlcnic_delete_lb_filters(adapter);
1938
1939 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1940 if (qlcnic_sriov_vf_check(adapter))
1941 qlcnic_sriov_cleanup_async_list(&adapter->ahw->sriov->bc);
1942
1943 qlcnic_napi_disable(adapter);
1944
1945 qlcnic_fw_destroy_ctx(adapter);
1946 adapter->flags &= ~QLCNIC_FW_LRO_MSS_CAP;
1947
1948 qlcnic_reset_rx_buffers_list(adapter);
1949
1950 for (ring = 0; ring < adapter->drv_tx_rings; ring++)
1951 qlcnic_release_tx_buffers(adapter, &adapter->tx_ring[ring]);
1952 }
1953
1954 /* Usage: During suspend and firmware recovery module */
1955
qlcnic_down(struct qlcnic_adapter * adapter,struct net_device * netdev)1956 void qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1957 {
1958 rtnl_lock();
1959 if (netif_running(netdev))
1960 __qlcnic_down(adapter, netdev);
1961 rtnl_unlock();
1962
1963 }
1964
1965 int
qlcnic_attach(struct qlcnic_adapter * adapter)1966 qlcnic_attach(struct qlcnic_adapter *adapter)
1967 {
1968 struct net_device *netdev = adapter->netdev;
1969 struct pci_dev *pdev = adapter->pdev;
1970 int err;
1971
1972 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1973 return 0;
1974
1975 err = qlcnic_napi_add(adapter, netdev);
1976 if (err)
1977 return err;
1978
1979 err = qlcnic_alloc_sw_resources(adapter);
1980 if (err) {
1981 dev_err(&pdev->dev, "Error in setting sw resources\n");
1982 goto err_out_napi_del;
1983 }
1984
1985 err = qlcnic_alloc_hw_resources(adapter);
1986 if (err) {
1987 dev_err(&pdev->dev, "Error in setting hw resources\n");
1988 goto err_out_free_sw;
1989 }
1990
1991 err = qlcnic_request_irq(adapter);
1992 if (err) {
1993 dev_err(&pdev->dev, "failed to setup interrupt\n");
1994 goto err_out_free_hw;
1995 }
1996
1997 qlcnic_create_sysfs_entries(adapter);
1998
1999 if (qlcnic_encap_rx_offload(adapter))
2000 udp_tunnel_nic_reset_ntf(netdev);
2001
2002 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
2003 return 0;
2004
2005 err_out_free_hw:
2006 qlcnic_free_hw_resources(adapter);
2007 err_out_free_sw:
2008 qlcnic_free_sw_resources(adapter);
2009 err_out_napi_del:
2010 qlcnic_napi_del(adapter);
2011 return err;
2012 }
2013
qlcnic_detach(struct qlcnic_adapter * adapter)2014 void qlcnic_detach(struct qlcnic_adapter *adapter)
2015 {
2016 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2017 return;
2018
2019 qlcnic_remove_sysfs_entries(adapter);
2020
2021 qlcnic_free_hw_resources(adapter);
2022 qlcnic_release_rx_buffers(adapter);
2023 qlcnic_free_irq(adapter);
2024 qlcnic_napi_del(adapter);
2025 qlcnic_free_sw_resources(adapter);
2026
2027 adapter->is_up = 0;
2028 }
2029
qlcnic_diag_free_res(struct net_device * netdev,int drv_sds_rings)2030 void qlcnic_diag_free_res(struct net_device *netdev, int drv_sds_rings)
2031 {
2032 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2033 struct qlcnic_host_sds_ring *sds_ring;
2034 int drv_tx_rings = adapter->drv_tx_rings;
2035 int ring;
2036
2037 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
2038 if (adapter->ahw->diag_test == QLCNIC_INTERRUPT_TEST) {
2039 for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
2040 sds_ring = &adapter->recv_ctx->sds_rings[ring];
2041 qlcnic_disable_sds_intr(adapter, sds_ring);
2042 }
2043 }
2044
2045 qlcnic_fw_destroy_ctx(adapter);
2046
2047 qlcnic_detach(adapter);
2048
2049 adapter->ahw->diag_test = 0;
2050 adapter->drv_sds_rings = drv_sds_rings;
2051 adapter->drv_tx_rings = drv_tx_rings;
2052
2053 if (qlcnic_attach(adapter))
2054 goto out;
2055
2056 if (netif_running(netdev))
2057 __qlcnic_up(adapter, netdev);
2058 out:
2059 netif_device_attach(netdev);
2060 }
2061
qlcnic_alloc_adapter_resources(struct qlcnic_adapter * adapter)2062 static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
2063 {
2064 struct qlcnic_hardware_context *ahw = adapter->ahw;
2065 int err = 0;
2066
2067 adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
2068 GFP_KERNEL);
2069 if (!adapter->recv_ctx) {
2070 err = -ENOMEM;
2071 goto err_out;
2072 }
2073
2074 if (qlcnic_83xx_check(adapter)) {
2075 ahw->coal.type = QLCNIC_INTR_COAL_TYPE_RX_TX;
2076 ahw->coal.tx_time_us = QLCNIC_DEF_INTR_COALESCE_TX_TIME_US;
2077 ahw->coal.tx_packets = QLCNIC_DEF_INTR_COALESCE_TX_PACKETS;
2078 ahw->coal.rx_time_us = QLCNIC_DEF_INTR_COALESCE_RX_TIME_US;
2079 ahw->coal.rx_packets = QLCNIC_DEF_INTR_COALESCE_RX_PACKETS;
2080 } else {
2081 ahw->coal.type = QLCNIC_INTR_COAL_TYPE_RX;
2082 ahw->coal.rx_time_us = QLCNIC_DEF_INTR_COALESCE_RX_TIME_US;
2083 ahw->coal.rx_packets = QLCNIC_DEF_INTR_COALESCE_RX_PACKETS;
2084 }
2085
2086 /* clear stats */
2087 memset(&adapter->stats, 0, sizeof(adapter->stats));
2088 err_out:
2089 return err;
2090 }
2091
qlcnic_free_adapter_resources(struct qlcnic_adapter * adapter)2092 static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
2093 {
2094 struct qlcnic_fw_dump *fw_dump = &adapter->ahw->fw_dump;
2095
2096 kfree(adapter->recv_ctx);
2097 adapter->recv_ctx = NULL;
2098
2099 if (fw_dump->tmpl_hdr) {
2100 vfree(fw_dump->tmpl_hdr);
2101 fw_dump->tmpl_hdr = NULL;
2102 }
2103
2104 if (fw_dump->dma_buffer) {
2105 dma_free_coherent(&adapter->pdev->dev, QLC_PEX_DMA_READ_SIZE,
2106 fw_dump->dma_buffer, fw_dump->phys_addr);
2107 fw_dump->dma_buffer = NULL;
2108 }
2109
2110 kfree(adapter->ahw->reset.buff);
2111 adapter->ahw->fw_dump.tmpl_hdr = NULL;
2112 }
2113
qlcnic_diag_alloc_res(struct net_device * netdev,int test)2114 int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
2115 {
2116 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2117 struct qlcnic_host_sds_ring *sds_ring;
2118 struct qlcnic_host_rds_ring *rds_ring;
2119 int ring;
2120 int ret;
2121
2122 netif_device_detach(netdev);
2123
2124 if (netif_running(netdev))
2125 __qlcnic_down(adapter, netdev);
2126
2127 qlcnic_detach(adapter);
2128
2129 adapter->drv_sds_rings = QLCNIC_SINGLE_RING;
2130 adapter->ahw->diag_test = test;
2131 adapter->ahw->linkup = 0;
2132
2133 ret = qlcnic_attach(adapter);
2134 if (ret) {
2135 netif_device_attach(netdev);
2136 return ret;
2137 }
2138
2139 ret = qlcnic_fw_create_ctx(adapter);
2140 if (ret) {
2141 qlcnic_detach(adapter);
2142 netif_device_attach(netdev);
2143 return ret;
2144 }
2145
2146 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
2147 rds_ring = &adapter->recv_ctx->rds_rings[ring];
2148 qlcnic_post_rx_buffers(adapter, rds_ring, ring);
2149 }
2150
2151 if (adapter->ahw->diag_test == QLCNIC_INTERRUPT_TEST) {
2152 for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
2153 sds_ring = &adapter->recv_ctx->sds_rings[ring];
2154 qlcnic_enable_sds_intr(adapter, sds_ring);
2155 }
2156 }
2157
2158 if (adapter->ahw->diag_test == QLCNIC_LOOPBACK_TEST) {
2159 adapter->ahw->loopback_state = 0;
2160 qlcnic_linkevent_request(adapter, 1);
2161 }
2162
2163 set_bit(__QLCNIC_DEV_UP, &adapter->state);
2164
2165 return 0;
2166 }
2167
2168 /* Reset context in hardware only */
2169 static int
qlcnic_reset_hw_context(struct qlcnic_adapter * adapter)2170 qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
2171 {
2172 struct net_device *netdev = adapter->netdev;
2173
2174 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2175 return -EBUSY;
2176
2177 netif_device_detach(netdev);
2178
2179 qlcnic_down(adapter, netdev);
2180
2181 qlcnic_up(adapter, netdev);
2182
2183 netif_device_attach(netdev);
2184
2185 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2186 netdev_info(adapter->netdev, "%s: soft reset complete\n", __func__);
2187 return 0;
2188 }
2189
2190 int
qlcnic_reset_context(struct qlcnic_adapter * adapter)2191 qlcnic_reset_context(struct qlcnic_adapter *adapter)
2192 {
2193 int err = 0;
2194 struct net_device *netdev = adapter->netdev;
2195
2196 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2197 return -EBUSY;
2198
2199 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
2200
2201 netif_device_detach(netdev);
2202
2203 if (netif_running(netdev))
2204 __qlcnic_down(adapter, netdev);
2205
2206 qlcnic_detach(adapter);
2207
2208 if (netif_running(netdev)) {
2209 err = qlcnic_attach(adapter);
2210 if (!err) {
2211 __qlcnic_up(adapter, netdev);
2212 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
2213 }
2214 }
2215
2216 netif_device_attach(netdev);
2217 }
2218
2219 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2220 return err;
2221 }
2222
qlcnic_82xx_set_mac_filter_count(struct qlcnic_adapter * adapter)2223 static void qlcnic_82xx_set_mac_filter_count(struct qlcnic_adapter *adapter)
2224 {
2225 struct qlcnic_hardware_context *ahw = adapter->ahw;
2226 u16 act_pci_fn = ahw->total_nic_func;
2227 u16 count;
2228
2229 ahw->max_mc_count = QLCNIC_MAX_MC_COUNT;
2230 if (act_pci_fn <= 2)
2231 count = (QLCNIC_MAX_UC_COUNT - QLCNIC_MAX_MC_COUNT) /
2232 act_pci_fn;
2233 else
2234 count = (QLCNIC_LB_MAX_FILTERS - QLCNIC_MAX_MC_COUNT) /
2235 act_pci_fn;
2236 ahw->max_uc_count = count;
2237 }
2238
qlcnic_set_real_num_queues(struct qlcnic_adapter * adapter,u8 tx_queues,u8 rx_queues)2239 static int qlcnic_set_real_num_queues(struct qlcnic_adapter *adapter,
2240 u8 tx_queues, u8 rx_queues)
2241 {
2242 struct net_device *netdev = adapter->netdev;
2243 int err = 0;
2244
2245 if (tx_queues) {
2246 err = netif_set_real_num_tx_queues(netdev, tx_queues);
2247 if (err) {
2248 netdev_err(netdev, "failed to set %d Tx queues\n",
2249 tx_queues);
2250 return err;
2251 }
2252 }
2253
2254 if (rx_queues) {
2255 err = netif_set_real_num_rx_queues(netdev, rx_queues);
2256 if (err)
2257 netdev_err(netdev, "failed to set %d Rx queues\n",
2258 rx_queues);
2259 }
2260
2261 return err;
2262 }
2263
2264 int
qlcnic_setup_netdev(struct qlcnic_adapter * adapter,struct net_device * netdev,int pci_using_dac)2265 qlcnic_setup_netdev(struct qlcnic_adapter *adapter, struct net_device *netdev,
2266 int pci_using_dac)
2267 {
2268 int err;
2269 struct pci_dev *pdev = adapter->pdev;
2270
2271 adapter->rx_csum = 1;
2272 adapter->ahw->mc_enabled = 0;
2273 qlcnic_set_mac_filter_count(adapter);
2274
2275 netdev->netdev_ops = &qlcnic_netdev_ops;
2276 netdev->watchdog_timeo = QLCNIC_WATCHDOG_TIMEOUTVALUE * HZ;
2277
2278 qlcnic_change_mtu(netdev, netdev->mtu);
2279
2280 netdev->ethtool_ops = (qlcnic_sriov_vf_check(adapter)) ?
2281 &qlcnic_sriov_vf_ethtool_ops : &qlcnic_ethtool_ops;
2282
2283 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
2284 NETIF_F_IPV6_CSUM | NETIF_F_GRO |
2285 NETIF_F_HW_VLAN_CTAG_RX);
2286 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
2287 NETIF_F_IPV6_CSUM);
2288
2289 if (QLCNIC_IS_TSO_CAPABLE(adapter)) {
2290 netdev->features |= (NETIF_F_TSO | NETIF_F_TSO6);
2291 netdev->vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
2292 }
2293
2294 if (pci_using_dac) {
2295 netdev->features |= NETIF_F_HIGHDMA;
2296 netdev->vlan_features |= NETIF_F_HIGHDMA;
2297 }
2298
2299 if (qlcnic_vlan_tx_check(adapter))
2300 netdev->features |= (NETIF_F_HW_VLAN_CTAG_TX);
2301
2302 if (qlcnic_sriov_vf_check(adapter))
2303 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2304
2305 if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
2306 netdev->features |= NETIF_F_LRO;
2307
2308 if (qlcnic_encap_tx_offload(adapter)) {
2309 netdev->features |= NETIF_F_GSO_UDP_TUNNEL;
2310
2311 /* encapsulation Tx offload supported by Adapter */
2312 netdev->hw_enc_features = NETIF_F_IP_CSUM |
2313 NETIF_F_GSO_UDP_TUNNEL |
2314 NETIF_F_TSO |
2315 NETIF_F_TSO6;
2316 }
2317
2318 if (qlcnic_encap_rx_offload(adapter)) {
2319 netdev->hw_enc_features |= NETIF_F_RXCSUM;
2320
2321 netdev->udp_tunnel_nic_info = &qlcnic_udp_tunnels;
2322 }
2323
2324 netdev->hw_features = netdev->features;
2325 netdev->priv_flags |= IFF_UNICAST_FLT;
2326 netdev->irq = adapter->msix_entries[0].vector;
2327
2328 /* MTU range: 68 - 9600 */
2329 netdev->min_mtu = P3P_MIN_MTU;
2330 netdev->max_mtu = P3P_MAX_MTU;
2331
2332 err = qlcnic_set_real_num_queues(adapter, adapter->drv_tx_rings,
2333 adapter->drv_sds_rings);
2334 if (err)
2335 return err;
2336
2337 qlcnic_dcb_init_dcbnl_ops(adapter->dcb);
2338
2339 err = register_netdev(netdev);
2340 if (err) {
2341 dev_err(&pdev->dev, "failed to register net device\n");
2342 return err;
2343 }
2344
2345 return 0;
2346 }
2347
qlcnic_set_dma_mask(struct pci_dev * pdev,int * pci_using_dac)2348 static int qlcnic_set_dma_mask(struct pci_dev *pdev, int *pci_using_dac)
2349 {
2350 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
2351 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
2352 *pci_using_dac = 1;
2353 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
2354 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
2355 *pci_using_dac = 0;
2356 else {
2357 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
2358 return -EIO;
2359 }
2360
2361 return 0;
2362 }
2363
qlcnic_free_tx_rings(struct qlcnic_adapter * adapter)2364 void qlcnic_free_tx_rings(struct qlcnic_adapter *adapter)
2365 {
2366 int ring;
2367 struct qlcnic_host_tx_ring *tx_ring;
2368
2369 for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
2370 tx_ring = &adapter->tx_ring[ring];
2371 if (tx_ring) {
2372 vfree(tx_ring->cmd_buf_arr);
2373 tx_ring->cmd_buf_arr = NULL;
2374 }
2375 }
2376 kfree(adapter->tx_ring);
2377 }
2378
qlcnic_alloc_tx_rings(struct qlcnic_adapter * adapter,struct net_device * netdev)2379 int qlcnic_alloc_tx_rings(struct qlcnic_adapter *adapter,
2380 struct net_device *netdev)
2381 {
2382 int ring, vector, index;
2383 struct qlcnic_host_tx_ring *tx_ring;
2384 struct qlcnic_cmd_buffer *cmd_buf_arr;
2385
2386 tx_ring = kcalloc(adapter->drv_tx_rings,
2387 sizeof(struct qlcnic_host_tx_ring), GFP_KERNEL);
2388 if (tx_ring == NULL)
2389 return -ENOMEM;
2390
2391 adapter->tx_ring = tx_ring;
2392
2393 for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
2394 tx_ring = &adapter->tx_ring[ring];
2395 tx_ring->num_desc = adapter->num_txd;
2396 tx_ring->txq = netdev_get_tx_queue(netdev, ring);
2397 cmd_buf_arr = vzalloc(TX_BUFF_RINGSIZE(tx_ring));
2398 if (cmd_buf_arr == NULL) {
2399 qlcnic_free_tx_rings(adapter);
2400 return -ENOMEM;
2401 }
2402 tx_ring->cmd_buf_arr = cmd_buf_arr;
2403 spin_lock_init(&tx_ring->tx_clean_lock);
2404 }
2405
2406 if (qlcnic_83xx_check(adapter) ||
2407 (qlcnic_82xx_check(adapter) && qlcnic_check_multi_tx(adapter))) {
2408 for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
2409 tx_ring = &adapter->tx_ring[ring];
2410 tx_ring->adapter = adapter;
2411 if (adapter->flags & QLCNIC_MSIX_ENABLED) {
2412 index = adapter->drv_sds_rings + ring;
2413 vector = adapter->msix_entries[index].vector;
2414 tx_ring->irq = vector;
2415 }
2416 }
2417 }
2418
2419 return 0;
2420 }
2421
qlcnic_set_drv_version(struct qlcnic_adapter * adapter)2422 void qlcnic_set_drv_version(struct qlcnic_adapter *adapter)
2423 {
2424 struct qlcnic_hardware_context *ahw = adapter->ahw;
2425 u32 fw_cmd = 0;
2426
2427 if (qlcnic_82xx_check(adapter))
2428 fw_cmd = QLCNIC_CMD_82XX_SET_DRV_VER;
2429 else if (qlcnic_83xx_check(adapter))
2430 fw_cmd = QLCNIC_CMD_83XX_SET_DRV_VER;
2431
2432 if (ahw->extra_capability[0] & QLCNIC_FW_CAPABILITY_SET_DRV_VER)
2433 qlcnic_fw_cmd_set_drv_version(adapter, fw_cmd);
2434 }
2435
2436 /* Reset firmware API lock */
qlcnic_reset_api_lock(struct qlcnic_adapter * adapter)2437 static void qlcnic_reset_api_lock(struct qlcnic_adapter *adapter)
2438 {
2439 qlcnic_api_lock(adapter);
2440 qlcnic_api_unlock(adapter);
2441 }
2442
2443
2444 static int
qlcnic_probe(struct pci_dev * pdev,const struct pci_device_id * ent)2445 qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2446 {
2447 struct net_device *netdev = NULL;
2448 struct qlcnic_adapter *adapter = NULL;
2449 struct qlcnic_hardware_context *ahw;
2450 int err, pci_using_dac = -1;
2451 char board_name[QLCNIC_MAX_BOARD_NAME_LEN + 19]; /* MAC + ": " + name */
2452
2453 err = pci_enable_device(pdev);
2454 if (err)
2455 return err;
2456
2457 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2458 err = -ENODEV;
2459 goto err_out_disable_pdev;
2460 }
2461
2462 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
2463 if (err)
2464 goto err_out_disable_pdev;
2465
2466 err = pci_request_regions(pdev, qlcnic_driver_name);
2467 if (err)
2468 goto err_out_disable_pdev;
2469
2470 pci_set_master(pdev);
2471 pci_enable_pcie_error_reporting(pdev);
2472
2473 ahw = kzalloc(sizeof(struct qlcnic_hardware_context), GFP_KERNEL);
2474 if (!ahw) {
2475 err = -ENOMEM;
2476 goto err_out_free_res;
2477 }
2478
2479 switch (ent->device) {
2480 case PCI_DEVICE_ID_QLOGIC_QLE824X:
2481 ahw->hw_ops = &qlcnic_hw_ops;
2482 ahw->reg_tbl = (u32 *) qlcnic_reg_tbl;
2483 break;
2484 case PCI_DEVICE_ID_QLOGIC_QLE834X:
2485 case PCI_DEVICE_ID_QLOGIC_QLE8830:
2486 case PCI_DEVICE_ID_QLOGIC_QLE844X:
2487 qlcnic_83xx_register_map(ahw);
2488 break;
2489 case PCI_DEVICE_ID_QLOGIC_VF_QLE834X:
2490 case PCI_DEVICE_ID_QLOGIC_VF_QLE8C30:
2491 case PCI_DEVICE_ID_QLOGIC_VF_QLE844X:
2492 qlcnic_sriov_vf_register_map(ahw);
2493 break;
2494 default:
2495 err = -EINVAL;
2496 goto err_out_free_hw_res;
2497 }
2498
2499 err = qlcnic_setup_pci_map(pdev, ahw);
2500 if (err)
2501 goto err_out_free_hw_res;
2502
2503 netdev = alloc_etherdev_mq(sizeof(struct qlcnic_adapter),
2504 QLCNIC_MAX_TX_RINGS);
2505 if (!netdev) {
2506 err = -ENOMEM;
2507 goto err_out_iounmap;
2508 }
2509
2510 SET_NETDEV_DEV(netdev, &pdev->dev);
2511
2512 adapter = netdev_priv(netdev);
2513 adapter->netdev = netdev;
2514 adapter->pdev = pdev;
2515 adapter->ahw = ahw;
2516
2517 adapter->qlcnic_wq = create_singlethread_workqueue("qlcnic");
2518 if (adapter->qlcnic_wq == NULL) {
2519 err = -ENOMEM;
2520 dev_err(&pdev->dev, "Failed to create workqueue\n");
2521 goto err_out_free_netdev;
2522 }
2523
2524 err = qlcnic_alloc_adapter_resources(adapter);
2525 if (err)
2526 goto err_out_free_wq;
2527
2528 adapter->dev_rst_time = jiffies;
2529 ahw->revision_id = pdev->revision;
2530 ahw->max_vnic_func = qlcnic_get_vnic_func_count(adapter);
2531 if (qlcnic_mac_learn == FDB_MAC_LEARN)
2532 adapter->fdb_mac_learn = true;
2533 else if (qlcnic_mac_learn == DRV_MAC_LEARN)
2534 adapter->drv_mac_learn = true;
2535
2536 rwlock_init(&adapter->ahw->crb_lock);
2537 mutex_init(&adapter->ahw->mem_lock);
2538
2539 INIT_LIST_HEAD(&adapter->mac_list);
2540
2541 qlcnic_register_dcb(adapter);
2542
2543 if (qlcnic_82xx_check(adapter)) {
2544 qlcnic_check_vf(adapter, ent);
2545 adapter->portnum = adapter->ahw->pci_func;
2546 qlcnic_reset_api_lock(adapter);
2547 err = qlcnic_start_firmware(adapter);
2548 if (err) {
2549 dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n"
2550 "\t\tIf reboot doesn't help, try flashing the card\n");
2551 goto err_out_maintenance_mode;
2552 }
2553
2554 /* compute and set default and max tx/sds rings */
2555 if (adapter->ahw->msix_supported) {
2556 if (qlcnic_check_multi_tx_capability(adapter) == 1)
2557 qlcnic_set_tx_ring_count(adapter,
2558 QLCNIC_SINGLE_RING);
2559 else
2560 qlcnic_set_tx_ring_count(adapter,
2561 QLCNIC_DEF_TX_RINGS);
2562 qlcnic_set_sds_ring_count(adapter,
2563 QLCNIC_DEF_SDS_RINGS);
2564 } else {
2565 qlcnic_set_tx_ring_count(adapter, QLCNIC_SINGLE_RING);
2566 qlcnic_set_sds_ring_count(adapter, QLCNIC_SINGLE_RING);
2567 }
2568
2569 err = qlcnic_setup_idc_param(adapter);
2570 if (err)
2571 goto err_out_free_hw;
2572
2573 adapter->flags |= QLCNIC_NEED_FLR;
2574
2575 } else if (qlcnic_83xx_check(adapter)) {
2576 qlcnic_83xx_check_vf(adapter, ent);
2577 adapter->portnum = adapter->ahw->pci_func;
2578 err = qlcnic_83xx_init(adapter, pci_using_dac);
2579 if (err) {
2580 switch (err) {
2581 case -ENOTRECOVERABLE:
2582 dev_err(&pdev->dev, "Adapter initialization failed due to a faulty hardware\n");
2583 dev_err(&pdev->dev, "Please replace the adapter with new one and return the faulty adapter for repair\n");
2584 goto err_out_free_hw;
2585 case -ENOMEM:
2586 dev_err(&pdev->dev, "Adapter initialization failed. Please reboot\n");
2587 goto err_out_free_hw;
2588 case -EOPNOTSUPP:
2589 dev_err(&pdev->dev, "Adapter initialization failed\n");
2590 goto err_out_free_hw;
2591 default:
2592 dev_err(&pdev->dev, "Adapter initialization failed. Driver will load in maintenance mode to recover the adapter using the application\n");
2593 goto err_out_maintenance_mode;
2594 }
2595 }
2596
2597 if (qlcnic_sriov_vf_check(adapter))
2598 return 0;
2599 } else {
2600 dev_err(&pdev->dev,
2601 "%s: failed. Please Reboot\n", __func__);
2602 err = -ENODEV;
2603 goto err_out_free_hw;
2604 }
2605
2606 if (qlcnic_read_mac_addr(adapter))
2607 dev_warn(&pdev->dev, "failed to read mac addr\n");
2608
2609 qlcnic_read_phys_port_id(adapter);
2610
2611 if (adapter->portnum == 0) {
2612 qlcnic_get_board_name(adapter, board_name);
2613
2614 pr_info("%s: %s Board Chip rev 0x%x\n",
2615 module_name(THIS_MODULE),
2616 board_name, adapter->ahw->revision_id);
2617 }
2618
2619 if (qlcnic_83xx_check(adapter) && !qlcnic_use_msi_x &&
2620 !!qlcnic_use_msi)
2621 dev_warn(&pdev->dev,
2622 "Device does not support MSI interrupts\n");
2623
2624 if (qlcnic_82xx_check(adapter)) {
2625 err = qlcnic_dcb_enable(adapter->dcb);
2626 if (err) {
2627 qlcnic_dcb_free(adapter->dcb);
2628 dev_err(&pdev->dev, "Failed to enable DCB\n");
2629 goto err_out_free_hw;
2630 }
2631
2632 qlcnic_dcb_get_info(adapter->dcb);
2633 err = qlcnic_setup_intr(adapter);
2634
2635 if (err) {
2636 dev_err(&pdev->dev, "Failed to setup interrupt\n");
2637 goto err_out_disable_msi;
2638 }
2639 }
2640
2641 err = qlcnic_get_act_pci_func(adapter);
2642 if (err)
2643 goto err_out_disable_mbx_intr;
2644
2645 if (adapter->portnum == 0)
2646 qlcnic_set_drv_version(adapter);
2647
2648 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
2649 if (err)
2650 goto err_out_disable_mbx_intr;
2651
2652 pci_set_drvdata(pdev, adapter);
2653
2654 if (qlcnic_82xx_check(adapter))
2655 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2656 FW_POLL_DELAY);
2657
2658 switch (adapter->ahw->port_type) {
2659 case QLCNIC_GBE:
2660 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
2661 adapter->netdev->name);
2662 break;
2663 case QLCNIC_XGBE:
2664 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
2665 adapter->netdev->name);
2666 break;
2667 }
2668
2669 if (adapter->drv_mac_learn)
2670 qlcnic_alloc_lb_filters_mem(adapter);
2671
2672 qlcnic_add_sysfs(adapter);
2673 qlcnic_register_hwmon_dev(adapter);
2674 return 0;
2675
2676 err_out_disable_mbx_intr:
2677 if (qlcnic_83xx_check(adapter))
2678 qlcnic_83xx_free_mbx_intr(adapter);
2679
2680 err_out_disable_msi:
2681 qlcnic_teardown_intr(adapter);
2682 qlcnic_cancel_idc_work(adapter);
2683 qlcnic_clr_all_drv_state(adapter, 0);
2684
2685 err_out_free_hw:
2686 qlcnic_free_adapter_resources(adapter);
2687
2688 err_out_free_wq:
2689 destroy_workqueue(adapter->qlcnic_wq);
2690
2691 err_out_free_netdev:
2692 free_netdev(netdev);
2693
2694 err_out_iounmap:
2695 qlcnic_cleanup_pci_map(ahw);
2696
2697 err_out_free_hw_res:
2698 kfree(ahw);
2699
2700 err_out_free_res:
2701 pci_disable_pcie_error_reporting(pdev);
2702 pci_release_regions(pdev);
2703
2704 err_out_disable_pdev:
2705 pci_disable_device(pdev);
2706 return err;
2707
2708 err_out_maintenance_mode:
2709 set_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state);
2710 netdev->netdev_ops = &qlcnic_netdev_failed_ops;
2711 netdev->ethtool_ops = &qlcnic_ethtool_failed_ops;
2712 ahw->port_type = QLCNIC_XGBE;
2713
2714 if (qlcnic_83xx_check(adapter))
2715 adapter->tgt_status_reg = NULL;
2716 else
2717 ahw->board_type = QLCNIC_BRDTYPE_P3P_10G_SFP_PLUS;
2718
2719 err = register_netdev(netdev);
2720
2721 if (err) {
2722 dev_err(&pdev->dev, "Failed to register net device\n");
2723 qlcnic_clr_all_drv_state(adapter, 0);
2724 goto err_out_free_hw;
2725 }
2726
2727 pci_set_drvdata(pdev, adapter);
2728 qlcnic_add_sysfs(adapter);
2729
2730 return 0;
2731 }
2732
qlcnic_remove(struct pci_dev * pdev)2733 static void qlcnic_remove(struct pci_dev *pdev)
2734 {
2735 struct qlcnic_adapter *adapter;
2736 struct net_device *netdev;
2737 struct qlcnic_hardware_context *ahw;
2738
2739 adapter = pci_get_drvdata(pdev);
2740 if (adapter == NULL)
2741 return;
2742
2743 netdev = adapter->netdev;
2744
2745 qlcnic_cancel_idc_work(adapter);
2746 qlcnic_sriov_pf_disable(adapter);
2747 ahw = adapter->ahw;
2748
2749 unregister_netdev(netdev);
2750 qlcnic_sriov_cleanup(adapter);
2751
2752 if (qlcnic_83xx_check(adapter)) {
2753 qlcnic_83xx_initialize_nic(adapter, 0);
2754 cancel_delayed_work_sync(&adapter->idc_aen_work);
2755 qlcnic_83xx_free_mbx_intr(adapter);
2756 qlcnic_83xx_detach_mailbox_work(adapter);
2757 qlcnic_83xx_free_mailbox(ahw->mailbox);
2758 kfree(ahw->fw_info);
2759 }
2760
2761 qlcnic_dcb_free(adapter->dcb);
2762 qlcnic_detach(adapter);
2763 kfree(adapter->npars);
2764 kfree(adapter->eswitch);
2765
2766 if (qlcnic_82xx_check(adapter))
2767 qlcnic_clr_all_drv_state(adapter, 0);
2768
2769 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2770
2771 qlcnic_free_lb_filters_mem(adapter);
2772
2773 qlcnic_teardown_intr(adapter);
2774
2775 qlcnic_remove_sysfs(adapter);
2776
2777 qlcnic_unregister_hwmon_dev(adapter);
2778
2779 qlcnic_cleanup_pci_map(adapter->ahw);
2780
2781 qlcnic_release_firmware(adapter);
2782
2783 pci_disable_pcie_error_reporting(pdev);
2784 pci_release_regions(pdev);
2785 pci_disable_device(pdev);
2786
2787 if (adapter->qlcnic_wq) {
2788 destroy_workqueue(adapter->qlcnic_wq);
2789 adapter->qlcnic_wq = NULL;
2790 }
2791
2792 qlcnic_free_adapter_resources(adapter);
2793 kfree(ahw);
2794 free_netdev(netdev);
2795 }
2796
qlcnic_shutdown(struct pci_dev * pdev)2797 static void qlcnic_shutdown(struct pci_dev *pdev)
2798 {
2799 if (__qlcnic_shutdown(pdev))
2800 return;
2801
2802 pci_disable_device(pdev);
2803 }
2804
qlcnic_suspend(struct device * dev_d)2805 static int __maybe_unused qlcnic_suspend(struct device *dev_d)
2806 {
2807 return __qlcnic_shutdown(to_pci_dev(dev_d));
2808 }
2809
qlcnic_resume(struct device * dev_d)2810 static int __maybe_unused qlcnic_resume(struct device *dev_d)
2811 {
2812 struct qlcnic_adapter *adapter = dev_get_drvdata(dev_d);
2813
2814 return __qlcnic_resume(adapter);
2815 }
2816
qlcnic_open(struct net_device * netdev)2817 static int qlcnic_open(struct net_device *netdev)
2818 {
2819 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2820 int err;
2821
2822 if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state)) {
2823 netdev_err(netdev, "%s: Device is in non-operational state\n",
2824 __func__);
2825
2826 return -EIO;
2827 }
2828
2829 netif_carrier_off(netdev);
2830
2831 err = qlcnic_attach(adapter);
2832 if (err)
2833 return err;
2834
2835 err = __qlcnic_up(adapter, netdev);
2836 if (err)
2837 qlcnic_detach(adapter);
2838
2839 return err;
2840 }
2841
2842 /*
2843 * qlcnic_close - Disables a network interface entry point
2844 */
qlcnic_close(struct net_device * netdev)2845 static int qlcnic_close(struct net_device *netdev)
2846 {
2847 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2848
2849 __qlcnic_down(adapter, netdev);
2850
2851 return 0;
2852 }
2853
2854 #define QLCNIC_VF_LB_BUCKET_SIZE 1
2855
qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter * adapter)2856 void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
2857 {
2858 void *head;
2859 int i;
2860 struct net_device *netdev = adapter->netdev;
2861 u32 filter_size = 0;
2862 u16 act_pci_func = 0;
2863
2864 if (adapter->fhash.fmax && adapter->fhash.fhead)
2865 return;
2866
2867 act_pci_func = adapter->ahw->total_nic_func;
2868 spin_lock_init(&adapter->mac_learn_lock);
2869 spin_lock_init(&adapter->rx_mac_learn_lock);
2870
2871 if (qlcnic_sriov_vf_check(adapter)) {
2872 filter_size = QLCNIC_83XX_SRIOV_VF_MAX_MAC - 1;
2873 adapter->fhash.fbucket_size = QLCNIC_VF_LB_BUCKET_SIZE;
2874 } else if (qlcnic_82xx_check(adapter)) {
2875 filter_size = QLCNIC_LB_MAX_FILTERS;
2876 adapter->fhash.fbucket_size = QLCNIC_LB_BUCKET_SIZE;
2877 } else {
2878 filter_size = QLC_83XX_LB_MAX_FILTERS;
2879 adapter->fhash.fbucket_size = QLC_83XX_LB_BUCKET_SIZE;
2880 }
2881
2882 head = kcalloc(adapter->fhash.fbucket_size,
2883 sizeof(struct hlist_head), GFP_ATOMIC);
2884
2885 if (!head)
2886 return;
2887
2888 adapter->fhash.fmax = (filter_size / act_pci_func);
2889 adapter->fhash.fhead = head;
2890
2891 netdev_info(netdev, "active nic func = %d, mac filter size=%d\n",
2892 act_pci_func, adapter->fhash.fmax);
2893
2894 for (i = 0; i < adapter->fhash.fbucket_size; i++)
2895 INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
2896
2897 adapter->rx_fhash.fbucket_size = adapter->fhash.fbucket_size;
2898
2899 head = kcalloc(adapter->rx_fhash.fbucket_size,
2900 sizeof(struct hlist_head), GFP_ATOMIC);
2901
2902 if (!head)
2903 return;
2904
2905 adapter->rx_fhash.fmax = (filter_size / act_pci_func);
2906 adapter->rx_fhash.fhead = head;
2907
2908 for (i = 0; i < adapter->rx_fhash.fbucket_size; i++)
2909 INIT_HLIST_HEAD(&adapter->rx_fhash.fhead[i]);
2910 }
2911
qlcnic_free_lb_filters_mem(struct qlcnic_adapter * adapter)2912 static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
2913 {
2914 if (adapter->fhash.fmax)
2915 kfree(adapter->fhash.fhead);
2916
2917 adapter->fhash.fhead = NULL;
2918 adapter->fhash.fmax = 0;
2919
2920 if (adapter->rx_fhash.fmax)
2921 kfree(adapter->rx_fhash.fhead);
2922
2923 adapter->rx_fhash.fmax = 0;
2924 adapter->rx_fhash.fhead = NULL;
2925 }
2926
qlcnic_check_temp(struct qlcnic_adapter * adapter)2927 int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2928 {
2929 struct net_device *netdev = adapter->netdev;
2930 u32 temp_state, temp_val, temp = 0;
2931 int rv = 0;
2932
2933 if (qlcnic_83xx_check(adapter))
2934 temp = QLCRDX(adapter->ahw, QLC_83XX_ASIC_TEMP);
2935
2936 if (qlcnic_82xx_check(adapter))
2937 temp = QLC_SHARED_REG_RD32(adapter, QLCNIC_ASIC_TEMP);
2938
2939 temp_state = qlcnic_get_temp_state(temp);
2940 temp_val = qlcnic_get_temp_val(temp);
2941
2942 if (temp_state == QLCNIC_TEMP_PANIC) {
2943 dev_err(&netdev->dev,
2944 "Device temperature %d degrees C exceeds"
2945 " maximum allowed. Hardware has been shut down.\n",
2946 temp_val);
2947 rv = 1;
2948 } else if (temp_state == QLCNIC_TEMP_WARN) {
2949 if (adapter->ahw->temp == QLCNIC_TEMP_NORMAL) {
2950 dev_err(&netdev->dev,
2951 "Device temperature %d degrees C "
2952 "exceeds operating range."
2953 " Immediate action needed.\n",
2954 temp_val);
2955 }
2956 } else {
2957 if (adapter->ahw->temp == QLCNIC_TEMP_WARN) {
2958 dev_info(&netdev->dev,
2959 "Device temperature is now %d degrees C"
2960 " in normal range.\n", temp_val);
2961 }
2962 }
2963 adapter->ahw->temp = temp_state;
2964 return rv;
2965 }
2966
dump_tx_ring_desc(struct qlcnic_host_tx_ring * tx_ring)2967 static inline void dump_tx_ring_desc(struct qlcnic_host_tx_ring *tx_ring)
2968 {
2969 int i;
2970
2971 for (i = 0; i < tx_ring->num_desc; i++) {
2972 pr_info("TX Desc: %d\n", i);
2973 print_hex_dump(KERN_INFO, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
2974 &tx_ring->desc_head[i],
2975 sizeof(struct cmd_desc_type0), true);
2976 }
2977 }
2978
qlcnic_dump_rings(struct qlcnic_adapter * adapter)2979 static void qlcnic_dump_rings(struct qlcnic_adapter *adapter)
2980 {
2981 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2982 struct net_device *netdev = adapter->netdev;
2983 struct qlcnic_host_rds_ring *rds_ring;
2984 struct qlcnic_host_sds_ring *sds_ring;
2985 struct qlcnic_host_tx_ring *tx_ring;
2986 int ring;
2987
2988 if (!netdev || !netif_running(netdev))
2989 return;
2990
2991 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
2992 rds_ring = &recv_ctx->rds_rings[ring];
2993 if (!rds_ring)
2994 continue;
2995 netdev_info(netdev,
2996 "rds_ring=%d crb_rcv_producer=%d producer=%u num_desc=%u\n",
2997 ring, readl(rds_ring->crb_rcv_producer),
2998 rds_ring->producer, rds_ring->num_desc);
2999 }
3000
3001 for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
3002 sds_ring = &(recv_ctx->sds_rings[ring]);
3003 if (!sds_ring)
3004 continue;
3005 netdev_info(netdev,
3006 "sds_ring=%d crb_sts_consumer=%d consumer=%u crb_intr_mask=%d num_desc=%u\n",
3007 ring, readl(sds_ring->crb_sts_consumer),
3008 sds_ring->consumer, readl(sds_ring->crb_intr_mask),
3009 sds_ring->num_desc);
3010 }
3011
3012 for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
3013 tx_ring = &adapter->tx_ring[ring];
3014 if (!tx_ring)
3015 continue;
3016 netdev_info(netdev, "Tx ring=%d Context Id=0x%x\n",
3017 ring, tx_ring->ctx_id);
3018 netdev_info(netdev,
3019 "xmit_finished=%llu, xmit_called=%llu, xmit_on=%llu, xmit_off=%llu\n",
3020 tx_ring->tx_stats.xmit_finished,
3021 tx_ring->tx_stats.xmit_called,
3022 tx_ring->tx_stats.xmit_on,
3023 tx_ring->tx_stats.xmit_off);
3024
3025 if (tx_ring->crb_intr_mask)
3026 netdev_info(netdev, "crb_intr_mask=%d\n",
3027 readl(tx_ring->crb_intr_mask));
3028
3029 netdev_info(netdev,
3030 "hw_producer=%d, sw_producer=%d sw_consumer=%d, hw_consumer=%d\n",
3031 readl(tx_ring->crb_cmd_producer),
3032 tx_ring->producer, tx_ring->sw_consumer,
3033 le32_to_cpu(*(tx_ring->hw_consumer)));
3034
3035 netdev_info(netdev, "Total desc=%d, Available desc=%d\n",
3036 tx_ring->num_desc, qlcnic_tx_avail(tx_ring));
3037
3038 if (netif_msg_tx_err(adapter->ahw))
3039 dump_tx_ring_desc(tx_ring);
3040 }
3041
3042 }
3043
qlcnic_tx_timeout(struct net_device * netdev,unsigned int txqueue)3044 static void qlcnic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
3045 {
3046 struct qlcnic_adapter *adapter = netdev_priv(netdev);
3047
3048 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3049 return;
3050
3051 qlcnic_dump_rings(adapter);
3052
3053 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS ||
3054 netif_msg_tx_err(adapter->ahw)) {
3055 netdev_err(netdev, "Tx timeout, reset the adapter.\n");
3056 if (qlcnic_82xx_check(adapter))
3057 adapter->need_fw_reset = 1;
3058 else if (qlcnic_83xx_check(adapter))
3059 qlcnic_83xx_idc_request_reset(adapter,
3060 QLCNIC_FORCE_FW_DUMP_KEY);
3061 } else {
3062 netdev_err(netdev, "Tx timeout, reset adapter context.\n");
3063 adapter->ahw->reset_context = 1;
3064 }
3065 }
3066
qlcnic_get_stats(struct net_device * netdev)3067 static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
3068 {
3069 struct qlcnic_adapter *adapter = netdev_priv(netdev);
3070 struct net_device_stats *stats = &netdev->stats;
3071
3072 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
3073 qlcnic_update_stats(adapter);
3074
3075 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
3076 stats->tx_packets = adapter->stats.xmitfinished;
3077 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
3078 stats->tx_bytes = adapter->stats.txbytes;
3079 stats->rx_dropped = adapter->stats.rxdropped;
3080 stats->tx_dropped = adapter->stats.txdropped;
3081
3082 return stats;
3083 }
3084
qlcnic_82xx_clear_legacy_intr(struct qlcnic_adapter * adapter)3085 static irqreturn_t qlcnic_82xx_clear_legacy_intr(struct qlcnic_adapter *adapter)
3086 {
3087 u32 status;
3088
3089 status = readl(adapter->isr_int_vec);
3090
3091 if (!(status & adapter->ahw->int_vec_bit))
3092 return IRQ_NONE;
3093
3094 /* check interrupt state machine, to be sure */
3095 status = readl(adapter->crb_int_state_reg);
3096 if (!ISR_LEGACY_INT_TRIGGERED(status))
3097 return IRQ_NONE;
3098
3099 writel(0xffffffff, adapter->tgt_status_reg);
3100 /* read twice to ensure write is flushed */
3101 readl(adapter->isr_int_vec);
3102 readl(adapter->isr_int_vec);
3103
3104 return IRQ_HANDLED;
3105 }
3106
qlcnic_tmp_intr(int irq,void * data)3107 static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
3108 {
3109 struct qlcnic_host_sds_ring *sds_ring = data;
3110 struct qlcnic_adapter *adapter = sds_ring->adapter;
3111
3112 if (adapter->flags & QLCNIC_MSIX_ENABLED)
3113 goto done;
3114 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
3115 writel(0xffffffff, adapter->tgt_status_reg);
3116 goto done;
3117 }
3118
3119 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
3120 return IRQ_NONE;
3121
3122 done:
3123 adapter->ahw->diag_cnt++;
3124 qlcnic_enable_sds_intr(adapter, sds_ring);
3125 return IRQ_HANDLED;
3126 }
3127
qlcnic_intr(int irq,void * data)3128 static irqreturn_t qlcnic_intr(int irq, void *data)
3129 {
3130 struct qlcnic_host_sds_ring *sds_ring = data;
3131 struct qlcnic_adapter *adapter = sds_ring->adapter;
3132
3133 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
3134 return IRQ_NONE;
3135
3136 napi_schedule(&sds_ring->napi);
3137
3138 return IRQ_HANDLED;
3139 }
3140
qlcnic_msi_intr(int irq,void * data)3141 static irqreturn_t qlcnic_msi_intr(int irq, void *data)
3142 {
3143 struct qlcnic_host_sds_ring *sds_ring = data;
3144 struct qlcnic_adapter *adapter = sds_ring->adapter;
3145
3146 /* clear interrupt */
3147 writel(0xffffffff, adapter->tgt_status_reg);
3148
3149 napi_schedule(&sds_ring->napi);
3150 return IRQ_HANDLED;
3151 }
3152
qlcnic_msix_intr(int irq,void * data)3153 static irqreturn_t qlcnic_msix_intr(int irq, void *data)
3154 {
3155 struct qlcnic_host_sds_ring *sds_ring = data;
3156
3157 napi_schedule(&sds_ring->napi);
3158 return IRQ_HANDLED;
3159 }
3160
qlcnic_msix_tx_intr(int irq,void * data)3161 static irqreturn_t qlcnic_msix_tx_intr(int irq, void *data)
3162 {
3163 struct qlcnic_host_tx_ring *tx_ring = data;
3164
3165 napi_schedule(&tx_ring->napi);
3166 return IRQ_HANDLED;
3167 }
3168
3169 static void
qlcnic_idc_debug_info(struct qlcnic_adapter * adapter,u8 encoding)3170 qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
3171 {
3172 u32 val;
3173
3174 val = adapter->portnum & 0xf;
3175 val |= encoding << 7;
3176 val |= (jiffies - adapter->dev_rst_time) << 8;
3177
3178 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
3179 adapter->dev_rst_time = jiffies;
3180 }
3181
3182 static int
qlcnic_set_drv_state(struct qlcnic_adapter * adapter,u8 state)3183 qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
3184 {
3185 u32 val;
3186
3187 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
3188 state != QLCNIC_DEV_NEED_QUISCENT);
3189
3190 if (qlcnic_api_lock(adapter))
3191 return -EIO;
3192
3193 val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3194
3195 if (state == QLCNIC_DEV_NEED_RESET)
3196 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
3197 else if (state == QLCNIC_DEV_NEED_QUISCENT)
3198 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
3199
3200 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3201
3202 qlcnic_api_unlock(adapter);
3203
3204 return 0;
3205 }
3206
3207 static int
qlcnic_clr_drv_state(struct qlcnic_adapter * adapter)3208 qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
3209 {
3210 u32 val;
3211
3212 if (qlcnic_api_lock(adapter))
3213 return -EBUSY;
3214
3215 val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3216 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
3217 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3218
3219 qlcnic_api_unlock(adapter);
3220
3221 return 0;
3222 }
3223
qlcnic_clr_all_drv_state(struct qlcnic_adapter * adapter,u8 failed)3224 void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
3225 {
3226 u32 val;
3227
3228 if (qlcnic_api_lock(adapter))
3229 goto err;
3230
3231 val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
3232 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
3233 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
3234
3235 if (failed) {
3236 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3237 QLCNIC_DEV_FAILED);
3238 dev_info(&adapter->pdev->dev,
3239 "Device state set to Failed. Please Reboot\n");
3240 } else if (!(val & 0x11111111))
3241 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3242 QLCNIC_DEV_COLD);
3243
3244 val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3245 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
3246 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3247
3248 qlcnic_api_unlock(adapter);
3249 err:
3250 adapter->fw_fail_cnt = 0;
3251 adapter->flags &= ~QLCNIC_FW_HANG;
3252 clear_bit(__QLCNIC_START_FW, &adapter->state);
3253 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3254 }
3255
3256 /* Grab api lock, before checking state */
3257 static int
qlcnic_check_drv_state(struct qlcnic_adapter * adapter)3258 qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
3259 {
3260 int act, state, active_mask;
3261 struct qlcnic_hardware_context *ahw = adapter->ahw;
3262
3263 state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3264 act = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
3265
3266 if (adapter->flags & QLCNIC_FW_RESET_OWNER) {
3267 active_mask = (~(1 << (ahw->pci_func * 4)));
3268 act = act & active_mask;
3269 }
3270
3271 if (((state & 0x11111111) == (act & 0x11111111)) ||
3272 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
3273 return 0;
3274 else
3275 return 1;
3276 }
3277
qlcnic_check_idc_ver(struct qlcnic_adapter * adapter)3278 static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
3279 {
3280 u32 val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
3281
3282 if (val != QLCNIC_DRV_IDC_VER) {
3283 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
3284 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
3285 }
3286
3287 return 0;
3288 }
3289
3290 static int
qlcnic_can_start_firmware(struct qlcnic_adapter * adapter)3291 qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
3292 {
3293 u32 val, prev_state;
3294 u8 dev_init_timeo = adapter->dev_init_timeo;
3295 u8 portnum = adapter->portnum;
3296 u8 ret;
3297
3298 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
3299 return 1;
3300
3301 if (qlcnic_api_lock(adapter))
3302 return -1;
3303
3304 val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
3305 if (!(val & (1 << (portnum * 4)))) {
3306 QLC_DEV_SET_REF_CNT(val, portnum);
3307 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
3308 }
3309
3310 prev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3311 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
3312
3313 switch (prev_state) {
3314 case QLCNIC_DEV_COLD:
3315 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3316 QLCNIC_DEV_INITIALIZING);
3317 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_IDC_VER,
3318 QLCNIC_DRV_IDC_VER);
3319 qlcnic_idc_debug_info(adapter, 0);
3320 qlcnic_api_unlock(adapter);
3321 return 1;
3322
3323 case QLCNIC_DEV_READY:
3324 ret = qlcnic_check_idc_ver(adapter);
3325 qlcnic_api_unlock(adapter);
3326 return ret;
3327
3328 case QLCNIC_DEV_NEED_RESET:
3329 val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3330 QLC_DEV_SET_RST_RDY(val, portnum);
3331 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3332 break;
3333
3334 case QLCNIC_DEV_NEED_QUISCENT:
3335 val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3336 QLC_DEV_SET_QSCNT_RDY(val, portnum);
3337 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3338 break;
3339
3340 case QLCNIC_DEV_FAILED:
3341 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
3342 qlcnic_api_unlock(adapter);
3343 return -1;
3344
3345 case QLCNIC_DEV_INITIALIZING:
3346 case QLCNIC_DEV_QUISCENT:
3347 break;
3348 }
3349
3350 qlcnic_api_unlock(adapter);
3351
3352 do {
3353 msleep(1000);
3354 prev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3355
3356 if (prev_state == QLCNIC_DEV_QUISCENT)
3357 continue;
3358 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
3359
3360 if (!dev_init_timeo) {
3361 dev_err(&adapter->pdev->dev,
3362 "Waiting for device to initialize timeout\n");
3363 return -1;
3364 }
3365
3366 if (qlcnic_api_lock(adapter))
3367 return -1;
3368
3369 val = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DRV_STATE);
3370 QLC_DEV_CLR_RST_QSCNT(val, portnum);
3371 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DRV_STATE, val);
3372
3373 ret = qlcnic_check_idc_ver(adapter);
3374 qlcnic_api_unlock(adapter);
3375
3376 return ret;
3377 }
3378
3379 static void
qlcnic_fwinit_work(struct work_struct * work)3380 qlcnic_fwinit_work(struct work_struct *work)
3381 {
3382 struct qlcnic_adapter *adapter = container_of(work,
3383 struct qlcnic_adapter, fw_work.work);
3384 u32 dev_state = 0xf;
3385 u32 val;
3386
3387 if (qlcnic_api_lock(adapter))
3388 goto err_ret;
3389
3390 dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3391 if (dev_state == QLCNIC_DEV_QUISCENT ||
3392 dev_state == QLCNIC_DEV_NEED_QUISCENT) {
3393 qlcnic_api_unlock(adapter);
3394 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
3395 FW_POLL_DELAY * 2);
3396 return;
3397 }
3398
3399 if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
3400 qlcnic_api_unlock(adapter);
3401 goto wait_npar;
3402 }
3403
3404 if (dev_state == QLCNIC_DEV_INITIALIZING ||
3405 dev_state == QLCNIC_DEV_READY) {
3406 dev_info(&adapter->pdev->dev, "Detected state change from "
3407 "DEV_NEED_RESET, skipping ack check\n");
3408 goto skip_ack_check;
3409 }
3410
3411 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
3412 dev_info(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
3413 adapter->reset_ack_timeo);
3414 goto skip_ack_check;
3415 }
3416
3417 if (!qlcnic_check_drv_state(adapter)) {
3418 skip_ack_check:
3419 dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3420
3421 if (dev_state == QLCNIC_DEV_NEED_RESET) {
3422 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3423 QLCNIC_DEV_INITIALIZING);
3424 set_bit(__QLCNIC_START_FW, &adapter->state);
3425 QLCDB(adapter, DRV, "Restarting fw\n");
3426 qlcnic_idc_debug_info(adapter, 0);
3427 val = QLC_SHARED_REG_RD32(adapter,
3428 QLCNIC_CRB_DRV_STATE);
3429 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
3430 QLC_SHARED_REG_WR32(adapter,
3431 QLCNIC_CRB_DRV_STATE, val);
3432 }
3433
3434 qlcnic_api_unlock(adapter);
3435
3436 rtnl_lock();
3437 if (qlcnic_check_fw_dump_state(adapter) &&
3438 (adapter->flags & QLCNIC_FW_RESET_OWNER)) {
3439 QLCDB(adapter, DRV, "Take FW dump\n");
3440 qlcnic_dump_fw(adapter);
3441 adapter->flags |= QLCNIC_FW_HANG;
3442 }
3443 rtnl_unlock();
3444
3445 adapter->flags &= ~QLCNIC_FW_RESET_OWNER;
3446 if (!adapter->nic_ops->start_firmware(adapter)) {
3447 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
3448 adapter->fw_wait_cnt = 0;
3449 return;
3450 }
3451 goto err_ret;
3452 }
3453
3454 qlcnic_api_unlock(adapter);
3455
3456 wait_npar:
3457 dev_state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3458 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
3459
3460 switch (dev_state) {
3461 case QLCNIC_DEV_READY:
3462 if (!qlcnic_start_firmware(adapter)) {
3463 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
3464 adapter->fw_wait_cnt = 0;
3465 return;
3466 }
3467 case QLCNIC_DEV_FAILED:
3468 break;
3469 default:
3470 qlcnic_schedule_work(adapter,
3471 qlcnic_fwinit_work, FW_POLL_DELAY);
3472 return;
3473 }
3474
3475 err_ret:
3476 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
3477 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
3478 netif_device_attach(adapter->netdev);
3479 qlcnic_clr_all_drv_state(adapter, 0);
3480 }
3481
3482 static void
qlcnic_detach_work(struct work_struct * work)3483 qlcnic_detach_work(struct work_struct *work)
3484 {
3485 struct qlcnic_adapter *adapter = container_of(work,
3486 struct qlcnic_adapter, fw_work.work);
3487 struct net_device *netdev = adapter->netdev;
3488 u32 status;
3489
3490 netif_device_detach(netdev);
3491
3492 /* Dont grab rtnl lock during Quiscent mode */
3493 if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
3494 if (netif_running(netdev))
3495 __qlcnic_down(adapter, netdev);
3496 } else
3497 qlcnic_down(adapter, netdev);
3498
3499 status = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS1);
3500
3501 if (status & QLCNIC_RCODE_FATAL_ERROR) {
3502 dev_err(&adapter->pdev->dev,
3503 "Detaching the device: peg halt status1=0x%x\n",
3504 status);
3505
3506 if (QLCNIC_FWERROR_CODE(status) == QLCNIC_FWERROR_FAN_FAILURE) {
3507 dev_err(&adapter->pdev->dev,
3508 "On board active cooling fan failed. "
3509 "Device has been halted.\n");
3510 dev_err(&adapter->pdev->dev,
3511 "Replace the adapter.\n");
3512 }
3513
3514 goto err_ret;
3515 }
3516
3517 if (adapter->ahw->temp == QLCNIC_TEMP_PANIC) {
3518 dev_err(&adapter->pdev->dev, "Detaching the device: temp=%d\n",
3519 adapter->ahw->temp);
3520 goto err_ret;
3521 }
3522
3523 /* Dont ack if this instance is the reset owner */
3524 if (!(adapter->flags & QLCNIC_FW_RESET_OWNER)) {
3525 if (qlcnic_set_drv_state(adapter, adapter->dev_state)) {
3526 dev_err(&adapter->pdev->dev,
3527 "Failed to set driver state,"
3528 "detaching the device.\n");
3529 goto err_ret;
3530 }
3531 }
3532
3533 adapter->fw_wait_cnt = 0;
3534
3535 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
3536
3537 return;
3538
3539 err_ret:
3540 netif_device_attach(netdev);
3541 qlcnic_clr_all_drv_state(adapter, 1);
3542 }
3543
3544 /*Transit NPAR state to NON Operational */
3545 static void
qlcnic_set_npar_non_operational(struct qlcnic_adapter * adapter)3546 qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
3547 {
3548 u32 state;
3549
3550 state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
3551 if (state == QLCNIC_DEV_NPAR_NON_OPER)
3552 return;
3553
3554 if (qlcnic_api_lock(adapter))
3555 return;
3556 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
3557 QLCNIC_DEV_NPAR_NON_OPER);
3558 qlcnic_api_unlock(adapter);
3559 }
3560
qlcnic_82xx_dev_request_reset(struct qlcnic_adapter * adapter,u32 key)3561 static void qlcnic_82xx_dev_request_reset(struct qlcnic_adapter *adapter,
3562 u32 key)
3563 {
3564 u32 state, xg_val = 0, gb_val = 0;
3565
3566 qlcnic_xg_set_xg0_mask(xg_val);
3567 qlcnic_xg_set_xg1_mask(xg_val);
3568 QLCWR32(adapter, QLCNIC_NIU_XG_PAUSE_CTL, xg_val);
3569 qlcnic_gb_set_gb0_mask(gb_val);
3570 qlcnic_gb_set_gb1_mask(gb_val);
3571 qlcnic_gb_set_gb2_mask(gb_val);
3572 qlcnic_gb_set_gb3_mask(gb_val);
3573 QLCWR32(adapter, QLCNIC_NIU_GB_PAUSE_CTL, gb_val);
3574 dev_info(&adapter->pdev->dev, "Pause control frames disabled"
3575 " on all ports\n");
3576 adapter->need_fw_reset = 1;
3577
3578 if (qlcnic_api_lock(adapter))
3579 return;
3580
3581 state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3582
3583 if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state)) {
3584 netdev_err(adapter->netdev, "%s: Device is in non-operational state\n",
3585 __func__);
3586 qlcnic_api_unlock(adapter);
3587
3588 return;
3589 }
3590
3591 if (state == QLCNIC_DEV_READY) {
3592 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3593 QLCNIC_DEV_NEED_RESET);
3594 adapter->flags |= QLCNIC_FW_RESET_OWNER;
3595 QLCDB(adapter, DRV, "NEED_RESET state set\n");
3596 qlcnic_idc_debug_info(adapter, 0);
3597 }
3598
3599 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
3600 QLCNIC_DEV_NPAR_NON_OPER);
3601 qlcnic_api_unlock(adapter);
3602 }
3603
3604 /* Transit to NPAR READY state from NPAR NOT READY state */
3605 static void
qlcnic_dev_set_npar_ready(struct qlcnic_adapter * adapter)3606 qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
3607 {
3608 if (qlcnic_api_lock(adapter))
3609 return;
3610
3611 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
3612 QLCNIC_DEV_NPAR_OPER);
3613 QLCDB(adapter, DRV, "NPAR operational state set\n");
3614
3615 qlcnic_api_unlock(adapter);
3616 }
3617
qlcnic_schedule_work(struct qlcnic_adapter * adapter,work_func_t func,int delay)3618 void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
3619 work_func_t func, int delay)
3620 {
3621 if (test_bit(__QLCNIC_AER, &adapter->state))
3622 return;
3623
3624 INIT_DELAYED_WORK(&adapter->fw_work, func);
3625 queue_delayed_work(adapter->qlcnic_wq, &adapter->fw_work,
3626 round_jiffies_relative(delay));
3627 }
3628
3629 static void
qlcnic_attach_work(struct work_struct * work)3630 qlcnic_attach_work(struct work_struct *work)
3631 {
3632 struct qlcnic_adapter *adapter = container_of(work,
3633 struct qlcnic_adapter, fw_work.work);
3634 struct net_device *netdev = adapter->netdev;
3635 u32 npar_state;
3636
3637 if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC) {
3638 npar_state = QLC_SHARED_REG_RD32(adapter,
3639 QLCNIC_CRB_DEV_NPAR_STATE);
3640 if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
3641 qlcnic_clr_all_drv_state(adapter, 0);
3642 else if (npar_state != QLCNIC_DEV_NPAR_OPER)
3643 qlcnic_schedule_work(adapter, qlcnic_attach_work,
3644 FW_POLL_DELAY);
3645 else
3646 goto attach;
3647 QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
3648 return;
3649 }
3650 attach:
3651 qlcnic_dcb_get_info(adapter->dcb);
3652
3653 if (netif_running(netdev)) {
3654 if (qlcnic_up(adapter, netdev))
3655 goto done;
3656
3657 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3658 }
3659
3660 done:
3661 netif_device_attach(netdev);
3662 adapter->fw_fail_cnt = 0;
3663 adapter->flags &= ~QLCNIC_FW_HANG;
3664 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3665 if (adapter->portnum == 0)
3666 qlcnic_set_drv_version(adapter);
3667
3668 if (!qlcnic_clr_drv_state(adapter))
3669 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3670 FW_POLL_DELAY);
3671 }
3672
3673 static int
qlcnic_check_health(struct qlcnic_adapter * adapter)3674 qlcnic_check_health(struct qlcnic_adapter *adapter)
3675 {
3676 struct qlcnic_hardware_context *ahw = adapter->ahw;
3677 struct qlcnic_fw_dump *fw_dump = &ahw->fw_dump;
3678 u32 state = 0, heartbeat;
3679 u32 peg_status;
3680 int err = 0;
3681
3682 if (qlcnic_check_temp(adapter))
3683 goto detach;
3684
3685 if (adapter->need_fw_reset)
3686 qlcnic_dev_request_reset(adapter, 0);
3687
3688 state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3689 if (state == QLCNIC_DEV_NEED_RESET) {
3690 qlcnic_set_npar_non_operational(adapter);
3691 adapter->need_fw_reset = 1;
3692 } else if (state == QLCNIC_DEV_NEED_QUISCENT)
3693 goto detach;
3694
3695 heartbeat = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3696 if (heartbeat != adapter->heartbeat) {
3697 adapter->heartbeat = heartbeat;
3698 adapter->fw_fail_cnt = 0;
3699 if (adapter->need_fw_reset)
3700 goto detach;
3701
3702 if (ahw->reset_context && qlcnic_auto_fw_reset)
3703 qlcnic_reset_hw_context(adapter);
3704
3705 return 0;
3706 }
3707
3708 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3709 return 0;
3710
3711 adapter->flags |= QLCNIC_FW_HANG;
3712
3713 qlcnic_dev_request_reset(adapter, 0);
3714
3715 if (qlcnic_auto_fw_reset)
3716 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
3717
3718 dev_err(&adapter->pdev->dev, "firmware hang detected\n");
3719 peg_status = QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS1);
3720 dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
3721 "PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
3722 "PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
3723 "PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
3724 "PEG_NET_4_PC: 0x%x\n",
3725 peg_status,
3726 QLC_SHARED_REG_RD32(adapter, QLCNIC_PEG_HALT_STATUS2),
3727 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_0 + 0x3c, &err),
3728 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_1 + 0x3c, &err),
3729 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_2 + 0x3c, &err),
3730 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_3 + 0x3c, &err),
3731 QLCRD32(adapter, QLCNIC_CRB_PEG_NET_4 + 0x3c, &err));
3732 if (QLCNIC_FWERROR_CODE(peg_status) == 0x67)
3733 dev_err(&adapter->pdev->dev,
3734 "Firmware aborted with error code 0x00006700. "
3735 "Device is being reset.\n");
3736 detach:
3737 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3738 QLCNIC_DEV_NEED_RESET;
3739
3740 if (qlcnic_auto_fw_reset && !test_and_set_bit(__QLCNIC_RESETTING,
3741 &adapter->state)) {
3742
3743 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
3744 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3745 } else if (!qlcnic_auto_fw_reset && fw_dump->enable &&
3746 adapter->flags & QLCNIC_FW_RESET_OWNER) {
3747 qlcnic_dump_fw(adapter);
3748 }
3749
3750 return 1;
3751 }
3752
qlcnic_fw_poll_work(struct work_struct * work)3753 void qlcnic_fw_poll_work(struct work_struct *work)
3754 {
3755 struct qlcnic_adapter *adapter = container_of(work,
3756 struct qlcnic_adapter, fw_work.work);
3757
3758 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3759 goto reschedule;
3760
3761
3762 if (qlcnic_check_health(adapter))
3763 return;
3764
3765 if (adapter->fhash.fnum)
3766 qlcnic_prune_lb_filters(adapter);
3767
3768 reschedule:
3769 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3770 }
3771
qlcnic_is_first_func(struct pci_dev * pdev)3772 static int qlcnic_is_first_func(struct pci_dev *pdev)
3773 {
3774 struct pci_dev *oth_pdev;
3775 int val = pdev->devfn;
3776
3777 while (val-- > 0) {
3778 oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3779 (pdev->bus), pdev->bus->number,
3780 PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
3781 if (!oth_pdev)
3782 continue;
3783
3784 if (oth_pdev->current_state != PCI_D3cold) {
3785 pci_dev_put(oth_pdev);
3786 return 0;
3787 }
3788 pci_dev_put(oth_pdev);
3789 }
3790 return 1;
3791 }
3792
qlcnic_attach_func(struct pci_dev * pdev)3793 static int qlcnic_attach_func(struct pci_dev *pdev)
3794 {
3795 int err, first_func;
3796 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3797 struct net_device *netdev = adapter->netdev;
3798
3799 pdev->error_state = pci_channel_io_normal;
3800
3801 err = pci_enable_device(pdev);
3802 if (err)
3803 return err;
3804
3805 pci_set_master(pdev);
3806 pci_restore_state(pdev);
3807
3808 first_func = qlcnic_is_first_func(pdev);
3809
3810 if (qlcnic_api_lock(adapter))
3811 return -EINVAL;
3812
3813 if (adapter->ahw->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
3814 adapter->need_fw_reset = 1;
3815 set_bit(__QLCNIC_START_FW, &adapter->state);
3816 QLC_SHARED_REG_WR32(adapter, QLCNIC_CRB_DEV_STATE,
3817 QLCNIC_DEV_INITIALIZING);
3818 QLCDB(adapter, DRV, "Restarting fw\n");
3819 }
3820 qlcnic_api_unlock(adapter);
3821
3822 err = qlcnic_start_firmware(adapter);
3823 if (err)
3824 return err;
3825
3826 qlcnic_clr_drv_state(adapter);
3827 kfree(adapter->msix_entries);
3828 adapter->msix_entries = NULL;
3829 err = qlcnic_setup_intr(adapter);
3830
3831 if (err) {
3832 kfree(adapter->msix_entries);
3833 netdev_err(netdev, "failed to setup interrupt\n");
3834 return err;
3835 }
3836
3837 if (netif_running(netdev)) {
3838 err = qlcnic_attach(adapter);
3839 if (err) {
3840 qlcnic_clr_all_drv_state(adapter, 1);
3841 clear_bit(__QLCNIC_AER, &adapter->state);
3842 netif_device_attach(netdev);
3843 return err;
3844 }
3845
3846 err = qlcnic_up(adapter, netdev);
3847 if (err)
3848 goto done;
3849
3850 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
3851 }
3852 done:
3853 netif_device_attach(netdev);
3854 return err;
3855 }
3856
qlcnic_82xx_io_error_detected(struct pci_dev * pdev,pci_channel_state_t state)3857 static pci_ers_result_t qlcnic_82xx_io_error_detected(struct pci_dev *pdev,
3858 pci_channel_state_t state)
3859 {
3860 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3861 struct net_device *netdev = adapter->netdev;
3862
3863 if (state == pci_channel_io_perm_failure)
3864 return PCI_ERS_RESULT_DISCONNECT;
3865
3866 if (state == pci_channel_io_normal)
3867 return PCI_ERS_RESULT_RECOVERED;
3868
3869 set_bit(__QLCNIC_AER, &adapter->state);
3870 netif_device_detach(netdev);
3871
3872 cancel_delayed_work_sync(&adapter->fw_work);
3873
3874 if (netif_running(netdev))
3875 qlcnic_down(adapter, netdev);
3876
3877 qlcnic_detach(adapter);
3878 qlcnic_teardown_intr(adapter);
3879
3880 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3881
3882 pci_save_state(pdev);
3883 pci_disable_device(pdev);
3884
3885 return PCI_ERS_RESULT_NEED_RESET;
3886 }
3887
qlcnic_82xx_io_slot_reset(struct pci_dev * pdev)3888 static pci_ers_result_t qlcnic_82xx_io_slot_reset(struct pci_dev *pdev)
3889 {
3890 pci_ers_result_t res;
3891
3892 rtnl_lock();
3893 res = qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3894 PCI_ERS_RESULT_RECOVERED;
3895 rtnl_unlock();
3896
3897 return res;
3898 }
3899
qlcnic_82xx_io_resume(struct pci_dev * pdev)3900 static void qlcnic_82xx_io_resume(struct pci_dev *pdev)
3901 {
3902 u32 state;
3903 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3904
3905 state = QLC_SHARED_REG_RD32(adapter, QLCNIC_CRB_DEV_STATE);
3906 if (state == QLCNIC_DEV_READY && test_and_clear_bit(__QLCNIC_AER,
3907 &adapter->state))
3908 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3909 FW_POLL_DELAY);
3910 }
3911
qlcnic_io_error_detected(struct pci_dev * pdev,pci_channel_state_t state)3912 static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3913 pci_channel_state_t state)
3914 {
3915 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3916 struct qlcnic_hardware_ops *hw_ops = adapter->ahw->hw_ops;
3917
3918 if (hw_ops->io_error_detected) {
3919 return hw_ops->io_error_detected(pdev, state);
3920 } else {
3921 dev_err(&pdev->dev, "AER error_detected handler not registered.\n");
3922 return PCI_ERS_RESULT_DISCONNECT;
3923 }
3924 }
3925
qlcnic_io_slot_reset(struct pci_dev * pdev)3926 static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3927 {
3928 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3929 struct qlcnic_hardware_ops *hw_ops = adapter->ahw->hw_ops;
3930
3931 if (hw_ops->io_slot_reset) {
3932 return hw_ops->io_slot_reset(pdev);
3933 } else {
3934 dev_err(&pdev->dev, "AER slot_reset handler not registered.\n");
3935 return PCI_ERS_RESULT_DISCONNECT;
3936 }
3937 }
3938
qlcnic_io_resume(struct pci_dev * pdev)3939 static void qlcnic_io_resume(struct pci_dev *pdev)
3940 {
3941 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3942 struct qlcnic_hardware_ops *hw_ops = adapter->ahw->hw_ops;
3943
3944 if (hw_ops->io_resume)
3945 hw_ops->io_resume(pdev);
3946 else
3947 dev_err(&pdev->dev, "AER resume handler not registered.\n");
3948 }
3949
3950
3951 static int
qlcnicvf_start_firmware(struct qlcnic_adapter * adapter)3952 qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3953 {
3954 int err;
3955
3956 err = qlcnic_can_start_firmware(adapter);
3957 if (err)
3958 return err;
3959
3960 err = qlcnic_check_npar_opertional(adapter);
3961 if (err)
3962 return err;
3963
3964 err = qlcnic_initialize_nic(adapter);
3965 if (err)
3966 return err;
3967
3968 qlcnic_check_options(adapter);
3969
3970 err = qlcnic_set_eswitch_port_config(adapter);
3971 if (err)
3972 return err;
3973
3974 adapter->need_fw_reset = 0;
3975
3976 return err;
3977 }
3978
qlcnic_validate_rings(struct qlcnic_adapter * adapter,__u32 ring_cnt,int queue_type)3979 int qlcnic_validate_rings(struct qlcnic_adapter *adapter, __u32 ring_cnt,
3980 int queue_type)
3981 {
3982 struct net_device *netdev = adapter->netdev;
3983 char buf[8];
3984
3985 if (queue_type == QLCNIC_RX_QUEUE)
3986 strcpy(buf, "SDS");
3987 else
3988 strcpy(buf, "Tx");
3989
3990 if (!is_power_of_2(ring_cnt)) {
3991 netdev_err(netdev, "%s rings value should be a power of 2\n",
3992 buf);
3993 return -EINVAL;
3994 }
3995
3996 if (qlcnic_82xx_check(adapter) && (queue_type == QLCNIC_TX_QUEUE) &&
3997 !qlcnic_check_multi_tx(adapter)) {
3998 netdev_err(netdev, "No Multi Tx queue support\n");
3999 return -EINVAL;
4000 }
4001
4002 if (ring_cnt > num_online_cpus()) {
4003 netdev_err(netdev,
4004 "%s value[%u] should not be higher than, number of online CPUs\n",
4005 buf, num_online_cpus());
4006 return -EINVAL;
4007 }
4008
4009 return 0;
4010 }
4011
qlcnic_setup_rings(struct qlcnic_adapter * adapter)4012 int qlcnic_setup_rings(struct qlcnic_adapter *adapter)
4013 {
4014 struct net_device *netdev = adapter->netdev;
4015 u8 tx_rings, rx_rings;
4016 int err;
4017
4018 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
4019 return -EBUSY;
4020
4021 tx_rings = adapter->drv_tss_rings;
4022 rx_rings = adapter->drv_rss_rings;
4023
4024 netif_device_detach(netdev);
4025
4026 err = qlcnic_set_real_num_queues(adapter, tx_rings, rx_rings);
4027 if (err)
4028 goto done;
4029
4030 if (netif_running(netdev))
4031 __qlcnic_down(adapter, netdev);
4032
4033 qlcnic_detach(adapter);
4034
4035 if (qlcnic_83xx_check(adapter)) {
4036 qlcnic_83xx_free_mbx_intr(adapter);
4037 qlcnic_83xx_enable_mbx_poll(adapter);
4038 }
4039
4040 qlcnic_teardown_intr(adapter);
4041
4042 err = qlcnic_setup_intr(adapter);
4043 if (err) {
4044 kfree(adapter->msix_entries);
4045 netdev_err(netdev, "failed to setup interrupt\n");
4046 return err;
4047 }
4048
4049 /* Check if we need to update real_num_{tx|rx}_queues because
4050 * qlcnic_setup_intr() may change Tx/Rx rings size
4051 */
4052 if ((tx_rings != adapter->drv_tx_rings) ||
4053 (rx_rings != adapter->drv_sds_rings)) {
4054 err = qlcnic_set_real_num_queues(adapter,
4055 adapter->drv_tx_rings,
4056 adapter->drv_sds_rings);
4057 if (err)
4058 goto done;
4059 }
4060
4061 if (qlcnic_83xx_check(adapter)) {
4062 qlcnic_83xx_initialize_nic(adapter, 1);
4063 err = qlcnic_83xx_setup_mbx_intr(adapter);
4064 qlcnic_83xx_disable_mbx_poll(adapter);
4065 if (err) {
4066 dev_err(&adapter->pdev->dev,
4067 "failed to setup mbx interrupt\n");
4068 goto done;
4069 }
4070 }
4071
4072 if (netif_running(netdev)) {
4073 err = qlcnic_attach(adapter);
4074 if (err)
4075 goto done;
4076 err = __qlcnic_up(adapter, netdev);
4077 if (err)
4078 goto done;
4079 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
4080 }
4081 done:
4082 netif_device_attach(netdev);
4083 clear_bit(__QLCNIC_RESETTING, &adapter->state);
4084 return err;
4085 }
4086
4087 #ifdef CONFIG_INET
4088
4089 #define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4090
4091 static void
qlcnic_config_indev_addr(struct qlcnic_adapter * adapter,struct net_device * dev,unsigned long event)4092 qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4093 struct net_device *dev, unsigned long event)
4094 {
4095 const struct in_ifaddr *ifa;
4096 struct in_device *indev;
4097
4098 indev = in_dev_get(dev);
4099 if (!indev)
4100 return;
4101
4102 in_dev_for_each_ifa_rtnl(ifa, indev) {
4103 switch (event) {
4104 case NETDEV_UP:
4105 qlcnic_config_ipaddr(adapter,
4106 ifa->ifa_address, QLCNIC_IP_UP);
4107 break;
4108 case NETDEV_DOWN:
4109 qlcnic_config_ipaddr(adapter,
4110 ifa->ifa_address, QLCNIC_IP_DOWN);
4111 break;
4112 default:
4113 break;
4114 }
4115 }
4116
4117 in_dev_put(indev);
4118 }
4119
qlcnic_restore_indev_addr(struct net_device * netdev,unsigned long event)4120 void qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4121 {
4122 struct qlcnic_adapter *adapter = netdev_priv(netdev);
4123 struct net_device *dev;
4124 u16 vid;
4125
4126 qlcnic_config_indev_addr(adapter, netdev, event);
4127
4128 rcu_read_lock();
4129 for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
4130 dev = __vlan_find_dev_deep_rcu(netdev, htons(ETH_P_8021Q), vid);
4131 if (!dev)
4132 continue;
4133 qlcnic_config_indev_addr(adapter, dev, event);
4134 }
4135 rcu_read_unlock();
4136 }
4137
qlcnic_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)4138 static int qlcnic_netdev_event(struct notifier_block *this,
4139 unsigned long event, void *ptr)
4140 {
4141 struct qlcnic_adapter *adapter;
4142 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4143
4144 recheck:
4145 if (dev == NULL)
4146 goto done;
4147
4148 if (is_vlan_dev(dev)) {
4149 dev = vlan_dev_real_dev(dev);
4150 goto recheck;
4151 }
4152
4153 if (!is_qlcnic_netdev(dev))
4154 goto done;
4155
4156 adapter = netdev_priv(dev);
4157
4158 if (!adapter)
4159 goto done;
4160
4161 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4162 goto done;
4163
4164 qlcnic_config_indev_addr(adapter, dev, event);
4165 done:
4166 return NOTIFY_DONE;
4167 }
4168
4169 static int
qlcnic_inetaddr_event(struct notifier_block * this,unsigned long event,void * ptr)4170 qlcnic_inetaddr_event(struct notifier_block *this,
4171 unsigned long event, void *ptr)
4172 {
4173 struct qlcnic_adapter *adapter;
4174 struct net_device *dev;
4175
4176 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4177
4178 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4179
4180 recheck:
4181 if (dev == NULL)
4182 goto done;
4183
4184 if (is_vlan_dev(dev)) {
4185 dev = vlan_dev_real_dev(dev);
4186 goto recheck;
4187 }
4188
4189 if (!is_qlcnic_netdev(dev))
4190 goto done;
4191
4192 adapter = netdev_priv(dev);
4193
4194 if (!adapter)
4195 goto done;
4196
4197 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
4198 goto done;
4199
4200 switch (event) {
4201 case NETDEV_UP:
4202 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4203
4204 break;
4205 case NETDEV_DOWN:
4206 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4207
4208 break;
4209 default:
4210 break;
4211 }
4212
4213 done:
4214 return NOTIFY_DONE;
4215 }
4216
4217 static struct notifier_block qlcnic_netdev_cb = {
4218 .notifier_call = qlcnic_netdev_event,
4219 };
4220
4221 static struct notifier_block qlcnic_inetaddr_cb = {
4222 .notifier_call = qlcnic_inetaddr_event,
4223 };
4224 #else
qlcnic_restore_indev_addr(struct net_device * dev,unsigned long event)4225 void qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
4226 { }
4227 #endif
4228 static const struct pci_error_handlers qlcnic_err_handler = {
4229 .error_detected = qlcnic_io_error_detected,
4230 .slot_reset = qlcnic_io_slot_reset,
4231 .resume = qlcnic_io_resume,
4232 };
4233
4234 static SIMPLE_DEV_PM_OPS(qlcnic_pm_ops, qlcnic_suspend, qlcnic_resume);
4235
4236 static struct pci_driver qlcnic_driver = {
4237 .name = qlcnic_driver_name,
4238 .id_table = qlcnic_pci_tbl,
4239 .probe = qlcnic_probe,
4240 .remove = qlcnic_remove,
4241 .driver.pm = &qlcnic_pm_ops,
4242 .shutdown = qlcnic_shutdown,
4243 .err_handler = &qlcnic_err_handler,
4244 #ifdef CONFIG_QLCNIC_SRIOV
4245 .sriov_configure = qlcnic_pci_sriov_configure,
4246 #endif
4247
4248 };
4249
qlcnic_init_module(void)4250 static int __init qlcnic_init_module(void)
4251 {
4252 int ret;
4253
4254 printk(KERN_INFO "%s\n", qlcnic_driver_string);
4255
4256 #ifdef CONFIG_INET
4257 register_netdevice_notifier(&qlcnic_netdev_cb);
4258 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4259 #endif
4260
4261 ret = pci_register_driver(&qlcnic_driver);
4262 if (ret) {
4263 #ifdef CONFIG_INET
4264 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4265 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4266 #endif
4267 }
4268
4269 return ret;
4270 }
4271
4272 module_init(qlcnic_init_module);
4273
qlcnic_exit_module(void)4274 static void __exit qlcnic_exit_module(void)
4275 {
4276 pci_unregister_driver(&qlcnic_driver);
4277
4278 #ifdef CONFIG_INET
4279 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4280 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4281 #endif
4282 }
4283
4284 module_exit(qlcnic_exit_module);
4285