]>
Commit | Line | Data |
---|---|---|
3d396eb1 AK |
1 | /* |
2 | * Copyright (C) 2003 - 2006 NetXen, Inc. | |
3 | * All rights reserved. | |
80922fbc | 4 | * |
3d396eb1 AK |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License | |
7 | * as published by the Free Software Foundation; either version 2 | |
8 | * of the License, or (at your option) any later version. | |
80922fbc | 9 | * |
3d396eb1 AK |
10 | * This program is distributed in the hope that it will be useful, but |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
80922fbc | 14 | * |
3d396eb1 AK |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
18 | * MA 02111-1307, USA. | |
80922fbc | 19 | * |
3d396eb1 AK |
20 | * The full GNU General Public License is included in this distribution |
21 | * in the file called LICENSE. | |
80922fbc | 22 | * |
3d396eb1 AK |
23 | * Contact Information: |
24 | * info@netxen.com | |
25 | * NetXen, | |
26 | * 3965 Freedom Circle, Fourth floor, | |
27 | * Santa Clara, CA 95054 | |
28 | * | |
29 | * | |
30 | * Main source file for NetXen NIC Driver on Linux | |
31 | * | |
32 | */ | |
33 | ||
cb8011ad | 34 | #include <linux/vmalloc.h> |
ed25ffa1 | 35 | #include <linux/highmem.h> |
3d396eb1 AK |
36 | #include "netxen_nic_hw.h" |
37 | ||
38 | #include "netxen_nic.h" | |
3d396eb1 | 39 | #include "netxen_nic_phan_reg.h" |
3d396eb1 | 40 | |
1494a814 | 41 | #include <linux/dma-mapping.h> |
c9bdd4b5 | 42 | #include <net/ip.h> |
1494a814 | 43 | |
3d396eb1 AK |
44 | MODULE_DESCRIPTION("NetXen Multi port (1/10) Gigabit Network Driver"); |
45 | MODULE_LICENSE("GPL"); | |
46 | MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID); | |
47 | ||
ceded32f | 48 | char netxen_nic_driver_name[] = "netxen_nic"; |
3d396eb1 | 49 | static char netxen_nic_driver_string[] = "NetXen Network Driver version " |
cb8011ad | 50 | NETXEN_NIC_LINUX_VERSIONID; |
3d396eb1 AK |
51 | |
52 | #define NETXEN_NETDEV_WEIGHT 120 | |
53 | #define NETXEN_ADAPTER_UP_MAGIC 777 | |
cb8011ad | 54 | #define NETXEN_NIC_PEG_TUNE 0 |
3d396eb1 AK |
55 | |
56 | /* Local functions to NetXen NIC driver */ | |
57 | static int __devinit netxen_nic_probe(struct pci_dev *pdev, | |
58 | const struct pci_device_id *ent); | |
59 | static void __devexit netxen_nic_remove(struct pci_dev *pdev); | |
60 | static int netxen_nic_open(struct net_device *netdev); | |
61 | static int netxen_nic_close(struct net_device *netdev); | |
62 | static int netxen_nic_xmit_frame(struct sk_buff *, struct net_device *); | |
63 | static void netxen_tx_timeout(struct net_device *netdev); | |
6d5aefb8 | 64 | static void netxen_tx_timeout_task(struct work_struct *work); |
3d396eb1 | 65 | static void netxen_watchdog(unsigned long); |
bea3348e | 66 | static int netxen_nic_poll(struct napi_struct *napi, int budget); |
3d396eb1 AK |
67 | #ifdef CONFIG_NET_POLL_CONTROLLER |
68 | static void netxen_nic_poll_controller(struct net_device *netdev); | |
69 | #endif | |
1494a814 | 70 | static irqreturn_t netxen_intr(int irq, void *data); |
05aaa02d | 71 | static irqreturn_t netxen_msi_intr(int irq, void *data); |
3d396eb1 AK |
72 | |
73 | /* PCI Device ID Table */ | |
70081ac5 AV |
74 | #define ENTRY(device) \ |
75 | {PCI_DEVICE(0x4040, (device)), \ | |
76 | .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0} | |
77 | ||
3d396eb1 | 78 | static struct pci_device_id netxen_pci_tbl[] __devinitdata = { |
70081ac5 AV |
79 | ENTRY(0x0001), |
80 | ENTRY(0x0002), | |
81 | ENTRY(0x0003), | |
82 | ENTRY(0x0004), | |
83 | ENTRY(0x0005), | |
84 | ENTRY(0x0024), | |
85 | ENTRY(0x0025), | |
3d396eb1 AK |
86 | {0,} |
87 | }; | |
88 | ||
89 | MODULE_DEVICE_TABLE(pci, netxen_pci_tbl); | |
90 | ||
b1555130 AB |
91 | /* |
92 | * In netxen_nic_down(), we must wait for any pending callback requests into | |
93 | * netxen_watchdog_task() to complete; eg otherwise the watchdog_timer could be | |
94 | * reenabled right after it is deleted in netxen_nic_down(). | |
95 | * FLUSH_SCHEDULED_WORK() does this synchronization. | |
96 | * | |
97 | * Normally, schedule_work()/flush_scheduled_work() could have worked, but | |
98 | * netxen_nic_close() is invoked with kernel rtnl lock held. netif_carrier_off() | |
99 | * call in netxen_nic_close() triggers a schedule_work(&linkwatch_work), and a | |
100 | * subsequent call to flush_scheduled_work() in netxen_nic_down() would cause | |
101 | * linkwatch_event() to be executed which also attempts to acquire the rtnl | |
102 | * lock thus causing a deadlock. | |
103 | */ | |
104 | ||
105 | static struct workqueue_struct *netxen_workq; | |
106 | #define SCHEDULE_WORK(tp) queue_work(netxen_workq, tp) | |
107 | #define FLUSH_SCHEDULED_WORK() flush_workqueue(netxen_workq) | |
108 | ||
ed25ffa1 AK |
109 | static void netxen_watchdog(unsigned long); |
110 | ||
7830b22c DP |
111 | static uint32_t crb_cmd_producer[4] = { |
112 | CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1, | |
113 | CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3 | |
114 | }; | |
115 | ||
993fb90c AB |
116 | static void netxen_nic_update_cmd_producer(struct netxen_adapter *adapter, |
117 | uint32_t crb_producer) | |
3176ff3e | 118 | { |
7830b22c DP |
119 | writel(crb_producer, NETXEN_CRB_NORMALIZE(adapter, |
120 | adapter->crb_addr_cmd_producer)); | |
3176ff3e MT |
121 | } |
122 | ||
7830b22c DP |
123 | static uint32_t crb_cmd_consumer[4] = { |
124 | CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1, | |
125 | CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3 | |
126 | }; | |
127 | ||
993fb90c AB |
128 | static void netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter, |
129 | u32 crb_consumer) | |
3176ff3e | 130 | { |
7830b22c DP |
131 | writel(crb_consumer, NETXEN_CRB_NORMALIZE(adapter, |
132 | adapter->crb_addr_cmd_consumer)); | |
3176ff3e MT |
133 | } |
134 | ||
443be796 DP |
135 | static uint32_t msi_tgt_status[4] = { |
136 | ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1, | |
137 | ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3 | |
138 | }; | |
139 | ||
140 | static uint32_t sw_int_mask[4] = { | |
141 | CRB_SW_INT_MASK_0, CRB_SW_INT_MASK_1, | |
142 | CRB_SW_INT_MASK_2, CRB_SW_INT_MASK_3 | |
143 | }; | |
144 | ||
4638aef1 SH |
145 | static void netxen_nic_disable_int(struct netxen_adapter *adapter) |
146 | { | |
443be796 | 147 | u32 mask = 0x7ff; |
4638aef1 | 148 | int retries = 32; |
443be796 DP |
149 | int port = adapter->portnum; |
150 | int pci_fn = adapter->ahw.pci_func; | |
4638aef1 | 151 | |
f0c88f9c | 152 | if (adapter->msi_mode != MSI_MODE_MULTIFUNC) |
443be796 | 153 | writel(0x0, NETXEN_CRB_NORMALIZE(adapter, sw_int_mask[port])); |
4638aef1 SH |
154 | |
155 | if (adapter->intr_scheme != -1 && | |
156 | adapter->intr_scheme != INTR_SCHEME_PERPORT) | |
157 | writel(mask,PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK)); | |
158 | ||
4638aef1 SH |
159 | if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) { |
160 | do { | |
161 | writel(0xffffffff, | |
162 | PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_STATUS)); | |
163 | mask = readl(pci_base_offset(adapter, ISR_INT_VECTOR)); | |
164 | if (!(mask & 0x80)) | |
165 | break; | |
166 | udelay(10); | |
167 | } while (--retries); | |
168 | ||
169 | if (!retries) { | |
170 | printk(KERN_NOTICE "%s: Failed to disable interrupt completely\n", | |
171 | netxen_nic_driver_name); | |
172 | } | |
443be796 DP |
173 | } else { |
174 | if (adapter->msi_mode == MSI_MODE_MULTIFUNC) { | |
175 | writel(0xffffffff, PCI_OFFSET_SECOND_RANGE(adapter, | |
176 | msi_tgt_status[pci_fn])); | |
177 | } | |
4638aef1 | 178 | } |
4638aef1 SH |
179 | } |
180 | ||
181 | static void netxen_nic_enable_int(struct netxen_adapter *adapter) | |
182 | { | |
183 | u32 mask; | |
443be796 | 184 | int port = adapter->portnum; |
4638aef1 SH |
185 | |
186 | DPRINTK(1, INFO, "Entered ISR Enable \n"); | |
187 | ||
188 | if (adapter->intr_scheme != -1 && | |
189 | adapter->intr_scheme != INTR_SCHEME_PERPORT) { | |
190 | switch (adapter->ahw.board_type) { | |
191 | case NETXEN_NIC_GBE: | |
192 | mask = 0x77b; | |
193 | break; | |
194 | case NETXEN_NIC_XGBE: | |
195 | mask = 0x77f; | |
196 | break; | |
197 | default: | |
198 | mask = 0x7ff; | |
199 | break; | |
200 | } | |
201 | ||
202 | writel(mask, PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_MASK)); | |
203 | } | |
204 | ||
443be796 | 205 | writel(0x1, NETXEN_CRB_NORMALIZE(adapter, sw_int_mask[port])); |
4638aef1 SH |
206 | |
207 | if (!(adapter->flags & NETXEN_NIC_MSI_ENABLED)) { | |
208 | mask = 0xbff; | |
209 | if (adapter->intr_scheme != -1 && | |
210 | adapter->intr_scheme != INTR_SCHEME_PERPORT) { | |
211 | writel(0X0, NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR)); | |
212 | } | |
213 | writel(mask, | |
214 | PCI_OFFSET_SECOND_RANGE(adapter, ISR_INT_TARGET_MASK)); | |
215 | } | |
216 | ||
217 | DPRINTK(1, INFO, "Done with enable Int\n"); | |
218 | } | |
219 | ||
3d396eb1 AK |
220 | /* |
221 | * netxen_nic_probe() | |
222 | * | |
223 | * The Linux system will invoke this after identifying the vendor ID and | |
224 | * device Id in the pci_tbl supported by this module. | |
225 | * | |
226 | * A quad port card has one operational PCI config space, (function 0), | |
227 | * which is used to access all four ports. | |
228 | * | |
229 | * This routine will initialize the adapter, and setup the global parameters | |
230 | * along with the port's specific structure. | |
231 | */ | |
232 | static int __devinit | |
233 | netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent) | |
234 | { | |
235 | struct net_device *netdev = NULL; | |
236 | struct netxen_adapter *adapter = NULL; | |
ed25ffa1 AK |
237 | void __iomem *mem_ptr0 = NULL; |
238 | void __iomem *mem_ptr1 = NULL; | |
239 | void __iomem *mem_ptr2 = NULL; | |
6c80b18d MT |
240 | unsigned long first_page_group_end; |
241 | unsigned long first_page_group_start; | |
242 | ||
cb8011ad | 243 | |
a608ab9c | 244 | u8 __iomem *db_ptr = NULL; |
ed25ffa1 | 245 | unsigned long mem_base, mem_len, db_base, db_len; |
3176ff3e | 246 | int pci_using_dac, i = 0, err; |
3d396eb1 AK |
247 | int ring; |
248 | struct netxen_recv_context *recv_ctx = NULL; | |
249 | struct netxen_rcv_desc_ctx *rcv_desc = NULL; | |
250 | struct netxen_cmd_buffer *cmd_buf_arr = NULL; | |
f305f789 | 251 | __le64 mac_addr[FLASH_NUM_PORTS + 1]; |
6c80b18d MT |
252 | int valid_mac = 0; |
253 | u32 val; | |
3176ff3e | 254 | int pci_func_id = PCI_FUNC(pdev->devfn); |
0795af57 | 255 | DECLARE_MAC_BUF(mac); |
3d396eb1 | 256 | |
dcd56fdb DP |
257 | if (pci_func_id == 0) |
258 | printk(KERN_INFO "%s \n", netxen_nic_driver_string); | |
6c80b18d | 259 | |
3176ff3e | 260 | if (pdev->class != 0x020000) { |
dcd56fdb | 261 | printk(KERN_DEBUG "NetXen function %d, class %x will not " |
3176ff3e | 262 | "be enabled.\n",pci_func_id, pdev->class); |
ed25ffa1 AK |
263 | return -ENODEV; |
264 | } | |
3d396eb1 AK |
265 | if ((err = pci_enable_device(pdev))) |
266 | return err; | |
267 | if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { | |
268 | err = -ENODEV; | |
269 | goto err_out_disable_pdev; | |
270 | } | |
271 | ||
272 | if ((err = pci_request_regions(pdev, netxen_nic_driver_name))) | |
273 | goto err_out_disable_pdev; | |
274 | ||
275 | pci_set_master(pdev); | |
44c10138 | 276 | if (pdev->revision == NX_P2_C1 && |
ed25ffa1 AK |
277 | (pci_set_dma_mask(pdev, DMA_35BIT_MASK) == 0) && |
278 | (pci_set_consistent_dma_mask(pdev, DMA_35BIT_MASK) == 0)) { | |
3d396eb1 | 279 | pci_using_dac = 1; |
ed25ffa1 | 280 | } else { |
3d396eb1 AK |
281 | if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) || |
282 | (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK))) | |
283 | goto err_out_free_res; | |
284 | ||
285 | pci_using_dac = 0; | |
286 | } | |
287 | ||
3176ff3e MT |
288 | |
289 | netdev = alloc_etherdev(sizeof(struct netxen_adapter)); | |
290 | if(!netdev) { | |
291 | printk(KERN_ERR"%s: Failed to allocate memory for the " | |
292 | "device block.Check system memory resource" | |
293 | " usage.\n", netxen_nic_driver_name); | |
294 | goto err_out_free_res; | |
295 | } | |
296 | ||
3176ff3e MT |
297 | SET_NETDEV_DEV(netdev, &pdev->dev); |
298 | ||
299 | adapter = netdev->priv; | |
3176ff3e | 300 | |
13ba9c77 MT |
301 | adapter->ahw.pci_func = pci_func_id; |
302 | ||
3d396eb1 AK |
303 | /* remap phys address */ |
304 | mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */ | |
305 | mem_len = pci_resource_len(pdev, 0); | |
306 | ||
307 | /* 128 Meg of memory */ | |
6c80b18d MT |
308 | if (mem_len == NETXEN_PCI_128MB_SIZE) { |
309 | mem_ptr0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE); | |
310 | mem_ptr1 = ioremap(mem_base + SECOND_PAGE_GROUP_START, | |
311 | SECOND_PAGE_GROUP_SIZE); | |
312 | mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START, | |
313 | THIRD_PAGE_GROUP_SIZE); | |
314 | first_page_group_start = FIRST_PAGE_GROUP_START; | |
315 | first_page_group_end = FIRST_PAGE_GROUP_END; | |
316 | } else if (mem_len == NETXEN_PCI_32MB_SIZE) { | |
317 | mem_ptr1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE); | |
318 | mem_ptr2 = ioremap(mem_base + THIRD_PAGE_GROUP_START - | |
319 | SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE); | |
320 | first_page_group_start = 0; | |
321 | first_page_group_end = 0; | |
322 | } else { | |
4790654c | 323 | err = -EIO; |
6c80b18d MT |
324 | goto err_out_free_netdev; |
325 | } | |
cb8011ad | 326 | |
79ea13ce AV |
327 | if ((!mem_ptr0 && mem_len == NETXEN_PCI_128MB_SIZE) || |
328 | !mem_ptr1 || !mem_ptr2) { | |
ed25ffa1 | 329 | DPRINTK(ERR, |
cb8011ad AK |
330 | "Cannot remap adapter memory aborting.:" |
331 | "0 -> %p, 1 -> %p, 2 -> %p\n", | |
332 | mem_ptr0, mem_ptr1, mem_ptr2); | |
333 | ||
3d396eb1 | 334 | err = -EIO; |
ed25ffa1 AK |
335 | goto err_out_iounmap; |
336 | } | |
337 | db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */ | |
338 | db_len = pci_resource_len(pdev, 4); | |
339 | ||
340 | if (db_len == 0) { | |
341 | printk(KERN_ERR "%s: doorbell is disabled\n", | |
342 | netxen_nic_driver_name); | |
343 | err = -EIO; | |
344 | goto err_out_iounmap; | |
345 | } | |
346 | DPRINTK(INFO, "doorbell ioremap from %lx a size of %lx\n", db_base, | |
347 | db_len); | |
348 | ||
349 | db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES); | |
a608ab9c | 350 | if (!db_ptr) { |
ed25ffa1 AK |
351 | printk(KERN_ERR "%s: Failed to allocate doorbell map.", |
352 | netxen_nic_driver_name); | |
353 | err = -EIO; | |
354 | goto err_out_iounmap; | |
3d396eb1 | 355 | } |
ed25ffa1 | 356 | DPRINTK(INFO, "doorbell ioremaped at %p\n", db_ptr); |
3d396eb1 | 357 | |
6c80b18d MT |
358 | adapter->ahw.pci_base0 = mem_ptr0; |
359 | adapter->ahw.first_page_group_start = first_page_group_start; | |
360 | adapter->ahw.first_page_group_end = first_page_group_end; | |
361 | adapter->ahw.pci_base1 = mem_ptr1; | |
362 | adapter->ahw.pci_base2 = mem_ptr2; | |
363 | adapter->ahw.db_base = db_ptr; | |
364 | adapter->ahw.db_len = db_len; | |
3d396eb1 | 365 | |
3176ff3e MT |
366 | adapter->netdev = netdev; |
367 | adapter->pdev = pdev; | |
2d1a3bbd | 368 | |
bea3348e SH |
369 | netif_napi_add(netdev, &adapter->napi, |
370 | netxen_nic_poll, NETXEN_NETDEV_WEIGHT); | |
371 | ||
2d1a3bbd | 372 | /* this will be read from FW later */ |
373 | adapter->intr_scheme = -1; | |
443be796 | 374 | adapter->msi_mode = -1; |
2d1a3bbd | 375 | |
376 | /* This will be reset for mezz cards */ | |
3176ff3e | 377 | adapter->portnum = pci_func_id; |
2d1a3bbd | 378 | adapter->status &= ~NETXEN_NETDEV_STATUS; |
200eef20 | 379 | adapter->rx_csum = 1; |
3176ff3e MT |
380 | |
381 | netdev->open = netxen_nic_open; | |
382 | netdev->stop = netxen_nic_close; | |
383 | netdev->hard_start_xmit = netxen_nic_xmit_frame; | |
4790654c | 384 | netdev->get_stats = netxen_nic_get_stats; |
3176ff3e MT |
385 | netdev->set_multicast_list = netxen_nic_set_multi; |
386 | netdev->set_mac_address = netxen_nic_set_mac; | |
387 | netdev->change_mtu = netxen_nic_change_mtu; | |
388 | netdev->tx_timeout = netxen_tx_timeout; | |
05aaa02d | 389 | netdev->watchdog_timeo = 2*HZ; |
3176ff3e MT |
390 | |
391 | netxen_nic_change_mtu(netdev, netdev->mtu); | |
392 | ||
393 | SET_ETHTOOL_OPS(netdev, &netxen_nic_ethtool_ops); | |
3176ff3e MT |
394 | #ifdef CONFIG_NET_POLL_CONTROLLER |
395 | netdev->poll_controller = netxen_nic_poll_controller; | |
396 | #endif | |
397 | /* ScatterGather support */ | |
398 | netdev->features = NETIF_F_SG; | |
399 | netdev->features |= NETIF_F_IP_CSUM; | |
400 | netdev->features |= NETIF_F_TSO; | |
401 | ||
402 | if (pci_using_dac) | |
403 | netdev->features |= NETIF_F_HIGHDMA; | |
404 | ||
644caeef | 405 | if (pci_enable_msi(pdev)) |
3176ff3e | 406 | adapter->flags &= ~NETXEN_NIC_MSI_ENABLED; |
644caeef | 407 | else |
3176ff3e MT |
408 | adapter->flags |= NETXEN_NIC_MSI_ENABLED; |
409 | ||
6c80b18d MT |
410 | netdev->irq = pdev->irq; |
411 | INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task); | |
412 | ||
413 | /* | |
414 | * Set the CRB window to invalid. If any register in window 0 is | |
415 | * accessed it should set the window to 0 and then reset it to 1. | |
416 | */ | |
417 | adapter->curr_window = 255; | |
418 | ||
dcd56fdb DP |
419 | if (netxen_nic_get_board_info(adapter) != 0) { |
420 | printk("%s: Error getting board config info.\n", | |
421 | netxen_nic_driver_name); | |
422 | err = -EIO; | |
423 | goto err_out_iounmap; | |
424 | } | |
6c80b18d | 425 | |
6c80b18d MT |
426 | /* |
427 | * Adapter in our case is quad port so initialize it before | |
428 | * initializing the ports | |
429 | */ | |
430 | ||
431 | netxen_initialize_adapter_ops(adapter); | |
432 | ||
433 | adapter->max_tx_desc_count = MAX_CMD_DESCRIPTORS_HOST; | |
434 | if ((adapter->ahw.boardcfg.board_type == NETXEN_BRDTYPE_P2_SB35_4G) || | |
4790654c JG |
435 | (adapter->ahw.boardcfg.board_type == |
436 | NETXEN_BRDTYPE_P2_SB31_2G)) | |
6c80b18d MT |
437 | adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS_1G; |
438 | else | |
439 | adapter->max_rx_desc_count = MAX_RCV_DESCRIPTORS; | |
440 | adapter->max_jumbo_rx_desc_count = MAX_JUMBO_RCV_DESCRIPTORS; | |
441 | adapter->max_lro_rx_desc_count = MAX_LRO_RCV_DESCRIPTORS; | |
442 | ||
3d396eb1 AK |
443 | cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE); |
444 | if (cmd_buf_arr == NULL) { | |
ed25ffa1 AK |
445 | printk(KERN_ERR |
446 | "%s: Could not allocate cmd_buf_arr memory:%d\n", | |
447 | netxen_nic_driver_name, (int)TX_RINGSIZE); | |
3d396eb1 AK |
448 | err = -ENOMEM; |
449 | goto err_out_free_adapter; | |
450 | } | |
451 | memset(cmd_buf_arr, 0, TX_RINGSIZE); | |
6c80b18d | 452 | adapter->cmd_buf_arr = cmd_buf_arr; |
3d396eb1 AK |
453 | |
454 | for (i = 0; i < MAX_RCV_CTX; ++i) { | |
455 | recv_ctx = &adapter->recv_ctx[i]; | |
456 | for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) { | |
457 | rcv_desc = &recv_ctx->rcv_desc[ring]; | |
458 | switch (RCV_DESC_TYPE(ring)) { | |
459 | case RCV_DESC_NORMAL: | |
460 | rcv_desc->max_rx_desc_count = | |
461 | adapter->max_rx_desc_count; | |
462 | rcv_desc->flags = RCV_DESC_NORMAL; | |
463 | rcv_desc->dma_size = RX_DMA_MAP_LEN; | |
464 | rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH; | |
465 | break; | |
466 | ||
467 | case RCV_DESC_JUMBO: | |
468 | rcv_desc->max_rx_desc_count = | |
469 | adapter->max_jumbo_rx_desc_count; | |
470 | rcv_desc->flags = RCV_DESC_JUMBO; | |
471 | rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN; | |
472 | rcv_desc->skb_size = MAX_RX_JUMBO_BUFFER_LENGTH; | |
473 | break; | |
474 | ||
ed25ffa1 AK |
475 | case RCV_RING_LRO: |
476 | rcv_desc->max_rx_desc_count = | |
477 | adapter->max_lro_rx_desc_count; | |
478 | rcv_desc->flags = RCV_DESC_LRO; | |
479 | rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN; | |
480 | rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH; | |
481 | break; | |
482 | ||
3d396eb1 AK |
483 | } |
484 | rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *) | |
485 | vmalloc(RCV_BUFFSIZE); | |
486 | ||
487 | if (rcv_desc->rx_buf_arr == NULL) { | |
5bc51424 | 488 | printk(KERN_ERR "%s: Could not allocate " |
ed25ffa1 AK |
489 | "rcv_desc->rx_buf_arr memory:%d\n", |
490 | netxen_nic_driver_name, | |
491 | (int)RCV_BUFFSIZE); | |
3d396eb1 AK |
492 | err = -ENOMEM; |
493 | goto err_out_free_rx_buffer; | |
494 | } | |
495 | memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE); | |
496 | } | |
497 | ||
498 | } | |
499 | ||
ed25ffa1 | 500 | netxen_initialize_adapter_sw(adapter); /* initialize the buffers in adapter */ |
6c80b18d | 501 | |
13ba9c77 | 502 | /* Mezz cards have PCI function 0,2,3 enabled */ |
dc515f2e DP |
503 | switch (adapter->ahw.boardcfg.board_type) { |
504 | case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ: | |
505 | case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ: | |
506 | if (pci_func_id >= 2) | |
13ba9c77 | 507 | adapter->portnum = pci_func_id - 2; |
dc515f2e DP |
508 | break; |
509 | default: | |
510 | break; | |
511 | } | |
13ba9c77 | 512 | |
3d396eb1 AK |
513 | init_timer(&adapter->watchdog_timer); |
514 | adapter->ahw.xg_linkup = 0; | |
515 | adapter->watchdog_timer.function = &netxen_watchdog; | |
516 | adapter->watchdog_timer.data = (unsigned long)adapter; | |
6d5aefb8 | 517 | INIT_WORK(&adapter->watchdog_task, netxen_watchdog_task); |
44c10138 | 518 | adapter->ahw.revision_id = pdev->revision; |
3d396eb1 | 519 | |
6c80b18d MT |
520 | /* make sure Window == 1 */ |
521 | netxen_nic_pci_change_crbwindow(adapter, 1); | |
522 | ||
7830b22c DP |
523 | adapter->crb_addr_cmd_producer = crb_cmd_producer[adapter->portnum]; |
524 | adapter->crb_addr_cmd_consumer = crb_cmd_consumer[adapter->portnum]; | |
3176ff3e MT |
525 | netxen_nic_update_cmd_producer(adapter, 0); |
526 | netxen_nic_update_cmd_consumer(adapter, 0); | |
6c80b18d | 527 | writel(0, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_CMD_ADDR_LO)); |
3d396eb1 AK |
528 | |
529 | if (netxen_is_flash_supported(adapter) == 0 && | |
530 | netxen_get_flash_mac_addr(adapter, mac_addr) == 0) | |
531 | valid_mac = 1; | |
532 | else | |
533 | valid_mac = 0; | |
534 | ||
3176ff3e | 535 | if (valid_mac) { |
13ba9c77 | 536 | unsigned char *p = (unsigned char *)&mac_addr[adapter->portnum]; |
3176ff3e MT |
537 | netdev->dev_addr[0] = *(p + 5); |
538 | netdev->dev_addr[1] = *(p + 4); | |
539 | netdev->dev_addr[2] = *(p + 3); | |
540 | netdev->dev_addr[3] = *(p + 2); | |
541 | netdev->dev_addr[4] = *(p + 1); | |
542 | netdev->dev_addr[5] = *(p + 0); | |
543 | ||
544 | memcpy(netdev->perm_addr, netdev->dev_addr, | |
545 | netdev->addr_len); | |
546 | if (!is_valid_ether_addr(netdev->perm_addr)) { | |
0795af57 JP |
547 | printk(KERN_ERR "%s: Bad MAC address %s.\n", |
548 | netxen_nic_driver_name, | |
549 | print_mac(mac, netdev->dev_addr)); | |
3176ff3e MT |
550 | } else { |
551 | if (adapter->macaddr_set) | |
552 | adapter->macaddr_set(adapter, | |
553 | netdev->dev_addr); | |
554 | } | |
555 | } | |
556 | ||
6c80b18d | 557 | if (adapter->portnum == 0) { |
13ba9c77 | 558 | err = netxen_initialize_adapter_offload(adapter); |
4790654c | 559 | if (err) |
6c80b18d | 560 | goto err_out_free_rx_buffer; |
4790654c | 561 | val = readl(NETXEN_CRB_NORMALIZE(adapter, |
6c80b18d MT |
562 | NETXEN_CAM_RAM(0x1fc))); |
563 | if (val == 0x55555555) { | |
564 | /* This is the first boot after power up */ | |
644caeef | 565 | netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val); |
96acb6eb DP |
566 | if (!(val & 0x4)) { |
567 | val |= 0x4; | |
568 | netxen_nic_write_w0(adapter, NETXEN_PCIE_REG(0x4), val); | |
569 | netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(0x4), &val); | |
570 | if (!(val & 0x4)) | |
571 | printk(KERN_ERR "%s: failed to set MSI bit in PCI-e reg\n", | |
572 | netxen_nic_driver_name); | |
573 | } | |
6c80b18d MT |
574 | val = readl(NETXEN_CRB_NORMALIZE(adapter, |
575 | NETXEN_ROMUSB_GLB_SW_RESET)); | |
576 | printk(KERN_INFO"NetXen: read 0x%08x for reset reg.\n",val); | |
577 | if (val != 0x80000f) { | |
578 | /* clear the register for future unloads/loads */ | |
644caeef | 579 | writel(0, NETXEN_CRB_NORMALIZE(adapter, |
580 | NETXEN_CAM_RAM(0x1fc))); | |
581 | printk(KERN_ERR "ERROR in NetXen HW init sequence.\n"); | |
582 | err = -ENODEV; | |
583 | goto err_out_free_dev; | |
6c80b18d | 584 | } |
439b454e DP |
585 | } else { |
586 | writel(0, NETXEN_CRB_NORMALIZE(adapter, | |
587 | CRB_CMDPEG_STATE)); | |
588 | netxen_pinit_from_rom(adapter, 0); | |
589 | msleep(1); | |
590 | netxen_load_firmware(adapter); | |
591 | netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE); | |
6c80b18d | 592 | } |
96acb6eb DP |
593 | |
594 | /* clear the register for future unloads/loads */ | |
595 | writel(0, NETXEN_CRB_NORMALIZE(adapter, NETXEN_CAM_RAM(0x1fc))); | |
439b454e | 596 | dev_info(&pdev->dev, "cmdpeg state: 0x%0x\n", |
6c80b18d MT |
597 | readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE))); |
598 | ||
599 | /* | |
600 | * Tell the hardware our version number. | |
601 | */ | |
4790654c | 602 | i = (_NETXEN_NIC_LINUX_MAJOR << 16) |
6c80b18d MT |
603 | | ((_NETXEN_NIC_LINUX_MINOR << 8)) |
604 | | (_NETXEN_NIC_LINUX_SUBVERSION); | |
605 | writel(i, NETXEN_CRB_NORMALIZE(adapter, CRB_DRIVER_VERSION)); | |
3d396eb1 | 606 | |
3176ff3e MT |
607 | /* Unlock the HW, prompting the boot sequence */ |
608 | writel(1, | |
609 | NETXEN_CRB_NORMALIZE(adapter, | |
610 | NETXEN_ROMUSB_GLB_PEGTUNE_DONE)); | |
611 | /* Handshake with the card before we register the devices. */ | |
612 | netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE); | |
613 | } | |
3d396eb1 | 614 | |
3d396eb1 | 615 | /* |
6c80b18d | 616 | * See if the firmware gave us a virtual-physical port mapping. |
3d396eb1 | 617 | */ |
dcd56fdb | 618 | adapter->physical_port = adapter->portnum; |
6c80b18d MT |
619 | i = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_V2P(adapter->portnum))); |
620 | if (i != 0x55555555) | |
3276fbad | 621 | adapter->physical_port = i; |
6c80b18d | 622 | |
3176ff3e MT |
623 | netif_carrier_off(netdev); |
624 | netif_stop_queue(netdev); | |
625 | ||
6c80b18d MT |
626 | if ((err = register_netdev(netdev))) { |
627 | printk(KERN_ERR "%s: register_netdev failed port #%d" | |
628 | " aborting\n", netxen_nic_driver_name, | |
629 | adapter->portnum); | |
630 | err = -EIO; | |
631 | goto err_out_free_dev; | |
632 | } | |
633 | ||
dcd56fdb | 634 | netxen_nic_flash_print(adapter); |
6c80b18d | 635 | pci_set_drvdata(pdev, adapter); |
3d396eb1 | 636 | |
3d396eb1 AK |
637 | return 0; |
638 | ||
6c80b18d MT |
639 | err_out_free_dev: |
640 | if (adapter->portnum == 0) | |
641 | netxen_free_adapter_offload(adapter); | |
ed25ffa1 | 642 | |
6c80b18d | 643 | err_out_free_rx_buffer: |
3d396eb1 AK |
644 | for (i = 0; i < MAX_RCV_CTX; ++i) { |
645 | recv_ctx = &adapter->recv_ctx[i]; | |
646 | for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) { | |
647 | rcv_desc = &recv_ctx->rcv_desc[ring]; | |
648 | if (rcv_desc->rx_buf_arr != NULL) { | |
649 | vfree(rcv_desc->rx_buf_arr); | |
650 | rcv_desc->rx_buf_arr = NULL; | |
651 | } | |
652 | } | |
653 | } | |
3d396eb1 AK |
654 | vfree(cmd_buf_arr); |
655 | ||
6c80b18d MT |
656 | err_out_free_adapter: |
657 | if (adapter->flags & NETXEN_NIC_MSI_ENABLED) | |
658 | pci_disable_msi(pdev); | |
659 | ||
3d396eb1 | 660 | pci_set_drvdata(pdev, NULL); |
3d396eb1 | 661 | |
ed25ffa1 AK |
662 | if (db_ptr) |
663 | iounmap(db_ptr); | |
664 | ||
6c80b18d | 665 | err_out_iounmap: |
ed25ffa1 AK |
666 | if (mem_ptr0) |
667 | iounmap(mem_ptr0); | |
668 | if (mem_ptr1) | |
669 | iounmap(mem_ptr1); | |
670 | if (mem_ptr2) | |
671 | iounmap(mem_ptr2); | |
cb8011ad | 672 | |
6c80b18d MT |
673 | err_out_free_netdev: |
674 | free_netdev(netdev); | |
675 | ||
676 | err_out_free_res: | |
3d396eb1 | 677 | pci_release_regions(pdev); |
6c80b18d MT |
678 | |
679 | err_out_disable_pdev: | |
3d396eb1 AK |
680 | pci_disable_device(pdev); |
681 | return err; | |
682 | } | |
683 | ||
684 | static void __devexit netxen_nic_remove(struct pci_dev *pdev) | |
685 | { | |
686 | struct netxen_adapter *adapter; | |
3176ff3e | 687 | struct net_device *netdev; |
3d396eb1 AK |
688 | struct netxen_rx_buffer *buffer; |
689 | struct netxen_recv_context *recv_ctx; | |
690 | struct netxen_rcv_desc_ctx *rcv_desc; | |
96acb6eb DP |
691 | int i, ctxid, ring; |
692 | static int init_firmware_done = 0; | |
3d396eb1 | 693 | |
6c80b18d | 694 | adapter = pci_get_drvdata(pdev); |
3d396eb1 AK |
695 | if (adapter == NULL) |
696 | return; | |
697 | ||
6c80b18d MT |
698 | netdev = adapter->netdev; |
699 | ||
96acb6eb DP |
700 | unregister_netdev(netdev); |
701 | ||
96acb6eb DP |
702 | if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) { |
703 | init_firmware_done++; | |
3d396eb1 | 704 | netxen_free_hw_resources(adapter); |
96acb6eb | 705 | } |
3d396eb1 | 706 | |
3d396eb1 AK |
707 | for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) { |
708 | recv_ctx = &adapter->recv_ctx[ctxid]; | |
709 | for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) { | |
710 | rcv_desc = &recv_ctx->rcv_desc[ring]; | |
711 | for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) { | |
712 | buffer = &(rcv_desc->rx_buf_arr[i]); | |
713 | if (buffer->state == NETXEN_BUFFER_FREE) | |
714 | continue; | |
715 | pci_unmap_single(pdev, buffer->dma, | |
716 | rcv_desc->dma_size, | |
717 | PCI_DMA_FROMDEVICE); | |
718 | if (buffer->skb != NULL) | |
719 | dev_kfree_skb_any(buffer->skb); | |
720 | } | |
721 | vfree(rcv_desc->rx_buf_arr); | |
722 | } | |
723 | } | |
724 | ||
6c80b18d MT |
725 | vfree(adapter->cmd_buf_arr); |
726 | ||
439b454e DP |
727 | if (adapter->portnum == 0) |
728 | netxen_free_adapter_offload(adapter); | |
96acb6eb | 729 | |
3052246c | 730 | if (adapter->irq) |
731 | free_irq(adapter->irq, adapter); | |
732 | ||
733 | if (adapter->flags & NETXEN_NIC_MSI_ENABLED) | |
734 | pci_disable_msi(pdev); | |
735 | ||
6c80b18d MT |
736 | iounmap(adapter->ahw.db_base); |
737 | iounmap(adapter->ahw.pci_base0); | |
738 | iounmap(adapter->ahw.pci_base1); | |
739 | iounmap(adapter->ahw.pci_base2); | |
595e3fb8 MT |
740 | |
741 | pci_release_regions(pdev); | |
3052246c | 742 | pci_disable_device(pdev); |
595e3fb8 MT |
743 | pci_set_drvdata(pdev, NULL); |
744 | ||
6c80b18d | 745 | free_netdev(netdev); |
3d396eb1 AK |
746 | } |
747 | ||
748 | /* | |
749 | * Called when a network interface is made active | |
750 | * @returns 0 on success, negative value on failure | |
751 | */ | |
752 | static int netxen_nic_open(struct net_device *netdev) | |
753 | { | |
3176ff3e | 754 | struct netxen_adapter *adapter = (struct netxen_adapter *)netdev->priv; |
3d396eb1 AK |
755 | int err = 0; |
756 | int ctx, ring; | |
05aaa02d DP |
757 | irq_handler_t handler; |
758 | unsigned long flags = IRQF_SAMPLE_RANDOM; | |
3d396eb1 | 759 | |
dcd56fdb DP |
760 | if (adapter->driver_mismatch) |
761 | return -EIO; | |
762 | ||
3d396eb1 AK |
763 | if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC) { |
764 | err = netxen_init_firmware(adapter); | |
765 | if (err != 0) { | |
766 | printk(KERN_ERR "Failed to init firmware\n"); | |
767 | return -EIO; | |
768 | } | |
3d396eb1 AK |
769 | |
770 | /* setup all the resources for the Phantom... */ | |
771 | /* this include the descriptors for rcv, tx, and status */ | |
772 | netxen_nic_clear_stats(adapter); | |
773 | err = netxen_nic_hw_resources(adapter); | |
774 | if (err) { | |
775 | printk(KERN_ERR "Error in setting hw resources:%d\n", | |
776 | err); | |
777 | return err; | |
778 | } | |
3d396eb1 | 779 | for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) { |
cb8011ad | 780 | for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) |
3d396eb1 | 781 | netxen_post_rx_buffers(adapter, ctx, ring); |
3d396eb1 | 782 | } |
7830b22c | 783 | adapter->irq = adapter->pdev->irq; |
05aaa02d DP |
784 | if (adapter->flags & NETXEN_NIC_MSI_ENABLED) |
785 | handler = netxen_msi_intr; | |
786 | else { | |
787 | flags |= IRQF_SHARED; | |
788 | handler = netxen_intr; | |
789 | } | |
790 | err = request_irq(adapter->irq, handler, | |
791 | flags, netdev->name, adapter); | |
3d396eb1 AK |
792 | if (err) { |
793 | printk(KERN_ERR "request_irq failed with: %d\n", err); | |
ed25ffa1 | 794 | netxen_free_hw_resources(adapter); |
3d396eb1 AK |
795 | return err; |
796 | } | |
ed25ffa1 AK |
797 | |
798 | adapter->is_up = NETXEN_ADAPTER_UP_MAGIC; | |
799 | } | |
3d396eb1 AK |
800 | /* Done here again so that even if phantom sw overwrote it, |
801 | * we set it */ | |
595e3fb8 MT |
802 | if (adapter->init_port |
803 | && adapter->init_port(adapter, adapter->portnum) != 0) { | |
804 | printk(KERN_ERR "%s: Failed to initialize port %d\n", | |
805 | netxen_nic_driver_name, adapter->portnum); | |
595e3fb8 MT |
806 | return -EIO; |
807 | } | |
7eec517c | 808 | if (adapter->macaddr_set) |
809 | adapter->macaddr_set(adapter, netdev->dev_addr); | |
595e3fb8 | 810 | |
3176ff3e | 811 | netxen_nic_set_link_parameters(adapter); |
3d396eb1 AK |
812 | |
813 | netxen_nic_set_multi(netdev); | |
ed25ffa1 | 814 | if (adapter->set_mtu) |
3176ff3e | 815 | adapter->set_mtu(adapter, netdev->mtu); |
ed25ffa1 | 816 | |
dcd56fdb | 817 | mod_timer(&adapter->watchdog_timer, jiffies); |
05aaa02d DP |
818 | |
819 | napi_enable(&adapter->napi); | |
820 | netxen_nic_enable_int(adapter); | |
821 | ||
dcd56fdb | 822 | netif_start_queue(netdev); |
3d396eb1 AK |
823 | |
824 | return 0; | |
825 | } | |
826 | ||
827 | /* | |
828 | * netxen_nic_close - Disables a network interface entry point | |
829 | */ | |
830 | static int netxen_nic_close(struct net_device *netdev) | |
831 | { | |
3176ff3e | 832 | struct netxen_adapter *adapter = netdev_priv(netdev); |
3d396eb1 AK |
833 | int i, j; |
834 | struct netxen_cmd_buffer *cmd_buff; | |
835 | struct netxen_skb_frag *buffrag; | |
836 | ||
837 | netif_carrier_off(netdev); | |
838 | netif_stop_queue(netdev); | |
bea3348e | 839 | napi_disable(&adapter->napi); |
3d396eb1 | 840 | |
72b0a7a8 | 841 | if (adapter->stop_port) |
842 | adapter->stop_port(adapter); | |
843 | ||
e01872af | 844 | netxen_nic_disable_int(adapter); |
845 | ||
3176ff3e MT |
846 | cmd_buff = adapter->cmd_buf_arr; |
847 | for (i = 0; i < adapter->max_tx_desc_count; i++) { | |
848 | buffrag = cmd_buff->frag_array; | |
849 | if (buffrag->dma) { | |
850 | pci_unmap_single(adapter->pdev, buffrag->dma, | |
851 | buffrag->length, PCI_DMA_TODEVICE); | |
96acb6eb | 852 | buffrag->dma = 0ULL; |
3176ff3e MT |
853 | } |
854 | for (j = 0; j < cmd_buff->frag_count; j++) { | |
855 | buffrag++; | |
3d396eb1 | 856 | if (buffrag->dma) { |
3176ff3e | 857 | pci_unmap_page(adapter->pdev, buffrag->dma, |
4790654c | 858 | buffrag->length, |
3176ff3e | 859 | PCI_DMA_TODEVICE); |
96acb6eb | 860 | buffrag->dma = 0ULL; |
3d396eb1 | 861 | } |
3d396eb1 | 862 | } |
3176ff3e MT |
863 | /* Free the skb we received in netxen_nic_xmit_frame */ |
864 | if (cmd_buff->skb) { | |
865 | dev_kfree_skb_any(cmd_buff->skb); | |
866 | cmd_buff->skb = NULL; | |
867 | } | |
868 | cmd_buff++; | |
3d396eb1 | 869 | } |
96acb6eb DP |
870 | if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) { |
871 | FLUSH_SCHEDULED_WORK(); | |
872 | del_timer_sync(&adapter->watchdog_timer); | |
873 | } | |
3d396eb1 AK |
874 | |
875 | return 0; | |
876 | } | |
877 | ||
878 | static int netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev) | |
879 | { | |
3176ff3e | 880 | struct netxen_adapter *adapter = netdev_priv(netdev); |
3d396eb1 AK |
881 | struct netxen_hardware_context *hw = &adapter->ahw; |
882 | unsigned int first_seg_len = skb->len - skb->data_len; | |
883 | struct netxen_skb_frag *buffrag; | |
884 | unsigned int i; | |
885 | ||
ba53e6b4 | 886 | u32 producer, consumer; |
3d396eb1 AK |
887 | u32 saved_producer = 0; |
888 | struct cmd_desc_type0 *hwdesc; | |
889 | int k; | |
890 | struct netxen_cmd_buffer *pbuf = NULL; | |
3d396eb1 | 891 | int frag_count; |
3d396eb1 | 892 | int no_of_desc; |
ba53e6b4 | 893 | u32 num_txd = adapter->max_tx_desc_count; |
3d396eb1 | 894 | |
3d396eb1 AK |
895 | frag_count = skb_shinfo(skb)->nr_frags + 1; |
896 | ||
3d396eb1 AK |
897 | /* There 4 fragments per descriptor */ |
898 | no_of_desc = (frag_count + 3) >> 2; | |
cb8011ad AK |
899 | if (netdev->features & NETIF_F_TSO) { |
900 | if (skb_shinfo(skb)->gso_size > 0) { | |
901 | ||
3d396eb1 | 902 | no_of_desc++; |
ab6a5bb6 | 903 | if ((ip_hdrlen(skb) + tcp_hdrlen(skb) + |
c9bdd4b5 | 904 | sizeof(struct ethhdr)) > |
ed25ffa1 | 905 | (sizeof(struct cmd_desc_type0) - 2)) { |
cb8011ad AK |
906 | no_of_desc++; |
907 | } | |
3d396eb1 AK |
908 | } |
909 | } | |
53a01e00 | 910 | |
ba53e6b4 DP |
911 | producer = adapter->cmd_producer; |
912 | smp_mb(); | |
913 | consumer = adapter->last_cmd_consumer; | |
914 | if ((no_of_desc+2) > find_diff_among(producer, consumer, num_txd)) { | |
915 | netif_stop_queue(netdev); | |
916 | smp_mb(); | |
917 | return NETDEV_TX_BUSY; | |
53a01e00 | 918 | } |
3d396eb1 | 919 | |
3d396eb1 | 920 | /* Copy the descriptors into the hardware */ |
3d396eb1 AK |
921 | saved_producer = producer; |
922 | hwdesc = &hw->cmd_desc_head[producer]; | |
923 | memset(hwdesc, 0, sizeof(struct cmd_desc_type0)); | |
924 | /* Take skb->data itself */ | |
925 | pbuf = &adapter->cmd_buf_arr[producer]; | |
cb8011ad | 926 | if ((netdev->features & NETIF_F_TSO) && skb_shinfo(skb)->gso_size > 0) { |
a608ab9c | 927 | pbuf->mss = skb_shinfo(skb)->gso_size; |
ed33ebe4 | 928 | hwdesc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size); |
3d396eb1 AK |
929 | } else { |
930 | pbuf->mss = 0; | |
931 | hwdesc->mss = 0; | |
932 | } | |
3d396eb1 AK |
933 | pbuf->total_length = skb->len; |
934 | pbuf->skb = skb; | |
935 | pbuf->cmd = TX_ETHER_PKT; | |
936 | pbuf->frag_count = frag_count; | |
3176ff3e | 937 | pbuf->port = adapter->portnum; |
3d396eb1 | 938 | buffrag = &pbuf->frag_array[0]; |
3176ff3e | 939 | buffrag->dma = pci_map_single(adapter->pdev, skb->data, first_seg_len, |
3d396eb1 AK |
940 | PCI_DMA_TODEVICE); |
941 | buffrag->length = first_seg_len; | |
ed25ffa1 AK |
942 | netxen_set_cmd_desc_totallength(hwdesc, skb->len); |
943 | netxen_set_cmd_desc_num_of_buff(hwdesc, frag_count); | |
944 | netxen_set_cmd_desc_opcode(hwdesc, TX_ETHER_PKT); | |
3d396eb1 | 945 | |
3176ff3e | 946 | netxen_set_cmd_desc_port(hwdesc, adapter->portnum); |
6c80b18d | 947 | netxen_set_cmd_desc_ctxid(hwdesc, adapter->portnum); |
3d396eb1 AK |
948 | hwdesc->buffer1_length = cpu_to_le16(first_seg_len); |
949 | hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma); | |
950 | ||
951 | for (i = 1, k = 1; i < frag_count; i++, k++) { | |
952 | struct skb_frag_struct *frag; | |
953 | int len, temp_len; | |
954 | unsigned long offset; | |
955 | dma_addr_t temp_dma; | |
956 | ||
957 | /* move to next desc. if there is a need */ | |
958 | if ((i & 0x3) == 0) { | |
959 | k = 0; | |
ba53e6b4 | 960 | producer = get_next_index(producer, num_txd); |
3d396eb1 AK |
961 | hwdesc = &hw->cmd_desc_head[producer]; |
962 | memset(hwdesc, 0, sizeof(struct cmd_desc_type0)); | |
53a01e00 | 963 | pbuf = &adapter->cmd_buf_arr[producer]; |
964 | pbuf->skb = NULL; | |
3d396eb1 AK |
965 | } |
966 | frag = &skb_shinfo(skb)->frags[i - 1]; | |
967 | len = frag->size; | |
968 | offset = frag->page_offset; | |
969 | ||
970 | temp_len = len; | |
3176ff3e | 971 | temp_dma = pci_map_page(adapter->pdev, frag->page, offset, |
3d396eb1 AK |
972 | len, PCI_DMA_TODEVICE); |
973 | ||
974 | buffrag++; | |
975 | buffrag->dma = temp_dma; | |
976 | buffrag->length = temp_len; | |
977 | ||
3d396eb1 AK |
978 | switch (k) { |
979 | case 0: | |
980 | hwdesc->buffer1_length = cpu_to_le16(temp_len); | |
981 | hwdesc->addr_buffer1 = cpu_to_le64(temp_dma); | |
982 | break; | |
983 | case 1: | |
984 | hwdesc->buffer2_length = cpu_to_le16(temp_len); | |
985 | hwdesc->addr_buffer2 = cpu_to_le64(temp_dma); | |
986 | break; | |
987 | case 2: | |
988 | hwdesc->buffer3_length = cpu_to_le16(temp_len); | |
989 | hwdesc->addr_buffer3 = cpu_to_le64(temp_dma); | |
990 | break; | |
991 | case 3: | |
a608ab9c | 992 | hwdesc->buffer4_length = cpu_to_le16(temp_len); |
3d396eb1 AK |
993 | hwdesc->addr_buffer4 = cpu_to_le64(temp_dma); |
994 | break; | |
995 | } | |
996 | frag++; | |
997 | } | |
ba53e6b4 | 998 | producer = get_next_index(producer, num_txd); |
3d396eb1 AK |
999 | |
1000 | /* might change opcode to TX_TCP_LSO */ | |
1001 | netxen_tso_check(adapter, &hw->cmd_desc_head[saved_producer], skb); | |
1002 | ||
1003 | /* For LSO, we need to copy the MAC/IP/TCP headers into | |
1004 | * the descriptor ring | |
1005 | */ | |
ed25ffa1 AK |
1006 | if (netxen_get_cmd_desc_opcode(&hw->cmd_desc_head[saved_producer]) |
1007 | == TX_TCP_LSO) { | |
3d396eb1 AK |
1008 | int hdr_len, first_hdr_len, more_hdr; |
1009 | hdr_len = hw->cmd_desc_head[saved_producer].total_hdr_length; | |
ed25ffa1 AK |
1010 | if (hdr_len > (sizeof(struct cmd_desc_type0) - 2)) { |
1011 | first_hdr_len = sizeof(struct cmd_desc_type0) - 2; | |
3d396eb1 AK |
1012 | more_hdr = 1; |
1013 | } else { | |
1014 | first_hdr_len = hdr_len; | |
1015 | more_hdr = 0; | |
1016 | } | |
1017 | /* copy the MAC/IP/TCP headers to the cmd descriptor list */ | |
1018 | hwdesc = &hw->cmd_desc_head[producer]; | |
53a01e00 | 1019 | pbuf = &adapter->cmd_buf_arr[producer]; |
1020 | pbuf->skb = NULL; | |
3d396eb1 AK |
1021 | |
1022 | /* copy the first 64 bytes */ | |
ed25ffa1 | 1023 | memcpy(((void *)hwdesc) + 2, |
3d396eb1 | 1024 | (void *)(skb->data), first_hdr_len); |
ba53e6b4 | 1025 | producer = get_next_index(producer, num_txd); |
3d396eb1 AK |
1026 | |
1027 | if (more_hdr) { | |
1028 | hwdesc = &hw->cmd_desc_head[producer]; | |
53a01e00 | 1029 | pbuf = &adapter->cmd_buf_arr[producer]; |
1030 | pbuf->skb = NULL; | |
3d396eb1 AK |
1031 | /* copy the next 64 bytes - should be enough except |
1032 | * for pathological case | |
1033 | */ | |
d626f62b ACM |
1034 | skb_copy_from_linear_data_offset(skb, first_hdr_len, |
1035 | hwdesc, | |
1036 | (hdr_len - | |
1037 | first_hdr_len)); | |
ba53e6b4 | 1038 | producer = get_next_index(producer, num_txd); |
3d396eb1 AK |
1039 | } |
1040 | } | |
6c80b18d | 1041 | |
ba53e6b4 | 1042 | adapter->cmd_producer = producer; |
5dc16268 | 1043 | adapter->stats.txbytes += skb->len; |
6c80b18d | 1044 | |
ba53e6b4 | 1045 | netxen_nic_update_cmd_producer(adapter, adapter->cmd_producer); |
3d396eb1 | 1046 | |
ba53e6b4 | 1047 | adapter->stats.xmitcalled++; |
3d396eb1 AK |
1048 | netdev->trans_start = jiffies; |
1049 | ||
3d396eb1 AK |
1050 | return NETDEV_TX_OK; |
1051 | } | |
1052 | ||
1053 | static void netxen_watchdog(unsigned long v) | |
1054 | { | |
1055 | struct netxen_adapter *adapter = (struct netxen_adapter *)v; | |
ed25ffa1 AK |
1056 | |
1057 | SCHEDULE_WORK(&adapter->watchdog_task); | |
3d396eb1 AK |
1058 | } |
1059 | ||
1060 | static void netxen_tx_timeout(struct net_device *netdev) | |
1061 | { | |
3176ff3e MT |
1062 | struct netxen_adapter *adapter = (struct netxen_adapter *) |
1063 | netdev_priv(netdev); | |
1064 | SCHEDULE_WORK(&adapter->tx_timeout_task); | |
3d396eb1 AK |
1065 | } |
1066 | ||
6d5aefb8 | 1067 | static void netxen_tx_timeout_task(struct work_struct *work) |
3d396eb1 | 1068 | { |
4790654c | 1069 | struct netxen_adapter *adapter = |
3176ff3e | 1070 | container_of(work, struct netxen_adapter, tx_timeout_task); |
3d396eb1 AK |
1071 | |
1072 | printk(KERN_ERR "%s %s: transmit timeout, resetting.\n", | |
6c80b18d | 1073 | netxen_nic_driver_name, adapter->netdev->name); |
3d396eb1 | 1074 | |
ba53e6b4 DP |
1075 | netxen_nic_disable_int(adapter); |
1076 | napi_disable(&adapter->napi); | |
1077 | ||
6c80b18d | 1078 | adapter->netdev->trans_start = jiffies; |
ba53e6b4 DP |
1079 | |
1080 | napi_enable(&adapter->napi); | |
1081 | netxen_nic_enable_int(adapter); | |
6c80b18d | 1082 | netif_wake_queue(adapter->netdev); |
3d396eb1 AK |
1083 | } |
1084 | ||
05aaa02d DP |
1085 | static inline void |
1086 | netxen_handle_int(struct netxen_adapter *adapter) | |
3d396eb1 | 1087 | { |
2d1a3bbd | 1088 | netxen_nic_disable_int(adapter); |
05aaa02d | 1089 | napi_schedule(&adapter->napi); |
3d396eb1 AK |
1090 | } |
1091 | ||
1494a814 | 1092 | irqreturn_t netxen_intr(int irq, void *data) |
3d396eb1 | 1093 | { |
1daec86a | 1094 | struct netxen_adapter *adapter = data; |
e01872af | 1095 | u32 our_int = 0; |
3d396eb1 | 1096 | |
05aaa02d DP |
1097 | our_int = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR)); |
1098 | /* not our interrupt */ | |
1099 | if ((our_int & (0x80 << adapter->portnum)) == 0) | |
1100 | return IRQ_NONE; | |
e01872af | 1101 | |
1102 | if (adapter->intr_scheme == INTR_SCHEME_PERPORT) { | |
1103 | /* claim interrupt */ | |
05aaa02d | 1104 | writel(our_int & ~((u32)(0x80 << adapter->portnum)), |
e01872af | 1105 | NETXEN_CRB_NORMALIZE(adapter, CRB_INT_VECTOR)); |
e01872af | 1106 | } |
1107 | ||
05aaa02d | 1108 | netxen_handle_int(adapter); |
3d396eb1 AK |
1109 | |
1110 | return IRQ_HANDLED; | |
1111 | } | |
1112 | ||
05aaa02d DP |
1113 | irqreturn_t netxen_msi_intr(int irq, void *data) |
1114 | { | |
1115 | struct netxen_adapter *adapter = data; | |
1116 | ||
1117 | netxen_handle_int(adapter); | |
1118 | return IRQ_HANDLED; | |
1119 | } | |
1120 | ||
bea3348e | 1121 | static int netxen_nic_poll(struct napi_struct *napi, int budget) |
3d396eb1 | 1122 | { |
bea3348e | 1123 | struct netxen_adapter *adapter = container_of(napi, struct netxen_adapter, napi); |
05aaa02d | 1124 | int tx_complete; |
3d396eb1 | 1125 | int ctx; |
bea3348e | 1126 | int work_done; |
3d396eb1 | 1127 | |
05aaa02d | 1128 | tx_complete = netxen_process_cmd_ring(adapter); |
3d396eb1 | 1129 | |
80922fbc | 1130 | work_done = 0; |
3d396eb1 AK |
1131 | for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) { |
1132 | /* | |
1133 | * Fairness issue. This will give undue weight to the | |
1134 | * receive context 0. | |
1135 | */ | |
1136 | ||
1137 | /* | |
1138 | * To avoid starvation, we give each of our receivers, | |
1139 | * a fraction of the quota. Sometimes, it might happen that we | |
1140 | * have enough quota to process every packet, but since all the | |
1141 | * packets are on one context, it gets only half of the quota, | |
1142 | * and ends up not processing it. | |
1143 | */ | |
bea3348e SH |
1144 | work_done += netxen_process_rcv_ring(adapter, ctx, |
1145 | budget / MAX_RCV_CTX); | |
3d396eb1 AK |
1146 | } |
1147 | ||
05aaa02d DP |
1148 | if ((work_done < budget) && tx_complete) { |
1149 | netif_rx_complete(adapter->netdev, &adapter->napi); | |
3d396eb1 AK |
1150 | netxen_nic_enable_int(adapter); |
1151 | } | |
1152 | ||
bea3348e | 1153 | return work_done; |
3d396eb1 AK |
1154 | } |
1155 | ||
1156 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
1157 | static void netxen_nic_poll_controller(struct net_device *netdev) | |
1158 | { | |
3176ff3e | 1159 | struct netxen_adapter *adapter = netdev_priv(netdev); |
3d396eb1 | 1160 | disable_irq(adapter->irq); |
1494a814 | 1161 | netxen_intr(adapter->irq, adapter); |
3d396eb1 AK |
1162 | enable_irq(adapter->irq); |
1163 | } | |
1164 | #endif | |
3d396eb1 AK |
1165 | |
1166 | static struct pci_driver netxen_driver = { | |
1167 | .name = netxen_nic_driver_name, | |
1168 | .id_table = netxen_pci_tbl, | |
1169 | .probe = netxen_nic_probe, | |
1170 | .remove = __devexit_p(netxen_nic_remove) | |
1171 | }; | |
1172 | ||
1173 | /* Driver Registration on NetXen card */ | |
1174 | ||
1175 | static int __init netxen_init_module(void) | |
1176 | { | |
79ea13ce | 1177 | if ((netxen_workq = create_singlethread_workqueue("netxen")) == NULL) |
ed25ffa1 AK |
1178 | return -ENOMEM; |
1179 | ||
184231bd | 1180 | return pci_register_driver(&netxen_driver); |
3d396eb1 AK |
1181 | } |
1182 | ||
1183 | module_init(netxen_init_module); | |
1184 | ||
1185 | static void __exit netxen_exit_module(void) | |
1186 | { | |
3d396eb1 | 1187 | pci_unregister_driver(&netxen_driver); |
9de06610 | 1188 | destroy_workqueue(netxen_workq); |
3d396eb1 AK |
1189 | } |
1190 | ||
1191 | module_exit(netxen_exit_module); |