blob: bf6d87adda4fff7a5aa1d1e20728383d08e621f1 [file] [log] [blame]
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001/*
2 * Copyright (C) 2009 - QLogic Corporation.
3 * All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 * MA 02111-1307, USA.
19 *
20 * The full GNU General Public License is included in this distribution
21 * in the file called "COPYING".
22 *
23 */
24
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000026#include <linux/vmalloc.h>
27#include <linux/interrupt.h>
28
29#include "qlcnic.h"
30
31#include <linux/dma-mapping.h>
32#include <linux/if_vlan.h>
33#include <net/ip.h>
34#include <linux/ipv6.h>
35#include <linux/inetdevice.h>
36#include <linux/sysfs.h>
Sucheta Chakraborty451724c2010-07-13 20:33:34 +000037#include <linux/aer.h>
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000038
Sritej Velaga7f9a0c32010-06-17 02:56:39 +000039MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000040MODULE_LICENSE("GPL");
41MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
42MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
43
44char qlcnic_driver_name[] = "qlcnic";
Sritej Velaga7f9a0c32010-06-17 02:56:39 +000045static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
46 "Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000047
48static int port_mode = QLCNIC_PORT_MODE_AUTO_NEG;
49
50/* Default to restricted 1G auto-neg mode */
51static int wol_port_mode = 5;
52
53static int use_msi = 1;
54module_param(use_msi, int, 0644);
55MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
56
57static int use_msi_x = 1;
58module_param(use_msi_x, int, 0644);
59MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
60
61static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
62module_param(auto_fw_reset, int, 0644);
63MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
64
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +000065static int load_fw_file;
66module_param(load_fw_file, int, 0644);
67MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file");
68
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +000069static int qlcnic_config_npars;
70module_param(qlcnic_config_npars, int, 0644);
71MODULE_PARM_DESC(qlcnic_config_npars, "Configure NPARs (0=disabled, 1=enabled");
72
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000073static int __devinit qlcnic_probe(struct pci_dev *pdev,
74 const struct pci_device_id *ent);
75static void __devexit qlcnic_remove(struct pci_dev *pdev);
76static int qlcnic_open(struct net_device *netdev);
77static int qlcnic_close(struct net_device *netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000078static void qlcnic_tx_timeout(struct net_device *netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000079static void qlcnic_attach_work(struct work_struct *work);
80static void qlcnic_fwinit_work(struct work_struct *work);
81static void qlcnic_fw_poll_work(struct work_struct *work);
82static void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
83 work_func_t func, int delay);
84static void qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter);
85static int qlcnic_poll(struct napi_struct *napi, int budget);
schacko8f891382010-06-17 02:56:40 +000086static int qlcnic_rx_poll(struct napi_struct *napi, int budget);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000087#ifdef CONFIG_NET_POLL_CONTROLLER
88static void qlcnic_poll_controller(struct net_device *netdev);
89#endif
90
91static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter);
92static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter);
93static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter);
94static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter);
95
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +000096static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000097static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter);
98static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
99
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000100static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000101static irqreturn_t qlcnic_intr(int irq, void *data);
102static irqreturn_t qlcnic_msi_intr(int irq, void *data);
103static irqreturn_t qlcnic_msix_intr(int irq, void *data);
104
105static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
106static void qlcnic_config_indev_addr(struct net_device *dev, unsigned long);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000107static int qlcnic_start_firmware(struct qlcnic_adapter *);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000108
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000109static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000110static int qlcnicvf_config_led(struct qlcnic_adapter *, u32, u32);
111static int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *, u32);
112static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000113/* PCI Device ID Table */
114#define ENTRY(device) \
115 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
116 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
117
118#define PCI_DEVICE_ID_QLOGIC_QLE824X 0x8020
119
Amit Kumar Salecha6a902882010-02-01 05:24:54 +0000120static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000121 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
122 {0,}
123};
124
125MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
126
127
128void
129qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
130 struct qlcnic_host_tx_ring *tx_ring)
131{
132 writel(tx_ring->producer, tx_ring->crb_cmd_producer);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000133}
134
135static const u32 msi_tgt_status[8] = {
136 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
137 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
138 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
139 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
140};
141
142static const
143struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
144
145static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
146{
147 writel(0, sds_ring->crb_intr_mask);
148}
149
150static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
151{
152 struct qlcnic_adapter *adapter = sds_ring->adapter;
153
154 writel(0x1, sds_ring->crb_intr_mask);
155
156 if (!QLCNIC_IS_MSI_FAMILY(adapter))
157 writel(0xfbff, adapter->tgt_mask_reg);
158}
159
160static int
161qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
162{
163 int size = sizeof(struct qlcnic_host_sds_ring) * count;
164
165 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
166
167 return (recv_ctx->sds_rings == NULL);
168}
169
170static void
171qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
172{
173 if (recv_ctx->sds_rings != NULL)
174 kfree(recv_ctx->sds_rings);
175
176 recv_ctx->sds_rings = NULL;
177}
178
179static int
180qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
181{
182 int ring;
183 struct qlcnic_host_sds_ring *sds_ring;
184 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
185
186 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
187 return -ENOMEM;
188
189 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
190 sds_ring = &recv_ctx->sds_rings[ring];
schacko8f891382010-06-17 02:56:40 +0000191
192 if (ring == adapter->max_sds_rings - 1)
193 netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
194 QLCNIC_NETDEV_WEIGHT/adapter->max_sds_rings);
195 else
196 netif_napi_add(netdev, &sds_ring->napi,
197 qlcnic_rx_poll, QLCNIC_NETDEV_WEIGHT*2);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000198 }
199
200 return 0;
201}
202
203static void
204qlcnic_napi_del(struct qlcnic_adapter *adapter)
205{
206 int ring;
207 struct qlcnic_host_sds_ring *sds_ring;
208 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
209
210 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
211 sds_ring = &recv_ctx->sds_rings[ring];
212 netif_napi_del(&sds_ring->napi);
213 }
214
215 qlcnic_free_sds_rings(&adapter->recv_ctx);
216}
217
218static void
219qlcnic_napi_enable(struct qlcnic_adapter *adapter)
220{
221 int ring;
222 struct qlcnic_host_sds_ring *sds_ring;
223 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
224
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000225 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
226 return;
227
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000228 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
229 sds_ring = &recv_ctx->sds_rings[ring];
230 napi_enable(&sds_ring->napi);
231 qlcnic_enable_int(sds_ring);
232 }
233}
234
235static void
236qlcnic_napi_disable(struct qlcnic_adapter *adapter)
237{
238 int ring;
239 struct qlcnic_host_sds_ring *sds_ring;
240 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
241
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000242 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
243 return;
244
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000245 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
246 sds_ring = &recv_ctx->sds_rings[ring];
247 qlcnic_disable_int(sds_ring);
248 napi_synchronize(&sds_ring->napi);
249 napi_disable(&sds_ring->napi);
250 }
251}
252
253static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
254{
255 memset(&adapter->stats, 0, sizeof(adapter->stats));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000256}
257
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000258static void qlcnic_set_port_mode(struct qlcnic_adapter *adapter)
259{
260 u32 val, data;
261
262 val = adapter->ahw.board_type;
263 if ((val == QLCNIC_BRDTYPE_P3_HMEZ) ||
264 (val == QLCNIC_BRDTYPE_P3_XG_LOM)) {
265 if (port_mode == QLCNIC_PORT_MODE_802_3_AP) {
266 data = QLCNIC_PORT_MODE_802_3_AP;
267 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
268 } else if (port_mode == QLCNIC_PORT_MODE_XG) {
269 data = QLCNIC_PORT_MODE_XG;
270 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
271 } else if (port_mode == QLCNIC_PORT_MODE_AUTO_NEG_1G) {
272 data = QLCNIC_PORT_MODE_AUTO_NEG_1G;
273 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
274 } else if (port_mode == QLCNIC_PORT_MODE_AUTO_NEG_XG) {
275 data = QLCNIC_PORT_MODE_AUTO_NEG_XG;
276 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
277 } else {
278 data = QLCNIC_PORT_MODE_AUTO_NEG;
279 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
280 }
281
282 if ((wol_port_mode != QLCNIC_PORT_MODE_802_3_AP) &&
283 (wol_port_mode != QLCNIC_PORT_MODE_XG) &&
284 (wol_port_mode != QLCNIC_PORT_MODE_AUTO_NEG_1G) &&
285 (wol_port_mode != QLCNIC_PORT_MODE_AUTO_NEG_XG)) {
286 wol_port_mode = QLCNIC_PORT_MODE_AUTO_NEG;
287 }
288 QLCWR32(adapter, QLCNIC_WOL_PORT_MODE, wol_port_mode);
289 }
290}
291
292static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
293{
294 u32 control;
295 int pos;
296
297 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
298 if (pos) {
299 pci_read_config_dword(pdev, pos, &control);
300 if (enable)
301 control |= PCI_MSIX_FLAGS_ENABLE;
302 else
303 control = 0;
304 pci_write_config_dword(pdev, pos, control);
305 }
306}
307
308static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
309{
310 int i;
311
312 for (i = 0; i < count; i++)
313 adapter->msix_entries[i].entry = i;
314}
315
316static int
317qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
318{
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000319 u8 mac_addr[ETH_ALEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000320 struct net_device *netdev = adapter->netdev;
321 struct pci_dev *pdev = adapter->pdev;
322
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000323 if (adapter->nic_ops->get_mac_addr(adapter, mac_addr) != 0)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000324 return -EIO;
325
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000326 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000327 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
328 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
329
330 /* set station address */
331
332 if (!is_valid_ether_addr(netdev->perm_addr))
333 dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
334 netdev->dev_addr);
335
336 return 0;
337}
338
339static int qlcnic_set_mac(struct net_device *netdev, void *p)
340{
341 struct qlcnic_adapter *adapter = netdev_priv(netdev);
342 struct sockaddr *addr = p;
343
344 if (!is_valid_ether_addr(addr->sa_data))
345 return -EINVAL;
346
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000347 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000348 netif_device_detach(netdev);
349 qlcnic_napi_disable(adapter);
350 }
351
352 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
353 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
354 qlcnic_set_multi(adapter->netdev);
355
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000356 if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000357 netif_device_attach(netdev);
358 qlcnic_napi_enable(adapter);
359 }
360 return 0;
361}
362
363static const struct net_device_ops qlcnic_netdev_ops = {
364 .ndo_open = qlcnic_open,
365 .ndo_stop = qlcnic_close,
366 .ndo_start_xmit = qlcnic_xmit_frame,
367 .ndo_get_stats = qlcnic_get_stats,
368 .ndo_validate_addr = eth_validate_addr,
369 .ndo_set_multicast_list = qlcnic_set_multi,
370 .ndo_set_mac_address = qlcnic_set_mac,
371 .ndo_change_mtu = qlcnic_change_mtu,
372 .ndo_tx_timeout = qlcnic_tx_timeout,
373#ifdef CONFIG_NET_POLL_CONTROLLER
374 .ndo_poll_controller = qlcnic_poll_controller,
375#endif
376};
377
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000378static struct qlcnic_nic_template qlcnic_ops = {
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000379 .get_mac_addr = qlcnic_get_mac_address,
380 .config_bridged_mode = qlcnic_config_bridged_mode,
381 .config_led = qlcnic_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000382 .start_firmware = qlcnic_start_firmware
383};
384
385static struct qlcnic_nic_template qlcnic_vf_ops = {
386 .get_mac_addr = qlcnic_get_mac_address,
387 .config_bridged_mode = qlcnicvf_config_bridged_mode,
388 .config_led = qlcnicvf_config_led,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000389 .start_firmware = qlcnicvf_start_firmware
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000390};
391
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000392static void
393qlcnic_setup_intr(struct qlcnic_adapter *adapter)
394{
395 const struct qlcnic_legacy_intr_set *legacy_intrp;
396 struct pci_dev *pdev = adapter->pdev;
397 int err, num_msix;
398
399 if (adapter->rss_supported) {
400 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
401 MSIX_ENTRIES_PER_ADAPTER : 2;
402 } else
403 num_msix = 1;
404
405 adapter->max_sds_rings = 1;
406
407 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
408
409 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
410
411 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
412 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
413 legacy_intrp->tgt_status_reg);
414 adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
415 legacy_intrp->tgt_mask_reg);
416 adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
417
418 adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
419 ISR_INT_STATE_REG);
420
421 qlcnic_set_msix_bit(pdev, 0);
422
423 if (adapter->msix_supported) {
424
425 qlcnic_init_msix_entries(adapter, num_msix);
426 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
427 if (err == 0) {
428 adapter->flags |= QLCNIC_MSIX_ENABLED;
429 qlcnic_set_msix_bit(pdev, 1);
430
431 if (adapter->rss_supported)
432 adapter->max_sds_rings = num_msix;
433
434 dev_info(&pdev->dev, "using msi-x interrupts\n");
435 return;
436 }
437
438 if (err > 0)
439 pci_disable_msix(pdev);
440
441 /* fall through for msi */
442 }
443
444 if (use_msi && !pci_enable_msi(pdev)) {
445 adapter->flags |= QLCNIC_MSI_ENABLED;
446 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
447 msi_tgt_status[adapter->ahw.pci_func]);
448 dev_info(&pdev->dev, "using msi interrupts\n");
449 adapter->msix_entries[0].vector = pdev->irq;
450 return;
451 }
452
453 dev_info(&pdev->dev, "using legacy interrupts\n");
454 adapter->msix_entries[0].vector = pdev->irq;
455}
456
457static void
458qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
459{
460 if (adapter->flags & QLCNIC_MSIX_ENABLED)
461 pci_disable_msix(adapter->pdev);
462 if (adapter->flags & QLCNIC_MSI_ENABLED)
463 pci_disable_msi(adapter->pdev);
464}
465
466static void
467qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
468{
469 if (adapter->ahw.pci_base0 != NULL)
470 iounmap(adapter->ahw.pci_base0);
471}
472
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000473static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000474qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
475{
Dan Carpentere88db3b2010-08-09 21:49:36 +0000476 struct qlcnic_pci_info *pci_info;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000477 int i, ret = 0, err;
478 u8 pfn;
479
Dan Carpentere88db3b2010-08-09 21:49:36 +0000480 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
481 if (!pci_info)
482 return -ENOMEM;
483
Dan Carpenterca315ac2010-08-09 21:47:56 +0000484 adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000485 QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
Dan Carpentere88db3b2010-08-09 21:49:36 +0000486 if (!adapter->npars) {
487 err = -ENOMEM;
488 goto err_pci_info;
489 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000490
Dan Carpenterca315ac2010-08-09 21:47:56 +0000491 adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000492 QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
493 if (!adapter->eswitch) {
494 err = -ENOMEM;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000495 goto err_npars;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000496 }
497
498 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000499 if (ret)
500 goto err_eswitch;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000501
Dan Carpenterca315ac2010-08-09 21:47:56 +0000502 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
503 pfn = pci_info[i].id;
504 if (pfn > QLCNIC_MAX_PCI_FUNC)
505 return QL_STATUS_INVALID_PARAM;
506 adapter->npars[pfn].active = pci_info[i].active;
507 adapter->npars[pfn].type = pci_info[i].type;
508 adapter->npars[pfn].phy_port = pci_info[i].default_port;
509 adapter->npars[pfn].mac_learning = DEFAULT_MAC_LEARN;
510 adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
511 adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000512 }
513
Dan Carpenterca315ac2010-08-09 21:47:56 +0000514 for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
515 adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
516
Dan Carpentere88db3b2010-08-09 21:49:36 +0000517 kfree(pci_info);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000518 return 0;
519
520err_eswitch:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000521 kfree(adapter->eswitch);
522 adapter->eswitch = NULL;
Dan Carpenterca315ac2010-08-09 21:47:56 +0000523err_npars:
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000524 kfree(adapter->npars);
Dan Carpenterca315ac2010-08-09 21:47:56 +0000525 adapter->npars = NULL;
Dan Carpentere88db3b2010-08-09 21:49:36 +0000526err_pci_info:
527 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000528
529 return ret;
530}
531
532static int
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000533qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
534{
535 u8 id;
536 u32 ref_count;
537 int i, ret = 1;
538 u32 data = QLCNIC_MGMT_FUNC;
539 void __iomem *priv_op = adapter->ahw.pci_base0 + QLCNIC_DRV_OP_MODE;
540
541 /* If other drivers are not in use set their privilege level */
542 ref_count = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
543 ret = qlcnic_api_lock(adapter);
544 if (ret)
545 goto err_lock;
546 if (QLC_DEV_CLR_REF_CNT(ref_count, adapter->ahw.pci_func))
547 goto err_npar;
548
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000549 if (qlcnic_config_npars) {
550 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000551 id = i;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000552 if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
553 id == adapter->ahw.pci_func)
554 continue;
555 data |= (qlcnic_config_npars &
556 QLC_DEV_SET_DRV(0xf, id));
557 }
558 } else {
559 data = readl(priv_op);
560 data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw.pci_func)) |
561 (QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
562 adapter->ahw.pci_func));
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000563 }
564 writel(data, priv_op);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000565err_npar:
566 qlcnic_api_unlock(adapter);
567err_lock:
568 return ret;
569}
570
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000571static u32
572qlcnic_get_driver_mode(struct qlcnic_adapter *adapter)
573{
574 void __iomem *msix_base_addr;
575 void __iomem *priv_op;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000576 struct qlcnic_info nic_info;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000577 u32 func;
578 u32 msix_base;
579 u32 op_mode, priv_level;
580
581 /* Determine FW API version */
582 adapter->fw_hal_version = readl(adapter->ahw.pci_base0 + QLCNIC_FW_API);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000583
584 /* Find PCI function number */
585 pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
586 msix_base_addr = adapter->ahw.pci_base0 + QLCNIC_MSIX_BASE;
587 msix_base = readl(msix_base_addr);
588 func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
589 adapter->ahw.pci_func = func;
590
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000591 if (!qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw.pci_func)) {
592 adapter->capabilities = nic_info.capabilities;
593
594 if (adapter->capabilities & BIT_6)
595 adapter->flags |= QLCNIC_ESWITCH_ENABLED;
596 else
597 adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
598 }
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000599
600 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED)) {
601 adapter->nic_ops = &qlcnic_ops;
602 return adapter->fw_hal_version;
603 }
604
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000605 /* Determine function privilege level */
606 priv_op = adapter->ahw.pci_base0 + QLCNIC_DRV_OP_MODE;
607 op_mode = readl(priv_op);
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000608 if (op_mode == QLC_DEV_DRV_DEFAULT)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000609 priv_level = QLCNIC_MGMT_FUNC;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000610 else
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000611 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw.pci_func);
612
613 switch (priv_level) {
614 case QLCNIC_MGMT_FUNC:
615 adapter->op_mode = QLCNIC_MGMT_FUNC;
Anirban Chakraborty45918e22010-06-29 07:52:12 +0000616 adapter->nic_ops = &qlcnic_ops;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000617 qlcnic_init_pci_info(adapter);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000618 /* Set privilege level for other functions */
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000619 qlcnic_set_function_modes(adapter);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000620 dev_info(&adapter->pdev->dev,
621 "HAL Version: %d, Management function\n",
622 adapter->fw_hal_version);
623 break;
624 case QLCNIC_PRIV_FUNC:
625 adapter->op_mode = QLCNIC_PRIV_FUNC;
626 dev_info(&adapter->pdev->dev,
627 "HAL Version: %d, Privileged function\n",
628 adapter->fw_hal_version);
Anirban Chakraborty45918e22010-06-29 07:52:12 +0000629 adapter->nic_ops = &qlcnic_ops;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000630 break;
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000631 case QLCNIC_NON_PRIV_FUNC:
632 adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
633 dev_info(&adapter->pdev->dev,
634 "HAL Version: %d Non Privileged function\n",
635 adapter->fw_hal_version);
636 adapter->nic_ops = &qlcnic_vf_ops;
637 break;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000638 default:
639 dev_info(&adapter->pdev->dev, "Unknown function mode: %d\n",
640 priv_level);
641 return 0;
642 }
643 return adapter->fw_hal_version;
644}
645
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000646static int
647qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
648{
649 void __iomem *mem_ptr0 = NULL;
650 resource_size_t mem_base;
651 unsigned long mem_len, pci_len0 = 0;
652
653 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000654
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000655 /* remap phys address */
656 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
657 mem_len = pci_resource_len(pdev, 0);
658
659 if (mem_len == QLCNIC_PCI_2MB_SIZE) {
660
661 mem_ptr0 = pci_ioremap_bar(pdev, 0);
662 if (mem_ptr0 == NULL) {
663 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
664 return -EIO;
665 }
666 pci_len0 = mem_len;
667 } else {
668 return -EIO;
669 }
670
671 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
672
673 adapter->ahw.pci_base0 = mem_ptr0;
674 adapter->ahw.pci_len0 = pci_len0;
675
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000676 if (!qlcnic_get_driver_mode(adapter)) {
677 iounmap(adapter->ahw.pci_base0);
678 return -EIO;
679 }
680
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000681 adapter->ahw.ocm_win_crb = qlcnic_get_ioaddr(adapter,
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000682 QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(adapter->ahw.pci_func)));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000683
684 return 0;
685}
686
687static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
688{
689 struct pci_dev *pdev = adapter->pdev;
690 int i, found = 0;
691
692 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
693 if (qlcnic_boards[i].vendor == pdev->vendor &&
694 qlcnic_boards[i].device == pdev->device &&
695 qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
696 qlcnic_boards[i].sub_device == pdev->subsystem_device) {
Sucheta Chakraborty02f6e462010-05-17 01:22:09 +0000697 sprintf(name, "%pM: %s" ,
698 adapter->mac_addr,
699 qlcnic_boards[i].short_name);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000700 found = 1;
701 break;
702 }
703
704 }
705
706 if (!found)
Sritej Velaga7f9a0c32010-06-17 02:56:39 +0000707 sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000708}
709
710static void
711qlcnic_check_options(struct qlcnic_adapter *adapter)
712{
713 u32 fw_major, fw_minor, fw_build;
714 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
715 char serial_num[32];
716 int i, offset, val;
717 int *ptr32;
718 struct pci_dev *pdev = adapter->pdev;
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000719 struct qlcnic_info nic_info;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000720 adapter->driver_mismatch = 0;
721
722 ptr32 = (int *)&serial_num;
723 offset = QLCNIC_FW_SERIAL_NUM_OFFSET;
724 for (i = 0; i < 8; i++) {
725 if (qlcnic_rom_fast_read(adapter, offset, &val) == -1) {
726 dev_err(&pdev->dev, "error reading board info\n");
727 adapter->driver_mismatch = 1;
728 return;
729 }
730 ptr32[i] = cpu_to_le32(val);
731 offset += sizeof(u32);
732 }
733
734 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
735 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
736 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
737
738 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
739
740 if (adapter->portnum == 0) {
741 get_brd_name(adapter, brd_name);
742
743 pr_info("%s: %s Board Chip rev 0x%x\n",
744 module_name(THIS_MODULE),
745 brd_name, adapter->ahw.revision_id);
746 }
747
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +0000748 dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
749 fw_major, fw_minor, fw_build);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000750
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000751 adapter->flags &= ~QLCNIC_LRO_ENABLED;
752
753 if (adapter->ahw.port_type == QLCNIC_XGBE) {
754 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
755 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
756 } else if (adapter->ahw.port_type == QLCNIC_GBE) {
757 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
758 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
759 }
760
Rajesh K Borundia346fe762010-06-29 08:01:20 +0000761 if (!qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw.pci_func)) {
762 adapter->physical_port = nic_info.phys_port;
763 adapter->switch_mode = nic_info.switch_mode;
764 adapter->max_tx_ques = nic_info.max_tx_ques;
765 adapter->max_rx_ques = nic_info.max_rx_ques;
766 adapter->capabilities = nic_info.capabilities;
767 adapter->max_mac_filters = nic_info.max_mac_filters;
768 adapter->max_mtu = nic_info.max_mtu;
769 }
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000770
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000771 adapter->msix_supported = !!use_msi_x;
772 adapter->rss_supported = !!use_msi_x;
773
774 adapter->num_txd = MAX_CMD_DESCRIPTORS;
775
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000776 adapter->max_rds_rings = 2;
777}
778
779static int
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000780qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
781{
782 int i, err = 0;
783 struct qlcnic_npar_info *npar;
784 struct qlcnic_info nic_info;
785
786 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
787 !adapter->need_fw_reset)
788 return 0;
789
790 if (adapter->op_mode == QLCNIC_MGMT_FUNC) {
791 /* Set the NPAR config data after FW reset */
792 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
793 npar = &adapter->npars[i];
794 if (npar->type != QLCNIC_TYPE_NIC)
795 continue;
796 err = qlcnic_get_nic_info(adapter, &nic_info, i);
797 if (err)
798 goto err_out;
799 nic_info.min_tx_bw = npar->min_bw;
800 nic_info.max_tx_bw = npar->max_bw;
801 err = qlcnic_set_nic_info(adapter, &nic_info);
802 if (err)
803 goto err_out;
804
805 if (npar->enable_pm) {
806 err = qlcnic_config_port_mirroring(adapter,
807 npar->dest_npar, 1, i);
808 if (err)
809 goto err_out;
810
811 }
812 npar->mac_learning = DEFAULT_MAC_LEARN;
813 npar->host_vlan_tag = 0;
814 npar->promisc_mode = 0;
815 npar->discard_tagged = 0;
816 npar->vlan_id = 0;
817 }
818 }
819err_out:
820 return err;
821}
822
823static int
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000824qlcnic_start_firmware(struct qlcnic_adapter *adapter)
825{
826 int val, err, first_boot;
827
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +0000828 err = qlcnic_can_start_firmware(adapter);
829 if (err < 0)
830 return err;
831 else if (!err)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000832 goto wait_init;
833
834 first_boot = QLCRD32(adapter, QLCNIC_CAM_RAM(0x1fc));
835 if (first_boot == 0x55555555)
836 /* This is the first boot after power up */
837 QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC);
838
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +0000839 if (load_fw_file)
840 qlcnic_request_firmware(adapter);
schacko8f891382010-06-17 02:56:40 +0000841 else {
842 if (qlcnic_check_flash_fw_ver(adapter))
843 goto err_out;
844
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +0000845 adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
schacko8f891382010-06-17 02:56:40 +0000846 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000847
848 err = qlcnic_need_fw_reset(adapter);
849 if (err < 0)
850 goto err_out;
851 if (err == 0)
852 goto wait_init;
853
854 if (first_boot != 0x55555555) {
855 QLCWR32(adapter, CRB_CMDPEG_STATE, 0);
Amit Kumar Salecha900c6cf2010-06-22 03:18:59 +0000856 QLCWR32(adapter, CRB_RCVPEG_STATE, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000857 qlcnic_pinit_from_rom(adapter);
858 msleep(1);
859 }
860
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000861 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS1, 0);
862 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS2, 0);
863
864 qlcnic_set_port_mode(adapter);
865
866 err = qlcnic_load_firmware(adapter);
867 if (err)
868 goto err_out;
869
870 qlcnic_release_firmware(adapter);
871
872 val = (_QLCNIC_LINUX_MAJOR << 16)
873 | ((_QLCNIC_LINUX_MINOR << 8))
874 | (_QLCNIC_LINUX_SUBVERSION);
875 QLCWR32(adapter, CRB_DRIVER_VERSION, val);
876
877wait_init:
878 /* Handshake with the card before we register the devices. */
Amit Kumar Salecha900c6cf2010-06-22 03:18:59 +0000879 err = qlcnic_init_firmware(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000880 if (err)
881 goto err_out;
882
883 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +0000884 qlcnic_idc_debug_info(adapter, 1);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000885
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000886 qlcnic_check_options(adapter);
Anirban Chakrabortycea89752010-07-13 20:33:35 +0000887 if (qlcnic_reset_npar_config(adapter))
888 goto err_out;
889 qlcnic_dev_set_npar_ready(adapter);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000890
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000891 adapter->need_fw_reset = 0;
892
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +0000893 qlcnic_release_firmware(adapter);
894 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000895
896err_out:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +0000897 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
898 dev_err(&adapter->pdev->dev, "Device state set to failed\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000899 qlcnic_release_firmware(adapter);
900 return err;
901}
902
903static int
904qlcnic_request_irq(struct qlcnic_adapter *adapter)
905{
906 irq_handler_t handler;
907 struct qlcnic_host_sds_ring *sds_ring;
908 int err, ring;
909
910 unsigned long flags = 0;
911 struct net_device *netdev = adapter->netdev;
912 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
913
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000914 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
915 handler = qlcnic_tmp_intr;
916 if (!QLCNIC_IS_MSI_FAMILY(adapter))
917 flags |= IRQF_SHARED;
918
919 } else {
920 if (adapter->flags & QLCNIC_MSIX_ENABLED)
921 handler = qlcnic_msix_intr;
922 else if (adapter->flags & QLCNIC_MSI_ENABLED)
923 handler = qlcnic_msi_intr;
924 else {
925 flags |= IRQF_SHARED;
926 handler = qlcnic_intr;
927 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000928 }
929 adapter->irq = netdev->irq;
930
931 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
932 sds_ring = &recv_ctx->sds_rings[ring];
933 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
934 err = request_irq(sds_ring->irq, handler,
935 flags, sds_ring->name, sds_ring);
936 if (err)
937 return err;
938 }
939
940 return 0;
941}
942
943static void
944qlcnic_free_irq(struct qlcnic_adapter *adapter)
945{
946 int ring;
947 struct qlcnic_host_sds_ring *sds_ring;
948
949 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
950
951 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
952 sds_ring = &recv_ctx->sds_rings[ring];
953 free_irq(sds_ring->irq, sds_ring);
954 }
955}
956
957static void
958qlcnic_init_coalesce_defaults(struct qlcnic_adapter *adapter)
959{
960 adapter->coal.flags = QLCNIC_INTR_DEFAULT;
961 adapter->coal.normal.data.rx_time_us =
962 QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
963 adapter->coal.normal.data.rx_packets =
964 QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
965 adapter->coal.normal.data.tx_time_us =
966 QLCNIC_DEFAULT_INTR_COALESCE_TX_TIME_US;
967 adapter->coal.normal.data.tx_packets =
968 QLCNIC_DEFAULT_INTR_COALESCE_TX_PACKETS;
969}
970
971static int
972__qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
973{
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000974 int ring;
975 struct qlcnic_host_rds_ring *rds_ring;
976
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000977 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
978 return -EIO;
979
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +0000980 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
981 return 0;
982
983 if (qlcnic_fw_create_ctx(adapter))
984 return -EIO;
985
986 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
987 rds_ring = &adapter->recv_ctx.rds_rings[ring];
988 qlcnic_post_rx_buffers(adapter, ring, rds_ring);
989 }
990
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000991 qlcnic_set_multi(netdev);
992 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
993
994 adapter->ahw.linkup = 0;
995
996 if (adapter->max_sds_rings > 1)
997 qlcnic_config_rss(adapter, 1);
998
999 qlcnic_config_intr_coalesce(adapter);
1000
1001 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
1002 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
1003
1004 qlcnic_napi_enable(adapter);
1005
1006 qlcnic_linkevent_request(adapter, 1);
1007
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001008 adapter->reset_context = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001009 set_bit(__QLCNIC_DEV_UP, &adapter->state);
1010 return 0;
1011}
1012
1013/* Usage: During resume and firmware recovery module.*/
1014
1015static int
1016qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
1017{
1018 int err = 0;
1019
1020 rtnl_lock();
1021 if (netif_running(netdev))
1022 err = __qlcnic_up(adapter, netdev);
1023 rtnl_unlock();
1024
1025 return err;
1026}
1027
1028static void
1029__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1030{
1031 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1032 return;
1033
1034 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
1035 return;
1036
1037 smp_mb();
1038 spin_lock(&adapter->tx_clean_lock);
1039 netif_carrier_off(netdev);
1040 netif_tx_disable(netdev);
1041
1042 qlcnic_free_mac_list(adapter);
1043
1044 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
1045
1046 qlcnic_napi_disable(adapter);
1047
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001048 qlcnic_fw_destroy_ctx(adapter);
1049
1050 qlcnic_reset_rx_buffers_list(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001051 qlcnic_release_tx_buffers(adapter);
1052 spin_unlock(&adapter->tx_clean_lock);
1053}
1054
1055/* Usage: During suspend and firmware recovery module */
1056
1057static void
1058qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
1059{
1060 rtnl_lock();
1061 if (netif_running(netdev))
1062 __qlcnic_down(adapter, netdev);
1063 rtnl_unlock();
1064
1065}
1066
1067static int
1068qlcnic_attach(struct qlcnic_adapter *adapter)
1069{
1070 struct net_device *netdev = adapter->netdev;
1071 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001072 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001073
1074 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
1075 return 0;
1076
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001077 err = qlcnic_napi_add(adapter, netdev);
1078 if (err)
1079 return err;
1080
1081 err = qlcnic_alloc_sw_resources(adapter);
1082 if (err) {
1083 dev_err(&pdev->dev, "Error in setting sw resources\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001084 goto err_out_napi_del;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001085 }
1086
1087 err = qlcnic_alloc_hw_resources(adapter);
1088 if (err) {
1089 dev_err(&pdev->dev, "Error in setting hw resources\n");
1090 goto err_out_free_sw;
1091 }
1092
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001093 err = qlcnic_request_irq(adapter);
1094 if (err) {
1095 dev_err(&pdev->dev, "failed to setup interrupt\n");
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001096 goto err_out_free_hw;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001097 }
1098
1099 qlcnic_init_coalesce_defaults(adapter);
1100
1101 qlcnic_create_sysfs_entries(adapter);
1102
1103 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
1104 return 0;
1105
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001106err_out_free_hw:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001107 qlcnic_free_hw_resources(adapter);
1108err_out_free_sw:
1109 qlcnic_free_sw_resources(adapter);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001110err_out_napi_del:
1111 qlcnic_napi_del(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001112 return err;
1113}
1114
1115static void
1116qlcnic_detach(struct qlcnic_adapter *adapter)
1117{
1118 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1119 return;
1120
1121 qlcnic_remove_sysfs_entries(adapter);
1122
1123 qlcnic_free_hw_resources(adapter);
1124 qlcnic_release_rx_buffers(adapter);
1125 qlcnic_free_irq(adapter);
1126 qlcnic_napi_del(adapter);
1127 qlcnic_free_sw_resources(adapter);
1128
1129 adapter->is_up = 0;
1130}
1131
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001132void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1133{
1134 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1135 struct qlcnic_host_sds_ring *sds_ring;
1136 int ring;
1137
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001138 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001139 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1140 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1141 sds_ring = &adapter->recv_ctx.sds_rings[ring];
1142 qlcnic_disable_int(sds_ring);
1143 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001144 }
1145
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001146 qlcnic_fw_destroy_ctx(adapter);
1147
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001148 qlcnic_detach(adapter);
1149
1150 adapter->diag_test = 0;
1151 adapter->max_sds_rings = max_sds_rings;
1152
1153 if (qlcnic_attach(adapter))
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001154 goto out;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001155
1156 if (netif_running(netdev))
1157 __qlcnic_up(adapter, netdev);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001158out:
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001159 netif_device_attach(netdev);
1160}
1161
1162int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1163{
1164 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1165 struct qlcnic_host_sds_ring *sds_ring;
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001166 struct qlcnic_host_rds_ring *rds_ring;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001167 int ring;
1168 int ret;
1169
1170 netif_device_detach(netdev);
1171
1172 if (netif_running(netdev))
1173 __qlcnic_down(adapter, netdev);
1174
1175 qlcnic_detach(adapter);
1176
1177 adapter->max_sds_rings = 1;
1178 adapter->diag_test = test;
1179
1180 ret = qlcnic_attach(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001181 if (ret) {
1182 netif_device_attach(netdev);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001183 return ret;
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001184 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001185
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001186 ret = qlcnic_fw_create_ctx(adapter);
1187 if (ret) {
1188 qlcnic_detach(adapter);
Sony Chacko57e46242010-07-24 18:32:18 +00001189 netif_device_attach(netdev);
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00001190 return ret;
1191 }
1192
1193 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1194 rds_ring = &adapter->recv_ctx.rds_rings[ring];
1195 qlcnic_post_rx_buffers(adapter, ring, rds_ring);
1196 }
1197
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001198 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1199 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1200 sds_ring = &adapter->recv_ctx.sds_rings[ring];
1201 qlcnic_enable_int(sds_ring);
1202 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001203 }
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001204 set_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001205
1206 return 0;
1207}
1208
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001209/* Reset context in hardware only */
1210static int
1211qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
1212{
1213 struct net_device *netdev = adapter->netdev;
1214
1215 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1216 return -EBUSY;
1217
1218 netif_device_detach(netdev);
1219
1220 qlcnic_down(adapter, netdev);
1221
1222 qlcnic_up(adapter, netdev);
1223
1224 netif_device_attach(netdev);
1225
1226 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1227 return 0;
1228}
1229
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001230int
1231qlcnic_reset_context(struct qlcnic_adapter *adapter)
1232{
1233 int err = 0;
1234 struct net_device *netdev = adapter->netdev;
1235
1236 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1237 return -EBUSY;
1238
1239 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1240
1241 netif_device_detach(netdev);
1242
1243 if (netif_running(netdev))
1244 __qlcnic_down(adapter, netdev);
1245
1246 qlcnic_detach(adapter);
1247
1248 if (netif_running(netdev)) {
1249 err = qlcnic_attach(adapter);
1250 if (!err)
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001251 __qlcnic_up(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001252 }
1253
1254 netif_device_attach(netdev);
1255 }
1256
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001257 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1258 return err;
1259}
1260
1261static int
1262qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001263 struct net_device *netdev, u8 pci_using_dac)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001264{
1265 int err;
1266 struct pci_dev *pdev = adapter->pdev;
1267
1268 adapter->rx_csum = 1;
1269 adapter->mc_enabled = 0;
1270 adapter->max_mc_count = 38;
1271
1272 netdev->netdev_ops = &qlcnic_netdev_ops;
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00001273 netdev->watchdog_timeo = 5*HZ;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001274
1275 qlcnic_change_mtu(netdev, netdev->mtu);
1276
1277 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1278
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001279 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
Anirban Chakrabortyac8d0c42010-07-09 13:14:58 +00001280 NETIF_F_IPV6_CSUM | NETIF_F_GRO);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001281 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
Anirban Chakrabortyac8d0c42010-07-09 13:14:58 +00001282 NETIF_F_IPV6_CSUM);
1283
1284 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
1285 netdev->features |= (NETIF_F_TSO | NETIF_F_TSO6);
1286 netdev->vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
1287 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001288
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001289 if (pci_using_dac) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001290 netdev->features |= NETIF_F_HIGHDMA;
1291 netdev->vlan_features |= NETIF_F_HIGHDMA;
1292 }
1293
1294 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
1295 netdev->features |= (NETIF_F_HW_VLAN_TX);
1296
1297 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
1298 netdev->features |= NETIF_F_LRO;
1299
1300 netdev->irq = adapter->msix_entries[0].vector;
1301
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001302 if (qlcnic_read_mac_addr(adapter))
1303 dev_warn(&pdev->dev, "failed to read mac addr\n");
1304
1305 netif_carrier_off(netdev);
1306 netif_stop_queue(netdev);
1307
1308 err = register_netdev(netdev);
1309 if (err) {
1310 dev_err(&pdev->dev, "failed to register net device\n");
1311 return err;
1312 }
1313
1314 return 0;
1315}
1316
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001317static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1318{
1319 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1320 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1321 *pci_using_dac = 1;
1322 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1323 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1324 *pci_using_dac = 0;
1325 else {
1326 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1327 return -EIO;
1328 }
1329
1330 return 0;
1331}
1332
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001333static int __devinit
1334qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1335{
1336 struct net_device *netdev = NULL;
1337 struct qlcnic_adapter *adapter = NULL;
1338 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001339 uint8_t revision_id;
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001340 uint8_t pci_using_dac;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001341
1342 err = pci_enable_device(pdev);
1343 if (err)
1344 return err;
1345
1346 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1347 err = -ENODEV;
1348 goto err_out_disable_pdev;
1349 }
1350
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001351 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1352 if (err)
1353 goto err_out_disable_pdev;
1354
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001355 err = pci_request_regions(pdev, qlcnic_driver_name);
1356 if (err)
1357 goto err_out_disable_pdev;
1358
1359 pci_set_master(pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001360 pci_enable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001361
1362 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1363 if (!netdev) {
1364 dev_err(&pdev->dev, "failed to allocate net_device\n");
1365 err = -ENOMEM;
1366 goto err_out_free_res;
1367 }
1368
1369 SET_NETDEV_DEV(netdev, &pdev->dev);
1370
1371 adapter = netdev_priv(netdev);
1372 adapter->netdev = netdev;
1373 adapter->pdev = pdev;
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001374 adapter->dev_rst_time = jiffies;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001375
1376 revision_id = pdev->revision;
1377 adapter->ahw.revision_id = revision_id;
1378
1379 rwlock_init(&adapter->ahw.crb_lock);
1380 mutex_init(&adapter->ahw.mem_lock);
1381
1382 spin_lock_init(&adapter->tx_clean_lock);
1383 INIT_LIST_HEAD(&adapter->mac_list);
1384
1385 err = qlcnic_setup_pci_map(adapter);
1386 if (err)
1387 goto err_out_free_netdev;
1388
1389 /* This will be reset for mezz cards */
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001390 adapter->portnum = adapter->ahw.pci_func;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001391
1392 err = qlcnic_get_board_info(adapter);
1393 if (err) {
1394 dev_err(&pdev->dev, "Error getting board config info.\n");
1395 goto err_out_iounmap;
1396 }
1397
Sucheta Chakraborty02f6e462010-05-17 01:22:09 +00001398 if (qlcnic_read_mac_addr(adapter))
1399 dev_warn(&pdev->dev, "failed to read mac addr\n");
1400
Sucheta Chakrabortyb3a24642010-05-13 03:07:48 +00001401 if (qlcnic_setup_idc_param(adapter))
1402 goto err_out_iounmap;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001403
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001404 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001405 if (err) {
1406 dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001407 goto err_out_decr_ref;
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001408 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001409
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001410 qlcnic_clear_stats(adapter);
1411
1412 qlcnic_setup_intr(adapter);
1413
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001414 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001415 if (err)
1416 goto err_out_disable_msi;
1417
1418 pci_set_drvdata(pdev, adapter);
1419
1420 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1421
1422 switch (adapter->ahw.port_type) {
1423 case QLCNIC_GBE:
1424 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1425 adapter->netdev->name);
1426 break;
1427 case QLCNIC_XGBE:
1428 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1429 adapter->netdev->name);
1430 break;
1431 }
1432
1433 qlcnic_create_diag_entries(adapter);
1434
1435 return 0;
1436
1437err_out_disable_msi:
1438 qlcnic_teardown_intr(adapter);
1439
1440err_out_decr_ref:
1441 qlcnic_clr_all_drv_state(adapter);
1442
1443err_out_iounmap:
1444 qlcnic_cleanup_pci_map(adapter);
1445
1446err_out_free_netdev:
1447 free_netdev(netdev);
1448
1449err_out_free_res:
1450 pci_release_regions(pdev);
1451
1452err_out_disable_pdev:
1453 pci_set_drvdata(pdev, NULL);
1454 pci_disable_device(pdev);
1455 return err;
1456}
1457
1458static void __devexit qlcnic_remove(struct pci_dev *pdev)
1459{
1460 struct qlcnic_adapter *adapter;
1461 struct net_device *netdev;
1462
1463 adapter = pci_get_drvdata(pdev);
1464 if (adapter == NULL)
1465 return;
1466
1467 netdev = adapter->netdev;
1468
1469 qlcnic_cancel_fw_work(adapter);
1470
1471 unregister_netdev(netdev);
1472
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001473 qlcnic_detach(adapter);
1474
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001475 if (adapter->npars != NULL)
1476 kfree(adapter->npars);
1477 if (adapter->eswitch != NULL)
1478 kfree(adapter->eswitch);
1479
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001480 qlcnic_clr_all_drv_state(adapter);
1481
1482 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1483
1484 qlcnic_teardown_intr(adapter);
1485
1486 qlcnic_remove_diag_entries(adapter);
1487
1488 qlcnic_cleanup_pci_map(adapter);
1489
1490 qlcnic_release_firmware(adapter);
1491
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00001492 pci_disable_pcie_error_reporting(pdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001493 pci_release_regions(pdev);
1494 pci_disable_device(pdev);
1495 pci_set_drvdata(pdev, NULL);
1496
1497 free_netdev(netdev);
1498}
1499static int __qlcnic_shutdown(struct pci_dev *pdev)
1500{
1501 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1502 struct net_device *netdev = adapter->netdev;
1503 int retval;
1504
1505 netif_device_detach(netdev);
1506
1507 qlcnic_cancel_fw_work(adapter);
1508
1509 if (netif_running(netdev))
1510 qlcnic_down(adapter, netdev);
1511
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001512 qlcnic_clr_all_drv_state(adapter);
1513
1514 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1515
1516 retval = pci_save_state(pdev);
1517 if (retval)
1518 return retval;
1519
1520 if (qlcnic_wol_supported(adapter)) {
1521 pci_enable_wake(pdev, PCI_D3cold, 1);
1522 pci_enable_wake(pdev, PCI_D3hot, 1);
1523 }
1524
1525 return 0;
1526}
1527
1528static void qlcnic_shutdown(struct pci_dev *pdev)
1529{
1530 if (__qlcnic_shutdown(pdev))
1531 return;
1532
1533 pci_disable_device(pdev);
1534}
1535
1536#ifdef CONFIG_PM
1537static int
1538qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1539{
1540 int retval;
1541
1542 retval = __qlcnic_shutdown(pdev);
1543 if (retval)
1544 return retval;
1545
1546 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1547 return 0;
1548}
1549
1550static int
1551qlcnic_resume(struct pci_dev *pdev)
1552{
1553 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1554 struct net_device *netdev = adapter->netdev;
1555 int err;
1556
1557 err = pci_enable_device(pdev);
1558 if (err)
1559 return err;
1560
1561 pci_set_power_state(pdev, PCI_D0);
1562 pci_set_master(pdev);
1563 pci_restore_state(pdev);
1564
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001565 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001566 if (err) {
1567 dev_err(&pdev->dev, "failed to start firmware\n");
1568 return err;
1569 }
1570
1571 if (netif_running(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001572 err = qlcnic_up(adapter, netdev);
1573 if (err)
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001574 goto done;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001575
1576 qlcnic_config_indev_addr(netdev, NETDEV_UP);
1577 }
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00001578done:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001579 netif_device_attach(netdev);
1580 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1581 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001582}
1583#endif
1584
1585static int qlcnic_open(struct net_device *netdev)
1586{
1587 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1588 int err;
1589
1590 if (adapter->driver_mismatch)
1591 return -EIO;
1592
1593 err = qlcnic_attach(adapter);
1594 if (err)
1595 return err;
1596
1597 err = __qlcnic_up(adapter, netdev);
1598 if (err)
1599 goto err_out;
1600
1601 netif_start_queue(netdev);
1602
1603 return 0;
1604
1605err_out:
1606 qlcnic_detach(adapter);
1607 return err;
1608}
1609
1610/*
1611 * qlcnic_close - Disables a network interface entry point
1612 */
1613static int qlcnic_close(struct net_device *netdev)
1614{
1615 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1616
1617 __qlcnic_down(adapter, netdev);
1618 return 0;
1619}
1620
1621static void
1622qlcnic_tso_check(struct net_device *netdev,
1623 struct qlcnic_host_tx_ring *tx_ring,
1624 struct cmd_desc_type0 *first_desc,
1625 struct sk_buff *skb)
1626{
1627 u8 opcode = TX_ETHER_PKT;
1628 __be16 protocol = skb->protocol;
1629 u16 flags = 0, vid = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001630 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1631 struct cmd_desc_type0 *hwdesc;
1632 struct vlan_ethhdr *vh;
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001633 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001634 u32 producer = tx_ring->producer;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001635
1636 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1637
1638 vh = (struct vlan_ethhdr *)skb->data;
1639 protocol = vh->h_vlan_encapsulated_proto;
1640 flags = FLAGS_VLAN_TAGGED;
1641
1642 } else if (vlan_tx_tag_present(skb)) {
1643
1644 flags = FLAGS_VLAN_OOB;
1645 vid = vlan_tx_tag_get(skb);
1646 qlcnic_set_tx_vlan_tci(first_desc, vid);
1647 vlan_oob = 1;
1648 }
1649
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001650 if (*(skb->data) & BIT_0) {
1651 flags |= BIT_0;
1652 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
1653 }
1654
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001655 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1656 skb_shinfo(skb)->gso_size > 0) {
1657
1658 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1659
1660 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1661 first_desc->total_hdr_length = hdr_len;
1662 if (vlan_oob) {
1663 first_desc->total_hdr_length += VLAN_HLEN;
1664 first_desc->tcp_hdr_offset = VLAN_HLEN;
1665 first_desc->ip_hdr_offset = VLAN_HLEN;
1666 /* Only in case of TSO on vlan device */
1667 flags |= FLAGS_VLAN_TAGGED;
1668 }
1669
1670 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1671 TX_TCP_LSO6 : TX_TCP_LSO;
1672 tso = 1;
1673
1674 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1675 u8 l4proto;
1676
1677 if (protocol == cpu_to_be16(ETH_P_IP)) {
1678 l4proto = ip_hdr(skb)->protocol;
1679
1680 if (l4proto == IPPROTO_TCP)
1681 opcode = TX_TCP_PKT;
1682 else if (l4proto == IPPROTO_UDP)
1683 opcode = TX_UDP_PKT;
1684 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1685 l4proto = ipv6_hdr(skb)->nexthdr;
1686
1687 if (l4proto == IPPROTO_TCP)
1688 opcode = TX_TCPV6_PKT;
1689 else if (l4proto == IPPROTO_UDP)
1690 opcode = TX_UDPV6_PKT;
1691 }
1692 }
1693
1694 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1695 first_desc->ip_hdr_offset += skb_network_offset(skb);
1696 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
1697
1698 if (!tso)
1699 return;
1700
1701 /* For LSO, we need to copy the MAC/IP/TCP headers into
1702 * the descriptor ring
1703 */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001704 copied = 0;
1705 offset = 2;
1706
1707 if (vlan_oob) {
1708 /* Create a TSO vlan header template for firmware */
1709
1710 hwdesc = &tx_ring->desc_head[producer];
1711 tx_ring->cmd_buf_arr[producer].skb = NULL;
1712
1713 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1714 hdr_len + VLAN_HLEN);
1715
1716 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1717 skb_copy_from_linear_data(skb, vh, 12);
1718 vh->h_vlan_proto = htons(ETH_P_8021Q);
1719 vh->h_vlan_TCI = htons(vid);
1720 skb_copy_from_linear_data_offset(skb, 12,
1721 (char *)vh + 16, copy_len - 16);
1722
1723 copied = copy_len - VLAN_HLEN;
1724 offset = 0;
1725
1726 producer = get_next_index(producer, tx_ring->num_desc);
1727 }
1728
1729 while (copied < hdr_len) {
1730
1731 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1732 (hdr_len - copied));
1733
1734 hwdesc = &tx_ring->desc_head[producer];
1735 tx_ring->cmd_buf_arr[producer].skb = NULL;
1736
1737 skb_copy_from_linear_data_offset(skb, copied,
1738 (char *)hwdesc + offset, copy_len);
1739
1740 copied += copy_len;
1741 offset = 0;
1742
1743 producer = get_next_index(producer, tx_ring->num_desc);
1744 }
1745
1746 tx_ring->producer = producer;
1747 barrier();
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001748 adapter->stats.lso_frames++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001749}
1750
1751static int
1752qlcnic_map_tx_skb(struct pci_dev *pdev,
1753 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
1754{
1755 struct qlcnic_skb_frag *nf;
1756 struct skb_frag_struct *frag;
1757 int i, nr_frags;
1758 dma_addr_t map;
1759
1760 nr_frags = skb_shinfo(skb)->nr_frags;
1761 nf = &pbuf->frag_array[0];
1762
1763 map = pci_map_single(pdev, skb->data,
1764 skb_headlen(skb), PCI_DMA_TODEVICE);
1765 if (pci_dma_mapping_error(pdev, map))
1766 goto out_err;
1767
1768 nf->dma = map;
1769 nf->length = skb_headlen(skb);
1770
1771 for (i = 0; i < nr_frags; i++) {
1772 frag = &skb_shinfo(skb)->frags[i];
1773 nf = &pbuf->frag_array[i+1];
1774
1775 map = pci_map_page(pdev, frag->page, frag->page_offset,
1776 frag->size, PCI_DMA_TODEVICE);
1777 if (pci_dma_mapping_error(pdev, map))
1778 goto unwind;
1779
1780 nf->dma = map;
1781 nf->length = frag->size;
1782 }
1783
1784 return 0;
1785
1786unwind:
1787 while (--i >= 0) {
1788 nf = &pbuf->frag_array[i+1];
1789 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1790 }
1791
1792 nf = &pbuf->frag_array[0];
1793 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1794
1795out_err:
1796 return -ENOMEM;
1797}
1798
1799static inline void
1800qlcnic_clear_cmddesc(u64 *desc)
1801{
1802 desc[0] = 0ULL;
1803 desc[2] = 0ULL;
1804}
1805
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001806netdev_tx_t
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001807qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1808{
1809 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1810 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1811 struct qlcnic_cmd_buffer *pbuf;
1812 struct qlcnic_skb_frag *buffrag;
1813 struct cmd_desc_type0 *hwdesc, *first_desc;
1814 struct pci_dev *pdev;
1815 int i, k;
1816
1817 u32 producer;
1818 int frag_count, no_of_desc;
1819 u32 num_txd = tx_ring->num_desc;
1820
Amit Kumar Salecha780ab792010-04-22 02:51:41 +00001821 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
1822 netif_stop_queue(netdev);
1823 return NETDEV_TX_BUSY;
1824 }
1825
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001826 frag_count = skb_shinfo(skb)->nr_frags + 1;
1827
1828 /* 4 fragments per cmd des */
1829 no_of_desc = (frag_count + 3) >> 2;
1830
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00001831 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001832 netif_stop_queue(netdev);
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00001833 smp_mb();
1834 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
1835 netif_start_queue(netdev);
1836 else {
1837 adapter->stats.xmit_off++;
1838 return NETDEV_TX_BUSY;
1839 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001840 }
1841
1842 producer = tx_ring->producer;
1843 pbuf = &tx_ring->cmd_buf_arr[producer];
1844
1845 pdev = adapter->pdev;
1846
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00001847 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
1848 adapter->stats.tx_dma_map_error++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001849 goto drop_packet;
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00001850 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001851
1852 pbuf->skb = skb;
1853 pbuf->frag_count = frag_count;
1854
1855 first_desc = hwdesc = &tx_ring->desc_head[producer];
1856 qlcnic_clear_cmddesc((u64 *)hwdesc);
1857
1858 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
1859 qlcnic_set_tx_port(first_desc, adapter->portnum);
1860
1861 for (i = 0; i < frag_count; i++) {
1862
1863 k = i % 4;
1864
1865 if ((k == 0) && (i > 0)) {
1866 /* move to next desc.*/
1867 producer = get_next_index(producer, num_txd);
1868 hwdesc = &tx_ring->desc_head[producer];
1869 qlcnic_clear_cmddesc((u64 *)hwdesc);
1870 tx_ring->cmd_buf_arr[producer].skb = NULL;
1871 }
1872
1873 buffrag = &pbuf->frag_array[i];
1874
1875 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
1876 switch (k) {
1877 case 0:
1878 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1879 break;
1880 case 1:
1881 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
1882 break;
1883 case 2:
1884 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
1885 break;
1886 case 3:
1887 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
1888 break;
1889 }
1890 }
1891
1892 tx_ring->producer = get_next_index(producer, num_txd);
1893
1894 qlcnic_tso_check(netdev, tx_ring, first_desc, skb);
1895
1896 qlcnic_update_cmd_producer(adapter, tx_ring);
1897
1898 adapter->stats.txbytes += skb->len;
1899 adapter->stats.xmitcalled++;
1900
1901 return NETDEV_TX_OK;
1902
1903drop_packet:
1904 adapter->stats.txdropped++;
1905 dev_kfree_skb_any(skb);
1906 return NETDEV_TX_OK;
1907}
1908
1909static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
1910{
1911 struct net_device *netdev = adapter->netdev;
1912 u32 temp, temp_state, temp_val;
1913 int rv = 0;
1914
1915 temp = QLCRD32(adapter, CRB_TEMP_STATE);
1916
1917 temp_state = qlcnic_get_temp_state(temp);
1918 temp_val = qlcnic_get_temp_val(temp);
1919
1920 if (temp_state == QLCNIC_TEMP_PANIC) {
1921 dev_err(&netdev->dev,
1922 "Device temperature %d degrees C exceeds"
1923 " maximum allowed. Hardware has been shut down.\n",
1924 temp_val);
1925 rv = 1;
1926 } else if (temp_state == QLCNIC_TEMP_WARN) {
1927 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
1928 dev_err(&netdev->dev,
1929 "Device temperature %d degrees C "
1930 "exceeds operating range."
1931 " Immediate action needed.\n",
1932 temp_val);
1933 }
1934 } else {
1935 if (adapter->temp == QLCNIC_TEMP_WARN) {
1936 dev_info(&netdev->dev,
1937 "Device temperature is now %d degrees C"
1938 " in normal range.\n", temp_val);
1939 }
1940 }
1941 adapter->temp = temp_state;
1942 return rv;
1943}
1944
1945void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
1946{
1947 struct net_device *netdev = adapter->netdev;
1948
1949 if (adapter->ahw.linkup && !linkup) {
1950 dev_info(&netdev->dev, "NIC Link is down\n");
1951 adapter->ahw.linkup = 0;
1952 if (netif_running(netdev)) {
1953 netif_carrier_off(netdev);
1954 netif_stop_queue(netdev);
1955 }
1956 } else if (!adapter->ahw.linkup && linkup) {
1957 dev_info(&netdev->dev, "NIC Link is up\n");
1958 adapter->ahw.linkup = 1;
1959 if (netif_running(netdev)) {
1960 netif_carrier_on(netdev);
1961 netif_wake_queue(netdev);
1962 }
1963 }
1964}
1965
1966static void qlcnic_tx_timeout(struct net_device *netdev)
1967{
1968 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1969
1970 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
1971 return;
1972
1973 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001974
1975 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00001976 adapter->need_fw_reset = 1;
1977 else
1978 adapter->reset_context = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001979}
1980
1981static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
1982{
1983 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1984 struct net_device_stats *stats = &netdev->stats;
1985
1986 memset(stats, 0, sizeof(*stats));
1987
1988 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
1989 stats->tx_packets = adapter->stats.xmitfinished;
Sucheta Chakraborty7e382592010-05-17 01:22:10 +00001990 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001991 stats->tx_bytes = adapter->stats.txbytes;
1992 stats->rx_dropped = adapter->stats.rxdropped;
1993 stats->tx_dropped = adapter->stats.txdropped;
1994
1995 return stats;
1996}
1997
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001998static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001999{
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002000 u32 status;
2001
2002 status = readl(adapter->isr_int_vec);
2003
2004 if (!(status & adapter->int_vec_bit))
2005 return IRQ_NONE;
2006
2007 /* check interrupt state machine, to be sure */
2008 status = readl(adapter->crb_int_state_reg);
2009 if (!ISR_LEGACY_INT_TRIGGERED(status))
2010 return IRQ_NONE;
2011
2012 writel(0xffffffff, adapter->tgt_status_reg);
2013 /* read twice to ensure write is flushed */
2014 readl(adapter->isr_int_vec);
2015 readl(adapter->isr_int_vec);
2016
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00002017 return IRQ_HANDLED;
2018}
2019
2020static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
2021{
2022 struct qlcnic_host_sds_ring *sds_ring = data;
2023 struct qlcnic_adapter *adapter = sds_ring->adapter;
2024
2025 if (adapter->flags & QLCNIC_MSIX_ENABLED)
2026 goto done;
2027 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
2028 writel(0xffffffff, adapter->tgt_status_reg);
2029 goto done;
2030 }
2031
2032 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2033 return IRQ_NONE;
2034
2035done:
2036 adapter->diag_cnt++;
2037 qlcnic_enable_int(sds_ring);
2038 return IRQ_HANDLED;
2039}
2040
2041static irqreturn_t qlcnic_intr(int irq, void *data)
2042{
2043 struct qlcnic_host_sds_ring *sds_ring = data;
2044 struct qlcnic_adapter *adapter = sds_ring->adapter;
2045
2046 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
2047 return IRQ_NONE;
2048
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002049 napi_schedule(&sds_ring->napi);
2050
2051 return IRQ_HANDLED;
2052}
2053
2054static irqreturn_t qlcnic_msi_intr(int irq, void *data)
2055{
2056 struct qlcnic_host_sds_ring *sds_ring = data;
2057 struct qlcnic_adapter *adapter = sds_ring->adapter;
2058
2059 /* clear interrupt */
2060 writel(0xffffffff, adapter->tgt_status_reg);
2061
2062 napi_schedule(&sds_ring->napi);
2063 return IRQ_HANDLED;
2064}
2065
2066static irqreturn_t qlcnic_msix_intr(int irq, void *data)
2067{
2068 struct qlcnic_host_sds_ring *sds_ring = data;
2069
2070 napi_schedule(&sds_ring->napi);
2071 return IRQ_HANDLED;
2072}
2073
2074static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
2075{
2076 u32 sw_consumer, hw_consumer;
2077 int count = 0, i;
2078 struct qlcnic_cmd_buffer *buffer;
2079 struct pci_dev *pdev = adapter->pdev;
2080 struct net_device *netdev = adapter->netdev;
2081 struct qlcnic_skb_frag *frag;
2082 int done;
2083 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
2084
2085 if (!spin_trylock(&adapter->tx_clean_lock))
2086 return 1;
2087
2088 sw_consumer = tx_ring->sw_consumer;
2089 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2090
2091 while (sw_consumer != hw_consumer) {
2092 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
2093 if (buffer->skb) {
2094 frag = &buffer->frag_array[0];
2095 pci_unmap_single(pdev, frag->dma, frag->length,
2096 PCI_DMA_TODEVICE);
2097 frag->dma = 0ULL;
2098 for (i = 1; i < buffer->frag_count; i++) {
2099 frag++;
2100 pci_unmap_page(pdev, frag->dma, frag->length,
2101 PCI_DMA_TODEVICE);
2102 frag->dma = 0ULL;
2103 }
2104
2105 adapter->stats.xmitfinished++;
2106 dev_kfree_skb_any(buffer->skb);
2107 buffer->skb = NULL;
2108 }
2109
2110 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2111 if (++count >= MAX_STATUS_HANDLE)
2112 break;
2113 }
2114
2115 if (count && netif_running(netdev)) {
2116 tx_ring->sw_consumer = sw_consumer;
2117
2118 smp_mb();
2119
2120 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002121 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2122 netif_wake_queue(netdev);
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00002123 adapter->stats.xmit_on++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002124 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002125 }
Rajesh K Borundiaef71ff82010-06-17 02:56:41 +00002126 adapter->tx_timeo_cnt = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002127 }
2128 /*
2129 * If everything is freed up to consumer then check if the ring is full
2130 * If the ring is full then check if more needs to be freed and
2131 * schedule the call back again.
2132 *
2133 * This happens when there are 2 CPUs. One could be freeing and the
2134 * other filling it. If the ring is full when we get out of here and
2135 * the card has already interrupted the host then the host can miss the
2136 * interrupt.
2137 *
2138 * There is still a possible race condition and the host could miss an
2139 * interrupt. The card has to take care of this.
2140 */
2141 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2142 done = (sw_consumer == hw_consumer);
2143 spin_unlock(&adapter->tx_clean_lock);
2144
2145 return done;
2146}
2147
2148static int qlcnic_poll(struct napi_struct *napi, int budget)
2149{
2150 struct qlcnic_host_sds_ring *sds_ring =
2151 container_of(napi, struct qlcnic_host_sds_ring, napi);
2152
2153 struct qlcnic_adapter *adapter = sds_ring->adapter;
2154
2155 int tx_complete;
2156 int work_done;
2157
2158 tx_complete = qlcnic_process_cmd_ring(adapter);
2159
2160 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2161
2162 if ((work_done < budget) && tx_complete) {
2163 napi_complete(&sds_ring->napi);
2164 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2165 qlcnic_enable_int(sds_ring);
2166 }
2167
2168 return work_done;
2169}
2170
schacko8f891382010-06-17 02:56:40 +00002171static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
2172{
2173 struct qlcnic_host_sds_ring *sds_ring =
2174 container_of(napi, struct qlcnic_host_sds_ring, napi);
2175
2176 struct qlcnic_adapter *adapter = sds_ring->adapter;
2177 int work_done;
2178
2179 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2180
2181 if (work_done < budget) {
2182 napi_complete(&sds_ring->napi);
2183 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2184 qlcnic_enable_int(sds_ring);
2185 }
2186
2187 return work_done;
2188}
2189
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002190#ifdef CONFIG_NET_POLL_CONTROLLER
2191static void qlcnic_poll_controller(struct net_device *netdev)
2192{
2193 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2194 disable_irq(adapter->irq);
2195 qlcnic_intr(adapter->irq, adapter);
2196 enable_irq(adapter->irq);
2197}
2198#endif
2199
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002200static void
2201qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2202{
2203 u32 val;
2204
2205 val = adapter->portnum & 0xf;
2206 val |= encoding << 7;
2207 val |= (jiffies - adapter->dev_rst_time) << 8;
2208
2209 QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2210 adapter->dev_rst_time = jiffies;
2211}
2212
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002213static int
2214qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002215{
2216 u32 val;
2217
2218 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2219 state != QLCNIC_DEV_NEED_QUISCENT);
2220
2221 if (qlcnic_api_lock(adapter))
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002222 return -EIO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002223
2224 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2225
2226 if (state == QLCNIC_DEV_NEED_RESET)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002227 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002228 else if (state == QLCNIC_DEV_NEED_QUISCENT)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002229 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002230
2231 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2232
2233 qlcnic_api_unlock(adapter);
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002234
2235 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002236}
2237
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002238static int
2239qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2240{
2241 u32 val;
2242
2243 if (qlcnic_api_lock(adapter))
2244 return -EBUSY;
2245
2246 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002247 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002248 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2249
2250 qlcnic_api_unlock(adapter);
2251
2252 return 0;
2253}
2254
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002255static void
2256qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter)
2257{
2258 u32 val;
2259
2260 if (qlcnic_api_lock(adapter))
2261 goto err;
2262
2263 val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002264 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002265 QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val);
2266
2267 if (!(val & 0x11111111))
2268 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2269
2270 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002271 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002272 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2273
2274 qlcnic_api_unlock(adapter);
2275err:
2276 adapter->fw_fail_cnt = 0;
2277 clear_bit(__QLCNIC_START_FW, &adapter->state);
2278 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2279}
2280
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002281/* Grab api lock, before checking state */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002282static int
2283qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2284{
2285 int act, state;
2286
2287 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2288 act = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
2289
2290 if (((state & 0x11111111) == (act & 0x11111111)) ||
2291 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2292 return 0;
2293 else
2294 return 1;
2295}
2296
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002297static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2298{
2299 u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2300
2301 if (val != QLCNIC_DRV_IDC_VER) {
2302 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2303 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2304 }
2305
2306 return 0;
2307}
2308
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002309static int
2310qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2311{
2312 u32 val, prev_state;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002313 u8 dev_init_timeo = adapter->dev_init_timeo;
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002314 u8 portnum = adapter->portnum;
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002315 u8 ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002316
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002317 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2318 return 1;
2319
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002320 if (qlcnic_api_lock(adapter))
2321 return -1;
2322
2323 val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002324 if (!(val & (1 << (portnum * 4)))) {
2325 QLC_DEV_SET_REF_CNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002326 QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002327 }
2328
2329 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002330 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002331
2332 switch (prev_state) {
2333 case QLCNIC_DEV_COLD:
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002334 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002335 QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002336 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002337 qlcnic_api_unlock(adapter);
2338 return 1;
2339
2340 case QLCNIC_DEV_READY:
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002341 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002342 qlcnic_api_unlock(adapter);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002343 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002344
2345 case QLCNIC_DEV_NEED_RESET:
2346 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002347 QLC_DEV_SET_RST_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002348 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2349 break;
2350
2351 case QLCNIC_DEV_NEED_QUISCENT:
2352 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002353 QLC_DEV_SET_QSCNT_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002354 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2355 break;
2356
2357 case QLCNIC_DEV_FAILED:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00002358 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002359 qlcnic_api_unlock(adapter);
2360 return -1;
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002361
2362 case QLCNIC_DEV_INITIALIZING:
2363 case QLCNIC_DEV_QUISCENT:
2364 break;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002365 }
2366
2367 qlcnic_api_unlock(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002368
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002369 do {
2370 msleep(1000);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002371 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2372
2373 if (prev_state == QLCNIC_DEV_QUISCENT)
2374 continue;
2375 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002376
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002377 if (!dev_init_timeo) {
2378 dev_err(&adapter->pdev->dev,
2379 "Waiting for device to initialize timeout\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002380 return -1;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002381 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002382
2383 if (qlcnic_api_lock(adapter))
2384 return -1;
2385
2386 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002387 QLC_DEV_CLR_RST_QSCNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002388 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2389
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002390 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002391 qlcnic_api_unlock(adapter);
2392
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002393 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002394}
2395
2396static void
2397qlcnic_fwinit_work(struct work_struct *work)
2398{
2399 struct qlcnic_adapter *adapter = container_of(work,
2400 struct qlcnic_adapter, fw_work.work);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002401 u32 dev_state = 0xf, npar_state;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002402
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002403 if (qlcnic_api_lock(adapter))
2404 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002405
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002406 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2407 if (dev_state == QLCNIC_DEV_QUISCENT) {
2408 qlcnic_api_unlock(adapter);
2409 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2410 FW_POLL_DELAY * 2);
2411 return;
2412 }
2413
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002414 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
2415 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2416 if (npar_state == QLCNIC_DEV_NPAR_RDY) {
2417 qlcnic_api_unlock(adapter);
2418 goto wait_npar;
2419 } else {
2420 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2421 FW_POLL_DELAY);
2422 qlcnic_api_unlock(adapter);
2423 return;
2424 }
2425 }
2426
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002427 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2428 dev_err(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2429 adapter->reset_ack_timeo);
2430 goto skip_ack_check;
2431 }
2432
2433 if (!qlcnic_check_drv_state(adapter)) {
2434skip_ack_check:
2435 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002436
2437 if (dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2438 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2439 QLCNIC_DEV_QUISCENT);
2440 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2441 FW_POLL_DELAY * 2);
2442 QLCDB(adapter, DRV, "Quiscing the driver\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002443 qlcnic_idc_debug_info(adapter, 0);
2444
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002445 qlcnic_api_unlock(adapter);
2446 return;
2447 }
2448
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002449 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2450 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2451 QLCNIC_DEV_INITIALIZING);
2452 set_bit(__QLCNIC_START_FW, &adapter->state);
2453 QLCDB(adapter, DRV, "Restarting fw\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002454 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002455 }
2456
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002457 qlcnic_api_unlock(adapter);
2458
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002459 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002460 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2461 return;
2462 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002463 goto err_ret;
2464 }
2465
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002466 qlcnic_api_unlock(adapter);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002467
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002468wait_npar:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002469 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002470 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002471
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002472 switch (dev_state) {
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002473 case QLCNIC_DEV_QUISCENT:
2474 case QLCNIC_DEV_NEED_QUISCENT:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002475 case QLCNIC_DEV_NEED_RESET:
2476 qlcnic_schedule_work(adapter,
2477 qlcnic_fwinit_work, FW_POLL_DELAY);
2478 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002479 case QLCNIC_DEV_FAILED:
2480 break;
2481
2482 default:
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002483 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002484 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2485 return;
2486 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002487 }
2488
2489err_ret:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002490 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2491 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002492 netif_device_attach(adapter->netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002493 qlcnic_clr_all_drv_state(adapter);
2494}
2495
2496static void
2497qlcnic_detach_work(struct work_struct *work)
2498{
2499 struct qlcnic_adapter *adapter = container_of(work,
2500 struct qlcnic_adapter, fw_work.work);
2501 struct net_device *netdev = adapter->netdev;
2502 u32 status;
2503
2504 netif_device_detach(netdev);
2505
2506 qlcnic_down(adapter, netdev);
2507
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002508 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2509
2510 if (status & QLCNIC_RCODE_FATAL_ERROR)
2511 goto err_ret;
2512
2513 if (adapter->temp == QLCNIC_TEMP_PANIC)
2514 goto err_ret;
2515
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002516 if (qlcnic_set_drv_state(adapter, adapter->dev_state))
2517 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002518
2519 adapter->fw_wait_cnt = 0;
2520
2521 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2522
2523 return;
2524
2525err_ret:
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002526 dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n",
2527 status, adapter->temp);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002528 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002529 qlcnic_clr_all_drv_state(adapter);
2530
2531}
2532
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002533/*Transit to RESET state from READY state only */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002534static void
2535qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
2536{
2537 u32 state;
2538
Anirban Chakrabortycea89752010-07-13 20:33:35 +00002539 adapter->need_fw_reset = 1;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002540 if (qlcnic_api_lock(adapter))
2541 return;
2542
2543 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2544
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002545 if (state == QLCNIC_DEV_READY) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002546 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002547 QLCDB(adapter, DRV, "NEED_RESET state set\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002548 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002549 }
2550
2551 qlcnic_api_unlock(adapter);
2552}
2553
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002554/* Transit to NPAR READY state from NPAR NOT READY state */
2555static void
2556qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
2557{
2558 u32 state;
2559
Anirban Chakrabortycea89752010-07-13 20:33:35 +00002560 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
2561 adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
2562 return;
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002563 if (qlcnic_api_lock(adapter))
2564 return;
2565
2566 state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2567
2568 if (state != QLCNIC_DEV_NPAR_RDY) {
2569 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
2570 QLCNIC_DEV_NPAR_RDY);
2571 QLCDB(adapter, DRV, "NPAR READY state set\n");
2572 }
2573
2574 qlcnic_api_unlock(adapter);
2575}
2576
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002577static void
2578qlcnic_schedule_work(struct qlcnic_adapter *adapter,
2579 work_func_t func, int delay)
2580{
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00002581 if (test_bit(__QLCNIC_AER, &adapter->state))
2582 return;
2583
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002584 INIT_DELAYED_WORK(&adapter->fw_work, func);
2585 schedule_delayed_work(&adapter->fw_work, round_jiffies_relative(delay));
2586}
2587
2588static void
2589qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
2590{
2591 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2592 msleep(10);
2593
2594 cancel_delayed_work_sync(&adapter->fw_work);
2595}
2596
2597static void
2598qlcnic_attach_work(struct work_struct *work)
2599{
2600 struct qlcnic_adapter *adapter = container_of(work,
2601 struct qlcnic_adapter, fw_work.work);
2602 struct net_device *netdev = adapter->netdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002603
2604 if (netif_running(netdev)) {
Amit Kumar Salecha52486a32010-06-22 03:19:02 +00002605 if (qlcnic_up(adapter, netdev))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002606 goto done;
2607
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002608 qlcnic_config_indev_addr(netdev, NETDEV_UP);
2609 }
2610
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002611done:
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002612 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002613 adapter->fw_fail_cnt = 0;
2614 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002615
2616 if (!qlcnic_clr_drv_state(adapter))
2617 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2618 FW_POLL_DELAY);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002619}
2620
2621static int
2622qlcnic_check_health(struct qlcnic_adapter *adapter)
2623{
2624 u32 state = 0, heartbit;
2625 struct net_device *netdev = adapter->netdev;
2626
2627 if (qlcnic_check_temp(adapter))
2628 goto detach;
2629
Amit Kumar Salecha2372a5f2010-05-13 03:07:42 +00002630 if (adapter->need_fw_reset)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002631 qlcnic_dev_request_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002632
2633 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2634 if (state == QLCNIC_DEV_NEED_RESET || state == QLCNIC_DEV_NEED_QUISCENT)
2635 adapter->need_fw_reset = 1;
2636
2637 heartbit = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
2638 if (heartbit != adapter->heartbit) {
2639 adapter->heartbit = heartbit;
2640 adapter->fw_fail_cnt = 0;
2641 if (adapter->need_fw_reset)
2642 goto detach;
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00002643
Amit Kumar Salecha0df170b2010-07-13 20:33:32 +00002644 if (adapter->reset_context &&
2645 auto_fw_reset == AUTO_FW_RESET_ENABLED) {
Amit Kumar Salecha68bf1c62010-06-22 03:19:03 +00002646 qlcnic_reset_hw_context(adapter);
2647 adapter->netdev->trans_start = jiffies;
2648 }
2649
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002650 return 0;
2651 }
2652
2653 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2654 return 0;
2655
2656 qlcnic_dev_request_reset(adapter);
2657
Amit Kumar Salecha0df170b2010-07-13 20:33:32 +00002658 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED))
2659 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002660
2661 dev_info(&netdev->dev, "firmware hang detected\n");
2662
2663detach:
2664 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
2665 QLCNIC_DEV_NEED_RESET;
2666
2667 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002668 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
2669
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002670 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002671 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
2672 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002673
2674 return 1;
2675}
2676
2677static void
2678qlcnic_fw_poll_work(struct work_struct *work)
2679{
2680 struct qlcnic_adapter *adapter = container_of(work,
2681 struct qlcnic_adapter, fw_work.work);
2682
2683 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2684 goto reschedule;
2685
2686
2687 if (qlcnic_check_health(adapter))
2688 return;
2689
2690reschedule:
2691 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
2692}
2693
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00002694static int qlcnic_is_first_func(struct pci_dev *pdev)
2695{
2696 struct pci_dev *oth_pdev;
2697 int val = pdev->devfn;
2698
2699 while (val-- > 0) {
2700 oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
2701 (pdev->bus), pdev->bus->number,
2702 PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07002703 if (!oth_pdev)
2704 continue;
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00002705
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07002706 if (oth_pdev->current_state != PCI_D3cold) {
2707 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00002708 return 0;
Amit Kumar Salechabfc978f2010-07-18 14:51:59 -07002709 }
2710 pci_dev_put(oth_pdev);
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00002711 }
2712 return 1;
2713}
2714
2715static int qlcnic_attach_func(struct pci_dev *pdev)
2716{
2717 int err, first_func;
2718 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
2719 struct net_device *netdev = adapter->netdev;
2720
2721 pdev->error_state = pci_channel_io_normal;
2722
2723 err = pci_enable_device(pdev);
2724 if (err)
2725 return err;
2726
2727 pci_set_power_state(pdev, PCI_D0);
2728 pci_set_master(pdev);
2729 pci_restore_state(pdev);
2730
2731 first_func = qlcnic_is_first_func(pdev);
2732
2733 if (qlcnic_api_lock(adapter))
2734 return -EINVAL;
2735
2736 if (first_func) {
2737 adapter->need_fw_reset = 1;
2738 set_bit(__QLCNIC_START_FW, &adapter->state);
2739 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
2740 QLCDB(adapter, DRV, "Restarting fw\n");
2741 }
2742 qlcnic_api_unlock(adapter);
2743
2744 err = adapter->nic_ops->start_firmware(adapter);
2745 if (err)
2746 return err;
2747
2748 qlcnic_clr_drv_state(adapter);
2749 qlcnic_setup_intr(adapter);
2750
2751 if (netif_running(netdev)) {
2752 err = qlcnic_attach(adapter);
2753 if (err) {
2754 qlcnic_clr_all_drv_state(adapter);
2755 clear_bit(__QLCNIC_AER, &adapter->state);
2756 netif_device_attach(netdev);
2757 return err;
2758 }
2759
2760 err = qlcnic_up(adapter, netdev);
2761 if (err)
2762 goto done;
2763
2764 qlcnic_config_indev_addr(netdev, NETDEV_UP);
2765 }
2766 done:
2767 netif_device_attach(netdev);
2768 return err;
2769}
2770
2771static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
2772 pci_channel_state_t state)
2773{
2774 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
2775 struct net_device *netdev = adapter->netdev;
2776
2777 if (state == pci_channel_io_perm_failure)
2778 return PCI_ERS_RESULT_DISCONNECT;
2779
2780 if (state == pci_channel_io_normal)
2781 return PCI_ERS_RESULT_RECOVERED;
2782
2783 set_bit(__QLCNIC_AER, &adapter->state);
2784 netif_device_detach(netdev);
2785
2786 cancel_delayed_work_sync(&adapter->fw_work);
2787
2788 if (netif_running(netdev))
2789 qlcnic_down(adapter, netdev);
2790
2791 qlcnic_detach(adapter);
2792 qlcnic_teardown_intr(adapter);
2793
2794 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2795
2796 pci_save_state(pdev);
2797 pci_disable_device(pdev);
2798
2799 return PCI_ERS_RESULT_NEED_RESET;
2800}
2801
2802static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
2803{
2804 return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
2805 PCI_ERS_RESULT_RECOVERED;
2806}
2807
2808static void qlcnic_io_resume(struct pci_dev *pdev)
2809{
2810 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
2811
2812 pci_cleanup_aer_uncorrect_error_status(pdev);
2813
2814 if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
2815 test_and_clear_bit(__QLCNIC_AER, &adapter->state))
2816 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2817 FW_POLL_DELAY);
2818}
2819
2820
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00002821static int
2822qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
2823{
2824 int err;
2825
2826 err = qlcnic_can_start_firmware(adapter);
2827 if (err)
2828 return err;
2829
2830 qlcnic_check_options(adapter);
2831
2832 adapter->need_fw_reset = 0;
2833
2834 return err;
2835}
2836
2837static int
2838qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
2839{
2840 return -EOPNOTSUPP;
2841}
2842
2843static int
2844qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
2845{
2846 return -EOPNOTSUPP;
2847}
2848
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002849static ssize_t
2850qlcnic_store_bridged_mode(struct device *dev,
2851 struct device_attribute *attr, const char *buf, size_t len)
2852{
2853 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2854 unsigned long new;
2855 int ret = -EINVAL;
2856
2857 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
2858 goto err_out;
2859
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00002860 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002861 goto err_out;
2862
2863 if (strict_strtoul(buf, 2, &new))
2864 goto err_out;
2865
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00002866 if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002867 ret = len;
2868
2869err_out:
2870 return ret;
2871}
2872
2873static ssize_t
2874qlcnic_show_bridged_mode(struct device *dev,
2875 struct device_attribute *attr, char *buf)
2876{
2877 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2878 int bridged_mode = 0;
2879
2880 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
2881 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
2882
2883 return sprintf(buf, "%d\n", bridged_mode);
2884}
2885
2886static struct device_attribute dev_attr_bridged_mode = {
2887 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2888 .show = qlcnic_show_bridged_mode,
2889 .store = qlcnic_store_bridged_mode,
2890};
2891
2892static ssize_t
2893qlcnic_store_diag_mode(struct device *dev,
2894 struct device_attribute *attr, const char *buf, size_t len)
2895{
2896 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2897 unsigned long new;
2898
2899 if (strict_strtoul(buf, 2, &new))
2900 return -EINVAL;
2901
2902 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
2903 adapter->flags ^= QLCNIC_DIAG_ENABLED;
2904
2905 return len;
2906}
2907
2908static ssize_t
2909qlcnic_show_diag_mode(struct device *dev,
2910 struct device_attribute *attr, char *buf)
2911{
2912 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2913
2914 return sprintf(buf, "%d\n",
2915 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
2916}
2917
2918static struct device_attribute dev_attr_diag_mode = {
2919 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
2920 .show = qlcnic_show_diag_mode,
2921 .store = qlcnic_store_diag_mode,
2922};
2923
2924static int
2925qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
2926 loff_t offset, size_t size)
2927{
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002928 size_t crb_size = 4;
2929
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002930 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
2931 return -EIO;
2932
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002933 if (offset < QLCNIC_PCI_CRBSPACE) {
2934 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
2935 QLCNIC_PCI_CAMQM_END))
2936 crb_size = 8;
2937 else
2938 return -EINVAL;
2939 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002940
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002941 if ((size != crb_size) || (offset & (crb_size-1)))
2942 return -EINVAL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002943
2944 return 0;
2945}
2946
2947static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07002948qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
2949 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002950 char *buf, loff_t offset, size_t size)
2951{
2952 struct device *dev = container_of(kobj, struct device, kobj);
2953 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2954 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002955 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002956 int ret;
2957
2958 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
2959 if (ret != 0)
2960 return ret;
2961
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002962 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
2963 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
2964 memcpy(buf, &qmdata, size);
2965 } else {
2966 data = QLCRD32(adapter, offset);
2967 memcpy(buf, &data, size);
2968 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002969 return size;
2970}
2971
2972static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07002973qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
2974 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002975 char *buf, loff_t offset, size_t size)
2976{
2977 struct device *dev = container_of(kobj, struct device, kobj);
2978 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2979 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002980 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002981 int ret;
2982
2983 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
2984 if (ret != 0)
2985 return ret;
2986
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002987 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
2988 memcpy(&qmdata, buf, size);
2989 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
2990 } else {
2991 memcpy(&data, buf, size);
2992 QLCWR32(adapter, offset, data);
2993 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002994 return size;
2995}
2996
2997static int
2998qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
2999 loff_t offset, size_t size)
3000{
3001 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
3002 return -EIO;
3003
3004 if ((size != 8) || (offset & 0x7))
3005 return -EIO;
3006
3007 return 0;
3008}
3009
3010static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003011qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
3012 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003013 char *buf, loff_t offset, size_t size)
3014{
3015 struct device *dev = container_of(kobj, struct device, kobj);
3016 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3017 u64 data;
3018 int ret;
3019
3020 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3021 if (ret != 0)
3022 return ret;
3023
3024 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
3025 return -EIO;
3026
3027 memcpy(buf, &data, size);
3028
3029 return size;
3030}
3031
3032static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07003033qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
3034 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003035 char *buf, loff_t offset, size_t size)
3036{
3037 struct device *dev = container_of(kobj, struct device, kobj);
3038 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3039 u64 data;
3040 int ret;
3041
3042 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
3043 if (ret != 0)
3044 return ret;
3045
3046 memcpy(&data, buf, size);
3047
3048 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
3049 return -EIO;
3050
3051 return size;
3052}
3053
3054
3055static struct bin_attribute bin_attr_crb = {
3056 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
3057 .size = 0,
3058 .read = qlcnic_sysfs_read_crb,
3059 .write = qlcnic_sysfs_write_crb,
3060};
3061
3062static struct bin_attribute bin_attr_mem = {
3063 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
3064 .size = 0,
3065 .read = qlcnic_sysfs_read_mem,
3066 .write = qlcnic_sysfs_write_mem,
3067};
3068
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003069static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003070validate_pm_config(struct qlcnic_adapter *adapter,
3071 struct qlcnic_pm_func_cfg *pm_cfg, int count)
3072{
3073
3074 u8 src_pci_func, s_esw_id, d_esw_id;
3075 u8 dest_pci_func;
3076 int i;
3077
3078 for (i = 0; i < count; i++) {
3079 src_pci_func = pm_cfg[i].pci_func;
3080 dest_pci_func = pm_cfg[i].dest_npar;
3081 if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
3082 || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
3083 return QL_STATUS_INVALID_PARAM;
3084
3085 if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
3086 return QL_STATUS_INVALID_PARAM;
3087
3088 if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
3089 return QL_STATUS_INVALID_PARAM;
3090
3091 if (!IS_VALID_MODE(pm_cfg[i].action))
3092 return QL_STATUS_INVALID_PARAM;
3093
3094 s_esw_id = adapter->npars[src_pci_func].phy_port;
3095 d_esw_id = adapter->npars[dest_pci_func].phy_port;
3096
3097 if (s_esw_id != d_esw_id)
3098 return QL_STATUS_INVALID_PARAM;
3099
3100 }
3101 return 0;
3102
3103}
3104
3105static ssize_t
3106qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
3107 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3108{
3109 struct device *dev = container_of(kobj, struct device, kobj);
3110 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3111 struct qlcnic_pm_func_cfg *pm_cfg;
3112 u32 id, action, pci_func;
3113 int count, rem, i, ret;
3114
3115 count = size / sizeof(struct qlcnic_pm_func_cfg);
3116 rem = size % sizeof(struct qlcnic_pm_func_cfg);
3117 if (rem)
3118 return QL_STATUS_INVALID_PARAM;
3119
3120 pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
3121
3122 ret = validate_pm_config(adapter, pm_cfg, count);
3123 if (ret)
3124 return ret;
3125 for (i = 0; i < count; i++) {
3126 pci_func = pm_cfg[i].pci_func;
3127 action = pm_cfg[i].action;
3128 id = adapter->npars[pci_func].phy_port;
3129 ret = qlcnic_config_port_mirroring(adapter, id,
3130 action, pci_func);
3131 if (ret)
3132 return ret;
3133 }
3134
3135 for (i = 0; i < count; i++) {
3136 pci_func = pm_cfg[i].pci_func;
3137 id = adapter->npars[pci_func].phy_port;
3138 adapter->npars[pci_func].enable_pm = pm_cfg[i].action;
3139 adapter->npars[pci_func].dest_npar = id;
3140 }
3141 return size;
3142}
3143
3144static ssize_t
3145qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
3146 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3147{
3148 struct device *dev = container_of(kobj, struct device, kobj);
3149 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3150 struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
3151 int i;
3152
3153 if (size != sizeof(pm_cfg))
3154 return QL_STATUS_INVALID_PARAM;
3155
3156 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3157 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3158 continue;
3159 pm_cfg[i].action = adapter->npars[i].enable_pm;
3160 pm_cfg[i].dest_npar = 0;
3161 pm_cfg[i].pci_func = i;
3162 }
3163 memcpy(buf, &pm_cfg, size);
3164
3165 return size;
3166}
3167
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003168static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003169validate_esw_config(struct qlcnic_adapter *adapter,
3170 struct qlcnic_esw_func_cfg *esw_cfg, int count)
3171{
3172 u8 pci_func;
3173 int i;
3174
3175 for (i = 0; i < count; i++) {
3176 pci_func = esw_cfg[i].pci_func;
3177 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3178 return QL_STATUS_INVALID_PARAM;
3179
3180 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3181 return QL_STATUS_INVALID_PARAM;
3182
3183 if (esw_cfg->host_vlan_tag == 1)
3184 if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
3185 return QL_STATUS_INVALID_PARAM;
3186
3187 if (!IS_VALID_MODE(esw_cfg[i].promisc_mode)
3188 || !IS_VALID_MODE(esw_cfg[i].host_vlan_tag)
3189 || !IS_VALID_MODE(esw_cfg[i].mac_learning)
3190 || !IS_VALID_MODE(esw_cfg[i].discard_tagged))
3191 return QL_STATUS_INVALID_PARAM;
3192 }
3193
3194 return 0;
3195}
3196
3197static ssize_t
3198qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
3199 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3200{
3201 struct device *dev = container_of(kobj, struct device, kobj);
3202 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3203 struct qlcnic_esw_func_cfg *esw_cfg;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003204 int count, rem, i, ret;
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003205 u8 id, pci_func;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003206
3207 count = size / sizeof(struct qlcnic_esw_func_cfg);
3208 rem = size % sizeof(struct qlcnic_esw_func_cfg);
3209 if (rem)
3210 return QL_STATUS_INVALID_PARAM;
3211
3212 esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
3213 ret = validate_esw_config(adapter, esw_cfg, count);
3214 if (ret)
3215 return ret;
3216
3217 for (i = 0; i < count; i++) {
3218 pci_func = esw_cfg[i].pci_func;
3219 id = adapter->npars[pci_func].phy_port;
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003220 ret = qlcnic_config_switch_port(adapter, id,
3221 esw_cfg[i].host_vlan_tag,
3222 esw_cfg[i].discard_tagged,
3223 esw_cfg[i].promisc_mode,
3224 esw_cfg[i].mac_learning,
3225 esw_cfg[i].pci_func,
3226 esw_cfg[i].vlan_id);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003227 if (ret)
3228 return ret;
3229 }
3230
3231 for (i = 0; i < count; i++) {
3232 pci_func = esw_cfg[i].pci_func;
3233 adapter->npars[pci_func].promisc_mode = esw_cfg[i].promisc_mode;
3234 adapter->npars[pci_func].mac_learning = esw_cfg[i].mac_learning;
3235 adapter->npars[pci_func].vlan_id = esw_cfg[i].vlan_id;
3236 adapter->npars[pci_func].discard_tagged =
3237 esw_cfg[i].discard_tagged;
3238 adapter->npars[pci_func].host_vlan_tag =
3239 esw_cfg[i].host_vlan_tag;
3240 }
3241
3242 return size;
3243}
3244
3245static ssize_t
3246qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
3247 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3248{
3249 struct device *dev = container_of(kobj, struct device, kobj);
3250 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3251 struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
3252 int i;
3253
3254 if (size != sizeof(esw_cfg))
3255 return QL_STATUS_INVALID_PARAM;
3256
3257 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
3258 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3259 continue;
3260
3261 esw_cfg[i].host_vlan_tag = adapter->npars[i].host_vlan_tag;
3262 esw_cfg[i].promisc_mode = adapter->npars[i].promisc_mode;
3263 esw_cfg[i].discard_tagged = adapter->npars[i].discard_tagged;
3264 esw_cfg[i].vlan_id = adapter->npars[i].vlan_id;
3265 esw_cfg[i].mac_learning = adapter->npars[i].mac_learning;
3266 }
3267 memcpy(buf, &esw_cfg, size);
3268
3269 return size;
3270}
3271
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003272static int
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003273validate_npar_config(struct qlcnic_adapter *adapter,
3274 struct qlcnic_npar_func_cfg *np_cfg, int count)
3275{
3276 u8 pci_func, i;
3277
3278 for (i = 0; i < count; i++) {
3279 pci_func = np_cfg[i].pci_func;
3280 if (pci_func >= QLCNIC_MAX_PCI_FUNC)
3281 return QL_STATUS_INVALID_PARAM;
3282
3283 if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
3284 return QL_STATUS_INVALID_PARAM;
3285
3286 if (!IS_VALID_BW(np_cfg[i].min_bw)
3287 || !IS_VALID_BW(np_cfg[i].max_bw)
3288 || !IS_VALID_RX_QUEUES(np_cfg[i].max_rx_queues)
3289 || !IS_VALID_TX_QUEUES(np_cfg[i].max_tx_queues))
3290 return QL_STATUS_INVALID_PARAM;
3291 }
3292 return 0;
3293}
3294
3295static ssize_t
3296qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
3297 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3298{
3299 struct device *dev = container_of(kobj, struct device, kobj);
3300 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3301 struct qlcnic_info nic_info;
3302 struct qlcnic_npar_func_cfg *np_cfg;
3303 int i, count, rem, ret;
3304 u8 pci_func;
3305
3306 count = size / sizeof(struct qlcnic_npar_func_cfg);
3307 rem = size % sizeof(struct qlcnic_npar_func_cfg);
3308 if (rem)
3309 return QL_STATUS_INVALID_PARAM;
3310
3311 np_cfg = (struct qlcnic_npar_func_cfg *) buf;
3312 ret = validate_npar_config(adapter, np_cfg, count);
3313 if (ret)
3314 return ret;
3315
3316 for (i = 0; i < count ; i++) {
3317 pci_func = np_cfg[i].pci_func;
3318 ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
3319 if (ret)
3320 return ret;
3321 nic_info.pci_func = pci_func;
3322 nic_info.min_tx_bw = np_cfg[i].min_bw;
3323 nic_info.max_tx_bw = np_cfg[i].max_bw;
3324 ret = qlcnic_set_nic_info(adapter, &nic_info);
3325 if (ret)
3326 return ret;
Anirban Chakrabortycea89752010-07-13 20:33:35 +00003327 adapter->npars[i].min_bw = nic_info.min_tx_bw;
3328 adapter->npars[i].max_bw = nic_info.max_tx_bw;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003329 }
3330
3331 return size;
3332
3333}
3334static ssize_t
3335qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
3336 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3337{
3338 struct device *dev = container_of(kobj, struct device, kobj);
3339 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3340 struct qlcnic_info nic_info;
3341 struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
3342 int i, ret;
3343
3344 if (size != sizeof(np_cfg))
3345 return QL_STATUS_INVALID_PARAM;
3346
3347 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3348 if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
3349 continue;
3350 ret = qlcnic_get_nic_info(adapter, &nic_info, i);
3351 if (ret)
3352 return ret;
3353
3354 np_cfg[i].pci_func = i;
3355 np_cfg[i].op_mode = nic_info.op_mode;
3356 np_cfg[i].port_num = nic_info.phys_port;
3357 np_cfg[i].fw_capab = nic_info.capabilities;
3358 np_cfg[i].min_bw = nic_info.min_tx_bw ;
3359 np_cfg[i].max_bw = nic_info.max_tx_bw;
3360 np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
3361 np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
3362 }
3363 memcpy(buf, &np_cfg, size);
3364 return size;
3365}
3366
3367static ssize_t
3368qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
3369 struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
3370{
3371 struct device *dev = container_of(kobj, struct device, kobj);
3372 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
3373 struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
Dan Carpentere88db3b2010-08-09 21:49:36 +00003374 struct qlcnic_pci_info *pci_info;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003375 int i, ret;
3376
3377 if (size != sizeof(pci_cfg))
3378 return QL_STATUS_INVALID_PARAM;
3379
Dan Carpentere88db3b2010-08-09 21:49:36 +00003380 pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
3381 if (!pci_info)
3382 return -ENOMEM;
3383
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003384 ret = qlcnic_get_pci_info(adapter, pci_info);
Dan Carpentere88db3b2010-08-09 21:49:36 +00003385 if (ret) {
3386 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003387 return ret;
Dan Carpentere88db3b2010-08-09 21:49:36 +00003388 }
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003389
3390 for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
3391 pci_cfg[i].pci_func = pci_info[i].id;
3392 pci_cfg[i].func_type = pci_info[i].type;
3393 pci_cfg[i].port_num = pci_info[i].default_port;
3394 pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
3395 pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
3396 memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
3397 }
3398 memcpy(buf, &pci_cfg, size);
Dan Carpentere88db3b2010-08-09 21:49:36 +00003399 kfree(pci_info);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003400 return size;
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003401}
3402static struct bin_attribute bin_attr_npar_config = {
3403 .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
3404 .size = 0,
3405 .read = qlcnic_sysfs_read_npar_config,
3406 .write = qlcnic_sysfs_write_npar_config,
3407};
3408
3409static struct bin_attribute bin_attr_pci_config = {
3410 .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
3411 .size = 0,
3412 .read = qlcnic_sysfs_read_pci_config,
3413 .write = NULL,
3414};
3415
3416static struct bin_attribute bin_attr_esw_config = {
3417 .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
3418 .size = 0,
3419 .read = qlcnic_sysfs_read_esw_config,
3420 .write = qlcnic_sysfs_write_esw_config,
3421};
3422
3423static struct bin_attribute bin_attr_pm_config = {
3424 .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
3425 .size = 0,
3426 .read = qlcnic_sysfs_read_pm_config,
3427 .write = qlcnic_sysfs_write_pm_config,
3428};
3429
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003430static void
3431qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
3432{
3433 struct device *dev = &adapter->pdev->dev;
3434
3435 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3436 if (device_create_file(dev, &dev_attr_bridged_mode))
3437 dev_warn(dev,
3438 "failed to create bridged_mode sysfs entry\n");
3439}
3440
3441static void
3442qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
3443{
3444 struct device *dev = &adapter->pdev->dev;
3445
3446 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
3447 device_remove_file(dev, &dev_attr_bridged_mode);
3448}
3449
3450static void
3451qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
3452{
3453 struct device *dev = &adapter->pdev->dev;
3454
Anirban Chakraborty132ff002010-07-09 13:15:05 +00003455 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
3456 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003457 if (device_create_file(dev, &dev_attr_diag_mode))
3458 dev_info(dev, "failed to create diag_mode sysfs entry\n");
3459 if (device_create_bin_file(dev, &bin_attr_crb))
3460 dev_info(dev, "failed to create crb sysfs entry\n");
3461 if (device_create_bin_file(dev, &bin_attr_mem))
3462 dev_info(dev, "failed to create mem sysfs entry\n");
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003463 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
3464 adapter->op_mode != QLCNIC_MGMT_FUNC)
3465 return;
3466 if (device_create_bin_file(dev, &bin_attr_pci_config))
3467 dev_info(dev, "failed to create pci config sysfs entry");
3468 if (device_create_bin_file(dev, &bin_attr_npar_config))
3469 dev_info(dev, "failed to create npar config sysfs entry");
3470 if (device_create_bin_file(dev, &bin_attr_esw_config))
3471 dev_info(dev, "failed to create esw config sysfs entry");
3472 if (device_create_bin_file(dev, &bin_attr_pm_config))
3473 dev_info(dev, "failed to create pm config sysfs entry");
3474
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003475}
3476
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003477static void
3478qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
3479{
3480 struct device *dev = &adapter->pdev->dev;
3481
Anirban Chakraborty132ff002010-07-09 13:15:05 +00003482 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
3483 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003484 device_remove_file(dev, &dev_attr_diag_mode);
3485 device_remove_bin_file(dev, &bin_attr_crb);
3486 device_remove_bin_file(dev, &bin_attr_mem);
Rajesh K Borundia346fe762010-06-29 08:01:20 +00003487 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
3488 adapter->op_mode != QLCNIC_MGMT_FUNC)
3489 return;
3490 device_remove_bin_file(dev, &bin_attr_pci_config);
3491 device_remove_bin_file(dev, &bin_attr_npar_config);
3492 device_remove_bin_file(dev, &bin_attr_esw_config);
3493 device_remove_bin_file(dev, &bin_attr_pm_config);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003494}
3495
3496#ifdef CONFIG_INET
3497
3498#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
3499
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003500static void
3501qlcnic_config_indev_addr(struct net_device *dev, unsigned long event)
3502{
3503 struct in_device *indev;
3504 struct qlcnic_adapter *adapter = netdev_priv(dev);
3505
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003506 indev = in_dev_get(dev);
3507 if (!indev)
3508 return;
3509
3510 for_ifa(indev) {
3511 switch (event) {
3512 case NETDEV_UP:
3513 qlcnic_config_ipaddr(adapter,
3514 ifa->ifa_address, QLCNIC_IP_UP);
3515 break;
3516 case NETDEV_DOWN:
3517 qlcnic_config_ipaddr(adapter,
3518 ifa->ifa_address, QLCNIC_IP_DOWN);
3519 break;
3520 default:
3521 break;
3522 }
3523 } endfor_ifa(indev);
3524
3525 in_dev_put(indev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003526}
3527
3528static int qlcnic_netdev_event(struct notifier_block *this,
3529 unsigned long event, void *ptr)
3530{
3531 struct qlcnic_adapter *adapter;
3532 struct net_device *dev = (struct net_device *)ptr;
3533
3534recheck:
3535 if (dev == NULL)
3536 goto done;
3537
3538 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3539 dev = vlan_dev_real_dev(dev);
3540 goto recheck;
3541 }
3542
3543 if (!is_qlcnic_netdev(dev))
3544 goto done;
3545
3546 adapter = netdev_priv(dev);
3547
3548 if (!adapter)
3549 goto done;
3550
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00003551 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003552 goto done;
3553
3554 qlcnic_config_indev_addr(dev, event);
3555done:
3556 return NOTIFY_DONE;
3557}
3558
3559static int
3560qlcnic_inetaddr_event(struct notifier_block *this,
3561 unsigned long event, void *ptr)
3562{
3563 struct qlcnic_adapter *adapter;
3564 struct net_device *dev;
3565
3566 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
3567
3568 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
3569
3570recheck:
3571 if (dev == NULL || !netif_running(dev))
3572 goto done;
3573
3574 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3575 dev = vlan_dev_real_dev(dev);
3576 goto recheck;
3577 }
3578
3579 if (!is_qlcnic_netdev(dev))
3580 goto done;
3581
3582 adapter = netdev_priv(dev);
3583
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +00003584 if (!adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003585 goto done;
3586
Amit Kumar Salecha8a15ad12010-06-22 03:19:01 +00003587 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003588 goto done;
3589
3590 switch (event) {
3591 case NETDEV_UP:
3592 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
3593 break;
3594 case NETDEV_DOWN:
3595 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
3596 break;
3597 default:
3598 break;
3599 }
3600
3601done:
3602 return NOTIFY_DONE;
3603}
3604
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003605static struct notifier_block qlcnic_netdev_cb = {
3606 .notifier_call = qlcnic_netdev_event,
3607};
3608
3609static struct notifier_block qlcnic_inetaddr_cb = {
3610 .notifier_call = qlcnic_inetaddr_event,
3611};
3612#else
3613static void
3614qlcnic_config_indev_addr(struct net_device *dev, unsigned long event)
3615{ }
3616#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003617static struct pci_error_handlers qlcnic_err_handler = {
3618 .error_detected = qlcnic_io_error_detected,
3619 .slot_reset = qlcnic_io_slot_reset,
3620 .resume = qlcnic_io_resume,
3621};
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003622
3623static struct pci_driver qlcnic_driver = {
3624 .name = qlcnic_driver_name,
3625 .id_table = qlcnic_pci_tbl,
3626 .probe = qlcnic_probe,
3627 .remove = __devexit_p(qlcnic_remove),
3628#ifdef CONFIG_PM
3629 .suspend = qlcnic_suspend,
3630 .resume = qlcnic_resume,
3631#endif
Sucheta Chakraborty451724c2010-07-13 20:33:34 +00003632 .shutdown = qlcnic_shutdown,
3633 .err_handler = &qlcnic_err_handler
3634
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003635};
3636
3637static int __init qlcnic_init_module(void)
3638{
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00003639 int ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003640
3641 printk(KERN_INFO "%s\n", qlcnic_driver_string);
3642
3643#ifdef CONFIG_INET
3644 register_netdevice_notifier(&qlcnic_netdev_cb);
3645 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
3646#endif
3647
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00003648 ret = pci_register_driver(&qlcnic_driver);
3649 if (ret) {
3650#ifdef CONFIG_INET
3651 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
3652 unregister_netdevice_notifier(&qlcnic_netdev_cb);
3653#endif
3654 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003655
Amit Kumar Salecha0cf3a142010-07-13 20:33:33 +00003656 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00003657}
3658
3659module_init(qlcnic_init_module);
3660
3661static void __exit qlcnic_exit_module(void)
3662{
3663
3664 pci_unregister_driver(&qlcnic_driver);
3665
3666#ifdef CONFIG_INET
3667 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
3668 unregister_netdevice_notifier(&qlcnic_netdev_cb);
3669#endif
3670}
3671
3672module_exit(qlcnic_exit_module);