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