blob: c6e2bc71d8f33e89bed81a7eedac22bf672b0dc0 [file] [log] [blame]
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001/*
2 * Copyright (C) 2003 - 2006 NetXen, Inc.
3 * 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
25 * NetXen,
26 * 3965 Freedom Circle, Fourth floor,
27 * Santa Clara, CA 95054
28 *
29 *
30 * Source file for NIC routines to initialize the Phantom Hardware
31 *
32 */
33
34#include <linux/netdevice.h>
35#include <linux/delay.h>
36#include "netxen_nic.h"
37#include "netxen_nic_hw.h"
Amit S. Kale3d396eb2006-10-21 15:33:03 -040038#include "netxen_nic_phan_reg.h"
39
40struct crb_addr_pair {
Linsys Contractor Mithlesh Thukrale0e20a12007-02-28 05:16:40 -080041 u32 addr;
42 u32 data;
Amit S. Kale3d396eb2006-10-21 15:33:03 -040043};
44
45#define NETXEN_MAX_CRB_XFORM 60
46static unsigned int crb_addr_xform[NETXEN_MAX_CRB_XFORM];
Linsys Contractor Mithlesh Thukrale0e20a12007-02-28 05:16:40 -080047#define NETXEN_ADDR_ERROR (0xffffffff)
Amit S. Kale3d396eb2006-10-21 15:33:03 -040048
49#define crb_addr_transform(name) \
50 crb_addr_xform[NETXEN_HW_PX_MAP_CRB_##name] = \
51 NETXEN_HW_CRB_HUB_AGT_ADR_##name << 20
52
Amit S. Kalecb8011a2006-11-29 09:00:10 -080053#define NETXEN_NIC_XDMA_RESET 0x8000ff
54
Adrian Bunk993fb902007-11-05 18:07:31 +010055static void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
56 uint32_t ctx, uint32_t ringid);
57
58#if 0
59static void netxen_nic_locked_write_reg(struct netxen_adapter *adapter,
60 unsigned long off, int *data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -040061{
Amit S. Kalecb8011a2006-11-29 09:00:10 -080062 void __iomem *addr = pci_base_offset(adapter, off);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040063 writel(*data, addr);
64}
Adrian Bunk993fb902007-11-05 18:07:31 +010065#endif /* 0 */
Amit S. Kale3d396eb2006-10-21 15:33:03 -040066
67static void crb_addr_transform_setup(void)
68{
69 crb_addr_transform(XDMA);
70 crb_addr_transform(TIMR);
71 crb_addr_transform(SRE);
72 crb_addr_transform(SQN3);
73 crb_addr_transform(SQN2);
74 crb_addr_transform(SQN1);
75 crb_addr_transform(SQN0);
76 crb_addr_transform(SQS3);
77 crb_addr_transform(SQS2);
78 crb_addr_transform(SQS1);
79 crb_addr_transform(SQS0);
80 crb_addr_transform(RPMX7);
81 crb_addr_transform(RPMX6);
82 crb_addr_transform(RPMX5);
83 crb_addr_transform(RPMX4);
84 crb_addr_transform(RPMX3);
85 crb_addr_transform(RPMX2);
86 crb_addr_transform(RPMX1);
87 crb_addr_transform(RPMX0);
88 crb_addr_transform(ROMUSB);
89 crb_addr_transform(SN);
90 crb_addr_transform(QMN);
91 crb_addr_transform(QMS);
92 crb_addr_transform(PGNI);
93 crb_addr_transform(PGND);
94 crb_addr_transform(PGN3);
95 crb_addr_transform(PGN2);
96 crb_addr_transform(PGN1);
97 crb_addr_transform(PGN0);
98 crb_addr_transform(PGSI);
99 crb_addr_transform(PGSD);
100 crb_addr_transform(PGS3);
101 crb_addr_transform(PGS2);
102 crb_addr_transform(PGS1);
103 crb_addr_transform(PGS0);
104 crb_addr_transform(PS);
105 crb_addr_transform(PH);
106 crb_addr_transform(NIU);
107 crb_addr_transform(I2Q);
108 crb_addr_transform(EG);
109 crb_addr_transform(MN);
110 crb_addr_transform(MS);
111 crb_addr_transform(CAS2);
112 crb_addr_transform(CAS1);
113 crb_addr_transform(CAS0);
114 crb_addr_transform(CAM);
115 crb_addr_transform(C2C1);
116 crb_addr_transform(C2C0);
Amit S. Kale1fcca1a2007-02-05 07:35:26 -0800117 crb_addr_transform(SMB);
Dhananjay Phadkee4c93c82008-07-21 19:44:02 -0700118 crb_addr_transform(OCM0);
119 crb_addr_transform(I2C0);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400120}
121
122int netxen_init_firmware(struct netxen_adapter *adapter)
123{
124 u32 state = 0, loops = 0, err = 0;
125
126 /* Window 1 call */
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700127 state = adapter->pci_read_normalize(adapter, CRB_CMDPEG_STATE);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400128
129 if (state == PHAN_INITIALIZE_ACK)
130 return 0;
131
132 while (state != PHAN_INITIALIZE_COMPLETE && loops < 2000) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700133 msleep(1);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400134 /* Window 1 call */
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700135 state = adapter->pci_read_normalize(adapter, CRB_CMDPEG_STATE);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400136
137 loops++;
138 }
139 if (loops >= 2000) {
140 printk(KERN_ERR "Cmd Peg initialization not complete:%x.\n",
141 state);
142 err = -EIO;
143 return err;
144 }
145 /* Window 1 call */
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -0700146 adapter->pci_write_normalize(adapter,
147 CRB_NIC_CAPABILITIES_HOST, INTR_SCHEME_PERPORT);
148 adapter->pci_write_normalize(adapter,
149 CRB_NIC_MSI_MODE_HOST, MSI_MODE_MULTIFUNC);
150 adapter->pci_write_normalize(adapter,
151 CRB_MPORT_MODE, MPORT_MULTI_FUNCTION_MODE);
152 adapter->pci_write_normalize(adapter,
153 CRB_CMDPEG_STATE, PHAN_INITIALIZE_ACK);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400154
155 return err;
156}
157
Dhananjay Phadke29566402008-07-21 19:44:04 -0700158void netxen_release_rx_buffers(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400159{
Dhananjay Phadke29566402008-07-21 19:44:04 -0700160 struct netxen_recv_context *recv_ctx;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400161 struct netxen_rcv_desc_ctx *rcv_desc;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700162 struct netxen_rx_buffer *rx_buf;
163 int i, ctxid, ring;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400164
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400165 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700166 recv_ctx = &adapter->recv_ctx[ctxid];
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400167 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
Dhananjay Phadke29566402008-07-21 19:44:04 -0700168 rcv_desc = &recv_ctx->rcv_desc[ring];
169 for (i = 0; i < rcv_desc->max_rx_desc_count; ++i) {
170 rx_buf = &(rcv_desc->rx_buf_arr[i]);
171 if (rx_buf->state == NETXEN_BUFFER_FREE)
172 continue;
173 pci_unmap_single(adapter->pdev,
174 rx_buf->dma,
175 rcv_desc->dma_size,
176 PCI_DMA_FROMDEVICE);
177 if (rx_buf->skb != NULL)
178 dev_kfree_skb_any(rx_buf->skb);
179 }
180 }
181 }
182}
183
184void netxen_release_tx_buffers(struct netxen_adapter *adapter)
185{
186 struct netxen_cmd_buffer *cmd_buf;
187 struct netxen_skb_frag *buffrag;
188 int i, j;
189
190 cmd_buf = adapter->cmd_buf_arr;
191 for (i = 0; i < adapter->max_tx_desc_count; i++) {
192 buffrag = cmd_buf->frag_array;
193 if (buffrag->dma) {
194 pci_unmap_single(adapter->pdev, buffrag->dma,
195 buffrag->length, PCI_DMA_TODEVICE);
196 buffrag->dma = 0ULL;
197 }
198 for (j = 0; j < cmd_buf->frag_count; j++) {
199 buffrag++;
200 if (buffrag->dma) {
201 pci_unmap_page(adapter->pdev, buffrag->dma,
202 buffrag->length,
203 PCI_DMA_TODEVICE);
204 buffrag->dma = 0ULL;
205 }
206 }
207 /* Free the skb we received in netxen_nic_xmit_frame */
208 if (cmd_buf->skb) {
209 dev_kfree_skb_any(cmd_buf->skb);
210 cmd_buf->skb = NULL;
211 }
212 cmd_buf++;
213 }
214}
215
216void netxen_free_sw_resources(struct netxen_adapter *adapter)
217{
218 struct netxen_recv_context *recv_ctx;
219 struct netxen_rcv_desc_ctx *rcv_desc;
220 int ctx, ring;
221
222 for (ctx = 0; ctx < MAX_RCV_CTX; ctx++) {
223 recv_ctx = &adapter->recv_ctx[ctx];
224 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
225 rcv_desc = &recv_ctx->rcv_desc[ring];
226 if (rcv_desc->rx_buf_arr) {
227 vfree(rcv_desc->rx_buf_arr);
228 rcv_desc->rx_buf_arr = NULL;
229 }
230 }
231 }
232 if (adapter->cmd_buf_arr)
233 vfree(adapter->cmd_buf_arr);
234 return;
235}
236
237int netxen_alloc_sw_resources(struct netxen_adapter *adapter)
238{
239 struct netxen_recv_context *recv_ctx;
240 struct netxen_rcv_desc_ctx *rcv_desc;
241 struct netxen_rx_buffer *rx_buf;
242 int ctx, ring, i, num_rx_bufs;
243
244 struct netxen_cmd_buffer *cmd_buf_arr;
245 struct net_device *netdev = adapter->netdev;
246
247 cmd_buf_arr = (struct netxen_cmd_buffer *)vmalloc(TX_RINGSIZE);
248 if (cmd_buf_arr == NULL) {
249 printk(KERN_ERR "%s: Failed to allocate cmd buffer ring\n",
250 netdev->name);
251 return -ENOMEM;
252 }
253 memset(cmd_buf_arr, 0, TX_RINGSIZE);
254 adapter->cmd_buf_arr = cmd_buf_arr;
255
256 for (ctx = 0; ctx < MAX_RCV_CTX; ctx++) {
257 recv_ctx = &adapter->recv_ctx[ctx];
258 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
259 rcv_desc = &recv_ctx->rcv_desc[ring];
260 switch (RCV_DESC_TYPE(ring)) {
261 case RCV_DESC_NORMAL:
262 rcv_desc->max_rx_desc_count =
263 adapter->max_rx_desc_count;
264 rcv_desc->flags = RCV_DESC_NORMAL;
265 rcv_desc->dma_size = RX_DMA_MAP_LEN;
266 rcv_desc->skb_size = MAX_RX_BUFFER_LENGTH;
267 break;
268
269 case RCV_DESC_JUMBO:
270 rcv_desc->max_rx_desc_count =
271 adapter->max_jumbo_rx_desc_count;
272 rcv_desc->flags = RCV_DESC_JUMBO;
273 rcv_desc->dma_size = RX_JUMBO_DMA_MAP_LEN;
274 rcv_desc->skb_size =
275 MAX_RX_JUMBO_BUFFER_LENGTH;
276 break;
277
278 case RCV_RING_LRO:
279 rcv_desc->max_rx_desc_count =
280 adapter->max_lro_rx_desc_count;
281 rcv_desc->flags = RCV_DESC_LRO;
282 rcv_desc->dma_size = RX_LRO_DMA_MAP_LEN;
283 rcv_desc->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
284 break;
285
286 }
287 rcv_desc->rx_buf_arr = (struct netxen_rx_buffer *)
288 vmalloc(RCV_BUFFSIZE);
289 if (rcv_desc->rx_buf_arr == NULL) {
290 printk(KERN_ERR "%s: Failed to allocate "
291 "rx buffer ring %d\n",
292 netdev->name, ring);
293 /* free whatever was already allocated */
294 goto err_out;
295 }
296 memset(rcv_desc->rx_buf_arr, 0, RCV_BUFFSIZE);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400297 rcv_desc->begin_alloc = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400298 /*
299 * Now go through all of them, set reference handles
300 * and put them in the queues.
301 */
Dhananjay Phadke29566402008-07-21 19:44:04 -0700302 num_rx_bufs = rcv_desc->max_rx_desc_count;
303 rx_buf = rcv_desc->rx_buf_arr;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400304 for (i = 0; i < num_rx_bufs; i++) {
305 rx_buf->ref_handle = i;
306 rx_buf->state = NETXEN_BUFFER_FREE;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400307 rx_buf++;
308 }
309 }
310 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700311
312 return 0;
313
314err_out:
315 netxen_free_sw_resources(adapter);
316 return -ENOMEM;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400317}
318
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400319void netxen_initialize_adapter_ops(struct netxen_adapter *adapter)
320{
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400321 switch (adapter->ahw.board_type) {
322 case NETXEN_NIC_GBE:
Amit S. Kale80922fb2006-12-04 09:18:00 -0800323 adapter->enable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400324 netxen_niu_gbe_enable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800325 adapter->disable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400326 netxen_niu_gbe_disable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800327 adapter->handle_phy_intr = netxen_nic_gbe_handle_phy_intr;
328 adapter->macaddr_set = netxen_niu_macaddr_set;
329 adapter->set_mtu = netxen_nic_set_mtu_gb;
330 adapter->set_promisc = netxen_niu_set_promiscuous_mode;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800331 adapter->phy_read = netxen_niu_gbe_phy_read;
332 adapter->phy_write = netxen_niu_gbe_phy_write;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800333 adapter->init_niu = netxen_nic_init_niu_gb;
334 adapter->stop_port = netxen_niu_disable_gbe_port;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400335 break;
336
337 case NETXEN_NIC_XGBE:
Amit S. Kale80922fb2006-12-04 09:18:00 -0800338 adapter->enable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400339 netxen_niu_xgbe_enable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800340 adapter->disable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400341 netxen_niu_xgbe_disable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800342 adapter->handle_phy_intr = netxen_nic_xgbe_handle_phy_intr;
343 adapter->macaddr_set = netxen_niu_xg_macaddr_set;
344 adapter->set_mtu = netxen_nic_set_mtu_xgb;
345 adapter->init_port = netxen_niu_xg_init_port;
346 adapter->set_promisc = netxen_niu_xg_set_promiscuous_mode;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800347 adapter->stop_port = netxen_niu_disable_xg_port;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400348 break;
349
350 default:
351 break;
352 }
353}
354
355/*
356 * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
357 * address to external PCI CRB address.
358 */
Adrian Bunk993fb902007-11-05 18:07:31 +0100359static u32 netxen_decode_crb_addr(u32 addr)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400360{
361 int i;
Linsys Contractor Mithlesh Thukrale0e20a12007-02-28 05:16:40 -0800362 u32 base_addr, offset, pci_base;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400363
364 crb_addr_transform_setup();
365
366 pci_base = NETXEN_ADDR_ERROR;
367 base_addr = addr & 0xfff00000;
368 offset = addr & 0x000fffff;
369
370 for (i = 0; i < NETXEN_MAX_CRB_XFORM; i++) {
371 if (crb_addr_xform[i] == base_addr) {
372 pci_base = i << 20;
373 break;
374 }
375 }
376 if (pci_base == NETXEN_ADDR_ERROR)
377 return pci_base;
378 else
379 return (pci_base + offset);
380}
381
Mithlesh Thukral13ba9c72007-04-20 07:53:05 -0700382static long rom_max_timeout = 100;
383static long rom_lock_timeout = 10000;
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700384#if 0
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800385static long rom_write_timeout = 700;
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700386#endif
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400387
Adrian Bunk993fb902007-11-05 18:07:31 +0100388static int rom_lock(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400389{
390 int iter;
391 u32 done = 0;
392 int timeout = 0;
393
394 while (!done) {
395 /* acquire semaphore2 from PCI HW block */
396 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_LOCK),
397 &done);
398 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 }
414 netxen_nic_reg_write(adapter, NETXEN_ROM_LOCK_ID, ROM_LOCK_DRIVER);
415 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
423 while (done == 0) {
424 done = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_GLB_STATUS);
425 done &= 2;
426 timeout++;
427 if (timeout >= rom_max_timeout) {
428 printk("Timeout reached waiting for rom done");
429 return -EIO;
430 }
431 }
432 return 0;
433}
434
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700435#if 0
Adrian Bunk993fb902007-11-05 18:07:31 +0100436static int netxen_rom_wren(struct netxen_adapter *adapter)
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800437{
438 /* Set write enable latch in ROM status register */
439 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
440 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
441 M25P_INSTR_WREN);
442 if (netxen_wait_rom_done(adapter)) {
443 return -1;
444 }
445 return 0;
446}
447
Adrian Bunk993fb902007-11-05 18:07:31 +0100448static unsigned int netxen_rdcrbreg(struct netxen_adapter *adapter,
449 unsigned int addr)
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800450{
451 unsigned int data = 0xdeaddead;
452 data = netxen_nic_reg_read(adapter, addr);
453 return data;
454}
455
Adrian Bunk993fb902007-11-05 18:07:31 +0100456static int netxen_do_rom_rdsr(struct netxen_adapter *adapter)
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800457{
458 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
459 M25P_INSTR_RDSR);
460 if (netxen_wait_rom_done(adapter)) {
461 return -1;
462 }
463 return netxen_rdcrbreg(adapter, NETXEN_ROMUSB_ROM_RDATA);
464}
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700465#endif
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800466
Adrian Bunk993fb902007-11-05 18:07:31 +0100467static void netxen_rom_unlock(struct netxen_adapter *adapter)
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800468{
469 u32 val;
470
471 /* release semaphore2 */
472 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_UNLOCK), &val);
473
474}
475
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700476#if 0
Adrian Bunk993fb902007-11-05 18:07:31 +0100477static int netxen_rom_wip_poll(struct netxen_adapter *adapter)
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800478{
479 long timeout = 0;
480 long wip = 1;
481 int val;
482 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
483 while (wip != 0) {
484 val = netxen_do_rom_rdsr(adapter);
485 wip = val & 1;
486 timeout++;
487 if (timeout > rom_max_timeout) {
488 return -1;
489 }
490 }
491 return 0;
492}
493
Adrian Bunk993fb902007-11-05 18:07:31 +0100494static int do_rom_fast_write(struct netxen_adapter *adapter, int addr,
495 int data)
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800496{
497 if (netxen_rom_wren(adapter)) {
498 return -1;
499 }
500 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
501 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
502 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
503 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
504 M25P_INSTR_PP);
505 if (netxen_wait_rom_done(adapter)) {
506 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
507 return -1;
508 }
509
510 return netxen_rom_wip_poll(adapter);
511}
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700512#endif
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800513
Adrian Bunk993fb902007-11-05 18:07:31 +0100514static int do_rom_fast_read(struct netxen_adapter *adapter,
515 int addr, int *valp)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400516{
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +0530517 cond_resched();
Linsys Contractor Mithlesh Thukralb58ecad2007-03-13 04:15:06 -0800518
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400519 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
520 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
Linsys Contractor Mithlesh Thukralb58ecad2007-03-13 04:15:06 -0800521 udelay(100); /* prevent bursting on CRB */
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400522 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
523 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0xb);
524 if (netxen_wait_rom_done(adapter)) {
525 printk("Error waiting for rom done\n");
526 return -EIO;
527 }
528 /* reset abyte_cnt and dummy_byte_cnt */
529 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
Linsys Contractor Mithlesh Thukralb58ecad2007-03-13 04:15:06 -0800530 udelay(100); /* prevent bursting on CRB */
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400531 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
532
533 *valp = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_ROM_RDATA);
534 return 0;
535}
536
Adrian Bunk993fb902007-11-05 18:07:31 +0100537static int do_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
538 u8 *bytes, size_t size)
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800539{
540 int addridx;
541 int ret = 0;
542
543 for (addridx = addr; addridx < (addr + size); addridx += 4) {
Al Virof305f782007-12-22 19:44:00 +0000544 int v;
545 ret = do_rom_fast_read(adapter, addridx, &v);
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800546 if (ret != 0)
547 break;
Al Virof305f782007-12-22 19:44:00 +0000548 *(__le32 *)bytes = cpu_to_le32(v);
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800549 bytes += 4;
550 }
551
552 return ret;
553}
554
555int
Jeff Garzik47906542007-11-23 21:23:36 -0500556netxen_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800557 u8 *bytes, size_t size)
558{
559 int ret;
560
561 ret = rom_lock(adapter);
562 if (ret < 0)
563 return ret;
564
565 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
566
567 netxen_rom_unlock(adapter);
568 return ret;
569}
570
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400571int netxen_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
572{
573 int ret;
574
575 if (rom_lock(adapter) != 0)
576 return -EIO;
577
578 ret = do_rom_fast_read(adapter, addr, valp);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800579 netxen_rom_unlock(adapter);
580 return ret;
581}
582
Adrian Bunk993fb902007-11-05 18:07:31 +0100583#if 0
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800584int netxen_rom_fast_write(struct netxen_adapter *adapter, int addr, int data)
585{
586 int ret = 0;
587
588 if (rom_lock(adapter) != 0) {
589 return -1;
590 }
591 ret = do_rom_fast_write(adapter, addr, data);
592 netxen_rom_unlock(adapter);
593 return ret;
594}
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800595
Jeff Garzik47906542007-11-23 21:23:36 -0500596static int do_rom_fast_write_words(struct netxen_adapter *adapter,
Adrian Bunk993fb902007-11-05 18:07:31 +0100597 int addr, u8 *bytes, size_t size)
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800598{
599 int addridx = addr;
600 int ret = 0;
601
602 while (addridx < (addr + size)) {
603 int last_attempt = 0;
604 int timeout = 0;
605 int data;
606
Al Virof305f782007-12-22 19:44:00 +0000607 data = le32_to_cpu((*(__le32*)bytes));
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800608 ret = do_rom_fast_write(adapter, addridx, data);
609 if (ret < 0)
610 return ret;
Jeff Garzik47906542007-11-23 21:23:36 -0500611
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800612 while(1) {
613 int data1;
614
Stephen Hemmingerf8dfdd52007-02-26 14:51:56 -0800615 ret = do_rom_fast_read(adapter, addridx, &data1);
616 if (ret < 0)
617 return ret;
618
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800619 if (data1 == data)
620 break;
621
622 if (timeout++ >= rom_write_timeout) {
623 if (last_attempt++ < 4) {
Jeff Garzik47906542007-11-23 21:23:36 -0500624 ret = do_rom_fast_write(adapter,
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800625 addridx, data);
626 if (ret < 0)
627 return ret;
628 }
629 else {
630 printk(KERN_INFO "Data write did not "
631 "succeed at address 0x%x\n", addridx);
632 break;
633 }
634 }
635 }
636
637 bytes += 4;
638 addridx += 4;
639 }
640
641 return ret;
642}
643
Jeff Garzik47906542007-11-23 21:23:36 -0500644int netxen_rom_fast_write_words(struct netxen_adapter *adapter, int addr,
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800645 u8 *bytes, size_t size)
646{
647 int ret = 0;
648
649 ret = rom_lock(adapter);
650 if (ret < 0)
651 return ret;
652
653 ret = do_rom_fast_write_words(adapter, addr, bytes, size);
654 netxen_rom_unlock(adapter);
655
656 return ret;
657}
658
Adrian Bunk993fb902007-11-05 18:07:31 +0100659static int netxen_rom_wrsr(struct netxen_adapter *adapter, int data)
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800660{
661 int ret;
662
663 ret = netxen_rom_wren(adapter);
664 if (ret < 0)
665 return ret;
666
667 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
Jeff Garzik47906542007-11-23 21:23:36 -0500668 netxen_crb_writelit_adapter(adapter,
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800669 NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0x1);
670
671 ret = netxen_wait_rom_done(adapter);
672 if (ret < 0)
673 return ret;
674
675 return netxen_rom_wip_poll(adapter);
676}
677
Adrian Bunk993fb902007-11-05 18:07:31 +0100678static int netxen_rom_rdsr(struct netxen_adapter *adapter)
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800679{
680 int ret;
681
682 ret = rom_lock(adapter);
683 if (ret < 0)
684 return ret;
685
686 ret = netxen_do_rom_rdsr(adapter);
687 netxen_rom_unlock(adapter);
688 return ret;
689}
690
691int netxen_backup_crbinit(struct netxen_adapter *adapter)
692{
693 int ret = FLASH_SUCCESS;
694 int val;
Mithlesh Thukral0d047612007-06-07 04:36:36 -0700695 char *buffer = kmalloc(NETXEN_FLASH_SECTOR_SIZE, GFP_KERNEL);
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800696
697 if (!buffer)
Jeff Garzik47906542007-11-23 21:23:36 -0500698 return -ENOMEM;
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800699 /* unlock sector 63 */
700 val = netxen_rom_rdsr(adapter);
701 val = val & 0xe3;
702 ret = netxen_rom_wrsr(adapter, val);
703 if (ret != FLASH_SUCCESS)
704 goto out_kfree;
705
706 ret = netxen_rom_wip_poll(adapter);
707 if (ret != FLASH_SUCCESS)
708 goto out_kfree;
709
710 /* copy sector 0 to sector 63 */
Jeff Garzik47906542007-11-23 21:23:36 -0500711 ret = netxen_rom_fast_read_words(adapter, NETXEN_CRBINIT_START,
Mithlesh Thukral0d047612007-06-07 04:36:36 -0700712 buffer, NETXEN_FLASH_SECTOR_SIZE);
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800713 if (ret != FLASH_SUCCESS)
714 goto out_kfree;
715
Jeff Garzik47906542007-11-23 21:23:36 -0500716 ret = netxen_rom_fast_write_words(adapter, NETXEN_FIXED_START,
Mithlesh Thukral0d047612007-06-07 04:36:36 -0700717 buffer, NETXEN_FLASH_SECTOR_SIZE);
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800718 if (ret != FLASH_SUCCESS)
719 goto out_kfree;
720
721 /* lock sector 63 */
722 val = netxen_rom_rdsr(adapter);
723 if (!(val & 0x8)) {
724 val |= (0x1 << 2);
725 /* lock sector 63 */
726 if (netxen_rom_wrsr(adapter, val) == 0) {
727 ret = netxen_rom_wip_poll(adapter);
728 if (ret != FLASH_SUCCESS)
729 goto out_kfree;
730
731 /* lock SR writes */
732 ret = netxen_rom_wip_poll(adapter);
733 if (ret != FLASH_SUCCESS)
734 goto out_kfree;
735 }
736 }
737
738out_kfree:
739 kfree(buffer);
740 return ret;
741}
742
Adrian Bunk993fb902007-11-05 18:07:31 +0100743static int netxen_do_rom_se(struct netxen_adapter *adapter, int addr)
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800744{
745 netxen_rom_wren(adapter);
746 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
747 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
748 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
749 M25P_INSTR_SE);
750 if (netxen_wait_rom_done(adapter)) {
751 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
752 return -1;
753 }
754 return netxen_rom_wip_poll(adapter);
755}
756
Adrian Bunk993fb902007-11-05 18:07:31 +0100757static void check_erased_flash(struct netxen_adapter *adapter, int addr)
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800758{
759 int i;
760 int val;
761 int count = 0, erased_errors = 0;
762 int range;
763
Jeff Garzik47906542007-11-23 21:23:36 -0500764 range = (addr == NETXEN_USER_START) ?
Mithlesh Thukral0d047612007-06-07 04:36:36 -0700765 NETXEN_FIXED_START : addr + NETXEN_FLASH_SECTOR_SIZE;
Jeff Garzik47906542007-11-23 21:23:36 -0500766
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800767 for (i = addr; i < range; i += 4) {
768 netxen_rom_fast_read(adapter, i, &val);
769 if (val != 0xffffffff)
770 erased_errors++;
771 count++;
772 }
773
774 if (erased_errors)
775 printk(KERN_INFO "0x%x out of 0x%x words fail to be erased "
776 "for sector address: %x\n", erased_errors, count, addr);
777}
778
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800779int netxen_rom_se(struct netxen_adapter *adapter, int addr)
780{
781 int ret = 0;
782 if (rom_lock(adapter) != 0) {
783 return -1;
784 }
785 ret = netxen_do_rom_se(adapter, addr);
786 netxen_rom_unlock(adapter);
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800787 msleep(30);
788 check_erased_flash(adapter, addr);
789
790 return ret;
791}
792
Adrian Bunk993fb902007-11-05 18:07:31 +0100793static int netxen_flash_erase_sections(struct netxen_adapter *adapter,
794 int start, int end)
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800795{
796 int ret = FLASH_SUCCESS;
797 int i;
798
799 for (i = start; i < end; i++) {
Mithlesh Thukral0d047612007-06-07 04:36:36 -0700800 ret = netxen_rom_se(adapter, i * NETXEN_FLASH_SECTOR_SIZE);
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800801 if (ret)
802 break;
803 ret = netxen_rom_wip_poll(adapter);
804 if (ret < 0)
805 return ret;
806 }
807
808 return ret;
809}
810
811int
812netxen_flash_erase_secondary(struct netxen_adapter *adapter)
813{
814 int ret = FLASH_SUCCESS;
815 int start, end;
816
Mithlesh Thukral0d047612007-06-07 04:36:36 -0700817 start = NETXEN_SECONDARY_START / NETXEN_FLASH_SECTOR_SIZE;
818 end = NETXEN_USER_START / NETXEN_FLASH_SECTOR_SIZE;
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800819 ret = netxen_flash_erase_sections(adapter, start, end);
820
821 return ret;
822}
823
824int
825netxen_flash_erase_primary(struct netxen_adapter *adapter)
826{
827 int ret = FLASH_SUCCESS;
828 int start, end;
829
Mithlesh Thukral0d047612007-06-07 04:36:36 -0700830 start = NETXEN_PRIMARY_START / NETXEN_FLASH_SECTOR_SIZE;
831 end = NETXEN_SECONDARY_START / NETXEN_FLASH_SECTOR_SIZE;
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800832 ret = netxen_flash_erase_sections(adapter, start, end);
833
834 return ret;
835}
836
Amit S. Kalee45d9ab2007-02-09 05:49:08 -0800837void netxen_halt_pegs(struct netxen_adapter *adapter)
838{
839 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c, 1);
840 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c, 1);
841 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c, 1);
842 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c, 1);
843}
844
Amit S. Kale27d2ab52007-02-05 07:40:49 -0800845int netxen_flash_unlock(struct netxen_adapter *adapter)
846{
847 int ret = 0;
848
849 ret = netxen_rom_wrsr(adapter, 0);
850 if (ret < 0)
851 return ret;
852
853 ret = netxen_rom_wren(adapter);
854 if (ret < 0)
855 return ret;
856
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400857 return ret;
858}
Dhananjay Phadke7830b222008-07-21 19:44:00 -0700859#endif /* 0 */
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400860
861#define NETXEN_BOARDTYPE 0x4008
862#define NETXEN_BOARDNUM 0x400c
863#define NETXEN_CHIPNUM 0x4010
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400864
865int netxen_pinit_from_rom(struct netxen_adapter *adapter, int verbose)
866{
Dhananjay Phadkedcd56fd2008-06-15 22:59:45 -0700867 int addr, val;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700868 int i, init_delay = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400869 struct crb_addr_pair *buf;
Dhananjay Phadke29566402008-07-21 19:44:04 -0700870 unsigned offset, n;
Linsys Contractor Mithlesh Thukrale0e20a12007-02-28 05:16:40 -0800871 u32 off;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400872
873 /* resetall */
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400874 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
Dhananjay Phadke29566402008-07-21 19:44:04 -0700875 0xffffffff);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400876
877 if (verbose) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400878 if (netxen_rom_fast_read(adapter, NETXEN_BOARDTYPE, &val) == 0)
879 printk("P2 ROM board type: 0x%08x\n", val);
880 else
881 printk("Could not read board type\n");
882 if (netxen_rom_fast_read(adapter, NETXEN_BOARDNUM, &val) == 0)
883 printk("P2 ROM board num: 0x%08x\n", val);
884 else
885 printk("Could not read board number\n");
886 if (netxen_rom_fast_read(adapter, NETXEN_CHIPNUM, &val) == 0)
887 printk("P2 ROM chip num: 0x%08x\n", val);
888 else
889 printk("Could not read chip number\n");
890 }
891
Dhananjay Phadke29566402008-07-21 19:44:04 -0700892 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
893 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
894 (n != 0xcafecafeUL) ||
895 netxen_rom_fast_read(adapter, 4, &n) != 0) {
896 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
897 "n: %08x\n", netxen_nic_driver_name, n);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400898 return -EIO;
899 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700900 offset = n & 0xffffU;
901 n = (n >> 16) & 0xffffU;
902 } else {
903 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
904 !(n & 0x80000000)) {
905 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
906 "n: %08x\n", netxen_nic_driver_name, n);
907 return -EIO;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400908 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700909 offset = 1;
910 n &= ~0x80000000;
911 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400912
Dhananjay Phadke29566402008-07-21 19:44:04 -0700913 if (n < 1024) {
914 if (verbose)
915 printk(KERN_DEBUG "%s: %d CRB init values found"
916 " in ROM.\n", netxen_nic_driver_name, n);
917 } else {
918 printk(KERN_ERR "%s:n=0x%x Error! NetXen card flash not"
919 " initialized.\n", __func__, n);
920 return -EIO;
921 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400922
Dhananjay Phadke29566402008-07-21 19:44:04 -0700923 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
924 if (buf == NULL) {
925 printk("%s: netxen_pinit_from_rom: Unable to calloc memory.\n",
926 netxen_nic_driver_name);
927 return -ENOMEM;
928 }
929 for (i = 0; i < n; i++) {
930 if (netxen_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
931 netxen_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0)
932 return -EIO;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400933
Dhananjay Phadke29566402008-07-21 19:44:04 -0700934 buf[i].addr = addr;
935 buf[i].data = val;
936
937 if (verbose)
938 printk(KERN_DEBUG "%s: PCI: 0x%08x == 0x%08x\n",
939 netxen_nic_driver_name,
940 (u32)netxen_decode_crb_addr(addr), val);
941 }
942 for (i = 0; i < n; i++) {
943
944 off = netxen_decode_crb_addr(buf[i].addr);
945 if (off == NETXEN_ADDR_ERROR) {
946 printk(KERN_ERR"CRB init value out of range %x\n",
Amit S. Kale1fcca1a2007-02-05 07:35:26 -0800947 buf[i].addr);
Dhananjay Phadke29566402008-07-21 19:44:04 -0700948 continue;
949 }
950 off += NETXEN_PCI_CRBSPACE;
951 /* skipping cold reboot MAGIC */
952 if (off == NETXEN_CAM_RAM(0x1fc))
953 continue;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400954
Dhananjay Phadke29566402008-07-21 19:44:04 -0700955 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
956 /* do not reset PCI */
957 if (off == (ROMUSB_GLB + 0xbc))
958 continue;
959 if (off == (NETXEN_CRB_PEG_NET_1 + 0x18))
960 buf[i].data = 0x1020;
961 /* skip the function enable register */
962 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION))
963 continue;
964 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION2))
965 continue;
966 if ((off & 0x0ff00000) == NETXEN_CRB_SMB)
967 continue;
968 }
969
970 if (off == NETXEN_ADDR_ERROR) {
971 printk(KERN_ERR "%s: Err: Unknown addr: 0x%08x\n",
972 netxen_nic_driver_name, buf[i].addr);
973 continue;
974 }
975
976 /* After writing this register, HW needs time for CRB */
977 /* to quiet down (else crb_window returns 0xffffffff) */
978 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
979 init_delay = 1;
980 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400981 /* hold xdma in reset also */
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800982 buf[i].data = NETXEN_NIC_XDMA_RESET;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400983 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400984 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400985
Dhananjay Phadke29566402008-07-21 19:44:04 -0700986 adapter->hw_write_wx(adapter, off, &buf[i].data, 4);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400987
Dhananjay Phadke29566402008-07-21 19:44:04 -0700988 if (init_delay == 1) {
989 msleep(1000);
990 init_delay = 0;
991 }
992 msleep(1);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400993 }
Dhananjay Phadke29566402008-07-21 19:44:04 -0700994 kfree(buf);
995
996 /* disable_peg_cache_all */
997
998 /* unreset_net_cache */
999 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1000 adapter->hw_read_wx(adapter,
1001 NETXEN_ROMUSB_GLB_SW_RESET, &val, 4);
1002 netxen_crb_writelit_adapter(adapter,
1003 NETXEN_ROMUSB_GLB_SW_RESET, (val & 0xffffff0f));
1004 }
1005
1006 /* p2dn replyCount */
1007 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
1008 /* disable_peg_cache 0 */
1009 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_D + 0x4c, 8);
1010 /* disable_peg_cache 1 */
1011 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_I + 0x4c, 8);
1012
1013 /* peg_clr_all */
1014
1015 /* peg_clr 0 */
1016 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x8, 0);
1017 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0xc, 0);
1018 /* peg_clr 1 */
1019 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x8, 0);
1020 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0xc, 0);
1021 /* peg_clr 2 */
1022 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x8, 0);
1023 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0xc, 0);
1024 /* peg_clr 3 */
1025 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x8, 0);
1026 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0xc, 0);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001027 return 0;
1028}
1029
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001030int netxen_initialize_adapter_offload(struct netxen_adapter *adapter)
1031{
1032 uint64_t addr;
1033 uint32_t hi;
1034 uint32_t lo;
1035
1036 adapter->dummy_dma.addr =
Dhananjay Phadke7830b222008-07-21 19:44:00 -07001037 pci_alloc_consistent(adapter->pdev,
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001038 NETXEN_HOST_DUMMY_DMA_SIZE,
1039 &adapter->dummy_dma.phys_addr);
1040 if (adapter->dummy_dma.addr == NULL) {
1041 printk("%s: ERROR: Could not allocate dummy DMA memory\n",
Dhananjay Phadke29566402008-07-21 19:44:04 -07001042 __func__);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001043 return -ENOMEM;
1044 }
1045
1046 addr = (uint64_t) adapter->dummy_dma.phys_addr;
1047 hi = (addr >> 32) & 0xffffffff;
1048 lo = addr & 0xffffffff;
1049
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001050 adapter->pci_write_normalize(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI, hi);
1051 adapter->pci_write_normalize(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO, lo);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001052
Dhananjay Phadke29566402008-07-21 19:44:04 -07001053 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1054 uint32_t temp = 0;
1055 adapter->hw_write_wx(adapter, CRB_HOST_DUMMY_BUF, &temp, 4);
1056 }
1057
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001058 return 0;
1059}
1060
1061void netxen_free_adapter_offload(struct netxen_adapter *adapter)
1062{
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001063 int i;
1064
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001065 if (adapter->dummy_dma.addr) {
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001066 i = 100;
1067 do {
1068 if (dma_watchdog_shutdown_request(adapter) == 1)
1069 break;
1070 msleep(50);
1071 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
1072 break;
1073 } while (--i);
1074
1075 if (i) {
Dhananjay Phadke7830b222008-07-21 19:44:00 -07001076 pci_free_consistent(adapter->pdev,
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001077 NETXEN_HOST_DUMMY_DMA_SIZE,
1078 adapter->dummy_dma.addr,
1079 adapter->dummy_dma.phys_addr);
Dhananjay Phadke439b4542008-06-15 22:59:46 -07001080 adapter->dummy_dma.addr = NULL;
1081 } else {
1082 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
1083 adapter->netdev->name);
1084 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001085 }
1086}
1087
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301088int netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001089{
1090 u32 val = 0;
Dhananjay Phadke29566402008-07-21 19:44:04 -07001091 int retries = 60;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001092
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001093 if (!pegtune_val) {
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301094 do {
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001095 val = adapter->pci_read_normalize(adapter,
1096 CRB_CMDPEG_STATE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301097
1098 if (val == PHAN_INITIALIZE_COMPLETE ||
1099 val == PHAN_INITIALIZE_ACK)
1100 return 0;
1101
Dhananjay Phadke29566402008-07-21 19:44:04 -07001102 msleep(500);
1103
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301104 } while (--retries);
Dhananjay Phadke29566402008-07-21 19:44:04 -07001105
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301106 if (!retries) {
Dhananjay Phadke29566402008-07-21 19:44:04 -07001107 pegtune_val = adapter->pci_read_normalize(adapter,
1108 NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301109 printk(KERN_WARNING "netxen_phantom_init: init failed, "
1110 "pegtune_val=%x\n", pegtune_val);
1111 return -1;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001112 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001113 }
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301114
1115 return 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001116}
1117
Dhananjay Phadke29566402008-07-21 19:44:04 -07001118int netxen_receive_peg_ready(struct netxen_adapter *adapter)
1119{
1120 u32 val = 0;
1121 int retries = 2000;
1122
1123 do {
1124 val = adapter->pci_read_normalize(adapter, CRB_RCVPEG_STATE);
1125
1126 if (val == PHAN_PEG_RCV_INITIALIZED)
1127 return 0;
1128
1129 msleep(10);
1130
1131 } while (--retries);
1132
1133 if (!retries) {
1134 printk(KERN_ERR "Receive Peg initialization not "
1135 "complete, state: 0x%x.\n", val);
1136 return -EIO;
1137 }
1138
1139 return 0;
1140}
1141
Adrian Bunk993fb902007-11-05 18:07:31 +01001142static int netxen_nic_check_temp(struct netxen_adapter *adapter)
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001143{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001144 struct net_device *netdev = adapter->netdev;
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001145 uint32_t temp, temp_state, temp_val;
1146 int rv = 0;
1147
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001148 temp = adapter->pci_read_normalize(adapter, CRB_TEMP_STATE);
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001149
1150 temp_state = nx_get_temp_state(temp);
1151 temp_val = nx_get_temp_val(temp);
1152
1153 if (temp_state == NX_TEMP_PANIC) {
1154 printk(KERN_ALERT
1155 "%s: Device temperature %d degrees C exceeds"
1156 " maximum allowed. Hardware has been shut down.\n",
1157 netxen_nic_driver_name, temp_val);
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001158
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001159 netif_carrier_off(netdev);
1160 netif_stop_queue(netdev);
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001161 rv = 1;
1162 } else if (temp_state == NX_TEMP_WARN) {
1163 if (adapter->temp == NX_TEMP_NORMAL) {
1164 printk(KERN_ALERT
1165 "%s: Device temperature %d degrees C "
1166 "exceeds operating range."
1167 " Immediate action needed.\n",
1168 netxen_nic_driver_name, temp_val);
1169 }
1170 } else {
1171 if (adapter->temp == NX_TEMP_WARN) {
1172 printk(KERN_INFO
1173 "%s: Device temperature is now %d degrees C"
1174 " in normal range.\n", netxen_nic_driver_name,
1175 temp_val);
1176 }
1177 }
1178 adapter->temp = temp_state;
1179 return rv;
1180}
1181
David Howells6d5aefb2006-12-05 19:36:26 +00001182void netxen_watchdog_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001183{
David Howells6d5aefb2006-12-05 19:36:26 +00001184 struct netxen_adapter *adapter =
1185 container_of(work, struct netxen_adapter, watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001186
Mithlesh Thukral6c80b182007-04-20 07:55:26 -07001187 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
Amit S. Kalecb8011a2006-11-29 09:00:10 -08001188 return;
1189
Amit S. Kale80922fb2006-12-04 09:18:00 -08001190 if (adapter->handle_phy_intr)
1191 adapter->handle_phy_intr(adapter);
Mithlesh Thukralc27e6722007-06-11 03:30:58 -07001192
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001193 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1194}
1195
1196/*
1197 * netxen_process_rcv() send the received packet to the protocol stack.
1198 * and if the number of receives exceeds RX_BUFFERS_REFILL, then we
1199 * invoke the routine to send more rx buffers to the Phantom...
1200 */
Adrian Bunk993fb902007-11-05 18:07:31 +01001201static void netxen_process_rcv(struct netxen_adapter *adapter, int ctxid,
1202 struct status_desc *desc)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001203{
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001204 struct pci_dev *pdev = adapter->pdev;
1205 struct net_device *netdev = adapter->netdev;
Dhananjay Phadke5dc16262007-12-31 10:08:57 -08001206 u64 sts_data = le64_to_cpu(desc->status_desc_data);
1207 int index = netxen_get_sts_refhandle(sts_data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001208 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1209 struct netxen_rx_buffer *buffer;
1210 struct sk_buff *skb;
Dhananjay Phadke5dc16262007-12-31 10:08:57 -08001211 u32 length = netxen_get_sts_totallength(sts_data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001212 u32 desc_ctx;
1213 struct netxen_rcv_desc_ctx *rcv_desc;
1214 int ret;
1215
Dhananjay Phadke5dc16262007-12-31 10:08:57 -08001216 desc_ctx = netxen_get_sts_type(sts_data);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001217 if (unlikely(desc_ctx >= NUM_RCV_DESC_RINGS)) {
1218 printk("%s: %s Bad Rcv descriptor ring\n",
1219 netxen_nic_driver_name, netdev->name);
1220 return;
1221 }
1222
1223 rcv_desc = &recv_ctx->rcv_desc[desc_ctx];
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001224 if (unlikely(index > rcv_desc->max_rx_desc_count)) {
1225 DPRINTK(ERR, "Got a buffer index:%x Max is %x\n",
1226 index, rcv_desc->max_rx_desc_count);
1227 return;
1228 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001229 buffer = &rcv_desc->rx_buf_arr[index];
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001230 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1231 buffer->lro_current_frags++;
1232 if (netxen_get_sts_desc_lro_last_frag(desc)) {
1233 buffer->lro_expected_frags =
1234 netxen_get_sts_desc_lro_cnt(desc);
1235 buffer->lro_length = length;
1236 }
1237 if (buffer->lro_current_frags != buffer->lro_expected_frags) {
1238 if (buffer->lro_expected_frags != 0) {
Joe Perches5bc51422007-11-19 17:48:24 -08001239 printk("LRO: (refhandle:%x) recv frag. "
1240 "wait for last. flags: %x expected:%d "
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001241 "have:%d\n", index,
1242 netxen_get_sts_desc_lro_last_frag(desc),
1243 buffer->lro_expected_frags,
1244 buffer->lro_current_frags);
1245 }
1246 return;
1247 }
1248 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001249
1250 pci_unmap_single(pdev, buffer->dma, rcv_desc->dma_size,
1251 PCI_DMA_FROMDEVICE);
1252
1253 skb = (struct sk_buff *)buffer->skb;
1254
Dhananjay Phadke200eef22007-09-03 10:33:35 +05301255 if (likely(adapter->rx_csum &&
Dhananjay Phadked1847a72008-03-17 19:59:51 -07001256 netxen_get_sts_status(sts_data) == STATUS_CKSUM_OK)) {
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001257 adapter->stats.csummed++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001258 skb->ip_summed = CHECKSUM_UNNECESSARY;
Dhananjay Phadke200eef22007-09-03 10:33:35 +05301259 } else
1260 skb->ip_summed = CHECKSUM_NONE;
1261
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301262 skb->dev = netdev;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001263 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1264 /* True length was only available on the last pkt */
1265 skb_put(skb, buffer->lro_length);
1266 } else {
1267 skb_put(skb, length);
1268 }
1269
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001270 skb->protocol = eth_type_trans(skb, netdev);
1271
1272 ret = netif_receive_skb(skb);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001273 netdev->last_rx = jiffies;
1274
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001275 /*
1276 * We just consumed one buffer so post a buffer.
1277 */
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001278 buffer->skb = NULL;
1279 buffer->state = NETXEN_BUFFER_FREE;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001280 buffer->lro_current_frags = 0;
1281 buffer->lro_expected_frags = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001282
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001283 adapter->stats.no_rcv++;
1284 adapter->stats.rxbytes += length;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001285}
1286
1287/* Process Receive status ring */
1288u32 netxen_process_rcv_ring(struct netxen_adapter *adapter, int ctxid, int max)
1289{
1290 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1291 struct status_desc *desc_head = recv_ctx->rcv_status_desc_head;
1292 struct status_desc *desc; /* used to read status desc here */
1293 u32 consumer = recv_ctx->status_rx_consumer;
1294 int count = 0, ring;
1295
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001296 while (count < max) {
1297 desc = &desc_head[consumer];
Al Viroa608ab92007-01-02 10:39:10 +00001298 if (!(netxen_get_sts_owner(desc) & STATUS_OWNER_HOST)) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001299 DPRINTK(ERR, "desc %p ownedby %x\n", desc,
1300 netxen_get_sts_owner(desc));
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001301 break;
1302 }
1303 netxen_process_rcv(adapter, ctxid, desc);
Al Viroa608ab92007-01-02 10:39:10 +00001304 netxen_set_sts_owner(desc, STATUS_OWNER_PHANTOM);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001305 consumer = (consumer + 1) & (adapter->max_rx_desc_count - 1);
1306 count++;
1307 }
Jeff Garzikf0c88f92008-03-25 23:53:24 -04001308 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++)
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001309 netxen_post_rx_buffers_nodb(adapter, ctxid, ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001310
1311 /* update the consumer index in phantom */
1312 if (count) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001313 recv_ctx->status_rx_consumer = consumer;
1314
1315 /* Window = 1 */
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001316 adapter->pci_write_normalize(adapter,
1317 recv_ctx->crb_sts_consumer, consumer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001318 }
1319
1320 return count;
1321}
1322
1323/* Process Command status ring */
Dhananjay Phadke05aaa022008-03-17 19:59:49 -07001324int netxen_process_cmd_ring(struct netxen_adapter *adapter)
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001325{
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001326 u32 last_consumer, consumer;
1327 int count = 0, i;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001328 struct netxen_cmd_buffer *buffer;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001329 struct pci_dev *pdev = adapter->pdev;
1330 struct net_device *netdev = adapter->netdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001331 struct netxen_skb_frag *frag;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001332 int done = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001333
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001334 last_consumer = adapter->last_cmd_consumer;
Amit S. Kale9b410112007-02-09 05:45:18 -08001335 consumer = le32_to_cpu(*(adapter->cmd_consumer));
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001336
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001337 while (last_consumer != consumer) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001338 buffer = &adapter->cmd_buf_arr[last_consumer];
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001339 if (buffer->skb) {
1340 frag = &buffer->frag_array[0];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001341 pci_unmap_single(pdev, frag->dma, frag->length,
1342 PCI_DMA_TODEVICE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301343 frag->dma = 0ULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001344 for (i = 1; i < buffer->frag_count; i++) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001345 frag++; /* Get the next frag */
1346 pci_unmap_page(pdev, frag->dma, frag->length,
1347 PCI_DMA_TODEVICE);
Dhananjay Phadke96acb6e2007-07-02 09:37:57 +05301348 frag->dma = 0ULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001349 }
1350
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001351 adapter->stats.xmitfinished++;
dhananjay@netxen.com53a01e02007-12-26 10:23:58 -08001352 dev_kfree_skb_any(buffer->skb);
1353 buffer->skb = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001354 }
1355
1356 last_consumer = get_next_index(last_consumer,
1357 adapter->max_tx_desc_count);
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001358 if (++count >= MAX_STATUS_HANDLE)
1359 break;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001360 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001361
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001362 if (count) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001363 adapter->last_cmd_consumer = last_consumer;
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001364 smp_mb();
1365 if (netif_queue_stopped(netdev) && netif_running(netdev)) {
1366 netif_tx_lock(netdev);
1367 netif_wake_queue(netdev);
1368 smp_mb();
1369 netif_tx_unlock(netdev);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001370 }
1371 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001372 /*
1373 * If everything is freed up to consumer then check if the ring is full
1374 * If the ring is full then check if more needs to be freed and
1375 * schedule the call back again.
1376 *
1377 * This happens when there are 2 CPUs. One could be freeing and the
1378 * other filling it. If the ring is full when we get out of here and
1379 * the card has already interrupted the host then the host can miss the
1380 * interrupt.
1381 *
1382 * There is still a possible race condition and the host could miss an
1383 * interrupt. The card has to take care of this.
1384 */
Dhananjay Phadkeba53e6b2008-03-17 19:59:50 -07001385 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1386 done = (last_consumer == consumer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001387
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001388 return (done);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001389}
1390
1391/*
1392 * netxen_post_rx_buffers puts buffer in the Phantom memory
1393 */
1394void netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ctx, u32 ringid)
1395{
Dhananjay Phadke7830b222008-07-21 19:44:00 -07001396 struct pci_dev *pdev = adapter->pdev;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001397 struct sk_buff *skb;
1398 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1399 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001400 uint producer;
1401 struct rcv_desc *pdesc;
1402 struct netxen_rx_buffer *buffer;
1403 int count = 0;
1404 int index = 0;
1405 netxen_ctx_msg msg = 0;
1406 dma_addr_t dma;
1407
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001408 rcv_desc = &recv_ctx->rcv_desc[ringid];
1409
1410 producer = rcv_desc->producer;
1411 index = rcv_desc->begin_alloc;
1412 buffer = &rcv_desc->rx_buf_arr[index];
1413 /* We can start writing rx descriptors into the phantom memory. */
1414 while (buffer->state == NETXEN_BUFFER_FREE) {
1415 skb = dev_alloc_skb(rcv_desc->skb_size);
1416 if (unlikely(!skb)) {
1417 /*
1418 * TODO
1419 * We need to schedule the posting of buffers to the pegs.
1420 */
1421 rcv_desc->begin_alloc = index;
1422 DPRINTK(ERR, "netxen_post_rx_buffers: "
1423 " allocated only %d buffers\n", count);
1424 break;
1425 }
1426
1427 count++; /* now there should be no failure */
1428 pdesc = &rcv_desc->desc_head[producer];
1429
1430#if defined(XGB_DEBUG)
1431 *(unsigned long *)(skb->head) = 0xc0debabe;
1432 if (skb_is_nonlinear(skb)) {
1433 printk("Allocated SKB @%p is nonlinear\n");
1434 }
1435#endif
1436 skb_reserve(skb, 2);
1437 /* This will be setup when we receive the
1438 * buffer after it has been filled FSL TBD TBD
1439 * skb->dev = netdev;
1440 */
1441 dma = pci_map_single(pdev, skb->data, rcv_desc->dma_size,
1442 PCI_DMA_FROMDEVICE);
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001443 pdesc->addr_buffer = cpu_to_le64(dma);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001444 buffer->skb = skb;
1445 buffer->state = NETXEN_BUFFER_BUSY;
1446 buffer->dma = dma;
1447 /* make a rcv descriptor */
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001448 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1449 pdesc->buffer_length = cpu_to_le32(rcv_desc->dma_size);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001450 DPRINTK(INFO, "done writing descripter\n");
1451 producer =
1452 get_next_index(producer, rcv_desc->max_rx_desc_count);
1453 index = get_next_index(index, rcv_desc->max_rx_desc_count);
1454 buffer = &rcv_desc->rx_buf_arr[index];
1455 }
1456 /* if we did allocate buffers, then write the count to Phantom */
1457 if (count) {
1458 rcv_desc->begin_alloc = index;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001459 rcv_desc->producer = producer;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001460 /* Window = 1 */
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001461 adapter->pci_write_normalize(adapter,
1462 rcv_desc->crb_rcv_producer,
1463 (producer-1) & (rcv_desc->max_rx_desc_count-1));
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001464 /*
1465 * Write a doorbell msg to tell phanmon of change in
1466 * receive ring producer
1467 */
1468 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1469 netxen_set_msg_privid(msg);
1470 netxen_set_msg_count(msg,
1471 ((producer -
1472 1) & (rcv_desc->
1473 max_rx_desc_count - 1)));
Mithlesh Thukral3176ff32007-04-20 07:52:37 -07001474 netxen_set_msg_ctxid(msg, adapter->portnum);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001475 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1476 writel(msg,
1477 DB_NORMALIZE(adapter,
1478 NETXEN_RCV_PRODUCER_OFFSET));
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001479 }
1480}
1481
Adrian Bunk993fb902007-11-05 18:07:31 +01001482static void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
1483 uint32_t ctx, uint32_t ringid)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001484{
Dhananjay Phadke7830b222008-07-21 19:44:00 -07001485 struct pci_dev *pdev = adapter->pdev;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001486 struct sk_buff *skb;
1487 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1488 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001489 u32 producer;
1490 struct rcv_desc *pdesc;
1491 struct netxen_rx_buffer *buffer;
1492 int count = 0;
1493 int index = 0;
1494
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001495 rcv_desc = &recv_ctx->rcv_desc[ringid];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001496
1497 producer = rcv_desc->producer;
1498 index = rcv_desc->begin_alloc;
1499 buffer = &rcv_desc->rx_buf_arr[index];
1500 /* We can start writing rx descriptors into the phantom memory. */
1501 while (buffer->state == NETXEN_BUFFER_FREE) {
1502 skb = dev_alloc_skb(rcv_desc->skb_size);
1503 if (unlikely(!skb)) {
1504 /*
1505 * We need to schedule the posting of buffers to the pegs.
1506 */
1507 rcv_desc->begin_alloc = index;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001508 DPRINTK(ERR, "netxen_post_rx_buffers_nodb: "
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001509 " allocated only %d buffers\n", count);
1510 break;
1511 }
1512 count++; /* now there should be no failure */
1513 pdesc = &rcv_desc->desc_head[producer];
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001514 skb_reserve(skb, 2);
Jeff Garzik47906542007-11-23 21:23:36 -05001515 /*
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001516 * This will be setup when we receive the
1517 * buffer after it has been filled
1518 * skb->dev = netdev;
1519 */
1520 buffer->skb = skb;
1521 buffer->state = NETXEN_BUFFER_BUSY;
1522 buffer->dma = pci_map_single(pdev, skb->data,
1523 rcv_desc->dma_size,
1524 PCI_DMA_FROMDEVICE);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001525
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001526 /* make a rcv descriptor */
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001527 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
Al Viroa608ab92007-01-02 10:39:10 +00001528 pdesc->buffer_length = cpu_to_le32(rcv_desc->dma_size);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001529 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001530 producer =
1531 get_next_index(producer, rcv_desc->max_rx_desc_count);
1532 index = get_next_index(index, rcv_desc->max_rx_desc_count);
1533 buffer = &rcv_desc->rx_buf_arr[index];
1534 }
1535
1536 /* if we did allocate buffers, then write the count to Phantom */
1537 if (count) {
1538 rcv_desc->begin_alloc = index;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001539 rcv_desc->producer = producer;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001540 /* Window = 1 */
Dhananjay Phadke3ce06a32008-07-21 19:44:03 -07001541 adapter->pci_write_normalize(adapter,
1542 rcv_desc->crb_rcv_producer,
1543 (producer-1) & (rcv_desc->max_rx_desc_count-1));
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001544 wmb();
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001545 }
1546}
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