Merge branch 'drm-next' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[linux-2.6-block.git] / drivers / net / netxen / netxen_nic_init.c
CommitLineData
3d396eb1
AK
1/*
2 * Copyright (C) 2003 - 2006 NetXen, Inc.
3 * All rights reserved.
80922fbc 4 *
3d396eb1
AK
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
80922fbc 9 *
3d396eb1
AK
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
80922fbc 14 *
3d396eb1
AK
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 * MA 02111-1307, USA.
80922fbc 19 *
3d396eb1
AK
20 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
80922fbc 22 *
3d396eb1
AK
23 * Contact Information:
24 * info@netxen.com
25 * NetXen,
26 * 3965 Freedom Circle, Fourth floor,
27 * Santa Clara, CA 95054
28 *
29 *
30 * 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"
3d396eb1
AK
38#include "netxen_nic_phan_reg.h"
39
40struct crb_addr_pair {
e0e20a1a
LCMT
41 u32 addr;
42 u32 data;
3d396eb1
AK
43};
44
45#define NETXEN_MAX_CRB_XFORM 60
46static unsigned int crb_addr_xform[NETXEN_MAX_CRB_XFORM];
e0e20a1a 47#define NETXEN_ADDR_ERROR (0xffffffff)
3d396eb1
AK
48
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
cb8011ad
AK
53#define NETXEN_NIC_XDMA_RESET 0x8000ff
54
993fb90c
AB
55static 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)
3d396eb1 61{
cb8011ad 62 void __iomem *addr = pci_base_offset(adapter, off);
3d396eb1
AK
63 writel(*data, addr);
64}
993fb90c 65#endif /* 0 */
3d396eb1
AK
66
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);
1fcca1a5 117 crb_addr_transform(SMB);
e4c93c81
DP
118 crb_addr_transform(OCM0);
119 crb_addr_transform(I2C0);
3d396eb1
AK
120}
121
122int netxen_init_firmware(struct netxen_adapter *adapter)
123{
124 u32 state = 0, loops = 0, err = 0;
125
126 /* Window 1 call */
3ce06a32 127 state = adapter->pci_read_normalize(adapter, CRB_CMDPEG_STATE);
3d396eb1
AK
128
129 if (state == PHAN_INITIALIZE_ACK)
130 return 0;
131
132 while (state != PHAN_INITIALIZE_COMPLETE && loops < 2000) {
2956640d 133 msleep(1);
3d396eb1 134 /* Window 1 call */
3ce06a32 135 state = adapter->pci_read_normalize(adapter, CRB_CMDPEG_STATE);
3d396eb1
AK
136
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 */
3ce06a32
DP
146 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);
3d396eb1
AK
154
155 return err;
156}
157
2956640d 158void netxen_release_rx_buffers(struct netxen_adapter *adapter)
3d396eb1 159{
2956640d 160 struct netxen_recv_context *recv_ctx;
48bfd1e0 161 struct nx_host_rds_ring *rds_ring;
2956640d
DP
162 struct netxen_rx_buffer *rx_buf;
163 int i, ctxid, ring;
3d396eb1 164
3d396eb1 165 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
2956640d 166 recv_ctx = &adapter->recv_ctx[ctxid];
48bfd1e0
DP
167 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
168 rds_ring = &recv_ctx->rds_rings[ring];
169 for (i = 0; i < rds_ring->max_rx_desc_count; ++i) {
170 rx_buf = &(rds_ring->rx_buf_arr[i]);
2956640d
DP
171 if (rx_buf->state == NETXEN_BUFFER_FREE)
172 continue;
173 pci_unmap_single(adapter->pdev,
174 rx_buf->dma,
48bfd1e0 175 rds_ring->dma_size,
2956640d
DP
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;
48bfd1e0 219 struct nx_host_rds_ring *rds_ring;
2956640d
DP
220 int ctx, ring;
221
222 for (ctx = 0; ctx < MAX_RCV_CTX; ctx++) {
223 recv_ctx = &adapter->recv_ctx[ctx];
48bfd1e0
DP
224 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
225 rds_ring = &recv_ctx->rds_rings[ring];
226 if (rds_ring->rx_buf_arr) {
227 vfree(rds_ring->rx_buf_arr);
228 rds_ring->rx_buf_arr = NULL;
2956640d
DP
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;
48bfd1e0 240 struct nx_host_rds_ring *rds_ring;
2956640d
DP
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];
48bfd1e0
DP
258 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
259 rds_ring = &recv_ctx->rds_rings[ring];
2956640d
DP
260 switch (RCV_DESC_TYPE(ring)) {
261 case RCV_DESC_NORMAL:
48bfd1e0 262 rds_ring->max_rx_desc_count =
2956640d 263 adapter->max_rx_desc_count;
48bfd1e0 264 rds_ring->flags = RCV_DESC_NORMAL;
d9e651bc
DP
265 if (adapter->ahw.cut_through) {
266 rds_ring->dma_size =
267 NX_CT_DEFAULT_RX_BUF_LEN;
268 rds_ring->skb_size =
269 NX_CT_DEFAULT_RX_BUF_LEN;
270 } else {
271 rds_ring->dma_size = RX_DMA_MAP_LEN;
272 rds_ring->skb_size =
273 MAX_RX_BUFFER_LENGTH;
274 }
2956640d
DP
275 break;
276
277 case RCV_DESC_JUMBO:
48bfd1e0 278 rds_ring->max_rx_desc_count =
2956640d 279 adapter->max_jumbo_rx_desc_count;
48bfd1e0 280 rds_ring->flags = RCV_DESC_JUMBO;
d9e651bc
DP
281 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
282 rds_ring->dma_size =
283 NX_P3_RX_JUMBO_BUF_MAX_LEN;
284 else
285 rds_ring->dma_size =
286 NX_P2_RX_JUMBO_BUF_MAX_LEN;
48bfd1e0 287 rds_ring->skb_size =
d9e651bc 288 rds_ring->dma_size + NET_IP_ALIGN;
2956640d
DP
289 break;
290
291 case RCV_RING_LRO:
48bfd1e0 292 rds_ring->max_rx_desc_count =
2956640d 293 adapter->max_lro_rx_desc_count;
48bfd1e0
DP
294 rds_ring->flags = RCV_DESC_LRO;
295 rds_ring->dma_size = RX_LRO_DMA_MAP_LEN;
296 rds_ring->skb_size = MAX_RX_LRO_BUFFER_LENGTH;
2956640d
DP
297 break;
298
299 }
48bfd1e0 300 rds_ring->rx_buf_arr = (struct netxen_rx_buffer *)
2956640d 301 vmalloc(RCV_BUFFSIZE);
48bfd1e0 302 if (rds_ring->rx_buf_arr == NULL) {
2956640d
DP
303 printk(KERN_ERR "%s: Failed to allocate "
304 "rx buffer ring %d\n",
305 netdev->name, ring);
306 /* free whatever was already allocated */
307 goto err_out;
308 }
48bfd1e0 309 memset(rds_ring->rx_buf_arr, 0, RCV_BUFFSIZE);
d9e651bc 310 INIT_LIST_HEAD(&rds_ring->free_list);
3d396eb1
AK
311 /*
312 * Now go through all of them, set reference handles
313 * and put them in the queues.
314 */
48bfd1e0
DP
315 num_rx_bufs = rds_ring->max_rx_desc_count;
316 rx_buf = rds_ring->rx_buf_arr;
3d396eb1 317 for (i = 0; i < num_rx_bufs; i++) {
d9e651bc
DP
318 list_add_tail(&rx_buf->list,
319 &rds_ring->free_list);
3d396eb1
AK
320 rx_buf->ref_handle = i;
321 rx_buf->state = NETXEN_BUFFER_FREE;
3d396eb1
AK
322 rx_buf++;
323 }
324 }
325 }
2956640d
DP
326
327 return 0;
328
329err_out:
330 netxen_free_sw_resources(adapter);
331 return -ENOMEM;
3d396eb1
AK
332}
333
3d396eb1
AK
334void netxen_initialize_adapter_ops(struct netxen_adapter *adapter)
335{
3d396eb1
AK
336 switch (adapter->ahw.board_type) {
337 case NETXEN_NIC_GBE:
80922fbc 338 adapter->enable_phy_interrupts =
3d396eb1 339 netxen_niu_gbe_enable_phy_interrupts;
80922fbc 340 adapter->disable_phy_interrupts =
3d396eb1 341 netxen_niu_gbe_disable_phy_interrupts;
80922fbc
AK
342 adapter->macaddr_set = netxen_niu_macaddr_set;
343 adapter->set_mtu = netxen_nic_set_mtu_gb;
344 adapter->set_promisc = netxen_niu_set_promiscuous_mode;
80922fbc
AK
345 adapter->phy_read = netxen_niu_gbe_phy_read;
346 adapter->phy_write = netxen_niu_gbe_phy_write;
c9fc891f 347 adapter->init_port = netxen_niu_gbe_init_port;
80922fbc 348 adapter->stop_port = netxen_niu_disable_gbe_port;
3d396eb1
AK
349 break;
350
351 case NETXEN_NIC_XGBE:
80922fbc 352 adapter->enable_phy_interrupts =
3d396eb1 353 netxen_niu_xgbe_enable_phy_interrupts;
80922fbc 354 adapter->disable_phy_interrupts =
3d396eb1 355 netxen_niu_xgbe_disable_phy_interrupts;
80922fbc
AK
356 adapter->macaddr_set = netxen_niu_xg_macaddr_set;
357 adapter->set_mtu = netxen_nic_set_mtu_xgb;
358 adapter->init_port = netxen_niu_xg_init_port;
359 adapter->set_promisc = netxen_niu_xg_set_promiscuous_mode;
80922fbc 360 adapter->stop_port = netxen_niu_disable_xg_port;
3d396eb1
AK
361 break;
362
363 default:
364 break;
365 }
9ad27643
DP
366
367 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
368 adapter->set_mtu = nx_fw_cmd_set_mtu;
369 adapter->set_promisc = netxen_p3_nic_set_promisc;
370 }
3d396eb1
AK
371}
372
373/*
374 * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
375 * address to external PCI CRB address.
376 */
993fb90c 377static u32 netxen_decode_crb_addr(u32 addr)
3d396eb1
AK
378{
379 int i;
e0e20a1a 380 u32 base_addr, offset, pci_base;
3d396eb1
AK
381
382 crb_addr_transform_setup();
383
384 pci_base = NETXEN_ADDR_ERROR;
385 base_addr = addr & 0xfff00000;
386 offset = addr & 0x000fffff;
387
388 for (i = 0; i < NETXEN_MAX_CRB_XFORM; i++) {
389 if (crb_addr_xform[i] == base_addr) {
390 pci_base = i << 20;
391 break;
392 }
393 }
394 if (pci_base == NETXEN_ADDR_ERROR)
395 return pci_base;
396 else
397 return (pci_base + offset);
398}
399
13ba9c77
MT
400static long rom_max_timeout = 100;
401static long rom_lock_timeout = 10000;
7830b22c 402#if 0
27d2ab54 403static long rom_write_timeout = 700;
7830b22c 404#endif
3d396eb1 405
993fb90c 406static int rom_lock(struct netxen_adapter *adapter)
3d396eb1
AK
407{
408 int iter;
409 u32 done = 0;
410 int timeout = 0;
411
412 while (!done) {
413 /* acquire semaphore2 from PCI HW block */
414 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_LOCK),
415 &done);
416 if (done == 1)
417 break;
418 if (timeout >= rom_lock_timeout)
419 return -EIO;
420
421 timeout++;
422 /*
423 * Yield CPU
424 */
425 if (!in_atomic())
426 schedule();
427 else {
428 for (iter = 0; iter < 20; iter++)
429 cpu_relax(); /*This a nop instr on i386 */
430 }
431 }
432 netxen_nic_reg_write(adapter, NETXEN_ROM_LOCK_ID, ROM_LOCK_DRIVER);
433 return 0;
434}
435
993fb90c 436static int netxen_wait_rom_done(struct netxen_adapter *adapter)
3d396eb1
AK
437{
438 long timeout = 0;
439 long done = 0;
440
27c915a4
DP
441 cond_resched();
442
3d396eb1
AK
443 while (done == 0) {
444 done = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_GLB_STATUS);
445 done &= 2;
446 timeout++;
447 if (timeout >= rom_max_timeout) {
448 printk("Timeout reached waiting for rom done");
449 return -EIO;
450 }
451 }
452 return 0;
453}
454
7830b22c 455#if 0
993fb90c 456static int netxen_rom_wren(struct netxen_adapter *adapter)
cb8011ad
AK
457{
458 /* Set write enable latch in ROM status register */
459 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
460 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
461 M25P_INSTR_WREN);
462 if (netxen_wait_rom_done(adapter)) {
463 return -1;
464 }
465 return 0;
466}
467
993fb90c
AB
468static unsigned int netxen_rdcrbreg(struct netxen_adapter *adapter,
469 unsigned int addr)
cb8011ad
AK
470{
471 unsigned int data = 0xdeaddead;
472 data = netxen_nic_reg_read(adapter, addr);
473 return data;
474}
475
993fb90c 476static int netxen_do_rom_rdsr(struct netxen_adapter *adapter)
cb8011ad
AK
477{
478 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
479 M25P_INSTR_RDSR);
480 if (netxen_wait_rom_done(adapter)) {
481 return -1;
482 }
483 return netxen_rdcrbreg(adapter, NETXEN_ROMUSB_ROM_RDATA);
484}
7830b22c 485#endif
cb8011ad 486
993fb90c 487static void netxen_rom_unlock(struct netxen_adapter *adapter)
cb8011ad
AK
488{
489 u32 val;
490
491 /* release semaphore2 */
492 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_UNLOCK), &val);
493
494}
495
7830b22c 496#if 0
993fb90c 497static int netxen_rom_wip_poll(struct netxen_adapter *adapter)
cb8011ad
AK
498{
499 long timeout = 0;
500 long wip = 1;
501 int val;
502 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
503 while (wip != 0) {
504 val = netxen_do_rom_rdsr(adapter);
505 wip = val & 1;
506 timeout++;
507 if (timeout > rom_max_timeout) {
508 return -1;
509 }
510 }
511 return 0;
512}
513
993fb90c
AB
514static int do_rom_fast_write(struct netxen_adapter *adapter, int addr,
515 int data)
cb8011ad
AK
516{
517 if (netxen_rom_wren(adapter)) {
518 return -1;
519 }
520 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
521 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
522 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
523 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
524 M25P_INSTR_PP);
525 if (netxen_wait_rom_done(adapter)) {
526 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
527 return -1;
528 }
529
530 return netxen_rom_wip_poll(adapter);
531}
7830b22c 532#endif
cb8011ad 533
993fb90c
AB
534static int do_rom_fast_read(struct netxen_adapter *adapter,
535 int addr, int *valp)
3d396eb1
AK
536{
537 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
3d396eb1 538 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
27c915a4 539 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
3d396eb1
AK
540 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0xb);
541 if (netxen_wait_rom_done(adapter)) {
542 printk("Error waiting for rom done\n");
543 return -EIO;
544 }
545 /* reset abyte_cnt and dummy_byte_cnt */
546 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
27c915a4 547 udelay(10);
3d396eb1
AK
548 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
549
550 *valp = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_ROM_RDATA);
551 return 0;
552}
553
993fb90c
AB
554static int do_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
555 u8 *bytes, size_t size)
27d2ab54
AK
556{
557 int addridx;
558 int ret = 0;
559
560 for (addridx = addr; addridx < (addr + size); addridx += 4) {
f305f789
AV
561 int v;
562 ret = do_rom_fast_read(adapter, addridx, &v);
27d2ab54
AK
563 if (ret != 0)
564 break;
f305f789 565 *(__le32 *)bytes = cpu_to_le32(v);
27d2ab54
AK
566 bytes += 4;
567 }
568
569 return ret;
570}
571
572int
4790654c 573netxen_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
27d2ab54
AK
574 u8 *bytes, size_t size)
575{
576 int ret;
577
578 ret = rom_lock(adapter);
579 if (ret < 0)
580 return ret;
581
582 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
583
584 netxen_rom_unlock(adapter);
585 return ret;
586}
587
3d396eb1
AK
588int netxen_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
589{
590 int ret;
591
592 if (rom_lock(adapter) != 0)
593 return -EIO;
594
595 ret = do_rom_fast_read(adapter, addr, valp);
cb8011ad
AK
596 netxen_rom_unlock(adapter);
597 return ret;
598}
599
993fb90c 600#if 0
cb8011ad
AK
601int netxen_rom_fast_write(struct netxen_adapter *adapter, int addr, int data)
602{
603 int ret = 0;
604
605 if (rom_lock(adapter) != 0) {
606 return -1;
607 }
608 ret = do_rom_fast_write(adapter, addr, data);
609 netxen_rom_unlock(adapter);
610 return ret;
611}
27d2ab54 612
4790654c 613static int do_rom_fast_write_words(struct netxen_adapter *adapter,
993fb90c 614 int addr, u8 *bytes, size_t size)
27d2ab54
AK
615{
616 int addridx = addr;
617 int ret = 0;
618
619 while (addridx < (addr + size)) {
620 int last_attempt = 0;
621 int timeout = 0;
622 int data;
623
f305f789 624 data = le32_to_cpu((*(__le32*)bytes));
27d2ab54
AK
625 ret = do_rom_fast_write(adapter, addridx, data);
626 if (ret < 0)
627 return ret;
4790654c 628
27d2ab54
AK
629 while(1) {
630 int data1;
631
f8dfdd5c
SH
632 ret = do_rom_fast_read(adapter, addridx, &data1);
633 if (ret < 0)
634 return ret;
635
27d2ab54
AK
636 if (data1 == data)
637 break;
638
639 if (timeout++ >= rom_write_timeout) {
640 if (last_attempt++ < 4) {
4790654c 641 ret = do_rom_fast_write(adapter,
27d2ab54
AK
642 addridx, data);
643 if (ret < 0)
644 return ret;
645 }
646 else {
647 printk(KERN_INFO "Data write did not "
648 "succeed at address 0x%x\n", addridx);
649 break;
650 }
651 }
652 }
653
654 bytes += 4;
655 addridx += 4;
656 }
657
658 return ret;
659}
660
4790654c 661int netxen_rom_fast_write_words(struct netxen_adapter *adapter, int addr,
27d2ab54
AK
662 u8 *bytes, size_t size)
663{
664 int ret = 0;
665
666 ret = rom_lock(adapter);
667 if (ret < 0)
668 return ret;
669
670 ret = do_rom_fast_write_words(adapter, addr, bytes, size);
671 netxen_rom_unlock(adapter);
672
673 return ret;
674}
675
993fb90c 676static int netxen_rom_wrsr(struct netxen_adapter *adapter, int data)
27d2ab54
AK
677{
678 int ret;
679
680 ret = netxen_rom_wren(adapter);
681 if (ret < 0)
682 return ret;
683
684 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
4790654c 685 netxen_crb_writelit_adapter(adapter,
27d2ab54
AK
686 NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0x1);
687
688 ret = netxen_wait_rom_done(adapter);
689 if (ret < 0)
690 return ret;
691
692 return netxen_rom_wip_poll(adapter);
693}
694
993fb90c 695static int netxen_rom_rdsr(struct netxen_adapter *adapter)
27d2ab54
AK
696{
697 int ret;
698
699 ret = rom_lock(adapter);
700 if (ret < 0)
701 return ret;
702
703 ret = netxen_do_rom_rdsr(adapter);
704 netxen_rom_unlock(adapter);
705 return ret;
706}
707
708int netxen_backup_crbinit(struct netxen_adapter *adapter)
709{
710 int ret = FLASH_SUCCESS;
711 int val;
0d04761d 712 char *buffer = kmalloc(NETXEN_FLASH_SECTOR_SIZE, GFP_KERNEL);
27d2ab54
AK
713
714 if (!buffer)
4790654c 715 return -ENOMEM;
27d2ab54
AK
716 /* unlock sector 63 */
717 val = netxen_rom_rdsr(adapter);
718 val = val & 0xe3;
719 ret = netxen_rom_wrsr(adapter, val);
720 if (ret != FLASH_SUCCESS)
721 goto out_kfree;
722
723 ret = netxen_rom_wip_poll(adapter);
724 if (ret != FLASH_SUCCESS)
725 goto out_kfree;
726
727 /* copy sector 0 to sector 63 */
4790654c 728 ret = netxen_rom_fast_read_words(adapter, NETXEN_CRBINIT_START,
0d04761d 729 buffer, NETXEN_FLASH_SECTOR_SIZE);
27d2ab54
AK
730 if (ret != FLASH_SUCCESS)
731 goto out_kfree;
732
4790654c 733 ret = netxen_rom_fast_write_words(adapter, NETXEN_FIXED_START,
0d04761d 734 buffer, NETXEN_FLASH_SECTOR_SIZE);
27d2ab54
AK
735 if (ret != FLASH_SUCCESS)
736 goto out_kfree;
737
738 /* lock sector 63 */
739 val = netxen_rom_rdsr(adapter);
740 if (!(val & 0x8)) {
741 val |= (0x1 << 2);
742 /* lock sector 63 */
743 if (netxen_rom_wrsr(adapter, val) == 0) {
744 ret = netxen_rom_wip_poll(adapter);
745 if (ret != FLASH_SUCCESS)
746 goto out_kfree;
747
748 /* lock SR writes */
749 ret = netxen_rom_wip_poll(adapter);
750 if (ret != FLASH_SUCCESS)
751 goto out_kfree;
752 }
753 }
754
755out_kfree:
756 kfree(buffer);
757 return ret;
758}
759
993fb90c 760static int netxen_do_rom_se(struct netxen_adapter *adapter, int addr)
cb8011ad
AK
761{
762 netxen_rom_wren(adapter);
763 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
764 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
765 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
766 M25P_INSTR_SE);
767 if (netxen_wait_rom_done(adapter)) {
768 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
769 return -1;
770 }
771 return netxen_rom_wip_poll(adapter);
772}
773
993fb90c 774static void check_erased_flash(struct netxen_adapter *adapter, int addr)
27d2ab54
AK
775{
776 int i;
777 int val;
778 int count = 0, erased_errors = 0;
779 int range;
780
4790654c 781 range = (addr == NETXEN_USER_START) ?
0d04761d 782 NETXEN_FIXED_START : addr + NETXEN_FLASH_SECTOR_SIZE;
4790654c 783
27d2ab54
AK
784 for (i = addr; i < range; i += 4) {
785 netxen_rom_fast_read(adapter, i, &val);
786 if (val != 0xffffffff)
787 erased_errors++;
788 count++;
789 }
790
791 if (erased_errors)
792 printk(KERN_INFO "0x%x out of 0x%x words fail to be erased "
793 "for sector address: %x\n", erased_errors, count, addr);
794}
795
cb8011ad
AK
796int netxen_rom_se(struct netxen_adapter *adapter, int addr)
797{
798 int ret = 0;
799 if (rom_lock(adapter) != 0) {
800 return -1;
801 }
802 ret = netxen_do_rom_se(adapter, addr);
803 netxen_rom_unlock(adapter);
27d2ab54
AK
804 msleep(30);
805 check_erased_flash(adapter, addr);
806
807 return ret;
808}
809
993fb90c
AB
810static int netxen_flash_erase_sections(struct netxen_adapter *adapter,
811 int start, int end)
27d2ab54
AK
812{
813 int ret = FLASH_SUCCESS;
814 int i;
815
816 for (i = start; i < end; i++) {
0d04761d 817 ret = netxen_rom_se(adapter, i * NETXEN_FLASH_SECTOR_SIZE);
27d2ab54
AK
818 if (ret)
819 break;
820 ret = netxen_rom_wip_poll(adapter);
821 if (ret < 0)
822 return ret;
823 }
824
825 return ret;
826}
827
828int
829netxen_flash_erase_secondary(struct netxen_adapter *adapter)
830{
831 int ret = FLASH_SUCCESS;
832 int start, end;
833
0d04761d
MT
834 start = NETXEN_SECONDARY_START / NETXEN_FLASH_SECTOR_SIZE;
835 end = NETXEN_USER_START / NETXEN_FLASH_SECTOR_SIZE;
27d2ab54
AK
836 ret = netxen_flash_erase_sections(adapter, start, end);
837
838 return ret;
839}
840
841int
842netxen_flash_erase_primary(struct netxen_adapter *adapter)
843{
844 int ret = FLASH_SUCCESS;
845 int start, end;
846
0d04761d
MT
847 start = NETXEN_PRIMARY_START / NETXEN_FLASH_SECTOR_SIZE;
848 end = NETXEN_SECONDARY_START / NETXEN_FLASH_SECTOR_SIZE;
27d2ab54
AK
849 ret = netxen_flash_erase_sections(adapter, start, end);
850
851 return ret;
852}
853
e45d9ab4
AK
854void netxen_halt_pegs(struct netxen_adapter *adapter)
855{
856 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c, 1);
857 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c, 1);
858 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c, 1);
859 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c, 1);
860}
861
27d2ab54
AK
862int netxen_flash_unlock(struct netxen_adapter *adapter)
863{
864 int ret = 0;
865
866 ret = netxen_rom_wrsr(adapter, 0);
867 if (ret < 0)
868 return ret;
869
870 ret = netxen_rom_wren(adapter);
871 if (ret < 0)
872 return ret;
873
3d396eb1
AK
874 return ret;
875}
7830b22c 876#endif /* 0 */
3d396eb1
AK
877
878#define NETXEN_BOARDTYPE 0x4008
879#define NETXEN_BOARDNUM 0x400c
880#define NETXEN_CHIPNUM 0x4010
3d396eb1
AK
881
882int netxen_pinit_from_rom(struct netxen_adapter *adapter, int verbose)
883{
dcd56fdb 884 int addr, val;
27c915a4 885 int i, n, init_delay = 0;
3d396eb1 886 struct crb_addr_pair *buf;
27c915a4 887 unsigned offset;
e0e20a1a 888 u32 off;
3d396eb1
AK
889
890 /* resetall */
27c915a4 891 rom_lock(adapter);
3d396eb1 892 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
2956640d 893 0xffffffff);
27c915a4 894 netxen_rom_unlock(adapter);
3d396eb1
AK
895
896 if (verbose) {
3d396eb1
AK
897 if (netxen_rom_fast_read(adapter, NETXEN_BOARDTYPE, &val) == 0)
898 printk("P2 ROM board type: 0x%08x\n", val);
899 else
900 printk("Could not read board type\n");
901 if (netxen_rom_fast_read(adapter, NETXEN_BOARDNUM, &val) == 0)
902 printk("P2 ROM board num: 0x%08x\n", val);
903 else
904 printk("Could not read board number\n");
905 if (netxen_rom_fast_read(adapter, NETXEN_CHIPNUM, &val) == 0)
906 printk("P2 ROM chip num: 0x%08x\n", val);
907 else
908 printk("Could not read chip number\n");
909 }
910
2956640d
DP
911 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
912 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
27c915a4 913 (n != 0xcafecafe) ||
2956640d
DP
914 netxen_rom_fast_read(adapter, 4, &n) != 0) {
915 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
916 "n: %08x\n", netxen_nic_driver_name, n);
3d396eb1
AK
917 return -EIO;
918 }
2956640d
DP
919 offset = n & 0xffffU;
920 n = (n >> 16) & 0xffffU;
921 } else {
922 if (netxen_rom_fast_read(adapter, 0, &n) != 0 ||
923 !(n & 0x80000000)) {
924 printk(KERN_ERR "%s: ERROR Reading crb_init area: "
925 "n: %08x\n", netxen_nic_driver_name, n);
926 return -EIO;
3d396eb1 927 }
2956640d
DP
928 offset = 1;
929 n &= ~0x80000000;
930 }
931
932 if (n < 1024) {
933 if (verbose)
934 printk(KERN_DEBUG "%s: %d CRB init values found"
935 " in ROM.\n", netxen_nic_driver_name, n);
936 } else {
937 printk(KERN_ERR "%s:n=0x%x Error! NetXen card flash not"
938 " initialized.\n", __func__, n);
939 return -EIO;
940 }
3d396eb1 941
2956640d
DP
942 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
943 if (buf == NULL) {
944 printk("%s: netxen_pinit_from_rom: Unable to calloc memory.\n",
945 netxen_nic_driver_name);
946 return -ENOMEM;
947 }
948 for (i = 0; i < n; i++) {
949 if (netxen_rom_fast_read(adapter, 8*i + 4*offset, &val) != 0 ||
950 netxen_rom_fast_read(adapter, 8*i + 4*offset + 4, &addr) != 0)
951 return -EIO;
952
953 buf[i].addr = addr;
954 buf[i].data = val;
955
956 if (verbose)
957 printk(KERN_DEBUG "%s: PCI: 0x%08x == 0x%08x\n",
958 netxen_nic_driver_name,
959 (u32)netxen_decode_crb_addr(addr), val);
960 }
961 for (i = 0; i < n; i++) {
962
963 off = netxen_decode_crb_addr(buf[i].addr);
964 if (off == NETXEN_ADDR_ERROR) {
965 printk(KERN_ERR"CRB init value out of range %x\n",
1fcca1a5 966 buf[i].addr);
2956640d
DP
967 continue;
968 }
969 off += NETXEN_PCI_CRBSPACE;
970 /* skipping cold reboot MAGIC */
971 if (off == NETXEN_CAM_RAM(0x1fc))
972 continue;
973
974 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
975 /* do not reset PCI */
976 if (off == (ROMUSB_GLB + 0xbc))
1fcca1a5 977 continue;
27c915a4
DP
978 if (off == (ROMUSB_GLB + 0xa8))
979 continue;
980 if (off == (ROMUSB_GLB + 0xc8)) /* core clock */
981 continue;
982 if (off == (ROMUSB_GLB + 0x24)) /* MN clock */
983 continue;
984 if (off == (ROMUSB_GLB + 0x1c)) /* MS clock */
985 continue;
2956640d
DP
986 if (off == (NETXEN_CRB_PEG_NET_1 + 0x18))
987 buf[i].data = 0x1020;
988 /* skip the function enable register */
989 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION))
3d396eb1 990 continue;
2956640d
DP
991 if (off == NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION2))
992 continue;
993 if ((off & 0x0ff00000) == NETXEN_CRB_SMB)
994 continue;
995 }
3d396eb1 996
2956640d
DP
997 if (off == NETXEN_ADDR_ERROR) {
998 printk(KERN_ERR "%s: Err: Unknown addr: 0x%08x\n",
999 netxen_nic_driver_name, buf[i].addr);
1000 continue;
1001 }
1002
27c915a4 1003 init_delay = 1;
2956640d
DP
1004 /* After writing this register, HW needs time for CRB */
1005 /* to quiet down (else crb_window returns 0xffffffff) */
1006 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
27c915a4 1007 init_delay = 1000;
2956640d 1008 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
3d396eb1 1009 /* hold xdma in reset also */
cb8011ad 1010 buf[i].data = NETXEN_NIC_XDMA_RESET;
27c915a4 1011 buf[i].data = 0x8000ff;
3d396eb1 1012 }
2956640d 1013 }
3d396eb1 1014
2956640d 1015 adapter->hw_write_wx(adapter, off, &buf[i].data, 4);
3d396eb1 1016
27c915a4 1017 msleep(init_delay);
2956640d
DP
1018 }
1019 kfree(buf);
3d396eb1 1020
2956640d 1021 /* disable_peg_cache_all */
3d396eb1 1022
2956640d
DP
1023 /* unreset_net_cache */
1024 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
1025 adapter->hw_read_wx(adapter,
1026 NETXEN_ROMUSB_GLB_SW_RESET, &val, 4);
3d396eb1 1027 netxen_crb_writelit_adapter(adapter,
2956640d 1028 NETXEN_ROMUSB_GLB_SW_RESET, (val & 0xffffff0f));
3d396eb1 1029 }
2956640d
DP
1030
1031 /* p2dn replyCount */
1032 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
1033 /* disable_peg_cache 0 */
1034 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_D + 0x4c, 8);
1035 /* disable_peg_cache 1 */
1036 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_I + 0x4c, 8);
1037
1038 /* peg_clr_all */
1039
1040 /* peg_clr 0 */
1041 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x8, 0);
1042 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0xc, 0);
1043 /* peg_clr 1 */
1044 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x8, 0);
1045 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0xc, 0);
1046 /* peg_clr 2 */
1047 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x8, 0);
1048 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0xc, 0);
1049 /* peg_clr 3 */
1050 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x8, 0);
1051 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0xc, 0);
3d396eb1
AK
1052 return 0;
1053}
1054
ed25ffa1
AK
1055int netxen_initialize_adapter_offload(struct netxen_adapter *adapter)
1056{
1057 uint64_t addr;
1058 uint32_t hi;
1059 uint32_t lo;
1060
1061 adapter->dummy_dma.addr =
7830b22c 1062 pci_alloc_consistent(adapter->pdev,
ed25ffa1
AK
1063 NETXEN_HOST_DUMMY_DMA_SIZE,
1064 &adapter->dummy_dma.phys_addr);
1065 if (adapter->dummy_dma.addr == NULL) {
1066 printk("%s: ERROR: Could not allocate dummy DMA memory\n",
2956640d 1067 __func__);
ed25ffa1
AK
1068 return -ENOMEM;
1069 }
1070
1071 addr = (uint64_t) adapter->dummy_dma.phys_addr;
1072 hi = (addr >> 32) & 0xffffffff;
1073 lo = addr & 0xffffffff;
1074
3ce06a32
DP
1075 adapter->pci_write_normalize(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI, hi);
1076 adapter->pci_write_normalize(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO, lo);
ed25ffa1 1077
2956640d
DP
1078 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
1079 uint32_t temp = 0;
1080 adapter->hw_write_wx(adapter, CRB_HOST_DUMMY_BUF, &temp, 4);
1081 }
1082
ed25ffa1
AK
1083 return 0;
1084}
1085
1086void netxen_free_adapter_offload(struct netxen_adapter *adapter)
1087{
15eef1e1
DP
1088 int i = 100;
1089
1090 if (!adapter->dummy_dma.addr)
1091 return;
439b454e 1092
15eef1e1 1093 if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
439b454e
DP
1094 do {
1095 if (dma_watchdog_shutdown_request(adapter) == 1)
1096 break;
1097 msleep(50);
1098 if (dma_watchdog_shutdown_poll_result(adapter) == 1)
1099 break;
1100 } while (--i);
15eef1e1 1101 }
439b454e 1102
15eef1e1
DP
1103 if (i) {
1104 pci_free_consistent(adapter->pdev,
1105 NETXEN_HOST_DUMMY_DMA_SIZE,
1106 adapter->dummy_dma.addr,
1107 adapter->dummy_dma.phys_addr);
1108 adapter->dummy_dma.addr = NULL;
1109 } else {
1110 printk(KERN_ERR "%s: dma_watchdog_shutdown failed\n",
1111 adapter->netdev->name);
ed25ffa1
AK
1112 }
1113}
1114
96acb6eb 1115int netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
3d396eb1
AK
1116{
1117 u32 val = 0;
2956640d 1118 int retries = 60;
3d396eb1 1119
cb8011ad 1120 if (!pegtune_val) {
96acb6eb 1121 do {
3ce06a32
DP
1122 val = adapter->pci_read_normalize(adapter,
1123 CRB_CMDPEG_STATE);
96acb6eb
DP
1124
1125 if (val == PHAN_INITIALIZE_COMPLETE ||
1126 val == PHAN_INITIALIZE_ACK)
1127 return 0;
1128
2956640d
DP
1129 msleep(500);
1130
96acb6eb 1131 } while (--retries);
2956640d 1132
96acb6eb 1133 if (!retries) {
2956640d
DP
1134 pegtune_val = adapter->pci_read_normalize(adapter,
1135 NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
96acb6eb
DP
1136 printk(KERN_WARNING "netxen_phantom_init: init failed, "
1137 "pegtune_val=%x\n", pegtune_val);
1138 return -1;
3d396eb1 1139 }
3d396eb1 1140 }
96acb6eb
DP
1141
1142 return 0;
3d396eb1
AK
1143}
1144
2956640d
DP
1145int netxen_receive_peg_ready(struct netxen_adapter *adapter)
1146{
1147 u32 val = 0;
1148 int retries = 2000;
1149
1150 do {
1151 val = adapter->pci_read_normalize(adapter, CRB_RCVPEG_STATE);
1152
1153 if (val == PHAN_PEG_RCV_INITIALIZED)
1154 return 0;
1155
1156 msleep(10);
1157
1158 } while (--retries);
1159
1160 if (!retries) {
1161 printk(KERN_ERR "Receive Peg initialization not "
1162 "complete, state: 0x%x.\n", val);
1163 return -EIO;
1164 }
1165
1166 return 0;
1167}
1168
d9e651bc
DP
1169static struct sk_buff *netxen_process_rxbuf(struct netxen_adapter *adapter,
1170 struct nx_host_rds_ring *rds_ring, u16 index, u16 cksum)
1171{
1172 struct netxen_rx_buffer *buffer;
1173 struct sk_buff *skb;
1174
1175 buffer = &rds_ring->rx_buf_arr[index];
1176
1177 pci_unmap_single(adapter->pdev, buffer->dma, rds_ring->dma_size,
1178 PCI_DMA_FROMDEVICE);
1179
1180 skb = buffer->skb;
1181 if (!skb)
1182 goto no_skb;
1183
1184 if (likely(adapter->rx_csum && cksum == STATUS_CKSUM_OK)) {
1185 adapter->stats.csummed++;
1186 skb->ip_summed = CHECKSUM_UNNECESSARY;
1187 } else
1188 skb->ip_summed = CHECKSUM_NONE;
1189
1190 skb->dev = adapter->netdev;
1191
1192 buffer->skb = NULL;
1193
1194no_skb:
1195 buffer->state = NETXEN_BUFFER_FREE;
1196 buffer->lro_current_frags = 0;
1197 buffer->lro_expected_frags = 0;
1198 list_add_tail(&buffer->list, &rds_ring->free_list);
1199 return skb;
1200}
1201
3d396eb1
AK
1202/*
1203 * netxen_process_rcv() send the received packet to the protocol stack.
1204 * and if the number of receives exceeds RX_BUFFERS_REFILL, then we
1205 * invoke the routine to send more rx buffers to the Phantom...
1206 */
993fb90c 1207static void netxen_process_rcv(struct netxen_adapter *adapter, int ctxid,
d9e651bc 1208 struct status_desc *desc, struct status_desc *frag_desc)
3d396eb1 1209{
3176ff3e 1210 struct net_device *netdev = adapter->netdev;
5dc16268
DP
1211 u64 sts_data = le64_to_cpu(desc->status_desc_data);
1212 int index = netxen_get_sts_refhandle(sts_data);
3d396eb1
AK
1213 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1214 struct netxen_rx_buffer *buffer;
1215 struct sk_buff *skb;
5dc16268 1216 u32 length = netxen_get_sts_totallength(sts_data);
3d396eb1 1217 u32 desc_ctx;
d9e651bc 1218 u16 pkt_offset = 0, cksum;
48bfd1e0 1219 struct nx_host_rds_ring *rds_ring;
3d396eb1 1220
5dc16268 1221 desc_ctx = netxen_get_sts_type(sts_data);
3d396eb1
AK
1222 if (unlikely(desc_ctx >= NUM_RCV_DESC_RINGS)) {
1223 printk("%s: %s Bad Rcv descriptor ring\n",
1224 netxen_nic_driver_name, netdev->name);
1225 return;
1226 }
1227
48bfd1e0
DP
1228 rds_ring = &recv_ctx->rds_rings[desc_ctx];
1229 if (unlikely(index > rds_ring->max_rx_desc_count)) {
ed25ffa1 1230 DPRINTK(ERR, "Got a buffer index:%x Max is %x\n",
48bfd1e0 1231 index, rds_ring->max_rx_desc_count);
ed25ffa1
AK
1232 return;
1233 }
48bfd1e0 1234 buffer = &rds_ring->rx_buf_arr[index];
ed25ffa1
AK
1235 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1236 buffer->lro_current_frags++;
1237 if (netxen_get_sts_desc_lro_last_frag(desc)) {
1238 buffer->lro_expected_frags =
1239 netxen_get_sts_desc_lro_cnt(desc);
1240 buffer->lro_length = length;
1241 }
1242 if (buffer->lro_current_frags != buffer->lro_expected_frags) {
1243 if (buffer->lro_expected_frags != 0) {
5bc51424
JP
1244 printk("LRO: (refhandle:%x) recv frag. "
1245 "wait for last. flags: %x expected:%d "
ed25ffa1
AK
1246 "have:%d\n", index,
1247 netxen_get_sts_desc_lro_last_frag(desc),
1248 buffer->lro_expected_frags,
1249 buffer->lro_current_frags);
1250 }
1251 return;
1252 }
1253 }
3d396eb1 1254
d9e651bc 1255 cksum = netxen_get_sts_status(sts_data);
3d396eb1 1256
d9e651bc
DP
1257 skb = netxen_process_rxbuf(adapter, rds_ring, index, cksum);
1258 if (!skb)
1259 return;
200eef20 1260
ed25ffa1
AK
1261 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1262 /* True length was only available on the last pkt */
1263 skb_put(skb, buffer->lro_length);
1264 } else {
d9e651bc
DP
1265 if (length > rds_ring->skb_size)
1266 skb_put(skb, rds_ring->skb_size);
1267 else
1268 skb_put(skb, length);
1269
1270 pkt_offset = netxen_get_sts_pkt_offset(sts_data);
1271 if (pkt_offset)
1272 skb_pull(skb, pkt_offset);
ed25ffa1
AK
1273 }
1274
3d396eb1
AK
1275 skb->protocol = eth_type_trans(skb, netdev);
1276
3d396eb1 1277 /*
d9e651bc
DP
1278 * rx buffer chaining is disabled, walk and free
1279 * any spurious rx buffer chain.
3d396eb1 1280 */
d9e651bc
DP
1281 if (frag_desc) {
1282 u16 i, nr_frags = desc->nr_frags;
1283
1284 dev_kfree_skb_any(skb);
1285 for (i = 0; i < nr_frags; i++) {
2edbb454 1286 index = le16_to_cpu(frag_desc->frag_handles[i]);
d9e651bc
DP
1287 skb = netxen_process_rxbuf(adapter,
1288 rds_ring, index, cksum);
1289 if (skb)
1290 dev_kfree_skb_any(skb);
1291 }
1292 adapter->stats.rxdropped++;
1293 } else {
d9e651bc 1294 netif_receive_skb(skb);
d9e651bc
DP
1295
1296 adapter->stats.no_rcv++;
1297 adapter->stats.rxbytes += length;
1298 }
3d396eb1
AK
1299}
1300
1301/* Process Receive status ring */
1302u32 netxen_process_rcv_ring(struct netxen_adapter *adapter, int ctxid, int max)
1303{
1304 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1305 struct status_desc *desc_head = recv_ctx->rcv_status_desc_head;
d9e651bc 1306 struct status_desc *desc, *frag_desc;
3d396eb1
AK
1307 u32 consumer = recv_ctx->status_rx_consumer;
1308 int count = 0, ring;
d9e651bc
DP
1309 u64 sts_data;
1310 u16 opcode;
3d396eb1 1311
3d396eb1
AK
1312 while (count < max) {
1313 desc = &desc_head[consumer];
a608ab9c 1314 if (!(netxen_get_sts_owner(desc) & STATUS_OWNER_HOST)) {
ed25ffa1
AK
1315 DPRINTK(ERR, "desc %p ownedby %x\n", desc,
1316 netxen_get_sts_owner(desc));
3d396eb1
AK
1317 break;
1318 }
d9e651bc
DP
1319
1320 sts_data = le64_to_cpu(desc->status_desc_data);
1321 opcode = netxen_get_sts_opcode(sts_data);
1322 frag_desc = NULL;
1323 if (opcode == NETXEN_NIC_RXPKT_DESC) {
1324 if (desc->nr_frags) {
1325 consumer = get_next_index(consumer,
1326 adapter->max_rx_desc_count);
1327 frag_desc = &desc_head[consumer];
1328 netxen_set_sts_owner(frag_desc,
1329 STATUS_OWNER_PHANTOM);
1330 }
1331 }
1332
1333 netxen_process_rcv(adapter, ctxid, desc, frag_desc);
1334
a608ab9c 1335 netxen_set_sts_owner(desc, STATUS_OWNER_PHANTOM);
d9e651bc
DP
1336
1337 consumer = get_next_index(consumer,
1338 adapter->max_rx_desc_count);
3d396eb1
AK
1339 count++;
1340 }
48bfd1e0 1341 for (ring = 0; ring < adapter->max_rds_rings; ring++)
05aaa02d 1342 netxen_post_rx_buffers_nodb(adapter, ctxid, ring);
3d396eb1
AK
1343
1344 /* update the consumer index in phantom */
1345 if (count) {
3d396eb1
AK
1346 recv_ctx->status_rx_consumer = consumer;
1347
1348 /* Window = 1 */
3ce06a32
DP
1349 adapter->pci_write_normalize(adapter,
1350 recv_ctx->crb_sts_consumer, consumer);
3d396eb1
AK
1351 }
1352
1353 return count;
1354}
1355
1356/* Process Command status ring */
05aaa02d 1357int netxen_process_cmd_ring(struct netxen_adapter *adapter)
3d396eb1 1358{
ba53e6b4
DP
1359 u32 last_consumer, consumer;
1360 int count = 0, i;
3d396eb1 1361 struct netxen_cmd_buffer *buffer;
ba53e6b4
DP
1362 struct pci_dev *pdev = adapter->pdev;
1363 struct net_device *netdev = adapter->netdev;
3d396eb1 1364 struct netxen_skb_frag *frag;
ba53e6b4 1365 int done = 0;
3d396eb1 1366
3d396eb1 1367 last_consumer = adapter->last_cmd_consumer;
9b410117 1368 consumer = le32_to_cpu(*(adapter->cmd_consumer));
3d396eb1 1369
ba53e6b4 1370 while (last_consumer != consumer) {
3d396eb1 1371 buffer = &adapter->cmd_buf_arr[last_consumer];
53a01e00 1372 if (buffer->skb) {
1373 frag = &buffer->frag_array[0];
3d396eb1
AK
1374 pci_unmap_single(pdev, frag->dma, frag->length,
1375 PCI_DMA_TODEVICE);
96acb6eb 1376 frag->dma = 0ULL;
3d396eb1 1377 for (i = 1; i < buffer->frag_count; i++) {
3d396eb1
AK
1378 frag++; /* Get the next frag */
1379 pci_unmap_page(pdev, frag->dma, frag->length,
1380 PCI_DMA_TODEVICE);
96acb6eb 1381 frag->dma = 0ULL;
3d396eb1
AK
1382 }
1383
ba53e6b4 1384 adapter->stats.xmitfinished++;
53a01e00 1385 dev_kfree_skb_any(buffer->skb);
1386 buffer->skb = NULL;
3d396eb1
AK
1387 }
1388
1389 last_consumer = get_next_index(last_consumer,
1390 adapter->max_tx_desc_count);
ba53e6b4
DP
1391 if (++count >= MAX_STATUS_HANDLE)
1392 break;
3d396eb1 1393 }
3d396eb1 1394
ba53e6b4 1395 if (count) {
3d396eb1 1396 adapter->last_cmd_consumer = last_consumer;
ba53e6b4
DP
1397 smp_mb();
1398 if (netif_queue_stopped(netdev) && netif_running(netdev)) {
1399 netif_tx_lock(netdev);
1400 netif_wake_queue(netdev);
1401 smp_mb();
1402 netif_tx_unlock(netdev);
3d396eb1
AK
1403 }
1404 }
ed25ffa1
AK
1405 /*
1406 * If everything is freed up to consumer then check if the ring is full
1407 * If the ring is full then check if more needs to be freed and
1408 * schedule the call back again.
1409 *
1410 * This happens when there are 2 CPUs. One could be freeing and the
1411 * other filling it. If the ring is full when we get out of here and
1412 * the card has already interrupted the host then the host can miss the
1413 * interrupt.
1414 *
1415 * There is still a possible race condition and the host could miss an
1416 * interrupt. The card has to take care of this.
1417 */
ba53e6b4
DP
1418 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1419 done = (last_consumer == consumer);
3d396eb1 1420
ed25ffa1 1421 return (done);
3d396eb1
AK
1422}
1423
1424/*
1425 * netxen_post_rx_buffers puts buffer in the Phantom memory
1426 */
1427void netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ctx, u32 ringid)
1428{
7830b22c 1429 struct pci_dev *pdev = adapter->pdev;
3d396eb1
AK
1430 struct sk_buff *skb;
1431 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
48bfd1e0 1432 struct nx_host_rds_ring *rds_ring = NULL;
ed25ffa1 1433 uint producer;
3d396eb1
AK
1434 struct rcv_desc *pdesc;
1435 struct netxen_rx_buffer *buffer;
1436 int count = 0;
ed25ffa1
AK
1437 netxen_ctx_msg msg = 0;
1438 dma_addr_t dma;
d9e651bc 1439 struct list_head *head;
3d396eb1 1440
48bfd1e0 1441 rds_ring = &recv_ctx->rds_rings[ringid];
3d396eb1 1442
48bfd1e0 1443 producer = rds_ring->producer;
d9e651bc
DP
1444 head = &rds_ring->free_list;
1445
3d396eb1 1446 /* We can start writing rx descriptors into the phantom memory. */
d9e651bc
DP
1447 while (!list_empty(head)) {
1448
48bfd1e0 1449 skb = dev_alloc_skb(rds_ring->skb_size);
3d396eb1 1450 if (unlikely(!skb)) {
3d396eb1
AK
1451 break;
1452 }
ed25ffa1 1453
6f703406
DP
1454 if (!adapter->ahw.cut_through)
1455 skb_reserve(skb, 2);
1456
1457 dma = pci_map_single(pdev, skb->data,
1458 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1459 if (pci_dma_mapping_error(pdev, dma)) {
1460 dev_kfree_skb_any(skb);
1461 break;
1462 }
1463
1464 count++;
d9e651bc
DP
1465 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
1466 list_del(&buffer->list);
1467
ed25ffa1
AK
1468 buffer->skb = skb;
1469 buffer->state = NETXEN_BUFFER_BUSY;
1470 buffer->dma = dma;
6f703406 1471
ed25ffa1 1472 /* make a rcv descriptor */
6f703406
DP
1473 pdesc = &rds_ring->desc_head[producer];
1474 pdesc->addr_buffer = cpu_to_le64(dma);
ed33ebe4 1475 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
48bfd1e0 1476 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
6f703406
DP
1477
1478 producer = get_next_index(producer, rds_ring->max_rx_desc_count);
ed25ffa1
AK
1479 }
1480 /* if we did allocate buffers, then write the count to Phantom */
1481 if (count) {
48bfd1e0 1482 rds_ring->producer = producer;
ed25ffa1 1483 /* Window = 1 */
3ce06a32 1484 adapter->pci_write_normalize(adapter,
48bfd1e0
DP
1485 rds_ring->crb_rcv_producer,
1486 (producer-1) & (rds_ring->max_rx_desc_count-1));
1487
1488 if (adapter->fw_major < 4) {
ed25ffa1
AK
1489 /*
1490 * Write a doorbell msg to tell phanmon of change in
1491 * receive ring producer
48bfd1e0 1492 * Only for firmware version < 4.0.0
ed25ffa1
AK
1493 */
1494 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1495 netxen_set_msg_privid(msg);
1496 netxen_set_msg_count(msg,
1497 ((producer -
48bfd1e0 1498 1) & (rds_ring->
ed25ffa1 1499 max_rx_desc_count - 1)));
3176ff3e 1500 netxen_set_msg_ctxid(msg, adapter->portnum);
ed25ffa1
AK
1501 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1502 writel(msg,
1503 DB_NORMALIZE(adapter,
1504 NETXEN_RCV_PRODUCER_OFFSET));
48bfd1e0 1505 }
ed25ffa1
AK
1506 }
1507}
1508
993fb90c
AB
1509static void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
1510 uint32_t ctx, uint32_t ringid)
ed25ffa1 1511{
7830b22c 1512 struct pci_dev *pdev = adapter->pdev;
ed25ffa1
AK
1513 struct sk_buff *skb;
1514 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
48bfd1e0 1515 struct nx_host_rds_ring *rds_ring = NULL;
ed25ffa1
AK
1516 u32 producer;
1517 struct rcv_desc *pdesc;
1518 struct netxen_rx_buffer *buffer;
1519 int count = 0;
d9e651bc 1520 struct list_head *head;
6f703406 1521 dma_addr_t dma;
ed25ffa1 1522
48bfd1e0 1523 rds_ring = &recv_ctx->rds_rings[ringid];
ed25ffa1 1524
48bfd1e0 1525 producer = rds_ring->producer;
d9e651bc 1526 head = &rds_ring->free_list;
ed25ffa1 1527 /* We can start writing rx descriptors into the phantom memory. */
d9e651bc
DP
1528 while (!list_empty(head)) {
1529
48bfd1e0 1530 skb = dev_alloc_skb(rds_ring->skb_size);
ed25ffa1 1531 if (unlikely(!skb)) {
ed25ffa1
AK
1532 break;
1533 }
d9e651bc 1534
6f703406
DP
1535 if (!adapter->ahw.cut_through)
1536 skb_reserve(skb, 2);
1537
1538 dma = pci_map_single(pdev, skb->data,
1539 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
1540 if (pci_dma_mapping_error(pdev, dma)) {
1541 dev_kfree_skb_any(skb);
1542 break;
1543 }
1544
1545 count++;
d9e651bc
DP
1546 buffer = list_entry(head->next, struct netxen_rx_buffer, list);
1547 list_del(&buffer->list);
1548
3d396eb1
AK
1549 buffer->skb = skb;
1550 buffer->state = NETXEN_BUFFER_BUSY;
6f703406 1551 buffer->dma = dma;
ed25ffa1 1552
3d396eb1 1553 /* make a rcv descriptor */
6f703406 1554 pdesc = &rds_ring->desc_head[producer];
ed33ebe4 1555 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
48bfd1e0 1556 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
3d396eb1 1557 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
6f703406
DP
1558
1559 producer = get_next_index(producer, rds_ring->max_rx_desc_count);
3d396eb1
AK
1560 }
1561
1562 /* if we did allocate buffers, then write the count to Phantom */
1563 if (count) {
48bfd1e0 1564 rds_ring->producer = producer;
3d396eb1 1565 /* Window = 1 */
3ce06a32 1566 adapter->pci_write_normalize(adapter,
48bfd1e0
DP
1567 rds_ring->crb_rcv_producer,
1568 (producer-1) & (rds_ring->max_rx_desc_count-1));
3d396eb1 1569 wmb();
3d396eb1
AK
1570 }
1571}
1572
3d396eb1
AK
1573void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1574{
3d396eb1 1575 memset(&adapter->stats, 0, sizeof(adapter->stats));
3176ff3e 1576 return;
3d396eb1
AK
1577}
1578