blob: 7bdcbc67342f7e964d4b410fbc395b9c7af100fb [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
31#include <linux/netdevice.h>
32#include <linux/delay.h>
33#include "netxen_nic.h"
34#include "netxen_nic_hw.h"
Amit S. Kale3d396eb2006-10-21 15:33:03 -040035#include "netxen_nic_phan_reg.h"
36
37struct crb_addr_pair {
Linsys Contractor Mithlesh Thukrale0e20a12007-02-28 05:16:40 -080038 u32 addr;
39 u32 data;
Amit S. Kale3d396eb2006-10-21 15:33:03 -040040};
41
42#define NETXEN_MAX_CRB_XFORM 60
43static unsigned int crb_addr_xform[NETXEN_MAX_CRB_XFORM];
Linsys Contractor Mithlesh Thukrale0e20a12007-02-28 05:16:40 -080044#define NETXEN_ADDR_ERROR (0xffffffff)
Amit S. Kale3d396eb2006-10-21 15:33:03 -040045
46#define crb_addr_transform(name) \
47 crb_addr_xform[NETXEN_HW_PX_MAP_CRB_##name] = \
48 NETXEN_HW_CRB_HUB_AGT_ADR_##name << 20
49
Amit S. Kalecb8011a2006-11-29 09:00:10 -080050#define NETXEN_NIC_XDMA_RESET 0x8000ff
51
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +000052static void
Dhananjay Phadked8b100c2009-03-13 14:52:05 +000053netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
54 struct nx_host_rds_ring *rds_ring);
Adrian Bunk993fb902007-11-05 18:07:31 +010055
Amit S. Kale3d396eb2006-10-21 15:33:03 -040056static void crb_addr_transform_setup(void)
57{
58 crb_addr_transform(XDMA);
59 crb_addr_transform(TIMR);
60 crb_addr_transform(SRE);
61 crb_addr_transform(SQN3);
62 crb_addr_transform(SQN2);
63 crb_addr_transform(SQN1);
64 crb_addr_transform(SQN0);
65 crb_addr_transform(SQS3);
66 crb_addr_transform(SQS2);
67 crb_addr_transform(SQS1);
68 crb_addr_transform(SQS0);
69 crb_addr_transform(RPMX7);
70 crb_addr_transform(RPMX6);
71 crb_addr_transform(RPMX5);
72 crb_addr_transform(RPMX4);
73 crb_addr_transform(RPMX3);
74 crb_addr_transform(RPMX2);
75 crb_addr_transform(RPMX1);
76 crb_addr_transform(RPMX0);
77 crb_addr_transform(ROMUSB);
78 crb_addr_transform(SN);
79 crb_addr_transform(QMN);
80 crb_addr_transform(QMS);
81 crb_addr_transform(PGNI);
82 crb_addr_transform(PGND);
83 crb_addr_transform(PGN3);
84 crb_addr_transform(PGN2);
85 crb_addr_transform(PGN1);
86 crb_addr_transform(PGN0);
87 crb_addr_transform(PGSI);
88 crb_addr_transform(PGSD);
89 crb_addr_transform(PGS3);
90 crb_addr_transform(PGS2);
91 crb_addr_transform(PGS1);
92 crb_addr_transform(PGS0);
93 crb_addr_transform(PS);
94 crb_addr_transform(PH);
95 crb_addr_transform(NIU);
96 crb_addr_transform(I2Q);
97 crb_addr_transform(EG);
98 crb_addr_transform(MN);
99 crb_addr_transform(MS);
100 crb_addr_transform(CAS2);
101 crb_addr_transform(CAS1);
102 crb_addr_transform(CAS0);
103 crb_addr_transform(CAM);
104 crb_addr_transform(C2C1);
105 crb_addr_transform(C2C0);
Amit S. Kale1fcca1a2007-02-05 07:35:26 -0800106 crb_addr_transform(SMB);
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -0700107 crb_addr_transform(OCM0);
108 crb_addr_transform(I2C0);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400109}
110
Dhananjay Phadke29566402008-07-21 19:44:04 -0700111void netxen_release_rx_buffers(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400112{
Dhananjay Phadke29566402008-07-21 19:44:04 -0700113 struct netxen_recv_context *recv_ctx;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700114 struct nx_host_rds_ring *rds_ring;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700115 struct netxen_rx_buffer *rx_buf;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000116 int i, ring;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400117
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000118 recv_ctx = &adapter->recv_ctx;
119 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
120 rds_ring = &recv_ctx->rds_rings[ring];
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000121 for (i = 0; i < rds_ring->num_desc; ++i) {
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000122 rx_buf = &(rds_ring->rx_buf_arr[i]);
123 if (rx_buf->state == NETXEN_BUFFER_FREE)
124 continue;
125 pci_unmap_single(adapter->pdev,
126 rx_buf->dma,
127 rds_ring->dma_size,
128 PCI_DMA_FROMDEVICE);
129 if (rx_buf->skb != NULL)
130 dev_kfree_skb_any(rx_buf->skb);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700131 }
132 }
133}
134
135void netxen_release_tx_buffers(struct netxen_adapter *adapter)
136{
137 struct netxen_cmd_buffer *cmd_buf;
138 struct netxen_skb_frag *buffrag;
139 int i, j;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000140 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700141
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000142 cmd_buf = tx_ring->cmd_buf_arr;
143 for (i = 0; i < tx_ring->num_desc; i++) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700144 buffrag = cmd_buf->frag_array;
145 if (buffrag->dma) {
146 pci_unmap_single(adapter->pdev, buffrag->dma,
147 buffrag->length, PCI_DMA_TODEVICE);
148 buffrag->dma = 0ULL;
149 }
150 for (j = 0; j < cmd_buf->frag_count; j++) {
151 buffrag++;
152 if (buffrag->dma) {
153 pci_unmap_page(adapter->pdev, buffrag->dma,
154 buffrag->length,
155 PCI_DMA_TODEVICE);
156 buffrag->dma = 0ULL;
157 }
158 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700159 if (cmd_buf->skb) {
160 dev_kfree_skb_any(cmd_buf->skb);
161 cmd_buf->skb = NULL;
162 }
163 cmd_buf++;
164 }
165}
166
167void netxen_free_sw_resources(struct netxen_adapter *adapter)
168{
169 struct netxen_recv_context *recv_ctx;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700170 struct nx_host_rds_ring *rds_ring;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000171 struct nx_host_tx_ring *tx_ring;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000172 int ring;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700173
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000174 recv_ctx = &adapter->recv_ctx;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000175
176 if (recv_ctx->rds_rings == NULL)
177 goto skip_rds;
178
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000179 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
180 rds_ring = &recv_ctx->rds_rings[ring];
Figo.zhangf2333a02009-06-11 02:49:10 -0700181 vfree(rds_ring->rx_buf_arr);
182 rds_ring->rx_buf_arr = NULL;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700183 }
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000184 kfree(recv_ctx->rds_rings);
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000185
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000186skip_rds:
187 if (adapter->tx_ring == NULL)
188 return;
189
190 tx_ring = adapter->tx_ring;
Figo.zhangf2333a02009-06-11 02:49:10 -0700191 vfree(tx_ring->cmd_buf_arr);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700192}
193
194int netxen_alloc_sw_resources(struct netxen_adapter *adapter)
195{
196 struct netxen_recv_context *recv_ctx;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700197 struct nx_host_rds_ring *rds_ring;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000198 struct nx_host_sds_ring *sds_ring;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000199 struct nx_host_tx_ring *tx_ring;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700200 struct netxen_rx_buffer *rx_buf;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000201 int ring, i, size;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700202
203 struct netxen_cmd_buffer *cmd_buf_arr;
204 struct net_device *netdev = adapter->netdev;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000205 struct pci_dev *pdev = adapter->pdev;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700206
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000207 size = sizeof(struct nx_host_tx_ring);
208 tx_ring = kzalloc(size, GFP_KERNEL);
209 if (tx_ring == NULL) {
210 dev_err(&pdev->dev, "%s: failed to allocate tx ring struct\n",
211 netdev->name);
212 return -ENOMEM;
213 }
214 adapter->tx_ring = tx_ring;
215
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000216 tx_ring->num_desc = adapter->num_txd;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000217
218 cmd_buf_arr = vmalloc(TX_BUFF_RINGSIZE(tx_ring));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700219 if (cmd_buf_arr == NULL) {
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000220 dev_err(&pdev->dev, "%s: failed to allocate cmd buffer ring\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700221 netdev->name);
222 return -ENOMEM;
223 }
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000224 memset(cmd_buf_arr, 0, TX_BUFF_RINGSIZE(tx_ring));
225 tx_ring->cmd_buf_arr = cmd_buf_arr;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700226
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000227 recv_ctx = &adapter->recv_ctx;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000228
229 size = adapter->max_rds_rings * sizeof (struct nx_host_rds_ring);
230 rds_ring = kzalloc(size, GFP_KERNEL);
231 if (rds_ring == NULL) {
232 dev_err(&pdev->dev, "%s: failed to allocate rds ring struct\n",
233 netdev->name);
234 return -ENOMEM;
235 }
236 recv_ctx->rds_rings = rds_ring;
237
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000238 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
239 rds_ring = &recv_ctx->rds_rings[ring];
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000240 switch (ring) {
241 case RCV_RING_NORMAL:
242 rds_ring->num_desc = adapter->num_rxd;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000243 if (adapter->ahw.cut_through) {
244 rds_ring->dma_size =
245 NX_CT_DEFAULT_RX_BUF_LEN;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700246 rds_ring->skb_size =
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000247 NX_CT_DEFAULT_RX_BUF_LEN;
248 } else {
249 rds_ring->dma_size = RX_DMA_MAP_LEN;
250 rds_ring->skb_size =
251 MAX_RX_BUFFER_LENGTH;
252 }
253 break;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700254
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000255 case RCV_RING_JUMBO:
256 rds_ring->num_desc = adapter->num_jumbo_rxd;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000257 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
258 rds_ring->dma_size =
259 NX_P3_RX_JUMBO_BUF_MAX_LEN;
260 else
261 rds_ring->dma_size =
262 NX_P2_RX_JUMBO_BUF_MAX_LEN;
263 rds_ring->skb_size =
264 rds_ring->dma_size + NET_IP_ALIGN;
265 break;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700266
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000267 case RCV_RING_LRO:
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000268 rds_ring->num_desc = adapter->num_lro_rxd;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000269 rds_ring->dma_size = RX_LRO_DMA_MAP_LEN;
270 rds_ring->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
271 break;
272
273 }
274 rds_ring->rx_buf_arr = (struct netxen_rx_buffer *)
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000275 vmalloc(RCV_BUFF_RINGSIZE(rds_ring));
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000276 if (rds_ring->rx_buf_arr == NULL) {
277 printk(KERN_ERR "%s: Failed to allocate "
278 "rx buffer ring %d\n",
279 netdev->name, ring);
280 /* free whatever was already allocated */
281 goto err_out;
282 }
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000283 memset(rds_ring->rx_buf_arr, 0, RCV_BUFF_RINGSIZE(rds_ring));
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000284 INIT_LIST_HEAD(&rds_ring->free_list);
285 /*
286 * Now go through all of them, set reference handles
287 * and put them in the queues.
288 */
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000289 rx_buf = rds_ring->rx_buf_arr;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000290 for (i = 0; i < rds_ring->num_desc; i++) {
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000291 list_add_tail(&rx_buf->list,
292 &rds_ring->free_list);
293 rx_buf->ref_handle = i;
294 rx_buf->state = NETXEN_BUFFER_FREE;
295 rx_buf++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400296 }
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000297 spin_lock_init(&rds_ring->lock);
298 }
299
300 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
301 sds_ring = &recv_ctx->sds_rings[ring];
302 sds_ring->irq = adapter->msix_entries[ring].vector;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000303 sds_ring->adapter = adapter;
304 sds_ring->num_desc = adapter->num_rxd;
305
306 for (i = 0; i < NUM_RCV_DESC_RINGS; i++)
307 INIT_LIST_HEAD(&sds_ring->free_list[i]);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400308 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700309
310 return 0;
311
312err_out:
313 netxen_free_sw_resources(adapter);
314 return -ENOMEM;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400315}
316
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400317void netxen_initialize_adapter_ops(struct netxen_adapter *adapter)
318{
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000319 adapter->macaddr_set = netxen_p2_nic_set_mac_addr;
320 adapter->set_multi = netxen_p2_nic_set_multi;
321
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000322 switch (adapter->ahw.port_type) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400323 case NETXEN_NIC_GBE:
Amit S. Kale80922fb2006-12-04 09:18:00 -0800324 adapter->enable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400325 netxen_niu_gbe_enable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800326 adapter->disable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400327 netxen_niu_gbe_disable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800328 adapter->set_mtu = netxen_nic_set_mtu_gb;
329 adapter->set_promisc = netxen_niu_set_promiscuous_mode;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800330 adapter->phy_read = netxen_niu_gbe_phy_read;
331 adapter->phy_write = netxen_niu_gbe_phy_write;
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -0700332 adapter->init_port = netxen_niu_gbe_init_port;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800333 adapter->stop_port = netxen_niu_disable_gbe_port;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400334 break;
335
336 case NETXEN_NIC_XGBE:
Amit S. Kale80922fb2006-12-04 09:18:00 -0800337 adapter->enable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400338 netxen_niu_xgbe_enable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800339 adapter->disable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400340 netxen_niu_xgbe_disable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800341 adapter->set_mtu = netxen_nic_set_mtu_xgb;
342 adapter->init_port = netxen_niu_xg_init_port;
343 adapter->set_promisc = netxen_niu_xg_set_promiscuous_mode;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800344 adapter->stop_port = netxen_niu_disable_xg_port;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400345 break;
346
347 default:
348 break;
349 }
Dhananjay Phadke9ad27642008-08-01 03:14:59 -0700350
351 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
352 adapter->set_mtu = nx_fw_cmd_set_mtu;
353 adapter->set_promisc = netxen_p3_nic_set_promisc;
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000354 adapter->macaddr_set = netxen_p3_nic_set_mac_addr;
355 adapter->set_multi = netxen_p3_nic_set_multi;
Dhananjay Phadke9ad27642008-08-01 03:14:59 -0700356 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400357}
358
359/*
360 * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
361 * address to external PCI CRB address.
362 */
Adrian Bunk993fb902007-11-05 18:07:31 +0100363static u32 netxen_decode_crb_addr(u32 addr)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400364{
365 int i;
Linsys Contractor Mithlesh Thukrale0e20a12007-02-28 05:16:40 -0800366 u32 base_addr, offset, pci_base;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400367
368 crb_addr_transform_setup();
369
370 pci_base = NETXEN_ADDR_ERROR;
371 base_addr = addr & 0xfff00000;
372 offset = addr & 0x000fffff;
373
374 for (i = 0; i < NETXEN_MAX_CRB_XFORM; i++) {
375 if (crb_addr_xform[i] == base_addr) {
376 pci_base = i << 20;
377 break;
378 }
379 }
380 if (pci_base == NETXEN_ADDR_ERROR)
381 return pci_base;
382 else
383 return (pci_base + offset);
384}
385
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700386static long rom_max_timeout = 100;
387static long rom_lock_timeout = 10000;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400388
Adrian Bunk993fb902007-11-05 18:07:31 +0100389static int rom_lock(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400390{
391 int iter;
392 u32 done = 0;
393 int timeout = 0;
394
395 while (!done) {
396 /* acquire semaphore2 from PCI HW block */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000397 done = NXRD32(adapter, NETXEN_PCIE_REG(PCIE_SEM2_LOCK));
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400398 if (done == 1)
399 break;
400 if (timeout >= rom_lock_timeout)
401 return -EIO;
402
403 timeout++;
404 /*
405 * Yield CPU
406 */
407 if (!in_atomic())
408 schedule();
409 else {
410 for (iter = 0; iter < 20; iter++)
411 cpu_relax(); /*This a nop instr on i386 */
412 }
413 }
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000414 NXWR32(adapter, NETXEN_ROM_LOCK_ID, ROM_LOCK_DRIVER);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400415 return 0;
416}
417
Adrian Bunk993fb902007-11-05 18:07:31 +0100418static int netxen_wait_rom_done(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400419{
420 long timeout = 0;
421 long done = 0;
422
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800423 cond_resched();
424
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400425 while (done == 0) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000426 done = NXRD32(adapter, NETXEN_ROMUSB_GLB_STATUS);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400427 done &= 2;
428 timeout++;
429 if (timeout >= rom_max_timeout) {
430 printk("Timeout reached waiting for rom done");
431 return -EIO;
432 }
433 }
434 return 0;
435}
436
Adrian Bunk993fb902007-11-05 18:07:31 +0100437static void netxen_rom_unlock(struct netxen_adapter *adapter)
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800438{
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800439 /* release semaphore2 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000440 NXRD32(adapter, NETXEN_PCIE_REG(PCIE_SEM2_UNLOCK));
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800441
442}
443
Adrian Bunk993fb902007-11-05 18:07:31 +0100444static int do_rom_fast_read(struct netxen_adapter *adapter,
445 int addr, int *valp)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400446{
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000447 NXWR32(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
448 NXWR32(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
449 NXWR32(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
450 NXWR32(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0xb);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400451 if (netxen_wait_rom_done(adapter)) {
452 printk("Error waiting for rom done\n");
453 return -EIO;
454 }
455 /* reset abyte_cnt and dummy_byte_cnt */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000456 NXWR32(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800457 udelay(10);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000458 NXWR32(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400459
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000460 *valp = NXRD32(adapter, NETXEN_ROMUSB_ROM_RDATA);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400461 return 0;
462}
463
Adrian Bunk993fb902007-11-05 18:07:31 +0100464static int do_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
465 u8 *bytes, size_t size)
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800466{
467 int addridx;
468 int ret = 0;
469
470 for (addridx = addr; addridx < (addr + size); addridx += 4) {
Al Virof305f782007-12-22 19:44:00 +0000471 int v;
472 ret = do_rom_fast_read(adapter, addridx, &v);
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800473 if (ret != 0)
474 break;
Al Virof305f782007-12-22 19:44:00 +0000475 *(__le32 *)bytes = cpu_to_le32(v);
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800476 bytes += 4;
477 }
478
479 return ret;
480}
481
482int
Jeff Garzik47906542007-11-23 21:23:36 -0500483netxen_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800484 u8 *bytes, size_t size)
485{
486 int ret;
487
488 ret = rom_lock(adapter);
489 if (ret < 0)
490 return ret;
491
492 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
493
494 netxen_rom_unlock(adapter);
495 return ret;
496}
497
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400498int netxen_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
499{
500 int ret;
501
502 if (rom_lock(adapter) != 0)
503 return -EIO;
504
505 ret = do_rom_fast_read(adapter, addr, valp);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800506 netxen_rom_unlock(adapter);
507 return ret;
508}
509
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400510#define NETXEN_BOARDTYPE 0x4008
511#define NETXEN_BOARDNUM 0x400c
512#define NETXEN_CHIPNUM 0x4010
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400513
514int netxen_pinit_from_rom(struct netxen_adapter *adapter, int verbose)
515{
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700516 int addr, val;
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800517 int i, n, init_delay = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400518 struct crb_addr_pair *buf;
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800519 unsigned offset;
Linsys Contractor Mithlesh Thukrale0e20a12007-02-28 05:16:40 -0800520 u32 off;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400521
522 /* resetall */
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800523 rom_lock(adapter);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000524 NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, 0xffffffff);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800525 netxen_rom_unlock(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400526
527 if (verbose) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400528 if (netxen_rom_fast_read(adapter, NETXEN_BOARDTYPE, &val) == 0)
529 printk("P2 ROM board type: 0x%08x\n", val);
530 else
531 printk("Could not read board type\n");
532 if (netxen_rom_fast_read(adapter, NETXEN_BOARDNUM, &val) == 0)
533 printk("P2 ROM board num: 0x%08x\n", val);
534 else
535 printk("Could not read board number\n");
536 if (netxen_rom_fast_read(adapter, NETXEN_CHIPNUM, &val) == 0)
537 printk("P2 ROM chip num: 0x%08x\n", val);
538 else
539 printk("Could not read chip number\n");
540 }
541
Dhananjay Phadke29566402008-07-21 19:44:04 -0700542 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
543 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800544 (n != 0xcafecafe) ||
Dhananjay Phadke29566402008-07-21 19:44:04 -0700545 netxen_rom_fast_read(adapter, 4, &n) != 0) {
546 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
547 "n: %08x\n", netxen_nic_driver_name, n);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400548 return -EIO;
549 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700550 offset = n & 0xffffU;
551 n = (n >> 16) & 0xffffU;
552 } else {
553 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
554 !(n & 0x80000000)) {
555 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
556 "n: %08x\n", netxen_nic_driver_name, n);
557 return -EIO;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400558 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700559 offset = 1;
560 n &= ~0x80000000;
561 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400562
Dhananjay Phadke29566402008-07-21 19:44:04 -0700563 if (n < 1024) {
564 if (verbose)
565 printk(KERN_DEBUG "%s: %d CRB init values found"
566 " in ROM.\n", netxen_nic_driver_name, n);
567 } else {
568 printk(KERN_ERR "%s:n=0x%x Error! NetXen card flash not"
569 " initialized.\n", __func__, n);
570 return -EIO;
571 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400572
Dhananjay Phadke29566402008-07-21 19:44:04 -0700573 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
574 if (buf == NULL) {
575 printk("%s: netxen_pinit_from_rom: Unable to calloc memory.\n",
576 netxen_nic_driver_name);
577 return -ENOMEM;
578 }
579 for (i = 0; i < n; i++) {
580 if (netxen_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
Daniel Marjamäki584dbe92009-01-29 08:55:56 +0000581 netxen_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0) {
582 kfree(buf);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700583 return -EIO;
Daniel Marjamäki584dbe92009-01-29 08:55:56 +0000584 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400585
Dhananjay Phadke29566402008-07-21 19:44:04 -0700586 buf[i].addr = addr;
587 buf[i].data = val;
588
589 if (verbose)
590 printk(KERN_DEBUG "%s: PCI: 0x%08x == 0x%08x\n",
591 netxen_nic_driver_name,
592 (u32)netxen_decode_crb_addr(addr), val);
593 }
594 for (i = 0; i < n; i++) {
595
596 off = netxen_decode_crb_addr(buf[i].addr);
597 if (off == NETXEN_ADDR_ERROR) {
598 printk(KERN_ERR"CRB init value out of range %x\n",
Amit S. Kale1fcca1a2007-02-05 07:35:26 -0800599 buf[i].addr);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700600 continue;
601 }
602 off += NETXEN_PCI_CRBSPACE;
603 /* skipping cold reboot MAGIC */
604 if (off == NETXEN_CAM_RAM(0x1fc))
605 continue;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400606
Dhananjay Phadke29566402008-07-21 19:44:04 -0700607 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
608 /* do not reset PCI */
609 if (off == (ROMUSB_GLB + 0xbc))
610 continue;
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800611 if (off == (ROMUSB_GLB + 0xa8))
612 continue;
613 if (off == (ROMUSB_GLB + 0xc8)) /* core clock */
614 continue;
615 if (off == (ROMUSB_GLB + 0x24)) /* MN clock */
616 continue;
617 if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */
618 continue;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700619 if (off == (NETXEN_CRB_PEG_NET_1 + 0x18))
620 buf[i].data = 0x1020;
621 /* skip the function enable register */
622 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION))
623 continue;
624 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION2))
625 continue;
626 if ((off & 0x0ff00000) == NETXEN_CRB_SMB)
627 continue;
628 }
629
630 if (off == NETXEN_ADDR_ERROR) {
631 printk(KERN_ERR "%s: Err: Unknown addr: 0x%08x\n",
632 netxen_nic_driver_name, buf[i].addr);
633 continue;
634 }
635
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800636 init_delay = 1;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700637 /* After writing this register, HW needs time for CRB */
638 /* to quiet down (else crb_window returns 0xffffffff) */
639 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800640 init_delay = 1000;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700641 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400642 /* hold xdma in reset also */
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800643 buf[i].data = NETXEN_NIC_XDMA_RESET;
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800644 buf[i].data = 0x8000ff;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400645 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400646 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400647
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000648 NXWR32(adapter, off, buf[i].data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400649
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800650 msleep(init_delay);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400651 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700652 kfree(buf);
653
654 /* disable_peg_cache_all */
655
656 /* unreset_net_cache */
657 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000658 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
659 NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, (val & 0xffffff0f));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700660 }
661
662 /* p2dn replyCount */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000663 NXWR32(adapter, NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700664 /* disable_peg_cache 0 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000665 NXWR32(adapter, NETXEN_CRB_PEG_NET_D + 0x4c, 8);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700666 /* disable_peg_cache 1 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000667 NXWR32(adapter, NETXEN_CRB_PEG_NET_I + 0x4c, 8);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700668
669 /* peg_clr_all */
670
671 /* peg_clr 0 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000672 NXWR32(adapter, NETXEN_CRB_PEG_NET_0 + 0x8, 0);
673 NXWR32(adapter, NETXEN_CRB_PEG_NET_0 + 0xc, 0);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700674 /* peg_clr 1 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000675 NXWR32(adapter, NETXEN_CRB_PEG_NET_1 + 0x8, 0);
676 NXWR32(adapter, NETXEN_CRB_PEG_NET_1 + 0xc, 0);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700677 /* peg_clr 2 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000678 NXWR32(adapter, NETXEN_CRB_PEG_NET_2 + 0x8, 0);
679 NXWR32(adapter, NETXEN_CRB_PEG_NET_2 + 0xc, 0);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700680 /* peg_clr 3 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000681 NXWR32(adapter, NETXEN_CRB_PEG_NET_3 + 0x8, 0);
682 NXWR32(adapter, NETXEN_CRB_PEG_NET_3 + 0xc, 0);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400683 return 0;
684}
685
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000686int
687netxen_load_firmware(struct netxen_adapter *adapter)
688{
689 u64 *ptr64;
690 u32 i, flashaddr, size;
691 const struct firmware *fw = adapter->fw;
692
693 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
694 NXWR32(adapter, NETXEN_ROMUSB_GLB_CAS_RST, 1);
695
696 if (fw) {
697 __le64 data;
698
699 size = (NETXEN_IMAGE_START - NETXEN_BOOTLD_START) / 8;
700
701 ptr64 = (u64 *)&fw->data[NETXEN_BOOTLD_START];
702 flashaddr = NETXEN_BOOTLD_START;
703
704 for (i = 0; i < size; i++) {
705 data = cpu_to_le64(ptr64[i]);
706 adapter->pci_mem_write(adapter, flashaddr, &data, 8);
707 flashaddr += 8;
708 }
709
710 size = *(u32 *)&fw->data[NX_FW_SIZE_OFFSET];
711 size = (__force u32)cpu_to_le32(size) / 8;
712
713 ptr64 = (u64 *)&fw->data[NETXEN_IMAGE_START];
714 flashaddr = NETXEN_IMAGE_START;
715
716 for (i = 0; i < size; i++) {
717 data = cpu_to_le64(ptr64[i]);
718
719 if (adapter->pci_mem_write(adapter,
720 flashaddr, &data, 8))
721 return -EIO;
722
723 flashaddr += 8;
724 }
725 } else {
726 u32 data;
727
728 size = (NETXEN_IMAGE_START - NETXEN_BOOTLD_START) / 4;
729 flashaddr = NETXEN_BOOTLD_START;
730
731 for (i = 0; i < size; i++) {
732 if (netxen_rom_fast_read(adapter,
733 flashaddr, (int *)&data) != 0)
734 return -EIO;
735
736 if (adapter->pci_mem_write(adapter,
737 flashaddr, &data, 4))
738 return -EIO;
739
740 flashaddr += 4;
741 }
742 }
743 msleep(1);
744
745 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
746 NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, 0x80001d);
747 else {
748 NXWR32(adapter, NETXEN_ROMUSB_GLB_CHIP_CLK_CTRL, 0x3fff);
749 NXWR32(adapter, NETXEN_ROMUSB_GLB_CAS_RST, 0);
750 }
751
752 return 0;
753}
754
755static int
756netxen_validate_firmware(struct netxen_adapter *adapter, const char *fwname)
757{
758 __le32 val;
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000759 u32 ver, min_ver, bios;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000760 struct pci_dev *pdev = adapter->pdev;
761 const struct firmware *fw = adapter->fw;
762
763 if (fw->size < NX_FW_MIN_SIZE)
764 return -EINVAL;
765
766 val = cpu_to_le32(*(u32 *)&fw->data[NX_FW_MAGIC_OFFSET]);
767 if ((__force u32)val != NETXEN_BDINFO_MAGIC)
768 return -EINVAL;
769
770 val = cpu_to_le32(*(u32 *)&fw->data[NX_FW_VERSION_OFFSET]);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000771
772 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
773 min_ver = NETXEN_VERSION_CODE(4, 0, 216);
774 else
775 min_ver = NETXEN_VERSION_CODE(3, 4, 216);
776
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000777 ver = NETXEN_DECODE_VERSION(val);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000778
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000779 if ((_major(ver) > _NETXEN_NIC_LINUX_MAJOR) || (ver < min_ver)) {
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000780 dev_err(&pdev->dev,
781 "%s: firmware version %d.%d.%d unsupported\n",
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000782 fwname, _major(ver), _minor(ver), _build(ver));
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000783 return -EINVAL;
784 }
785
786 val = cpu_to_le32(*(u32 *)&fw->data[NX_BIOS_VERSION_OFFSET]);
787 netxen_rom_fast_read(adapter, NX_BIOS_VERSION_OFFSET, (int *)&bios);
788 if ((__force u32)val != bios) {
789 dev_err(&pdev->dev, "%s: firmware bios is incompatible\n",
790 fwname);
791 return -EINVAL;
792 }
793
794 /* check if flashed firmware is newer */
795 if (netxen_rom_fast_read(adapter,
796 NX_FW_VERSION_OFFSET, (int *)&val))
797 return -EIO;
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000798 val = NETXEN_DECODE_VERSION(val);
799 if (val > ver) {
800 dev_info(&pdev->dev, "%s: firmware is older than flash\n",
801 fwname);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000802 return -EINVAL;
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000803 }
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000804
805 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
806 return 0;
807}
808
809static char *fw_name[] = { "nxromimg.bin", "nx3fwct.bin", "nx3fwmn.bin" };
810
811void netxen_request_firmware(struct netxen_adapter *adapter)
812{
813 u32 capability, flashed_ver;
814 int fw_type;
815 struct pci_dev *pdev = adapter->pdev;
816 int rc = 0;
817
818 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
819 fw_type = NX_P2_MN_ROMIMAGE;
820 goto request_fw;
821 } else {
822 fw_type = NX_P3_CT_ROMIMAGE;
823 goto request_fw;
824 }
825
826request_mn:
827 capability = 0;
828
829 netxen_rom_fast_read(adapter,
830 NX_FW_VERSION_OFFSET, (int *)&flashed_ver);
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000831 flashed_ver = NETXEN_DECODE_VERSION(flashed_ver);
832
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000833 if (flashed_ver >= NETXEN_VERSION_CODE(4, 0, 220)) {
834 capability = NXRD32(adapter, NX_PEG_TUNE_CAPABILITY);
835 if (capability & NX_PEG_TUNE_MN_PRESENT) {
836 fw_type = NX_P3_MN_ROMIMAGE;
837 goto request_fw;
838 }
839 }
840
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000841 adapter->fw = NULL;
842 goto done;
843
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000844request_fw:
845 rc = request_firmware(&adapter->fw, fw_name[fw_type], &pdev->dev);
846 if (rc != 0) {
847 if (fw_type == NX_P3_CT_ROMIMAGE) {
848 msleep(1);
849 goto request_mn;
850 }
851
852 adapter->fw = NULL;
853 goto done;
854 }
855
856 rc = netxen_validate_firmware(adapter, fw_name[fw_type]);
857 if (rc != 0) {
858 release_firmware(adapter->fw);
859
860 if (fw_type == NX_P3_CT_ROMIMAGE) {
861 msleep(1);
862 goto request_mn;
863 }
864
865 adapter->fw = NULL;
866 goto done;
867 }
868
869done:
870 if (adapter->fw)
871 dev_info(&pdev->dev, "loading firmware from file %s\n",
872 fw_name[fw_type]);
873 else
874 dev_info(&pdev->dev, "loading firmware from flash\n");
875}
876
877
878void
879netxen_release_firmware(struct netxen_adapter *adapter)
880{
881 if (adapter->fw)
882 release_firmware(adapter->fw);
883}
884
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800885int netxen_initialize_adapter_offload(struct netxen_adapter *adapter)
886{
887 uint64_t addr;
888 uint32_t hi;
889 uint32_t lo;
890
891 adapter->dummy_dma.addr =
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700892 pci_alloc_consistent(adapter->pdev,
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800893 NETXEN_HOST_DUMMY_DMA_SIZE,
894 &adapter->dummy_dma.phys_addr);
895 if (adapter->dummy_dma.addr == NULL) {
896 printk("%s: ERROR: Could not allocate dummy DMA memory\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700897 __func__);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800898 return -ENOMEM;
899 }
900
901 addr = (uint64_t) adapter->dummy_dma.phys_addr;
902 hi = (addr >> 32) & 0xffffffff;
903 lo = addr & 0xffffffff;
904
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000905 NXWR32(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI, hi);
906 NXWR32(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO, lo);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800907
Dhananjay Phadke29566402008-07-21 19:44:04 -0700908 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
909 uint32_t temp = 0;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000910 NXWR32(adapter, CRB_HOST_DUMMY_BUF, temp);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700911 }
912
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800913 return 0;
914}
915
916void netxen_free_adapter_offload(struct netxen_adapter *adapter)
917{
Dhananjay Phadke15eef1e2008-08-08 00:08:42 -0700918 int i = 100;
Dhananjay Phadke439b4542008-06-15 22:59:46 -0700919
Dhananjay Phadke15eef1e2008-08-08 00:08:42 -0700920 if (!adapter->dummy_dma.addr)
921 return;
922
923 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadke439b4542008-06-15 22:59:46 -0700924 do {
925 if (dma_watchdog_shutdown_request(adapter) == 1)
926 break;
927 msleep(50);
928 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
929 break;
930 } while (--i);
Dhananjay Phadke15eef1e2008-08-08 00:08:42 -0700931 }
Dhananjay Phadke439b4542008-06-15 22:59:46 -0700932
Dhananjay Phadke15eef1e2008-08-08 00:08:42 -0700933 if (i) {
934 pci_free_consistent(adapter->pdev,
935 NETXEN_HOST_DUMMY_DMA_SIZE,
936 adapter->dummy_dma.addr,
937 adapter->dummy_dma.phys_addr);
938 adapter->dummy_dma.addr = NULL;
939 } else {
940 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
941 adapter->netdev->name);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800942 }
943}
944
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530945int netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400946{
947 u32 val = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700948 int retries = 60;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400949
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +0000950 if (pegtune_val)
951 return 0;
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530952
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +0000953 do {
954 val = NXRD32(adapter, CRB_CMDPEG_STATE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530955
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +0000956 switch (val) {
957 case PHAN_INITIALIZE_COMPLETE:
958 case PHAN_INITIALIZE_ACK:
959 return 0;
960 case PHAN_INITIALIZE_FAILED:
961 goto out_err;
962 default:
963 break;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400964 }
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530965
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +0000966 msleep(500);
967
968 } while (--retries);
969
970 NXWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
971
972out_err:
973 dev_warn(&adapter->pdev->dev, "firmware init failed\n");
974 return -EIO;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400975}
976
Dhananjay Phadke56a00782009-04-07 22:50:41 +0000977static int
978netxen_receive_peg_ready(struct netxen_adapter *adapter)
Dhananjay Phadke29566402008-07-21 19:44:04 -0700979{
980 u32 val = 0;
981 int retries = 2000;
982
983 do {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000984 val = NXRD32(adapter, CRB_RCVPEG_STATE);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700985
986 if (val == PHAN_PEG_RCV_INITIALIZED)
987 return 0;
988
989 msleep(10);
990
991 } while (--retries);
992
993 if (!retries) {
994 printk(KERN_ERR "Receive Peg initialization not "
995 "complete, state: 0x%x.\n", val);
996 return -EIO;
997 }
998
999 return 0;
1000}
1001
Dhananjay Phadke56a00782009-04-07 22:50:41 +00001002int netxen_init_firmware(struct netxen_adapter *adapter)
1003{
1004 int err;
1005
1006 err = netxen_receive_peg_ready(adapter);
1007 if (err)
1008 return err;
1009
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001010 NXWR32(adapter, CRB_NIC_CAPABILITIES_HOST, INTR_SCHEME_PERPORT);
1011 NXWR32(adapter, CRB_NIC_MSI_MODE_HOST, MSI_MODE_MULTIFUNC);
1012 NXWR32(adapter, CRB_MPORT_MODE, MPORT_MULTI_FUNCTION_MODE);
1013 NXWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK);
Dhananjay Phadke56a00782009-04-07 22:50:41 +00001014
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001015 if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222)) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001016 adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001017 }
1018
Dhananjay Phadke56a00782009-04-07 22:50:41 +00001019 return err;
1020}
1021
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001022static void
1023netxen_handle_linkevent(struct netxen_adapter *adapter, nx_fw_msg_t *msg)
1024{
1025 u32 cable_OUI;
1026 u16 cable_len;
1027 u16 link_speed;
1028 u8 link_status, module, duplex, autoneg;
1029 struct net_device *netdev = adapter->netdev;
1030
1031 adapter->has_link_events = 1;
1032
1033 cable_OUI = msg->body[1] & 0xffffffff;
1034 cable_len = (msg->body[1] >> 32) & 0xffff;
1035 link_speed = (msg->body[1] >> 48) & 0xffff;
1036
1037 link_status = msg->body[2] & 0xff;
1038 duplex = (msg->body[2] >> 16) & 0xff;
1039 autoneg = (msg->body[2] >> 24) & 0xff;
1040
1041 module = (msg->body[2] >> 8) & 0xff;
1042 if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE) {
1043 printk(KERN_INFO "%s: unsupported cable: OUI 0x%x, length %d\n",
1044 netdev->name, cable_OUI, cable_len);
1045 } else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN) {
1046 printk(KERN_INFO "%s: unsupported cable length %d\n",
1047 netdev->name, cable_len);
1048 }
1049
1050 netxen_advert_link_change(adapter, link_status);
1051
1052 /* update link parameters */
1053 if (duplex == LINKEVENT_FULL_DUPLEX)
1054 adapter->link_duplex = DUPLEX_FULL;
1055 else
1056 adapter->link_duplex = DUPLEX_HALF;
1057 adapter->module_type = module;
1058 adapter->link_autoneg = autoneg;
1059 adapter->link_speed = link_speed;
1060}
1061
1062static void
1063netxen_handle_fw_message(int desc_cnt, int index,
1064 struct nx_host_sds_ring *sds_ring)
1065{
1066 nx_fw_msg_t msg;
1067 struct status_desc *desc;
1068 int i = 0, opcode;
1069
1070 while (desc_cnt > 0 && i < 8) {
1071 desc = &sds_ring->desc_head[index];
1072 msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
1073 msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
1074
1075 index = get_next_index(index, sds_ring->num_desc);
1076 desc_cnt--;
1077 }
1078
1079 opcode = netxen_get_nic_msg_opcode(msg.body[0]);
1080 switch (opcode) {
1081 case NX_NIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
1082 netxen_handle_linkevent(sds_ring->adapter, &msg);
1083 break;
1084 default:
1085 break;
1086 }
1087}
1088
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001089static int
1090netxen_alloc_rx_skb(struct netxen_adapter *adapter,
1091 struct nx_host_rds_ring *rds_ring,
1092 struct netxen_rx_buffer *buffer)
1093{
1094 struct sk_buff *skb;
1095 dma_addr_t dma;
1096 struct pci_dev *pdev = adapter->pdev;
1097
1098 buffer->skb = dev_alloc_skb(rds_ring->skb_size);
1099 if (!buffer->skb)
1100 return 1;
1101
1102 skb = buffer->skb;
1103
1104 if (!adapter->ahw.cut_through)
1105 skb_reserve(skb, 2);
1106
1107 dma = pci_map_single(pdev, skb->data,
1108 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1109
1110 if (pci_dma_mapping_error(pdev, dma)) {
1111 dev_kfree_skb_any(skb);
1112 buffer->skb = NULL;
1113 return 1;
1114 }
1115
1116 buffer->skb = skb;
1117 buffer->dma = dma;
1118 buffer->state = NETXEN_BUFFER_BUSY;
1119
1120 return 0;
1121}
1122
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001123static struct sk_buff *netxen_process_rxbuf(struct netxen_adapter *adapter,
1124 struct nx_host_rds_ring *rds_ring, u16 index, u16 cksum)
1125{
1126 struct netxen_rx_buffer *buffer;
1127 struct sk_buff *skb;
1128
1129 buffer = &rds_ring->rx_buf_arr[index];
1130
1131 pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1132 PCI_DMA_FROMDEVICE);
1133
1134 skb = buffer->skb;
1135 if (!skb)
1136 goto no_skb;
1137
1138 if (likely(adapter->rx_csum && cksum == STATUS_CKSUM_OK)) {
1139 adapter->stats.csummed++;
1140 skb->ip_summed = CHECKSUM_UNNECESSARY;
1141 } else
1142 skb->ip_summed = CHECKSUM_NONE;
1143
1144 skb->dev = adapter->netdev;
1145
1146 buffer->skb = NULL;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001147no_skb:
1148 buffer->state = NETXEN_BUFFER_FREE;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001149 return skb;
1150}
1151
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001152static struct netxen_rx_buffer *
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001153netxen_process_rcv(struct netxen_adapter *adapter,
Amit Kumar Salechaa92e9e62009-04-07 22:50:47 +00001154 int ring, int index, int length, int cksum, int pkt_offset,
1155 struct nx_host_sds_ring *sds_ring)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001156{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001157 struct net_device *netdev = adapter->netdev;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001158 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001159 struct netxen_rx_buffer *buffer;
1160 struct sk_buff *skb;
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001161 struct nx_host_rds_ring *rds_ring = &recv_ctx->rds_rings[ring];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001162
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001163 if (unlikely(index > rds_ring->num_desc))
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001164 return NULL;
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001165
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001166 buffer = &rds_ring->rx_buf_arr[index];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001167
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001168 skb = netxen_process_rxbuf(adapter, rds_ring, index, cksum);
1169 if (!skb)
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001170 return buffer;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001171
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001172 if (length > rds_ring->skb_size)
1173 skb_put(skb, rds_ring->skb_size);
1174 else
1175 skb_put(skb, length);
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001176
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001177
1178 if (pkt_offset)
1179 skb_pull(skb, pkt_offset);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001180
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001181 skb->protocol = eth_type_trans(skb, netdev);
1182
Amit Kumar Salechaa92e9e62009-04-07 22:50:47 +00001183 napi_gro_receive(&sds_ring->napi, skb);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001184
Dhananjay Phadke0ddc1102009-03-09 08:50:52 +00001185 adapter->stats.no_rcv++;
1186 adapter->stats.rxbytes += length;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001187
1188 return buffer;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001189}
1190
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001191#define netxen_merge_rx_buffers(list, head) \
1192 do { list_splice_tail_init(list, head); } while (0);
1193
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001194int
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001195netxen_process_rcv_ring(struct nx_host_sds_ring *sds_ring, int max)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001196{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001197 struct netxen_adapter *adapter = sds_ring->adapter;
1198
1199 struct list_head *cur;
1200
Dhananjay Phadke0ddc1102009-03-09 08:50:52 +00001201 struct status_desc *desc;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001202 struct netxen_rx_buffer *rxbuf;
1203
1204 u32 consumer = sds_ring->consumer;
1205
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001206 int count = 0;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001207 u64 sts_data;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001208 int opcode, ring, index, length, cksum, pkt_offset, desc_cnt;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001209
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001210 while (count < max) {
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001211 desc = &sds_ring->desc_head[consumer];
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001212 sts_data = le64_to_cpu(desc->status_desc_data[0]);
Dhananjay Phadke0ddc1102009-03-09 08:50:52 +00001213
1214 if (!(sts_data & STATUS_OWNER_HOST))
1215 break;
1216
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001217 desc_cnt = netxen_get_sts_desc_cnt(sts_data);
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001218 ring = netxen_get_sts_type(sts_data);
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001219
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001220 if (ring > RCV_RING_JUMBO)
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001221 goto skip;
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001222
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001223 opcode = netxen_get_sts_opcode(sts_data);
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001224
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001225 switch (opcode) {
1226 case NETXEN_NIC_RXPKT_DESC:
1227 case NETXEN_OLD_RXPKT_DESC:
1228 break;
1229 case NETXEN_NIC_RESPONSE_DESC:
1230 netxen_handle_fw_message(desc_cnt, consumer, sds_ring);
1231 default:
1232 goto skip;
1233 }
1234
1235 WARN_ON(desc_cnt > 1);
1236
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001237 index = netxen_get_sts_refhandle(sts_data);
1238 length = netxen_get_sts_totallength(sts_data);
1239 cksum = netxen_get_sts_status(sts_data);
1240 pkt_offset = netxen_get_sts_pkt_offset(sts_data);
1241
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001242 rxbuf = netxen_process_rcv(adapter, ring, index,
Amit Kumar Salechaa92e9e62009-04-07 22:50:47 +00001243 length, cksum, pkt_offset, sds_ring);
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001244
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001245 if (rxbuf)
1246 list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1247
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001248skip:
1249 for (; desc_cnt > 0; desc_cnt--) {
1250 desc = &sds_ring->desc_head[consumer];
1251 desc->status_desc_data[0] =
1252 cpu_to_le64(STATUS_OWNER_PHANTOM);
1253 consumer = get_next_index(consumer, sds_ring->num_desc);
1254 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001255 count++;
1256 }
Dhananjay Phadke0ddc1102009-03-09 08:50:52 +00001257
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001258 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1259 struct nx_host_rds_ring *rds_ring =
1260 &adapter->recv_ctx.rds_rings[ring];
1261
1262 if (!list_empty(&sds_ring->free_list[ring])) {
1263 list_for_each(cur, &sds_ring->free_list[ring]) {
1264 rxbuf = list_entry(cur,
1265 struct netxen_rx_buffer, list);
1266 netxen_alloc_rx_skb(adapter, rds_ring, rxbuf);
1267 }
1268 spin_lock(&rds_ring->lock);
1269 netxen_merge_rx_buffers(&sds_ring->free_list[ring],
1270 &rds_ring->free_list);
1271 spin_unlock(&rds_ring->lock);
1272 }
1273
1274 netxen_post_rx_buffers_nodb(adapter, rds_ring);
1275 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001276
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001277 if (count) {
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001278 sds_ring->consumer = consumer;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001279 NXWR32(adapter, sds_ring->crb_sts_consumer, consumer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001280 }
1281
1282 return count;
1283}
1284
1285/* Process Command status ring */
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001286int netxen_process_cmd_ring(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001287{
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001288 u32 sw_consumer, hw_consumer;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001289 int count = 0, i;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001290 struct netxen_cmd_buffer *buffer;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001291 struct pci_dev *pdev = adapter->pdev;
1292 struct net_device *netdev = adapter->netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001293 struct netxen_skb_frag *frag;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001294 int done = 0;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +00001295 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001296
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001297 if (!spin_trylock(&adapter->tx_clean_lock))
1298 return 1;
1299
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001300 sw_consumer = tx_ring->sw_consumer;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001301 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001302
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001303 while (sw_consumer != hw_consumer) {
1304 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001305 if (buffer->skb) {
1306 frag = &buffer->frag_array[0];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001307 pci_unmap_single(pdev, frag->dma, frag->length,
1308 PCI_DMA_TODEVICE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301309 frag->dma = 0ULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001310 for (i = 1; i < buffer->frag_count; i++) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001311 frag++; /* Get the next frag */
1312 pci_unmap_page(pdev, frag->dma, frag->length,
1313 PCI_DMA_TODEVICE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301314 frag->dma = 0ULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001315 }
1316
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001317 adapter->stats.xmitfinished++;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001318 dev_kfree_skb_any(buffer->skb);
1319 buffer->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001320 }
1321
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001322 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001323 if (++count >= MAX_STATUS_HANDLE)
1324 break;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001325 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001326
Dhananjay Phadke22527862009-05-05 19:05:06 +00001327 if (count && netif_running(netdev)) {
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001328 tx_ring->sw_consumer = sw_consumer;
1329
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001330 smp_mb();
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001331
Dhananjay Phadke22527862009-05-05 19:05:06 +00001332 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001333 netif_tx_lock(netdev);
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001334 if (netxen_tx_avail(tx_ring) > TX_STOP_THRESH)
1335 netif_wake_queue(netdev);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001336 netif_tx_unlock(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001337 }
1338 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001339 /*
1340 * If everything is freed up to consumer then check if the ring is full
1341 * If the ring is full then check if more needs to be freed and
1342 * schedule the call back again.
1343 *
1344 * This happens when there are 2 CPUs. One could be freeing and the
1345 * other filling it. If the ring is full when we get out of here and
1346 * the card has already interrupted the host then the host can miss the
1347 * interrupt.
1348 *
1349 * There is still a possible race condition and the host could miss an
1350 * interrupt. The card has to take care of this.
1351 */
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001352 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
1353 done = (sw_consumer == hw_consumer);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001354 spin_unlock(&adapter->tx_clean_lock);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001355
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001356 return (done);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001357}
1358
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001359void
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001360netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ringid,
1361 struct nx_host_rds_ring *rds_ring)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001362{
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001363 struct rcv_desc *pdesc;
1364 struct netxen_rx_buffer *buffer;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001365 int producer, count = 0;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001366 netxen_ctx_msg msg = 0;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001367 struct list_head *head;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001368
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001369 producer = rds_ring->producer;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001370
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001371 spin_lock(&rds_ring->lock);
1372 head = &rds_ring->free_list;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001373 while (!list_empty(head)) {
1374
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001375 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001376
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001377 if (!buffer->skb) {
1378 if (netxen_alloc_rx_skb(adapter, rds_ring, buffer))
1379 break;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001380 }
1381
1382 count++;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001383 list_del(&buffer->list);
1384
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001385 /* make a rcv descriptor */
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001386 pdesc = &rds_ring->desc_head[producer];
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001387 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001388 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001389 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001390
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001391 producer = get_next_index(producer, rds_ring->num_desc);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001392 }
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001393 spin_unlock(&rds_ring->lock);
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001394
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001395 if (count) {
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001396 rds_ring->producer = producer;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001397 NXWR32(adapter, rds_ring->crb_rcv_producer,
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001398 (producer-1) & (rds_ring->num_desc-1));
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001399
1400 if (adapter->fw_major < 4) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001401 /*
1402 * Write a doorbell msg to tell phanmon of change in
1403 * receive ring producer
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001404 * Only for firmware version < 4.0.0
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001405 */
1406 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1407 netxen_set_msg_privid(msg);
1408 netxen_set_msg_count(msg,
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001409 ((producer - 1) &
1410 (rds_ring->num_desc - 1)));
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001411 netxen_set_msg_ctxid(msg, adapter->portnum);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001412 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1413 writel(msg,
1414 DB_NORMALIZE(adapter,
1415 NETXEN_RCV_PRODUCER_OFFSET));
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001416 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001417 }
1418}
1419
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001420static void
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001421netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
1422 struct nx_host_rds_ring *rds_ring)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001423{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001424 struct rcv_desc *pdesc;
1425 struct netxen_rx_buffer *buffer;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001426 int producer, count = 0;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001427 struct list_head *head;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001428
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001429 producer = rds_ring->producer;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001430 if (!spin_trylock(&rds_ring->lock))
1431 return;
1432
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001433 head = &rds_ring->free_list;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001434 while (!list_empty(head)) {
1435
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001436 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001437
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001438 if (!buffer->skb) {
1439 if (netxen_alloc_rx_skb(adapter, rds_ring, buffer))
1440 break;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001441 }
1442
1443 count++;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001444 list_del(&buffer->list);
1445
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001446 /* make a rcv descriptor */
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001447 pdesc = &rds_ring->desc_head[producer];
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001448 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001449 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001450 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001451
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001452 producer = get_next_index(producer, rds_ring->num_desc);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001453 }
1454
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001455 if (count) {
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001456 rds_ring->producer = producer;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001457 NXWR32(adapter, rds_ring->crb_rcv_producer,
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001458 (producer - 1) & (rds_ring->num_desc - 1));
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001459 }
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001460 spin_unlock(&rds_ring->lock);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001461}
1462
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001463void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1464{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001465 memset(&adapter->stats, 0, sizeof(adapter->stats));
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001466 return;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001467}
1468