blob: a6b244a9f4a15ee9c322e72ff344d9e14846e435 [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 Phadkeb2af9cb2009-07-17 15:27:07 +0000217 tx_ring->txq = netdev_get_tx_queue(netdev, 0);
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000218
219 cmd_buf_arr = vmalloc(TX_BUFF_RINGSIZE(tx_ring));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700220 if (cmd_buf_arr == NULL) {
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000221 dev_err(&pdev->dev, "%s: failed to allocate cmd buffer ring\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -0700222 netdev->name);
223 return -ENOMEM;
224 }
Dhananjay Phadked877f1e2009-04-07 22:50:40 +0000225 memset(cmd_buf_arr, 0, TX_BUFF_RINGSIZE(tx_ring));
226 tx_ring->cmd_buf_arr = cmd_buf_arr;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700227
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000228 recv_ctx = &adapter->recv_ctx;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000229
230 size = adapter->max_rds_rings * sizeof (struct nx_host_rds_ring);
231 rds_ring = kzalloc(size, GFP_KERNEL);
232 if (rds_ring == NULL) {
233 dev_err(&pdev->dev, "%s: failed to allocate rds ring struct\n",
234 netdev->name);
235 return -ENOMEM;
236 }
237 recv_ctx->rds_rings = rds_ring;
238
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000239 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
240 rds_ring = &recv_ctx->rds_rings[ring];
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000241 switch (ring) {
242 case RCV_RING_NORMAL:
243 rds_ring->num_desc = adapter->num_rxd;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000244 if (adapter->ahw.cut_through) {
245 rds_ring->dma_size =
246 NX_CT_DEFAULT_RX_BUF_LEN;
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -0700247 rds_ring->skb_size =
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000248 NX_CT_DEFAULT_RX_BUF_LEN;
249 } else {
Dhananjay Phadke9b08beb2009-07-26 20:07:44 +0000250 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
251 rds_ring->dma_size =
252 NX_P3_RX_BUF_MAX_LEN;
253 else
254 rds_ring->dma_size =
255 NX_P2_RX_BUF_MAX_LEN;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000256 rds_ring->skb_size =
Dhananjay Phadke9b08beb2009-07-26 20:07:44 +0000257 rds_ring->dma_size + NET_IP_ALIGN;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000258 }
259 break;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700260
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000261 case RCV_RING_JUMBO:
262 rds_ring->num_desc = adapter->num_jumbo_rxd;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000263 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
264 rds_ring->dma_size =
265 NX_P3_RX_JUMBO_BUF_MAX_LEN;
266 else
267 rds_ring->dma_size =
268 NX_P2_RX_JUMBO_BUF_MAX_LEN;
269 rds_ring->skb_size =
270 rds_ring->dma_size + NET_IP_ALIGN;
271 break;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700272
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000273 case RCV_RING_LRO:
Dhananjay Phadke438627c2009-03-13 14:52:03 +0000274 rds_ring->num_desc = adapter->num_lro_rxd;
Dhananjay Phadke9b08beb2009-07-26 20:07:44 +0000275 rds_ring->dma_size = NX_RX_LRO_BUFFER_LENGTH;
276 rds_ring->skb_size = rds_ring->dma_size + NET_IP_ALIGN;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000277 break;
278
279 }
280 rds_ring->rx_buf_arr = (struct netxen_rx_buffer *)
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000281 vmalloc(RCV_BUFF_RINGSIZE(rds_ring));
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000282 if (rds_ring->rx_buf_arr == NULL) {
283 printk(KERN_ERR "%s: Failed to allocate "
284 "rx buffer ring %d\n",
285 netdev->name, ring);
286 /* free whatever was already allocated */
287 goto err_out;
288 }
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000289 memset(rds_ring->rx_buf_arr, 0, RCV_BUFF_RINGSIZE(rds_ring));
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000290 INIT_LIST_HEAD(&rds_ring->free_list);
291 /*
292 * Now go through all of them, set reference handles
293 * and put them in the queues.
294 */
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000295 rx_buf = rds_ring->rx_buf_arr;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +0000296 for (i = 0; i < rds_ring->num_desc; i++) {
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +0000297 list_add_tail(&rx_buf->list,
298 &rds_ring->free_list);
299 rx_buf->ref_handle = i;
300 rx_buf->state = NETXEN_BUFFER_FREE;
301 rx_buf++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400302 }
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000303 spin_lock_init(&rds_ring->lock);
304 }
305
306 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
307 sds_ring = &recv_ctx->sds_rings[ring];
308 sds_ring->irq = adapter->msix_entries[ring].vector;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +0000309 sds_ring->adapter = adapter;
310 sds_ring->num_desc = adapter->num_rxd;
311
312 for (i = 0; i < NUM_RCV_DESC_RINGS; i++)
313 INIT_LIST_HEAD(&sds_ring->free_list[i]);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400314 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700315
316 return 0;
317
318err_out:
319 netxen_free_sw_resources(adapter);
320 return -ENOMEM;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400321}
322
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400323void netxen_initialize_adapter_ops(struct netxen_adapter *adapter)
324{
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000325 adapter->macaddr_set = netxen_p2_nic_set_mac_addr;
326 adapter->set_multi = netxen_p2_nic_set_multi;
327
Dhananjay Phadke1e2d0052009-03-09 08:50:56 +0000328 switch (adapter->ahw.port_type) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400329 case NETXEN_NIC_GBE:
Amit S. Kale80922fb2006-12-04 09:18:00 -0800330 adapter->enable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400331 netxen_niu_gbe_enable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800332 adapter->disable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400333 netxen_niu_gbe_disable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800334 adapter->set_mtu = netxen_nic_set_mtu_gb;
335 adapter->set_promisc = netxen_niu_set_promiscuous_mode;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800336 adapter->phy_read = netxen_niu_gbe_phy_read;
337 adapter->phy_write = netxen_niu_gbe_phy_write;
Dhananjay Phadkec9fc8912008-07-21 19:44:07 -0700338 adapter->init_port = netxen_niu_gbe_init_port;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800339 adapter->stop_port = netxen_niu_disable_gbe_port;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400340 break;
341
342 case NETXEN_NIC_XGBE:
Amit S. Kale80922fb2006-12-04 09:18:00 -0800343 adapter->enable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400344 netxen_niu_xgbe_enable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800345 adapter->disable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400346 netxen_niu_xgbe_disable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800347 adapter->set_mtu = netxen_nic_set_mtu_xgb;
348 adapter->init_port = netxen_niu_xg_init_port;
349 adapter->set_promisc = netxen_niu_xg_set_promiscuous_mode;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800350 adapter->stop_port = netxen_niu_disable_xg_port;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400351 break;
352
353 default:
354 break;
355 }
Dhananjay Phadke9ad27642008-08-01 03:14:59 -0700356
357 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
358 adapter->set_mtu = nx_fw_cmd_set_mtu;
359 adapter->set_promisc = netxen_p3_nic_set_promisc;
Dhananjay Phadke3d0a3cc2009-05-05 19:05:08 +0000360 adapter->macaddr_set = netxen_p3_nic_set_mac_addr;
361 adapter->set_multi = netxen_p3_nic_set_multi;
Dhananjay Phadke9ad27642008-08-01 03:14:59 -0700362 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400363}
364
365/*
366 * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
367 * address to external PCI CRB address.
368 */
Adrian Bunk993fb902007-11-05 18:07:31 +0100369static u32 netxen_decode_crb_addr(u32 addr)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400370{
371 int i;
Linsys Contractor Mithlesh Thukrale0e20a12007-02-28 05:16:40 -0800372 u32 base_addr, offset, pci_base;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400373
374 crb_addr_transform_setup();
375
376 pci_base = NETXEN_ADDR_ERROR;
377 base_addr = addr & 0xfff00000;
378 offset = addr & 0x000fffff;
379
380 for (i = 0; i < NETXEN_MAX_CRB_XFORM; i++) {
381 if (crb_addr_xform[i] == base_addr) {
382 pci_base = i << 20;
383 break;
384 }
385 }
386 if (pci_base == NETXEN_ADDR_ERROR)
387 return pci_base;
388 else
389 return (pci_base + offset);
390}
391
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700392static long rom_max_timeout = 100;
393static long rom_lock_timeout = 10000;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400394
Adrian Bunk993fb902007-11-05 18:07:31 +0100395static int rom_lock(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400396{
397 int iter;
398 u32 done = 0;
399 int timeout = 0;
400
401 while (!done) {
402 /* acquire semaphore2 from PCI HW block */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000403 done = NXRD32(adapter, NETXEN_PCIE_REG(PCIE_SEM2_LOCK));
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400404 if (done == 1)
405 break;
406 if (timeout >= rom_lock_timeout)
407 return -EIO;
408
409 timeout++;
410 /*
411 * Yield CPU
412 */
413 if (!in_atomic())
414 schedule();
415 else {
416 for (iter = 0; iter < 20; iter++)
417 cpu_relax(); /*This a nop instr on i386 */
418 }
419 }
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000420 NXWR32(adapter, NETXEN_ROM_LOCK_ID, ROM_LOCK_DRIVER);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400421 return 0;
422}
423
Adrian Bunk993fb902007-11-05 18:07:31 +0100424static int netxen_wait_rom_done(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400425{
426 long timeout = 0;
427 long done = 0;
428
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800429 cond_resched();
430
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400431 while (done == 0) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000432 done = NXRD32(adapter, NETXEN_ROMUSB_GLB_STATUS);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400433 done &= 2;
434 timeout++;
435 if (timeout >= rom_max_timeout) {
436 printk("Timeout reached waiting for rom done");
437 return -EIO;
438 }
439 }
440 return 0;
441}
442
Adrian Bunk993fb902007-11-05 18:07:31 +0100443static void netxen_rom_unlock(struct netxen_adapter *adapter)
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800444{
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800445 /* release semaphore2 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000446 NXRD32(adapter, NETXEN_PCIE_REG(PCIE_SEM2_UNLOCK));
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800447
448}
449
Adrian Bunk993fb902007-11-05 18:07:31 +0100450static int do_rom_fast_read(struct netxen_adapter *adapter,
451 int addr, int *valp)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400452{
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000453 NXWR32(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
454 NXWR32(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
455 NXWR32(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
456 NXWR32(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0xb);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400457 if (netxen_wait_rom_done(adapter)) {
458 printk("Error waiting for rom done\n");
459 return -EIO;
460 }
461 /* reset abyte_cnt and dummy_byte_cnt */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000462 NXWR32(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800463 udelay(10);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000464 NXWR32(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400465
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000466 *valp = NXRD32(adapter, NETXEN_ROMUSB_ROM_RDATA);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400467 return 0;
468}
469
Adrian Bunk993fb902007-11-05 18:07:31 +0100470static int do_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
471 u8 *bytes, size_t size)
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800472{
473 int addridx;
474 int ret = 0;
475
476 for (addridx = addr; addridx < (addr + size); addridx += 4) {
Al Virof305f782007-12-22 19:44:00 +0000477 int v;
478 ret = do_rom_fast_read(adapter, addridx, &v);
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800479 if (ret != 0)
480 break;
Al Virof305f782007-12-22 19:44:00 +0000481 *(__le32 *)bytes = cpu_to_le32(v);
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800482 bytes += 4;
483 }
484
485 return ret;
486}
487
488int
Jeff Garzik47906542007-11-23 21:23:36 -0500489netxen_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800490 u8 *bytes, size_t size)
491{
492 int ret;
493
494 ret = rom_lock(adapter);
495 if (ret < 0)
496 return ret;
497
498 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
499
500 netxen_rom_unlock(adapter);
501 return ret;
502}
503
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400504int netxen_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
505{
506 int ret;
507
508 if (rom_lock(adapter) != 0)
509 return -EIO;
510
511 ret = do_rom_fast_read(adapter, addr, valp);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800512 netxen_rom_unlock(adapter);
513 return ret;
514}
515
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400516#define NETXEN_BOARDTYPE 0x4008
517#define NETXEN_BOARDNUM 0x400c
518#define NETXEN_CHIPNUM 0x4010
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400519
520int netxen_pinit_from_rom(struct netxen_adapter *adapter, int verbose)
521{
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700522 int addr, val;
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800523 int i, n, init_delay = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400524 struct crb_addr_pair *buf;
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800525 unsigned offset;
Linsys Contractor Mithlesh Thukrale0e20a12007-02-28 05:16:40 -0800526 u32 off;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400527
528 /* resetall */
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800529 rom_lock(adapter);
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000530 NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, 0xffffffff);
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800531 netxen_rom_unlock(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400532
533 if (verbose) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400534 if (netxen_rom_fast_read(adapter, NETXEN_BOARDTYPE, &val) == 0)
535 printk("P2 ROM board type: 0x%08x\n", val);
536 else
537 printk("Could not read board type\n");
538 if (netxen_rom_fast_read(adapter, NETXEN_BOARDNUM, &val) == 0)
539 printk("P2 ROM board num: 0x%08x\n", val);
540 else
541 printk("Could not read board number\n");
542 if (netxen_rom_fast_read(adapter, NETXEN_CHIPNUM, &val) == 0)
543 printk("P2 ROM chip num: 0x%08x\n", val);
544 else
545 printk("Could not read chip number\n");
546 }
547
Dhananjay Phadke29566402008-07-21 19:44:04 -0700548 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
549 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800550 (n != 0xcafecafe) ||
Dhananjay Phadke29566402008-07-21 19:44:04 -0700551 netxen_rom_fast_read(adapter, 4, &n) != 0) {
552 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
553 "n: %08x\n", netxen_nic_driver_name, n);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400554 return -EIO;
555 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700556 offset = n & 0xffffU;
557 n = (n >> 16) & 0xffffU;
558 } else {
559 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
560 !(n & 0x80000000)) {
561 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
562 "n: %08x\n", netxen_nic_driver_name, n);
563 return -EIO;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400564 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700565 offset = 1;
566 n &= ~0x80000000;
567 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400568
Dhananjay Phadke29566402008-07-21 19:44:04 -0700569 if (n < 1024) {
570 if (verbose)
571 printk(KERN_DEBUG "%s: %d CRB init values found"
572 " in ROM.\n", netxen_nic_driver_name, n);
573 } else {
574 printk(KERN_ERR "%s:n=0x%x Error! NetXen card flash not"
575 " initialized.\n", __func__, n);
576 return -EIO;
577 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400578
Dhananjay Phadke29566402008-07-21 19:44:04 -0700579 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
580 if (buf == NULL) {
581 printk("%s: netxen_pinit_from_rom: Unable to calloc memory.\n",
582 netxen_nic_driver_name);
583 return -ENOMEM;
584 }
585 for (i = 0; i < n; i++) {
586 if (netxen_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
Daniel Marjamäki584dbe92009-01-29 08:55:56 +0000587 netxen_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0) {
588 kfree(buf);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700589 return -EIO;
Daniel Marjamäki584dbe92009-01-29 08:55:56 +0000590 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400591
Dhananjay Phadke29566402008-07-21 19:44:04 -0700592 buf[i].addr = addr;
593 buf[i].data = val;
594
595 if (verbose)
596 printk(KERN_DEBUG "%s: PCI: 0x%08x == 0x%08x\n",
597 netxen_nic_driver_name,
598 (u32)netxen_decode_crb_addr(addr), val);
599 }
600 for (i = 0; i < n; i++) {
601
602 off = netxen_decode_crb_addr(buf[i].addr);
603 if (off == NETXEN_ADDR_ERROR) {
604 printk(KERN_ERR"CRB init value out of range %x\n",
Amit S. Kale1fcca1a2007-02-05 07:35:26 -0800605 buf[i].addr);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700606 continue;
607 }
608 off += NETXEN_PCI_CRBSPACE;
609 /* skipping cold reboot MAGIC */
610 if (off == NETXEN_CAM_RAM(0x1fc))
611 continue;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400612
Dhananjay Phadke29566402008-07-21 19:44:04 -0700613 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
614 /* do not reset PCI */
615 if (off == (ROMUSB_GLB + 0xbc))
616 continue;
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800617 if (off == (ROMUSB_GLB + 0xa8))
618 continue;
619 if (off == (ROMUSB_GLB + 0xc8)) /* core clock */
620 continue;
621 if (off == (ROMUSB_GLB + 0x24)) /* MN clock */
622 continue;
623 if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */
624 continue;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700625 if (off == (NETXEN_CRB_PEG_NET_1 + 0x18))
626 buf[i].data = 0x1020;
627 /* skip the function enable register */
628 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION))
629 continue;
630 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION2))
631 continue;
632 if ((off & 0x0ff00000) == NETXEN_CRB_SMB)
633 continue;
634 }
635
636 if (off == NETXEN_ADDR_ERROR) {
637 printk(KERN_ERR "%s: Err: Unknown addr: 0x%08x\n",
638 netxen_nic_driver_name, buf[i].addr);
639 continue;
640 }
641
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800642 init_delay = 1;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700643 /* After writing this register, HW needs time for CRB */
644 /* to quiet down (else crb_window returns 0xffffffff) */
645 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800646 init_delay = 1000;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700647 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400648 /* hold xdma in reset also */
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800649 buf[i].data = NETXEN_NIC_XDMA_RESET;
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800650 buf[i].data = 0x8000ff;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400651 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400652 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400653
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000654 NXWR32(adapter, off, buf[i].data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400655
Dhananjay Phadke27c915a2009-01-14 20:49:00 -0800656 msleep(init_delay);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400657 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700658 kfree(buf);
659
660 /* disable_peg_cache_all */
661
662 /* unreset_net_cache */
663 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000664 val = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
665 NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, (val & 0xffffff0f));
Dhananjay Phadke29566402008-07-21 19:44:04 -0700666 }
667
668 /* p2dn replyCount */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000669 NXWR32(adapter, NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700670 /* disable_peg_cache 0 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000671 NXWR32(adapter, NETXEN_CRB_PEG_NET_D + 0x4c, 8);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700672 /* disable_peg_cache 1 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000673 NXWR32(adapter, NETXEN_CRB_PEG_NET_I + 0x4c, 8);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700674
675 /* peg_clr_all */
676
677 /* peg_clr 0 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000678 NXWR32(adapter, NETXEN_CRB_PEG_NET_0 + 0x8, 0);
679 NXWR32(adapter, NETXEN_CRB_PEG_NET_0 + 0xc, 0);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700680 /* peg_clr 1 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000681 NXWR32(adapter, NETXEN_CRB_PEG_NET_1 + 0x8, 0);
682 NXWR32(adapter, NETXEN_CRB_PEG_NET_1 + 0xc, 0);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700683 /* peg_clr 2 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000684 NXWR32(adapter, NETXEN_CRB_PEG_NET_2 + 0x8, 0);
685 NXWR32(adapter, NETXEN_CRB_PEG_NET_2 + 0xc, 0);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700686 /* peg_clr 3 */
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000687 NXWR32(adapter, NETXEN_CRB_PEG_NET_3 + 0x8, 0);
688 NXWR32(adapter, NETXEN_CRB_PEG_NET_3 + 0xc, 0);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400689 return 0;
690}
691
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000692int
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000693netxen_need_fw_reset(struct netxen_adapter *adapter)
694{
695 u32 count, old_count;
696 u32 val, version, major, minor, build;
697 int i, timeout;
698 u8 fw_type;
699
700 /* NX2031 firmware doesn't support heartbit */
701 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
702 return 1;
703
704 /* last attempt had failed */
705 if (NXRD32(adapter, CRB_CMDPEG_STATE) == PHAN_INITIALIZE_FAILED)
706 return 1;
707
708 old_count = count = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
709
710 for (i = 0; i < 10; i++) {
711
712 timeout = msleep_interruptible(200);
713 if (timeout) {
714 NXWR32(adapter, CRB_CMDPEG_STATE,
715 PHAN_INITIALIZE_FAILED);
716 return -EINTR;
717 }
718
719 count = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
720 if (count != old_count)
721 break;
722 }
723
724 /* firmware is dead */
725 if (count == old_count)
726 return 1;
727
728 /* check if we have got newer or different file firmware */
729 if (adapter->fw) {
730
731 const struct firmware *fw = adapter->fw;
732
733 val = cpu_to_le32(*(u32 *)&fw->data[NX_FW_VERSION_OFFSET]);
734 version = NETXEN_DECODE_VERSION(val);
735
736 major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
737 minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
738 build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
739
740 if (version > NETXEN_VERSION_CODE(major, minor, build))
741 return 1;
742
743 if (version == NETXEN_VERSION_CODE(major, minor, build)) {
744
745 val = NXRD32(adapter, NETXEN_MIU_MN_CONTROL);
746 fw_type = (val & 0x4) ?
747 NX_P3_CT_ROMIMAGE : NX_P3_MN_ROMIMAGE;
748
749 if (adapter->fw_type != fw_type)
750 return 1;
751 }
752 }
753
754 return 0;
755}
756
757static char *fw_name[] = {
758 "nxromimg.bin", "nx3fwct.bin", "nx3fwmn.bin", "flash",
759};
760
761int
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000762netxen_load_firmware(struct netxen_adapter *adapter)
763{
764 u64 *ptr64;
765 u32 i, flashaddr, size;
766 const struct firmware *fw = adapter->fw;
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000767 struct pci_dev *pdev = adapter->pdev;
768
769 dev_info(&pdev->dev, "loading firmware from %s\n",
770 fw_name[adapter->fw_type]);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000771
772 if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
773 NXWR32(adapter, NETXEN_ROMUSB_GLB_CAS_RST, 1);
774
775 if (fw) {
776 __le64 data;
777
778 size = (NETXEN_IMAGE_START - NETXEN_BOOTLD_START) / 8;
779
780 ptr64 = (u64 *)&fw->data[NETXEN_BOOTLD_START];
781 flashaddr = NETXEN_BOOTLD_START;
782
783 for (i = 0; i < size; i++) {
784 data = cpu_to_le64(ptr64[i]);
785 adapter->pci_mem_write(adapter, flashaddr, &data, 8);
786 flashaddr += 8;
787 }
788
789 size = *(u32 *)&fw->data[NX_FW_SIZE_OFFSET];
790 size = (__force u32)cpu_to_le32(size) / 8;
791
792 ptr64 = (u64 *)&fw->data[NETXEN_IMAGE_START];
793 flashaddr = NETXEN_IMAGE_START;
794
795 for (i = 0; i < size; i++) {
796 data = cpu_to_le64(ptr64[i]);
797
798 if (adapter->pci_mem_write(adapter,
799 flashaddr, &data, 8))
800 return -EIO;
801
802 flashaddr += 8;
803 }
804 } else {
805 u32 data;
806
807 size = (NETXEN_IMAGE_START - NETXEN_BOOTLD_START) / 4;
808 flashaddr = NETXEN_BOOTLD_START;
809
810 for (i = 0; i < size; i++) {
811 if (netxen_rom_fast_read(adapter,
812 flashaddr, (int *)&data) != 0)
813 return -EIO;
814
815 if (adapter->pci_mem_write(adapter,
816 flashaddr, &data, 4))
817 return -EIO;
818
819 flashaddr += 4;
820 }
821 }
822 msleep(1);
823
824 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
825 NXWR32(adapter, NETXEN_ROMUSB_GLB_SW_RESET, 0x80001d);
826 else {
827 NXWR32(adapter, NETXEN_ROMUSB_GLB_CHIP_CLK_CTRL, 0x3fff);
828 NXWR32(adapter, NETXEN_ROMUSB_GLB_CAS_RST, 0);
829 }
830
831 return 0;
832}
833
834static int
835netxen_validate_firmware(struct netxen_adapter *adapter, const char *fwname)
836{
837 __le32 val;
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000838 u32 ver, min_ver, bios;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000839 struct pci_dev *pdev = adapter->pdev;
840 const struct firmware *fw = adapter->fw;
841
842 if (fw->size < NX_FW_MIN_SIZE)
843 return -EINVAL;
844
845 val = cpu_to_le32(*(u32 *)&fw->data[NX_FW_MAGIC_OFFSET]);
846 if ((__force u32)val != NETXEN_BDINFO_MAGIC)
847 return -EINVAL;
848
849 val = cpu_to_le32(*(u32 *)&fw->data[NX_FW_VERSION_OFFSET]);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000850
851 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
852 min_ver = NETXEN_VERSION_CODE(4, 0, 216);
853 else
854 min_ver = NETXEN_VERSION_CODE(3, 4, 216);
855
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000856 ver = NETXEN_DECODE_VERSION(val);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000857
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000858 if ((_major(ver) > _NETXEN_NIC_LINUX_MAJOR) || (ver < min_ver)) {
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000859 dev_err(&pdev->dev,
860 "%s: firmware version %d.%d.%d unsupported\n",
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000861 fwname, _major(ver), _minor(ver), _build(ver));
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000862 return -EINVAL;
863 }
864
865 val = cpu_to_le32(*(u32 *)&fw->data[NX_BIOS_VERSION_OFFSET]);
866 netxen_rom_fast_read(adapter, NX_BIOS_VERSION_OFFSET, (int *)&bios);
867 if ((__force u32)val != bios) {
868 dev_err(&pdev->dev, "%s: firmware bios is incompatible\n",
869 fwname);
870 return -EINVAL;
871 }
872
873 /* check if flashed firmware is newer */
874 if (netxen_rom_fast_read(adapter,
875 NX_FW_VERSION_OFFSET, (int *)&val))
876 return -EIO;
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000877 val = NETXEN_DECODE_VERSION(val);
878 if (val > ver) {
879 dev_info(&pdev->dev, "%s: firmware is older than flash\n",
880 fwname);
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000881 return -EINVAL;
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000882 }
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000883
884 NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
885 return 0;
886}
887
Dhananjay Phadke6598b162009-07-26 20:07:37 +0000888static int
889netxen_p3_has_mn(struct netxen_adapter *adapter)
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000890{
891 u32 capability, flashed_ver;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000892 capability = 0;
893
894 netxen_rom_fast_read(adapter,
895 NX_FW_VERSION_OFFSET, (int *)&flashed_ver);
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000896 flashed_ver = NETXEN_DECODE_VERSION(flashed_ver);
897
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000898 if (flashed_ver >= NETXEN_VERSION_CODE(4, 0, 220)) {
Dhananjay Phadke6598b162009-07-26 20:07:37 +0000899
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000900 capability = NXRD32(adapter, NX_PEG_TUNE_CAPABILITY);
Dhananjay Phadke6598b162009-07-26 20:07:37 +0000901 if (capability & NX_PEG_TUNE_MN_PRESENT)
902 return 1;
903 }
904 return 0;
905}
906
907void netxen_request_firmware(struct netxen_adapter *adapter)
908{
909 u8 fw_type;
910 struct pci_dev *pdev = adapter->pdev;
911 int rc = 0;
912
913 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
914 fw_type = NX_P2_MN_ROMIMAGE;
915 goto request_fw;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000916 }
917
Dhananjay Phadke6598b162009-07-26 20:07:37 +0000918 fw_type = netxen_p3_has_mn(adapter) ?
919 NX_P3_MN_ROMIMAGE : NX_P3_CT_ROMIMAGE;
Dhananjay Phadke98e31bb2009-07-01 11:41:42 +0000920
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000921request_fw:
922 rc = request_firmware(&adapter->fw, fw_name[fw_type], &pdev->dev);
923 if (rc != 0) {
Dhananjay Phadke6598b162009-07-26 20:07:37 +0000924 if (fw_type == NX_P3_MN_ROMIMAGE) {
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000925 msleep(1);
Dhananjay Phadke6598b162009-07-26 20:07:37 +0000926 fw_type = NX_P3_CT_ROMIMAGE;
927 goto request_fw;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000928 }
929
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000930 fw_type = NX_FLASH_ROMIMAGE;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000931 adapter->fw = NULL;
932 goto done;
933 }
934
935 rc = netxen_validate_firmware(adapter, fw_name[fw_type]);
936 if (rc != 0) {
937 release_firmware(adapter->fw);
938
Dhananjay Phadke6598b162009-07-26 20:07:37 +0000939 if (fw_type == NX_P3_MN_ROMIMAGE) {
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000940 msleep(1);
Dhananjay Phadke6598b162009-07-26 20:07:37 +0000941 fw_type = NX_P3_CT_ROMIMAGE;
942 goto request_fw;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000943 }
944
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000945 fw_type = NX_FLASH_ROMIMAGE;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000946 adapter->fw = NULL;
947 goto done;
948 }
949
950done:
Dhananjay Phadke67c38fc2009-07-01 11:41:43 +0000951 adapter->fw_type = fw_type;
Dhananjay Phadkef7185c72009-04-28 15:29:11 +0000952}
953
954
955void
956netxen_release_firmware(struct netxen_adapter *adapter)
957{
958 if (adapter->fw)
959 release_firmware(adapter->fw);
960}
961
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000962int netxen_init_dummy_dma(struct netxen_adapter *adapter)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800963{
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000964 u64 addr;
965 u32 hi, lo;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800966
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000967 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
968 return 0;
969
970 adapter->dummy_dma.addr = pci_alloc_consistent(adapter->pdev,
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800971 NETXEN_HOST_DUMMY_DMA_SIZE,
972 &adapter->dummy_dma.phys_addr);
973 if (adapter->dummy_dma.addr == NULL) {
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000974 dev_err(&adapter->pdev->dev,
975 "ERROR: Could not allocate dummy DMA memory\n");
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800976 return -ENOMEM;
977 }
978
979 addr = (uint64_t) adapter->dummy_dma.phys_addr;
980 hi = (addr >> 32) & 0xffffffff;
981 lo = addr & 0xffffffff;
982
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +0000983 NXWR32(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI, hi);
984 NXWR32(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO, lo);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800985
986 return 0;
987}
988
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +0000989/*
990 * NetXen DMA watchdog control:
991 *
992 * Bit 0 : enabled => R/O: 1 watchdog active, 0 inactive
993 * Bit 1 : disable_request => 1 req disable dma watchdog
994 * Bit 2 : enable_request => 1 req enable dma watchdog
995 * Bit 3-31 : unused
996 */
997void netxen_free_dummy_dma(struct netxen_adapter *adapter)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800998{
Dhananjay Phadke15eef1e2008-08-08 00:08:42 -0700999 int i = 100;
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001000 u32 ctrl;
1001
1002 if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
1003 return;
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001004
Dhananjay Phadke15eef1e2008-08-08 00:08:42 -07001005 if (!adapter->dummy_dma.addr)
1006 return;
1007
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001008 ctrl = NXRD32(adapter, NETXEN_DMA_WATCHDOG_CTRL);
1009 if ((ctrl & 0x1) != 0) {
1010 NXWR32(adapter, NETXEN_DMA_WATCHDOG_CTRL, (ctrl | 0x2));
1011
1012 while ((ctrl & 0x1) != 0) {
1013
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001014 msleep(50);
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001015
1016 ctrl = NXRD32(adapter, NETXEN_DMA_WATCHDOG_CTRL);
1017
1018 if (--i == 0)
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001019 break;
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001020 };
Dhananjay Phadke15eef1e2008-08-08 00:08:42 -07001021 }
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001022
Dhananjay Phadke15eef1e2008-08-08 00:08:42 -07001023 if (i) {
1024 pci_free_consistent(adapter->pdev,
1025 NETXEN_HOST_DUMMY_DMA_SIZE,
1026 adapter->dummy_dma.addr,
1027 adapter->dummy_dma.phys_addr);
1028 adapter->dummy_dma.addr = NULL;
Dhananjay Phadke83ac51f2009-07-26 20:07:39 +00001029 } else
1030 dev_err(&adapter->pdev->dev, "dma_watchdog_shutdown failed\n");
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001031}
1032
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301033int netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001034{
1035 u32 val = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001036 int retries = 60;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001037
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +00001038 if (pegtune_val)
1039 return 0;
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301040
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +00001041 do {
1042 val = NXRD32(adapter, CRB_CMDPEG_STATE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301043
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +00001044 switch (val) {
1045 case PHAN_INITIALIZE_COMPLETE:
1046 case PHAN_INITIALIZE_ACK:
1047 return 0;
1048 case PHAN_INITIALIZE_FAILED:
1049 goto out_err;
1050 default:
1051 break;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001052 }
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301053
Dhananjay Phadke96f2ebd2009-06-22 20:26:21 +00001054 msleep(500);
1055
1056 } while (--retries);
1057
1058 NXWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
1059
1060out_err:
1061 dev_warn(&adapter->pdev->dev, "firmware init failed\n");
1062 return -EIO;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001063}
1064
Dhananjay Phadke56a00782009-04-07 22:50:41 +00001065static int
1066netxen_receive_peg_ready(struct netxen_adapter *adapter)
Dhananjay Phadke29566402008-07-21 19:44:04 -07001067{
1068 u32 val = 0;
1069 int retries = 2000;
1070
1071 do {
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001072 val = NXRD32(adapter, CRB_RCVPEG_STATE);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001073
1074 if (val == PHAN_PEG_RCV_INITIALIZED)
1075 return 0;
1076
1077 msleep(10);
1078
1079 } while (--retries);
1080
1081 if (!retries) {
1082 printk(KERN_ERR "Receive Peg initialization not "
1083 "complete, state: 0x%x.\n", val);
1084 return -EIO;
1085 }
1086
1087 return 0;
1088}
1089
Dhananjay Phadke56a00782009-04-07 22:50:41 +00001090int netxen_init_firmware(struct netxen_adapter *adapter)
1091{
1092 int err;
1093
1094 err = netxen_receive_peg_ready(adapter);
1095 if (err)
1096 return err;
1097
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001098 NXWR32(adapter, CRB_NIC_CAPABILITIES_HOST, INTR_SCHEME_PERPORT);
1099 NXWR32(adapter, CRB_NIC_MSI_MODE_HOST, MSI_MODE_MULTIFUNC);
1100 NXWR32(adapter, CRB_MPORT_MODE, MPORT_MULTI_FUNCTION_MODE);
1101 NXWR32(adapter, CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK);
Dhananjay Phadke56a00782009-04-07 22:50:41 +00001102
1103 return err;
1104}
1105
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001106static void
1107netxen_handle_linkevent(struct netxen_adapter *adapter, nx_fw_msg_t *msg)
1108{
1109 u32 cable_OUI;
1110 u16 cable_len;
1111 u16 link_speed;
1112 u8 link_status, module, duplex, autoneg;
1113 struct net_device *netdev = adapter->netdev;
1114
1115 adapter->has_link_events = 1;
1116
1117 cable_OUI = msg->body[1] & 0xffffffff;
1118 cable_len = (msg->body[1] >> 32) & 0xffff;
1119 link_speed = (msg->body[1] >> 48) & 0xffff;
1120
1121 link_status = msg->body[2] & 0xff;
1122 duplex = (msg->body[2] >> 16) & 0xff;
1123 autoneg = (msg->body[2] >> 24) & 0xff;
1124
1125 module = (msg->body[2] >> 8) & 0xff;
1126 if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE) {
1127 printk(KERN_INFO "%s: unsupported cable: OUI 0x%x, length %d\n",
1128 netdev->name, cable_OUI, cable_len);
1129 } else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN) {
1130 printk(KERN_INFO "%s: unsupported cable length %d\n",
1131 netdev->name, cable_len);
1132 }
1133
1134 netxen_advert_link_change(adapter, link_status);
1135
1136 /* update link parameters */
1137 if (duplex == LINKEVENT_FULL_DUPLEX)
1138 adapter->link_duplex = DUPLEX_FULL;
1139 else
1140 adapter->link_duplex = DUPLEX_HALF;
1141 adapter->module_type = module;
1142 adapter->link_autoneg = autoneg;
1143 adapter->link_speed = link_speed;
1144}
1145
1146static void
1147netxen_handle_fw_message(int desc_cnt, int index,
1148 struct nx_host_sds_ring *sds_ring)
1149{
1150 nx_fw_msg_t msg;
1151 struct status_desc *desc;
1152 int i = 0, opcode;
1153
1154 while (desc_cnt > 0 && i < 8) {
1155 desc = &sds_ring->desc_head[index];
1156 msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
1157 msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
1158
1159 index = get_next_index(index, sds_ring->num_desc);
1160 desc_cnt--;
1161 }
1162
1163 opcode = netxen_get_nic_msg_opcode(msg.body[0]);
1164 switch (opcode) {
1165 case NX_NIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
1166 netxen_handle_linkevent(sds_ring->adapter, &msg);
1167 break;
1168 default:
1169 break;
1170 }
1171}
1172
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001173static int
1174netxen_alloc_rx_skb(struct netxen_adapter *adapter,
1175 struct nx_host_rds_ring *rds_ring,
1176 struct netxen_rx_buffer *buffer)
1177{
1178 struct sk_buff *skb;
1179 dma_addr_t dma;
1180 struct pci_dev *pdev = adapter->pdev;
1181
1182 buffer->skb = dev_alloc_skb(rds_ring->skb_size);
1183 if (!buffer->skb)
1184 return 1;
1185
1186 skb = buffer->skb;
1187
1188 if (!adapter->ahw.cut_through)
1189 skb_reserve(skb, 2);
1190
1191 dma = pci_map_single(pdev, skb->data,
1192 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1193
1194 if (pci_dma_mapping_error(pdev, dma)) {
1195 dev_kfree_skb_any(skb);
1196 buffer->skb = NULL;
1197 return 1;
1198 }
1199
1200 buffer->skb = skb;
1201 buffer->dma = dma;
1202 buffer->state = NETXEN_BUFFER_BUSY;
1203
1204 return 0;
1205}
1206
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001207static struct sk_buff *netxen_process_rxbuf(struct netxen_adapter *adapter,
1208 struct nx_host_rds_ring *rds_ring, u16 index, u16 cksum)
1209{
1210 struct netxen_rx_buffer *buffer;
1211 struct sk_buff *skb;
1212
1213 buffer = &rds_ring->rx_buf_arr[index];
1214
1215 pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1216 PCI_DMA_FROMDEVICE);
1217
1218 skb = buffer->skb;
1219 if (!skb)
1220 goto no_skb;
1221
1222 if (likely(adapter->rx_csum && cksum == STATUS_CKSUM_OK)) {
1223 adapter->stats.csummed++;
1224 skb->ip_summed = CHECKSUM_UNNECESSARY;
1225 } else
1226 skb->ip_summed = CHECKSUM_NONE;
1227
1228 skb->dev = adapter->netdev;
1229
1230 buffer->skb = NULL;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001231no_skb:
1232 buffer->state = NETXEN_BUFFER_FREE;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001233 return skb;
1234}
1235
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001236static struct netxen_rx_buffer *
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001237netxen_process_rcv(struct netxen_adapter *adapter,
Amit Kumar Salechaa92e9e62009-04-07 22:50:47 +00001238 int ring, int index, int length, int cksum, int pkt_offset,
1239 struct nx_host_sds_ring *sds_ring)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001240{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001241 struct net_device *netdev = adapter->netdev;
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001242 struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001243 struct netxen_rx_buffer *buffer;
1244 struct sk_buff *skb;
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001245 struct nx_host_rds_ring *rds_ring = &recv_ctx->rds_rings[ring];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001246
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001247 if (unlikely(index > rds_ring->num_desc))
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001248 return NULL;
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001249
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001250 buffer = &rds_ring->rx_buf_arr[index];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001251
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001252 skb = netxen_process_rxbuf(adapter, rds_ring, index, cksum);
1253 if (!skb)
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001254 return buffer;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001255
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001256 if (length > rds_ring->skb_size)
1257 skb_put(skb, rds_ring->skb_size);
1258 else
1259 skb_put(skb, length);
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001260
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001261
1262 if (pkt_offset)
1263 skb_pull(skb, pkt_offset);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001264
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001265 skb->protocol = eth_type_trans(skb, netdev);
1266
Amit Kumar Salechaa92e9e62009-04-07 22:50:47 +00001267 napi_gro_receive(&sds_ring->napi, skb);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001268
Dhananjay Phadke0ddc1102009-03-09 08:50:52 +00001269 adapter->stats.no_rcv++;
1270 adapter->stats.rxbytes += length;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001271
1272 return buffer;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001273}
1274
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001275#define netxen_merge_rx_buffers(list, head) \
1276 do { list_splice_tail_init(list, head); } while (0);
1277
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001278int
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001279netxen_process_rcv_ring(struct nx_host_sds_ring *sds_ring, int max)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001280{
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001281 struct netxen_adapter *adapter = sds_ring->adapter;
1282
1283 struct list_head *cur;
1284
Dhananjay Phadke0ddc1102009-03-09 08:50:52 +00001285 struct status_desc *desc;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001286 struct netxen_rx_buffer *rxbuf;
1287
1288 u32 consumer = sds_ring->consumer;
1289
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001290 int count = 0;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001291 u64 sts_data;
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001292 int opcode, ring, index, length, cksum, pkt_offset, desc_cnt;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001293
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001294 while (count < max) {
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001295 desc = &sds_ring->desc_head[consumer];
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001296 sts_data = le64_to_cpu(desc->status_desc_data[0]);
Dhananjay Phadke0ddc1102009-03-09 08:50:52 +00001297
1298 if (!(sts_data & STATUS_OWNER_HOST))
1299 break;
1300
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001301 desc_cnt = netxen_get_sts_desc_cnt(sts_data);
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001302 ring = netxen_get_sts_type(sts_data);
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001303
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001304 if (ring > RCV_RING_JUMBO)
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001305 goto skip;
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001306
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001307 opcode = netxen_get_sts_opcode(sts_data);
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001308
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001309 switch (opcode) {
1310 case NETXEN_NIC_RXPKT_DESC:
1311 case NETXEN_OLD_RXPKT_DESC:
Dhananjay Phadke6598b162009-07-26 20:07:37 +00001312 case NETXEN_NIC_SYN_OFFLOAD:
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001313 break;
1314 case NETXEN_NIC_RESPONSE_DESC:
1315 netxen_handle_fw_message(desc_cnt, consumer, sds_ring);
1316 default:
1317 goto skip;
1318 }
1319
1320 WARN_ON(desc_cnt > 1);
1321
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001322 index = netxen_get_sts_refhandle(sts_data);
1323 length = netxen_get_sts_totallength(sts_data);
1324 cksum = netxen_get_sts_status(sts_data);
1325 pkt_offset = netxen_get_sts_pkt_offset(sts_data);
1326
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001327 rxbuf = netxen_process_rcv(adapter, ring, index,
Amit Kumar Salechaa92e9e62009-04-07 22:50:47 +00001328 length, cksum, pkt_offset, sds_ring);
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001329
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001330 if (rxbuf)
1331 list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1332
Dhananjay Phadke3bf26ce2009-04-07 22:50:42 +00001333skip:
1334 for (; desc_cnt > 0; desc_cnt--) {
1335 desc = &sds_ring->desc_head[consumer];
1336 desc->status_desc_data[0] =
1337 cpu_to_le64(STATUS_OWNER_PHANTOM);
1338 consumer = get_next_index(consumer, sds_ring->num_desc);
1339 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001340 count++;
1341 }
Dhananjay Phadke0ddc1102009-03-09 08:50:52 +00001342
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001343 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1344 struct nx_host_rds_ring *rds_ring =
1345 &adapter->recv_ctx.rds_rings[ring];
1346
1347 if (!list_empty(&sds_ring->free_list[ring])) {
1348 list_for_each(cur, &sds_ring->free_list[ring]) {
1349 rxbuf = list_entry(cur,
1350 struct netxen_rx_buffer, list);
1351 netxen_alloc_rx_skb(adapter, rds_ring, rxbuf);
1352 }
1353 spin_lock(&rds_ring->lock);
1354 netxen_merge_rx_buffers(&sds_ring->free_list[ring],
1355 &rds_ring->free_list);
1356 spin_unlock(&rds_ring->lock);
1357 }
1358
1359 netxen_post_rx_buffers_nodb(adapter, rds_ring);
1360 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001361
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001362 if (count) {
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001363 sds_ring->consumer = consumer;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001364 NXWR32(adapter, sds_ring->crb_sts_consumer, consumer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001365 }
1366
1367 return count;
1368}
1369
1370/* Process Command status ring */
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001371int netxen_process_cmd_ring(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001372{
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001373 u32 sw_consumer, hw_consumer;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001374 int count = 0, i;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001375 struct netxen_cmd_buffer *buffer;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001376 struct pci_dev *pdev = adapter->pdev;
1377 struct net_device *netdev = adapter->netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001378 struct netxen_skb_frag *frag;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001379 int done = 0;
Dhananjay Phadke4ea528a2009-04-28 15:29:10 +00001380 struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001381
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001382 if (!spin_trylock(&adapter->tx_clean_lock))
1383 return 1;
1384
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001385 sw_consumer = tx_ring->sw_consumer;
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001386 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001387
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001388 while (sw_consumer != hw_consumer) {
1389 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001390 if (buffer->skb) {
1391 frag = &buffer->frag_array[0];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001392 pci_unmap_single(pdev, frag->dma, frag->length,
1393 PCI_DMA_TODEVICE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301394 frag->dma = 0ULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001395 for (i = 1; i < buffer->frag_count; i++) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001396 frag++; /* Get the next frag */
1397 pci_unmap_page(pdev, frag->dma, frag->length,
1398 PCI_DMA_TODEVICE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301399 frag->dma = 0ULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001400 }
1401
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001402 adapter->stats.xmitfinished++;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001403 dev_kfree_skb_any(buffer->skb);
1404 buffer->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001405 }
1406
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001407 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001408 if (++count >= MAX_STATUS_HANDLE)
1409 break;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001410 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001411
Dhananjay Phadke22527862009-05-05 19:05:06 +00001412 if (count && netif_running(netdev)) {
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001413 tx_ring->sw_consumer = sw_consumer;
1414
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001415 smp_mb();
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001416
Dhananjay Phadke22527862009-05-05 19:05:06 +00001417 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001418 __netif_tx_lock(tx_ring->txq, smp_processor_id());
Dhananjay Phadkecb2107b2009-06-17 17:27:25 +00001419 if (netxen_tx_avail(tx_ring) > TX_STOP_THRESH)
1420 netif_wake_queue(netdev);
Dhananjay Phadkeb2af9cb2009-07-17 15:27:07 +00001421 __netif_tx_unlock(tx_ring->txq);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001422 }
1423 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001424 /*
1425 * If everything is freed up to consumer then check if the ring is full
1426 * If the ring is full then check if more needs to be freed and
1427 * schedule the call back again.
1428 *
1429 * This happens when there are 2 CPUs. One could be freeing and the
1430 * other filling it. If the ring is full when we get out of here and
1431 * the card has already interrupted the host then the host can miss the
1432 * interrupt.
1433 *
1434 * There is still a possible race condition and the host could miss an
1435 * interrupt. The card has to take care of this.
1436 */
Dhananjay Phadked877f1e2009-04-07 22:50:40 +00001437 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
1438 done = (sw_consumer == hw_consumer);
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001439 spin_unlock(&adapter->tx_clean_lock);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001440
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001441 return (done);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001442}
1443
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001444void
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001445netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ringid,
1446 struct nx_host_rds_ring *rds_ring)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001447{
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001448 struct rcv_desc *pdesc;
1449 struct netxen_rx_buffer *buffer;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001450 int producer, count = 0;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001451 netxen_ctx_msg msg = 0;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001452 struct list_head *head;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001453
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001454 producer = rds_ring->producer;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001455
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001456 spin_lock(&rds_ring->lock);
1457 head = &rds_ring->free_list;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001458 while (!list_empty(head)) {
1459
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001460 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001461
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001462 if (!buffer->skb) {
1463 if (netxen_alloc_rx_skb(adapter, rds_ring, buffer))
1464 break;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001465 }
1466
1467 count++;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001468 list_del(&buffer->list);
1469
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001470 /* make a rcv descriptor */
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001471 pdesc = &rds_ring->desc_head[producer];
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001472 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001473 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001474 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001475
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001476 producer = get_next_index(producer, rds_ring->num_desc);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001477 }
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001478 spin_unlock(&rds_ring->lock);
Dhananjay Phadke9b3ef552009-03-13 14:52:04 +00001479
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001480 if (count) {
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001481 rds_ring->producer = producer;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001482 NXWR32(adapter, rds_ring->crb_rcv_producer,
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001483 (producer-1) & (rds_ring->num_desc-1));
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001484
Dhananjay Phadke4f96b982009-07-26 20:07:42 +00001485 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001486 /*
1487 * Write a doorbell msg to tell phanmon of change in
1488 * receive ring producer
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001489 * Only for firmware version < 4.0.0
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001490 */
1491 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1492 netxen_set_msg_privid(msg);
1493 netxen_set_msg_count(msg,
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001494 ((producer - 1) &
1495 (rds_ring->num_desc - 1)));
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001496 netxen_set_msg_ctxid(msg, adapter->portnum);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001497 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1498 writel(msg,
1499 DB_NORMALIZE(adapter,
1500 NETXEN_RCV_PRODUCER_OFFSET));
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001501 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001502 }
1503}
1504
Dhananjay Phadkebecf46a2009-03-09 08:50:55 +00001505static void
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001506netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
1507 struct nx_host_rds_ring *rds_ring)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001508{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001509 struct rcv_desc *pdesc;
1510 struct netxen_rx_buffer *buffer;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001511 int producer, count = 0;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001512 struct list_head *head;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001513
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001514 producer = rds_ring->producer;
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001515 if (!spin_trylock(&rds_ring->lock))
1516 return;
1517
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001518 head = &rds_ring->free_list;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001519 while (!list_empty(head)) {
1520
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001521 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001522
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001523 if (!buffer->skb) {
1524 if (netxen_alloc_rx_skb(adapter, rds_ring, buffer))
1525 break;
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001526 }
1527
1528 count++;
Dhananjay Phadked9e651b2008-07-21 19:44:08 -07001529 list_del(&buffer->list);
1530
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001531 /* make a rcv descriptor */
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001532 pdesc = &rds_ring->desc_head[producer];
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001533 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001534 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001535 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
Dhananjay Phadke6f703402009-01-14 20:50:00 -08001536
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001537 producer = get_next_index(producer, rds_ring->num_desc);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001538 }
1539
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001540 if (count) {
Dhananjay Phadke48bfd1e2008-07-21 19:44:06 -07001541 rds_ring->producer = producer;
Dhananjay Phadkef98a9f62009-04-07 22:50:45 +00001542 NXWR32(adapter, rds_ring->crb_rcv_producer,
Dhananjay Phadke438627c2009-03-13 14:52:03 +00001543 (producer - 1) & (rds_ring->num_desc - 1));
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001544 }
Dhananjay Phadked8b100c2009-03-13 14:52:05 +00001545 spin_unlock(&rds_ring->lock);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001546}
1547
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001548void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1549{
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001550 memset(&adapter->stats, 0, sizeof(adapter->stats));
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001551 return;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001552}
1553