blob: 6e619514feeebb6d0de692dc036789701972fa8e [file] [log] [blame]
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001/*
Sritej Velaga40839129f2010-12-02 20:41:56 +00002 * QLogic qlcnic NIC Driver
3 * Copyright (c) 2009-2010 QLogic Corporation
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004 *
Sritej Velaga40839129f2010-12-02 20:41:56 +00005 * See LICENSE.qlcnic for copyright and licensing details.
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00006 */
7
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00009#include <linux/vmalloc.h>
10#include <linux/interrupt.h>
11
12#include "qlcnic.h"
13
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +000014#include <linux/swab.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000015#include <linux/dma-mapping.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000016#include <net/ip.h>
17#include <linux/ipv6.h>
18#include <linux/inetdevice.h>
19#include <linux/sysfs.h>
Sucheta Chakraborty451724c2010-07-13 20:33:34 +000020#include <linux/aer.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000021
Sritej Velaga7f9a0c32010-06-17 02:56:39 +000022MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000023MODULE_LICENSE("GPL");
24MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
25MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
26
27char qlcnic_driver_name[] = "qlcnic";
Sritej Velaga7f9a0c32010-06-17 02:56:39 +000028static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
29 "Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000030
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +000031static struct workqueue_struct *qlcnic_wq;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +000032static int qlcnic_mac_learn;
amit salechab11a25a2011-01-10 00:15:23 +000033module_param(qlcnic_mac_learn, int, 0444);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +000034MODULE_PARM_DESC(qlcnic_mac_learn, "Mac Filter (0=disabled, 1=enabled)");
35
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000036static int use_msi = 1;
amit salechab11a25a2011-01-10 00:15:23 +000037module_param(use_msi, int, 0444);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000038MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
39
40static int use_msi_x = 1;
amit salechab11a25a2011-01-10 00:15:23 +000041module_param(use_msi_x, int, 0444);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000042MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
43
amit salecha9ce13ca2011-02-23 03:21:24 +000044static int auto_fw_reset = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000045module_param(auto_fw_reset, int, 0644);
46MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
47
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +000048static int load_fw_file;
amit salechab11a25a2011-01-10 00:15:23 +000049module_param(load_fw_file, int, 0444);
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +000050MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file");
51
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +000052static int qlcnic_config_npars;
amit salechab11a25a2011-01-10 00:15:23 +000053module_param(qlcnic_config_npars, int, 0444);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +000054MODULE_PARM_DESC(qlcnic_config_npars, "Configure NPARs (0=disabled, 1=enabled");
55
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000056static int __devinit qlcnic_probe(struct pci_dev *pdev,
57 const struct pci_device_id *ent);
58static void __devexit qlcnic_remove(struct pci_dev *pdev);
59static int qlcnic_open(struct net_device *netdev);
60static int qlcnic_close(struct net_device *netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000061static void qlcnic_tx_timeout(struct net_device *netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000062static void qlcnic_attach_work(struct work_struct *work);
63static void qlcnic_fwinit_work(struct work_struct *work);
64static void qlcnic_fw_poll_work(struct work_struct *work);
65static void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
66 work_func_t func, int delay);
67static void qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter);
68static int qlcnic_poll(struct napi_struct *napi, int budget);
schacko8f891382010-06-17 02:56:40 +000069static int qlcnic_rx_poll(struct napi_struct *napi, int budget);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000070#ifdef CONFIG_NET_POLL_CONTROLLER
71static void qlcnic_poll_controller(struct net_device *netdev);
72#endif
73
74static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter);
75static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter);
76static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter);
77static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter);
78
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +000079static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +000080static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000081static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
82
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +000083static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000084static irqreturn_t qlcnic_intr(int irq, void *data);
85static irqreturn_t qlcnic_msi_intr(int irq, void *data);
86static irqreturn_t qlcnic_msix_intr(int irq, void *data);
87
88static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +000089static void qlcnic_restore_indev_addr(struct net_device *dev, unsigned long);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +000090static int qlcnic_start_firmware(struct qlcnic_adapter *);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000091
Amit Kumar Salechab5e54922010-08-31 17:17:51 +000092static void qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter);
93static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +000094static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +000095static int qlcnicvf_config_led(struct qlcnic_adapter *, u32, u32);
96static int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *, u32);
97static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
Rajesh Borundia0325d692010-08-19 05:08:26 +000098static void qlcnic_set_netdev_features(struct qlcnic_adapter *,
99 struct qlcnic_esw_func_cfg *);
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000100static void qlcnic_vlan_rx_add(struct net_device *, u16);
101static void qlcnic_vlan_rx_del(struct net_device *, u16);
102
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000103/* PCI Device ID Table */
104#define ENTRY(device) \
105 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
106 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
107
108#define PCI_DEVICE_ID_QLOGIC_QLE824X 0x8020
109
Amit Kumar Salecha6a902882010-02-01 05:24:54 +0000110static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000111 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
112 {0,}
113};
114
115MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
116
117
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000118inline void
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000119qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
120 struct qlcnic_host_tx_ring *tx_ring)
121{
122 writel(tx_ring->producer, tx_ring->crb_cmd_producer);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000123}
124
125static const u32 msi_tgt_status[8] = {
126 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
127 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
128 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
129 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
130};
131
132static const
133struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
134
135static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
136{
137 writel(0, sds_ring->crb_intr_mask);
138}
139
140static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
141{
142 struct qlcnic_adapter *adapter = sds_ring->adapter;
143
144 writel(0x1, sds_ring->crb_intr_mask);
145
146 if (!QLCNIC_IS_MSI_FAMILY(adapter))
147 writel(0xfbff, adapter->tgt_mask_reg);
148}
149
150static int
151qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
152{
153 int size = sizeof(struct qlcnic_host_sds_ring) * count;
154
155 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
156
Eric Dumazet807540b2010-09-23 05:40:09 +0000157 return recv_ctx->sds_rings == NULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000158}
159
160static void
161qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
162{
163 if (recv_ctx->sds_rings != NULL)
164 kfree(recv_ctx->sds_rings);
165
166 recv_ctx->sds_rings = NULL;
167}
168
169static int
170qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
171{
172 int ring;
173 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000174 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000175
176 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
177 return -ENOMEM;
178
179 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
180 sds_ring = &recv_ctx->sds_rings[ring];
schacko8f891382010-06-17 02:56:40 +0000181
182 if (ring == adapter->max_sds_rings - 1)
183 netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
184 QLCNIC_NETDEV_WEIGHT/adapter->max_sds_rings);
185 else
186 netif_napi_add(netdev, &sds_ring->napi,
187 qlcnic_rx_poll, QLCNIC_NETDEV_WEIGHT*2);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000188 }
189
190 return 0;
191}
192
193static void
194qlcnic_napi_del(struct qlcnic_adapter *adapter)
195{
196 int ring;
197 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000198 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000199
200 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
201 sds_ring = &recv_ctx->sds_rings[ring];
202 netif_napi_del(&sds_ring->napi);
203 }
204
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000205 qlcnic_free_sds_rings(adapter->recv_ctx);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000206}
207
208static void
209qlcnic_napi_enable(struct qlcnic_adapter *adapter)
210{
211 int ring;
212 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000213 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000214
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000215 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
216 return;
217
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000218 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
219 sds_ring = &recv_ctx->sds_rings[ring];
220 napi_enable(&sds_ring->napi);
221 qlcnic_enable_int(sds_ring);
222 }
223}
224
225static void
226qlcnic_napi_disable(struct qlcnic_adapter *adapter)
227{
228 int ring;
229 struct qlcnic_host_sds_ring *sds_ring;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000230 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000231
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000232 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
233 return;
234
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000235 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
236 sds_ring = &recv_ctx->sds_rings[ring];
237 qlcnic_disable_int(sds_ring);
238 napi_synchronize(&sds_ring->napi);
239 napi_disable(&sds_ring->napi);
240 }
241}
242
243static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
244{
245 memset(&adapter->stats, 0, sizeof(adapter->stats));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000246}
247
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000248static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
249{
250 u32 control;
251 int pos;
252
253 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
254 if (pos) {
255 pci_read_config_dword(pdev, pos, &control);
256 if (enable)
257 control |= PCI_MSIX_FLAGS_ENABLE;
258 else
259 control = 0;
260 pci_write_config_dword(pdev, pos, control);
261 }
262}
263
264static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
265{
266 int i;
267
268 for (i = 0; i < count; i++)
269 adapter->msix_entries[i].entry = i;
270}
271
272static int
273qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
274{
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000275 u8 mac_addr[ETH_ALEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000276 struct net_device *netdev = adapter->netdev;
277 struct pci_dev *pdev = adapter->pdev;
278
Rajesh Borundiada48e6c2010-08-31 17:17:46 +0000279 if (qlcnic_get_mac_address(adapter, mac_addr) != 0)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000280 return -EIO;
281
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000282 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000283 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
284 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
285
286 /* set station address */
287
288 if (!is_valid_ether_addr(netdev->perm_addr))
289 dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
290 netdev->dev_addr);
291
292 return 0;
293}
294
295static int qlcnic_set_mac(struct net_device *netdev, void *p)
296{
297 struct qlcnic_adapter *adapter = netdev_priv(netdev);
298 struct sockaddr *addr = p;
299
Rajesh Borundia73733732010-08-31 17:17:50 +0000300 if ((adapter->flags & QLCNIC_MAC_OVERRIDE_DISABLED))
301 return -EOPNOTSUPP;
302
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000303 if (!is_valid_ether_addr(addr->sa_data))
304 return -EINVAL;
305
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000306 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000307 netif_device_detach(netdev);
308 qlcnic_napi_disable(adapter);
309 }
310
311 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
312 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
313 qlcnic_set_multi(adapter->netdev);
314
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000315 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000316 netif_device_attach(netdev);
317 qlcnic_napi_enable(adapter);
318 }
319 return 0;
320}
321
322static const struct net_device_ops qlcnic_netdev_ops = {
323 .ndo_open = qlcnic_open,
324 .ndo_stop = qlcnic_close,
325 .ndo_start_xmit = qlcnic_xmit_frame,
326 .ndo_get_stats = qlcnic_get_stats,
327 .ndo_validate_addr = eth_validate_addr,
328 .ndo_set_multicast_list = qlcnic_set_multi,
329 .ndo_set_mac_address = qlcnic_set_mac,
330 .ndo_change_mtu = qlcnic_change_mtu,
Michał Mirosław135d84a2011-04-19 03:03:57 +0000331 .ndo_fix_features = qlcnic_fix_features,
332 .ndo_set_features = qlcnic_set_features,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000333 .ndo_tx_timeout = qlcnic_tx_timeout,
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000334 .ndo_vlan_rx_add_vid = qlcnic_vlan_rx_add,
335 .ndo_vlan_rx_kill_vid = qlcnic_vlan_rx_del,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000336#ifdef CONFIG_NET_POLL_CONTROLLER
337 .ndo_poll_controller = qlcnic_poll_controller,
338#endif
339};
340
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000341static struct qlcnic_nic_template qlcnic_ops = {
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000342 .config_bridged_mode = qlcnic_config_bridged_mode,
343 .config_led = qlcnic_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000344 .start_firmware = qlcnic_start_firmware
345};
346
347static struct qlcnic_nic_template qlcnic_vf_ops = {
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000348 .config_bridged_mode = qlcnicvf_config_bridged_mode,
349 .config_led = qlcnicvf_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000350 .start_firmware = qlcnicvf_start_firmware
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000351};
352
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000353static void
354qlcnic_setup_intr(struct qlcnic_adapter *adapter)
355{
356 const struct qlcnic_legacy_intr_set *legacy_intrp;
357 struct pci_dev *pdev = adapter->pdev;
358 int err, num_msix;
359
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000360 if (adapter->msix_supported) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000361 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
362 MSIX_ENTRIES_PER_ADAPTER : 2;
363 } else
364 num_msix = 1;
365
366 adapter->max_sds_rings = 1;
367
368 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
369
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000370 legacy_intrp = &legacy_intr[adapter->ahw->pci_func];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000371
372 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
373 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
374 legacy_intrp->tgt_status_reg);
375 adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
376 legacy_intrp->tgt_mask_reg);
377 adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
378
379 adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
380 ISR_INT_STATE_REG);
381
382 qlcnic_set_msix_bit(pdev, 0);
383
384 if (adapter->msix_supported) {
385
386 qlcnic_init_msix_entries(adapter, num_msix);
387 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
388 if (err == 0) {
389 adapter->flags |= QLCNIC_MSIX_ENABLED;
390 qlcnic_set_msix_bit(pdev, 1);
391
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000392 adapter->max_sds_rings = num_msix;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000393
394 dev_info(&pdev->dev, "using msi-x interrupts\n");
395 return;
396 }
397
398 if (err > 0)
399 pci_disable_msix(pdev);
400
401 /* fall through for msi */
402 }
403
404 if (use_msi && !pci_enable_msi(pdev)) {
405 adapter->flags |= QLCNIC_MSI_ENABLED;
406 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000407 msi_tgt_status[adapter->ahw->pci_func]);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000408 dev_info(&pdev->dev, "using msi interrupts\n");
409 adapter->msix_entries[0].vector = pdev->irq;
410 return;
411 }
412
413 dev_info(&pdev->dev, "using legacy interrupts\n");
414 adapter->msix_entries[0].vector = pdev->irq;
415}
416
417static void
418qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
419{
420 if (adapter->flags & QLCNIC_MSIX_ENABLED)
421 pci_disable_msix(adapter->pdev);
422 if (adapter->flags & QLCNIC_MSI_ENABLED)
423 pci_disable_msi(adapter->pdev);
424}
425
426static void
427qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
428{
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000429 if (adapter->ahw->pci_base0 != NULL)
430 iounmap(adapter->ahw->pci_base0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000431}
432
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000433static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000434qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
435{
Dan Carpentere88db3b2010-08-09 21:49:36 +0000436 struct qlcnic_pci_info *pci_info;
Julia Lawall900853a2010-08-16 10:23:51 +0000437 int i, ret = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000438 u8 pfn;
439
Dan Carpentere88db3b2010-08-09 21:49:36 +0000440 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
441 if (!pci_info)
442 return -ENOMEM;
443
Dan Carpenterca315ac2010-08-09 21:47:56 +0000444 adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000445 QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
Dan Carpentere88db3b2010-08-09 21:49:36 +0000446 if (!adapter->npars) {
Julia Lawall900853a2010-08-16 10:23:51 +0000447 ret = -ENOMEM;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000448 goto err_pci_info;
449 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000450
Dan Carpenterca315ac2010-08-09 21:47:56 +0000451 adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000452 QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
453 if (!adapter->eswitch) {
Julia Lawall900853a2010-08-16 10:23:51 +0000454 ret = -ENOMEM;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000455 goto err_npars;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000456 }
457
458 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000459 if (ret)
460 goto err_eswitch;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000461
Dan Carpenterca315ac2010-08-09 21:47:56 +0000462 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
463 pfn = pci_info[i].id;
Sony Chackof848d6d2011-04-01 14:27:59 +0000464 if (pfn > QLCNIC_MAX_PCI_FUNC) {
465 ret = QL_STATUS_INVALID_PARAM;
466 goto err_eswitch;
467 }
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +0000468 adapter->npars[pfn].active = (u8)pci_info[i].active;
469 adapter->npars[pfn].type = (u8)pci_info[i].type;
470 adapter->npars[pfn].phy_port = (u8)pci_info[i].default_port;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000471 adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
472 adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000473 }
474
Dan Carpenterca315ac2010-08-09 21:47:56 +0000475 for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
476 adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
477
Dan Carpentere88db3b2010-08-09 21:49:36 +0000478 kfree(pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000479 return 0;
480
481err_eswitch:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000482 kfree(adapter->eswitch);
483 adapter->eswitch = NULL;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000484err_npars:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000485 kfree(adapter->npars);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000486 adapter->npars = NULL;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000487err_pci_info:
488 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000489
490 return ret;
491}
492
493static int
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000494qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
495{
496 u8 id;
497 u32 ref_count;
498 int i, ret = 1;
499 u32 data = QLCNIC_MGMT_FUNC;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000500 void __iomem *priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000501
502 /* If other drivers are not in use set their privilege level */
Amit Kumar Salecha31018e02010-08-25 04:03:05 +0000503 ref_count = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000504 ret = qlcnic_api_lock(adapter);
505 if (ret)
506 goto err_lock;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000507
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000508 if (qlcnic_config_npars) {
509 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000510 id = i;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000511 if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000512 id == adapter->ahw->pci_func)
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000513 continue;
514 data |= (qlcnic_config_npars &
515 QLC_DEV_SET_DRV(0xf, id));
516 }
517 } else {
518 data = readl(priv_op);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000519 data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw->pci_func)) |
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000520 (QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000521 adapter->ahw->pci_func));
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000522 }
523 writel(data, priv_op);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000524 qlcnic_api_unlock(adapter);
525err_lock:
526 return ret;
527}
528
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000529static void
530qlcnic_check_vf(struct qlcnic_adapter *adapter)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000531{
532 void __iomem *msix_base_addr;
533 void __iomem *priv_op;
534 u32 func;
535 u32 msix_base;
536 u32 op_mode, priv_level;
537
538 /* Determine FW API version */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000539 adapter->fw_hal_version = readl(adapter->ahw->pci_base0 +
540 QLCNIC_FW_API);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000541
542 /* Find PCI function number */
543 pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000544 msix_base_addr = adapter->ahw->pci_base0 + QLCNIC_MSIX_BASE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000545 msix_base = readl(msix_base_addr);
546 func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000547 adapter->ahw->pci_func = func;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000548
549 /* Determine function privilege level */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000550 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000551 op_mode = readl(priv_op);
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000552 if (op_mode == QLC_DEV_DRV_DEFAULT)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000553 priv_level = QLCNIC_MGMT_FUNC;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000554 else
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000555 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000556
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000557 if (priv_level == QLCNIC_NON_PRIV_FUNC) {
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000558 adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
559 dev_info(&adapter->pdev->dev,
560 "HAL Version: %d Non Privileged function\n",
561 adapter->fw_hal_version);
562 adapter->nic_ops = &qlcnic_vf_ops;
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000563 } else
564 adapter->nic_ops = &qlcnic_ops;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000565}
566
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000567static int
568qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
569{
570 void __iomem *mem_ptr0 = NULL;
571 resource_size_t mem_base;
572 unsigned long mem_len, pci_len0 = 0;
573
574 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000575
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000576 /* remap phys address */
577 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
578 mem_len = pci_resource_len(pdev, 0);
579
580 if (mem_len == QLCNIC_PCI_2MB_SIZE) {
581
582 mem_ptr0 = pci_ioremap_bar(pdev, 0);
583 if (mem_ptr0 == NULL) {
584 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
585 return -EIO;
586 }
587 pci_len0 = mem_len;
588 } else {
589 return -EIO;
590 }
591
592 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
593
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000594 adapter->ahw->pci_base0 = mem_ptr0;
595 adapter->ahw->pci_len0 = pci_len0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000596
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000597 qlcnic_check_vf(adapter);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000598
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000599 adapter->ahw->ocm_win_crb = qlcnic_get_ioaddr(adapter,
600 QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(
601 adapter->ahw->pci_func)));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000602
603 return 0;
604}
605
606static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
607{
608 struct pci_dev *pdev = adapter->pdev;
609 int i, found = 0;
610
611 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
612 if (qlcnic_boards[i].vendor == pdev->vendor &&
613 qlcnic_boards[i].device == pdev->device &&
614 qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
615 qlcnic_boards[i].sub_device == pdev->subsystem_device) {
Sucheta Chakraborty02f6e462010-05-17 01:22:09 +0000616 sprintf(name, "%pM: %s" ,
617 adapter->mac_addr,
618 qlcnic_boards[i].short_name);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000619 found = 1;
620 break;
621 }
622
623 }
624
625 if (!found)
Sritej Velaga7f9a0c32010-06-17 02:56:39 +0000626 sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000627}
628
629static void
630qlcnic_check_options(struct qlcnic_adapter *adapter)
631{
632 u32 fw_major, fw_minor, fw_build;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000633 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000634
635 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
636 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
637 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
638
639 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
640
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +0000641 dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
642 fw_major, fw_minor, fw_build);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000643 if (adapter->ahw->port_type == QLCNIC_XGBE) {
Sony Chacko90d19002010-10-26 17:53:08 +0000644 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
645 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
646 adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
647 } else {
648 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
649 adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
650 }
651
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000652 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
Sony Chacko90d19002010-10-26 17:53:08 +0000653 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
654
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000655 } else if (adapter->ahw->port_type == QLCNIC_GBE) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000656 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
657 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
Sony Chacko90d19002010-10-26 17:53:08 +0000658 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
659 adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000660 }
661
662 adapter->msix_supported = !!use_msi_x;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000663
664 adapter->num_txd = MAX_CMD_DESCRIPTORS;
665
Sony Chacko251b0362010-08-19 05:08:24 +0000666 adapter->max_rds_rings = MAX_RDS_RINGS;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000667}
668
Rajesh Borundia174240a2010-08-31 17:17:47 +0000669static int
670qlcnic_initialize_nic(struct qlcnic_adapter *adapter)
671{
672 int err;
673 struct qlcnic_info nic_info;
674
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000675 err = qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw->pci_func);
Rajesh Borundia174240a2010-08-31 17:17:47 +0000676 if (err)
677 return err;
678
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +0000679 adapter->physical_port = (u8)nic_info.phys_port;
Rajesh Borundia174240a2010-08-31 17:17:47 +0000680 adapter->switch_mode = nic_info.switch_mode;
681 adapter->max_tx_ques = nic_info.max_tx_ques;
682 adapter->max_rx_ques = nic_info.max_rx_ques;
683 adapter->capabilities = nic_info.capabilities;
684 adapter->max_mac_filters = nic_info.max_mac_filters;
685 adapter->max_mtu = nic_info.max_mtu;
686
687 if (adapter->capabilities & BIT_6)
688 adapter->flags |= QLCNIC_ESWITCH_ENABLED;
689 else
690 adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
691
692 return err;
693}
694
Rajesh Borundia0325d692010-08-19 05:08:26 +0000695static void
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +0000696qlcnic_set_vlan_config(struct qlcnic_adapter *adapter,
697 struct qlcnic_esw_func_cfg *esw_cfg)
698{
699 if (esw_cfg->discard_tagged)
700 adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
701 else
702 adapter->flags |= QLCNIC_TAGGING_ENABLED;
703
704 if (esw_cfg->vlan_id)
705 adapter->pvid = esw_cfg->vlan_id;
706 else
707 adapter->pvid = 0;
708}
709
710static void
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000711qlcnic_vlan_rx_add(struct net_device *netdev, u16 vid)
712{
713 struct qlcnic_adapter *adapter = netdev_priv(netdev);
714 set_bit(vid, adapter->vlans);
715}
716
717static void
718qlcnic_vlan_rx_del(struct net_device *netdev, u16 vid)
719{
720 struct qlcnic_adapter *adapter = netdev_priv(netdev);
721
722 qlcnic_restore_indev_addr(netdev, NETDEV_DOWN);
723 clear_bit(vid, adapter->vlans);
724}
725
726static void
Rajesh Borundia0325d692010-08-19 05:08:26 +0000727qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
728 struct qlcnic_esw_func_cfg *esw_cfg)
729{
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000730 adapter->flags &= ~(QLCNIC_MACSPOOF | QLCNIC_MAC_OVERRIDE_DISABLED |
731 QLCNIC_PROMISC_DISABLED);
Rajesh Borundia7613c872010-08-31 17:17:48 +0000732
733 if (esw_cfg->mac_anti_spoof)
734 adapter->flags |= QLCNIC_MACSPOOF;
Sony Chackofe4d4342010-08-19 05:08:27 +0000735
Rajesh Borundia73733732010-08-31 17:17:50 +0000736 if (!esw_cfg->mac_override)
737 adapter->flags |= QLCNIC_MAC_OVERRIDE_DISABLED;
738
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000739 if (!esw_cfg->promisc_mode)
740 adapter->flags |= QLCNIC_PROMISC_DISABLED;
741
Rajesh Borundia0325d692010-08-19 05:08:26 +0000742 qlcnic_set_netdev_features(adapter, esw_cfg);
743}
744
745static int
746qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
747{
748 struct qlcnic_esw_func_cfg esw_cfg;
749
750 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
751 return 0;
752
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000753 esw_cfg.pci_func = adapter->ahw->pci_func;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000754 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
755 return -EIO;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +0000756 qlcnic_set_vlan_config(adapter, &esw_cfg);
Rajesh Borundia0325d692010-08-19 05:08:26 +0000757 qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
758
759 return 0;
760}
761
762static void
763qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
764 struct qlcnic_esw_func_cfg *esw_cfg)
765{
766 struct net_device *netdev = adapter->netdev;
767 unsigned long features, vlan_features;
768
Michał Mirosław135d84a2011-04-19 03:03:57 +0000769 features = (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM |
Rajesh Borundia0325d692010-08-19 05:08:26 +0000770 NETIF_F_IPV6_CSUM | NETIF_F_GRO);
771 vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +0000772 NETIF_F_IPV6_CSUM | NETIF_F_HW_VLAN_FILTER);
Rajesh Borundia0325d692010-08-19 05:08:26 +0000773
774 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
775 features |= (NETIF_F_TSO | NETIF_F_TSO6);
776 vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
777 }
Rajesh Borundiab56421d2011-04-01 14:28:31 +0000778
779 if (netdev->features & NETIF_F_LRO)
Rajesh Borundia0325d692010-08-19 05:08:26 +0000780 features |= NETIF_F_LRO;
781
782 if (esw_cfg->offload_flags & BIT_0) {
783 netdev->features |= features;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000784 if (!(esw_cfg->offload_flags & BIT_1))
785 netdev->features &= ~NETIF_F_TSO;
786 if (!(esw_cfg->offload_flags & BIT_2))
787 netdev->features &= ~NETIF_F_TSO6;
788 } else {
789 netdev->features &= ~features;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000790 }
791
792 netdev->vlan_features = (features & vlan_features);
793}
794
795static int
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000796qlcnic_check_eswitch_mode(struct qlcnic_adapter *adapter)
797{
798 void __iomem *priv_op;
799 u32 op_mode, priv_level;
800 int err = 0;
801
Rajesh Borundia174240a2010-08-31 17:17:47 +0000802 err = qlcnic_initialize_nic(adapter);
803 if (err)
804 return err;
805
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000806 if (adapter->flags & QLCNIC_ADAPTER_INITIALIZED)
807 return 0;
808
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000809 priv_op = adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE;
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000810 op_mode = readl(priv_op);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000811 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000812
813 if (op_mode == QLC_DEV_DRV_DEFAULT)
814 priv_level = QLCNIC_MGMT_FUNC;
815 else
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +0000816 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw->pci_func);
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000817
Rajesh Borundia174240a2010-08-31 17:17:47 +0000818 if (adapter->flags & QLCNIC_ESWITCH_ENABLED) {
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000819 if (priv_level == QLCNIC_MGMT_FUNC) {
820 adapter->op_mode = QLCNIC_MGMT_FUNC;
821 err = qlcnic_init_pci_info(adapter);
822 if (err)
823 return err;
824 /* Set privilege level for other functions */
825 qlcnic_set_function_modes(adapter);
826 dev_info(&adapter->pdev->dev,
827 "HAL Version: %d, Management function\n",
828 adapter->fw_hal_version);
829 } else if (priv_level == QLCNIC_PRIV_FUNC) {
830 adapter->op_mode = QLCNIC_PRIV_FUNC;
831 dev_info(&adapter->pdev->dev,
832 "HAL Version: %d, Privileged function\n",
833 adapter->fw_hal_version);
834 }
Rajesh Borundia174240a2010-08-31 17:17:47 +0000835 }
Anirban Chakraborty0866d962010-08-26 14:02:52 +0000836
837 adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
838
839 return err;
840}
841
842static int
Rajesh Borundia0325d692010-08-19 05:08:26 +0000843qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
844{
845 struct qlcnic_esw_func_cfg esw_cfg;
846 struct qlcnic_npar_info *npar;
847 u8 i;
848
Rajesh Borundia174240a2010-08-31 17:17:47 +0000849 if (adapter->need_fw_reset)
Rajesh Borundia0325d692010-08-19 05:08:26 +0000850 return 0;
851
852 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
853 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
854 continue;
855 memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
856 esw_cfg.pci_func = i;
857 esw_cfg.offload_flags = BIT_0;
Rajesh Borundia73733732010-08-31 17:17:50 +0000858 esw_cfg.mac_override = BIT_0;
Rajesh Borundiaee07c1a2010-10-07 23:46:09 +0000859 esw_cfg.promisc_mode = BIT_0;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000860 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
861 esw_cfg.offload_flags |= (BIT_1 | BIT_2);
862 if (qlcnic_config_switch_port(adapter, &esw_cfg))
863 return -EIO;
864 npar = &adapter->npars[i];
865 npar->pvid = esw_cfg.vlan_id;
Rajesh Borundia73733732010-08-31 17:17:50 +0000866 npar->mac_override = esw_cfg.mac_override;
Rajesh Borundia0325d692010-08-19 05:08:26 +0000867 npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
868 npar->discard_tagged = esw_cfg.discard_tagged;
869 npar->promisc_mode = esw_cfg.promisc_mode;
870 npar->offload_flags = esw_cfg.offload_flags;
871 }
872
873 return 0;
874}
875
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000876static int
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000877qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
878 struct qlcnic_npar_info *npar, int pci_func)
879{
880 struct qlcnic_esw_func_cfg esw_cfg;
881 esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
882 esw_cfg.pci_func = pci_func;
883 esw_cfg.vlan_id = npar->pvid;
Rajesh Borundia73733732010-08-31 17:17:50 +0000884 esw_cfg.mac_override = npar->mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000885 esw_cfg.discard_tagged = npar->discard_tagged;
886 esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
887 esw_cfg.offload_flags = npar->offload_flags;
888 esw_cfg.promisc_mode = npar->promisc_mode;
889 if (qlcnic_config_switch_port(adapter, &esw_cfg))
890 return -EIO;
891
892 esw_cfg.op_mode = QLCNIC_ADD_VLAN;
893 if (qlcnic_config_switch_port(adapter, &esw_cfg))
894 return -EIO;
895
896 return 0;
897}
898
899static int
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000900qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
901{
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000902 int i, err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000903 struct qlcnic_npar_info *npar;
904 struct qlcnic_info nic_info;
905
Rajesh Borundia174240a2010-08-31 17:17:47 +0000906 if (!adapter->need_fw_reset)
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000907 return 0;
908
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000909 /* Set the NPAR config data after FW reset */
910 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
911 npar = &adapter->npars[i];
912 if (npar->type != QLCNIC_TYPE_NIC)
913 continue;
914 err = qlcnic_get_nic_info(adapter, &nic_info, i);
915 if (err)
916 return err;
917 nic_info.min_tx_bw = npar->min_bw;
918 nic_info.max_tx_bw = npar->max_bw;
919 err = qlcnic_set_nic_info(adapter, &nic_info);
920 if (err)
921 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000922
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000923 if (npar->enable_pm) {
924 err = qlcnic_config_port_mirroring(adapter,
925 npar->dest_npar, 1, i);
926 if (err)
927 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000928 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000929 err = qlcnic_reset_eswitch_config(adapter, npar, i);
930 if (err)
931 return err;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000932 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +0000933 return 0;
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000934}
935
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +0000936static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
937{
938 u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
939 u32 npar_state;
940
941 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
942 return 0;
943
944 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
945 while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
946 msleep(1000);
947 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
948 }
949 if (!npar_opt_timeo) {
950 dev_err(&adapter->pdev->dev,
951 "Waiting for NPAR state to opertional timeout\n");
952 return -EIO;
953 }
954 return 0;
955}
956
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000957static int
Rajesh Borundia174240a2010-08-31 17:17:47 +0000958qlcnic_set_mgmt_operations(struct qlcnic_adapter *adapter)
959{
960 int err;
961
962 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
963 adapter->op_mode != QLCNIC_MGMT_FUNC)
964 return 0;
965
966 err = qlcnic_set_default_offload_settings(adapter);
967 if (err)
968 return err;
969
970 err = qlcnic_reset_npar_config(adapter);
971 if (err)
972 return err;
973
974 qlcnic_dev_set_npar_ready(adapter);
975
976 return err;
977}
978
979static int
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000980qlcnic_start_firmware(struct qlcnic_adapter *adapter)
981{
Sony Chackod4066832010-08-19 05:08:31 +0000982 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000983
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +0000984 err = qlcnic_can_start_firmware(adapter);
985 if (err < 0)
986 return err;
987 else if (!err)
Sony Chackod4066832010-08-19 05:08:31 +0000988 goto check_fw_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000989
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +0000990 if (load_fw_file)
991 qlcnic_request_firmware(adapter);
schacko8f891382010-06-17 02:56:40 +0000992 else {
Sony Chacko8cfdce02010-08-26 14:02:41 +0000993 err = qlcnic_check_flash_fw_ver(adapter);
994 if (err)
schacko8f891382010-06-17 02:56:40 +0000995 goto err_out;
996
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +0000997 adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
schacko8f891382010-06-17 02:56:40 +0000998 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000999
1000 err = qlcnic_need_fw_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001001 if (err == 0)
Sony Chacko4e708122010-08-31 17:17:44 +00001002 goto check_fw_status;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001003
Sony Chackod4066832010-08-19 05:08:31 +00001004 err = qlcnic_pinit_from_rom(adapter);
1005 if (err)
1006 goto err_out;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001007
1008 err = qlcnic_load_firmware(adapter);
1009 if (err)
1010 goto err_out;
1011
1012 qlcnic_release_firmware(adapter);
Sony Chackod4066832010-08-19 05:08:31 +00001013 QLCWR32(adapter, CRB_DRIVER_VERSION, QLCNIC_DRIVER_VERSION);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001014
Sony Chackod4066832010-08-19 05:08:31 +00001015check_fw_status:
1016 err = qlcnic_check_fw_status(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001017 if (err)
1018 goto err_out;
1019
1020 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001021 qlcnic_idc_debug_info(adapter, 1);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00001022
Anirban Chakraborty0866d962010-08-26 14:02:52 +00001023 err = qlcnic_check_eswitch_mode(adapter);
1024 if (err) {
1025 dev_err(&adapter->pdev->dev,
1026 "Memory allocation failed for eswitch\n");
1027 goto err_out;
1028 }
Rajesh Borundia174240a2010-08-31 17:17:47 +00001029 err = qlcnic_set_mgmt_operations(adapter);
1030 if (err)
1031 goto err_out;
1032
1033 qlcnic_check_options(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001034 adapter->need_fw_reset = 0;
1035
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001036 qlcnic_release_firmware(adapter);
1037 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001038
1039err_out:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001040 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
1041 dev_err(&adapter->pdev->dev, "Device state set to failed\n");
Anirban Chakraborty0866d962010-08-26 14:02:52 +00001042
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001043 qlcnic_release_firmware(adapter);
1044 return err;
1045}
1046
1047static int
1048qlcnic_request_irq(struct qlcnic_adapter *adapter)
1049{
1050 irq_handler_t handler;
1051 struct qlcnic_host_sds_ring *sds_ring;
1052 int err, ring;
1053
1054 unsigned long flags = 0;
1055 struct net_device *netdev = adapter->netdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001056 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001057
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001058 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1059 handler = qlcnic_tmp_intr;
1060 if (!QLCNIC_IS_MSI_FAMILY(adapter))
1061 flags |= IRQF_SHARED;
1062
1063 } else {
1064 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1065 handler = qlcnic_msix_intr;
1066 else if (adapter->flags & QLCNIC_MSI_ENABLED)
1067 handler = qlcnic_msi_intr;
1068 else {
1069 flags |= IRQF_SHARED;
1070 handler = qlcnic_intr;
1071 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001072 }
1073 adapter->irq = netdev->irq;
1074
1075 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1076 sds_ring = &recv_ctx->sds_rings[ring];
1077 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
1078 err = request_irq(sds_ring->irq, handler,
1079 flags, sds_ring->name, sds_ring);
1080 if (err)
1081 return err;
1082 }
1083
1084 return 0;
1085}
1086
1087static void
1088qlcnic_free_irq(struct qlcnic_adapter *adapter)
1089{
1090 int ring;
1091 struct qlcnic_host_sds_ring *sds_ring;
1092
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001093 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001094
1095 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1096 sds_ring = &recv_ctx->sds_rings[ring];
1097 free_irq(sds_ring->irq, sds_ring);
1098 }
1099}
1100
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001101static int
1102__qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1103{
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001104 int ring;
1105 struct qlcnic_host_rds_ring *rds_ring;
1106
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001107 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1108 return -EIO;
1109
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001110 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1111 return 0;
Rajesh Borundia0325d692010-08-19 05:08:26 +00001112 if (qlcnic_set_eswitch_port_config(adapter))
1113 return -EIO;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001114
1115 if (qlcnic_fw_create_ctx(adapter))
1116 return -EIO;
1117
1118 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001119 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1120 qlcnic_post_rx_buffers(adapter, rds_ring);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001121 }
1122
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001123 qlcnic_set_multi(netdev);
1124 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
1125
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001126 adapter->ahw->linkup = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001127
1128 if (adapter->max_sds_rings > 1)
1129 qlcnic_config_rss(adapter, 1);
1130
1131 qlcnic_config_intr_coalesce(adapter);
1132
Sucheta Chakraborty24763d82010-08-17 00:34:25 +00001133 if (netdev->features & NETIF_F_LRO)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001134 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1135
1136 qlcnic_napi_enable(adapter);
1137
1138 qlcnic_linkevent_request(adapter, 1);
1139
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001140 adapter->reset_context = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001141 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1142 return 0;
1143}
1144
1145/* Usage: During resume and firmware recovery module.*/
1146
1147static int
1148qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1149{
1150 int err = 0;
1151
1152 rtnl_lock();
1153 if (netif_running(netdev))
1154 err = __qlcnic_up(adapter, netdev);
1155 rtnl_unlock();
1156
1157 return err;
1158}
1159
1160static void
1161__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1162{
1163 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1164 return;
1165
1166 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1167 return;
1168
1169 smp_mb();
1170 spin_lock(&adapter->tx_clean_lock);
1171 netif_carrier_off(netdev);
1172 netif_tx_disable(netdev);
1173
1174 qlcnic_free_mac_list(adapter);
1175
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001176 if (adapter->fhash.fnum)
1177 qlcnic_delete_lb_filters(adapter);
1178
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001179 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1180
1181 qlcnic_napi_disable(adapter);
1182
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001183 qlcnic_fw_destroy_ctx(adapter);
1184
1185 qlcnic_reset_rx_buffers_list(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001186 qlcnic_release_tx_buffers(adapter);
1187 spin_unlock(&adapter->tx_clean_lock);
1188}
1189
1190/* Usage: During suspend and firmware recovery module */
1191
1192static void
1193qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1194{
1195 rtnl_lock();
1196 if (netif_running(netdev))
1197 __qlcnic_down(adapter, netdev);
1198 rtnl_unlock();
1199
1200}
1201
1202static int
1203qlcnic_attach(struct qlcnic_adapter *adapter)
1204{
1205 struct net_device *netdev = adapter->netdev;
1206 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001207 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001208
1209 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1210 return 0;
1211
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001212 err = qlcnic_napi_add(adapter, netdev);
1213 if (err)
1214 return err;
1215
1216 err = qlcnic_alloc_sw_resources(adapter);
1217 if (err) {
1218 dev_err(&pdev->dev, "Error in setting sw resources\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001219 goto err_out_napi_del;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001220 }
1221
1222 err = qlcnic_alloc_hw_resources(adapter);
1223 if (err) {
1224 dev_err(&pdev->dev, "Error in setting hw resources\n");
1225 goto err_out_free_sw;
1226 }
1227
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001228 err = qlcnic_request_irq(adapter);
1229 if (err) {
1230 dev_err(&pdev->dev, "failed to setup interrupt\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001231 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001232 }
1233
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001234 qlcnic_create_sysfs_entries(adapter);
1235
1236 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1237 return 0;
1238
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001239err_out_free_hw:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001240 qlcnic_free_hw_resources(adapter);
1241err_out_free_sw:
1242 qlcnic_free_sw_resources(adapter);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001243err_out_napi_del:
1244 qlcnic_napi_del(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001245 return err;
1246}
1247
1248static void
1249qlcnic_detach(struct qlcnic_adapter *adapter)
1250{
1251 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1252 return;
1253
1254 qlcnic_remove_sysfs_entries(adapter);
1255
1256 qlcnic_free_hw_resources(adapter);
1257 qlcnic_release_rx_buffers(adapter);
1258 qlcnic_free_irq(adapter);
1259 qlcnic_napi_del(adapter);
1260 qlcnic_free_sw_resources(adapter);
1261
1262 adapter->is_up = 0;
1263}
1264
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001265void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1266{
1267 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1268 struct qlcnic_host_sds_ring *sds_ring;
1269 int ring;
1270
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001271 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001272 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1273 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001274 sds_ring = &adapter->recv_ctx->sds_rings[ring];
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001275 qlcnic_disable_int(sds_ring);
1276 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001277 }
1278
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001279 qlcnic_fw_destroy_ctx(adapter);
1280
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001281 qlcnic_detach(adapter);
1282
1283 adapter->diag_test = 0;
1284 adapter->max_sds_rings = max_sds_rings;
1285
1286 if (qlcnic_attach(adapter))
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001287 goto out;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001288
1289 if (netif_running(netdev))
1290 __qlcnic_up(adapter, netdev);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001291out:
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001292 netif_device_attach(netdev);
1293}
1294
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001295static int qlcnic_alloc_adapter_resources(struct qlcnic_adapter *adapter)
1296{
1297 int err = 0;
1298 adapter->ahw = kzalloc(sizeof(struct qlcnic_hardware_context),
1299 GFP_KERNEL);
1300 if (!adapter->ahw) {
1301 dev_err(&adapter->pdev->dev,
1302 "Failed to allocate recv ctx resources for adapter\n");
1303 err = -ENOMEM;
1304 goto err_out;
1305 }
1306 adapter->recv_ctx = kzalloc(sizeof(struct qlcnic_recv_context),
1307 GFP_KERNEL);
1308 if (!adapter->recv_ctx) {
1309 dev_err(&adapter->pdev->dev,
1310 "Failed to allocate recv ctx resources for adapter\n");
1311 kfree(adapter->ahw);
1312 adapter->ahw = NULL;
1313 err = -ENOMEM;
Anirban Chakraborty8816d002011-04-01 14:28:21 +00001314 goto err_out;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001315 }
Anirban Chakraborty8816d002011-04-01 14:28:21 +00001316 /* Initialize interrupt coalesce parameters */
1317 adapter->ahw->coal.flag = QLCNIC_INTR_DEFAULT;
1318 adapter->ahw->coal.rx_time_us = QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
1319 adapter->ahw->coal.rx_packets = QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001320err_out:
1321 return err;
1322}
1323
1324static void qlcnic_free_adapter_resources(struct qlcnic_adapter *adapter)
1325{
1326 kfree(adapter->recv_ctx);
1327 adapter->recv_ctx = NULL;
1328
1329 kfree(adapter->ahw);
1330 adapter->ahw = NULL;
1331}
1332
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001333int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1334{
1335 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1336 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001337 struct qlcnic_host_rds_ring *rds_ring;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001338 int ring;
1339 int ret;
1340
1341 netif_device_detach(netdev);
1342
1343 if (netif_running(netdev))
1344 __qlcnic_down(adapter, netdev);
1345
1346 qlcnic_detach(adapter);
1347
1348 adapter->max_sds_rings = 1;
1349 adapter->diag_test = test;
1350
1351 ret = qlcnic_attach(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001352 if (ret) {
1353 netif_device_attach(netdev);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001354 return ret;
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001355 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001356
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001357 ret = qlcnic_fw_create_ctx(adapter);
1358 if (ret) {
1359 qlcnic_detach(adapter);
Sony Chacko57e46242010-07-24 18:32:18 +00001360 netif_device_attach(netdev);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001361 return ret;
1362 }
1363
1364 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001365 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1366 qlcnic_post_rx_buffers(adapter, rds_ring);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001367 }
1368
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001369 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1370 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001371 sds_ring = &adapter->recv_ctx->sds_rings[ring];
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001372 qlcnic_enable_int(sds_ring);
1373 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001374 }
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001375 set_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001376
1377 return 0;
1378}
1379
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001380/* Reset context in hardware only */
1381static int
1382qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1383{
1384 struct net_device *netdev = adapter->netdev;
1385
1386 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1387 return -EBUSY;
1388
1389 netif_device_detach(netdev);
1390
1391 qlcnic_down(adapter, netdev);
1392
1393 qlcnic_up(adapter, netdev);
1394
1395 netif_device_attach(netdev);
1396
1397 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1398 return 0;
1399}
1400
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001401int
1402qlcnic_reset_context(struct qlcnic_adapter *adapter)
1403{
1404 int err = 0;
1405 struct net_device *netdev = adapter->netdev;
1406
1407 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1408 return -EBUSY;
1409
1410 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1411
1412 netif_device_detach(netdev);
1413
1414 if (netif_running(netdev))
1415 __qlcnic_down(adapter, netdev);
1416
1417 qlcnic_detach(adapter);
1418
1419 if (netif_running(netdev)) {
1420 err = qlcnic_attach(adapter);
1421 if (!err)
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001422 __qlcnic_up(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001423 }
1424
1425 netif_device_attach(netdev);
1426 }
1427
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001428 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1429 return err;
1430}
1431
1432static int
1433qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001434 struct net_device *netdev, u8 pci_using_dac)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001435{
1436 int err;
1437 struct pci_dev *pdev = adapter->pdev;
1438
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001439 adapter->mc_enabled = 0;
1440 adapter->max_mc_count = 38;
1441
1442 netdev->netdev_ops = &qlcnic_netdev_ops;
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00001443 netdev->watchdog_timeo = 5*HZ;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001444
1445 qlcnic_change_mtu(netdev, netdev->mtu);
1446
1447 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1448
Michał Mirosław135d84a2011-04-19 03:03:57 +00001449 netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1450 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
Anirban Chakrabortyac8d0c42010-07-09 13:14:58 +00001451
Michał Mirosław135d84a2011-04-19 03:03:57 +00001452 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
1453 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1454 if (pci_using_dac)
1455 netdev->hw_features |= NETIF_F_HIGHDMA;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001456
Michał Mirosław135d84a2011-04-19 03:03:57 +00001457 netdev->vlan_features = netdev->hw_features;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001458
1459 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
Michał Mirosław135d84a2011-04-19 03:03:57 +00001460 netdev->hw_features |= NETIF_F_HW_VLAN_TX;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001461 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
Michał Mirosław135d84a2011-04-19 03:03:57 +00001462 netdev->hw_features |= NETIF_F_LRO;
1463
1464 netdev->features |= netdev->hw_features |
1465 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
1466
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001467 netdev->irq = adapter->msix_entries[0].vector;
1468
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001469 netif_carrier_off(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001470
1471 err = register_netdev(netdev);
1472 if (err) {
1473 dev_err(&pdev->dev, "failed to register net device\n");
1474 return err;
1475 }
1476
1477 return 0;
1478}
1479
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001480static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1481{
1482 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1483 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1484 *pci_using_dac = 1;
1485 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1486 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1487 *pci_using_dac = 0;
1488 else {
1489 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1490 return -EIO;
1491 }
1492
1493 return 0;
1494}
1495
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001496static int __devinit
1497qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1498{
1499 struct net_device *netdev = NULL;
1500 struct qlcnic_adapter *adapter = NULL;
1501 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001502 uint8_t revision_id;
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001503 uint8_t pci_using_dac;
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001504 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001505
1506 err = pci_enable_device(pdev);
1507 if (err)
1508 return err;
1509
1510 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1511 err = -ENODEV;
1512 goto err_out_disable_pdev;
1513 }
1514
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001515 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1516 if (err)
1517 goto err_out_disable_pdev;
1518
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001519 err = pci_request_regions(pdev, qlcnic_driver_name);
1520 if (err)
1521 goto err_out_disable_pdev;
1522
1523 pci_set_master(pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001524 pci_enable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001525
1526 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1527 if (!netdev) {
1528 dev_err(&pdev->dev, "failed to allocate net_device\n");
1529 err = -ENOMEM;
1530 goto err_out_free_res;
1531 }
1532
1533 SET_NETDEV_DEV(netdev, &pdev->dev);
1534
1535 adapter = netdev_priv(netdev);
1536 adapter->netdev = netdev;
1537 adapter->pdev = pdev;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001538
1539 if (qlcnic_alloc_adapter_resources(adapter))
1540 goto err_out_free_netdev;
1541
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001542 adapter->dev_rst_time = jiffies;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001543 revision_id = pdev->revision;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001544 adapter->ahw->revision_id = revision_id;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001545
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001546 rwlock_init(&adapter->ahw->crb_lock);
1547 mutex_init(&adapter->ahw->mem_lock);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001548
1549 spin_lock_init(&adapter->tx_clean_lock);
1550 INIT_LIST_HEAD(&adapter->mac_list);
1551
1552 err = qlcnic_setup_pci_map(adapter);
1553 if (err)
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001554 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001555
1556 /* This will be reset for mezz cards */
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001557 adapter->portnum = adapter->ahw->pci_func;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001558
1559 err = qlcnic_get_board_info(adapter);
1560 if (err) {
1561 dev_err(&pdev->dev, "Error getting board config info.\n");
1562 goto err_out_iounmap;
1563 }
1564
Sony Chacko8cfdce02010-08-26 14:02:41 +00001565 err = qlcnic_setup_idc_param(adapter);
1566 if (err)
Sucheta Chakrabortyb3a24642010-05-13 03:07:48 +00001567 goto err_out_iounmap;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001568
Rajesh Borundia1dc0f3c2010-12-16 22:59:02 +00001569 adapter->flags |= QLCNIC_NEED_FLR;
Rajesh Borundiab0044bc2010-11-23 01:25:21 +00001570
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001571 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001572 if (err) {
1573 dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001574 goto err_out_decr_ref;
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001575 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001576
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001577 if (qlcnic_read_mac_addr(adapter))
1578 dev_warn(&pdev->dev, "failed to read mac addr\n");
1579
1580 if (adapter->portnum == 0) {
1581 get_brd_name(adapter, brd_name);
1582
1583 pr_info("%s: %s Board Chip rev 0x%x\n",
1584 module_name(THIS_MODULE),
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001585 brd_name, adapter->ahw->revision_id);
Rajesh Borundiada48e6c2010-08-31 17:17:46 +00001586 }
1587
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001588 qlcnic_clear_stats(adapter);
1589
1590 qlcnic_setup_intr(adapter);
1591
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001592 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001593 if (err)
1594 goto err_out_disable_msi;
1595
1596 pci_set_drvdata(pdev, adapter);
1597
1598 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1599
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001600 switch (adapter->ahw->port_type) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001601 case QLCNIC_GBE:
1602 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1603 adapter->netdev->name);
1604 break;
1605 case QLCNIC_XGBE:
1606 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1607 adapter->netdev->name);
1608 break;
1609 }
1610
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001611 qlcnic_alloc_lb_filters_mem(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001612 qlcnic_create_diag_entries(adapter);
1613
1614 return 0;
1615
1616err_out_disable_msi:
1617 qlcnic_teardown_intr(adapter);
1618
1619err_out_decr_ref:
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001620 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001621
1622err_out_iounmap:
1623 qlcnic_cleanup_pci_map(adapter);
1624
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001625err_out_free_hw:
1626 qlcnic_free_adapter_resources(adapter);
1627
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001628err_out_free_netdev:
1629 free_netdev(netdev);
1630
1631err_out_free_res:
1632 pci_release_regions(pdev);
1633
1634err_out_disable_pdev:
1635 pci_set_drvdata(pdev, NULL);
1636 pci_disable_device(pdev);
1637 return err;
1638}
1639
1640static void __devexit qlcnic_remove(struct pci_dev *pdev)
1641{
1642 struct qlcnic_adapter *adapter;
1643 struct net_device *netdev;
1644
1645 adapter = pci_get_drvdata(pdev);
1646 if (adapter == NULL)
1647 return;
1648
1649 netdev = adapter->netdev;
1650
1651 qlcnic_cancel_fw_work(adapter);
1652
1653 unregister_netdev(netdev);
1654
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001655 qlcnic_detach(adapter);
1656
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001657 if (adapter->npars != NULL)
1658 kfree(adapter->npars);
1659 if (adapter->eswitch != NULL)
1660 kfree(adapter->eswitch);
1661
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001662 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001663
1664 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1665
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001666 qlcnic_free_lb_filters_mem(adapter);
1667
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001668 qlcnic_teardown_intr(adapter);
1669
1670 qlcnic_remove_diag_entries(adapter);
1671
1672 qlcnic_cleanup_pci_map(adapter);
1673
1674 qlcnic_release_firmware(adapter);
1675
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001676 pci_disable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001677 pci_release_regions(pdev);
1678 pci_disable_device(pdev);
1679 pci_set_drvdata(pdev, NULL);
1680
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00001681 qlcnic_free_adapter_resources(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001682 free_netdev(netdev);
1683}
1684static int __qlcnic_shutdown(struct pci_dev *pdev)
1685{
1686 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1687 struct net_device *netdev = adapter->netdev;
1688 int retval;
1689
1690 netif_device_detach(netdev);
1691
1692 qlcnic_cancel_fw_work(adapter);
1693
1694 if (netif_running(netdev))
1695 qlcnic_down(adapter, netdev);
1696
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00001697 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001698
1699 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1700
1701 retval = pci_save_state(pdev);
1702 if (retval)
1703 return retval;
1704
1705 if (qlcnic_wol_supported(adapter)) {
1706 pci_enable_wake(pdev, PCI_D3cold, 1);
1707 pci_enable_wake(pdev, PCI_D3hot, 1);
1708 }
1709
1710 return 0;
1711}
1712
1713static void qlcnic_shutdown(struct pci_dev *pdev)
1714{
1715 if (__qlcnic_shutdown(pdev))
1716 return;
1717
1718 pci_disable_device(pdev);
1719}
1720
1721#ifdef CONFIG_PM
1722static int
1723qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1724{
1725 int retval;
1726
1727 retval = __qlcnic_shutdown(pdev);
1728 if (retval)
1729 return retval;
1730
1731 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1732 return 0;
1733}
1734
1735static int
1736qlcnic_resume(struct pci_dev *pdev)
1737{
1738 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1739 struct net_device *netdev = adapter->netdev;
1740 int err;
1741
1742 err = pci_enable_device(pdev);
1743 if (err)
1744 return err;
1745
1746 pci_set_power_state(pdev, PCI_D0);
1747 pci_set_master(pdev);
1748 pci_restore_state(pdev);
1749
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001750 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001751 if (err) {
1752 dev_err(&pdev->dev, "failed to start firmware\n");
1753 return err;
1754 }
1755
1756 if (netif_running(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001757 err = qlcnic_up(adapter, netdev);
1758 if (err)
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001759 goto done;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001760
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00001761 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001762 }
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001763done:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001764 netif_device_attach(netdev);
1765 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1766 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001767}
1768#endif
1769
1770static int qlcnic_open(struct net_device *netdev)
1771{
1772 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1773 int err;
1774
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001775 err = qlcnic_attach(adapter);
1776 if (err)
1777 return err;
1778
1779 err = __qlcnic_up(adapter, netdev);
1780 if (err)
1781 goto err_out;
1782
1783 netif_start_queue(netdev);
1784
1785 return 0;
1786
1787err_out:
1788 qlcnic_detach(adapter);
1789 return err;
1790}
1791
1792/*
1793 * qlcnic_close - Disables a network interface entry point
1794 */
1795static int qlcnic_close(struct net_device *netdev)
1796{
1797 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1798
1799 __qlcnic_down(adapter, netdev);
1800 return 0;
1801}
1802
1803static void
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001804qlcnic_alloc_lb_filters_mem(struct qlcnic_adapter *adapter)
1805{
1806 void *head;
1807 int i;
1808
1809 if (!qlcnic_mac_learn)
1810 return;
1811
1812 spin_lock_init(&adapter->mac_learn_lock);
1813
1814 head = kcalloc(QLCNIC_LB_MAX_FILTERS, sizeof(struct hlist_head),
1815 GFP_KERNEL);
1816 if (!head)
1817 return;
1818
1819 adapter->fhash.fmax = QLCNIC_LB_MAX_FILTERS;
1820 adapter->fhash.fhead = (struct hlist_head *)head;
1821
1822 for (i = 0; i < adapter->fhash.fmax; i++)
1823 INIT_HLIST_HEAD(&adapter->fhash.fhead[i]);
1824}
1825
1826static void qlcnic_free_lb_filters_mem(struct qlcnic_adapter *adapter)
1827{
1828 if (adapter->fhash.fmax && adapter->fhash.fhead)
1829 kfree(adapter->fhash.fhead);
1830
1831 adapter->fhash.fhead = NULL;
1832 adapter->fhash.fmax = 0;
1833}
1834
1835static void qlcnic_change_filter(struct qlcnic_adapter *adapter,
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001836 u64 uaddr, __le16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001837{
1838 struct cmd_desc_type0 *hwdesc;
1839 struct qlcnic_nic_req *req;
1840 struct qlcnic_mac_req *mac_req;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001841 struct qlcnic_vlan_req *vlan_req;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001842 u32 producer;
1843 u64 word;
1844
1845 producer = tx_ring->producer;
1846 hwdesc = &tx_ring->desc_head[tx_ring->producer];
1847
1848 req = (struct qlcnic_nic_req *)hwdesc;
1849 memset(req, 0, sizeof(struct qlcnic_nic_req));
1850 req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
1851
1852 word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
1853 req->req_hdr = cpu_to_le64(word);
1854
1855 mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001856 mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001857 memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
1858
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001859 vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
1860 vlan_req->vlan_id = vlan_id;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001861
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001862 tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001863 smp_mb();
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001864}
1865
1866#define QLCNIC_MAC_HASH(MAC)\
1867 ((((MAC) & 0x70000) >> 0x10) | (((MAC) & 0x70000000000ULL) >> 0x25))
1868
1869static void
1870qlcnic_send_filter(struct qlcnic_adapter *adapter,
1871 struct qlcnic_host_tx_ring *tx_ring,
1872 struct cmd_desc_type0 *first_desc,
1873 struct sk_buff *skb)
1874{
1875 struct ethhdr *phdr = (struct ethhdr *)(skb->data);
1876 struct qlcnic_filter *fil, *tmp_fil;
1877 struct hlist_node *tmp_hnode, *n;
1878 struct hlist_head *head;
1879 u64 src_addr = 0;
Sucheta Chakraborty7e56cac2010-10-04 04:20:13 +00001880 __le16 vlan_id = 0;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001881 u8 hindex;
1882
1883 if (!compare_ether_addr(phdr->h_source, adapter->mac_addr))
1884 return;
1885
1886 if (adapter->fhash.fnum >= adapter->fhash.fmax)
1887 return;
1888
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001889 /* Only NPAR capable devices support vlan based learning*/
1890 if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
1891 vlan_id = first_desc->vlan_TCI;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001892 memcpy(&src_addr, phdr->h_source, ETH_ALEN);
1893 hindex = QLCNIC_MAC_HASH(src_addr) & (QLCNIC_LB_MAX_FILTERS - 1);
1894 head = &(adapter->fhash.fhead[hindex]);
1895
1896 hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001897 if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
1898 tmp_fil->vlan_id == vlan_id) {
amit salechae5edb7b2010-10-26 17:53:07 +00001899
1900 if (jiffies >
1901 (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
1902 qlcnic_change_filter(adapter, src_addr, vlan_id,
1903 tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001904 tmp_fil->ftime = jiffies;
1905 return;
1906 }
1907 }
1908
1909 fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
1910 if (!fil)
1911 return;
1912
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001913 qlcnic_change_filter(adapter, src_addr, vlan_id, tx_ring);
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001914
1915 fil->ftime = jiffies;
Amit Kumar Salecha03c5d772010-08-31 17:17:52 +00001916 fil->vlan_id = vlan_id;
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00001917 memcpy(fil->faddr, &src_addr, ETH_ALEN);
1918 spin_lock(&adapter->mac_learn_lock);
1919 hlist_add_head(&(fil->fnode), head);
1920 adapter->fhash.fnum++;
1921 spin_unlock(&adapter->mac_learn_lock);
1922}
1923
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001924static int
1925qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001926 struct cmd_desc_type0 *first_desc,
1927 struct sk_buff *skb)
1928{
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001929 u8 opcode = 0, hdr_len = 0;
1930 u16 flags = 0, vlan_tci = 0;
1931 int copied, offset, copy_len;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001932 struct cmd_desc_type0 *hwdesc;
1933 struct vlan_ethhdr *vh;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001934 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1935 u16 protocol = ntohs(skb->protocol);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001936 u32 producer = tx_ring->producer;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001937
1938 if (protocol == ETH_P_8021Q) {
1939 vh = (struct vlan_ethhdr *)skb->data;
1940 flags = FLAGS_VLAN_TAGGED;
1941 vlan_tci = vh->h_vlan_TCI;
1942 } else if (vlan_tx_tag_present(skb)) {
1943 flags = FLAGS_VLAN_OOB;
1944 vlan_tci = vlan_tx_tag_get(skb);
1945 }
1946 if (unlikely(adapter->pvid)) {
1947 if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
1948 return -EIO;
1949 if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
1950 goto set_flags;
1951
1952 flags = FLAGS_VLAN_OOB;
1953 vlan_tci = adapter->pvid;
1954 }
1955set_flags:
1956 qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
1957 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001958
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001959 if (*(skb->data) & BIT_0) {
1960 flags |= BIT_0;
1961 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
1962 }
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001963 opcode = TX_ETHER_PKT;
1964 if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001965 skb_shinfo(skb)->gso_size > 0) {
1966
1967 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1968
1969 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1970 first_desc->total_hdr_length = hdr_len;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001971
1972 opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
1973
1974 /* For LSO, we need to copy the MAC/IP/TCP headers into
1975 * the descriptor ring */
1976 copied = 0;
1977 offset = 2;
1978
1979 if (flags & FLAGS_VLAN_OOB) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001980 first_desc->total_hdr_length += VLAN_HLEN;
1981 first_desc->tcp_hdr_offset = VLAN_HLEN;
1982 first_desc->ip_hdr_offset = VLAN_HLEN;
1983 /* Only in case of TSO on vlan device */
1984 flags |= FLAGS_VLAN_TAGGED;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00001985
1986 /* Create a TSO vlan header template for firmware */
1987
1988 hwdesc = &tx_ring->desc_head[producer];
1989 tx_ring->cmd_buf_arr[producer].skb = NULL;
1990
1991 copy_len = min((int)sizeof(struct cmd_desc_type0) -
1992 offset, hdr_len + VLAN_HLEN);
1993
1994 vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
1995 skb_copy_from_linear_data(skb, vh, 12);
1996 vh->h_vlan_proto = htons(ETH_P_8021Q);
1997 vh->h_vlan_TCI = htons(vlan_tci);
1998
1999 skb_copy_from_linear_data_offset(skb, 12,
2000 (char *)vh + 16, copy_len - 16);
2001
2002 copied = copy_len - VLAN_HLEN;
2003 offset = 0;
2004
2005 producer = get_next_index(producer, tx_ring->num_desc);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002006 }
2007
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002008 while (copied < hdr_len) {
2009
2010 copy_len = min((int)sizeof(struct cmd_desc_type0) -
2011 offset, (hdr_len - copied));
2012
2013 hwdesc = &tx_ring->desc_head[producer];
2014 tx_ring->cmd_buf_arr[producer].skb = NULL;
2015
2016 skb_copy_from_linear_data_offset(skb, copied,
2017 (char *) hwdesc + offset, copy_len);
2018
2019 copied += copy_len;
2020 offset = 0;
2021
2022 producer = get_next_index(producer, tx_ring->num_desc);
2023 }
2024
2025 tx_ring->producer = producer;
2026 smp_mb();
2027 adapter->stats.lso_frames++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002028
2029 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2030 u8 l4proto;
2031
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002032 if (protocol == ETH_P_IP) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002033 l4proto = ip_hdr(skb)->protocol;
2034
2035 if (l4proto == IPPROTO_TCP)
2036 opcode = TX_TCP_PKT;
2037 else if (l4proto == IPPROTO_UDP)
2038 opcode = TX_UDP_PKT;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002039 } else if (protocol == ETH_P_IPV6) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002040 l4proto = ipv6_hdr(skb)->nexthdr;
2041
2042 if (l4proto == IPPROTO_TCP)
2043 opcode = TX_TCPV6_PKT;
2044 else if (l4proto == IPPROTO_UDP)
2045 opcode = TX_UDPV6_PKT;
2046 }
2047 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002048 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
2049 first_desc->ip_hdr_offset += skb_network_offset(skb);
2050 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
2051
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002052 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002053}
2054
2055static int
2056qlcnic_map_tx_skb(struct pci_dev *pdev,
2057 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
2058{
2059 struct qlcnic_skb_frag *nf;
2060 struct skb_frag_struct *frag;
2061 int i, nr_frags;
2062 dma_addr_t map;
2063
2064 nr_frags = skb_shinfo(skb)->nr_frags;
2065 nf = &pbuf->frag_array[0];
2066
2067 map = pci_map_single(pdev, skb->data,
2068 skb_headlen(skb), PCI_DMA_TODEVICE);
2069 if (pci_dma_mapping_error(pdev, map))
2070 goto out_err;
2071
2072 nf->dma = map;
2073 nf->length = skb_headlen(skb);
2074
2075 for (i = 0; i < nr_frags; i++) {
2076 frag = &skb_shinfo(skb)->frags[i];
2077 nf = &pbuf->frag_array[i+1];
2078
2079 map = pci_map_page(pdev, frag->page, frag->page_offset,
2080 frag->size, PCI_DMA_TODEVICE);
2081 if (pci_dma_mapping_error(pdev, map))
2082 goto unwind;
2083
2084 nf->dma = map;
2085 nf->length = frag->size;
2086 }
2087
2088 return 0;
2089
2090unwind:
2091 while (--i >= 0) {
2092 nf = &pbuf->frag_array[i+1];
2093 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
2094 }
2095
2096 nf = &pbuf->frag_array[0];
2097 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
2098
2099out_err:
2100 return -ENOMEM;
2101}
2102
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002103static void
2104qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
2105 struct qlcnic_cmd_buffer *pbuf)
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002106{
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002107 struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
2108 int nr_frags = skb_shinfo(skb)->nr_frags;
2109 int i;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002110
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002111 for (i = 0; i < nr_frags; i++) {
2112 nf = &pbuf->frag_array[i+1];
2113 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002114 }
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002115
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002116 nf = &pbuf->frag_array[0];
2117 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002118}
2119
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002120static inline void
2121qlcnic_clear_cmddesc(u64 *desc)
2122{
2123 desc[0] = 0ULL;
2124 desc[2] = 0ULL;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002125 desc[7] = 0ULL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002126}
2127
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00002128netdev_tx_t
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002129qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2130{
2131 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2132 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2133 struct qlcnic_cmd_buffer *pbuf;
2134 struct qlcnic_skb_frag *buffrag;
2135 struct cmd_desc_type0 *hwdesc, *first_desc;
2136 struct pci_dev *pdev;
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002137 struct ethhdr *phdr;
Amit Kumar Salecha91a403c2011-04-12 17:05:55 +00002138 int delta = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002139 int i, k;
2140
2141 u32 producer;
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002142 int frag_count;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002143 u32 num_txd = tx_ring->num_desc;
2144
Amit Kumar Salecha780ab792010-04-22 02:51:41 +00002145 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
2146 netif_stop_queue(netdev);
2147 return NETDEV_TX_BUSY;
2148 }
2149
Sony Chackofe4d4342010-08-19 05:08:27 +00002150 if (adapter->flags & QLCNIC_MACSPOOF) {
Rajesh Borundiadcb50af2010-08-31 17:17:49 +00002151 phdr = (struct ethhdr *)skb->data;
2152 if (compare_ether_addr(phdr->h_source,
Sony Chackofe4d4342010-08-19 05:08:27 +00002153 adapter->mac_addr))
2154 goto drop_packet;
2155 }
2156
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002157 frag_count = skb_shinfo(skb)->nr_frags + 1;
Amit Kumar Salecha91a403c2011-04-12 17:05:55 +00002158 /* 14 frags supported for normal packet and
2159 * 32 frags supported for TSO packet
2160 */
2161 if (!skb_is_gso(skb) && frag_count > QLCNIC_MAX_FRAGS_PER_TX) {
2162
2163 for (i = 0; i < (frag_count - QLCNIC_MAX_FRAGS_PER_TX); i++)
2164 delta += skb_shinfo(skb)->frags[i].size;
2165
2166 if (!__pskb_pull_tail(skb, delta))
2167 goto drop_packet;
2168
2169 frag_count = 1 + skb_shinfo(skb)->nr_frags;
2170 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002171
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002172 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002173 netif_stop_queue(netdev);
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002174 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
2175 netif_start_queue(netdev);
2176 else {
2177 adapter->stats.xmit_off++;
2178 return NETDEV_TX_BUSY;
2179 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002180 }
2181
2182 producer = tx_ring->producer;
2183 pbuf = &tx_ring->cmd_buf_arr[producer];
2184
2185 pdev = adapter->pdev;
2186
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00002187 first_desc = hwdesc = &tx_ring->desc_head[producer];
2188 qlcnic_clear_cmddesc((u64 *)hwdesc);
2189
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002190 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
2191 adapter->stats.tx_dma_map_error++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002192 goto drop_packet;
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00002193 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002194
2195 pbuf->skb = skb;
2196 pbuf->frag_count = frag_count;
2197
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002198 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
2199 qlcnic_set_tx_port(first_desc, adapter->portnum);
2200
2201 for (i = 0; i < frag_count; i++) {
2202
2203 k = i % 4;
2204
2205 if ((k == 0) && (i > 0)) {
2206 /* move to next desc.*/
2207 producer = get_next_index(producer, num_txd);
2208 hwdesc = &tx_ring->desc_head[producer];
2209 qlcnic_clear_cmddesc((u64 *)hwdesc);
2210 tx_ring->cmd_buf_arr[producer].skb = NULL;
2211 }
2212
2213 buffrag = &pbuf->frag_array[i];
2214
2215 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
2216 switch (k) {
2217 case 0:
2218 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
2219 break;
2220 case 1:
2221 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
2222 break;
2223 case 2:
2224 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
2225 break;
2226 case 3:
2227 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
2228 break;
2229 }
2230 }
2231
2232 tx_ring->producer = get_next_index(producer, num_txd);
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002233 smp_mb();
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002234
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002235 if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb)))
2236 goto unwind_buff;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002237
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00002238 if (qlcnic_mac_learn)
2239 qlcnic_send_filter(adapter, tx_ring, first_desc, skb);
2240
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002241 qlcnic_update_cmd_producer(adapter, tx_ring);
2242
2243 adapter->stats.txbytes += skb->len;
2244 adapter->stats.xmitcalled++;
2245
2246 return NETDEV_TX_OK;
2247
Anirban Chakraborty036d61f2011-04-01 14:28:11 +00002248unwind_buff:
2249 qlcnic_unmap_buffers(pdev, skb, pbuf);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002250drop_packet:
2251 adapter->stats.txdropped++;
2252 dev_kfree_skb_any(skb);
2253 return NETDEV_TX_OK;
2254}
2255
2256static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
2257{
2258 struct net_device *netdev = adapter->netdev;
2259 u32 temp, temp_state, temp_val;
2260 int rv = 0;
2261
2262 temp = QLCRD32(adapter, CRB_TEMP_STATE);
2263
2264 temp_state = qlcnic_get_temp_state(temp);
2265 temp_val = qlcnic_get_temp_val(temp);
2266
2267 if (temp_state == QLCNIC_TEMP_PANIC) {
2268 dev_err(&netdev->dev,
2269 "Device temperature %d degrees C exceeds"
2270 " maximum allowed. Hardware has been shut down.\n",
2271 temp_val);
2272 rv = 1;
2273 } else if (temp_state == QLCNIC_TEMP_WARN) {
2274 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
2275 dev_err(&netdev->dev,
2276 "Device temperature %d degrees C "
2277 "exceeds operating range."
2278 " Immediate action needed.\n",
2279 temp_val);
2280 }
2281 } else {
2282 if (adapter->temp == QLCNIC_TEMP_WARN) {
2283 dev_info(&netdev->dev,
2284 "Device temperature is now %d degrees C"
2285 " in normal range.\n", temp_val);
2286 }
2287 }
2288 adapter->temp = temp_state;
2289 return rv;
2290}
2291
2292void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
2293{
2294 struct net_device *netdev = adapter->netdev;
2295
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002296 if (adapter->ahw->linkup && !linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002297 netdev_info(netdev, "NIC Link is down\n");
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002298 adapter->ahw->linkup = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002299 if (netif_running(netdev)) {
2300 netif_carrier_off(netdev);
2301 netif_stop_queue(netdev);
2302 }
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002303 } else if (!adapter->ahw->linkup && linkup) {
Sony Chacko69324272010-08-17 00:34:23 +00002304 netdev_info(netdev, "NIC Link is up\n");
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002305 adapter->ahw->linkup = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002306 if (netif_running(netdev)) {
2307 netif_carrier_on(netdev);
2308 netif_wake_queue(netdev);
2309 }
2310 }
2311}
2312
2313static void qlcnic_tx_timeout(struct net_device *netdev)
2314{
2315 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2316
2317 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2318 return;
2319
2320 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002321
2322 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00002323 adapter->need_fw_reset = 1;
2324 else
2325 adapter->reset_context = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002326}
2327
2328static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
2329{
2330 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2331 struct net_device_stats *stats = &netdev->stats;
2332
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002333 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
2334 stats->tx_packets = adapter->stats.xmitfinished;
Sucheta Chakraborty7e382592010-05-17 01:22:10 +00002335 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002336 stats->tx_bytes = adapter->stats.txbytes;
2337 stats->rx_dropped = adapter->stats.rxdropped;
2338 stats->tx_dropped = adapter->stats.txdropped;
2339
2340 return stats;
2341}
2342
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002343static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002344{
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002345 u32 status;
2346
2347 status = readl(adapter->isr_int_vec);
2348
2349 if (!(status & adapter->int_vec_bit))
2350 return IRQ_NONE;
2351
2352 /* check interrupt state machine, to be sure */
2353 status = readl(adapter->crb_int_state_reg);
2354 if (!ISR_LEGACY_INT_TRIGGERED(status))
2355 return IRQ_NONE;
2356
2357 writel(0xffffffff, adapter->tgt_status_reg);
2358 /* read twice to ensure write is flushed */
2359 readl(adapter->isr_int_vec);
2360 readl(adapter->isr_int_vec);
2361
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002362 return IRQ_HANDLED;
2363}
2364
2365static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2366{
2367 struct qlcnic_host_sds_ring *sds_ring = data;
2368 struct qlcnic_adapter *adapter = sds_ring->adapter;
2369
2370 if (adapter->flags & QLCNIC_MSIX_ENABLED)
2371 goto done;
2372 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2373 writel(0xffffffff, adapter->tgt_status_reg);
2374 goto done;
2375 }
2376
2377 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2378 return IRQ_NONE;
2379
2380done:
2381 adapter->diag_cnt++;
2382 qlcnic_enable_int(sds_ring);
2383 return IRQ_HANDLED;
2384}
2385
2386static irqreturn_t qlcnic_intr(int irq, void *data)
2387{
2388 struct qlcnic_host_sds_ring *sds_ring = data;
2389 struct qlcnic_adapter *adapter = sds_ring->adapter;
2390
2391 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2392 return IRQ_NONE;
2393
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002394 napi_schedule(&sds_ring->napi);
2395
2396 return IRQ_HANDLED;
2397}
2398
2399static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2400{
2401 struct qlcnic_host_sds_ring *sds_ring = data;
2402 struct qlcnic_adapter *adapter = sds_ring->adapter;
2403
2404 /* clear interrupt */
2405 writel(0xffffffff, adapter->tgt_status_reg);
2406
2407 napi_schedule(&sds_ring->napi);
2408 return IRQ_HANDLED;
2409}
2410
2411static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2412{
2413 struct qlcnic_host_sds_ring *sds_ring = data;
2414
2415 napi_schedule(&sds_ring->napi);
2416 return IRQ_HANDLED;
2417}
2418
2419static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2420{
2421 u32 sw_consumer, hw_consumer;
2422 int count = 0, i;
2423 struct qlcnic_cmd_buffer *buffer;
2424 struct pci_dev *pdev = adapter->pdev;
2425 struct net_device *netdev = adapter->netdev;
2426 struct qlcnic_skb_frag *frag;
2427 int done;
2428 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2429
2430 if (!spin_trylock(&adapter->tx_clean_lock))
2431 return 1;
2432
2433 sw_consumer = tx_ring->sw_consumer;
2434 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2435
2436 while (sw_consumer != hw_consumer) {
2437 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2438 if (buffer->skb) {
2439 frag = &buffer->frag_array[0];
2440 pci_unmap_single(pdev, frag->dma, frag->length,
2441 PCI_DMA_TODEVICE);
2442 frag->dma = 0ULL;
2443 for (i = 1; i < buffer->frag_count; i++) {
2444 frag++;
2445 pci_unmap_page(pdev, frag->dma, frag->length,
2446 PCI_DMA_TODEVICE);
2447 frag->dma = 0ULL;
2448 }
2449
2450 adapter->stats.xmitfinished++;
2451 dev_kfree_skb_any(buffer->skb);
2452 buffer->skb = NULL;
2453 }
2454
2455 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2456 if (++count >= MAX_STATUS_HANDLE)
2457 break;
2458 }
2459
2460 if (count && netif_running(netdev)) {
2461 tx_ring->sw_consumer = sw_consumer;
2462
2463 smp_mb();
2464
2465 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002466 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2467 netif_wake_queue(netdev);
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00002468 adapter->stats.xmit_on++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002469 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002470 }
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002471 adapter->tx_timeo_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002472 }
2473 /*
2474 * If everything is freed up to consumer then check if the ring is full
2475 * If the ring is full then check if more needs to be freed and
2476 * schedule the call back again.
2477 *
2478 * This happens when there are 2 CPUs. One could be freeing and the
2479 * other filling it. If the ring is full when we get out of here and
2480 * the card has already interrupted the host then the host can miss the
2481 * interrupt.
2482 *
2483 * There is still a possible race condition and the host could miss an
2484 * interrupt. The card has to take care of this.
2485 */
2486 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2487 done = (sw_consumer == hw_consumer);
2488 spin_unlock(&adapter->tx_clean_lock);
2489
2490 return done;
2491}
2492
2493static int qlcnic_poll(struct napi_struct *napi, int budget)
2494{
2495 struct qlcnic_host_sds_ring *sds_ring =
2496 container_of(napi, struct qlcnic_host_sds_ring, napi);
2497
2498 struct qlcnic_adapter *adapter = sds_ring->adapter;
2499
2500 int tx_complete;
2501 int work_done;
2502
2503 tx_complete = qlcnic_process_cmd_ring(adapter);
2504
2505 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2506
2507 if ((work_done < budget) && tx_complete) {
2508 napi_complete(&sds_ring->napi);
2509 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2510 qlcnic_enable_int(sds_ring);
2511 }
2512
2513 return work_done;
2514}
2515
schacko8f891382010-06-17 02:56:40 +00002516static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2517{
2518 struct qlcnic_host_sds_ring *sds_ring =
2519 container_of(napi, struct qlcnic_host_sds_ring, napi);
2520
2521 struct qlcnic_adapter *adapter = sds_ring->adapter;
2522 int work_done;
2523
2524 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2525
2526 if (work_done < budget) {
2527 napi_complete(&sds_ring->napi);
2528 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2529 qlcnic_enable_int(sds_ring);
2530 }
2531
2532 return work_done;
2533}
2534
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002535#ifdef CONFIG_NET_POLL_CONTROLLER
2536static void qlcnic_poll_controller(struct net_device *netdev)
2537{
Yinglin Luanbf827912010-08-22 21:57:56 +00002538 int ring;
2539 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002540 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00002541 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
Yinglin Luanbf827912010-08-22 21:57:56 +00002542
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002543 disable_irq(adapter->irq);
Yinglin Luanbf827912010-08-22 21:57:56 +00002544 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
2545 sds_ring = &recv_ctx->sds_rings[ring];
2546 qlcnic_intr(adapter->irq, sds_ring);
2547 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002548 enable_irq(adapter->irq);
2549}
2550#endif
2551
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002552static void
2553qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2554{
2555 u32 val;
2556
2557 val = adapter->portnum & 0xf;
2558 val |= encoding << 7;
2559 val |= (jiffies - adapter->dev_rst_time) << 8;
2560
2561 QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2562 adapter->dev_rst_time = jiffies;
2563}
2564
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002565static int
2566qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002567{
2568 u32 val;
2569
2570 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2571 state != QLCNIC_DEV_NEED_QUISCENT);
2572
2573 if (qlcnic_api_lock(adapter))
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002574 return -EIO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002575
2576 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2577
2578 if (state == QLCNIC_DEV_NEED_RESET)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002579 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002580 else if (state == QLCNIC_DEV_NEED_QUISCENT)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002581 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002582
2583 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2584
2585 qlcnic_api_unlock(adapter);
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002586
2587 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002588}
2589
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002590static int
2591qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2592{
2593 u32 val;
2594
2595 if (qlcnic_api_lock(adapter))
2596 return -EBUSY;
2597
2598 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002599 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002600 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2601
2602 qlcnic_api_unlock(adapter);
2603
2604 return 0;
2605}
2606
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002607static void
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002608qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002609{
2610 u32 val;
2611
2612 if (qlcnic_api_lock(adapter))
2613 goto err;
2614
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002615 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002616 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002617 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002618
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002619 if (failed) {
2620 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
2621 dev_info(&adapter->pdev->dev,
2622 "Device state set to Failed. Please Reboot\n");
2623 } else if (!(val & 0x11111111))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002624 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2625
2626 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002627 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002628 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2629
2630 qlcnic_api_unlock(adapter);
2631err:
2632 adapter->fw_fail_cnt = 0;
2633 clear_bit(__QLCNIC_START_FW, &adapter->state);
2634 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2635}
2636
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002637/* Grab api lock, before checking state */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002638static int
2639qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2640{
2641 int act, state;
2642
2643 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002644 act = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002645
2646 if (((state & 0x11111111) == (act & 0x11111111)) ||
2647 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2648 return 0;
2649 else
2650 return 1;
2651}
2652
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002653static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2654{
2655 u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2656
2657 if (val != QLCNIC_DRV_IDC_VER) {
2658 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2659 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2660 }
2661
2662 return 0;
2663}
2664
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002665static int
2666qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2667{
2668 u32 val, prev_state;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002669 u8 dev_init_timeo = adapter->dev_init_timeo;
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002670 u8 portnum = adapter->portnum;
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002671 u8 ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002672
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002673 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2674 return 1;
2675
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002676 if (qlcnic_api_lock(adapter))
2677 return -1;
2678
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002679 val = QLCRD32(adapter, QLCNIC_CRB_DRV_ACTIVE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002680 if (!(val & (1 << (portnum * 4)))) {
2681 QLC_DEV_SET_REF_CNT(val, portnum);
Amit Kumar Salecha31018e02010-08-25 04:03:05 +00002682 QLCWR32(adapter, QLCNIC_CRB_DRV_ACTIVE, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002683 }
2684
2685 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002686 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002687
2688 switch (prev_state) {
2689 case QLCNIC_DEV_COLD:
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002690 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002691 QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002692 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002693 qlcnic_api_unlock(adapter);
2694 return 1;
2695
2696 case QLCNIC_DEV_READY:
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002697 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002698 qlcnic_api_unlock(adapter);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002699 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002700
2701 case QLCNIC_DEV_NEED_RESET:
2702 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002703 QLC_DEV_SET_RST_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002704 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2705 break;
2706
2707 case QLCNIC_DEV_NEED_QUISCENT:
2708 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002709 QLC_DEV_SET_QSCNT_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002710 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2711 break;
2712
2713 case QLCNIC_DEV_FAILED:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00002714 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002715 qlcnic_api_unlock(adapter);
2716 return -1;
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002717
2718 case QLCNIC_DEV_INITIALIZING:
2719 case QLCNIC_DEV_QUISCENT:
2720 break;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002721 }
2722
2723 qlcnic_api_unlock(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002724
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002725 do {
2726 msleep(1000);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002727 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2728
2729 if (prev_state == QLCNIC_DEV_QUISCENT)
2730 continue;
2731 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002732
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002733 if (!dev_init_timeo) {
2734 dev_err(&adapter->pdev->dev,
2735 "Waiting for device to initialize timeout\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002736 return -1;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002737 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002738
2739 if (qlcnic_api_lock(adapter))
2740 return -1;
2741
2742 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002743 QLC_DEV_CLR_RST_QSCNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002744 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2745
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002746 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002747 qlcnic_api_unlock(adapter);
2748
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002749 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002750}
2751
2752static void
2753qlcnic_fwinit_work(struct work_struct *work)
2754{
2755 struct qlcnic_adapter *adapter = container_of(work,
2756 struct qlcnic_adapter, fw_work.work);
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002757 u32 dev_state = 0xf;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002758
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002759 if (qlcnic_api_lock(adapter))
2760 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002761
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002762 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002763 if (dev_state == QLCNIC_DEV_QUISCENT ||
2764 dev_state == QLCNIC_DEV_NEED_QUISCENT) {
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002765 qlcnic_api_unlock(adapter);
2766 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2767 FW_POLL_DELAY * 2);
2768 return;
2769 }
2770
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002771 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002772 qlcnic_api_unlock(adapter);
2773 goto wait_npar;
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002774 }
2775
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002776 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2777 dev_err(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2778 adapter->reset_ack_timeo);
2779 goto skip_ack_check;
2780 }
2781
2782 if (!qlcnic_check_drv_state(adapter)) {
2783skip_ack_check:
2784 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002785
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002786 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2787 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2788 QLCNIC_DEV_INITIALIZING);
2789 set_bit(__QLCNIC_START_FW, &adapter->state);
2790 QLCDB(adapter, DRV, "Restarting fw\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002791 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002792 }
2793
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002794 qlcnic_api_unlock(adapter);
2795
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002796 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002797 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002798 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002799 return;
2800 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002801 goto err_ret;
2802 }
2803
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002804 qlcnic_api_unlock(adapter);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002805
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002806wait_npar:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002807 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002808 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002809
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002810 switch (dev_state) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002811 case QLCNIC_DEV_READY:
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002812 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002813 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002814 adapter->fw_wait_cnt = 0;
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002815 return;
2816 }
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002817 case QLCNIC_DEV_FAILED:
2818 break;
2819 default:
2820 qlcnic_schedule_work(adapter,
2821 qlcnic_fwinit_work, FW_POLL_DELAY);
2822 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002823 }
2824
2825err_ret:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002826 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2827 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002828 netif_device_attach(adapter->netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002829 qlcnic_clr_all_drv_state(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002830}
2831
2832static void
2833qlcnic_detach_work(struct work_struct *work)
2834{
2835 struct qlcnic_adapter *adapter = container_of(work,
2836 struct qlcnic_adapter, fw_work.work);
2837 struct net_device *netdev = adapter->netdev;
2838 u32 status;
2839
2840 netif_device_detach(netdev);
2841
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002842 /* Dont grab rtnl lock during Quiscent mode */
2843 if (adapter->dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2844 if (netif_running(netdev))
2845 __qlcnic_down(adapter, netdev);
2846 } else
2847 qlcnic_down(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002848
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002849 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2850
2851 if (status & QLCNIC_RCODE_FATAL_ERROR)
2852 goto err_ret;
2853
2854 if (adapter->temp == QLCNIC_TEMP_PANIC)
2855 goto err_ret;
2856
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002857 if (qlcnic_set_drv_state(adapter, adapter->dev_state))
2858 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002859
2860 adapter->fw_wait_cnt = 0;
2861
2862 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2863
2864 return;
2865
2866err_ret:
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002867 dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n",
2868 status, adapter->temp);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002869 netif_device_attach(netdev);
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00002870 qlcnic_clr_all_drv_state(adapter, 1);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002871}
2872
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002873/*Transit NPAR state to NON Operational */
2874static void
2875qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
2876{
2877 u32 state;
2878
2879 state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2880 if (state == QLCNIC_DEV_NPAR_NON_OPER)
2881 return;
2882
2883 if (qlcnic_api_lock(adapter))
2884 return;
2885 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
2886 qlcnic_api_unlock(adapter);
2887}
2888
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002889/*Transit to RESET state from READY state only */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002890static void
2891qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
2892{
2893 u32 state;
2894
Anirban Chakrabortycea89752010-07-13 20:33:35 +00002895 adapter->need_fw_reset = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002896 if (qlcnic_api_lock(adapter))
2897 return;
2898
2899 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2900
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002901 if (state == QLCNIC_DEV_READY) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002902 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002903 QLCDB(adapter, DRV, "NEED_RESET state set\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002904 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002905 }
2906
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002907 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002908 qlcnic_api_unlock(adapter);
2909}
2910
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002911/* Transit to NPAR READY state from NPAR NOT READY state */
2912static void
2913qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
2914{
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002915 if (qlcnic_api_lock(adapter))
2916 return;
2917
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002918 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
2919 QLCDB(adapter, DRV, "NPAR operational state set\n");
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002920
2921 qlcnic_api_unlock(adapter);
2922}
2923
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002924static void
2925qlcnic_schedule_work(struct qlcnic_adapter *adapter,
2926 work_func_t func, int delay)
2927{
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00002928 if (test_bit(__QLCNIC_AER, &adapter->state))
2929 return;
2930
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002931 INIT_DELAYED_WORK(&adapter->fw_work, func);
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00002932 queue_delayed_work(qlcnic_wq, &adapter->fw_work,
2933 round_jiffies_relative(delay));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002934}
2935
2936static void
2937qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
2938{
2939 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2940 msleep(10);
2941
2942 cancel_delayed_work_sync(&adapter->fw_work);
2943}
2944
2945static void
2946qlcnic_attach_work(struct work_struct *work)
2947{
2948 struct qlcnic_adapter *adapter = container_of(work,
2949 struct qlcnic_adapter, fw_work.work);
2950 struct net_device *netdev = adapter->netdev;
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002951 u32 npar_state;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002952
Amit Kumar Salechab18971d2010-08-25 04:03:04 +00002953 if (adapter->op_mode != QLCNIC_MGMT_FUNC) {
2954 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2955 if (adapter->fw_wait_cnt++ > QLCNIC_DEV_NPAR_OPER_TIMEO)
2956 qlcnic_clr_all_drv_state(adapter, 0);
2957 else if (npar_state != QLCNIC_DEV_NPAR_OPER)
2958 qlcnic_schedule_work(adapter, qlcnic_attach_work,
2959 FW_POLL_DELAY);
2960 else
2961 goto attach;
2962 QLCDB(adapter, DRV, "Waiting for NPAR state to operational\n");
2963 return;
2964 }
2965attach:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002966 if (netif_running(netdev)) {
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00002967 if (qlcnic_up(adapter, netdev))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002968 goto done;
2969
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00002970 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002971 }
2972
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002973done:
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002974 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002975 adapter->fw_fail_cnt = 0;
2976 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002977
2978 if (!qlcnic_clr_drv_state(adapter))
2979 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2980 FW_POLL_DELAY);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002981}
2982
2983static int
2984qlcnic_check_health(struct qlcnic_adapter *adapter)
2985{
Sony Chacko4e708122010-08-31 17:17:44 +00002986 u32 state = 0, heartbeat;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002987 struct net_device *netdev = adapter->netdev;
2988
2989 if (qlcnic_check_temp(adapter))
2990 goto detach;
2991
Amit Kumar Salecha2372a5f2010-05-13 03:07:42 +00002992 if (adapter->need_fw_reset)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002993 qlcnic_dev_request_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002994
2995 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002996 if (state == QLCNIC_DEV_NEED_RESET) {
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00002997 qlcnic_set_npar_non_operational(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002998 adapter->need_fw_reset = 1;
Amit Kumar Salechab8c17622010-10-07 23:46:06 +00002999 } else if (state == QLCNIC_DEV_NEED_QUISCENT)
3000 goto detach;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003001
Sony Chacko4e708122010-08-31 17:17:44 +00003002 heartbeat = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
3003 if (heartbeat != adapter->heartbeat) {
3004 adapter->heartbeat = heartbeat;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003005 adapter->fw_fail_cnt = 0;
3006 if (adapter->need_fw_reset)
3007 goto detach;
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00003008
amit salecha9ce13ca2011-02-23 03:21:24 +00003009 if (adapter->reset_context && auto_fw_reset) {
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00003010 qlcnic_reset_hw_context(adapter);
3011 adapter->netdev->trans_start = jiffies;
3012 }
3013
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003014 return 0;
3015 }
3016
3017 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
3018 return 0;
3019
3020 qlcnic_dev_request_reset(adapter);
3021
amit salecha9ce13ca2011-02-23 03:21:24 +00003022 if (auto_fw_reset)
Amit Kumar Salecha0df170b2010-07-13 20:33:32 +00003023 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003024
3025 dev_info(&netdev->dev, "firmware hang detected\n");
3026
3027detach:
3028 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
3029 QLCNIC_DEV_NEED_RESET;
3030
amit salecha9ce13ca2011-02-23 03:21:24 +00003031 if (auto_fw_reset &&
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003032 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
3033
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003034 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00003035 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
3036 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003037
3038 return 1;
3039}
3040
3041static void
3042qlcnic_fw_poll_work(struct work_struct *work)
3043{
3044 struct qlcnic_adapter *adapter = container_of(work,
3045 struct qlcnic_adapter, fw_work.work);
3046
3047 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
3048 goto reschedule;
3049
3050
3051 if (qlcnic_check_health(adapter))
3052 return;
3053
Amit Kumar Salechab5e54922010-08-31 17:17:51 +00003054 if (adapter->fhash.fnum)
3055 qlcnic_prune_lb_filters(adapter);
3056
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003057reschedule:
3058 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
3059}
3060
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003061static int qlcnic_is_first_func(struct pci_dev *pdev)
3062{
3063 struct pci_dev *oth_pdev;
3064 int val = pdev->devfn;
3065
3066 while (val-- > 0) {
3067 oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
3068 (pdev->bus), pdev->bus->number,
3069 PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003070 if (!oth_pdev)
3071 continue;
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003072
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003073 if (oth_pdev->current_state != PCI_D3cold) {
3074 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003075 return 0;
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07003076 }
3077 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003078 }
3079 return 1;
3080}
3081
3082static int qlcnic_attach_func(struct pci_dev *pdev)
3083{
3084 int err, first_func;
3085 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3086 struct net_device *netdev = adapter->netdev;
3087
3088 pdev->error_state = pci_channel_io_normal;
3089
3090 err = pci_enable_device(pdev);
3091 if (err)
3092 return err;
3093
3094 pci_set_power_state(pdev, PCI_D0);
3095 pci_set_master(pdev);
3096 pci_restore_state(pdev);
3097
3098 first_func = qlcnic_is_first_func(pdev);
3099
3100 if (qlcnic_api_lock(adapter))
3101 return -EINVAL;
3102
Amit Kumar Salecha933fce122010-08-17 00:34:19 +00003103 if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003104 adapter->need_fw_reset = 1;
3105 set_bit(__QLCNIC_START_FW, &adapter->state);
3106 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
3107 QLCDB(adapter, DRV, "Restarting fw\n");
3108 }
3109 qlcnic_api_unlock(adapter);
3110
3111 err = adapter->nic_ops->start_firmware(adapter);
3112 if (err)
3113 return err;
3114
3115 qlcnic_clr_drv_state(adapter);
3116 qlcnic_setup_intr(adapter);
3117
3118 if (netif_running(netdev)) {
3119 err = qlcnic_attach(adapter);
3120 if (err) {
Amit Kumar Salecha21854f02010-08-19 05:08:29 +00003121 qlcnic_clr_all_drv_state(adapter, 1);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003122 clear_bit(__QLCNIC_AER, &adapter->state);
3123 netif_device_attach(netdev);
3124 return err;
3125 }
3126
3127 err = qlcnic_up(adapter, netdev);
3128 if (err)
3129 goto done;
3130
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00003131 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003132 }
3133 done:
3134 netif_device_attach(netdev);
3135 return err;
3136}
3137
3138static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
3139 pci_channel_state_t state)
3140{
3141 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3142 struct net_device *netdev = adapter->netdev;
3143
3144 if (state == pci_channel_io_perm_failure)
3145 return PCI_ERS_RESULT_DISCONNECT;
3146
3147 if (state == pci_channel_io_normal)
3148 return PCI_ERS_RESULT_RECOVERED;
3149
3150 set_bit(__QLCNIC_AER, &adapter->state);
3151 netif_device_detach(netdev);
3152
3153 cancel_delayed_work_sync(&adapter->fw_work);
3154
3155 if (netif_running(netdev))
3156 qlcnic_down(adapter, netdev);
3157
3158 qlcnic_detach(adapter);
3159 qlcnic_teardown_intr(adapter);
3160
3161 clear_bit(__QLCNIC_RESETTING, &adapter->state);
3162
3163 pci_save_state(pdev);
3164 pci_disable_device(pdev);
3165
3166 return PCI_ERS_RESULT_NEED_RESET;
3167}
3168
3169static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
3170{
3171 return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
3172 PCI_ERS_RESULT_RECOVERED;
3173}
3174
3175static void qlcnic_io_resume(struct pci_dev *pdev)
3176{
3177 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
3178
3179 pci_cleanup_aer_uncorrect_error_status(pdev);
3180
3181 if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
3182 test_and_clear_bit(__QLCNIC_AER, &adapter->state))
3183 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
3184 FW_POLL_DELAY);
3185}
3186
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003187static int
3188qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
3189{
3190 int err;
3191
3192 err = qlcnic_can_start_firmware(adapter);
3193 if (err)
3194 return err;
3195
Amit Kumar Salecha78f84e12010-08-19 05:08:28 +00003196 err = qlcnic_check_npar_opertional(adapter);
3197 if (err)
3198 return err;
Amit Kumar Salecha3c4b23b2010-08-17 00:34:20 +00003199
Rajesh Borundia174240a2010-08-31 17:17:47 +00003200 err = qlcnic_initialize_nic(adapter);
3201 if (err)
3202 return err;
3203
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003204 qlcnic_check_options(adapter);
3205
Rajesh Borundia73733732010-08-31 17:17:50 +00003206 err = qlcnic_set_eswitch_port_config(adapter);
3207 if (err)
3208 return err;
3209
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00003210 adapter->need_fw_reset = 0;
3211
3212 return err;
3213}
3214
3215static int
3216qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
3217{
3218 return -EOPNOTSUPP;
3219}
3220
3221static int
3222qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
3223{
3224 return -EOPNOTSUPP;
3225}
3226
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003227static ssize_t
3228qlcnic_store_bridged_mode(struct device *dev,
3229 struct device_attribute *attr, const char *buf, size_t len)
3230{
3231 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3232 unsigned long new;
3233 int ret = -EINVAL;
3234
3235 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
3236 goto err_out;
3237
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00003238 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003239 goto err_out;
3240
3241 if (strict_strtoul(buf, 2, &new))
3242 goto err_out;
3243
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00003244 if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003245 ret = len;
3246
3247err_out:
3248 return ret;
3249}
3250
3251static ssize_t
3252qlcnic_show_bridged_mode(struct device *dev,
3253 struct device_attribute *attr, char *buf)
3254{
3255 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3256 int bridged_mode = 0;
3257
3258 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3259 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
3260
3261 return sprintf(buf, "%d\n", bridged_mode);
3262}
3263
3264static struct device_attribute dev_attr_bridged_mode = {
3265 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
3266 .show = qlcnic_show_bridged_mode,
3267 .store = qlcnic_store_bridged_mode,
3268};
3269
3270static ssize_t
3271qlcnic_store_diag_mode(struct device *dev,
3272 struct device_attribute *attr, const char *buf, size_t len)
3273{
3274 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3275 unsigned long new;
3276
3277 if (strict_strtoul(buf, 2, &new))
3278 return -EINVAL;
3279
3280 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
3281 adapter->flags ^= QLCNIC_DIAG_ENABLED;
3282
3283 return len;
3284}
3285
3286static ssize_t
3287qlcnic_show_diag_mode(struct device *dev,
3288 struct device_attribute *attr, char *buf)
3289{
3290 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3291
3292 return sprintf(buf, "%d\n",
3293 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
3294}
3295
3296static struct device_attribute dev_attr_diag_mode = {
3297 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
3298 .show = qlcnic_show_diag_mode,
3299 .store = qlcnic_store_diag_mode,
3300};
3301
3302static int
3303qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
3304 loff_t offset, size_t size)
3305{
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003306 size_t crb_size = 4;
3307
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003308 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3309 return -EIO;
3310
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003311 if (offset < QLCNIC_PCI_CRBSPACE) {
3312 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
3313 QLCNIC_PCI_CAMQM_END))
3314 crb_size = 8;
3315 else
3316 return -EINVAL;
3317 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003318
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003319 if ((size != crb_size) || (offset & (crb_size-1)))
3320 return -EINVAL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003321
3322 return 0;
3323}
3324
3325static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003326qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
3327 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003328 char *buf, loff_t offset, size_t size)
3329{
3330 struct device *dev = container_of(kobj, struct device, kobj);
3331 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3332 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003333 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003334 int ret;
3335
3336 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3337 if (ret != 0)
3338 return ret;
3339
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003340 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3341 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
3342 memcpy(buf, &qmdata, size);
3343 } else {
3344 data = QLCRD32(adapter, offset);
3345 memcpy(buf, &data, size);
3346 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003347 return size;
3348}
3349
3350static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003351qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
3352 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003353 char *buf, loff_t offset, size_t size)
3354{
3355 struct device *dev = container_of(kobj, struct device, kobj);
3356 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3357 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003358 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003359 int ret;
3360
3361 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
3362 if (ret != 0)
3363 return ret;
3364
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00003365 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
3366 memcpy(&qmdata, buf, size);
3367 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
3368 } else {
3369 memcpy(&data, buf, size);
3370 QLCWR32(adapter, offset, data);
3371 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003372 return size;
3373}
3374
3375static int
3376qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
3377 loff_t offset, size_t size)
3378{
3379 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3380 return -EIO;
3381
3382 if ((size != 8) || (offset & 0x7))
3383 return -EIO;
3384
3385 return 0;
3386}
3387
3388static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003389qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3390 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003391 char *buf, loff_t offset, size_t size)
3392{
3393 struct device *dev = container_of(kobj, struct device, kobj);
3394 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3395 u64 data;
3396 int ret;
3397
3398 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3399 if (ret != 0)
3400 return ret;
3401
3402 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3403 return -EIO;
3404
3405 memcpy(buf, &data, size);
3406
3407 return size;
3408}
3409
3410static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003411qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3412 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003413 char *buf, loff_t offset, size_t size)
3414{
3415 struct device *dev = container_of(kobj, struct device, kobj);
3416 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3417 u64 data;
3418 int ret;
3419
3420 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3421 if (ret != 0)
3422 return ret;
3423
3424 memcpy(&data, buf, size);
3425
3426 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3427 return -EIO;
3428
3429 return size;
3430}
3431
3432
3433static struct bin_attribute bin_attr_crb = {
3434 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3435 .size = 0,
3436 .read = qlcnic_sysfs_read_crb,
3437 .write = qlcnic_sysfs_write_crb,
3438};
3439
3440static struct bin_attribute bin_attr_mem = {
3441 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3442 .size = 0,
3443 .read = qlcnic_sysfs_read_mem,
3444 .write = qlcnic_sysfs_write_mem,
3445};
3446
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003447static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003448validate_pm_config(struct qlcnic_adapter *adapter,
3449 struct qlcnic_pm_func_cfg *pm_cfg, int count)
3450{
3451
3452 u8 src_pci_func, s_esw_id, d_esw_id;
3453 u8 dest_pci_func;
3454 int i;
3455
3456 for (i = 0; i < count; i++) {
3457 src_pci_func = pm_cfg[i].pci_func;
3458 dest_pci_func = pm_cfg[i].dest_npar;
3459 if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3460 || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3461 return QL_STATUS_INVALID_PARAM;
3462
3463 if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3464 return QL_STATUS_INVALID_PARAM;
3465
3466 if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3467 return QL_STATUS_INVALID_PARAM;
3468
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003469 s_esw_id = adapter->npars[src_pci_func].phy_port;
3470 d_esw_id = adapter->npars[dest_pci_func].phy_port;
3471
3472 if (s_esw_id != d_esw_id)
3473 return QL_STATUS_INVALID_PARAM;
3474
3475 }
3476 return 0;
3477
3478}
3479
3480static ssize_t
3481qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3482 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3483{
3484 struct device *dev = container_of(kobj, struct device, kobj);
3485 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3486 struct qlcnic_pm_func_cfg *pm_cfg;
3487 u32 id, action, pci_func;
3488 int count, rem, i, ret;
3489
3490 count = size / sizeof(struct qlcnic_pm_func_cfg);
3491 rem = size % sizeof(struct qlcnic_pm_func_cfg);
3492 if (rem)
3493 return QL_STATUS_INVALID_PARAM;
3494
3495 pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3496
3497 ret = validate_pm_config(adapter, pm_cfg, count);
3498 if (ret)
3499 return ret;
3500 for (i = 0; i < count; i++) {
3501 pci_func = pm_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003502 action = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003503 id = adapter->npars[pci_func].phy_port;
3504 ret = qlcnic_config_port_mirroring(adapter, id,
3505 action, pci_func);
3506 if (ret)
3507 return ret;
3508 }
3509
3510 for (i = 0; i < count; i++) {
3511 pci_func = pm_cfg[i].pci_func;
3512 id = adapter->npars[pci_func].phy_port;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003513 adapter->npars[pci_func].enable_pm = !!pm_cfg[i].action;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003514 adapter->npars[pci_func].dest_npar = id;
3515 }
3516 return size;
3517}
3518
3519static ssize_t
3520qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3521 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3522{
3523 struct device *dev = container_of(kobj, struct device, kobj);
3524 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3525 struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3526 int i;
3527
3528 if (size != sizeof(pm_cfg))
3529 return QL_STATUS_INVALID_PARAM;
3530
3531 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3532 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3533 continue;
3534 pm_cfg[i].action = adapter->npars[i].enable_pm;
3535 pm_cfg[i].dest_npar = 0;
3536 pm_cfg[i].pci_func = i;
3537 }
3538 memcpy(buf, &pm_cfg, size);
3539
3540 return size;
3541}
3542
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003543static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003544validate_esw_config(struct qlcnic_adapter *adapter,
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003545 struct qlcnic_esw_func_cfg *esw_cfg, int count)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003546{
Rajesh Borundia7613c872010-08-31 17:17:48 +00003547 u32 op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003548 u8 pci_func;
3549 int i;
Rajesh Borundia7613c872010-08-31 17:17:48 +00003550
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003551 op_mode = readl(adapter->ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
Rajesh Borundia7613c872010-08-31 17:17:48 +00003552
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003553 for (i = 0; i < count; i++) {
3554 pci_func = esw_cfg[i].pci_func;
3555 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3556 return QL_STATUS_INVALID_PARAM;
3557
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003558 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3559 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003560 return QL_STATUS_INVALID_PARAM;
3561
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003562 switch (esw_cfg[i].op_mode) {
3563 case QLCNIC_PORT_DEFAULTS:
Rajesh Borundia7613c872010-08-31 17:17:48 +00003564 if (QLC_DEV_GET_DRV(op_mode, pci_func) !=
Rajesh Borundia73733732010-08-31 17:17:50 +00003565 QLCNIC_NON_PRIV_FUNC) {
Amit Kumar Salecha091056b2010-12-02 20:41:43 +00003566 if (esw_cfg[i].mac_anti_spoof != 0)
3567 return QL_STATUS_INVALID_PARAM;
3568 if (esw_cfg[i].mac_override != 1)
3569 return QL_STATUS_INVALID_PARAM;
3570 if (esw_cfg[i].promisc_mode != 1)
3571 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia73733732010-08-31 17:17:50 +00003572 }
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003573 break;
3574 case QLCNIC_ADD_VLAN:
3575 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3576 return QL_STATUS_INVALID_PARAM;
3577 if (!esw_cfg[i].op_type)
3578 return QL_STATUS_INVALID_PARAM;
3579 break;
3580 case QLCNIC_DEL_VLAN:
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003581 if (!esw_cfg[i].op_type)
3582 return QL_STATUS_INVALID_PARAM;
3583 break;
3584 default:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003585 return QL_STATUS_INVALID_PARAM;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003586 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003587 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003588 return 0;
3589}
3590
3591static ssize_t
3592qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3593 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3594{
3595 struct device *dev = container_of(kobj, struct device, kobj);
3596 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3597 struct qlcnic_esw_func_cfg *esw_cfg;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003598 struct qlcnic_npar_info *npar;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003599 int count, rem, i, ret;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003600 u8 pci_func, op_mode = 0;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003601
3602 count = size / sizeof(struct qlcnic_esw_func_cfg);
3603 rem = size % sizeof(struct qlcnic_esw_func_cfg);
3604 if (rem)
3605 return QL_STATUS_INVALID_PARAM;
3606
3607 esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3608 ret = validate_esw_config(adapter, esw_cfg, count);
3609 if (ret)
3610 return ret;
3611
3612 for (i = 0; i < count; i++) {
Rajesh Borundia0325d692010-08-19 05:08:26 +00003613 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
3614 if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
3615 return QL_STATUS_INVALID_PARAM;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003616
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003617 if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003618 continue;
3619
3620 op_mode = esw_cfg[i].op_mode;
3621 qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
3622 esw_cfg[i].op_mode = op_mode;
Anirban Chakrabortyb1fc6d32011-04-01 14:28:05 +00003623 esw_cfg[i].pci_func = adapter->ahw->pci_func;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003624
3625 switch (esw_cfg[i].op_mode) {
3626 case QLCNIC_PORT_DEFAULTS:
3627 qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
3628 break;
Amit Kumar Salecha8cf61f82010-08-25 04:03:03 +00003629 case QLCNIC_ADD_VLAN:
3630 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3631 break;
3632 case QLCNIC_DEL_VLAN:
3633 esw_cfg[i].vlan_id = 0;
3634 qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
3635 break;
Rajesh Borundia0325d692010-08-19 05:08:26 +00003636 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003637 }
3638
Rajesh Borundia0325d692010-08-19 05:08:26 +00003639 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
3640 goto out;
Rajesh Borundiae9a47702010-08-25 04:03:02 +00003641
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003642 for (i = 0; i < count; i++) {
3643 pci_func = esw_cfg[i].pci_func;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003644 npar = &adapter->npars[pci_func];
3645 switch (esw_cfg[i].op_mode) {
3646 case QLCNIC_PORT_DEFAULTS:
3647 npar->promisc_mode = esw_cfg[i].promisc_mode;
Rajesh Borundia73733732010-08-31 17:17:50 +00003648 npar->mac_override = esw_cfg[i].mac_override;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003649 npar->offload_flags = esw_cfg[i].offload_flags;
3650 npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
3651 npar->discard_tagged = esw_cfg[i].discard_tagged;
3652 break;
3653 case QLCNIC_ADD_VLAN:
3654 npar->pvid = esw_cfg[i].vlan_id;
3655 break;
3656 case QLCNIC_DEL_VLAN:
3657 npar->pvid = 0;
3658 break;
3659 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003660 }
Rajesh Borundia0325d692010-08-19 05:08:26 +00003661out:
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003662 return size;
3663}
3664
3665static ssize_t
3666qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
3667 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3668{
3669 struct device *dev = container_of(kobj, struct device, kobj);
3670 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3671 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003672 u8 i;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003673
3674 if (size != sizeof(esw_cfg))
3675 return QL_STATUS_INVALID_PARAM;
3676
3677 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3678 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3679 continue;
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00003680 esw_cfg[i].pci_func = i;
3681 if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]))
3682 return QL_STATUS_INVALID_PARAM;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003683 }
3684 memcpy(buf, &esw_cfg, size);
3685
3686 return size;
3687}
3688
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003689static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003690validate_npar_config(struct qlcnic_adapter *adapter,
3691 struct qlcnic_npar_func_cfg *np_cfg, int count)
3692{
3693 u8 pci_func, i;
3694
3695 for (i = 0; i < count; i++) {
3696 pci_func = np_cfg[i].pci_func;
3697 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3698 return QL_STATUS_INVALID_PARAM;
3699
3700 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3701 return QL_STATUS_INVALID_PARAM;
3702
Rajesh Borundiad12b0d92011-02-23 03:21:25 +00003703 if (!IS_VALID_BW(np_cfg[i].min_bw) ||
3704 !IS_VALID_BW(np_cfg[i].max_bw))
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003705 return QL_STATUS_INVALID_PARAM;
3706 }
3707 return 0;
3708}
3709
3710static ssize_t
3711qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
3712 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3713{
3714 struct device *dev = container_of(kobj, struct device, kobj);
3715 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3716 struct qlcnic_info nic_info;
3717 struct qlcnic_npar_func_cfg *np_cfg;
3718 int i, count, rem, ret;
3719 u8 pci_func;
3720
3721 count = size / sizeof(struct qlcnic_npar_func_cfg);
3722 rem = size % sizeof(struct qlcnic_npar_func_cfg);
3723 if (rem)
3724 return QL_STATUS_INVALID_PARAM;
3725
3726 np_cfg = (struct qlcnic_npar_func_cfg *) buf;
3727 ret = validate_npar_config(adapter, np_cfg, count);
3728 if (ret)
3729 return ret;
3730
3731 for (i = 0; i < count ; i++) {
3732 pci_func = np_cfg[i].pci_func;
3733 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
3734 if (ret)
3735 return ret;
3736 nic_info.pci_func = pci_func;
3737 nic_info.min_tx_bw = np_cfg[i].min_bw;
3738 nic_info.max_tx_bw = np_cfg[i].max_bw;
3739 ret = qlcnic_set_nic_info(adapter, &nic_info);
3740 if (ret)
3741 return ret;
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003742 adapter->npars[i].min_bw = nic_info.min_tx_bw;
3743 adapter->npars[i].max_bw = nic_info.max_tx_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003744 }
3745
3746 return size;
3747
3748}
3749static ssize_t
3750qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
3751 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3752{
3753 struct device *dev = container_of(kobj, struct device, kobj);
3754 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3755 struct qlcnic_info nic_info;
3756 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
3757 int i, ret;
3758
3759 if (size != sizeof(np_cfg))
3760 return QL_STATUS_INVALID_PARAM;
3761
3762 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3763 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3764 continue;
3765 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
3766 if (ret)
3767 return ret;
3768
3769 np_cfg[i].pci_func = i;
Sucheta Chakrabortya1c0c452010-10-04 04:20:14 +00003770 np_cfg[i].op_mode = (u8)nic_info.op_mode;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003771 np_cfg[i].port_num = nic_info.phys_port;
3772 np_cfg[i].fw_capab = nic_info.capabilities;
3773 np_cfg[i].min_bw = nic_info.min_tx_bw ;
3774 np_cfg[i].max_bw = nic_info.max_tx_bw;
3775 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
3776 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
3777 }
3778 memcpy(buf, &np_cfg, size);
3779 return size;
3780}
3781
3782static ssize_t
Amit Kumar Salechab6021212010-08-17 00:34:22 +00003783qlcnic_sysfs_get_port_stats(struct file *file, struct kobject *kobj,
3784 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3785{
3786 struct device *dev = container_of(kobj, struct device, kobj);
3787 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3788 struct qlcnic_esw_statistics port_stats;
3789 int ret;
3790
3791 if (size != sizeof(struct qlcnic_esw_statistics))
3792 return QL_STATUS_INVALID_PARAM;
3793
3794 if (offset >= QLCNIC_MAX_PCI_FUNC)
3795 return QL_STATUS_INVALID_PARAM;
3796
3797 memset(&port_stats, 0, size);
3798 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
3799 &port_stats.rx);
3800 if (ret)
3801 return ret;
3802
3803 ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
3804 &port_stats.tx);
3805 if (ret)
3806 return ret;
3807
3808 memcpy(buf, &port_stats, size);
3809 return size;
3810}
3811
3812static ssize_t
3813qlcnic_sysfs_get_esw_stats(struct file *file, struct kobject *kobj,
3814 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3815{
3816 struct device *dev = container_of(kobj, struct device, kobj);
3817 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3818 struct qlcnic_esw_statistics esw_stats;
3819 int ret;
3820
3821 if (size != sizeof(struct qlcnic_esw_statistics))
3822 return QL_STATUS_INVALID_PARAM;
3823
3824 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
3825 return QL_STATUS_INVALID_PARAM;
3826
3827 memset(&esw_stats, 0, size);
3828 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
3829 &esw_stats.rx);
3830 if (ret)
3831 return ret;
3832
3833 ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
3834 &esw_stats.tx);
3835 if (ret)
3836 return ret;
3837
3838 memcpy(buf, &esw_stats, size);
3839 return size;
3840}
3841
3842static ssize_t
3843qlcnic_sysfs_clear_esw_stats(struct file *file, struct kobject *kobj,
3844 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3845{
3846 struct device *dev = container_of(kobj, struct device, kobj);
3847 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3848 int ret;
3849
3850 if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
3851 return QL_STATUS_INVALID_PARAM;
3852
3853 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
3854 QLCNIC_QUERY_RX_COUNTER);
3855 if (ret)
3856 return ret;
3857
3858 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
3859 QLCNIC_QUERY_TX_COUNTER);
3860 if (ret)
3861 return ret;
3862
3863 return size;
3864}
3865
3866static ssize_t
3867qlcnic_sysfs_clear_port_stats(struct file *file, struct kobject *kobj,
3868 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3869{
3870
3871 struct device *dev = container_of(kobj, struct device, kobj);
3872 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3873 int ret;
3874
3875 if (offset >= QLCNIC_MAX_PCI_FUNC)
3876 return QL_STATUS_INVALID_PARAM;
3877
3878 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
3879 QLCNIC_QUERY_RX_COUNTER);
3880 if (ret)
3881 return ret;
3882
3883 ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
3884 QLCNIC_QUERY_TX_COUNTER);
3885 if (ret)
3886 return ret;
3887
3888 return size;
3889}
3890
3891static ssize_t
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003892qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
3893 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3894{
3895 struct device *dev = container_of(kobj, struct device, kobj);
3896 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3897 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
Dan Carpentere88db3b2010-08-09 21:49:36 +00003898 struct qlcnic_pci_info *pci_info;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003899 int i, ret;
3900
3901 if (size != sizeof(pci_cfg))
3902 return QL_STATUS_INVALID_PARAM;
3903
Dan Carpentere88db3b2010-08-09 21:49:36 +00003904 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
3905 if (!pci_info)
3906 return -ENOMEM;
3907
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003908 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpentere88db3b2010-08-09 21:49:36 +00003909 if (ret) {
3910 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003911 return ret;
Dan Carpentere88db3b2010-08-09 21:49:36 +00003912 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003913
3914 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3915 pci_cfg[i].pci_func = pci_info[i].id;
3916 pci_cfg[i].func_type = pci_info[i].type;
3917 pci_cfg[i].port_num = pci_info[i].default_port;
3918 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
3919 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
3920 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
3921 }
3922 memcpy(buf, &pci_cfg, size);
Dan Carpentere88db3b2010-08-09 21:49:36 +00003923 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003924 return size;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003925}
3926static struct bin_attribute bin_attr_npar_config = {
3927 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
3928 .size = 0,
3929 .read = qlcnic_sysfs_read_npar_config,
3930 .write = qlcnic_sysfs_write_npar_config,
3931};
3932
3933static struct bin_attribute bin_attr_pci_config = {
3934 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
3935 .size = 0,
3936 .read = qlcnic_sysfs_read_pci_config,
3937 .write = NULL,
3938};
3939
Amit Kumar Salechab6021212010-08-17 00:34:22 +00003940static struct bin_attribute bin_attr_port_stats = {
3941 .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
3942 .size = 0,
3943 .read = qlcnic_sysfs_get_port_stats,
3944 .write = qlcnic_sysfs_clear_port_stats,
3945};
3946
3947static struct bin_attribute bin_attr_esw_stats = {
3948 .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
3949 .size = 0,
3950 .read = qlcnic_sysfs_get_esw_stats,
3951 .write = qlcnic_sysfs_clear_esw_stats,
3952};
3953
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003954static struct bin_attribute bin_attr_esw_config = {
3955 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
3956 .size = 0,
3957 .read = qlcnic_sysfs_read_esw_config,
3958 .write = qlcnic_sysfs_write_esw_config,
3959};
3960
3961static struct bin_attribute bin_attr_pm_config = {
3962 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
3963 .size = 0,
3964 .read = qlcnic_sysfs_read_pm_config,
3965 .write = qlcnic_sysfs_write_pm_config,
3966};
3967
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003968static void
3969qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
3970{
3971 struct device *dev = &adapter->pdev->dev;
3972
3973 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3974 if (device_create_file(dev, &dev_attr_bridged_mode))
3975 dev_warn(dev,
3976 "failed to create bridged_mode sysfs entry\n");
3977}
3978
3979static void
3980qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
3981{
3982 struct device *dev = &adapter->pdev->dev;
3983
3984 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3985 device_remove_file(dev, &dev_attr_bridged_mode);
3986}
3987
3988static void
3989qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
3990{
3991 struct device *dev = &adapter->pdev->dev;
3992
Amit Kumar Salechab6021212010-08-17 00:34:22 +00003993 if (device_create_bin_file(dev, &bin_attr_port_stats))
3994 dev_info(dev, "failed to create port stats sysfs entry");
3995
Anirban Chakraborty132ff002010-07-09 13:15:05 +00003996 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
3997 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003998 if (device_create_file(dev, &dev_attr_diag_mode))
3999 dev_info(dev, "failed to create diag_mode sysfs entry\n");
4000 if (device_create_bin_file(dev, &bin_attr_crb))
4001 dev_info(dev, "failed to create crb sysfs entry\n");
4002 if (device_create_bin_file(dev, &bin_attr_mem))
4003 dev_info(dev, "failed to create mem sysfs entry\n");
Sony Chacko53478fe2011-04-01 14:27:47 +00004004 if (device_create_bin_file(dev, &bin_attr_pci_config))
4005 dev_info(dev, "failed to create pci config sysfs entry");
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004006 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4007 return;
4008 if (device_create_bin_file(dev, &bin_attr_esw_config))
4009 dev_info(dev, "failed to create esw config sysfs entry");
4010 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004011 return;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004012 if (device_create_bin_file(dev, &bin_attr_npar_config))
4013 dev_info(dev, "failed to create npar config sysfs entry");
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004014 if (device_create_bin_file(dev, &bin_attr_pm_config))
4015 dev_info(dev, "failed to create pm config sysfs entry");
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004016 if (device_create_bin_file(dev, &bin_attr_esw_stats))
4017 dev_info(dev, "failed to create eswitch stats sysfs entry");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004018}
4019
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004020static void
4021qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
4022{
4023 struct device *dev = &adapter->pdev->dev;
4024
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004025 device_remove_bin_file(dev, &bin_attr_port_stats);
4026
Anirban Chakraborty132ff002010-07-09 13:15:05 +00004027 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
4028 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004029 device_remove_file(dev, &dev_attr_diag_mode);
4030 device_remove_bin_file(dev, &bin_attr_crb);
4031 device_remove_bin_file(dev, &bin_attr_mem);
Sony Chacko53478fe2011-04-01 14:27:47 +00004032 device_remove_bin_file(dev, &bin_attr_pci_config);
Rajesh Borundia4e8acb02010-08-19 05:08:25 +00004033 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
4034 return;
4035 device_remove_bin_file(dev, &bin_attr_esw_config);
4036 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004037 return;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004038 device_remove_bin_file(dev, &bin_attr_npar_config);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00004039 device_remove_bin_file(dev, &bin_attr_pm_config);
Amit Kumar Salechab6021212010-08-17 00:34:22 +00004040 device_remove_bin_file(dev, &bin_attr_esw_stats);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004041}
4042
4043#ifdef CONFIG_INET
4044
4045#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
4046
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004047static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004048qlcnic_config_indev_addr(struct qlcnic_adapter *adapter,
4049 struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004050{
4051 struct in_device *indev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004052
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004053 indev = in_dev_get(dev);
4054 if (!indev)
4055 return;
4056
4057 for_ifa(indev) {
4058 switch (event) {
4059 case NETDEV_UP:
4060 qlcnic_config_ipaddr(adapter,
4061 ifa->ifa_address, QLCNIC_IP_UP);
4062 break;
4063 case NETDEV_DOWN:
4064 qlcnic_config_ipaddr(adapter,
4065 ifa->ifa_address, QLCNIC_IP_DOWN);
4066 break;
4067 default:
4068 break;
4069 }
4070 } endfor_ifa(indev);
4071
4072 in_dev_put(indev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004073}
4074
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004075static void
4076qlcnic_restore_indev_addr(struct net_device *netdev, unsigned long event)
4077{
4078 struct qlcnic_adapter *adapter = netdev_priv(netdev);
4079 struct net_device *dev;
4080 u16 vid;
4081
4082 qlcnic_config_indev_addr(adapter, netdev, event);
4083
Anirban Chakrabortyb9796a12011-04-01 14:28:15 +00004084 for_each_set_bit(vid, adapter->vlans, VLAN_N_VID) {
4085 dev = vlan_find_dev(netdev, vid);
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004086 if (!dev)
4087 continue;
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004088 qlcnic_config_indev_addr(adapter, dev, event);
4089 }
4090}
4091
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004092static int qlcnic_netdev_event(struct notifier_block *this,
4093 unsigned long event, void *ptr)
4094{
4095 struct qlcnic_adapter *adapter;
4096 struct net_device *dev = (struct net_device *)ptr;
4097
4098recheck:
4099 if (dev == NULL)
4100 goto done;
4101
4102 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4103 dev = vlan_dev_real_dev(dev);
4104 goto recheck;
4105 }
4106
4107 if (!is_qlcnic_netdev(dev))
4108 goto done;
4109
4110 adapter = netdev_priv(dev);
4111
4112 if (!adapter)
4113 goto done;
4114
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004115 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004116 goto done;
4117
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004118 qlcnic_config_indev_addr(adapter, dev, event);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004119done:
4120 return NOTIFY_DONE;
4121}
4122
4123static int
4124qlcnic_inetaddr_event(struct notifier_block *this,
4125 unsigned long event, void *ptr)
4126{
4127 struct qlcnic_adapter *adapter;
4128 struct net_device *dev;
4129
4130 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
4131
4132 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
4133
4134recheck:
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004135 if (dev == NULL)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004136 goto done;
4137
4138 if (dev->priv_flags & IFF_802_1Q_VLAN) {
4139 dev = vlan_dev_real_dev(dev);
4140 goto recheck;
4141 }
4142
4143 if (!is_qlcnic_netdev(dev))
4144 goto done;
4145
4146 adapter = netdev_priv(dev);
4147
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +00004148 if (!adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004149 goto done;
4150
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00004151 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004152 goto done;
4153
4154 switch (event) {
4155 case NETDEV_UP:
4156 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
4157 break;
4158 case NETDEV_DOWN:
4159 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
4160 break;
4161 default:
4162 break;
4163 }
4164
4165done:
4166 return NOTIFY_DONE;
4167}
4168
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004169static struct notifier_block qlcnic_netdev_cb = {
4170 .notifier_call = qlcnic_netdev_event,
4171};
4172
4173static struct notifier_block qlcnic_inetaddr_cb = {
4174 .notifier_call = qlcnic_inetaddr_event,
4175};
4176#else
4177static void
Amit Kumar Salechaaec1e842010-09-16 19:14:41 +00004178qlcnic_restore_indev_addr(struct net_device *dev, unsigned long event)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004179{ }
4180#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004181static struct pci_error_handlers qlcnic_err_handler = {
4182 .error_detected = qlcnic_io_error_detected,
4183 .slot_reset = qlcnic_io_slot_reset,
4184 .resume = qlcnic_io_resume,
4185};
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004186
4187static struct pci_driver qlcnic_driver = {
4188 .name = qlcnic_driver_name,
4189 .id_table = qlcnic_pci_tbl,
4190 .probe = qlcnic_probe,
4191 .remove = __devexit_p(qlcnic_remove),
4192#ifdef CONFIG_PM
4193 .suspend = qlcnic_suspend,
4194 .resume = qlcnic_resume,
4195#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00004196 .shutdown = qlcnic_shutdown,
4197 .err_handler = &qlcnic_err_handler
4198
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004199};
4200
4201static int __init qlcnic_init_module(void)
4202{
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004203 int ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004204
4205 printk(KERN_INFO "%s\n", qlcnic_driver_string);
4206
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004207 qlcnic_wq = create_singlethread_workqueue("qlcnic");
4208 if (qlcnic_wq == NULL) {
4209 printk(KERN_ERR "qlcnic: cannot create workqueue\n");
4210 return -ENOMEM;
4211 }
4212
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004213#ifdef CONFIG_INET
4214 register_netdevice_notifier(&qlcnic_netdev_cb);
4215 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
4216#endif
4217
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004218 ret = pci_register_driver(&qlcnic_driver);
4219 if (ret) {
4220#ifdef CONFIG_INET
4221 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4222 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4223#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004224 destroy_workqueue(qlcnic_wq);
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004225 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004226
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00004227 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004228}
4229
4230module_init(qlcnic_init_module);
4231
4232static void __exit qlcnic_exit_module(void)
4233{
4234
4235 pci_unregister_driver(&qlcnic_driver);
4236
4237#ifdef CONFIG_INET
4238 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
4239 unregister_netdevice_notifier(&qlcnic_netdev_cb);
4240#endif
Amit Kumar Salechaf7ec8042010-10-07 23:46:05 +00004241 destroy_workqueue(qlcnic_wq);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00004242}
4243
4244module_exit(qlcnic_exit_module);