blob: 9445277321d796309489c4c6b7db8a87cd076840 [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{
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000215 switch (adapter->ahw.board_type) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700216 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;
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000253 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
Dhananjay Phadkec7860a22009-01-14 20:48:32 -0800254 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",
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000264 adapter->ahw.board_type);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700265 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
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000333 val = adapter->ahw.board_type;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700334 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
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800526 unsigned long mem_base, mem_len, db_base, db_len = 0, pci_len0 = 0;
527
528 struct pci_dev *pdev = adapter->pdev;
529 int pci_func = adapter->ahw.pci_func;
530
531 int err = 0;
532
533 /*
534 * Set the CRB window to invalid. If any register in window 0 is
535 * accessed it should set the window to 0 and then reset it to 1.
536 */
537 adapter->curr_window = 255;
538 adapter->ahw.qdr_sn_window = -1;
539 adapter->ahw.ddr_mn_window = -1;
540
541 /* remap phys address */
542 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
543 mem_len = pci_resource_len(pdev, 0);
544 pci_len0 = 0;
545
546 adapter->hw_write_wx = netxen_nic_hw_write_wx_128M;
547 adapter->hw_read_wx = netxen_nic_hw_read_wx_128M;
548 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_128M;
549 adapter->pci_write_immediate = netxen_nic_pci_write_immediate_128M;
550 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_128M;
551 adapter->pci_write_normalize = netxen_nic_pci_write_normalize_128M;
552 adapter->pci_set_window = netxen_nic_pci_set_window_128M;
553 adapter->pci_mem_read = netxen_nic_pci_mem_read_128M;
554 adapter->pci_mem_write = netxen_nic_pci_mem_write_128M;
555
556 /* 128 Meg of memory */
557 if (mem_len == NETXEN_PCI_128MB_SIZE) {
558 mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
559 mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
560 SECOND_PAGE_GROUP_SIZE);
561 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
562 THIRD_PAGE_GROUP_SIZE);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800563 } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
564 mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
565 mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
566 SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800567 } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
568 adapter->hw_write_wx = netxen_nic_hw_write_wx_2M;
569 adapter->hw_read_wx = netxen_nic_hw_read_wx_2M;
570 adapter->pci_read_immediate = netxen_nic_pci_read_immediate_2M;
571 adapter->pci_write_immediate =
572 netxen_nic_pci_write_immediate_2M;
573 adapter->pci_read_normalize = netxen_nic_pci_read_normalize_2M;
574 adapter->pci_write_normalize =
575 netxen_nic_pci_write_normalize_2M;
576 adapter->pci_set_window = netxen_nic_pci_set_window_2M;
577 adapter->pci_mem_read = netxen_nic_pci_mem_read_2M;
578 adapter->pci_mem_write = netxen_nic_pci_mem_write_2M;
579
580 mem_ptr0 = pci_ioremap_bar(pdev, 0);
581 if (mem_ptr0 == NULL) {
582 dev_err(&pdev->dev, "failed to map PCI bar 0\n");
583 return -EIO;
584 }
585 pci_len0 = mem_len;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800586
587 adapter->ahw.ddr_mn_window = 0;
588 adapter->ahw.qdr_sn_window = 0;
589
590 adapter->ahw.mn_win_crb = 0x100000 + PCIX_MN_WINDOW +
591 (pci_func * 0x20);
592 adapter->ahw.ms_win_crb = 0x100000 + PCIX_SN_WINDOW;
593 if (pci_func < 4)
594 adapter->ahw.ms_win_crb += (pci_func * 0x20);
595 else
596 adapter->ahw.ms_win_crb +=
597 0xA0 + ((pci_func - 4) * 0x10);
598 } else {
599 return -EIO;
600 }
601
602 dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
603
604 adapter->ahw.pci_base0 = mem_ptr0;
605 adapter->ahw.pci_len0 = pci_len0;
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800606 adapter->ahw.pci_base1 = mem_ptr1;
607 adapter->ahw.pci_base2 = mem_ptr2;
608
609 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
610 goto skip_doorbell;
611
612 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
613 db_len = pci_resource_len(pdev, 4);
614
615 if (db_len == 0) {
616 printk(KERN_ERR "%s: doorbell is disabled\n",
617 netxen_nic_driver_name);
618 err = -EIO;
619 goto err_out;
620 }
621
622 db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
623 if (!db_ptr) {
624 printk(KERN_ERR "%s: Failed to allocate doorbell map.",
625 netxen_nic_driver_name);
626 err = -EIO;
627 goto err_out;
628 }
629
630skip_doorbell:
631 adapter->ahw.db_base = db_ptr;
632 adapter->ahw.db_len = db_len;
633 return 0;
634
635err_out:
636 netxen_cleanup_pci_map(adapter);
637 return err;
638}
639
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800640static int
641netxen_start_firmware(struct netxen_adapter *adapter)
642{
643 int val, err, first_boot;
644 struct pci_dev *pdev = adapter->pdev;
645
646 first_boot = adapter->pci_read_normalize(adapter,
647 NETXEN_CAM_RAM(0x1fc));
648
649 err = netxen_check_hw_init(adapter, first_boot);
650 if (err) {
651 dev_err(&pdev->dev, "error in init HW init sequence\n");
652 return err;
653 }
654
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800655 if (first_boot != 0x55555555) {
656 adapter->pci_write_normalize(adapter,
657 CRB_CMDPEG_STATE, 0);
658 netxen_pinit_from_rom(adapter, 0);
659 msleep(1);
660 }
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000661
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000662 netxen_nic_reg_write(adapter, CRB_DMA_SHIFT, 0x55555555);
Dhananjay Phadke567c6c42009-03-02 16:02:17 +0000663 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
664 netxen_set_port_mode(adapter);
665
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800666 netxen_load_firmware(adapter);
667
668 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
669
670 /* Initialize multicast addr pool owners */
671 val = 0x7654;
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000672 if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800673 val |= 0x0f000000;
674 netxen_crb_writelit_adapter(adapter,
675 NETXEN_MAC_ADDR_CNTL_REG, val);
676
677 }
678
679 err = netxen_initialize_adapter_offload(adapter);
680 if (err)
681 return err;
682
683 /*
684 * Tell the hardware our version number.
685 */
686 val = (_NETXEN_NIC_LINUX_MAJOR << 16)
687 | ((_NETXEN_NIC_LINUX_MINOR << 8))
688 | (_NETXEN_NIC_LINUX_SUBVERSION);
689 adapter->pci_write_normalize(adapter, CRB_DRIVER_VERSION, val);
690
691 /* Handshake with the card before we register the devices. */
692 err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
693 if (err) {
694 netxen_free_adapter_offload(adapter);
695 return err;
696 }
697
698 return 0;
699}
700
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000701static int
702netxen_nic_request_irq(struct netxen_adapter *adapter)
703{
704 irq_handler_t handler;
705 unsigned long flags = IRQF_SAMPLE_RANDOM;
706 struct net_device *netdev = adapter->netdev;
707
708 if ((adapter->msi_mode != MSI_MODE_MULTIFUNC) ||
709 (adapter->intr_scheme != INTR_SCHEME_PERPORT)) {
710 printk(KERN_ERR "%s: Firmware interrupt scheme is "
711 "incompatible with driver\n",
712 netdev->name);
713 adapter->driver_mismatch = 1;
714 return -EINVAL;
715 }
716
717 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
718 handler = netxen_msix_intr;
719 else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
720 handler = netxen_msi_intr;
721 else {
722 flags |= IRQF_SHARED;
723 handler = netxen_intr;
724 }
725 adapter->irq = netdev->irq;
726
727 return request_irq(adapter->irq, handler,
728 flags, netdev->name, adapter);
729}
730
731static int
732netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
733{
734 int err;
735
736 err = adapter->init_port(adapter, adapter->physical_port);
737 if (err) {
738 printk(KERN_ERR "%s: Failed to initialize port %d\n",
739 netxen_nic_driver_name, adapter->portnum);
740 return err;
741 }
742 adapter->macaddr_set(adapter, netdev->dev_addr);
743
744 netxen_nic_set_link_parameters(adapter);
745
746 netxen_set_multicast_list(netdev);
747 if (adapter->set_mtu)
748 adapter->set_mtu(adapter, netdev->mtu);
749
750 adapter->ahw.linkup = 0;
751 mod_timer(&adapter->watchdog_timer, jiffies);
752
753 napi_enable(&adapter->napi);
754 netxen_nic_enable_int(adapter);
755
756 return 0;
757}
758
759static void
760netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
761{
762 netif_carrier_off(netdev);
763 netif_stop_queue(netdev);
764 napi_disable(&adapter->napi);
765
766 if (adapter->stop_port)
767 adapter->stop_port(adapter);
768
769 netxen_nic_disable_int(adapter);
770
771 netxen_release_tx_buffers(adapter);
772
773 FLUSH_SCHEDULED_WORK();
774 del_timer_sync(&adapter->watchdog_timer);
775}
776
777
778static int
779netxen_nic_attach(struct netxen_adapter *adapter)
780{
781 struct net_device *netdev = adapter->netdev;
782 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000783 int err, ring;
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000784
785 err = netxen_init_firmware(adapter);
786 if (err != 0) {
787 printk(KERN_ERR "Failed to init firmware\n");
788 return -EIO;
789 }
790
791 if (adapter->fw_major < 4)
792 adapter->max_rds_rings = 3;
793 else
794 adapter->max_rds_rings = 2;
795
796 err = netxen_alloc_sw_resources(adapter);
797 if (err) {
798 printk(KERN_ERR "%s: Error in setting sw resources\n",
799 netdev->name);
800 return err;
801 }
802
803 netxen_nic_clear_stats(adapter);
804
805 err = netxen_alloc_hw_resources(adapter);
806 if (err) {
807 printk(KERN_ERR "%s: Error in setting hw resources\n",
808 netdev->name);
809 goto err_out_free_sw;
810 }
811
812 if (adapter->fw_major < 4) {
813 adapter->crb_addr_cmd_producer =
814 crb_cmd_producer[adapter->portnum];
815 adapter->crb_addr_cmd_consumer =
816 crb_cmd_consumer[adapter->portnum];
817
818 netxen_nic_update_cmd_producer(adapter, 0);
819 netxen_nic_update_cmd_consumer(adapter, 0);
820 }
821
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000822 for (ring = 0; ring < adapter->max_rds_rings; ring++)
823 netxen_post_rx_buffers(adapter, ring);
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +0000824
825 err = netxen_nic_request_irq(adapter);
826 if (err) {
827 dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
828 netdev->name);
829 goto err_out_free_rxbuf;
830 }
831
832 adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
833 return 0;
834
835err_out_free_rxbuf:
836 netxen_release_rx_buffers(adapter);
837 netxen_free_hw_resources(adapter);
838err_out_free_sw:
839 netxen_free_sw_resources(adapter);
840 return err;
841}
842
843static void
844netxen_nic_detach(struct netxen_adapter *adapter)
845{
846 if (adapter->irq)
847 free_irq(adapter->irq, adapter);
848
849 netxen_release_rx_buffers(adapter);
850 netxen_free_hw_resources(adapter);
851 netxen_free_sw_resources(adapter);
852
853 adapter->is_up = 0;
854}
855
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400856static int __devinit
857netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
858{
859 struct net_device *netdev = NULL;
860 struct netxen_adapter *adapter = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700861 int i = 0, err;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800862 int first_driver;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700863 int pci_func_id = PCI_FUNC(pdev->devfn);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700864 uint8_t revision_id;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400865
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700866 if (pci_func_id == 0)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700867 printk(KERN_INFO "%s\n", netxen_nic_driver_string);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700868
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700869 if (pdev->class != 0x020000) {
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700870 printk(KERN_DEBUG "NetXen function %d, class %x will not "
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700871 "be enabled.\n",pci_func_id, pdev->class);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800872 return -ENODEV;
873 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700874
Dhananjay Phadke11d89d62008-08-08 00:08:45 -0700875 if (pdev->revision >= NX_P3_A0 && pdev->revision < NX_P3_B1) {
876 printk(KERN_WARNING "NetXen chip revisions between 0x%x-0x%x"
877 "will not be enabled.\n",
878 NX_P3_A0, NX_P3_B1);
879 return -ENODEV;
880 }
881
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400882 if ((err = pci_enable_device(pdev)))
883 return err;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700884
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400885 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
886 err = -ENODEV;
887 goto err_out_disable_pdev;
888 }
889
890 if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
891 goto err_out_disable_pdev;
892
893 pci_set_master(pdev);
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700894
895 netdev = alloc_etherdev(sizeof(struct netxen_adapter));
896 if(!netdev) {
897 printk(KERN_ERR"%s: Failed to allocate memory for the "
898 "device block.Check system memory resource"
899 " usage.\n", netxen_nic_driver_name);
900 goto err_out_free_res;
901 }
902
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700903 SET_NETDEV_DEV(netdev, &pdev->dev);
904
Wang Chen4cf16532008-11-12 23:38:14 -0800905 adapter = netdev_priv(netdev);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700906 adapter->netdev = netdev;
907 adapter->pdev = pdev;
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700908 adapter->ahw.pci_func = pci_func_id;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700909
910 revision_id = pdev->revision;
911 adapter->ahw.revision_id = revision_id;
912
913 err = nx_set_dma_mask(adapter, revision_id);
914 if (err)
915 goto err_out_free_netdev;
916
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700917 rwlock_init(&adapter->adapter_lock);
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700918
Dhananjay Phadke97daee82009-02-24 16:39:21 -0800919 err = netxen_setup_pci_map(adapter);
920 if (err)
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700921 goto err_out_free_netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400922
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700923 netif_napi_add(netdev, &adapter->napi,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700924 netxen_nic_poll, NETXEN_NETDEV_WEIGHT);
925
dhananjay.phadke@gmail.com2d1a3bb2007-07-02 00:26:00 +0530926 /* This will be reset for mezz cards */
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700927 adapter->portnum = pci_func_id;
Dhananjay Phadke200eef22007-09-03 10:33:35 +0530928 adapter->rx_csum = 1;
Dhananjay Phadke623621b2008-07-21 19:44:01 -0700929 adapter->mc_enabled = 0;
Dhananjay Phadke83821a02008-08-01 03:14:58 -0700930 if (NX_IS_REVISION_P3(revision_id))
Dhananjay Phadke29566402008-07-21 19:44:04 -0700931 adapter->max_mc_count = 38;
Dhananjay Phadke83821a02008-08-01 03:14:58 -0700932 else
Dhananjay Phadke29566402008-07-21 19:44:04 -0700933 adapter->max_mc_count = 16;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700934
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800935 netdev->netdev_ops = &netxen_netdev_ops;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -0700936 netdev->watchdog_timeo = 2*HZ;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700937
938 netxen_nic_change_mtu(netdev, netdev->mtu);
939
940 SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops);
Stephen Hemminger1abd2662008-11-21 17:32:15 -0800941
Dhananjay Phadkecdff1032009-01-26 12:34:57 -0800942 netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
943 netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
944
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -0700945 if (NX_IS_REVISION_P3(revision_id)) {
Dhananjay Phadkecdff1032009-01-26 12:34:57 -0800946 netdev->features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
947 netdev->vlan_features |= (NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -0700948 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700949
Dhananjay Phadkecdff1032009-01-26 12:34:57 -0800950 if (adapter->pci_using_dac) {
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700951 netdev->features |= NETIF_F_HIGHDMA;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -0800952 netdev->vlan_features |= NETIF_F_HIGHDMA;
953 }
Mithlesh Thukral3176ff32007-04-20 07:52:37 -0700954
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700955 if (netxen_nic_get_board_info(adapter) != 0) {
956 printk("%s: Error getting board config info.\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700957 netxen_nic_driver_name);
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700958 err = -EIO;
959 goto err_out_iounmap;
960 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700961
Mithlesh Thukral6c80b182007-04-20 07:55:26 -0700962 netxen_initialize_adapter_ops(adapter);
963
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700964 /* Mezz cards have PCI function 0,2,3 enabled */
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000965 switch (adapter->ahw.board_type) {
Dhananjay Phadkedc515f22008-06-15 22:59:43 -0700966 case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
967 case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
968 if (pci_func_id >= 2)
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700969 adapter->portnum = pci_func_id - 2;
Dhananjay Phadkedc515f22008-06-15 22:59:43 -0700970 break;
971 default:
972 break;
973 }
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700974
Dhananjay Phadke29566402008-07-21 19:44:04 -0700975 /*
976 * This call will setup various max rx/tx counts.
977 * It must be done before any buffer/ring allocations.
978 */
979 netxen_check_options(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400980
Dhananjay Phadke29566402008-07-21 19:44:04 -0700981 first_driver = 0;
982 if (NX_IS_REVISION_P3(revision_id)) {
983 if (adapter->ahw.pci_func == 0)
984 first_driver = 1;
985 } else {
986 if (adapter->portnum == 0)
987 first_driver = 1;
988 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400989
Dhananjay Phadke29566402008-07-21 19:44:04 -0700990 if (first_driver) {
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800991 err = netxen_start_firmware(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700992 if (err)
993 goto err_out_iounmap;
Dhananjay Phadkeba599d42009-02-24 16:38:22 -0800994 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700995
Dhananjay Phadke1f434f62009-03-02 16:02:16 +0000996 nx_update_dma_mask(adapter);
997
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000998 netxen_nic_get_firmware_info(adapter);
Dhananjay Phadked9e651b2008-07-21 19:44:08 -0700999
Dhananjay Phadke29566402008-07-21 19:44:04 -07001000 /*
1001 * See if the firmware gave us a virtual-physical port mapping.
1002 */
1003 adapter->physical_port = adapter->portnum;
Dhananjay Phadke044fad02009-02-24 03:42:59 -08001004 if (adapter->fw_major < 4) {
1005 i = adapter->pci_read_normalize(adapter,
1006 CRB_V2P(adapter->portnum));
1007 if (i != 0x55555555)
1008 adapter->physical_port = i;
1009 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001010
1011 adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
1012
1013 netxen_set_msix_bit(pdev, 0);
1014
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001015 netxen_setup_intr(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001016
1017 if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
1018 netdev->irq = adapter->msix_entries[0].vector;
1019 else
1020 netdev->irq = pdev->irq;
1021
1022 err = netxen_receive_peg_ready(adapter);
1023 if (err)
1024 goto err_out_disable_msi;
1025
1026 init_timer(&adapter->watchdog_timer);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001027 adapter->watchdog_timer.function = &netxen_watchdog;
1028 adapter->watchdog_timer.data = (unsigned long)adapter;
1029 INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task);
1030 INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
1031
Dhananjay Phadke9dc28ef2008-08-08 00:08:39 -07001032 err = netxen_read_mac_addr(adapter);
1033 if (err)
1034 dev_warn(&pdev->dev, "failed to read mac addr\n");
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001035
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001036 netif_carrier_off(netdev);
1037 netif_stop_queue(netdev);
1038
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001039 if ((err = register_netdev(netdev))) {
1040 printk(KERN_ERR "%s: register_netdev failed port #%d"
1041 " aborting\n", netxen_nic_driver_name,
1042 adapter->portnum);
1043 err = -EIO;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001044 goto err_out_disable_msi;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001045 }
1046
1047 pci_set_drvdata(pdev, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001048
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001049 switch (adapter->ahw.port_type) {
Dhananjay Phadke29566402008-07-21 19:44:04 -07001050 case NETXEN_NIC_GBE:
1051 dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
1052 adapter->netdev->name);
1053 break;
1054 case NETXEN_NIC_XGBE:
1055 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
1056 adapter->netdev->name);
1057 break;
1058 }
1059
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001060 return 0;
1061
Dhananjay Phadke29566402008-07-21 19:44:04 -07001062err_out_disable_msi:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001063 netxen_teardown_intr(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001064
Dhananjay Phadke29566402008-07-21 19:44:04 -07001065 if (first_driver)
1066 netxen_free_adapter_offload(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001067
Dhananjay Phadke29566402008-07-21 19:44:04 -07001068err_out_iounmap:
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001069 netxen_cleanup_pci_map(adapter);
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001070
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001071err_out_free_netdev:
1072 free_netdev(netdev);
1073
1074err_out_free_res:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001075 pci_release_regions(pdev);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001076
1077err_out_disable_pdev:
Dhananjay Phadke29566402008-07-21 19:44:04 -07001078 pci_set_drvdata(pdev, NULL);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001079 pci_disable_device(pdev);
1080 return err;
1081}
1082
1083static void __devexit netxen_nic_remove(struct pci_dev *pdev)
1084{
1085 struct netxen_adapter *adapter;
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001086 struct net_device *netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001087
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001088 adapter = pci_get_drvdata(pdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001089 if (adapter == NULL)
1090 return;
1091
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001092 netdev = adapter->netdev;
1093
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301094 unregister_netdev(netdev);
1095
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301096 if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001097 netxen_nic_detach(adapter);
Dhananjay Phadke06e9d9f2009-01-14 20:49:22 -08001098
1099 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
1100 netxen_p3_free_mac_list(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301101 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001102
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001103 if (adapter->portnum == 0)
1104 netxen_free_adapter_offload(adapter);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301105
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001106 netxen_teardown_intr(adapter);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301107
Dhananjay Phadke97daee82009-02-24 16:39:21 -08001108 netxen_cleanup_pci_map(adapter);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001109
1110 pci_release_regions(pdev);
dhananjay@netxen.com30522462007-08-28 17:23:26 +05301111 pci_disable_device(pdev);
Mithlesh Thukral595e3fb2007-04-20 07:53:52 -07001112 pci_set_drvdata(pdev, NULL);
1113
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001114 free_netdev(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001115}
1116
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001117static int netxen_nic_open(struct net_device *netdev)
1118{
Wang Chen4cf16532008-11-12 23:38:14 -08001119 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001120 int err = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001121
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001122 if (adapter->driver_mismatch)
1123 return -EIO;
1124
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001125 if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) {
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001126 err = netxen_nic_attach(adapter);
1127 if (err)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001128 return err;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001129 }
Dhananjay Phadke29566402008-07-21 19:44:04 -07001130
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001131 err = netxen_nic_up(adapter, netdev);
1132 if (err)
1133 goto err_out;
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001134
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -07001135 netif_start_queue(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001136
1137 return 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001138
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001139err_out:
1140 netxen_nic_detach(adapter);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001141 return err;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001142}
1143
1144/*
1145 * netxen_nic_close - Disables a network interface entry point
1146 */
1147static int netxen_nic_close(struct net_device *netdev)
1148{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001149 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001150
Dhananjay Phadke9f5bc7f2009-03-09 08:50:54 +00001151 netxen_nic_down(adapter, netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001152 return 0;
1153}
1154
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001155static bool netxen_tso_check(struct net_device *netdev,
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001156 struct cmd_desc_type0 *desc, struct sk_buff *skb)
1157{
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001158 bool tso = false;
1159 u8 opcode = TX_ETHER_PKT;
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001160 __be16 protocol = skb->protocol;
1161 u16 flags = 0;
1162
Harvey Harrison09640e632009-02-01 00:45:17 -08001163 if (protocol == cpu_to_be16(ETH_P_8021Q)) {
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001164 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)skb->data;
1165 protocol = vh->h_vlan_encapsulated_proto;
1166 flags = FLAGS_VLAN_TAGGED;
1167 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001168
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001169 if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
1170 skb_shinfo(skb)->gso_size > 0) {
1171
1172 desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1173 desc->total_hdr_length =
1174 skb_transport_offset(skb) + tcp_hdrlen(skb);
1175
Harvey Harrison09640e632009-02-01 00:45:17 -08001176 opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001177 TX_TCP_LSO6 : TX_TCP_LSO;
1178 tso = true;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001179
1180 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001181 u8 l4proto;
1182
Harvey Harrison09640e632009-02-01 00:45:17 -08001183 if (protocol == cpu_to_be16(ETH_P_IP)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001184 l4proto = ip_hdr(skb)->protocol;
1185
1186 if (l4proto == IPPROTO_TCP)
1187 opcode = TX_TCP_PKT;
1188 else if(l4proto == IPPROTO_UDP)
1189 opcode = TX_UDP_PKT;
Harvey Harrison09640e632009-02-01 00:45:17 -08001190 } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001191 l4proto = ipv6_hdr(skb)->nexthdr;
1192
1193 if (l4proto == IPPROTO_TCP)
1194 opcode = TX_TCPV6_PKT;
1195 else if(l4proto == IPPROTO_UDP)
1196 opcode = TX_UDPV6_PKT;
1197 }
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001198 }
1199 desc->tcp_hdr_offset = skb_transport_offset(skb);
1200 desc->ip_hdr_offset = skb_network_offset(skb);
Dhananjay Phadkecdff1032009-01-26 12:34:57 -08001201 netxen_set_tx_flags_opcode(desc, flags, opcode);
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001202 return tso;
Dhananjay Phadkecd1f8162008-07-21 19:44:09 -07001203}
1204
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001205static void
1206netxen_clean_tx_dma_mapping(struct pci_dev *pdev,
1207 struct netxen_cmd_buffer *pbuf, int last)
1208{
1209 int k;
1210 struct netxen_skb_frag *buffrag;
1211
1212 buffrag = &pbuf->frag_array[0];
1213 pci_unmap_single(pdev, buffrag->dma,
1214 buffrag->length, PCI_DMA_TODEVICE);
1215
1216 for (k = 1; k < last; k++) {
1217 buffrag = &pbuf->frag_array[k];
1218 pci_unmap_page(pdev, buffrag->dma,
1219 buffrag->length, PCI_DMA_TODEVICE);
1220 }
1221}
1222
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001223static inline void
1224netxen_clear_cmddesc(u64 *desc)
1225{
1226 int i;
1227 for (i = 0; i < 8; i++)
1228 desc[i] = 0ULL;
1229}
1230
1231static int
1232netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001233{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001234 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001235 struct netxen_hardware_context *hw = &adapter->ahw;
1236 unsigned int first_seg_len = skb->len - skb->data_len;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001237 struct netxen_cmd_buffer *pbuf;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001238 struct netxen_skb_frag *buffrag;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001239 struct cmd_desc_type0 *hwdesc;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001240 struct pci_dev *pdev = adapter->pdev;
1241 dma_addr_t temp_dma;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001242 int i, k;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001243
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001244 u32 producer, consumer;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001245 int frag_count, no_of_desc;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001246 u32 num_txd = adapter->max_tx_desc_count;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001247 bool is_tso = false;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001248
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001249 frag_count = skb_shinfo(skb)->nr_frags + 1;
1250
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001251 /* There 4 fragments per descriptor */
1252 no_of_desc = (frag_count + 3) >> 2;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001253
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001254 producer = adapter->cmd_producer;
1255 smp_mb();
1256 consumer = adapter->last_cmd_consumer;
1257 if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) {
1258 netif_stop_queue(netdev);
1259 smp_mb();
1260 return NETDEV_TX_BUSY;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001261 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001262
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001263 /* Copy the descriptors into the hardware */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001264 hwdesc = &hw->cmd_desc_head[producer];
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001265 netxen_clear_cmddesc((u64 *)hwdesc);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001266 /* Take skb->data itself */
1267 pbuf = &adapter->cmd_buf_arr[producer];
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001268
1269 is_tso = netxen_tso_check(netdev, hwdesc, skb);
1270
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001271 pbuf->skb = skb;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001272 pbuf->frag_count = frag_count;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001273 buffrag = &pbuf->frag_array[0];
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001274 temp_dma = pci_map_single(pdev, skb->data, first_seg_len,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001275 PCI_DMA_TODEVICE);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001276 if (pci_dma_mapping_error(pdev, temp_dma))
1277 goto drop_packet;
1278
1279 buffrag->dma = temp_dma;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001280 buffrag->length = first_seg_len;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001281 netxen_set_tx_frags_len(hwdesc, frag_count, skb->len);
1282 netxen_set_tx_port(hwdesc, adapter->portnum);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001283
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001284 hwdesc->buffer_length[0] = cpu_to_le16(first_seg_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001285 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
1286
1287 for (i = 1, k = 1; i < frag_count; i++, k++) {
1288 struct skb_frag_struct *frag;
1289 int len, temp_len;
1290 unsigned long offset;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001291
1292 /* move to next desc. if there is a need */
1293 if ((i & 0x3) == 0) {
1294 k = 0;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001295 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001296 hwdesc = &hw->cmd_desc_head[producer];
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001297 netxen_clear_cmddesc((u64 *)hwdesc);
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001298 pbuf = &adapter->cmd_buf_arr[producer];
1299 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001300 }
1301 frag = &skb_shinfo(skb)->frags[i - 1];
1302 len = frag->size;
1303 offset = frag->page_offset;
1304
1305 temp_len = len;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001306 temp_dma = pci_map_page(pdev, frag->page, offset,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001307 len, PCI_DMA_TODEVICE);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001308 if (pci_dma_mapping_error(pdev, temp_dma)) {
1309 netxen_clean_tx_dma_mapping(pdev, pbuf, i);
1310 goto drop_packet;
1311 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001312
1313 buffrag++;
1314 buffrag->dma = temp_dma;
1315 buffrag->length = temp_len;
1316
Dhananjay Phadked32cc3d2009-03-09 08:50:53 +00001317 hwdesc->buffer_length[k] = cpu_to_le16(temp_len);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001318 switch (k) {
1319 case 0:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001320 hwdesc->addr_buffer1 = cpu_to_le64(temp_dma);
1321 break;
1322 case 1:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001323 hwdesc->addr_buffer2 = cpu_to_le64(temp_dma);
1324 break;
1325 case 2:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001326 hwdesc->addr_buffer3 = cpu_to_le64(temp_dma);
1327 break;
1328 case 3:
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001329 hwdesc->addr_buffer4 = cpu_to_le64(temp_dma);
1330 break;
1331 }
1332 frag++;
1333 }
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001334 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001335
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001336 /* For LSO, we need to copy the MAC/IP/TCP headers into
1337 * the descriptor ring
1338 */
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001339 if (is_tso) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001340 int hdr_len, first_hdr_len, more_hdr;
Dhananjay Phadke391587c2009-01-14 20:48:11 -08001341 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001342 if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) {
1343 first_hdr_len = sizeof(struct cmd_desc_type0) - 2;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001344 more_hdr = 1;
1345 } else {
1346 first_hdr_len = hdr_len;
1347 more_hdr = 0;
1348 }
1349 /* copy the MAC/IP/TCP headers to the cmd descriptor list */
1350 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001351 pbuf = &adapter->cmd_buf_arr[producer];
1352 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001353
1354 /* copy the first 64 bytes */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001355 memcpy(((void *)hwdesc) + 2,
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001356 (void *)(skb->data), first_hdr_len);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001357 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001358
1359 if (more_hdr) {
1360 hwdesc = &hw->cmd_desc_head[producer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001361 pbuf = &adapter->cmd_buf_arr[producer];
1362 pbuf->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001363 /* copy the next 64 bytes - should be enough except
1364 * for pathological case
1365 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001366 skb_copy_from_linear_data_offset(skb, first_hdr_len,
1367 hwdesc,
1368 (hdr_len -
1369 first_hdr_len));
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001370 producer = get_next_index(producer, num_txd);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001371 }
1372 }
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001373
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001374 adapter->cmd_producer = producer;
Dhananjay Phadke5dc16262007-12-31 10:08:57 -08001375 adapter->stats.txbytes += skb->len;
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001376
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001377 netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001378
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001379 adapter->stats.xmitcalled++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001380 netdev->trans_start = jiffies;
1381
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001382 return NETDEV_TX_OK;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001383
1384drop_packet:
1385 adapter->stats.txdropped++;
1386 dev_kfree_skb_any(skb);
1387 return NETDEV_TX_OK;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001388}
1389
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001390static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1391{
1392 struct net_device *netdev = adapter->netdev;
1393 uint32_t temp, temp_state, temp_val;
1394 int rv = 0;
1395
1396 temp = adapter->pci_read_normalize(adapter, CRB_TEMP_STATE);
1397
1398 temp_state = nx_get_temp_state(temp);
1399 temp_val = nx_get_temp_val(temp);
1400
1401 if (temp_state == NX_TEMP_PANIC) {
1402 printk(KERN_ALERT
1403 "%s: Device temperature %d degrees C exceeds"
1404 " maximum allowed. Hardware has been shut down.\n",
1405 netxen_nic_driver_name, temp_val);
1406
1407 netif_carrier_off(netdev);
1408 netif_stop_queue(netdev);
1409 rv = 1;
1410 } else if (temp_state == NX_TEMP_WARN) {
1411 if (adapter->temp == NX_TEMP_NORMAL) {
1412 printk(KERN_ALERT
1413 "%s: Device temperature %d degrees C "
1414 "exceeds operating range."
1415 " Immediate action needed.\n",
1416 netxen_nic_driver_name, temp_val);
1417 }
1418 } else {
1419 if (adapter->temp == NX_TEMP_WARN) {
1420 printk(KERN_INFO
1421 "%s: Device temperature is now %d degrees C"
1422 " in normal range.\n", netxen_nic_driver_name,
1423 temp_val);
1424 }
1425 }
1426 adapter->temp = temp_state;
1427 return rv;
1428}
1429
1430static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
1431{
1432 struct net_device *netdev = adapter->netdev;
1433 u32 val, port, linkup;
1434
1435 port = adapter->physical_port;
1436
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001437 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1438 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE_P3);
1439 val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
1440 linkup = (val == XG_LINK_UP_P3);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001441 } else {
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001442 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE);
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +00001443 if (adapter->ahw.port_type == NETXEN_NIC_GBE)
Dhananjay Phadke24a7a452008-08-01 03:14:55 -07001444 linkup = (val >> port) & 1;
1445 else {
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001446 val = (val >> port*8) & 0xff;
1447 linkup = (val == XG_LINK_UP);
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -07001448 }
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001449 }
1450
1451 if (adapter->ahw.linkup && !linkup) {
1452 printk(KERN_INFO "%s: %s NIC Link is down\n",
1453 netxen_nic_driver_name, netdev->name);
1454 adapter->ahw.linkup = 0;
1455 if (netif_running(netdev)) {
1456 netif_carrier_off(netdev);
1457 netif_stop_queue(netdev);
1458 }
Dhananjay Phadkec7860a22009-01-14 20:48:32 -08001459
1460 netxen_nic_set_link_parameters(adapter);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001461 } else if (!adapter->ahw.linkup && linkup) {
1462 printk(KERN_INFO "%s: %s NIC Link is up\n",
1463 netxen_nic_driver_name, netdev->name);
1464 adapter->ahw.linkup = 1;
1465 if (netif_running(netdev)) {
1466 netif_carrier_on(netdev);
1467 netif_wake_queue(netdev);
1468 }
Dhananjay Phadkec7860a22009-01-14 20:48:32 -08001469
1470 netxen_nic_set_link_parameters(adapter);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001471 }
1472}
1473
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001474static void netxen_watchdog(unsigned long v)
1475{
1476 struct netxen_adapter *adapter = (struct netxen_adapter *)v;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001477
1478 SCHEDULE_WORK(&adapter->watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001479}
1480
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001481void netxen_watchdog_task(struct work_struct *work)
1482{
1483 struct netxen_adapter *adapter =
1484 container_of(work, struct netxen_adapter, watchdog_task);
1485
1486 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1487 return;
1488
1489 netxen_nic_handle_phy_intr(adapter);
1490
Dhananjay Phadke922c4f22008-08-08 00:08:41 -07001491 if (netif_running(adapter->netdev))
1492 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001493}
1494
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001495static void netxen_tx_timeout(struct net_device *netdev)
1496{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001497 struct netxen_adapter *adapter = (struct netxen_adapter *)
1498 netdev_priv(netdev);
1499 SCHEDULE_WORK(&adapter->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001500}
1501
David Howells6d5aefb2006-12-05 19:36:26 +00001502static void netxen_tx_timeout_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001503{
Jeff Garzik47906542007-11-23 21:23:36 -05001504 struct netxen_adapter *adapter =
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001505 container_of(work, struct netxen_adapter, tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001506
1507 printk(KERN_ERR "%s %s: transmit timeout, resetting.\n",
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001508 netxen_nic_driver_name, adapter->netdev->name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001509
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001510 netxen_nic_disable_int(adapter);
1511 napi_disable(&adapter->napi);
1512
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001513 adapter->netdev->trans_start = jiffies;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001514
1515 napi_enable(&adapter->napi);
1516 netxen_nic_enable_int(adapter);
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001517 netif_wake_queue(adapter->netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001518}
1519
Dhananjay Phadkea97342f2008-07-21 19:44:05 -07001520/*
1521 * netxen_nic_get_stats - Get System Network Statistics
1522 * @netdev: network interface device structure
1523 */
1524struct net_device_stats *netxen_nic_get_stats(struct net_device *netdev)
1525{
1526 struct netxen_adapter *adapter = netdev_priv(netdev);
1527 struct net_device_stats *stats = &adapter->net_stats;
1528
1529 memset(stats, 0, sizeof(*stats));
1530
1531 /* total packets received */
1532 stats->rx_packets = adapter->stats.no_rcv;
1533 /* total packets transmitted */
1534 stats->tx_packets = adapter->stats.xmitedframes +
1535 adapter->stats.xmitfinished;
1536 /* total bytes received */
1537 stats->rx_bytes = adapter->stats.rxbytes;
1538 /* total bytes transmitted */
1539 stats->tx_bytes = adapter->stats.txbytes;
1540 /* bad packets received */
1541 stats->rx_errors = adapter->stats.rcvdbadskb;
1542 /* packet transmit problems */
1543 stats->tx_errors = adapter->stats.nocmddescriptor;
1544 /* no space in linux buffers */
1545 stats->rx_dropped = adapter->stats.rxdropped;
1546 /* no space available in linux */
1547 stats->tx_dropped = adapter->stats.txdropped;
1548
1549 return stats;
1550}
1551
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001552static irqreturn_t netxen_intr(int irq, void *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001553{
Jeff Garzik1daec862007-10-19 03:14:03 -04001554 struct netxen_adapter *adapter = data;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001555 u32 status = 0;
1556
1557 status = adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1558
1559 if (!(status & adapter->legacy_intr.int_vec_bit))
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001560 return IRQ_NONE;
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301561
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001562 if (adapter->ahw.revision_id >= NX_P3_B1) {
1563 /* check interrupt state machine, to be sure */
1564 status = adapter->pci_read_immediate(adapter,
1565 ISR_INT_STATE_REG);
1566 if (!ISR_LEGACY_INT_TRIGGERED(status))
1567 return IRQ_NONE;
1568
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001569 } else {
1570 unsigned long our_int = 0;
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001571
1572 our_int = adapter->pci_read_normalize(adapter, CRB_INT_VECTOR);
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001573
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001574 /* not our interrupt */
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001575 if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
Dhananjay Phadked71e1be2008-08-01 03:14:57 -07001576 return IRQ_NONE;
1577
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001578 /* claim interrupt */
1579 adapter->pci_write_normalize(adapter,
1580 CRB_INT_VECTOR, (our_int & 0xffffffff));
dhananjay@netxen.come01872af2007-07-25 20:13:12 +05301581 }
1582
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001583 /* clear interrupt */
1584 if (adapter->fw_major < 4)
1585 netxen_nic_disable_int(adapter);
1586
1587 adapter->pci_write_immediate(adapter,
1588 adapter->legacy_intr.tgt_status_reg,
1589 0xffffffff);
1590 /* read twice to ensure write is flushed */
1591 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1592 adapter->pci_read_immediate(adapter, ISR_INT_VECTOR);
1593
1594 napi_schedule(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001595
1596 return IRQ_HANDLED;
1597}
1598
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -07001599static irqreturn_t netxen_msi_intr(int irq, void *data)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001600{
1601 struct netxen_adapter *adapter = data;
1602
Dhananjay Phadke092bc572008-08-08 00:08:43 -07001603 /* clear interrupt */
1604 adapter->pci_write_immediate(adapter,
1605 msi_tgt_status[adapter->ahw.pci_func], 0xffffffff);
1606
1607 napi_schedule(&adapter->napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001608 return IRQ_HANDLED;
1609}
1610
Dhananjay Phadkeb3df68f2009-02-08 19:20:19 -08001611static irqreturn_t netxen_msix_intr(int irq, void *data)
1612{
1613 struct netxen_adapter *adapter = data;
1614
1615 napi_schedule(&adapter->napi);
1616 return IRQ_HANDLED;
1617}
1618
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001619static int netxen_nic_poll(struct napi_struct *napi, int budget)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001620{
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001621 struct netxen_adapter *adapter =
1622 container_of(napi, struct netxen_adapter, napi);
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001623 int tx_complete;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001624 int work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001625
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001626 tx_complete = netxen_process_cmd_ring(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001627
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001628 work_done = netxen_process_rcv_ring(adapter, budget);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001629
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001630 if ((work_done < budget) && tx_complete) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001631 napi_complete(&adapter->napi);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001632 netxen_nic_enable_int(adapter);
1633 }
1634
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001635 return work_done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001636}
1637
1638#ifdef CONFIG_NET_POLL_CONTROLLER
1639static void netxen_nic_poll_controller(struct net_device *netdev)
1640{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001641 struct netxen_adapter *adapter = netdev_priv(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001642 disable_irq(adapter->irq);
Jeff Garzik1494a812006-11-07 05:12:16 -05001643 netxen_intr(adapter->irq, adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001644 enable_irq(adapter->irq);
1645}
1646#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001647
1648static struct pci_driver netxen_driver = {
1649 .name = netxen_nic_driver_name,
1650 .id_table = netxen_pci_tbl,
1651 .probe = netxen_nic_probe,
1652 .remove = __devexit_p(netxen_nic_remove)
1653};
1654
1655/* Driver Registration on NetXen card */
1656
1657static int __init netxen_init_module(void)
1658{
Al Viro79ea13c2008-01-24 02:06:46 -08001659 if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001660 return -ENOMEM;
1661
Amit S. Kale184231b2007-01-22 06:52:53 -08001662 return pci_register_driver(&netxen_driver);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001663}
1664
1665module_init(netxen_init_module);
1666
1667static void __exit netxen_exit_module(void)
1668{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001669 pci_unregister_driver(&netxen_driver);
Amit S. Kale9de06612007-02-21 06:37:06 -08001670 destroy_workqueue(netxen_workq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001671}
1672
1673module_exit(netxen_exit_module);