blob: 3b4d923f947d5420722026155ed3dfac309c4923 [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;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000793 int err, ring;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000794
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
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000832 for (ring = 0; ring < adapter->max_rds_rings; ring++)
833 netxen_post_rx_buffers(adapter, ring);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000834
835 err = netxen_nic_request_irq(adapter);
836 if (err) {
837 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
838 netdev->name);
839 goto err_out_free_rxbuf;
840 }
841
842 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
843 return 0;
844
845err_out_free_rxbuf:
846 netxen_release_rx_buffers(adapter);
847 netxen_free_hw_resources(adapter);
848err_out_free_sw:
849 netxen_free_sw_resources(adapter);
850 return err;
851}
852
853static void
854netxen_nic_detach(struct netxen_adapter *adapter)
855{
856 if (adapter->irq)
857 free_irq(adapter->irq, adapter);
858
859 netxen_release_rx_buffers(adapter);
860 netxen_free_hw_resources(adapter);
861 netxen_free_sw_resources(adapter);
862
863 adapter->is_up = 0;
864}
865
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400866static int __devinit
867netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
868{
869 struct net_device *netdev = NULL;
870 struct netxen_adapter *adapter = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700871 int i = 0, err;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800872 int first_driver;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700873 u32 val;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700874 int pci_func_id = PCI_FUNC(pdev->devfn);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700875 uint8_t revision_id;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400876
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700877 if (pci_func_id == 0)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700878 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700879
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700880 if (pdev->class != 0x020000) {
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700881 printk(KERN_DEBUG "NetXen function %d, class %x will not "
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700882 "be enabled.\n",pci_func_id, pdev->class);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800883 return -ENODEV;
884 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700885
Dhananjay Phadke11d89d62008-08-08 00:08:45 -0700886 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
887 printk(KERN_WARNING "NetXen chip revisions between 0x%x-0x%x"
888 "will not be enabled.\n",
889 NX_P3_A0, NX_P3_B1);
890 return -ENODEV;
891 }
892
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400893 if ((err = pci_enable_device(pdev)))
894 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700895
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400896 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
897 err = -ENODEV;
898 goto err_out_disable_pdev;
899 }
900
901 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
902 goto err_out_disable_pdev;
903
904 pci_set_master(pdev);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700905
906 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
907 if(!netdev) {
908 printk(KERN_ERR"%s: Failed to allocate memory for the "
909 "device block.Check system memory resource"
910 " usage.\n", netxen_nic_driver_name);
911 goto err_out_free_res;
912 }
913
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700914 SET_NETDEV_DEV(netdev, &pdev->dev);
915
Wang Chen4cf16532008-11-12 23:38:14 -0800916 adapter = netdev_priv(netdev);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700917 adapter->netdev = netdev;
918 adapter->pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700919 adapter->ahw.pci_func = pci_func_id;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700920
921 revision_id = pdev->revision;
922 adapter->ahw.revision_id = revision_id;
923
924 err = nx_set_dma_mask(adapter, revision_id);
925 if (err)
926 goto err_out_free_netdev;
927
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700928 rwlock_init(&adapter->adapter_lock);
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700929
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800930 err = netxen_setup_pci_map(adapter);
931 if (err)
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700932 goto err_out_free_netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400933
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700934 netif_napi_add(netdev, &adapter->napi,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700935 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
936
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530937 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700938 adapter->portnum = pci_func_id;
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530939 adapter->status &= ~NETXEN_NETDEV_STATUS;
Dhananjay Phadke200eef22007-09-03 10:33:35 +0530940 adapter->rx_csum = 1;
Dhananjay Phadke623621b2008-07-21 19:44:01 -0700941 adapter->mc_enabled = 0;
Dhananjay Phadke83821a02008-08-01 03:14:58 -0700942 if (NX_IS_REVISION_P3(revision_id))
Dhananjay Phadke29566402008-07-21 19:44:04 -0700943 adapter->max_mc_count = 38;
Dhananjay Phadke83821a02008-08-01 03:14:58 -0700944 else
Dhananjay Phadke29566402008-07-21 19:44:04 -0700945 adapter->max_mc_count = 16;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700946
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800947 netdev->netdev_ops = &netxen_netdev_ops;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -0700948 netdev->watchdog_timeo = 2*HZ;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700949
950 netxen_nic_change_mtu(netdev, netdev->mtu);
951
952 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800953
Dhananjay Phadkecdff1032009-01-26 12:34:57 -0800954 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
955 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
956
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -0700957 if (NX_IS_REVISION_P3(revision_id)) {
Dhananjay Phadkecdff1032009-01-26 12:34:57 -0800958 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
959 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -0700960 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700961
Dhananjay Phadkecdff1032009-01-26 12:34:57 -0800962 if (adapter->pci_using_dac) {
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700963 netdev->features |= NETIF_F_HIGHDMA;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -0800964 netdev->vlan_features |= NETIF_F_HIGHDMA;
965 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700966
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700967 if (netxen_nic_get_board_info(adapter) != 0) {
968 printk("%s: Error getting board config info.\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700969 netxen_nic_driver_name);
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700970 err = -EIO;
971 goto err_out_iounmap;
972 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700973
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700974 netxen_initialize_adapter_ops(adapter);
975
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700976 /* Mezz cards have PCI function 0,2,3 enabled */
Dhananjay Phadkedc515f22008-06-15 22:59:43 -0700977 switch (adapter->ahw.boardcfg.board_type) {
978 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
979 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
980 if (pci_func_id >= 2)
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700981 adapter->portnum = pci_func_id - 2;
Dhananjay Phadkedc515f22008-06-15 22:59:43 -0700982 break;
983 default:
984 break;
985 }
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700986
Dhananjay Phadke29566402008-07-21 19:44:04 -0700987 /*
988 * This call will setup various max rx/tx counts.
989 * It must be done before any buffer/ring allocations.
990 */
991 netxen_check_options(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400992
Dhananjay Phadke29566402008-07-21 19:44:04 -0700993 first_driver = 0;
994 if (NX_IS_REVISION_P3(revision_id)) {
995 if (adapter->ahw.pci_func == 0)
996 first_driver = 1;
997 } else {
998 if (adapter->portnum == 0)
999 first_driver = 1;
1000 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001001
Dhananjay Phadke29566402008-07-21 19:44:04 -07001002 if (first_driver) {
Dhananjay Phadkeba599d42009-02-24 16:38:22 -08001003 err = netxen_start_firmware(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001004 if (err)
1005 goto err_out_iounmap;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -08001006 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001007
Dhananjay Phadke1f434f62009-03-02 16:02:16 +00001008 nx_update_dma_mask(adapter);
1009
Dhananjay Phadke29566402008-07-21 19:44:04 -07001010 netxen_nic_flash_print(adapter);
1011
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001012 if (NX_IS_REVISION_P3(revision_id)) {
1013 adapter->hw_read_wx(adapter,
1014 NETXEN_MIU_MN_CONTROL, &val, 4);
1015 adapter->ahw.cut_through = (val & 0x4) ? 1 : 0;
1016 dev_info(&pdev->dev, "firmware running in %s mode\n",
1017 adapter->ahw.cut_through ? "cut through" : "legacy");
1018 }
1019
Dhananjay Phadke29566402008-07-21 19:44:04 -07001020 /*
1021 * See if the firmware gave us a virtual-physical port mapping.
1022 */
1023 adapter->physical_port = adapter->portnum;
Dhananjay Phadke044fad02009-02-24 03:42:59 -08001024 if (adapter->fw_major < 4) {
1025 i = adapter->pci_read_normalize(adapter,
1026 CRB_V2P(adapter->portnum));
1027 if (i != 0x55555555)
1028 adapter->physical_port = i;
1029 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001030
1031 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
1032
1033 netxen_set_msix_bit(pdev, 0);
1034
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001035 netxen_setup_intr(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001036
1037 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1038 netdev->irq = adapter->msix_entries[0].vector;
1039 else
1040 netdev->irq = pdev->irq;
1041
1042 err = netxen_receive_peg_ready(adapter);
1043 if (err)
1044 goto err_out_disable_msi;
1045
1046 init_timer(&adapter->watchdog_timer);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001047 adapter->watchdog_timer.function = &netxen_watchdog;
1048 adapter->watchdog_timer.data = (unsigned long)adapter;
1049 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
1050 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1051
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -07001052 err = netxen_read_mac_addr(adapter);
1053 if (err)
1054 dev_warn(&pdev->dev, "failed to read mac addr\n");
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001055
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001056 netif_carrier_off(netdev);
1057 netif_stop_queue(netdev);
1058
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001059 if ((err = register_netdev(netdev))) {
1060 printk(KERN_ERR "%s: register_netdev failed port #%d"
1061 " aborting\n", netxen_nic_driver_name,
1062 adapter->portnum);
1063 err = -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001064 goto err_out_disable_msi;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001065 }
1066
1067 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001068
Dhananjay Phadke29566402008-07-21 19:44:04 -07001069 switch (adapter->ahw.board_type) {
1070 case NETXEN_NIC_GBE:
1071 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1072 adapter->netdev->name);
1073 break;
1074 case NETXEN_NIC_XGBE:
1075 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1076 adapter->netdev->name);
1077 break;
1078 }
1079
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001080 return 0;
1081
Dhananjay Phadke29566402008-07-21 19:44:04 -07001082err_out_disable_msi:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001083 netxen_teardown_intr(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001084
Dhananjay Phadke29566402008-07-21 19:44:04 -07001085 if (first_driver)
1086 netxen_free_adapter_offload(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001087
Dhananjay Phadke29566402008-07-21 19:44:04 -07001088err_out_iounmap:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001089 netxen_cleanup_pci_map(adapter);
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001090
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001091err_out_free_netdev:
1092 free_netdev(netdev);
1093
1094err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001095 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001096
1097err_out_disable_pdev:
Dhananjay Phadke29566402008-07-21 19:44:04 -07001098 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001099 pci_disable_device(pdev);
1100 return err;
1101}
1102
1103static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1104{
1105 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001106 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001107
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001108 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001109 if (adapter == NULL)
1110 return;
1111
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001112 netdev = adapter->netdev;
1113
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301114 unregister_netdev(netdev);
1115
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301116 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001117 netxen_nic_detach(adapter);
Dhananjay Phadke06e9d9f2009-01-14 20:49:22 -08001118
1119 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1120 netxen_p3_free_mac_list(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301121 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001122
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001123 if (adapter->portnum == 0)
1124 netxen_free_adapter_offload(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301125
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001126 netxen_teardown_intr(adapter);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301127
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001128 netxen_cleanup_pci_map(adapter);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001129
1130 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301131 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001132 pci_set_drvdata(pdev, NULL);
1133
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001134 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001135}
1136
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001137static int netxen_nic_open(struct net_device *netdev)
1138{
Wang Chen4cf16532008-11-12 23:38:14 -08001139 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001140 int err = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001141
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001142 if (adapter->driver_mismatch)
1143 return -EIO;
1144
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001145 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001146 err = netxen_nic_attach(adapter);
1147 if (err)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001148 return err;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001149 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001150
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001151 err = netxen_nic_up(adapter, netdev);
1152 if (err)
1153 goto err_out;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001154
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001155 netif_start_queue(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001156
1157 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001158
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001159err_out:
1160 netxen_nic_detach(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001161 return err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001162}
1163
1164/*
1165 * netxen_nic_close - Disables a network interface entry point
1166 */
1167static int netxen_nic_close(struct net_device *netdev)
1168{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001169 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001170
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001171 netxen_nic_down(adapter, netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001172 return 0;
1173}
1174
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001175static bool netxen_tso_check(struct net_device *netdev,
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001176 struct cmd_desc_type0 *desc, struct sk_buff *skb)
1177{
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001178 bool tso = false;
1179 u8 opcode = TX_ETHER_PKT;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001180 __be16 protocol = skb->protocol;
1181 u16 flags = 0;
1182
Harvey Harrison09640e632009-02-01 00:45:17 -08001183 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001184 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)skb->data;
1185 protocol = vh->h_vlan_encapsulated_proto;
1186 flags = FLAGS_VLAN_TAGGED;
1187 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001188
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001189 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1190 skb_shinfo(skb)->gso_size > 0) {
1191
1192 desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1193 desc->total_hdr_length =
1194 skb_transport_offset(skb) + tcp_hdrlen(skb);
1195
Harvey Harrison09640e632009-02-01 00:45:17 -08001196 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001197 TX_TCP_LSO6 : TX_TCP_LSO;
1198 tso = true;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001199
1200 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001201 u8 l4proto;
1202
Harvey Harrison09640e632009-02-01 00:45:17 -08001203 if (protocol == cpu_to_be16(ETH_P_IP)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001204 l4proto = ip_hdr(skb)->protocol;
1205
1206 if (l4proto == IPPROTO_TCP)
1207 opcode = TX_TCP_PKT;
1208 else if(l4proto == IPPROTO_UDP)
1209 opcode = TX_UDP_PKT;
Harvey Harrison09640e632009-02-01 00:45:17 -08001210 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001211 l4proto = ipv6_hdr(skb)->nexthdr;
1212
1213 if (l4proto == IPPROTO_TCP)
1214 opcode = TX_TCPV6_PKT;
1215 else if(l4proto == IPPROTO_UDP)
1216 opcode = TX_UDPV6_PKT;
1217 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001218 }
1219 desc->tcp_hdr_offset = skb_transport_offset(skb);
1220 desc->ip_hdr_offset = skb_network_offset(skb);
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001221 netxen_set_tx_flags_opcode(desc, flags, opcode);
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001222 return tso;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001223}
1224
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001225static void
1226netxen_clean_tx_dma_mapping(struct pci_dev *pdev,
1227 struct netxen_cmd_buffer *pbuf, int last)
1228{
1229 int k;
1230 struct netxen_skb_frag *buffrag;
1231
1232 buffrag = &pbuf->frag_array[0];
1233 pci_unmap_single(pdev, buffrag->dma,
1234 buffrag->length, PCI_DMA_TODEVICE);
1235
1236 for (k = 1; k < last; k++) {
1237 buffrag = &pbuf->frag_array[k];
1238 pci_unmap_page(pdev, buffrag->dma,
1239 buffrag->length, PCI_DMA_TODEVICE);
1240 }
1241}
1242
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001243static inline void
1244netxen_clear_cmddesc(u64 *desc)
1245{
1246 int i;
1247 for (i = 0; i < 8; i++)
1248 desc[i] = 0ULL;
1249}
1250
1251static int
1252netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001253{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001254 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001255 struct netxen_hardware_context *hw = &adapter->ahw;
1256 unsigned int first_seg_len = skb->len - skb->data_len;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001257 struct netxen_cmd_buffer *pbuf;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001258 struct netxen_skb_frag *buffrag;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001259 struct cmd_desc_type0 *hwdesc;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001260 struct pci_dev *pdev = adapter->pdev;
1261 dma_addr_t temp_dma;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001262 int i, k;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001263
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001264 u32 producer, consumer;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001265 int frag_count, no_of_desc;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001266 u32 num_txd = adapter->max_tx_desc_count;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001267 bool is_tso = false;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001268
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001269 frag_count = skb_shinfo(skb)->nr_frags + 1;
1270
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001271 /* There 4 fragments per descriptor */
1272 no_of_desc = (frag_count + 3) >> 2;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001273
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001274 producer = adapter->cmd_producer;
1275 smp_mb();
1276 consumer = adapter->last_cmd_consumer;
1277 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
1278 netif_stop_queue(netdev);
1279 smp_mb();
1280 return NETDEV_TX_BUSY;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001281 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001282
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001283 /* Copy the descriptors into the hardware */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001284 hwdesc = &hw->cmd_desc_head[producer];
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001285 netxen_clear_cmddesc((u64 *)hwdesc);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001286 /* Take skb->data itself */
1287 pbuf = &adapter->cmd_buf_arr[producer];
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001288
1289 is_tso = netxen_tso_check(netdev, hwdesc, skb);
1290
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001291 pbuf->skb = skb;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001292 pbuf->frag_count = frag_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001293 buffrag = &pbuf->frag_array[0];
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001294 temp_dma = pci_map_single(pdev, skb->data, first_seg_len,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001295 PCI_DMA_TODEVICE);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001296 if (pci_dma_mapping_error(pdev, temp_dma))
1297 goto drop_packet;
1298
1299 buffrag->dma = temp_dma;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001300 buffrag->length = first_seg_len;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001301 netxen_set_tx_frags_len(hwdesc, frag_count, skb->len);
1302 netxen_set_tx_port(hwdesc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001303
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001304 hwdesc->buffer_length[0] = cpu_to_le16(first_seg_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001305 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1306
1307 for (i = 1, k = 1; i < frag_count; i++, k++) {
1308 struct skb_frag_struct *frag;
1309 int len, temp_len;
1310 unsigned long offset;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001311
1312 /* move to next desc. if there is a need */
1313 if ((i & 0x3) == 0) {
1314 k = 0;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001315 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001316 hwdesc = &hw->cmd_desc_head[producer];
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001317 netxen_clear_cmddesc((u64 *)hwdesc);
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001318 pbuf = &adapter->cmd_buf_arr[producer];
1319 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001320 }
1321 frag = &skb_shinfo(skb)->frags[i - 1];
1322 len = frag->size;
1323 offset = frag->page_offset;
1324
1325 temp_len = len;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001326 temp_dma = pci_map_page(pdev, frag->page, offset,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001327 len, PCI_DMA_TODEVICE);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001328 if (pci_dma_mapping_error(pdev, temp_dma)) {
1329 netxen_clean_tx_dma_mapping(pdev, pbuf, i);
1330 goto drop_packet;
1331 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001332
1333 buffrag++;
1334 buffrag->dma = temp_dma;
1335 buffrag->length = temp_len;
1336
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001337 hwdesc->buffer_length[k] = cpu_to_le16(temp_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001338 switch (k) {
1339 case 0:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001340 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1341 break;
1342 case 1:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001343 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1344 break;
1345 case 2:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001346 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1347 break;
1348 case 3:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001349 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1350 break;
1351 }
1352 frag++;
1353 }
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001354 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001355
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001356 /* For LSO, we need to copy the MAC/IP/TCP headers into
1357 * the descriptor ring
1358 */
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001359 if (is_tso) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001360 int hdr_len, first_hdr_len, more_hdr;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001361 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001362 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1363 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001364 more_hdr = 1;
1365 } else {
1366 first_hdr_len = hdr_len;
1367 more_hdr = 0;
1368 }
1369 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1370 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001371 pbuf = &adapter->cmd_buf_arr[producer];
1372 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001373
1374 /* copy the first 64 bytes */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001375 memcpy(((void *)hwdesc) + 2,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001376 (void *)(skb->data), first_hdr_len);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001377 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001378
1379 if (more_hdr) {
1380 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001381 pbuf = &adapter->cmd_buf_arr[producer];
1382 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001383 /* copy the next 64 bytes - should be enough except
1384 * for pathological case
1385 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001386 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1387 hwdesc,
1388 (hdr_len -
1389 first_hdr_len));
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001390 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001391 }
1392 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001393
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001394 adapter->cmd_producer = producer;
Dhananjay Phadke5dc16262007-12-31 10:08:57 -08001395 adapter->stats.txbytes += skb->len;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001396
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001397 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001398
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001399 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001400 netdev->trans_start = jiffies;
1401
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001402 return NETDEV_TX_OK;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001403
1404drop_packet:
1405 adapter->stats.txdropped++;
1406 dev_kfree_skb_any(skb);
1407 return NETDEV_TX_OK;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001408}
1409
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001410static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1411{
1412 struct net_device *netdev = adapter->netdev;
1413 uint32_t temp, temp_state, temp_val;
1414 int rv = 0;
1415
1416 temp = adapter->pci_read_normalize(adapter, CRB_TEMP_STATE);
1417
1418 temp_state = nx_get_temp_state(temp);
1419 temp_val = nx_get_temp_val(temp);
1420
1421 if (temp_state == NX_TEMP_PANIC) {
1422 printk(KERN_ALERT
1423 "%s: Device temperature %d degrees C exceeds"
1424 " maximum allowed. Hardware has been shut down.\n",
1425 netxen_nic_driver_name, temp_val);
1426
1427 netif_carrier_off(netdev);
1428 netif_stop_queue(netdev);
1429 rv = 1;
1430 } else if (temp_state == NX_TEMP_WARN) {
1431 if (adapter->temp == NX_TEMP_NORMAL) {
1432 printk(KERN_ALERT
1433 "%s: Device temperature %d degrees C "
1434 "exceeds operating range."
1435 " Immediate action needed.\n",
1436 netxen_nic_driver_name, temp_val);
1437 }
1438 } else {
1439 if (adapter->temp == NX_TEMP_WARN) {
1440 printk(KERN_INFO
1441 "%s: Device temperature is now %d degrees C"
1442 " in normal range.\n", netxen_nic_driver_name,
1443 temp_val);
1444 }
1445 }
1446 adapter->temp = temp_state;
1447 return rv;
1448}
1449
1450static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1451{
1452 struct net_device *netdev = adapter->netdev;
1453 u32 val, port, linkup;
1454
1455 port = adapter->physical_port;
1456
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001457 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1458 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE_P3);
1459 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1460 linkup = (val == XG_LINK_UP_P3);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001461 } else {
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001462 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE);
1463 if (adapter->ahw.board_type == NETXEN_NIC_GBE)
1464 linkup = (val >> port) & 1;
1465 else {
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001466 val = (val >> port*8) & 0xff;
1467 linkup = (val == XG_LINK_UP);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001468 }
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001469 }
1470
1471 if (adapter->ahw.linkup && !linkup) {
1472 printk(KERN_INFO "%s: %s NIC Link is down\n",
1473 netxen_nic_driver_name, netdev->name);
1474 adapter->ahw.linkup = 0;
1475 if (netif_running(netdev)) {
1476 netif_carrier_off(netdev);
1477 netif_stop_queue(netdev);
1478 }
Dhananjay Phadkec7860a22009-01-14 20:48:32 -08001479
1480 netxen_nic_set_link_parameters(adapter);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001481 } else if (!adapter->ahw.linkup && linkup) {
1482 printk(KERN_INFO "%s: %s NIC Link is up\n",
1483 netxen_nic_driver_name, netdev->name);
1484 adapter->ahw.linkup = 1;
1485 if (netif_running(netdev)) {
1486 netif_carrier_on(netdev);
1487 netif_wake_queue(netdev);
1488 }
Dhananjay Phadkec7860a22009-01-14 20:48:32 -08001489
1490 netxen_nic_set_link_parameters(adapter);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001491 }
1492}
1493
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001494static void netxen_watchdog(unsigned long v)
1495{
1496 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001497
1498 SCHEDULE_WORK(&adapter->watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001499}
1500
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001501void netxen_watchdog_task(struct work_struct *work)
1502{
1503 struct netxen_adapter *adapter =
1504 container_of(work, struct netxen_adapter, watchdog_task);
1505
1506 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1507 return;
1508
1509 netxen_nic_handle_phy_intr(adapter);
1510
Dhananjay Phadke922c4f22008-08-08 00:08:41 -07001511 if (netif_running(adapter->netdev))
1512 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001513}
1514
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001515static void netxen_tx_timeout(struct net_device *netdev)
1516{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001517 struct netxen_adapter *adapter = (struct netxen_adapter *)
1518 netdev_priv(netdev);
1519 SCHEDULE_WORK(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001520}
1521
David Howells6d5aefb2006-12-05 19:36:26 +00001522static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001523{
Jeff Garzik47906542007-11-23 21:23:36 -05001524 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001525 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001526
1527 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001528 netxen_nic_driver_name, adapter->netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001529
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001530 netxen_nic_disable_int(adapter);
1531 napi_disable(&adapter->napi);
1532
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001533 adapter->netdev->trans_start = jiffies;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001534
1535 napi_enable(&adapter->napi);
1536 netxen_nic_enable_int(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001537 netif_wake_queue(adapter->netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001538}
1539
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001540/*
1541 * netxen_nic_get_stats - Get System Network Statistics
1542 * @netdev: network interface device structure
1543 */
1544struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1545{
1546 struct netxen_adapter *adapter = netdev_priv(netdev);
1547 struct net_device_stats *stats = &adapter->net_stats;
1548
1549 memset(stats, 0, sizeof(*stats));
1550
1551 /* total packets received */
1552 stats->rx_packets = adapter->stats.no_rcv;
1553 /* total packets transmitted */
1554 stats->tx_packets = adapter->stats.xmitedframes +
1555 adapter->stats.xmitfinished;
1556 /* total bytes received */
1557 stats->rx_bytes = adapter->stats.rxbytes;
1558 /* total bytes transmitted */
1559 stats->tx_bytes = adapter->stats.txbytes;
1560 /* bad packets received */
1561 stats->rx_errors = adapter->stats.rcvdbadskb;
1562 /* packet transmit problems */
1563 stats->tx_errors = adapter->stats.nocmddescriptor;
1564 /* no space in linux buffers */
1565 stats->rx_dropped = adapter->stats.rxdropped;
1566 /* no space available in linux */
1567 stats->tx_dropped = adapter->stats.txdropped;
1568
1569 return stats;
1570}
1571
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001572static irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001573{
Jeff Garzik1daec862007-10-19 03:14:03 -04001574 struct netxen_adapter *adapter = data;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001575 u32 status = 0;
1576
1577 status = adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1578
1579 if (!(status & adapter->legacy_intr.int_vec_bit))
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001580 return IRQ_NONE;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301581
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001582 if (adapter->ahw.revision_id >= NX_P3_B1) {
1583 /* check interrupt state machine, to be sure */
1584 status = adapter->pci_read_immediate(adapter,
1585 ISR_INT_STATE_REG);
1586 if (!ISR_LEGACY_INT_TRIGGERED(status))
1587 return IRQ_NONE;
1588
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001589 } else {
1590 unsigned long our_int = 0;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001591
1592 our_int = adapter->pci_read_normalize(adapter, CRB_INT_VECTOR);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001593
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001594 /* not our interrupt */
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001595 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001596 return IRQ_NONE;
1597
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001598 /* claim interrupt */
1599 adapter->pci_write_normalize(adapter,
1600 CRB_INT_VECTOR, (our_int & 0xffffffff));
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301601 }
1602
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001603 /* clear interrupt */
1604 if (adapter->fw_major < 4)
1605 netxen_nic_disable_int(adapter);
1606
1607 adapter->pci_write_immediate(adapter,
1608 adapter->legacy_intr.tgt_status_reg,
1609 0xffffffff);
1610 /* read twice to ensure write is flushed */
1611 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1612 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1613
1614 napi_schedule(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001615
1616 return IRQ_HANDLED;
1617}
1618
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001619static irqreturn_t netxen_msi_intr(int irq, void *data)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001620{
1621 struct netxen_adapter *adapter = data;
1622
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001623 /* clear interrupt */
1624 adapter->pci_write_immediate(adapter,
1625 msi_tgt_status[adapter->ahw.pci_func], 0xffffffff);
1626
1627 napi_schedule(&adapter->napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001628 return IRQ_HANDLED;
1629}
1630
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001631static irqreturn_t netxen_msix_intr(int irq, void *data)
1632{
1633 struct netxen_adapter *adapter = data;
1634
1635 napi_schedule(&adapter->napi);
1636 return IRQ_HANDLED;
1637}
1638
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001639static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001640{
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001641 struct netxen_adapter *adapter =
1642 container_of(napi, struct netxen_adapter, napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001643 int tx_complete;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001644 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001645
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001646 tx_complete = netxen_process_cmd_ring(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001647
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001648 work_done = netxen_process_rcv_ring(adapter, budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001649
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001650 if ((work_done < budget) && tx_complete) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001651 napi_complete(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001652 netxen_nic_enable_int(adapter);
1653 }
1654
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001655 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001656}
1657
1658#ifdef CONFIG_NET_POLL_CONTROLLER
1659static void netxen_nic_poll_controller(struct net_device *netdev)
1660{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001661 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001662 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001663 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001664 enable_irq(adapter->irq);
1665}
1666#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001667
1668static struct pci_driver netxen_driver = {
1669 .name = netxen_nic_driver_name,
1670 .id_table = netxen_pci_tbl,
1671 .probe = netxen_nic_probe,
1672 .remove = __devexit_p(netxen_nic_remove)
1673};
1674
1675/* Driver Registration on NetXen card */
1676
1677static int __init netxen_init_module(void)
1678{
Al Viro79ea13c2008-01-24 02:06:46 -08001679 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001680 return -ENOMEM;
1681
Amit S. Kale184231b2007-01-22 06:52:53 -08001682 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001683}
1684
1685module_init(netxen_init_module);
1686
1687static void __exit netxen_exit_module(void)
1688{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001689 pci_unregister_driver(&netxen_driver);
Amit S. Kale9de06612007-02-21 06:37:06 -08001690 destroy_workqueue(netxen_workq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001691}
1692
1693module_exit(netxen_exit_module);