blob: 2953a83bc856d6e2af36ee3599955561b74d66d7 [file] [log] [blame]
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001/*
Dhananjay Phadke5d242f12009-02-25 15:57:56 +00002 * Copyright (C) 2003 - 2009 NetXen, Inc.
Amit S. Kale3d396eb2006-10-21 15:33:03 -04003 * All rights reserved.
Amit S. Kale80922fb2006-12-04 09:18:00 -08004 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -04005 * 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.
Amit S. Kale80922fb2006-12-04 09:18:00 -08009 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040010 * 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.
Amit S. Kale80922fb2006-12-04 09:18:00 -080014 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040015 * 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.
Amit S. Kale80922fb2006-12-04 09:18:00 -080019 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040020 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
Amit S. Kale80922fb2006-12-04 09:18:00 -080022 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040023 * Contact Information:
24 * info@netxen.com
Dhananjay Phadke5d242f12009-02-25 15:57:56 +000025 * NetXen Inc,
26 * 18922 Forge Drive
27 * Cupertino, CA 95014-0701
Amit S. Kale3d396eb2006-10-21 15:33:03 -040028 *
29 */
30
Amit S. Kalecb8011a2006-11-29 09:00:10 -080031#include <linux/vmalloc.h>
Amit S. Kaleed25ffa2006-12-04 09:23:25 -080032#include <linux/highmem.h>
Amit S. Kale3d396eb2006-10-21 15:33:03 -040033#include "netxen_nic_hw.h"
34
35#include "netxen_nic.h"
Amit S. Kale3d396eb2006-10-21 15:33:03 -040036#include "netxen_nic_phan_reg.h"
Amit S. Kale3d396eb2006-10-21 15:33:03 -040037
Jeff Garzik1494a812006-11-07 05:12:16 -050038#include <linux/dma-mapping.h>
Dhananjay Phadke391587c2009-01-14 20:48:11 -080039#include <linux/if_vlan.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030040#include <net/ip.h>
Dhananjay Phadke00977782009-01-16 11:03:25 -080041#include <linux/ipv6.h>
Jeff Garzik1494a812006-11-07 05:12:16 -050042
Amit S. Kale3d396eb2006-10-21 15:33:03 -040043MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver");
44MODULE_LICENSE("GPL");
45MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
46
dhananjay@netxen.comceded322007-07-19 14:41:09 +053047char netxen_nic_driver_name[] = "netxen_nic";
Amit S. Kale3d396eb2006-10-21 15:33:03 -040048static char netxen_nic_driver_string[] = "NetXen Network Driver version "
Amit S. Kalecb8011a2006-11-29 09:00:10 -080049 NETXEN_NIC_LINUX_VERSIONID;
Amit S. Kale3d396eb2006-10-21 15:33:03 -040050
Dhananjay Phadke29566402008-07-21 19:44:04 -070051static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
52
53/* Default to restricted 1G auto-neg mode */
54static int wol_port_mode = 5;
55
56static int use_msi = 1;
57
58static int use_msi_x = 1;
59
Amit S. Kale3d396eb2006-10-21 15:33:03 -040060/* Local functions to NetXen NIC driver */
61static int __devinit netxen_nic_probe(struct pci_dev *pdev,
Dhananjay Phadke29566402008-07-21 19:44:04 -070062 const struct pci_device_id *ent);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040063static void __devexit netxen_nic_remove(struct pci_dev *pdev);
64static int netxen_nic_open(struct net_device *netdev);
65static int netxen_nic_close(struct net_device *netdev);
66static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *);
67static void netxen_tx_timeout(struct net_device *netdev);
David Howells6d5aefb2006-12-05 19:36:26 +000068static void netxen_tx_timeout_task(struct work_struct *work);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040069static void netxen_watchdog(unsigned long);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070070static int netxen_nic_poll(struct napi_struct *napi, int budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040071#ifdef CONFIG_NET_POLL_CONTROLLER
72static void netxen_nic_poll_controller(struct net_device *netdev);
73#endif
Jeff Garzik1494a812006-11-07 05:12:16 -050074static irqreturn_t netxen_intr(int irq, void *data);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -070075static irqreturn_t netxen_msi_intr(int irq, void *data);
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -080076static irqreturn_t netxen_msix_intr(int irq, void *data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040077
78/* PCI Device ID Table */
Al Viro70081ac2008-06-23 02:04:50 +010079#define ENTRY(device) \
Dhananjay Phadke040dec32008-09-12 06:55:14 -070080 {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
Al Viro70081ac2008-06-23 02:04:50 +010081 .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
82
Amit S. Kale3d396eb2006-10-21 15:33:03 -040083static struct pci_device_id netxen_pci_tbl[] __devinitdata = {
Dhananjay Phadke040dec32008-09-12 06:55:14 -070084 ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
85 ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
86 ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
87 ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
88 ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
89 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
90 ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
91 ENTRY(PCI_DEVICE_ID_NX3031),
Amit S. Kale3d396eb2006-10-21 15:33:03 -040092 {0,}
93};
94
95MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
96
Adrian Bunkb15551302008-03-31 02:22:18 +030097static struct workqueue_struct *netxen_workq;
98#define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp)
99#define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq)
100
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800101static void netxen_watchdog(unsigned long);
102
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700103static uint32_t crb_cmd_producer[4] = {
104 CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
105 CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
106};
107
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -0700108void
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700109netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
110 uint32_t crb_producer)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700111{
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700112 adapter->pci_write_normalize(adapter,
113 adapter->crb_addr_cmd_producer, crb_producer);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700114}
115
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700116static uint32_t crb_cmd_consumer[4] = {
117 CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
118 CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
119};
120
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700121static inline void
122netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
123 u32 crb_consumer)
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700124{
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700125 adapter->pci_write_normalize(adapter,
126 adapter->crb_addr_cmd_consumer, crb_consumer);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700127}
128
Dhananjay Phadke29566402008-07-21 19:44:04 -0700129static uint32_t msi_tgt_status[8] = {
Dhananjay Phadke443be792008-03-17 19:59:48 -0700130 ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700131 ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
132 ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
133 ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
Dhananjay Phadke443be792008-03-17 19:59:48 -0700134};
135
Dhananjay Phadke29566402008-07-21 19:44:04 -0700136static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
137
Dhananjay Phadke092bc572008-08-08 00:08:43 -0700138static inline void netxen_nic_disable_int(struct netxen_adapter *adapter)
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700139{
Dhananjay Phadke092bc572008-08-08 00:08:43 -0700140 adapter->pci_write_normalize(adapter, adapter->crb_intr_mask, 0);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700141}
142
Dhananjay Phadke092bc572008-08-08 00:08:43 -0700143static inline void netxen_nic_enable_int(struct netxen_adapter *adapter)
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700144{
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700145 adapter->pci_write_normalize(adapter, adapter->crb_intr_mask, 0x1);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700146
Dhananjay Phadke092bc572008-08-08 00:08:43 -0700147 if (!NETXEN_IS_MSI_FAMILY(adapter))
148 adapter->pci_write_immediate(adapter,
149 adapter->legacy_intr.tgt_mask_reg, 0xfbff);
Stephen Hemminger4638aef12007-07-05 16:13:55 -0700150}
151
Dhananjay Phadke29566402008-07-21 19:44:04 -0700152static int nx_set_dma_mask(struct netxen_adapter *adapter, uint8_t revision_id)
153{
154 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000155 uint64_t mask, cmask;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700156
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000157 adapter->pci_using_dac = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700158
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000159 mask = DMA_32BIT_MASK;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700160 /*
161 * Consistent DMA mask is set to 32 bit because it cannot be set to
162 * 35 bits. For P3 also leave it at 32 bits for now. Only the rings
163 * come off this pool.
164 */
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000165 cmask = DMA_32BIT_MASK;
166
167#ifndef CONFIG_IA64
168 if (revision_id >= NX_P3_B0)
169 mask = DMA_39BIT_MASK;
170 else if (revision_id == NX_P2_C1)
171 mask = DMA_35BIT_MASK;
172#endif
Dhananjay Phadke29566402008-07-21 19:44:04 -0700173 if (pci_set_dma_mask(pdev, mask) == 0 &&
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000174 pci_set_consistent_dma_mask(pdev, cmask) == 0) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700175 adapter->pci_using_dac = 1;
176 return 0;
177 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700178
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000179 return -EIO;
180}
181
182/* Update addressable range if firmware supports it */
183static int
184nx_update_dma_mask(struct netxen_adapter *adapter)
185{
186 int change, shift, err;
187 uint64_t mask, old_mask;
188 struct pci_dev *pdev = adapter->pdev;
189
190 change = 0;
191
192 shift = netxen_nic_reg_read(adapter, CRB_DMA_SHIFT);
193 if (shift >= 32)
194 return 0;
195
196 if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
197 change = 1;
198 else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
199 change = 1;
200
201 if (change) {
202 old_mask = pdev->dma_mask;
203 mask = (1ULL<<(32+shift)) - 1;
204
205 err = pci_set_dma_mask(pdev, mask);
206 if (err)
207 return pci_set_dma_mask(pdev, old_mask);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700208 }
209
Dhananjay Phadke29566402008-07-21 19:44:04 -0700210 return 0;
211}
212
213static void netxen_check_options(struct netxen_adapter *adapter)
214{
215 switch (adapter->ahw.boardcfg.board_type) {
216 case NETXEN_BRDTYPE_P3_HMEZ:
217 case NETXEN_BRDTYPE_P3_XG_LOM:
218 case NETXEN_BRDTYPE_P3_10G_CX4:
219 case NETXEN_BRDTYPE_P3_10G_CX4_LP:
220 case NETXEN_BRDTYPE_P3_IMEZ:
221 case NETXEN_BRDTYPE_P3_10G_SFP_PLUS:
Dhananjay Phadkea70f9392008-08-01 03:14:56 -0700222 case NETXEN_BRDTYPE_P3_10G_SFP_QT:
223 case NETXEN_BRDTYPE_P3_10G_SFP_CT:
Dhananjay Phadke29566402008-07-21 19:44:04 -0700224 case NETXEN_BRDTYPE_P3_10G_XFP:
225 case NETXEN_BRDTYPE_P3_10000_BASE_T:
226 adapter->msix_supported = !!use_msi_x;
227 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
228 break;
229
230 case NETXEN_BRDTYPE_P2_SB31_10G:
231 case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
232 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
233 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
234 adapter->msix_supported = 0;
235 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
236 break;
237
238 case NETXEN_BRDTYPE_P3_REF_QG:
239 case NETXEN_BRDTYPE_P3_4_GB:
240 case NETXEN_BRDTYPE_P3_4_GB_MM:
Dhananjay Phadkeec68db62008-08-20 13:52:48 -0700241 adapter->msix_supported = !!use_msi_x;
Dhananjay Phadkec7860a22009-01-14 20:48:32 -0800242 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
Dhananjay Phadkea70f9392008-08-01 03:14:56 -0700243 break;
244
Dhananjay Phadke29566402008-07-21 19:44:04 -0700245 case NETXEN_BRDTYPE_P2_SB35_4G:
246 case NETXEN_BRDTYPE_P2_SB31_2G:
247 adapter->msix_supported = 0;
248 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
249 break;
250
Dhananjay Phadkec7860a22009-01-14 20:48:32 -0800251 case NETXEN_BRDTYPE_P3_10G_TP:
252 adapter->msix_supported = !!use_msi_x;
253 if (adapter->ahw.board_type == NETXEN_NIC_XGBE)
254 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_10G;
255 else
256 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
257 break;
258
Dhananjay Phadke29566402008-07-21 19:44:04 -0700259 default:
260 adapter->msix_supported = 0;
261 adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G;
262
263 printk(KERN_WARNING "Unknown board type(0x%x)\n",
264 adapter->ahw.boardcfg.board_type);
265 break;
266 }
267
268 adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST;
269 adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS;
270 adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS;
271
272 adapter->max_possible_rss_rings = 1;
273 return;
274}
275
276static int
277netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
278{
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800279 u32 val, timeout;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700280
281 if (first_boot == 0x55555555) {
282 /* This is the first boot after power up */
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800283 adapter->pci_write_normalize(adapter,
284 NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
285
286 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
287 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700288
289 /* PCI bus master workaround */
290 adapter->hw_read_wx(adapter,
291 NETXEN_PCIE_REG(0x4), &first_boot, 4);
292 if (!(first_boot & 0x4)) {
293 first_boot |= 0x4;
294 adapter->hw_write_wx(adapter,
295 NETXEN_PCIE_REG(0x4), &first_boot, 4);
296 adapter->hw_read_wx(adapter,
297 NETXEN_PCIE_REG(0x4), &first_boot, 4);
298 }
299
300 /* This is the first boot after power up */
301 adapter->hw_read_wx(adapter,
302 NETXEN_ROMUSB_GLB_SW_RESET, &first_boot, 4);
303 if (first_boot != 0x80000f) {
304 /* clear the register for future unloads/loads */
305 adapter->pci_write_normalize(adapter,
306 NETXEN_CAM_RAM(0x1fc), 0);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800307 return -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700308 }
309
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800310 /* Start P2 boot loader */
311 val = adapter->pci_read_normalize(adapter,
312 NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
313 adapter->pci_write_normalize(adapter,
314 NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
315 timeout = 0;
316 do {
317 msleep(1);
318 val = adapter->pci_read_normalize(adapter,
319 NETXEN_CAM_RAM(0x1fc));
320
321 if (++timeout > 5000)
322 return -EIO;
323
324 } while (val == NETXEN_BDINFO_MAGIC);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700325 }
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800326 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700327}
328
329static void netxen_set_port_mode(struct netxen_adapter *adapter)
330{
331 u32 val, data;
332
333 val = adapter->ahw.boardcfg.board_type;
334 if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
335 (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
336 if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
337 data = NETXEN_PORT_MODE_802_3_AP;
338 adapter->hw_write_wx(adapter,
339 NETXEN_PORT_MODE_ADDR, &data, 4);
340 } else if (port_mode == NETXEN_PORT_MODE_XG) {
341 data = NETXEN_PORT_MODE_XG;
342 adapter->hw_write_wx(adapter,
343 NETXEN_PORT_MODE_ADDR, &data, 4);
344 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
345 data = NETXEN_PORT_MODE_AUTO_NEG_1G;
346 adapter->hw_write_wx(adapter,
347 NETXEN_PORT_MODE_ADDR, &data, 4);
348 } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
349 data = NETXEN_PORT_MODE_AUTO_NEG_XG;
350 adapter->hw_write_wx(adapter,
351 NETXEN_PORT_MODE_ADDR, &data, 4);
352 } else {
353 data = NETXEN_PORT_MODE_AUTO_NEG;
354 adapter->hw_write_wx(adapter,
355 NETXEN_PORT_MODE_ADDR, &data, 4);
356 }
357
358 if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
359 (wol_port_mode != NETXEN_PORT_MODE_XG) &&
360 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
361 (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
362 wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
363 }
364 adapter->hw_write_wx(adapter, NETXEN_WOL_PORT_MODE,
365 &wol_port_mode, 4);
366 }
367}
368
Dhananjay Phadke29566402008-07-21 19:44:04 -0700369static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
370{
371 u32 control;
372 int pos;
373
374 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
375 if (pos) {
376 pci_read_config_dword(pdev, pos, &control);
377 if (enable)
378 control |= PCI_MSIX_FLAGS_ENABLE;
379 else
380 control = 0;
381 pci_write_config_dword(pdev, pos, control);
382 }
383}
384
385static void netxen_init_msix_entries(struct netxen_adapter *adapter)
386{
387 int i;
388
389 for (i = 0; i < MSIX_ENTRIES_PER_ADAPTER; i++)
390 adapter->msix_entries[i].entry = i;
391}
392
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700393static int
394netxen_read_mac_addr(struct netxen_adapter *adapter)
395{
396 int i;
397 unsigned char *p;
398 __le64 mac_addr;
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700399 struct net_device *netdev = adapter->netdev;
400 struct pci_dev *pdev = adapter->pdev;
401
402 if (netxen_is_flash_supported(adapter) != 0)
403 return -EIO;
404
405 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
406 if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
407 return -EIO;
408 } else {
409 if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
410 return -EIO;
411 }
412
413 p = (unsigned char *)&mac_addr;
414 for (i = 0; i < 6; i++)
415 netdev->dev_addr[i] = *(p + 5 - i);
416
417 memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
418
419 /* set station address */
420
Johannes Berge1749612008-10-27 15:59:26 -0700421 if (!is_valid_ether_addr(netdev->perm_addr))
422 dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
423 else
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -0700424 adapter->macaddr_set(adapter, netdev->dev_addr);
425
426 return 0;
427}
428
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800429static void netxen_set_multicast_list(struct net_device *dev)
430{
431 struct netxen_adapter *adapter = netdev_priv(dev);
432
433 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
434 netxen_p3_nic_set_multi(dev);
435 else
436 netxen_p2_nic_set_multi(dev);
437}
438
439static const struct net_device_ops netxen_netdev_ops = {
440 .ndo_open = netxen_nic_open,
441 .ndo_stop = netxen_nic_close,
442 .ndo_start_xmit = netxen_nic_xmit_frame,
443 .ndo_get_stats = netxen_nic_get_stats,
444 .ndo_validate_addr = eth_validate_addr,
445 .ndo_set_multicast_list = netxen_set_multicast_list,
446 .ndo_set_mac_address = netxen_nic_set_mac,
447 .ndo_change_mtu = netxen_nic_change_mtu,
448 .ndo_tx_timeout = netxen_tx_timeout,
449#ifdef CONFIG_NET_POLL_CONTROLLER
450 .ndo_poll_controller = netxen_nic_poll_controller,
451#endif
452};
453
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800454static void
455netxen_setup_intr(struct netxen_adapter *adapter)
456{
457 struct netxen_legacy_intr_set *legacy_intrp;
458 struct pci_dev *pdev = adapter->pdev;
459
460 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
461 adapter->intr_scheme = -1;
462 adapter->msi_mode = -1;
463
464 if (adapter->ahw.revision_id >= NX_P3_B0)
465 legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
466 else
467 legacy_intrp = &legacy_intr[0];
468 adapter->legacy_intr.int_vec_bit = legacy_intrp->int_vec_bit;
469 adapter->legacy_intr.tgt_status_reg = legacy_intrp->tgt_status_reg;
470 adapter->legacy_intr.tgt_mask_reg = legacy_intrp->tgt_mask_reg;
471 adapter->legacy_intr.pci_int_reg = legacy_intrp->pci_int_reg;
472
473 netxen_set_msix_bit(pdev, 0);
474
475 if (adapter->msix_supported) {
476
477 netxen_init_msix_entries(adapter);
478 if (pci_enable_msix(pdev, adapter->msix_entries,
479 MSIX_ENTRIES_PER_ADAPTER))
480 goto request_msi;
481
482 adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
483 netxen_set_msix_bit(pdev, 1);
484 dev_info(&pdev->dev, "using msi-x interrupts\n");
485
486 } else {
487request_msi:
488 if (use_msi && !pci_enable_msi(pdev)) {
489 adapter->flags |= NETXEN_NIC_MSI_ENABLED;
490 dev_info(&pdev->dev, "using msi interrupts\n");
491 } else
492 dev_info(&pdev->dev, "using legacy interrupts\n");
493 }
494}
495
496static void
497netxen_teardown_intr(struct netxen_adapter *adapter)
498{
499 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
500 pci_disable_msix(adapter->pdev);
501 if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
502 pci_disable_msi(adapter->pdev);
503}
504
505static void
506netxen_cleanup_pci_map(struct netxen_adapter *adapter)
507{
508 if (adapter->ahw.db_base != NULL)
509 iounmap(adapter->ahw.db_base);
510 if (adapter->ahw.pci_base0 != NULL)
511 iounmap(adapter->ahw.pci_base0);
512 if (adapter->ahw.pci_base1 != NULL)
513 iounmap(adapter->ahw.pci_base1);
514 if (adapter->ahw.pci_base2 != NULL)
515 iounmap(adapter->ahw.pci_base2);
516}
517
518static int
519netxen_setup_pci_map(struct netxen_adapter *adapter)
520{
521 void __iomem *mem_ptr0 = NULL;
522 void __iomem *mem_ptr1 = NULL;
523 void __iomem *mem_ptr2 = NULL;
524 void __iomem *db_ptr = NULL;
525
526 unsigned long first_page_group_end;
527 unsigned long first_page_group_start;
528 unsigned long mem_base, mem_len, db_base, db_len = 0, pci_len0 = 0;
529
530 struct pci_dev *pdev = adapter->pdev;
531 int pci_func = adapter->ahw.pci_func;
532
533 int err = 0;
534
535 /*
536 * Set the CRB window to invalid. If any register in window 0 is
537 * accessed it should set the window to 0 and then reset it to 1.
538 */
539 adapter->curr_window = 255;
540 adapter->ahw.qdr_sn_window = -1;
541 adapter->ahw.ddr_mn_window = -1;
542
543 /* remap phys address */
544 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
545 mem_len = pci_resource_len(pdev, 0);
546 pci_len0 = 0;
547
548 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
549 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
550 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
551 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
552 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_128M;
553 adapter->pci_write_normalize = netxen_nic_pci_write_normalize_128M;
554 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
555 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
556 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
557
558 /* 128 Meg of memory */
559 if (mem_len == NETXEN_PCI_128MB_SIZE) {
560 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
561 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
562 SECOND_PAGE_GROUP_SIZE);
563 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
564 THIRD_PAGE_GROUP_SIZE);
565 first_page_group_start = FIRST_PAGE_GROUP_START;
566 first_page_group_end = FIRST_PAGE_GROUP_END;
567 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
568 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
569 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
570 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
571 first_page_group_start = 0;
572 first_page_group_end = 0;
573 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
574 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
575 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
576 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
577 adapter->pci_write_immediate =
578 netxen_nic_pci_write_immediate_2M;
579 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_2M;
580 adapter->pci_write_normalize =
581 netxen_nic_pci_write_normalize_2M;
582 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
583 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
584 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
585
586 mem_ptr0 = pci_ioremap_bar(pdev, 0);
587 if (mem_ptr0 == NULL) {
588 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
589 return -EIO;
590 }
591 pci_len0 = mem_len;
592 first_page_group_start = 0;
593 first_page_group_end = 0;
594
595 adapter->ahw.ddr_mn_window = 0;
596 adapter->ahw.qdr_sn_window = 0;
597
598 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
599 (pci_func * 0x20);
600 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
601 if (pci_func < 4)
602 adapter->ahw.ms_win_crb += (pci_func * 0x20);
603 else
604 adapter->ahw.ms_win_crb +=
605 0xA0 + ((pci_func - 4) * 0x10);
606 } else {
607 return -EIO;
608 }
609
610 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
611
612 adapter->ahw.pci_base0 = mem_ptr0;
613 adapter->ahw.pci_len0 = pci_len0;
614 adapter->ahw.first_page_group_start = first_page_group_start;
615 adapter->ahw.first_page_group_end = first_page_group_end;
616 adapter->ahw.pci_base1 = mem_ptr1;
617 adapter->ahw.pci_base2 = mem_ptr2;
618
619 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
620 goto skip_doorbell;
621
622 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
623 db_len = pci_resource_len(pdev, 4);
624
625 if (db_len == 0) {
626 printk(KERN_ERR "%s: doorbell is disabled\n",
627 netxen_nic_driver_name);
628 err = -EIO;
629 goto err_out;
630 }
631
632 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
633 if (!db_ptr) {
634 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
635 netxen_nic_driver_name);
636 err = -EIO;
637 goto err_out;
638 }
639
640skip_doorbell:
641 adapter->ahw.db_base = db_ptr;
642 adapter->ahw.db_len = db_len;
643 return 0;
644
645err_out:
646 netxen_cleanup_pci_map(adapter);
647 return err;
648}
649
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800650static int
651netxen_start_firmware(struct netxen_adapter *adapter)
652{
653 int val, err, first_boot;
654 struct pci_dev *pdev = adapter->pdev;
655
656 first_boot = adapter->pci_read_normalize(adapter,
657 NETXEN_CAM_RAM(0x1fc));
658
659 err = netxen_check_hw_init(adapter, first_boot);
660 if (err) {
661 dev_err(&pdev->dev, "error in init HW init sequence\n");
662 return err;
663 }
664
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800665 if (first_boot != 0x55555555) {
666 adapter->pci_write_normalize(adapter,
667 CRB_CMDPEG_STATE, 0);
668 netxen_pinit_from_rom(adapter, 0);
669 msleep(1);
670 }
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000671
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000672 netxen_nic_reg_write(adapter, CRB_DMA_SHIFT, 0x55555555);
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000673 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
674 netxen_set_port_mode(adapter);
675
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800676 netxen_load_firmware(adapter);
677
678 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
679
680 /* Initialize multicast addr pool owners */
681 val = 0x7654;
682 if (adapter->ahw.board_type == NETXEN_NIC_XGBE)
683 val |= 0x0f000000;
684 netxen_crb_writelit_adapter(adapter,
685 NETXEN_MAC_ADDR_CNTL_REG, val);
686
687 }
688
689 err = netxen_initialize_adapter_offload(adapter);
690 if (err)
691 return err;
692
693 /*
694 * Tell the hardware our version number.
695 */
696 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
697 | ((_NETXEN_NIC_LINUX_MINOR << 8))
698 | (_NETXEN_NIC_LINUX_SUBVERSION);
699 adapter->pci_write_normalize(adapter, CRB_DRIVER_VERSION, val);
700
701 /* Handshake with the card before we register the devices. */
702 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
703 if (err) {
704 netxen_free_adapter_offload(adapter);
705 return err;
706 }
707
708 return 0;
709}
710
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000711static int
712netxen_nic_request_irq(struct netxen_adapter *adapter)
713{
714 irq_handler_t handler;
715 unsigned long flags = IRQF_SAMPLE_RANDOM;
716 struct net_device *netdev = adapter->netdev;
717
718 if ((adapter->msi_mode != MSI_MODE_MULTIFUNC) ||
719 (adapter->intr_scheme != INTR_SCHEME_PERPORT)) {
720 printk(KERN_ERR "%s: Firmware interrupt scheme is "
721 "incompatible with driver\n",
722 netdev->name);
723 adapter->driver_mismatch = 1;
724 return -EINVAL;
725 }
726
727 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
728 handler = netxen_msix_intr;
729 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
730 handler = netxen_msi_intr;
731 else {
732 flags |= IRQF_SHARED;
733 handler = netxen_intr;
734 }
735 adapter->irq = netdev->irq;
736
737 return request_irq(adapter->irq, handler,
738 flags, netdev->name, adapter);
739}
740
741static int
742netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
743{
744 int err;
745
746 err = adapter->init_port(adapter, adapter->physical_port);
747 if (err) {
748 printk(KERN_ERR "%s: Failed to initialize port %d\n",
749 netxen_nic_driver_name, adapter->portnum);
750 return err;
751 }
752 adapter->macaddr_set(adapter, netdev->dev_addr);
753
754 netxen_nic_set_link_parameters(adapter);
755
756 netxen_set_multicast_list(netdev);
757 if (adapter->set_mtu)
758 adapter->set_mtu(adapter, netdev->mtu);
759
760 adapter->ahw.linkup = 0;
761 mod_timer(&adapter->watchdog_timer, jiffies);
762
763 napi_enable(&adapter->napi);
764 netxen_nic_enable_int(adapter);
765
766 return 0;
767}
768
769static void
770netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
771{
772 netif_carrier_off(netdev);
773 netif_stop_queue(netdev);
774 napi_disable(&adapter->napi);
775
776 if (adapter->stop_port)
777 adapter->stop_port(adapter);
778
779 netxen_nic_disable_int(adapter);
780
781 netxen_release_tx_buffers(adapter);
782
783 FLUSH_SCHEDULED_WORK();
784 del_timer_sync(&adapter->watchdog_timer);
785}
786
787
788static int
789netxen_nic_attach(struct netxen_adapter *adapter)
790{
791 struct net_device *netdev = adapter->netdev;
792 struct pci_dev *pdev = adapter->pdev;
793 int err, ctx, ring;
794
795 err = netxen_init_firmware(adapter);
796 if (err != 0) {
797 printk(KERN_ERR "Failed to init firmware\n");
798 return -EIO;
799 }
800
801 if (adapter->fw_major < 4)
802 adapter->max_rds_rings = 3;
803 else
804 adapter->max_rds_rings = 2;
805
806 err = netxen_alloc_sw_resources(adapter);
807 if (err) {
808 printk(KERN_ERR "%s: Error in setting sw resources\n",
809 netdev->name);
810 return err;
811 }
812
813 netxen_nic_clear_stats(adapter);
814
815 err = netxen_alloc_hw_resources(adapter);
816 if (err) {
817 printk(KERN_ERR "%s: Error in setting hw resources\n",
818 netdev->name);
819 goto err_out_free_sw;
820 }
821
822 if (adapter->fw_major < 4) {
823 adapter->crb_addr_cmd_producer =
824 crb_cmd_producer[adapter->portnum];
825 adapter->crb_addr_cmd_consumer =
826 crb_cmd_consumer[adapter->portnum];
827
828 netxen_nic_update_cmd_producer(adapter, 0);
829 netxen_nic_update_cmd_consumer(adapter, 0);
830 }
831
832 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
833 for (ring = 0; ring < adapter->max_rds_rings; ring++)
834 netxen_post_rx_buffers(adapter, ctx, ring);
835 }
836
837 err = netxen_nic_request_irq(adapter);
838 if (err) {
839 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
840 netdev->name);
841 goto err_out_free_rxbuf;
842 }
843
844 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
845 return 0;
846
847err_out_free_rxbuf:
848 netxen_release_rx_buffers(adapter);
849 netxen_free_hw_resources(adapter);
850err_out_free_sw:
851 netxen_free_sw_resources(adapter);
852 return err;
853}
854
855static void
856netxen_nic_detach(struct netxen_adapter *adapter)
857{
858 if (adapter->irq)
859 free_irq(adapter->irq, adapter);
860
861 netxen_release_rx_buffers(adapter);
862 netxen_free_hw_resources(adapter);
863 netxen_free_sw_resources(adapter);
864
865 adapter->is_up = 0;
866}
867
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400868static int __devinit
869netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
870{
871 struct net_device *netdev = NULL;
872 struct netxen_adapter *adapter = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700873 int i = 0, err;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800874 int first_driver;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700875 u32 val;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700876 int pci_func_id = PCI_FUNC(pdev->devfn);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700877 uint8_t revision_id;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400878
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700879 if (pci_func_id == 0)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700880 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700881
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700882 if (pdev->class != 0x020000) {
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700883 printk(KERN_DEBUG "NetXen function %d, class %x will not "
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700884 "be enabled.\n",pci_func_id, pdev->class);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800885 return -ENODEV;
886 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700887
Dhananjay Phadke11d89d62008-08-08 00:08:45 -0700888 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
889 printk(KERN_WARNING "NetXen chip revisions between 0x%x-0x%x"
890 "will not be enabled.\n",
891 NX_P3_A0, NX_P3_B1);
892 return -ENODEV;
893 }
894
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400895 if ((err = pci_enable_device(pdev)))
896 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700897
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400898 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
899 err = -ENODEV;
900 goto err_out_disable_pdev;
901 }
902
903 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
904 goto err_out_disable_pdev;
905
906 pci_set_master(pdev);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700907
908 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
909 if(!netdev) {
910 printk(KERN_ERR"%s: Failed to allocate memory for the "
911 "device block.Check system memory resource"
912 " usage.\n", netxen_nic_driver_name);
913 goto err_out_free_res;
914 }
915
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700916 SET_NETDEV_DEV(netdev, &pdev->dev);
917
Wang Chen4cf16532008-11-12 23:38:14 -0800918 adapter = netdev_priv(netdev);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700919 adapter->netdev = netdev;
920 adapter->pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700921 adapter->ahw.pci_func = pci_func_id;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700922
923 revision_id = pdev->revision;
924 adapter->ahw.revision_id = revision_id;
925
926 err = nx_set_dma_mask(adapter, revision_id);
927 if (err)
928 goto err_out_free_netdev;
929
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700930 rwlock_init(&adapter->adapter_lock);
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700931
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800932 err = netxen_setup_pci_map(adapter);
933 if (err)
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700934 goto err_out_free_netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400935
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700936 netif_napi_add(netdev, &adapter->napi,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700937 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
938
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530939 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700940 adapter->portnum = pci_func_id;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530941 adapter->status &= ~NETXEN_NETDEV_STATUS;
Dhananjay Phadke200eef22007-09-03 10:33:35 +0530942 adapter->rx_csum = 1;
Dhananjay Phadke623621b2008-07-21 19:44:01 -0700943 adapter->mc_enabled = 0;
Dhananjay Phadke83821a02008-08-01 03:14:58 -0700944 if (NX_IS_REVISION_P3(revision_id))
Dhananjay Phadke29566402008-07-21 19:44:04 -0700945 adapter->max_mc_count = 38;
Dhananjay Phadke83821a02008-08-01 03:14:58 -0700946 else
Dhananjay Phadke29566402008-07-21 19:44:04 -0700947 adapter->max_mc_count = 16;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700948
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800949 netdev->netdev_ops = &netxen_netdev_ops;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -0700950 netdev->watchdog_timeo = 2*HZ;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700951
952 netxen_nic_change_mtu(netdev, netdev->mtu);
953
954 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800955
Dhananjay Phadkecdff1032009-01-26 12:34:57 -0800956 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
957 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
958
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -0700959 if (NX_IS_REVISION_P3(revision_id)) {
Dhananjay Phadkecdff1032009-01-26 12:34:57 -0800960 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
961 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -0700962 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700963
Dhananjay Phadkecdff1032009-01-26 12:34:57 -0800964 if (adapter->pci_using_dac) {
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700965 netdev->features |= NETIF_F_HIGHDMA;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -0800966 netdev->vlan_features |= NETIF_F_HIGHDMA;
967 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700968
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700969 if (netxen_nic_get_board_info(adapter) != 0) {
970 printk("%s: Error getting board config info.\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700971 netxen_nic_driver_name);
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700972 err = -EIO;
973 goto err_out_iounmap;
974 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700975
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700976 netxen_initialize_adapter_ops(adapter);
977
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700978 /* Mezz cards have PCI function 0,2,3 enabled */
Dhananjay Phadkedc515f22008-06-15 22:59:43 -0700979 switch (adapter->ahw.boardcfg.board_type) {
980 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
981 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
982 if (pci_func_id >= 2)
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700983 adapter->portnum = pci_func_id - 2;
Dhananjay Phadkedc515f22008-06-15 22:59:43 -0700984 break;
985 default:
986 break;
987 }
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700988
Dhananjay Phadke29566402008-07-21 19:44:04 -0700989 /*
990 * This call will setup various max rx/tx counts.
991 * It must be done before any buffer/ring allocations.
992 */
993 netxen_check_options(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400994
Dhananjay Phadke29566402008-07-21 19:44:04 -0700995 first_driver = 0;
996 if (NX_IS_REVISION_P3(revision_id)) {
997 if (adapter->ahw.pci_func == 0)
998 first_driver = 1;
999 } else {
1000 if (adapter->portnum == 0)
1001 first_driver = 1;
1002 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001003
Dhananjay Phadke29566402008-07-21 19:44:04 -07001004 if (first_driver) {
Dhananjay Phadkeba599d42009-02-24 16:38:22 -08001005 err = netxen_start_firmware(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001006 if (err)
1007 goto err_out_iounmap;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -08001008 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001009
Dhananjay Phadke1f434f62009-03-02 16:02:16 +00001010 nx_update_dma_mask(adapter);
1011
Dhananjay Phadke29566402008-07-21 19:44:04 -07001012 netxen_nic_flash_print(adapter);
1013
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001014 if (NX_IS_REVISION_P3(revision_id)) {
1015 adapter->hw_read_wx(adapter,
1016 NETXEN_MIU_MN_CONTROL, &val, 4);
1017 adapter->ahw.cut_through = (val & 0x4) ? 1 : 0;
1018 dev_info(&pdev->dev, "firmware running in %s mode\n",
1019 adapter->ahw.cut_through ? "cut through" : "legacy");
1020 }
1021
Dhananjay Phadke29566402008-07-21 19:44:04 -07001022 /*
1023 * See if the firmware gave us a virtual-physical port mapping.
1024 */
1025 adapter->physical_port = adapter->portnum;
Dhananjay Phadke044fad02009-02-24 03:42:59 -08001026 if (adapter->fw_major < 4) {
1027 i = adapter->pci_read_normalize(adapter,
1028 CRB_V2P(adapter->portnum));
1029 if (i != 0x55555555)
1030 adapter->physical_port = i;
1031 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001032
1033 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
1034
1035 netxen_set_msix_bit(pdev, 0);
1036
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001037 netxen_setup_intr(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001038
1039 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1040 netdev->irq = adapter->msix_entries[0].vector;
1041 else
1042 netdev->irq = pdev->irq;
1043
1044 err = netxen_receive_peg_ready(adapter);
1045 if (err)
1046 goto err_out_disable_msi;
1047
1048 init_timer(&adapter->watchdog_timer);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001049 adapter->watchdog_timer.function = &netxen_watchdog;
1050 adapter->watchdog_timer.data = (unsigned long)adapter;
1051 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
1052 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1053
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -07001054 err = netxen_read_mac_addr(adapter);
1055 if (err)
1056 dev_warn(&pdev->dev, "failed to read mac addr\n");
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001057
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001058 netif_carrier_off(netdev);
1059 netif_stop_queue(netdev);
1060
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001061 if ((err = register_netdev(netdev))) {
1062 printk(KERN_ERR "%s: register_netdev failed port #%d"
1063 " aborting\n", netxen_nic_driver_name,
1064 adapter->portnum);
1065 err = -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001066 goto err_out_disable_msi;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001067 }
1068
1069 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001070
Dhananjay Phadke29566402008-07-21 19:44:04 -07001071 switch (adapter->ahw.board_type) {
1072 case NETXEN_NIC_GBE:
1073 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1074 adapter->netdev->name);
1075 break;
1076 case NETXEN_NIC_XGBE:
1077 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1078 adapter->netdev->name);
1079 break;
1080 }
1081
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001082 return 0;
1083
Dhananjay Phadke29566402008-07-21 19:44:04 -07001084err_out_disable_msi:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001085 netxen_teardown_intr(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001086
Dhananjay Phadke29566402008-07-21 19:44:04 -07001087 if (first_driver)
1088 netxen_free_adapter_offload(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001089
Dhananjay Phadke29566402008-07-21 19:44:04 -07001090err_out_iounmap:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001091 netxen_cleanup_pci_map(adapter);
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001092
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001093err_out_free_netdev:
1094 free_netdev(netdev);
1095
1096err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001097 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001098
1099err_out_disable_pdev:
Dhananjay Phadke29566402008-07-21 19:44:04 -07001100 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001101 pci_disable_device(pdev);
1102 return err;
1103}
1104
1105static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1106{
1107 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001108 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001109
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001110 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001111 if (adapter == NULL)
1112 return;
1113
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001114 netdev = adapter->netdev;
1115
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301116 unregister_netdev(netdev);
1117
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301118 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001119 netxen_nic_detach(adapter);
Dhananjay Phadke06e9d9f2009-01-14 20:49:22 -08001120
1121 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1122 netxen_p3_free_mac_list(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301123 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001124
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001125 if (adapter->portnum == 0)
1126 netxen_free_adapter_offload(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301127
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001128 netxen_teardown_intr(adapter);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301129
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001130 netxen_cleanup_pci_map(adapter);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001131
1132 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301133 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001134 pci_set_drvdata(pdev, NULL);
1135
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001136 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001137}
1138
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001139static int netxen_nic_open(struct net_device *netdev)
1140{
Wang Chen4cf16532008-11-12 23:38:14 -08001141 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001142 int err = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001143
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001144 if (adapter->driver_mismatch)
1145 return -EIO;
1146
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001147 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001148 err = netxen_nic_attach(adapter);
1149 if (err)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001150 return err;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001151 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001152
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001153 err = netxen_nic_up(adapter, netdev);
1154 if (err)
1155 goto err_out;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001156
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001157 netif_start_queue(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001158
1159 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001160
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001161err_out:
1162 netxen_nic_detach(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001163 return err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001164}
1165
1166/*
1167 * netxen_nic_close - Disables a network interface entry point
1168 */
1169static int netxen_nic_close(struct net_device *netdev)
1170{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001171 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001172
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001173 netxen_nic_down(adapter, netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001174 return 0;
1175}
1176
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001177static bool netxen_tso_check(struct net_device *netdev,
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001178 struct cmd_desc_type0 *desc, struct sk_buff *skb)
1179{
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001180 bool tso = false;
1181 u8 opcode = TX_ETHER_PKT;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001182 __be16 protocol = skb->protocol;
1183 u16 flags = 0;
1184
Harvey Harrison09640e62009-02-01 00:45:17 -08001185 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001186 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)skb->data;
1187 protocol = vh->h_vlan_encapsulated_proto;
1188 flags = FLAGS_VLAN_TAGGED;
1189 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001190
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001191 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1192 skb_shinfo(skb)->gso_size > 0) {
1193
1194 desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1195 desc->total_hdr_length =
1196 skb_transport_offset(skb) + tcp_hdrlen(skb);
1197
Harvey Harrison09640e62009-02-01 00:45:17 -08001198 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001199 TX_TCP_LSO6 : TX_TCP_LSO;
1200 tso = true;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001201
1202 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001203 u8 l4proto;
1204
Harvey Harrison09640e62009-02-01 00:45:17 -08001205 if (protocol == cpu_to_be16(ETH_P_IP)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001206 l4proto = ip_hdr(skb)->protocol;
1207
1208 if (l4proto == IPPROTO_TCP)
1209 opcode = TX_TCP_PKT;
1210 else if(l4proto == IPPROTO_UDP)
1211 opcode = TX_UDP_PKT;
Harvey Harrison09640e62009-02-01 00:45:17 -08001212 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001213 l4proto = ipv6_hdr(skb)->nexthdr;
1214
1215 if (l4proto == IPPROTO_TCP)
1216 opcode = TX_TCPV6_PKT;
1217 else if(l4proto == IPPROTO_UDP)
1218 opcode = TX_UDPV6_PKT;
1219 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001220 }
1221 desc->tcp_hdr_offset = skb_transport_offset(skb);
1222 desc->ip_hdr_offset = skb_network_offset(skb);
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001223 netxen_set_tx_flags_opcode(desc, flags, opcode);
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001224 return tso;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001225}
1226
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001227static void
1228netxen_clean_tx_dma_mapping(struct pci_dev *pdev,
1229 struct netxen_cmd_buffer *pbuf, int last)
1230{
1231 int k;
1232 struct netxen_skb_frag *buffrag;
1233
1234 buffrag = &pbuf->frag_array[0];
1235 pci_unmap_single(pdev, buffrag->dma,
1236 buffrag->length, PCI_DMA_TODEVICE);
1237
1238 for (k = 1; k < last; k++) {
1239 buffrag = &pbuf->frag_array[k];
1240 pci_unmap_page(pdev, buffrag->dma,
1241 buffrag->length, PCI_DMA_TODEVICE);
1242 }
1243}
1244
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001245static inline void
1246netxen_clear_cmddesc(u64 *desc)
1247{
1248 int i;
1249 for (i = 0; i < 8; i++)
1250 desc[i] = 0ULL;
1251}
1252
1253static int
1254netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001255{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001256 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001257 struct netxen_hardware_context *hw = &adapter->ahw;
1258 unsigned int first_seg_len = skb->len - skb->data_len;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001259 struct netxen_cmd_buffer *pbuf;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001260 struct netxen_skb_frag *buffrag;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001261 struct cmd_desc_type0 *hwdesc;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001262 struct pci_dev *pdev = adapter->pdev;
1263 dma_addr_t temp_dma;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001264 int i, k;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001265
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001266 u32 producer, consumer;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001267 int frag_count, no_of_desc;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001268 u32 num_txd = adapter->max_tx_desc_count;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001269 bool is_tso = false;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001270
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001271 frag_count = skb_shinfo(skb)->nr_frags + 1;
1272
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001273 /* There 4 fragments per descriptor */
1274 no_of_desc = (frag_count + 3) >> 2;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001275
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001276 producer = adapter->cmd_producer;
1277 smp_mb();
1278 consumer = adapter->last_cmd_consumer;
1279 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
1280 netif_stop_queue(netdev);
1281 smp_mb();
1282 return NETDEV_TX_BUSY;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001283 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001284
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001285 /* Copy the descriptors into the hardware */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001286 hwdesc = &hw->cmd_desc_head[producer];
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001287 netxen_clear_cmddesc((u64 *)hwdesc);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001288 /* Take skb->data itself */
1289 pbuf = &adapter->cmd_buf_arr[producer];
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001290
1291 is_tso = netxen_tso_check(netdev, hwdesc, skb);
1292
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001293 pbuf->skb = skb;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001294 pbuf->frag_count = frag_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001295 buffrag = &pbuf->frag_array[0];
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001296 temp_dma = pci_map_single(pdev, skb->data, first_seg_len,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001297 PCI_DMA_TODEVICE);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001298 if (pci_dma_mapping_error(pdev, temp_dma))
1299 goto drop_packet;
1300
1301 buffrag->dma = temp_dma;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001302 buffrag->length = first_seg_len;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001303 netxen_set_tx_frags_len(hwdesc, frag_count, skb->len);
1304 netxen_set_tx_port(hwdesc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001305
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001306 hwdesc->buffer_length[0] = cpu_to_le16(first_seg_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001307 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1308
1309 for (i = 1, k = 1; i < frag_count; i++, k++) {
1310 struct skb_frag_struct *frag;
1311 int len, temp_len;
1312 unsigned long offset;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001313
1314 /* move to next desc. if there is a need */
1315 if ((i & 0x3) == 0) {
1316 k = 0;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001317 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001318 hwdesc = &hw->cmd_desc_head[producer];
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001319 netxen_clear_cmddesc((u64 *)hwdesc);
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001320 pbuf = &adapter->cmd_buf_arr[producer];
1321 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001322 }
1323 frag = &skb_shinfo(skb)->frags[i - 1];
1324 len = frag->size;
1325 offset = frag->page_offset;
1326
1327 temp_len = len;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001328 temp_dma = pci_map_page(pdev, frag->page, offset,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001329 len, PCI_DMA_TODEVICE);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001330 if (pci_dma_mapping_error(pdev, temp_dma)) {
1331 netxen_clean_tx_dma_mapping(pdev, pbuf, i);
1332 goto drop_packet;
1333 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001334
1335 buffrag++;
1336 buffrag->dma = temp_dma;
1337 buffrag->length = temp_len;
1338
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001339 hwdesc->buffer_length[k] = cpu_to_le16(temp_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001340 switch (k) {
1341 case 0:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001342 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1343 break;
1344 case 1:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001345 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1346 break;
1347 case 2:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001348 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1349 break;
1350 case 3:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001351 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1352 break;
1353 }
1354 frag++;
1355 }
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001356 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001357
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001358 /* For LSO, we need to copy the MAC/IP/TCP headers into
1359 * the descriptor ring
1360 */
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001361 if (is_tso) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001362 int hdr_len, first_hdr_len, more_hdr;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001363 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001364 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1365 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001366 more_hdr = 1;
1367 } else {
1368 first_hdr_len = hdr_len;
1369 more_hdr = 0;
1370 }
1371 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1372 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001373 pbuf = &adapter->cmd_buf_arr[producer];
1374 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001375
1376 /* copy the first 64 bytes */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001377 memcpy(((void *)hwdesc) + 2,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001378 (void *)(skb->data), first_hdr_len);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001379 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001380
1381 if (more_hdr) {
1382 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001383 pbuf = &adapter->cmd_buf_arr[producer];
1384 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001385 /* copy the next 64 bytes - should be enough except
1386 * for pathological case
1387 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001388 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1389 hwdesc,
1390 (hdr_len -
1391 first_hdr_len));
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001392 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001393 }
1394 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001395
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001396 adapter->cmd_producer = producer;
Dhananjay Phadke5dc16262007-12-31 10:08:57 -08001397 adapter->stats.txbytes += skb->len;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001398
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001399 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001400
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001401 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001402 netdev->trans_start = jiffies;
1403
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001404 return NETDEV_TX_OK;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001405
1406drop_packet:
1407 adapter->stats.txdropped++;
1408 dev_kfree_skb_any(skb);
1409 return NETDEV_TX_OK;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001410}
1411
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001412static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1413{
1414 struct net_device *netdev = adapter->netdev;
1415 uint32_t temp, temp_state, temp_val;
1416 int rv = 0;
1417
1418 temp = adapter->pci_read_normalize(adapter, CRB_TEMP_STATE);
1419
1420 temp_state = nx_get_temp_state(temp);
1421 temp_val = nx_get_temp_val(temp);
1422
1423 if (temp_state == NX_TEMP_PANIC) {
1424 printk(KERN_ALERT
1425 "%s: Device temperature %d degrees C exceeds"
1426 " maximum allowed. Hardware has been shut down.\n",
1427 netxen_nic_driver_name, temp_val);
1428
1429 netif_carrier_off(netdev);
1430 netif_stop_queue(netdev);
1431 rv = 1;
1432 } else if (temp_state == NX_TEMP_WARN) {
1433 if (adapter->temp == NX_TEMP_NORMAL) {
1434 printk(KERN_ALERT
1435 "%s: Device temperature %d degrees C "
1436 "exceeds operating range."
1437 " Immediate action needed.\n",
1438 netxen_nic_driver_name, temp_val);
1439 }
1440 } else {
1441 if (adapter->temp == NX_TEMP_WARN) {
1442 printk(KERN_INFO
1443 "%s: Device temperature is now %d degrees C"
1444 " in normal range.\n", netxen_nic_driver_name,
1445 temp_val);
1446 }
1447 }
1448 adapter->temp = temp_state;
1449 return rv;
1450}
1451
1452static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1453{
1454 struct net_device *netdev = adapter->netdev;
1455 u32 val, port, linkup;
1456
1457 port = adapter->physical_port;
1458
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001459 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1460 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE_P3);
1461 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1462 linkup = (val == XG_LINK_UP_P3);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001463 } else {
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001464 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE);
1465 if (adapter->ahw.board_type == NETXEN_NIC_GBE)
1466 linkup = (val >> port) & 1;
1467 else {
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001468 val = (val >> port*8) & 0xff;
1469 linkup = (val == XG_LINK_UP);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001470 }
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001471 }
1472
1473 if (adapter->ahw.linkup && !linkup) {
1474 printk(KERN_INFO "%s: %s NIC Link is down\n",
1475 netxen_nic_driver_name, netdev->name);
1476 adapter->ahw.linkup = 0;
1477 if (netif_running(netdev)) {
1478 netif_carrier_off(netdev);
1479 netif_stop_queue(netdev);
1480 }
Dhananjay Phadkec7860a22009-01-14 20:48:32 -08001481
1482 netxen_nic_set_link_parameters(adapter);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001483 } else if (!adapter->ahw.linkup && linkup) {
1484 printk(KERN_INFO "%s: %s NIC Link is up\n",
1485 netxen_nic_driver_name, netdev->name);
1486 adapter->ahw.linkup = 1;
1487 if (netif_running(netdev)) {
1488 netif_carrier_on(netdev);
1489 netif_wake_queue(netdev);
1490 }
Dhananjay Phadkec7860a22009-01-14 20:48:32 -08001491
1492 netxen_nic_set_link_parameters(adapter);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001493 }
1494}
1495
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001496static void netxen_watchdog(unsigned long v)
1497{
1498 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001499
1500 SCHEDULE_WORK(&adapter->watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001501}
1502
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001503void netxen_watchdog_task(struct work_struct *work)
1504{
1505 struct netxen_adapter *adapter =
1506 container_of(work, struct netxen_adapter, watchdog_task);
1507
1508 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1509 return;
1510
1511 netxen_nic_handle_phy_intr(adapter);
1512
Dhananjay Phadke922c4f22008-08-08 00:08:41 -07001513 if (netif_running(adapter->netdev))
1514 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001515}
1516
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001517static void netxen_tx_timeout(struct net_device *netdev)
1518{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001519 struct netxen_adapter *adapter = (struct netxen_adapter *)
1520 netdev_priv(netdev);
1521 SCHEDULE_WORK(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001522}
1523
David Howells6d5aefb2006-12-05 19:36:26 +00001524static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001525{
Jeff Garzik47906542007-11-23 21:23:36 -05001526 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001527 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001528
1529 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001530 netxen_nic_driver_name, adapter->netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001531
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001532 netxen_nic_disable_int(adapter);
1533 napi_disable(&adapter->napi);
1534
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001535 adapter->netdev->trans_start = jiffies;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001536
1537 napi_enable(&adapter->napi);
1538 netxen_nic_enable_int(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001539 netif_wake_queue(adapter->netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001540}
1541
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001542/*
1543 * netxen_nic_get_stats - Get System Network Statistics
1544 * @netdev: network interface device structure
1545 */
1546struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1547{
1548 struct netxen_adapter *adapter = netdev_priv(netdev);
1549 struct net_device_stats *stats = &adapter->net_stats;
1550
1551 memset(stats, 0, sizeof(*stats));
1552
1553 /* total packets received */
1554 stats->rx_packets = adapter->stats.no_rcv;
1555 /* total packets transmitted */
1556 stats->tx_packets = adapter->stats.xmitedframes +
1557 adapter->stats.xmitfinished;
1558 /* total bytes received */
1559 stats->rx_bytes = adapter->stats.rxbytes;
1560 /* total bytes transmitted */
1561 stats->tx_bytes = adapter->stats.txbytes;
1562 /* bad packets received */
1563 stats->rx_errors = adapter->stats.rcvdbadskb;
1564 /* packet transmit problems */
1565 stats->tx_errors = adapter->stats.nocmddescriptor;
1566 /* no space in linux buffers */
1567 stats->rx_dropped = adapter->stats.rxdropped;
1568 /* no space available in linux */
1569 stats->tx_dropped = adapter->stats.txdropped;
1570
1571 return stats;
1572}
1573
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001574static irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001575{
Jeff Garzik1daec862007-10-19 03:14:03 -04001576 struct netxen_adapter *adapter = data;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001577 u32 status = 0;
1578
1579 status = adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1580
1581 if (!(status & adapter->legacy_intr.int_vec_bit))
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001582 return IRQ_NONE;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301583
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001584 if (adapter->ahw.revision_id >= NX_P3_B1) {
1585 /* check interrupt state machine, to be sure */
1586 status = adapter->pci_read_immediate(adapter,
1587 ISR_INT_STATE_REG);
1588 if (!ISR_LEGACY_INT_TRIGGERED(status))
1589 return IRQ_NONE;
1590
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001591 } else {
1592 unsigned long our_int = 0;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001593
1594 our_int = adapter->pci_read_normalize(adapter, CRB_INT_VECTOR);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001595
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001596 /* not our interrupt */
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001597 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001598 return IRQ_NONE;
1599
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001600 /* claim interrupt */
1601 adapter->pci_write_normalize(adapter,
1602 CRB_INT_VECTOR, (our_int & 0xffffffff));
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301603 }
1604
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001605 /* clear interrupt */
1606 if (adapter->fw_major < 4)
1607 netxen_nic_disable_int(adapter);
1608
1609 adapter->pci_write_immediate(adapter,
1610 adapter->legacy_intr.tgt_status_reg,
1611 0xffffffff);
1612 /* read twice to ensure write is flushed */
1613 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1614 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1615
1616 napi_schedule(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001617
1618 return IRQ_HANDLED;
1619}
1620
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001621static irqreturn_t netxen_msi_intr(int irq, void *data)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001622{
1623 struct netxen_adapter *adapter = data;
1624
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001625 /* clear interrupt */
1626 adapter->pci_write_immediate(adapter,
1627 msi_tgt_status[adapter->ahw.pci_func], 0xffffffff);
1628
1629 napi_schedule(&adapter->napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001630 return IRQ_HANDLED;
1631}
1632
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001633static irqreturn_t netxen_msix_intr(int irq, void *data)
1634{
1635 struct netxen_adapter *adapter = data;
1636
1637 napi_schedule(&adapter->napi);
1638 return IRQ_HANDLED;
1639}
1640
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001641static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001642{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001643 struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001644 int tx_complete;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001645 int ctx;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001646 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001647
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001648 tx_complete = netxen_process_cmd_ring(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001649
Amit S. Kale80922fb2006-12-04 09:18:00 -08001650 work_done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001651 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
1652 /*
1653 * Fairness issue. This will give undue weight to the
1654 * receive context 0.
1655 */
1656
1657 /*
1658 * To avoid starvation, we give each of our receivers,
1659 * a fraction of the quota. Sometimes, it might happen that we
1660 * have enough quota to process every packet, but since all the
1661 * packets are on one context, it gets only half of the quota,
1662 * and ends up not processing it.
1663 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001664 work_done += netxen_process_rcv_ring(adapter, ctx,
1665 budget / MAX_RCV_CTX);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001666 }
1667
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001668 if ((work_done < budget) && tx_complete) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001669 napi_complete(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001670 netxen_nic_enable_int(adapter);
1671 }
1672
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001673 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001674}
1675
1676#ifdef CONFIG_NET_POLL_CONTROLLER
1677static void netxen_nic_poll_controller(struct net_device *netdev)
1678{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001679 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001680 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001681 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001682 enable_irq(adapter->irq);
1683}
1684#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001685
1686static struct pci_driver netxen_driver = {
1687 .name = netxen_nic_driver_name,
1688 .id_table = netxen_pci_tbl,
1689 .probe = netxen_nic_probe,
1690 .remove = __devexit_p(netxen_nic_remove)
1691};
1692
1693/* Driver Registration on NetXen card */
1694
1695static int __init netxen_init_module(void)
1696{
Al Viro79ea13c2008-01-24 02:06:46 -08001697 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001698 return -ENOMEM;
1699
Amit S. Kale184231b2007-01-22 06:52:53 -08001700 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001701}
1702
1703module_init(netxen_init_module);
1704
1705static void __exit netxen_exit_module(void)
1706{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001707 pci_unregister_driver(&netxen_driver);
Amit S. Kale9de06612007-02-21 06:37:06 -08001708 destroy_workqueue(netxen_workq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001709}
1710
1711module_exit(netxen_exit_module);