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