blob: 28ed28c1cbccbe695da2310aebe98612263e4fdb [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>
37
Sritej Velaga7f9a0c32010-06-17 02:56:39 +000038MODULE_DESCRIPTION("QLogic 1/10 GbE Converged/Intelligent Ethernet Driver");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000039MODULE_LICENSE("GPL");
40MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
41MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
42
43char qlcnic_driver_name[] = "qlcnic";
Sritej Velaga7f9a0c32010-06-17 02:56:39 +000044static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
45 "Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000046
47static int port_mode = QLCNIC_PORT_MODE_AUTO_NEG;
48
49/* Default to restricted 1G auto-neg mode */
50static int wol_port_mode = 5;
51
52static int use_msi = 1;
53module_param(use_msi, int, 0644);
54MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
55
56static int use_msi_x = 1;
57module_param(use_msi_x, int, 0644);
58MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
59
60static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
61module_param(auto_fw_reset, int, 0644);
62MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
63
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +000064static int load_fw_file;
65module_param(load_fw_file, int, 0644);
66MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file");
67
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +000068static int qlcnic_config_npars;
69module_param(qlcnic_config_npars, int, 0644);
70MODULE_PARM_DESC(qlcnic_config_npars, "Configure NPARs (0=disabled, 1=enabled");
71
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000072static int __devinit qlcnic_probe(struct pci_dev *pdev,
73 const struct pci_device_id *ent);
74static void __devexit qlcnic_remove(struct pci_dev *pdev);
75static int qlcnic_open(struct net_device *netdev);
76static int qlcnic_close(struct net_device *netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000077static void qlcnic_tx_timeout(struct net_device *netdev);
78static void qlcnic_tx_timeout_task(struct work_struct *work);
79static 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);
86#ifdef CONFIG_NET_POLL_CONTROLLER
87static void qlcnic_poll_controller(struct net_device *netdev);
88#endif
89
90static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter);
91static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter);
92static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter);
93static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter);
94
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +000095static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +000096static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter);
97static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
98
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +000099static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000100static irqreturn_t qlcnic_intr(int irq, void *data);
101static irqreturn_t qlcnic_msi_intr(int irq, void *data);
102static irqreturn_t qlcnic_msix_intr(int irq, void *data);
103
104static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
105static void qlcnic_config_indev_addr(struct net_device *dev, unsigned long);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000106static int qlcnic_start_firmware(struct qlcnic_adapter *);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000107
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000108static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
109static void qlcnicvf_clear_ilb_mode(struct qlcnic_adapter *);
110static int qlcnicvf_set_ilb_mode(struct qlcnic_adapter *);
111static int qlcnicvf_config_led(struct qlcnic_adapter *, u32, u32);
112static int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *, u32);
113static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000114/* PCI Device ID Table */
115#define ENTRY(device) \
116 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
117 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
118
119#define PCI_DEVICE_ID_QLOGIC_QLE824X 0x8020
120
Amit Kumar Salecha6a902882010-02-01 05:24:54 +0000121static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000122 ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
123 {0,}
124};
125
126MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
127
128
129void
130qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
131 struct qlcnic_host_tx_ring *tx_ring)
132{
133 writel(tx_ring->producer, tx_ring->crb_cmd_producer);
134
135 if (qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH) {
136 netif_stop_queue(adapter->netdev);
137 smp_mb();
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +0000138 adapter->stats.xmit_off++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000139 }
140}
141
142static const u32 msi_tgt_status[8] = {
143 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
144 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
145 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
146 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
147};
148
149static const
150struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
151
152static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
153{
154 writel(0, sds_ring->crb_intr_mask);
155}
156
157static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
158{
159 struct qlcnic_adapter *adapter = sds_ring->adapter;
160
161 writel(0x1, sds_ring->crb_intr_mask);
162
163 if (!QLCNIC_IS_MSI_FAMILY(adapter))
164 writel(0xfbff, adapter->tgt_mask_reg);
165}
166
167static int
168qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
169{
170 int size = sizeof(struct qlcnic_host_sds_ring) * count;
171
172 recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
173
174 return (recv_ctx->sds_rings == NULL);
175}
176
177static void
178qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
179{
180 if (recv_ctx->sds_rings != NULL)
181 kfree(recv_ctx->sds_rings);
182
183 recv_ctx->sds_rings = NULL;
184}
185
186static int
187qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
188{
189 int ring;
190 struct qlcnic_host_sds_ring *sds_ring;
191 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
192
193 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
194 return -ENOMEM;
195
196 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
197 sds_ring = &recv_ctx->sds_rings[ring];
198 netif_napi_add(netdev, &sds_ring->napi,
199 qlcnic_poll, QLCNIC_NETDEV_WEIGHT);
200 }
201
202 return 0;
203}
204
205static void
206qlcnic_napi_del(struct qlcnic_adapter *adapter)
207{
208 int ring;
209 struct qlcnic_host_sds_ring *sds_ring;
210 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
211
212 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
213 sds_ring = &recv_ctx->sds_rings[ring];
214 netif_napi_del(&sds_ring->napi);
215 }
216
217 qlcnic_free_sds_rings(&adapter->recv_ctx);
218}
219
220static void
221qlcnic_napi_enable(struct qlcnic_adapter *adapter)
222{
223 int ring;
224 struct qlcnic_host_sds_ring *sds_ring;
225 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
226
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000227 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
228 return;
229
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000230 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
231 sds_ring = &recv_ctx->sds_rings[ring];
232 napi_enable(&sds_ring->napi);
233 qlcnic_enable_int(sds_ring);
234 }
235}
236
237static void
238qlcnic_napi_disable(struct qlcnic_adapter *adapter)
239{
240 int ring;
241 struct qlcnic_host_sds_ring *sds_ring;
242 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
243
Amit Kumar Salecha780ab792010-04-22 02:51:41 +0000244 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
245 return;
246
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000247 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
248 sds_ring = &recv_ctx->sds_rings[ring];
249 qlcnic_disable_int(sds_ring);
250 napi_synchronize(&sds_ring->napi);
251 napi_disable(&sds_ring->napi);
252 }
253}
254
255static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
256{
257 memset(&adapter->stats, 0, sizeof(adapter->stats));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000258}
259
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000260static void qlcnic_set_port_mode(struct qlcnic_adapter *adapter)
261{
262 u32 val, data;
263
264 val = adapter->ahw.board_type;
265 if ((val == QLCNIC_BRDTYPE_P3_HMEZ) ||
266 (val == QLCNIC_BRDTYPE_P3_XG_LOM)) {
267 if (port_mode == QLCNIC_PORT_MODE_802_3_AP) {
268 data = QLCNIC_PORT_MODE_802_3_AP;
269 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
270 } else if (port_mode == QLCNIC_PORT_MODE_XG) {
271 data = QLCNIC_PORT_MODE_XG;
272 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
273 } else if (port_mode == QLCNIC_PORT_MODE_AUTO_NEG_1G) {
274 data = QLCNIC_PORT_MODE_AUTO_NEG_1G;
275 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
276 } else if (port_mode == QLCNIC_PORT_MODE_AUTO_NEG_XG) {
277 data = QLCNIC_PORT_MODE_AUTO_NEG_XG;
278 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
279 } else {
280 data = QLCNIC_PORT_MODE_AUTO_NEG;
281 QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
282 }
283
284 if ((wol_port_mode != QLCNIC_PORT_MODE_802_3_AP) &&
285 (wol_port_mode != QLCNIC_PORT_MODE_XG) &&
286 (wol_port_mode != QLCNIC_PORT_MODE_AUTO_NEG_1G) &&
287 (wol_port_mode != QLCNIC_PORT_MODE_AUTO_NEG_XG)) {
288 wol_port_mode = QLCNIC_PORT_MODE_AUTO_NEG;
289 }
290 QLCWR32(adapter, QLCNIC_WOL_PORT_MODE, wol_port_mode);
291 }
292}
293
294static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
295{
296 u32 control;
297 int pos;
298
299 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
300 if (pos) {
301 pci_read_config_dword(pdev, pos, &control);
302 if (enable)
303 control |= PCI_MSIX_FLAGS_ENABLE;
304 else
305 control = 0;
306 pci_write_config_dword(pdev, pos, control);
307 }
308}
309
310static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
311{
312 int i;
313
314 for (i = 0; i < count; i++)
315 adapter->msix_entries[i].entry = i;
316}
317
318static int
319qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
320{
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000321 u8 mac_addr[ETH_ALEN];
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000322 struct net_device *netdev = adapter->netdev;
323 struct pci_dev *pdev = adapter->pdev;
324
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000325 if (adapter->nic_ops->get_mac_addr(adapter, mac_addr) != 0)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000326 return -EIO;
327
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000328 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000329 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
330 memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
331
332 /* set station address */
333
334 if (!is_valid_ether_addr(netdev->perm_addr))
335 dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
336 netdev->dev_addr);
337
338 return 0;
339}
340
341static int qlcnic_set_mac(struct net_device *netdev, void *p)
342{
343 struct qlcnic_adapter *adapter = netdev_priv(netdev);
344 struct sockaddr *addr = p;
345
346 if (!is_valid_ether_addr(addr->sa_data))
347 return -EINVAL;
348
349 if (netif_running(netdev)) {
350 netif_device_detach(netdev);
351 qlcnic_napi_disable(adapter);
352 }
353
354 memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
355 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
356 qlcnic_set_multi(adapter->netdev);
357
358 if (netif_running(netdev)) {
359 netif_device_attach(netdev);
360 qlcnic_napi_enable(adapter);
361 }
362 return 0;
363}
364
365static const struct net_device_ops qlcnic_netdev_ops = {
366 .ndo_open = qlcnic_open,
367 .ndo_stop = qlcnic_close,
368 .ndo_start_xmit = qlcnic_xmit_frame,
369 .ndo_get_stats = qlcnic_get_stats,
370 .ndo_validate_addr = eth_validate_addr,
371 .ndo_set_multicast_list = qlcnic_set_multi,
372 .ndo_set_mac_address = qlcnic_set_mac,
373 .ndo_change_mtu = qlcnic_change_mtu,
374 .ndo_tx_timeout = qlcnic_tx_timeout,
375#ifdef CONFIG_NET_POLL_CONTROLLER
376 .ndo_poll_controller = qlcnic_poll_controller,
377#endif
378};
379
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000380static struct qlcnic_nic_template qlcnic_ops = {
381 .get_mac_addr = qlcnic_get_mac_addr,
382 .config_bridged_mode = qlcnic_config_bridged_mode,
383 .config_led = qlcnic_config_led,
384 .set_ilb_mode = qlcnic_set_ilb_mode,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000385 .clear_ilb_mode = qlcnic_clear_ilb_mode,
386 .start_firmware = qlcnic_start_firmware
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000387};
388
389static struct qlcnic_nic_template qlcnic_pf_ops = {
390 .get_mac_addr = qlcnic_get_mac_address,
391 .config_bridged_mode = qlcnic_config_bridged_mode,
392 .config_led = qlcnic_config_led,
393 .set_ilb_mode = qlcnic_set_ilb_mode,
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000394 .clear_ilb_mode = qlcnic_clear_ilb_mode,
395 .start_firmware = qlcnic_start_firmware
396};
397
398static struct qlcnic_nic_template qlcnic_vf_ops = {
399 .get_mac_addr = qlcnic_get_mac_address,
400 .config_bridged_mode = qlcnicvf_config_bridged_mode,
401 .config_led = qlcnicvf_config_led,
402 .set_ilb_mode = qlcnicvf_set_ilb_mode,
403 .clear_ilb_mode = qlcnicvf_clear_ilb_mode,
404 .start_firmware = qlcnicvf_start_firmware
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000405};
406
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000407static void
408qlcnic_setup_intr(struct qlcnic_adapter *adapter)
409{
410 const struct qlcnic_legacy_intr_set *legacy_intrp;
411 struct pci_dev *pdev = adapter->pdev;
412 int err, num_msix;
413
414 if (adapter->rss_supported) {
415 num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
416 MSIX_ENTRIES_PER_ADAPTER : 2;
417 } else
418 num_msix = 1;
419
420 adapter->max_sds_rings = 1;
421
422 adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
423
424 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
425
426 adapter->int_vec_bit = legacy_intrp->int_vec_bit;
427 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
428 legacy_intrp->tgt_status_reg);
429 adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
430 legacy_intrp->tgt_mask_reg);
431 adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
432
433 adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
434 ISR_INT_STATE_REG);
435
436 qlcnic_set_msix_bit(pdev, 0);
437
438 if (adapter->msix_supported) {
439
440 qlcnic_init_msix_entries(adapter, num_msix);
441 err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
442 if (err == 0) {
443 adapter->flags |= QLCNIC_MSIX_ENABLED;
444 qlcnic_set_msix_bit(pdev, 1);
445
446 if (adapter->rss_supported)
447 adapter->max_sds_rings = num_msix;
448
449 dev_info(&pdev->dev, "using msi-x interrupts\n");
450 return;
451 }
452
453 if (err > 0)
454 pci_disable_msix(pdev);
455
456 /* fall through for msi */
457 }
458
459 if (use_msi && !pci_enable_msi(pdev)) {
460 adapter->flags |= QLCNIC_MSI_ENABLED;
461 adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
462 msi_tgt_status[adapter->ahw.pci_func]);
463 dev_info(&pdev->dev, "using msi interrupts\n");
464 adapter->msix_entries[0].vector = pdev->irq;
465 return;
466 }
467
468 dev_info(&pdev->dev, "using legacy interrupts\n");
469 adapter->msix_entries[0].vector = pdev->irq;
470}
471
472static void
473qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
474{
475 if (adapter->flags & QLCNIC_MSIX_ENABLED)
476 pci_disable_msix(adapter->pdev);
477 if (adapter->flags & QLCNIC_MSI_ENABLED)
478 pci_disable_msi(adapter->pdev);
479}
480
481static void
482qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
483{
484 if (adapter->ahw.pci_base0 != NULL)
485 iounmap(adapter->ahw.pci_base0);
486}
487
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000488static int
489qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
490{
491 u8 id;
492 u32 ref_count;
493 int i, ret = 1;
494 u32 data = QLCNIC_MGMT_FUNC;
495 void __iomem *priv_op = adapter->ahw.pci_base0 + QLCNIC_DRV_OP_MODE;
496
497 /* If other drivers are not in use set their privilege level */
498 ref_count = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
499 ret = qlcnic_api_lock(adapter);
500 if (ret)
501 goto err_lock;
502 if (QLC_DEV_CLR_REF_CNT(ref_count, adapter->ahw.pci_func))
503 goto err_npar;
504
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000505 if (qlcnic_config_npars) {
506 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
507 id = adapter->npars[i].id;
508 if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
509 id == adapter->ahw.pci_func)
510 continue;
511 data |= (qlcnic_config_npars &
512 QLC_DEV_SET_DRV(0xf, id));
513 }
514 } else {
515 data = readl(priv_op);
516 data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw.pci_func)) |
517 (QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
518 adapter->ahw.pci_func));
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000519 }
520 writel(data, priv_op);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000521err_npar:
522 qlcnic_api_unlock(adapter);
523err_lock:
524 return ret;
525}
526
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000527static u32
528qlcnic_get_driver_mode(struct qlcnic_adapter *adapter)
529{
530 void __iomem *msix_base_addr;
531 void __iomem *priv_op;
532 u32 func;
533 u32 msix_base;
534 u32 op_mode, priv_level;
535
536 /* Determine FW API version */
537 adapter->fw_hal_version = readl(adapter->ahw.pci_base0 + QLCNIC_FW_API);
538 if (adapter->fw_hal_version == ~0) {
539 adapter->nic_ops = &qlcnic_ops;
540 adapter->fw_hal_version = QLCNIC_FW_BASE;
541 adapter->ahw.pci_func = PCI_FUNC(adapter->pdev->devfn);
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000542 adapter->capabilities = QLCRD32(adapter, CRB_FW_CAPABILITIES_1);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000543 dev_info(&adapter->pdev->dev,
544 "FW does not support nic partion\n");
545 return adapter->fw_hal_version;
546 }
547
548 /* Find PCI function number */
549 pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
550 msix_base_addr = adapter->ahw.pci_base0 + QLCNIC_MSIX_BASE;
551 msix_base = readl(msix_base_addr);
552 func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
553 adapter->ahw.pci_func = func;
554
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000555 qlcnic_get_nic_info(adapter, adapter->ahw.pci_func);
556
557 if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED)) {
558 adapter->nic_ops = &qlcnic_ops;
559 return adapter->fw_hal_version;
560 }
561
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000562 /* Determine function privilege level */
563 priv_op = adapter->ahw.pci_base0 + QLCNIC_DRV_OP_MODE;
564 op_mode = readl(priv_op);
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000565 if (op_mode == QLC_DEV_DRV_DEFAULT)
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000566 priv_level = QLCNIC_MGMT_FUNC;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000567 else
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000568 priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw.pci_func);
569
570 switch (priv_level) {
571 case QLCNIC_MGMT_FUNC:
572 adapter->op_mode = QLCNIC_MGMT_FUNC;
573 adapter->nic_ops = &qlcnic_pf_ops;
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000574 qlcnic_get_pci_info(adapter);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000575 /* Set privilege level for other functions */
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000576 qlcnic_set_function_modes(adapter);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000577 dev_info(&adapter->pdev->dev,
578 "HAL Version: %d, Management function\n",
579 adapter->fw_hal_version);
580 break;
581 case QLCNIC_PRIV_FUNC:
582 adapter->op_mode = QLCNIC_PRIV_FUNC;
583 dev_info(&adapter->pdev->dev,
584 "HAL Version: %d, Privileged function\n",
585 adapter->fw_hal_version);
586 adapter->nic_ops = &qlcnic_pf_ops;
587 break;
Anirban Chakraborty9f26f542010-06-01 11:33:09 +0000588 case QLCNIC_NON_PRIV_FUNC:
589 adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
590 dev_info(&adapter->pdev->dev,
591 "HAL Version: %d Non Privileged function\n",
592 adapter->fw_hal_version);
593 adapter->nic_ops = &qlcnic_vf_ops;
594 break;
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000595 default:
596 dev_info(&adapter->pdev->dev, "Unknown function mode: %d\n",
597 priv_level);
598 return 0;
599 }
600 return adapter->fw_hal_version;
601}
602
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000603static int
604qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
605{
606 void __iomem *mem_ptr0 = NULL;
607 resource_size_t mem_base;
608 unsigned long mem_len, pci_len0 = 0;
609
610 struct pci_dev *pdev = adapter->pdev;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000611
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000612 /* remap phys address */
613 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
614 mem_len = pci_resource_len(pdev, 0);
615
616 if (mem_len == QLCNIC_PCI_2MB_SIZE) {
617
618 mem_ptr0 = pci_ioremap_bar(pdev, 0);
619 if (mem_ptr0 == NULL) {
620 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
621 return -EIO;
622 }
623 pci_len0 = mem_len;
624 } else {
625 return -EIO;
626 }
627
628 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
629
630 adapter->ahw.pci_base0 = mem_ptr0;
631 adapter->ahw.pci_len0 = pci_len0;
632
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000633 if (!qlcnic_get_driver_mode(adapter)) {
634 iounmap(adapter->ahw.pci_base0);
635 return -EIO;
636 }
637
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000638 adapter->ahw.ocm_win_crb = qlcnic_get_ioaddr(adapter,
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000639 QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(adapter->ahw.pci_func)));
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000640
641 return 0;
642}
643
644static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
645{
646 struct pci_dev *pdev = adapter->pdev;
647 int i, found = 0;
648
649 for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
650 if (qlcnic_boards[i].vendor == pdev->vendor &&
651 qlcnic_boards[i].device == pdev->device &&
652 qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
653 qlcnic_boards[i].sub_device == pdev->subsystem_device) {
Sucheta Chakraborty02f6e462010-05-17 01:22:09 +0000654 sprintf(name, "%pM: %s" ,
655 adapter->mac_addr,
656 qlcnic_boards[i].short_name);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000657 found = 1;
658 break;
659 }
660
661 }
662
663 if (!found)
Sritej Velaga7f9a0c32010-06-17 02:56:39 +0000664 sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000665}
666
667static void
668qlcnic_check_options(struct qlcnic_adapter *adapter)
669{
670 u32 fw_major, fw_minor, fw_build;
671 char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
672 char serial_num[32];
673 int i, offset, val;
674 int *ptr32;
675 struct pci_dev *pdev = adapter->pdev;
676
677 adapter->driver_mismatch = 0;
678
679 ptr32 = (int *)&serial_num;
680 offset = QLCNIC_FW_SERIAL_NUM_OFFSET;
681 for (i = 0; i < 8; i++) {
682 if (qlcnic_rom_fast_read(adapter, offset, &val) == -1) {
683 dev_err(&pdev->dev, "error reading board info\n");
684 adapter->driver_mismatch = 1;
685 return;
686 }
687 ptr32[i] = cpu_to_le32(val);
688 offset += sizeof(u32);
689 }
690
691 fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
692 fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
693 fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
694
695 adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
696
697 if (adapter->portnum == 0) {
698 get_brd_name(adapter, brd_name);
699
700 pr_info("%s: %s Board Chip rev 0x%x\n",
701 module_name(THIS_MODULE),
702 brd_name, adapter->ahw.revision_id);
703 }
704
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +0000705 dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
706 fw_major, fw_minor, fw_build);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000707
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000708 adapter->flags &= ~QLCNIC_LRO_ENABLED;
709
710 if (adapter->ahw.port_type == QLCNIC_XGBE) {
711 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
712 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
713 } else if (adapter->ahw.port_type == QLCNIC_GBE) {
714 adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
715 adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
716 }
717
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000718 qlcnic_get_nic_info(adapter, adapter->ahw.pci_func);
719
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000720 adapter->msix_supported = !!use_msi_x;
721 adapter->rss_supported = !!use_msi_x;
722
723 adapter->num_txd = MAX_CMD_DESCRIPTORS;
724
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000725 adapter->max_rds_rings = 2;
726}
727
728static int
729qlcnic_start_firmware(struct qlcnic_adapter *adapter)
730{
731 int val, err, first_boot;
732
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +0000733 err = qlcnic_can_start_firmware(adapter);
734 if (err < 0)
735 return err;
736 else if (!err)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000737 goto wait_init;
738
739 first_boot = QLCRD32(adapter, QLCNIC_CAM_RAM(0x1fc));
740 if (first_boot == 0x55555555)
741 /* This is the first boot after power up */
742 QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC);
743
Amit Kumar Salecha4d5bdb32010-05-17 01:22:11 +0000744 if (load_fw_file)
745 qlcnic_request_firmware(adapter);
746 else
747 adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000748
749 err = qlcnic_need_fw_reset(adapter);
750 if (err < 0)
751 goto err_out;
752 if (err == 0)
753 goto wait_init;
754
755 if (first_boot != 0x55555555) {
756 QLCWR32(adapter, CRB_CMDPEG_STATE, 0);
757 qlcnic_pinit_from_rom(adapter);
758 msleep(1);
759 }
760
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000761 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS1, 0);
762 QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS2, 0);
763
764 qlcnic_set_port_mode(adapter);
765
766 err = qlcnic_load_firmware(adapter);
767 if (err)
768 goto err_out;
769
770 qlcnic_release_firmware(adapter);
771
772 val = (_QLCNIC_LINUX_MAJOR << 16)
773 | ((_QLCNIC_LINUX_MINOR << 8))
774 | (_QLCNIC_LINUX_SUBVERSION);
775 QLCWR32(adapter, CRB_DRIVER_VERSION, val);
776
777wait_init:
778 /* Handshake with the card before we register the devices. */
779 err = qlcnic_phantom_init(adapter);
780 if (err)
781 goto err_out;
782
783 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +0000784 qlcnic_idc_debug_info(adapter, 1);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000785
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000786 qlcnic_check_options(adapter);
787
Anirban Chakraborty0e33c662010-06-16 09:07:27 +0000788 if (adapter->flags & QLCNIC_ESWITCH_ENABLED &&
789 adapter->op_mode != QLCNIC_NON_PRIV_FUNC)
790 qlcnic_dev_set_npar_ready(adapter);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +0000791
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000792 adapter->need_fw_reset = 0;
793
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +0000794 qlcnic_release_firmware(adapter);
795 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000796
797err_out:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +0000798 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
799 dev_err(&adapter->pdev->dev, "Device state set to failed\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000800 qlcnic_release_firmware(adapter);
801 return err;
802}
803
804static int
805qlcnic_request_irq(struct qlcnic_adapter *adapter)
806{
807 irq_handler_t handler;
808 struct qlcnic_host_sds_ring *sds_ring;
809 int err, ring;
810
811 unsigned long flags = 0;
812 struct net_device *netdev = adapter->netdev;
813 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
814
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +0000815 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
816 handler = qlcnic_tmp_intr;
817 if (!QLCNIC_IS_MSI_FAMILY(adapter))
818 flags |= IRQF_SHARED;
819
820 } else {
821 if (adapter->flags & QLCNIC_MSIX_ENABLED)
822 handler = qlcnic_msix_intr;
823 else if (adapter->flags & QLCNIC_MSI_ENABLED)
824 handler = qlcnic_msi_intr;
825 else {
826 flags |= IRQF_SHARED;
827 handler = qlcnic_intr;
828 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +0000829 }
830 adapter->irq = netdev->irq;
831
832 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
833 sds_ring = &recv_ctx->sds_rings[ring];
834 sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
835 err = request_irq(sds_ring->irq, handler,
836 flags, sds_ring->name, sds_ring);
837 if (err)
838 return err;
839 }
840
841 return 0;
842}
843
844static void
845qlcnic_free_irq(struct qlcnic_adapter *adapter)
846{
847 int ring;
848 struct qlcnic_host_sds_ring *sds_ring;
849
850 struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
851
852 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
853 sds_ring = &recv_ctx->sds_rings[ring];
854 free_irq(sds_ring->irq, sds_ring);
855 }
856}
857
858static void
859qlcnic_init_coalesce_defaults(struct qlcnic_adapter *adapter)
860{
861 adapter->coal.flags = QLCNIC_INTR_DEFAULT;
862 adapter->coal.normal.data.rx_time_us =
863 QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
864 adapter->coal.normal.data.rx_packets =
865 QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
866 adapter->coal.normal.data.tx_time_us =
867 QLCNIC_DEFAULT_INTR_COALESCE_TX_TIME_US;
868 adapter->coal.normal.data.tx_packets =
869 QLCNIC_DEFAULT_INTR_COALESCE_TX_PACKETS;
870}
871
872static int
873__qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
874{
875 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
876 return -EIO;
877
878 qlcnic_set_multi(netdev);
879 qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
880
881 adapter->ahw.linkup = 0;
882
883 if (adapter->max_sds_rings > 1)
884 qlcnic_config_rss(adapter, 1);
885
886 qlcnic_config_intr_coalesce(adapter);
887
888 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
889 qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
890
891 qlcnic_napi_enable(adapter);
892
893 qlcnic_linkevent_request(adapter, 1);
894
895 set_bit(__QLCNIC_DEV_UP, &adapter->state);
896 return 0;
897}
898
899/* Usage: During resume and firmware recovery module.*/
900
901static int
902qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
903{
904 int err = 0;
905
906 rtnl_lock();
907 if (netif_running(netdev))
908 err = __qlcnic_up(adapter, netdev);
909 rtnl_unlock();
910
911 return err;
912}
913
914static void
915__qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
916{
917 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
918 return;
919
920 if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
921 return;
922
923 smp_mb();
924 spin_lock(&adapter->tx_clean_lock);
925 netif_carrier_off(netdev);
926 netif_tx_disable(netdev);
927
928 qlcnic_free_mac_list(adapter);
929
930 qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
931
932 qlcnic_napi_disable(adapter);
933
934 qlcnic_release_tx_buffers(adapter);
935 spin_unlock(&adapter->tx_clean_lock);
936}
937
938/* Usage: During suspend and firmware recovery module */
939
940static void
941qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
942{
943 rtnl_lock();
944 if (netif_running(netdev))
945 __qlcnic_down(adapter, netdev);
946 rtnl_unlock();
947
948}
949
950static int
951qlcnic_attach(struct qlcnic_adapter *adapter)
952{
953 struct net_device *netdev = adapter->netdev;
954 struct pci_dev *pdev = adapter->pdev;
955 int err, ring;
956 struct qlcnic_host_rds_ring *rds_ring;
957
958 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
959 return 0;
960
961 err = qlcnic_init_firmware(adapter);
962 if (err)
963 return err;
964
965 err = qlcnic_napi_add(adapter, netdev);
966 if (err)
967 return err;
968
969 err = qlcnic_alloc_sw_resources(adapter);
970 if (err) {
971 dev_err(&pdev->dev, "Error in setting sw resources\n");
972 return err;
973 }
974
975 err = qlcnic_alloc_hw_resources(adapter);
976 if (err) {
977 dev_err(&pdev->dev, "Error in setting hw resources\n");
978 goto err_out_free_sw;
979 }
980
981
982 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
983 rds_ring = &adapter->recv_ctx.rds_rings[ring];
984 qlcnic_post_rx_buffers(adapter, ring, rds_ring);
985 }
986
987 err = qlcnic_request_irq(adapter);
988 if (err) {
989 dev_err(&pdev->dev, "failed to setup interrupt\n");
990 goto err_out_free_rxbuf;
991 }
992
993 qlcnic_init_coalesce_defaults(adapter);
994
995 qlcnic_create_sysfs_entries(adapter);
996
997 adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
998 return 0;
999
1000err_out_free_rxbuf:
1001 qlcnic_release_rx_buffers(adapter);
1002 qlcnic_free_hw_resources(adapter);
1003err_out_free_sw:
1004 qlcnic_free_sw_resources(adapter);
1005 return err;
1006}
1007
1008static void
1009qlcnic_detach(struct qlcnic_adapter *adapter)
1010{
1011 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1012 return;
1013
1014 qlcnic_remove_sysfs_entries(adapter);
1015
1016 qlcnic_free_hw_resources(adapter);
1017 qlcnic_release_rx_buffers(adapter);
1018 qlcnic_free_irq(adapter);
1019 qlcnic_napi_del(adapter);
1020 qlcnic_free_sw_resources(adapter);
1021
1022 adapter->is_up = 0;
1023}
1024
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001025void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
1026{
1027 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1028 struct qlcnic_host_sds_ring *sds_ring;
1029 int ring;
1030
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001031 clear_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001032 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1033 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1034 sds_ring = &adapter->recv_ctx.sds_rings[ring];
1035 qlcnic_disable_int(sds_ring);
1036 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001037 }
1038
1039 qlcnic_detach(adapter);
1040
1041 adapter->diag_test = 0;
1042 adapter->max_sds_rings = max_sds_rings;
1043
1044 if (qlcnic_attach(adapter))
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001045 goto out;
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001046
1047 if (netif_running(netdev))
1048 __qlcnic_up(adapter, netdev);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001049out:
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001050 netif_device_attach(netdev);
1051}
1052
1053int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
1054{
1055 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1056 struct qlcnic_host_sds_ring *sds_ring;
1057 int ring;
1058 int ret;
1059
1060 netif_device_detach(netdev);
1061
1062 if (netif_running(netdev))
1063 __qlcnic_down(adapter, netdev);
1064
1065 qlcnic_detach(adapter);
1066
1067 adapter->max_sds_rings = 1;
1068 adapter->diag_test = test;
1069
1070 ret = qlcnic_attach(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001071 if (ret) {
1072 netif_device_attach(netdev);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001073 return ret;
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001074 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001075
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001076 if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
1077 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1078 sds_ring = &adapter->recv_ctx.sds_rings[ring];
1079 qlcnic_enable_int(sds_ring);
1080 }
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001081 }
Sucheta Chakraborty78ad3892010-05-17 01:22:12 +00001082 set_bit(__QLCNIC_DEV_UP, &adapter->state);
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001083
1084 return 0;
1085}
1086
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001087int
1088qlcnic_reset_context(struct qlcnic_adapter *adapter)
1089{
1090 int err = 0;
1091 struct net_device *netdev = adapter->netdev;
1092
1093 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1094 return -EBUSY;
1095
1096 if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
1097
1098 netif_device_detach(netdev);
1099
1100 if (netif_running(netdev))
1101 __qlcnic_down(adapter, netdev);
1102
1103 qlcnic_detach(adapter);
1104
1105 if (netif_running(netdev)) {
1106 err = qlcnic_attach(adapter);
1107 if (!err)
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001108 __qlcnic_up(adapter, netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001109 }
1110
1111 netif_device_attach(netdev);
1112 }
1113
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001114 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1115 return err;
1116}
1117
1118static int
1119qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001120 struct net_device *netdev, u8 pci_using_dac)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001121{
1122 int err;
1123 struct pci_dev *pdev = adapter->pdev;
1124
1125 adapter->rx_csum = 1;
1126 adapter->mc_enabled = 0;
1127 adapter->max_mc_count = 38;
1128
1129 netdev->netdev_ops = &qlcnic_netdev_ops;
1130 netdev->watchdog_timeo = 2*HZ;
1131
1132 qlcnic_change_mtu(netdev, netdev->mtu);
1133
1134 SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
1135
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001136 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
1137 NETIF_F_IPV6_CSUM | NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001138
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001139 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
1140 NETIF_F_IPV6_CSUM | NETIF_F_TSO | NETIF_F_TSO6);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001141
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001142 if (pci_using_dac) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001143 netdev->features |= NETIF_F_HIGHDMA;
1144 netdev->vlan_features |= NETIF_F_HIGHDMA;
1145 }
1146
1147 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
1148 netdev->features |= (NETIF_F_HW_VLAN_TX);
1149
1150 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
1151 netdev->features |= NETIF_F_LRO;
1152
1153 netdev->irq = adapter->msix_entries[0].vector;
1154
1155 INIT_WORK(&adapter->tx_timeout_task, qlcnic_tx_timeout_task);
1156
1157 if (qlcnic_read_mac_addr(adapter))
1158 dev_warn(&pdev->dev, "failed to read mac addr\n");
1159
1160 netif_carrier_off(netdev);
1161 netif_stop_queue(netdev);
1162
1163 err = register_netdev(netdev);
1164 if (err) {
1165 dev_err(&pdev->dev, "failed to register net device\n");
1166 return err;
1167 }
1168
1169 return 0;
1170}
1171
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001172static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
1173{
1174 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1175 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
1176 *pci_using_dac = 1;
1177 else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
1178 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1179 *pci_using_dac = 0;
1180 else {
1181 dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
1182 return -EIO;
1183 }
1184
1185 return 0;
1186}
1187
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001188static int __devinit
1189qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1190{
1191 struct net_device *netdev = NULL;
1192 struct qlcnic_adapter *adapter = NULL;
1193 int err;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001194 uint8_t revision_id;
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001195 uint8_t pci_using_dac;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001196
1197 err = pci_enable_device(pdev);
1198 if (err)
1199 return err;
1200
1201 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1202 err = -ENODEV;
1203 goto err_out_disable_pdev;
1204 }
1205
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001206 err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
1207 if (err)
1208 goto err_out_disable_pdev;
1209
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001210 err = pci_request_regions(pdev, qlcnic_driver_name);
1211 if (err)
1212 goto err_out_disable_pdev;
1213
1214 pci_set_master(pdev);
1215
1216 netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
1217 if (!netdev) {
1218 dev_err(&pdev->dev, "failed to allocate net_device\n");
1219 err = -ENOMEM;
1220 goto err_out_free_res;
1221 }
1222
1223 SET_NETDEV_DEV(netdev, &pdev->dev);
1224
1225 adapter = netdev_priv(netdev);
1226 adapter->netdev = netdev;
1227 adapter->pdev = pdev;
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00001228 adapter->dev_rst_time = jiffies;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001229
1230 revision_id = pdev->revision;
1231 adapter->ahw.revision_id = revision_id;
1232
1233 rwlock_init(&adapter->ahw.crb_lock);
1234 mutex_init(&adapter->ahw.mem_lock);
1235
1236 spin_lock_init(&adapter->tx_clean_lock);
1237 INIT_LIST_HEAD(&adapter->mac_list);
1238
1239 err = qlcnic_setup_pci_map(adapter);
1240 if (err)
1241 goto err_out_free_netdev;
1242
1243 /* This will be reset for mezz cards */
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001244 adapter->portnum = adapter->ahw.pci_func;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001245
1246 err = qlcnic_get_board_info(adapter);
1247 if (err) {
1248 dev_err(&pdev->dev, "Error getting board config info.\n");
1249 goto err_out_iounmap;
1250 }
1251
Sucheta Chakraborty02f6e462010-05-17 01:22:09 +00001252 if (qlcnic_read_mac_addr(adapter))
1253 dev_warn(&pdev->dev, "failed to read mac addr\n");
1254
Sucheta Chakrabortyb3a24642010-05-13 03:07:48 +00001255 if (qlcnic_setup_idc_param(adapter))
1256 goto err_out_iounmap;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001257
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001258 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001259 if (err) {
1260 dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001261 goto err_out_decr_ref;
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00001262 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001263
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001264 qlcnic_clear_stats(adapter);
1265
1266 qlcnic_setup_intr(adapter);
1267
Amit Kumar Salecha1bb09fb2010-05-14 03:07:46 -07001268 err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001269 if (err)
1270 goto err_out_disable_msi;
1271
1272 pci_set_drvdata(pdev, adapter);
1273
1274 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1275
1276 switch (adapter->ahw.port_type) {
1277 case QLCNIC_GBE:
1278 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1279 adapter->netdev->name);
1280 break;
1281 case QLCNIC_XGBE:
1282 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1283 adapter->netdev->name);
1284 break;
1285 }
1286
1287 qlcnic_create_diag_entries(adapter);
1288
1289 return 0;
1290
1291err_out_disable_msi:
1292 qlcnic_teardown_intr(adapter);
1293
1294err_out_decr_ref:
1295 qlcnic_clr_all_drv_state(adapter);
1296
1297err_out_iounmap:
1298 qlcnic_cleanup_pci_map(adapter);
1299
1300err_out_free_netdev:
1301 free_netdev(netdev);
1302
1303err_out_free_res:
1304 pci_release_regions(pdev);
1305
1306err_out_disable_pdev:
1307 pci_set_drvdata(pdev, NULL);
1308 pci_disable_device(pdev);
1309 return err;
1310}
1311
1312static void __devexit qlcnic_remove(struct pci_dev *pdev)
1313{
1314 struct qlcnic_adapter *adapter;
1315 struct net_device *netdev;
1316
1317 adapter = pci_get_drvdata(pdev);
1318 if (adapter == NULL)
1319 return;
1320
1321 netdev = adapter->netdev;
1322
1323 qlcnic_cancel_fw_work(adapter);
1324
1325 unregister_netdev(netdev);
1326
1327 cancel_work_sync(&adapter->tx_timeout_task);
1328
1329 qlcnic_detach(adapter);
1330
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001331 if (adapter->npars != NULL)
1332 kfree(adapter->npars);
1333 if (adapter->eswitch != NULL)
1334 kfree(adapter->eswitch);
1335
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001336 qlcnic_clr_all_drv_state(adapter);
1337
1338 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1339
1340 qlcnic_teardown_intr(adapter);
1341
1342 qlcnic_remove_diag_entries(adapter);
1343
1344 qlcnic_cleanup_pci_map(adapter);
1345
1346 qlcnic_release_firmware(adapter);
1347
1348 pci_release_regions(pdev);
1349 pci_disable_device(pdev);
1350 pci_set_drvdata(pdev, NULL);
1351
1352 free_netdev(netdev);
1353}
1354static int __qlcnic_shutdown(struct pci_dev *pdev)
1355{
1356 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1357 struct net_device *netdev = adapter->netdev;
1358 int retval;
1359
1360 netif_device_detach(netdev);
1361
1362 qlcnic_cancel_fw_work(adapter);
1363
1364 if (netif_running(netdev))
1365 qlcnic_down(adapter, netdev);
1366
1367 cancel_work_sync(&adapter->tx_timeout_task);
1368
1369 qlcnic_detach(adapter);
1370
1371 qlcnic_clr_all_drv_state(adapter);
1372
1373 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1374
1375 retval = pci_save_state(pdev);
1376 if (retval)
1377 return retval;
1378
1379 if (qlcnic_wol_supported(adapter)) {
1380 pci_enable_wake(pdev, PCI_D3cold, 1);
1381 pci_enable_wake(pdev, PCI_D3hot, 1);
1382 }
1383
1384 return 0;
1385}
1386
1387static void qlcnic_shutdown(struct pci_dev *pdev)
1388{
1389 if (__qlcnic_shutdown(pdev))
1390 return;
1391
1392 pci_disable_device(pdev);
1393}
1394
1395#ifdef CONFIG_PM
1396static int
1397qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
1398{
1399 int retval;
1400
1401 retval = __qlcnic_shutdown(pdev);
1402 if (retval)
1403 return retval;
1404
1405 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1406 return 0;
1407}
1408
1409static int
1410qlcnic_resume(struct pci_dev *pdev)
1411{
1412 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
1413 struct net_device *netdev = adapter->netdev;
1414 int err;
1415
1416 err = pci_enable_device(pdev);
1417 if (err)
1418 return err;
1419
1420 pci_set_power_state(pdev, PCI_D0);
1421 pci_set_master(pdev);
1422 pci_restore_state(pdev);
1423
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00001424 err = adapter->nic_ops->start_firmware(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001425 if (err) {
1426 dev_err(&pdev->dev, "failed to start firmware\n");
1427 return err;
1428 }
1429
1430 if (netif_running(netdev)) {
1431 err = qlcnic_attach(adapter);
1432 if (err)
1433 goto err_out;
1434
1435 err = qlcnic_up(adapter, netdev);
1436 if (err)
1437 goto err_out_detach;
1438
1439
1440 qlcnic_config_indev_addr(netdev, NETDEV_UP);
1441 }
1442
1443 netif_device_attach(netdev);
1444 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
1445 return 0;
1446
1447err_out_detach:
1448 qlcnic_detach(adapter);
1449err_out:
1450 qlcnic_clr_all_drv_state(adapter);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00001451 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001452 return err;
1453}
1454#endif
1455
1456static int qlcnic_open(struct net_device *netdev)
1457{
1458 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1459 int err;
1460
1461 if (adapter->driver_mismatch)
1462 return -EIO;
1463
1464 err = qlcnic_attach(adapter);
1465 if (err)
1466 return err;
1467
1468 err = __qlcnic_up(adapter, netdev);
1469 if (err)
1470 goto err_out;
1471
1472 netif_start_queue(netdev);
1473
1474 return 0;
1475
1476err_out:
1477 qlcnic_detach(adapter);
1478 return err;
1479}
1480
1481/*
1482 * qlcnic_close - Disables a network interface entry point
1483 */
1484static int qlcnic_close(struct net_device *netdev)
1485{
1486 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1487
1488 __qlcnic_down(adapter, netdev);
1489 return 0;
1490}
1491
1492static void
1493qlcnic_tso_check(struct net_device *netdev,
1494 struct qlcnic_host_tx_ring *tx_ring,
1495 struct cmd_desc_type0 *first_desc,
1496 struct sk_buff *skb)
1497{
1498 u8 opcode = TX_ETHER_PKT;
1499 __be16 protocol = skb->protocol;
1500 u16 flags = 0, vid = 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001501 int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
1502 struct cmd_desc_type0 *hwdesc;
1503 struct vlan_ethhdr *vh;
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001504 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001505 u32 producer = tx_ring->producer;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001506
1507 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
1508
1509 vh = (struct vlan_ethhdr *)skb->data;
1510 protocol = vh->h_vlan_encapsulated_proto;
1511 flags = FLAGS_VLAN_TAGGED;
1512
1513 } else if (vlan_tx_tag_present(skb)) {
1514
1515 flags = FLAGS_VLAN_OOB;
1516 vid = vlan_tx_tag_get(skb);
1517 qlcnic_set_tx_vlan_tci(first_desc, vid);
1518 vlan_oob = 1;
1519 }
1520
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00001521 if (*(skb->data) & BIT_0) {
1522 flags |= BIT_0;
1523 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
1524 }
1525
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001526 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1527 skb_shinfo(skb)->gso_size > 0) {
1528
1529 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1530
1531 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1532 first_desc->total_hdr_length = hdr_len;
1533 if (vlan_oob) {
1534 first_desc->total_hdr_length += VLAN_HLEN;
1535 first_desc->tcp_hdr_offset = VLAN_HLEN;
1536 first_desc->ip_hdr_offset = VLAN_HLEN;
1537 /* Only in case of TSO on vlan device */
1538 flags |= FLAGS_VLAN_TAGGED;
1539 }
1540
1541 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
1542 TX_TCP_LSO6 : TX_TCP_LSO;
1543 tso = 1;
1544
1545 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1546 u8 l4proto;
1547
1548 if (protocol == cpu_to_be16(ETH_P_IP)) {
1549 l4proto = ip_hdr(skb)->protocol;
1550
1551 if (l4proto == IPPROTO_TCP)
1552 opcode = TX_TCP_PKT;
1553 else if (l4proto == IPPROTO_UDP)
1554 opcode = TX_UDP_PKT;
1555 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
1556 l4proto = ipv6_hdr(skb)->nexthdr;
1557
1558 if (l4proto == IPPROTO_TCP)
1559 opcode = TX_TCPV6_PKT;
1560 else if (l4proto == IPPROTO_UDP)
1561 opcode = TX_UDPV6_PKT;
1562 }
1563 }
1564
1565 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
1566 first_desc->ip_hdr_offset += skb_network_offset(skb);
1567 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
1568
1569 if (!tso)
1570 return;
1571
1572 /* For LSO, we need to copy the MAC/IP/TCP headers into
1573 * the descriptor ring
1574 */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001575 copied = 0;
1576 offset = 2;
1577
1578 if (vlan_oob) {
1579 /* Create a TSO vlan header template for firmware */
1580
1581 hwdesc = &tx_ring->desc_head[producer];
1582 tx_ring->cmd_buf_arr[producer].skb = NULL;
1583
1584 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1585 hdr_len + VLAN_HLEN);
1586
1587 vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
1588 skb_copy_from_linear_data(skb, vh, 12);
1589 vh->h_vlan_proto = htons(ETH_P_8021Q);
1590 vh->h_vlan_TCI = htons(vid);
1591 skb_copy_from_linear_data_offset(skb, 12,
1592 (char *)vh + 16, copy_len - 16);
1593
1594 copied = copy_len - VLAN_HLEN;
1595 offset = 0;
1596
1597 producer = get_next_index(producer, tx_ring->num_desc);
1598 }
1599
1600 while (copied < hdr_len) {
1601
1602 copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
1603 (hdr_len - copied));
1604
1605 hwdesc = &tx_ring->desc_head[producer];
1606 tx_ring->cmd_buf_arr[producer].skb = NULL;
1607
1608 skb_copy_from_linear_data_offset(skb, copied,
1609 (char *)hwdesc + offset, copy_len);
1610
1611 copied += copy_len;
1612 offset = 0;
1613
1614 producer = get_next_index(producer, tx_ring->num_desc);
1615 }
1616
1617 tx_ring->producer = producer;
1618 barrier();
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001619 adapter->stats.lso_frames++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001620}
1621
1622static int
1623qlcnic_map_tx_skb(struct pci_dev *pdev,
1624 struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
1625{
1626 struct qlcnic_skb_frag *nf;
1627 struct skb_frag_struct *frag;
1628 int i, nr_frags;
1629 dma_addr_t map;
1630
1631 nr_frags = skb_shinfo(skb)->nr_frags;
1632 nf = &pbuf->frag_array[0];
1633
1634 map = pci_map_single(pdev, skb->data,
1635 skb_headlen(skb), PCI_DMA_TODEVICE);
1636 if (pci_dma_mapping_error(pdev, map))
1637 goto out_err;
1638
1639 nf->dma = map;
1640 nf->length = skb_headlen(skb);
1641
1642 for (i = 0; i < nr_frags; i++) {
1643 frag = &skb_shinfo(skb)->frags[i];
1644 nf = &pbuf->frag_array[i+1];
1645
1646 map = pci_map_page(pdev, frag->page, frag->page_offset,
1647 frag->size, PCI_DMA_TODEVICE);
1648 if (pci_dma_mapping_error(pdev, map))
1649 goto unwind;
1650
1651 nf->dma = map;
1652 nf->length = frag->size;
1653 }
1654
1655 return 0;
1656
1657unwind:
1658 while (--i >= 0) {
1659 nf = &pbuf->frag_array[i+1];
1660 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
1661 }
1662
1663 nf = &pbuf->frag_array[0];
1664 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
1665
1666out_err:
1667 return -ENOMEM;
1668}
1669
1670static inline void
1671qlcnic_clear_cmddesc(u64 *desc)
1672{
1673 desc[0] = 0ULL;
1674 desc[2] = 0ULL;
1675}
1676
Amit Kumar Salechacdaff182010-02-01 05:25:00 +00001677netdev_tx_t
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001678qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
1679{
1680 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1681 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1682 struct qlcnic_cmd_buffer *pbuf;
1683 struct qlcnic_skb_frag *buffrag;
1684 struct cmd_desc_type0 *hwdesc, *first_desc;
1685 struct pci_dev *pdev;
1686 int i, k;
1687
1688 u32 producer;
1689 int frag_count, no_of_desc;
1690 u32 num_txd = tx_ring->num_desc;
1691
Amit Kumar Salecha780ab792010-04-22 02:51:41 +00001692 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
1693 netif_stop_queue(netdev);
1694 return NETDEV_TX_BUSY;
1695 }
1696
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001697 frag_count = skb_shinfo(skb)->nr_frags + 1;
1698
1699 /* 4 fragments per cmd des */
1700 no_of_desc = (frag_count + 3) >> 2;
1701
1702 if (unlikely(no_of_desc + 2 > qlcnic_tx_avail(tx_ring))) {
1703 netif_stop_queue(netdev);
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00001704 adapter->stats.xmit_off++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001705 return NETDEV_TX_BUSY;
1706 }
1707
1708 producer = tx_ring->producer;
1709 pbuf = &tx_ring->cmd_buf_arr[producer];
1710
1711 pdev = adapter->pdev;
1712
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00001713 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
1714 adapter->stats.tx_dma_map_error++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001715 goto drop_packet;
Amit Kumar Salecha8ae6df92010-04-22 02:51:35 +00001716 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001717
1718 pbuf->skb = skb;
1719 pbuf->frag_count = frag_count;
1720
1721 first_desc = hwdesc = &tx_ring->desc_head[producer];
1722 qlcnic_clear_cmddesc((u64 *)hwdesc);
1723
1724 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
1725 qlcnic_set_tx_port(first_desc, adapter->portnum);
1726
1727 for (i = 0; i < frag_count; i++) {
1728
1729 k = i % 4;
1730
1731 if ((k == 0) && (i > 0)) {
1732 /* move to next desc.*/
1733 producer = get_next_index(producer, num_txd);
1734 hwdesc = &tx_ring->desc_head[producer];
1735 qlcnic_clear_cmddesc((u64 *)hwdesc);
1736 tx_ring->cmd_buf_arr[producer].skb = NULL;
1737 }
1738
1739 buffrag = &pbuf->frag_array[i];
1740
1741 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
1742 switch (k) {
1743 case 0:
1744 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1745 break;
1746 case 1:
1747 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
1748 break;
1749 case 2:
1750 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
1751 break;
1752 case 3:
1753 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
1754 break;
1755 }
1756 }
1757
1758 tx_ring->producer = get_next_index(producer, num_txd);
1759
1760 qlcnic_tso_check(netdev, tx_ring, first_desc, skb);
1761
1762 qlcnic_update_cmd_producer(adapter, tx_ring);
1763
1764 adapter->stats.txbytes += skb->len;
1765 adapter->stats.xmitcalled++;
1766
1767 return NETDEV_TX_OK;
1768
1769drop_packet:
1770 adapter->stats.txdropped++;
1771 dev_kfree_skb_any(skb);
1772 return NETDEV_TX_OK;
1773}
1774
1775static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
1776{
1777 struct net_device *netdev = adapter->netdev;
1778 u32 temp, temp_state, temp_val;
1779 int rv = 0;
1780
1781 temp = QLCRD32(adapter, CRB_TEMP_STATE);
1782
1783 temp_state = qlcnic_get_temp_state(temp);
1784 temp_val = qlcnic_get_temp_val(temp);
1785
1786 if (temp_state == QLCNIC_TEMP_PANIC) {
1787 dev_err(&netdev->dev,
1788 "Device temperature %d degrees C exceeds"
1789 " maximum allowed. Hardware has been shut down.\n",
1790 temp_val);
1791 rv = 1;
1792 } else if (temp_state == QLCNIC_TEMP_WARN) {
1793 if (adapter->temp == QLCNIC_TEMP_NORMAL) {
1794 dev_err(&netdev->dev,
1795 "Device temperature %d degrees C "
1796 "exceeds operating range."
1797 " Immediate action needed.\n",
1798 temp_val);
1799 }
1800 } else {
1801 if (adapter->temp == QLCNIC_TEMP_WARN) {
1802 dev_info(&netdev->dev,
1803 "Device temperature is now %d degrees C"
1804 " in normal range.\n", temp_val);
1805 }
1806 }
1807 adapter->temp = temp_state;
1808 return rv;
1809}
1810
1811void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
1812{
1813 struct net_device *netdev = adapter->netdev;
1814
1815 if (adapter->ahw.linkup && !linkup) {
1816 dev_info(&netdev->dev, "NIC Link is down\n");
1817 adapter->ahw.linkup = 0;
1818 if (netif_running(netdev)) {
1819 netif_carrier_off(netdev);
1820 netif_stop_queue(netdev);
1821 }
1822 } else if (!adapter->ahw.linkup && linkup) {
1823 dev_info(&netdev->dev, "NIC Link is up\n");
1824 adapter->ahw.linkup = 1;
1825 if (netif_running(netdev)) {
1826 netif_carrier_on(netdev);
1827 netif_wake_queue(netdev);
1828 }
1829 }
1830}
1831
1832static void qlcnic_tx_timeout(struct net_device *netdev)
1833{
1834 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1835
1836 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
1837 return;
1838
1839 dev_err(&netdev->dev, "transmit timeout, resetting.\n");
1840 schedule_work(&adapter->tx_timeout_task);
1841}
1842
1843static void qlcnic_tx_timeout_task(struct work_struct *work)
1844{
1845 struct qlcnic_adapter *adapter =
1846 container_of(work, struct qlcnic_adapter, tx_timeout_task);
1847
1848 if (!netif_running(adapter->netdev))
1849 return;
1850
1851 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1852 return;
1853
1854 if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
1855 goto request_reset;
1856
1857 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1858 if (!qlcnic_reset_context(adapter)) {
1859 adapter->netdev->trans_start = jiffies;
1860 return;
1861
1862 /* context reset failed, fall through for fw reset */
1863 }
1864
1865request_reset:
1866 adapter->need_fw_reset = 1;
1867 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00001868 QLCDB(adapter, DRV, "Resetting adapter\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001869}
1870
1871static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
1872{
1873 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1874 struct net_device_stats *stats = &netdev->stats;
1875
1876 memset(stats, 0, sizeof(*stats));
1877
1878 stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
1879 stats->tx_packets = adapter->stats.xmitfinished;
Sucheta Chakraborty7e382592010-05-17 01:22:10 +00001880 stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001881 stats->tx_bytes = adapter->stats.txbytes;
1882 stats->rx_dropped = adapter->stats.rxdropped;
1883 stats->tx_dropped = adapter->stats.txdropped;
1884
1885 return stats;
1886}
1887
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001888static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001889{
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001890 u32 status;
1891
1892 status = readl(adapter->isr_int_vec);
1893
1894 if (!(status & adapter->int_vec_bit))
1895 return IRQ_NONE;
1896
1897 /* check interrupt state machine, to be sure */
1898 status = readl(adapter->crb_int_state_reg);
1899 if (!ISR_LEGACY_INT_TRIGGERED(status))
1900 return IRQ_NONE;
1901
1902 writel(0xffffffff, adapter->tgt_status_reg);
1903 /* read twice to ensure write is flushed */
1904 readl(adapter->isr_int_vec);
1905 readl(adapter->isr_int_vec);
1906
Amit Kumar Salecha7eb98552010-02-01 05:24:59 +00001907 return IRQ_HANDLED;
1908}
1909
1910static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
1911{
1912 struct qlcnic_host_sds_ring *sds_ring = data;
1913 struct qlcnic_adapter *adapter = sds_ring->adapter;
1914
1915 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1916 goto done;
1917 else if (adapter->flags & QLCNIC_MSI_ENABLED) {
1918 writel(0xffffffff, adapter->tgt_status_reg);
1919 goto done;
1920 }
1921
1922 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
1923 return IRQ_NONE;
1924
1925done:
1926 adapter->diag_cnt++;
1927 qlcnic_enable_int(sds_ring);
1928 return IRQ_HANDLED;
1929}
1930
1931static irqreturn_t qlcnic_intr(int irq, void *data)
1932{
1933 struct qlcnic_host_sds_ring *sds_ring = data;
1934 struct qlcnic_adapter *adapter = sds_ring->adapter;
1935
1936 if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
1937 return IRQ_NONE;
1938
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00001939 napi_schedule(&sds_ring->napi);
1940
1941 return IRQ_HANDLED;
1942}
1943
1944static irqreturn_t qlcnic_msi_intr(int irq, void *data)
1945{
1946 struct qlcnic_host_sds_ring *sds_ring = data;
1947 struct qlcnic_adapter *adapter = sds_ring->adapter;
1948
1949 /* clear interrupt */
1950 writel(0xffffffff, adapter->tgt_status_reg);
1951
1952 napi_schedule(&sds_ring->napi);
1953 return IRQ_HANDLED;
1954}
1955
1956static irqreturn_t qlcnic_msix_intr(int irq, void *data)
1957{
1958 struct qlcnic_host_sds_ring *sds_ring = data;
1959
1960 napi_schedule(&sds_ring->napi);
1961 return IRQ_HANDLED;
1962}
1963
1964static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
1965{
1966 u32 sw_consumer, hw_consumer;
1967 int count = 0, i;
1968 struct qlcnic_cmd_buffer *buffer;
1969 struct pci_dev *pdev = adapter->pdev;
1970 struct net_device *netdev = adapter->netdev;
1971 struct qlcnic_skb_frag *frag;
1972 int done;
1973 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
1974
1975 if (!spin_trylock(&adapter->tx_clean_lock))
1976 return 1;
1977
1978 sw_consumer = tx_ring->sw_consumer;
1979 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
1980
1981 while (sw_consumer != hw_consumer) {
1982 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
1983 if (buffer->skb) {
1984 frag = &buffer->frag_array[0];
1985 pci_unmap_single(pdev, frag->dma, frag->length,
1986 PCI_DMA_TODEVICE);
1987 frag->dma = 0ULL;
1988 for (i = 1; i < buffer->frag_count; i++) {
1989 frag++;
1990 pci_unmap_page(pdev, frag->dma, frag->length,
1991 PCI_DMA_TODEVICE);
1992 frag->dma = 0ULL;
1993 }
1994
1995 adapter->stats.xmitfinished++;
1996 dev_kfree_skb_any(buffer->skb);
1997 buffer->skb = NULL;
1998 }
1999
2000 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
2001 if (++count >= MAX_STATUS_HANDLE)
2002 break;
2003 }
2004
2005 if (count && netif_running(netdev)) {
2006 tx_ring->sw_consumer = sw_consumer;
2007
2008 smp_mb();
2009
2010 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
2011 __netif_tx_lock(tx_ring->txq, smp_processor_id());
2012 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
2013 netif_wake_queue(netdev);
2014 adapter->tx_timeo_cnt = 0;
Sucheta Chakraborty8bfe8b92010-03-08 00:14:46 +00002015 adapter->stats.xmit_on++;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002016 }
2017 __netif_tx_unlock(tx_ring->txq);
2018 }
2019 }
2020 /*
2021 * If everything is freed up to consumer then check if the ring is full
2022 * If the ring is full then check if more needs to be freed and
2023 * schedule the call back again.
2024 *
2025 * This happens when there are 2 CPUs. One could be freeing and the
2026 * other filling it. If the ring is full when we get out of here and
2027 * the card has already interrupted the host then the host can miss the
2028 * interrupt.
2029 *
2030 * There is still a possible race condition and the host could miss an
2031 * interrupt. The card has to take care of this.
2032 */
2033 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
2034 done = (sw_consumer == hw_consumer);
2035 spin_unlock(&adapter->tx_clean_lock);
2036
2037 return done;
2038}
2039
2040static int qlcnic_poll(struct napi_struct *napi, int budget)
2041{
2042 struct qlcnic_host_sds_ring *sds_ring =
2043 container_of(napi, struct qlcnic_host_sds_ring, napi);
2044
2045 struct qlcnic_adapter *adapter = sds_ring->adapter;
2046
2047 int tx_complete;
2048 int work_done;
2049
2050 tx_complete = qlcnic_process_cmd_ring(adapter);
2051
2052 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
2053
2054 if ((work_done < budget) && tx_complete) {
2055 napi_complete(&sds_ring->napi);
2056 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2057 qlcnic_enable_int(sds_ring);
2058 }
2059
2060 return work_done;
2061}
2062
2063#ifdef CONFIG_NET_POLL_CONTROLLER
2064static void qlcnic_poll_controller(struct net_device *netdev)
2065{
2066 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2067 disable_irq(adapter->irq);
2068 qlcnic_intr(adapter->irq, adapter);
2069 enable_irq(adapter->irq);
2070}
2071#endif
2072
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002073static void
2074qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
2075{
2076 u32 val;
2077
2078 val = adapter->portnum & 0xf;
2079 val |= encoding << 7;
2080 val |= (jiffies - adapter->dev_rst_time) << 8;
2081
2082 QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
2083 adapter->dev_rst_time = jiffies;
2084}
2085
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002086static int
2087qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002088{
2089 u32 val;
2090
2091 WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
2092 state != QLCNIC_DEV_NEED_QUISCENT);
2093
2094 if (qlcnic_api_lock(adapter))
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002095 return -EIO;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002096
2097 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2098
2099 if (state == QLCNIC_DEV_NEED_RESET)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002100 QLC_DEV_SET_RST_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002101 else if (state == QLCNIC_DEV_NEED_QUISCENT)
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002102 QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002103
2104 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2105
2106 qlcnic_api_unlock(adapter);
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002107
2108 return 0;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002109}
2110
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002111static int
2112qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
2113{
2114 u32 val;
2115
2116 if (qlcnic_api_lock(adapter))
2117 return -EBUSY;
2118
2119 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002120 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002121 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2122
2123 qlcnic_api_unlock(adapter);
2124
2125 return 0;
2126}
2127
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002128static void
2129qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter)
2130{
2131 u32 val;
2132
2133 if (qlcnic_api_lock(adapter))
2134 goto err;
2135
2136 val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002137 QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002138 QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val);
2139
2140 if (!(val & 0x11111111))
2141 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
2142
2143 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002144 QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002145 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2146
2147 qlcnic_api_unlock(adapter);
2148err:
2149 adapter->fw_fail_cnt = 0;
2150 clear_bit(__QLCNIC_START_FW, &adapter->state);
2151 clear_bit(__QLCNIC_RESETTING, &adapter->state);
2152}
2153
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002154/* Grab api lock, before checking state */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002155static int
2156qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
2157{
2158 int act, state;
2159
2160 state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
2161 act = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
2162
2163 if (((state & 0x11111111) == (act & 0x11111111)) ||
2164 ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
2165 return 0;
2166 else
2167 return 1;
2168}
2169
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002170static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
2171{
2172 u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
2173
2174 if (val != QLCNIC_DRV_IDC_VER) {
2175 dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
2176 " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
2177 }
2178
2179 return 0;
2180}
2181
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002182static int
2183qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
2184{
2185 u32 val, prev_state;
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002186 u8 dev_init_timeo = adapter->dev_init_timeo;
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002187 u8 portnum = adapter->portnum;
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002188 u8 ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002189
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002190 if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
2191 return 1;
2192
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002193 if (qlcnic_api_lock(adapter))
2194 return -1;
2195
2196 val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002197 if (!(val & (1 << (portnum * 4)))) {
2198 QLC_DEV_SET_REF_CNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002199 QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002200 }
2201
2202 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002203 QLCDB(adapter, HW, "Device state = %u\n", prev_state);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002204
2205 switch (prev_state) {
2206 case QLCNIC_DEV_COLD:
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002207 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002208 QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002209 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002210 qlcnic_api_unlock(adapter);
2211 return 1;
2212
2213 case QLCNIC_DEV_READY:
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002214 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002215 qlcnic_api_unlock(adapter);
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002216 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002217
2218 case QLCNIC_DEV_NEED_RESET:
2219 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002220 QLC_DEV_SET_RST_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002221 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2222 break;
2223
2224 case QLCNIC_DEV_NEED_QUISCENT:
2225 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002226 QLC_DEV_SET_QSCNT_RDY(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002227 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2228 break;
2229
2230 case QLCNIC_DEV_FAILED:
Amit Kumar Salechaa7fc9482010-05-17 01:22:14 +00002231 dev_err(&adapter->pdev->dev, "Device in failed state.\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002232 qlcnic_api_unlock(adapter);
2233 return -1;
Amit Kumar Salechabbd8c6a2010-04-22 02:51:36 +00002234
2235 case QLCNIC_DEV_INITIALIZING:
2236 case QLCNIC_DEV_QUISCENT:
2237 break;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002238 }
2239
2240 qlcnic_api_unlock(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002241
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002242 do {
2243 msleep(1000);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002244 prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2245
2246 if (prev_state == QLCNIC_DEV_QUISCENT)
2247 continue;
2248 } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002249
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002250 if (!dev_init_timeo) {
2251 dev_err(&adapter->pdev->dev,
2252 "Waiting for device to initialize timeout\n");
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002253 return -1;
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002254 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002255
2256 if (qlcnic_api_lock(adapter))
2257 return -1;
2258
2259 val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
Amit Kumar Salecha6d2a4722010-04-22 02:51:38 +00002260 QLC_DEV_CLR_RST_QSCNT(val, portnum);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002261 QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
2262
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002263 ret = qlcnic_check_idc_ver(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002264 qlcnic_api_unlock(adapter);
2265
Sucheta Chakraborty96f81182010-05-13 03:07:47 +00002266 return ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002267}
2268
2269static void
2270qlcnic_fwinit_work(struct work_struct *work)
2271{
2272 struct qlcnic_adapter *adapter = container_of(work,
2273 struct qlcnic_adapter, fw_work.work);
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002274 u32 dev_state = 0xf, npar_state;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002275
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002276 if (qlcnic_api_lock(adapter))
2277 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002278
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002279 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2280 if (dev_state == QLCNIC_DEV_QUISCENT) {
2281 qlcnic_api_unlock(adapter);
2282 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2283 FW_POLL_DELAY * 2);
2284 return;
2285 }
2286
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002287 if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
2288 npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2289 if (npar_state == QLCNIC_DEV_NPAR_RDY) {
2290 qlcnic_api_unlock(adapter);
2291 goto wait_npar;
2292 } else {
2293 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2294 FW_POLL_DELAY);
2295 qlcnic_api_unlock(adapter);
2296 return;
2297 }
2298 }
2299
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002300 if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
2301 dev_err(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
2302 adapter->reset_ack_timeo);
2303 goto skip_ack_check;
2304 }
2305
2306 if (!qlcnic_check_drv_state(adapter)) {
2307skip_ack_check:
2308 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002309
2310 if (dev_state == QLCNIC_DEV_NEED_QUISCENT) {
2311 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2312 QLCNIC_DEV_QUISCENT);
2313 qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
2314 FW_POLL_DELAY * 2);
2315 QLCDB(adapter, DRV, "Quiscing the driver\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002316 qlcnic_idc_debug_info(adapter, 0);
2317
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002318 qlcnic_api_unlock(adapter);
2319 return;
2320 }
2321
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002322 if (dev_state == QLCNIC_DEV_NEED_RESET) {
2323 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
2324 QLCNIC_DEV_INITIALIZING);
2325 set_bit(__QLCNIC_START_FW, &adapter->state);
2326 QLCDB(adapter, DRV, "Restarting fw\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002327 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002328 }
2329
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002330 qlcnic_api_unlock(adapter);
2331
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002332 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002333 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2334 return;
2335 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002336 goto err_ret;
2337 }
2338
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002339 qlcnic_api_unlock(adapter);
Sucheta Chakrabortyaa5e18c2010-04-01 19:01:32 +00002340
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002341wait_npar:
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002342 dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002343 QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002344
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002345 switch (dev_state) {
Sucheta Chakrabortya5e463d2010-05-13 03:07:49 +00002346 case QLCNIC_DEV_QUISCENT:
2347 case QLCNIC_DEV_NEED_QUISCENT:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002348 case QLCNIC_DEV_NEED_RESET:
2349 qlcnic_schedule_work(adapter,
2350 qlcnic_fwinit_work, FW_POLL_DELAY);
2351 return;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002352 case QLCNIC_DEV_FAILED:
2353 break;
2354
2355 default:
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002356 if (!adapter->nic_ops->start_firmware(adapter)) {
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002357 qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
2358 return;
2359 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002360 }
2361
2362err_ret:
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002363 dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
2364 "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002365 netif_device_attach(adapter->netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002366 qlcnic_clr_all_drv_state(adapter);
2367}
2368
2369static void
2370qlcnic_detach_work(struct work_struct *work)
2371{
2372 struct qlcnic_adapter *adapter = container_of(work,
2373 struct qlcnic_adapter, fw_work.work);
2374 struct net_device *netdev = adapter->netdev;
2375 u32 status;
2376
2377 netif_device_detach(netdev);
2378
2379 qlcnic_down(adapter, netdev);
2380
Amit Kumar Salechace668442010-02-01 05:24:57 +00002381 rtnl_lock();
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002382 qlcnic_detach(adapter);
Amit Kumar Salechace668442010-02-01 05:24:57 +00002383 rtnl_unlock();
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002384
2385 status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
2386
2387 if (status & QLCNIC_RCODE_FATAL_ERROR)
2388 goto err_ret;
2389
2390 if (adapter->temp == QLCNIC_TEMP_PANIC)
2391 goto err_ret;
2392
Amit Kumar Salechaade91f82010-04-22 02:51:39 +00002393 if (qlcnic_set_drv_state(adapter, adapter->dev_state))
2394 goto err_ret;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002395
2396 adapter->fw_wait_cnt = 0;
2397
2398 qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
2399
2400 return;
2401
2402err_ret:
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002403 dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n",
2404 status, adapter->temp);
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002405 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002406 qlcnic_clr_all_drv_state(adapter);
2407
2408}
2409
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002410/*Transit to RESET state from READY state only */
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002411static void
2412qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
2413{
2414 u32 state;
2415
2416 if (qlcnic_api_lock(adapter))
2417 return;
2418
2419 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2420
Amit Kumar Salechaf73dfc52010-04-22 02:51:37 +00002421 if (state == QLCNIC_DEV_READY) {
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002422 QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002423 QLCDB(adapter, DRV, "NEED_RESET state set\n");
Sucheta Chakraborty6df900e2010-05-13 03:07:50 +00002424 qlcnic_idc_debug_info(adapter, 0);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002425 }
2426
2427 qlcnic_api_unlock(adapter);
2428}
2429
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002430/* Transit to NPAR READY state from NPAR NOT READY state */
2431static void
2432qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
2433{
2434 u32 state;
2435
Anirban Chakraborty9f26f542010-06-01 11:33:09 +00002436 if (qlcnic_api_lock(adapter))
2437 return;
2438
2439 state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
2440
2441 if (state != QLCNIC_DEV_NPAR_RDY) {
2442 QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE,
2443 QLCNIC_DEV_NPAR_RDY);
2444 QLCDB(adapter, DRV, "NPAR READY state set\n");
2445 }
2446
2447 qlcnic_api_unlock(adapter);
2448}
2449
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002450static void
2451qlcnic_schedule_work(struct qlcnic_adapter *adapter,
2452 work_func_t func, int delay)
2453{
2454 INIT_DELAYED_WORK(&adapter->fw_work, func);
2455 schedule_delayed_work(&adapter->fw_work, round_jiffies_relative(delay));
2456}
2457
2458static void
2459qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
2460{
2461 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
2462 msleep(10);
2463
2464 cancel_delayed_work_sync(&adapter->fw_work);
2465}
2466
2467static void
2468qlcnic_attach_work(struct work_struct *work)
2469{
2470 struct qlcnic_adapter *adapter = container_of(work,
2471 struct qlcnic_adapter, fw_work.work);
2472 struct net_device *netdev = adapter->netdev;
2473 int err;
2474
2475 if (netif_running(netdev)) {
2476 err = qlcnic_attach(adapter);
2477 if (err)
2478 goto done;
2479
2480 err = qlcnic_up(adapter, netdev);
2481 if (err) {
2482 qlcnic_detach(adapter);
2483 goto done;
2484 }
2485
2486 qlcnic_config_indev_addr(netdev, NETDEV_UP);
2487 }
2488
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002489done:
Amit Kumar Salecha34ce3622010-04-01 19:01:34 +00002490 netif_device_attach(netdev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002491 adapter->fw_fail_cnt = 0;
2492 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Amit Kumar Salecha1b95a832010-02-01 05:24:56 +00002493
2494 if (!qlcnic_clr_drv_state(adapter))
2495 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
2496 FW_POLL_DELAY);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002497}
2498
2499static int
2500qlcnic_check_health(struct qlcnic_adapter *adapter)
2501{
2502 u32 state = 0, heartbit;
2503 struct net_device *netdev = adapter->netdev;
2504
2505 if (qlcnic_check_temp(adapter))
2506 goto detach;
2507
Amit Kumar Salecha2372a5f2010-05-13 03:07:42 +00002508 if (adapter->need_fw_reset)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002509 qlcnic_dev_request_reset(adapter);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002510
2511 state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
2512 if (state == QLCNIC_DEV_NEED_RESET || state == QLCNIC_DEV_NEED_QUISCENT)
2513 adapter->need_fw_reset = 1;
2514
2515 heartbit = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
2516 if (heartbit != adapter->heartbit) {
2517 adapter->heartbit = heartbit;
2518 adapter->fw_fail_cnt = 0;
2519 if (adapter->need_fw_reset)
2520 goto detach;
2521 return 0;
2522 }
2523
2524 if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
2525 return 0;
2526
2527 qlcnic_dev_request_reset(adapter);
2528
2529 clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
2530
2531 dev_info(&netdev->dev, "firmware hang detected\n");
2532
2533detach:
2534 adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
2535 QLCNIC_DEV_NEED_RESET;
2536
2537 if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002538 !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
2539
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002540 qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
Amit Kumar Salecha65b5b422010-04-01 19:01:33 +00002541 QLCDB(adapter, DRV, "fw recovery scheduled.\n");
2542 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002543
2544 return 1;
2545}
2546
2547static void
2548qlcnic_fw_poll_work(struct work_struct *work)
2549{
2550 struct qlcnic_adapter *adapter = container_of(work,
2551 struct qlcnic_adapter, fw_work.work);
2552
2553 if (test_bit(__QLCNIC_RESETTING, &adapter->state))
2554 goto reschedule;
2555
2556
2557 if (qlcnic_check_health(adapter))
2558 return;
2559
2560reschedule:
2561 qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
2562}
2563
Anirban Chakraborty87eb7432010-06-03 07:50:56 +00002564static int
2565qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
2566{
2567 int err;
2568
2569 err = qlcnic_can_start_firmware(adapter);
2570 if (err)
2571 return err;
2572
2573 qlcnic_check_options(adapter);
2574
2575 adapter->need_fw_reset = 0;
2576
2577 return err;
2578}
2579
2580static int
2581qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
2582{
2583 return -EOPNOTSUPP;
2584}
2585
2586static int
2587qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
2588{
2589 return -EOPNOTSUPP;
2590}
2591
2592static int
2593qlcnicvf_set_ilb_mode(struct qlcnic_adapter *adapter)
2594{
2595 return -EOPNOTSUPP;
2596}
2597
2598static void
2599qlcnicvf_clear_ilb_mode(struct qlcnic_adapter *adapter)
2600{
2601 return;
2602}
2603
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002604static ssize_t
2605qlcnic_store_bridged_mode(struct device *dev,
2606 struct device_attribute *attr, const char *buf, size_t len)
2607{
2608 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2609 unsigned long new;
2610 int ret = -EINVAL;
2611
2612 if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
2613 goto err_out;
2614
2615 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2616 goto err_out;
2617
2618 if (strict_strtoul(buf, 2, &new))
2619 goto err_out;
2620
Anirban Chakraborty2e9d7222010-06-01 11:28:51 +00002621 if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002622 ret = len;
2623
2624err_out:
2625 return ret;
2626}
2627
2628static ssize_t
2629qlcnic_show_bridged_mode(struct device *dev,
2630 struct device_attribute *attr, char *buf)
2631{
2632 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2633 int bridged_mode = 0;
2634
2635 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
2636 bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
2637
2638 return sprintf(buf, "%d\n", bridged_mode);
2639}
2640
2641static struct device_attribute dev_attr_bridged_mode = {
2642 .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
2643 .show = qlcnic_show_bridged_mode,
2644 .store = qlcnic_store_bridged_mode,
2645};
2646
2647static ssize_t
2648qlcnic_store_diag_mode(struct device *dev,
2649 struct device_attribute *attr, const char *buf, size_t len)
2650{
2651 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2652 unsigned long new;
2653
2654 if (strict_strtoul(buf, 2, &new))
2655 return -EINVAL;
2656
2657 if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
2658 adapter->flags ^= QLCNIC_DIAG_ENABLED;
2659
2660 return len;
2661}
2662
2663static ssize_t
2664qlcnic_show_diag_mode(struct device *dev,
2665 struct device_attribute *attr, char *buf)
2666{
2667 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2668
2669 return sprintf(buf, "%d\n",
2670 !!(adapter->flags & QLCNIC_DIAG_ENABLED));
2671}
2672
2673static struct device_attribute dev_attr_diag_mode = {
2674 .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
2675 .show = qlcnic_show_diag_mode,
2676 .store = qlcnic_store_diag_mode,
2677};
2678
2679static int
2680qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
2681 loff_t offset, size_t size)
2682{
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002683 size_t crb_size = 4;
2684
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002685 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
2686 return -EIO;
2687
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002688 if (offset < QLCNIC_PCI_CRBSPACE) {
2689 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
2690 QLCNIC_PCI_CAMQM_END))
2691 crb_size = 8;
2692 else
2693 return -EINVAL;
2694 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002695
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002696 if ((size != crb_size) || (offset & (crb_size-1)))
2697 return -EINVAL;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002698
2699 return 0;
2700}
2701
2702static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07002703qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
2704 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002705 char *buf, loff_t offset, size_t size)
2706{
2707 struct device *dev = container_of(kobj, struct device, kobj);
2708 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2709 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002710 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002711 int ret;
2712
2713 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
2714 if (ret != 0)
2715 return ret;
2716
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002717 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
2718 qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
2719 memcpy(buf, &qmdata, size);
2720 } else {
2721 data = QLCRD32(adapter, offset);
2722 memcpy(buf, &data, size);
2723 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002724 return size;
2725}
2726
2727static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07002728qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
2729 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002730 char *buf, loff_t offset, size_t size)
2731{
2732 struct device *dev = container_of(kobj, struct device, kobj);
2733 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2734 u32 data;
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002735 u64 qmdata;
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002736 int ret;
2737
2738 ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
2739 if (ret != 0)
2740 return ret;
2741
Dhananjay Phadke897e8c72010-04-01 19:01:29 +00002742 if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
2743 memcpy(&qmdata, buf, size);
2744 qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
2745 } else {
2746 memcpy(&data, buf, size);
2747 QLCWR32(adapter, offset, data);
2748 }
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002749 return size;
2750}
2751
2752static int
2753qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
2754 loff_t offset, size_t size)
2755{
2756 if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
2757 return -EIO;
2758
2759 if ((size != 8) || (offset & 0x7))
2760 return -EIO;
2761
2762 return 0;
2763}
2764
2765static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07002766qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
2767 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002768 char *buf, loff_t offset, size_t size)
2769{
2770 struct device *dev = container_of(kobj, struct device, kobj);
2771 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2772 u64 data;
2773 int ret;
2774
2775 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
2776 if (ret != 0)
2777 return ret;
2778
2779 if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
2780 return -EIO;
2781
2782 memcpy(buf, &data, size);
2783
2784 return size;
2785}
2786
2787static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -07002788qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
2789 struct bin_attribute *attr,
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002790 char *buf, loff_t offset, size_t size)
2791{
2792 struct device *dev = container_of(kobj, struct device, kobj);
2793 struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
2794 u64 data;
2795 int ret;
2796
2797 ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
2798 if (ret != 0)
2799 return ret;
2800
2801 memcpy(&data, buf, size);
2802
2803 if (qlcnic_pci_mem_write_2M(adapter, offset, data))
2804 return -EIO;
2805
2806 return size;
2807}
2808
2809
2810static struct bin_attribute bin_attr_crb = {
2811 .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
2812 .size = 0,
2813 .read = qlcnic_sysfs_read_crb,
2814 .write = qlcnic_sysfs_write_crb,
2815};
2816
2817static struct bin_attribute bin_attr_mem = {
2818 .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
2819 .size = 0,
2820 .read = qlcnic_sysfs_read_mem,
2821 .write = qlcnic_sysfs_write_mem,
2822};
2823
2824static void
2825qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
2826{
2827 struct device *dev = &adapter->pdev->dev;
2828
2829 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
2830 if (device_create_file(dev, &dev_attr_bridged_mode))
2831 dev_warn(dev,
2832 "failed to create bridged_mode sysfs entry\n");
2833}
2834
2835static void
2836qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
2837{
2838 struct device *dev = &adapter->pdev->dev;
2839
2840 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
2841 device_remove_file(dev, &dev_attr_bridged_mode);
2842}
2843
2844static void
2845qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
2846{
2847 struct device *dev = &adapter->pdev->dev;
2848
2849 if (device_create_file(dev, &dev_attr_diag_mode))
2850 dev_info(dev, "failed to create diag_mode sysfs entry\n");
2851 if (device_create_bin_file(dev, &bin_attr_crb))
2852 dev_info(dev, "failed to create crb sysfs entry\n");
2853 if (device_create_bin_file(dev, &bin_attr_mem))
2854 dev_info(dev, "failed to create mem sysfs entry\n");
2855}
2856
2857
2858static void
2859qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
2860{
2861 struct device *dev = &adapter->pdev->dev;
2862
2863 device_remove_file(dev, &dev_attr_diag_mode);
2864 device_remove_bin_file(dev, &bin_attr_crb);
2865 device_remove_bin_file(dev, &bin_attr_mem);
2866}
2867
2868#ifdef CONFIG_INET
2869
2870#define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
2871
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002872static void
2873qlcnic_config_indev_addr(struct net_device *dev, unsigned long event)
2874{
2875 struct in_device *indev;
2876 struct qlcnic_adapter *adapter = netdev_priv(dev);
2877
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002878 indev = in_dev_get(dev);
2879 if (!indev)
2880 return;
2881
2882 for_ifa(indev) {
2883 switch (event) {
2884 case NETDEV_UP:
2885 qlcnic_config_ipaddr(adapter,
2886 ifa->ifa_address, QLCNIC_IP_UP);
2887 break;
2888 case NETDEV_DOWN:
2889 qlcnic_config_ipaddr(adapter,
2890 ifa->ifa_address, QLCNIC_IP_DOWN);
2891 break;
2892 default:
2893 break;
2894 }
2895 } endfor_ifa(indev);
2896
2897 in_dev_put(indev);
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002898}
2899
2900static int qlcnic_netdev_event(struct notifier_block *this,
2901 unsigned long event, void *ptr)
2902{
2903 struct qlcnic_adapter *adapter;
2904 struct net_device *dev = (struct net_device *)ptr;
2905
2906recheck:
2907 if (dev == NULL)
2908 goto done;
2909
2910 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2911 dev = vlan_dev_real_dev(dev);
2912 goto recheck;
2913 }
2914
2915 if (!is_qlcnic_netdev(dev))
2916 goto done;
2917
2918 adapter = netdev_priv(dev);
2919
2920 if (!adapter)
2921 goto done;
2922
2923 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2924 goto done;
2925
2926 qlcnic_config_indev_addr(dev, event);
2927done:
2928 return NOTIFY_DONE;
2929}
2930
2931static int
2932qlcnic_inetaddr_event(struct notifier_block *this,
2933 unsigned long event, void *ptr)
2934{
2935 struct qlcnic_adapter *adapter;
2936 struct net_device *dev;
2937
2938 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2939
2940 dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
2941
2942recheck:
2943 if (dev == NULL || !netif_running(dev))
2944 goto done;
2945
2946 if (dev->priv_flags & IFF_802_1Q_VLAN) {
2947 dev = vlan_dev_real_dev(dev);
2948 goto recheck;
2949 }
2950
2951 if (!is_qlcnic_netdev(dev))
2952 goto done;
2953
2954 adapter = netdev_priv(dev);
2955
Amit Kumar Salecha251a84c2010-05-13 03:07:46 +00002956 if (!adapter)
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002957 goto done;
2958
2959 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2960 goto done;
2961
2962 switch (event) {
2963 case NETDEV_UP:
2964 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
2965 break;
2966 case NETDEV_DOWN:
2967 qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
2968 break;
2969 default:
2970 break;
2971 }
2972
2973done:
2974 return NOTIFY_DONE;
2975}
2976
Amit Kumar Salechaaf19b492010-01-13 00:37:25 +00002977static struct notifier_block qlcnic_netdev_cb = {
2978 .notifier_call = qlcnic_netdev_event,
2979};
2980
2981static struct notifier_block qlcnic_inetaddr_cb = {
2982 .notifier_call = qlcnic_inetaddr_event,
2983};
2984#else
2985static void
2986qlcnic_config_indev_addr(struct net_device *dev, unsigned long event)
2987{ }
2988#endif
2989
2990static struct pci_driver qlcnic_driver = {
2991 .name = qlcnic_driver_name,
2992 .id_table = qlcnic_pci_tbl,
2993 .probe = qlcnic_probe,
2994 .remove = __devexit_p(qlcnic_remove),
2995#ifdef CONFIG_PM
2996 .suspend = qlcnic_suspend,
2997 .resume = qlcnic_resume,
2998#endif
2999 .shutdown = qlcnic_shutdown
3000};
3001
3002static int __init qlcnic_init_module(void)
3003{
3004
3005 printk(KERN_INFO "%s\n", qlcnic_driver_string);
3006
3007#ifdef CONFIG_INET
3008 register_netdevice_notifier(&qlcnic_netdev_cb);
3009 register_inetaddr_notifier(&qlcnic_inetaddr_cb);
3010#endif
3011
3012
3013 return pci_register_driver(&qlcnic_driver);
3014}
3015
3016module_init(qlcnic_init_module);
3017
3018static void __exit qlcnic_exit_module(void)
3019{
3020
3021 pci_unregister_driver(&qlcnic_driver);
3022
3023#ifdef CONFIG_INET
3024 unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
3025 unregister_netdevice_notifier(&qlcnic_netdev_cb);
3026#endif
3027}
3028
3029module_exit(qlcnic_exit_module);