2 * Copyright (C) 2003 - 2009 NetXen, Inc.
3 * Copyright (C) 2009 - QLogic Corporation.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 * The full GNU General Public License is included in this distribution
22 * in the file called "COPYING".
26 #include <linux/netdevice.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/if_vlan.h>
30 #include "netxen_nic.h"
31 #include "netxen_nic_hw.h"
33 struct crb_addr_pair
{
38 #define NETXEN_MAX_CRB_XFORM 60
39 static unsigned int crb_addr_xform
[NETXEN_MAX_CRB_XFORM
];
40 #define NETXEN_ADDR_ERROR (0xffffffff)
42 #define crb_addr_transform(name) \
43 crb_addr_xform[NETXEN_HW_PX_MAP_CRB_##name] = \
44 NETXEN_HW_CRB_HUB_AGT_ADR_##name << 20
46 #define NETXEN_NIC_XDMA_RESET 0x8000ff
49 netxen_post_rx_buffers_nodb(struct netxen_adapter
*adapter
,
50 struct nx_host_rds_ring
*rds_ring
);
51 static int netxen_p3_has_mn(struct netxen_adapter
*adapter
);
53 static void crb_addr_transform_setup(void)
55 crb_addr_transform(XDMA
);
56 crb_addr_transform(TIMR
);
57 crb_addr_transform(SRE
);
58 crb_addr_transform(SQN3
);
59 crb_addr_transform(SQN2
);
60 crb_addr_transform(SQN1
);
61 crb_addr_transform(SQN0
);
62 crb_addr_transform(SQS3
);
63 crb_addr_transform(SQS2
);
64 crb_addr_transform(SQS1
);
65 crb_addr_transform(SQS0
);
66 crb_addr_transform(RPMX7
);
67 crb_addr_transform(RPMX6
);
68 crb_addr_transform(RPMX5
);
69 crb_addr_transform(RPMX4
);
70 crb_addr_transform(RPMX3
);
71 crb_addr_transform(RPMX2
);
72 crb_addr_transform(RPMX1
);
73 crb_addr_transform(RPMX0
);
74 crb_addr_transform(ROMUSB
);
75 crb_addr_transform(SN
);
76 crb_addr_transform(QMN
);
77 crb_addr_transform(QMS
);
78 crb_addr_transform(PGNI
);
79 crb_addr_transform(PGND
);
80 crb_addr_transform(PGN3
);
81 crb_addr_transform(PGN2
);
82 crb_addr_transform(PGN1
);
83 crb_addr_transform(PGN0
);
84 crb_addr_transform(PGSI
);
85 crb_addr_transform(PGSD
);
86 crb_addr_transform(PGS3
);
87 crb_addr_transform(PGS2
);
88 crb_addr_transform(PGS1
);
89 crb_addr_transform(PGS0
);
90 crb_addr_transform(PS
);
91 crb_addr_transform(PH
);
92 crb_addr_transform(NIU
);
93 crb_addr_transform(I2Q
);
94 crb_addr_transform(EG
);
95 crb_addr_transform(MN
);
96 crb_addr_transform(MS
);
97 crb_addr_transform(CAS2
);
98 crb_addr_transform(CAS1
);
99 crb_addr_transform(CAS0
);
100 crb_addr_transform(CAM
);
101 crb_addr_transform(C2C1
);
102 crb_addr_transform(C2C0
);
103 crb_addr_transform(SMB
);
104 crb_addr_transform(OCM0
);
105 crb_addr_transform(I2C0
);
108 void netxen_release_rx_buffers(struct netxen_adapter
*adapter
)
110 struct netxen_recv_context
*recv_ctx
;
111 struct nx_host_rds_ring
*rds_ring
;
112 struct netxen_rx_buffer
*rx_buf
;
115 recv_ctx
= &adapter
->recv_ctx
;
116 for (ring
= 0; ring
< adapter
->max_rds_rings
; ring
++) {
117 rds_ring
= &recv_ctx
->rds_rings
[ring
];
118 for (i
= 0; i
< rds_ring
->num_desc
; ++i
) {
119 rx_buf
= &(rds_ring
->rx_buf_arr
[i
]);
120 if (rx_buf
->state
== NETXEN_BUFFER_FREE
)
122 pci_unmap_single(adapter
->pdev
,
126 if (rx_buf
->skb
!= NULL
)
127 dev_kfree_skb_any(rx_buf
->skb
);
132 void netxen_release_tx_buffers(struct netxen_adapter
*adapter
)
134 struct netxen_cmd_buffer
*cmd_buf
;
135 struct netxen_skb_frag
*buffrag
;
137 struct nx_host_tx_ring
*tx_ring
= adapter
->tx_ring
;
139 cmd_buf
= tx_ring
->cmd_buf_arr
;
140 for (i
= 0; i
< tx_ring
->num_desc
; i
++) {
141 buffrag
= cmd_buf
->frag_array
;
143 pci_unmap_single(adapter
->pdev
, buffrag
->dma
,
144 buffrag
->length
, PCI_DMA_TODEVICE
);
147 for (j
= 0; j
< cmd_buf
->frag_count
; j
++) {
150 pci_unmap_page(adapter
->pdev
, buffrag
->dma
,
157 dev_kfree_skb_any(cmd_buf
->skb
);
164 void netxen_free_sw_resources(struct netxen_adapter
*adapter
)
166 struct netxen_recv_context
*recv_ctx
;
167 struct nx_host_rds_ring
*rds_ring
;
168 struct nx_host_tx_ring
*tx_ring
;
171 recv_ctx
= &adapter
->recv_ctx
;
173 if (recv_ctx
->rds_rings
== NULL
)
176 for (ring
= 0; ring
< adapter
->max_rds_rings
; ring
++) {
177 rds_ring
= &recv_ctx
->rds_rings
[ring
];
178 vfree(rds_ring
->rx_buf_arr
);
179 rds_ring
->rx_buf_arr
= NULL
;
181 kfree(recv_ctx
->rds_rings
);
184 if (adapter
->tx_ring
== NULL
)
187 tx_ring
= adapter
->tx_ring
;
188 vfree(tx_ring
->cmd_buf_arr
);
190 adapter
->tx_ring
= NULL
;
193 int netxen_alloc_sw_resources(struct netxen_adapter
*adapter
)
195 struct netxen_recv_context
*recv_ctx
;
196 struct nx_host_rds_ring
*rds_ring
;
197 struct nx_host_sds_ring
*sds_ring
;
198 struct nx_host_tx_ring
*tx_ring
;
199 struct netxen_rx_buffer
*rx_buf
;
202 struct netxen_cmd_buffer
*cmd_buf_arr
;
203 struct net_device
*netdev
= adapter
->netdev
;
204 struct pci_dev
*pdev
= adapter
->pdev
;
206 size
= sizeof(struct nx_host_tx_ring
);
207 tx_ring
= kzalloc(size
, GFP_KERNEL
);
208 if (tx_ring
== NULL
) {
209 dev_err(&pdev
->dev
, "%s: failed to allocate tx ring struct\n",
213 adapter
->tx_ring
= tx_ring
;
215 tx_ring
->num_desc
= adapter
->num_txd
;
216 tx_ring
->txq
= netdev_get_tx_queue(netdev
, 0);
218 cmd_buf_arr
= vzalloc(TX_BUFF_RINGSIZE(tx_ring
));
219 if (cmd_buf_arr
== NULL
) {
220 dev_err(&pdev
->dev
, "%s: failed to allocate cmd buffer ring\n",
224 tx_ring
->cmd_buf_arr
= cmd_buf_arr
;
226 recv_ctx
= &adapter
->recv_ctx
;
228 size
= adapter
->max_rds_rings
* sizeof (struct nx_host_rds_ring
);
229 rds_ring
= kzalloc(size
, GFP_KERNEL
);
230 if (rds_ring
== NULL
) {
231 dev_err(&pdev
->dev
, "%s: failed to allocate rds ring struct\n",
235 recv_ctx
->rds_rings
= rds_ring
;
237 for (ring
= 0; ring
< adapter
->max_rds_rings
; ring
++) {
238 rds_ring
= &recv_ctx
->rds_rings
[ring
];
240 case RCV_RING_NORMAL
:
241 rds_ring
->num_desc
= adapter
->num_rxd
;
242 if (adapter
->ahw
.cut_through
) {
244 NX_CT_DEFAULT_RX_BUF_LEN
;
246 NX_CT_DEFAULT_RX_BUF_LEN
;
248 if (NX_IS_REVISION_P3(adapter
->ahw
.revision_id
))
250 NX_P3_RX_BUF_MAX_LEN
;
253 NX_P2_RX_BUF_MAX_LEN
;
255 rds_ring
->dma_size
+ NET_IP_ALIGN
;
260 rds_ring
->num_desc
= adapter
->num_jumbo_rxd
;
261 if (NX_IS_REVISION_P3(adapter
->ahw
.revision_id
))
263 NX_P3_RX_JUMBO_BUF_MAX_LEN
;
266 NX_P2_RX_JUMBO_BUF_MAX_LEN
;
268 if (adapter
->capabilities
& NX_CAP0_HW_LRO
)
269 rds_ring
->dma_size
+= NX_LRO_BUFFER_EXTRA
;
272 rds_ring
->dma_size
+ NET_IP_ALIGN
;
276 rds_ring
->num_desc
= adapter
->num_lro_rxd
;
277 rds_ring
->dma_size
= NX_RX_LRO_BUFFER_LENGTH
;
278 rds_ring
->skb_size
= rds_ring
->dma_size
+ NET_IP_ALIGN
;
282 rds_ring
->rx_buf_arr
= vzalloc(RCV_BUFF_RINGSIZE(rds_ring
));
283 if (rds_ring
->rx_buf_arr
== NULL
) {
284 printk(KERN_ERR
"%s: Failed to allocate "
285 "rx buffer ring %d\n",
287 /* free whatever was already allocated */
290 INIT_LIST_HEAD(&rds_ring
->free_list
);
292 * Now go through all of them, set reference handles
293 * and put them in the queues.
295 rx_buf
= rds_ring
->rx_buf_arr
;
296 for (i
= 0; i
< rds_ring
->num_desc
; i
++) {
297 list_add_tail(&rx_buf
->list
,
298 &rds_ring
->free_list
);
299 rx_buf
->ref_handle
= i
;
300 rx_buf
->state
= NETXEN_BUFFER_FREE
;
303 spin_lock_init(&rds_ring
->lock
);
306 for (ring
= 0; ring
< adapter
->max_sds_rings
; ring
++) {
307 sds_ring
= &recv_ctx
->sds_rings
[ring
];
308 sds_ring
->irq
= adapter
->msix_entries
[ring
].vector
;
309 sds_ring
->adapter
= adapter
;
310 sds_ring
->num_desc
= adapter
->num_rxd
;
312 for (i
= 0; i
< NUM_RCV_DESC_RINGS
; i
++)
313 INIT_LIST_HEAD(&sds_ring
->free_list
[i
]);
319 netxen_free_sw_resources(adapter
);
324 * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
325 * address to external PCI CRB address.
327 static u32
netxen_decode_crb_addr(u32 addr
)
330 u32 base_addr
, offset
, pci_base
;
332 crb_addr_transform_setup();
334 pci_base
= NETXEN_ADDR_ERROR
;
335 base_addr
= addr
& 0xfff00000;
336 offset
= addr
& 0x000fffff;
338 for (i
= 0; i
< NETXEN_MAX_CRB_XFORM
; i
++) {
339 if (crb_addr_xform
[i
] == base_addr
) {
344 if (pci_base
== NETXEN_ADDR_ERROR
)
347 return pci_base
+ offset
;
350 #define NETXEN_MAX_ROM_WAIT_USEC 100
352 static int netxen_wait_rom_done(struct netxen_adapter
*adapter
)
360 done
= NXRD32(adapter
, NETXEN_ROMUSB_GLB_STATUS
);
362 if (++timeout
>= NETXEN_MAX_ROM_WAIT_USEC
) {
363 dev_err(&adapter
->pdev
->dev
,
364 "Timeout reached waiting for rom done");
372 static int do_rom_fast_read(struct netxen_adapter
*adapter
,
375 NXWR32(adapter
, NETXEN_ROMUSB_ROM_ADDRESS
, addr
);
376 NXWR32(adapter
, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT
, 0);
377 NXWR32(adapter
, NETXEN_ROMUSB_ROM_ABYTE_CNT
, 3);
378 NXWR32(adapter
, NETXEN_ROMUSB_ROM_INSTR_OPCODE
, 0xb);
379 if (netxen_wait_rom_done(adapter
)) {
380 printk("Error waiting for rom done\n");
383 /* reset abyte_cnt and dummy_byte_cnt */
384 NXWR32(adapter
, NETXEN_ROMUSB_ROM_ABYTE_CNT
, 0);
386 NXWR32(adapter
, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT
, 0);
388 *valp
= NXRD32(adapter
, NETXEN_ROMUSB_ROM_RDATA
);
392 static int do_rom_fast_read_words(struct netxen_adapter
*adapter
, int addr
,
393 u8
*bytes
, size_t size
)
398 for (addridx
= addr
; addridx
< (addr
+ size
); addridx
+= 4) {
400 ret
= do_rom_fast_read(adapter
, addridx
, &v
);
403 *(__le32
*)bytes
= cpu_to_le32(v
);
411 netxen_rom_fast_read_words(struct netxen_adapter
*adapter
, int addr
,
412 u8
*bytes
, size_t size
)
416 ret
= netxen_rom_lock(adapter
);
420 ret
= do_rom_fast_read_words(adapter
, addr
, bytes
, size
);
422 netxen_rom_unlock(adapter
);
426 int netxen_rom_fast_read(struct netxen_adapter
*adapter
, int addr
, int *valp
)
430 if (netxen_rom_lock(adapter
) != 0)
433 ret
= do_rom_fast_read(adapter
, addr
, valp
);
434 netxen_rom_unlock(adapter
);
438 #define NETXEN_BOARDTYPE 0x4008
439 #define NETXEN_BOARDNUM 0x400c
440 #define NETXEN_CHIPNUM 0x4010
442 int netxen_pinit_from_rom(struct netxen_adapter
*adapter
)
445 int i
, n
, init_delay
= 0;
446 struct crb_addr_pair
*buf
;
451 netxen_rom_lock(adapter
);
452 NXWR32(adapter
, NETXEN_ROMUSB_GLB_SW_RESET
, 0xffffffff);
453 netxen_rom_unlock(adapter
);
455 if (NX_IS_REVISION_P3(adapter
->ahw
.revision_id
)) {
456 if (netxen_rom_fast_read(adapter
, 0, &n
) != 0 ||
458 netxen_rom_fast_read(adapter
, 4, &n
) != 0) {
459 printk(KERN_ERR
"%s: ERROR Reading crb_init area: "
460 "n: %08x\n", netxen_nic_driver_name
, n
);
463 offset
= n
& 0xffffU
;
464 n
= (n
>> 16) & 0xffffU
;
466 if (netxen_rom_fast_read(adapter
, 0, &n
) != 0 ||
468 printk(KERN_ERR
"%s: ERROR Reading crb_init area: "
469 "n: %08x\n", netxen_nic_driver_name
, n
);
477 printk(KERN_ERR
"%s:n=0x%x Error! NetXen card flash not"
478 " initialized.\n", __func__
, n
);
482 buf
= kcalloc(n
, sizeof(struct crb_addr_pair
), GFP_KERNEL
);
484 printk("%s: netxen_pinit_from_rom: Unable to calloc memory.\n",
485 netxen_nic_driver_name
);
489 for (i
= 0; i
< n
; i
++) {
490 if (netxen_rom_fast_read(adapter
, 8*i
+ 4*offset
, &val
) != 0 ||
491 netxen_rom_fast_read(adapter
, 8*i
+ 4*offset
+ 4, &addr
) != 0) {
501 for (i
= 0; i
< n
; i
++) {
503 off
= netxen_decode_crb_addr(buf
[i
].addr
);
504 if (off
== NETXEN_ADDR_ERROR
) {
505 printk(KERN_ERR
"CRB init value out of range %x\n",
509 off
+= NETXEN_PCI_CRBSPACE
;
514 /* skipping cold reboot MAGIC */
515 if (off
== NETXEN_CAM_RAM(0x1fc))
518 if (NX_IS_REVISION_P3(adapter
->ahw
.revision_id
)) {
519 if (off
== (NETXEN_CRB_I2C0
+ 0x1c))
521 /* do not reset PCI */
522 if (off
== (ROMUSB_GLB
+ 0xbc))
524 if (off
== (ROMUSB_GLB
+ 0xa8))
526 if (off
== (ROMUSB_GLB
+ 0xc8)) /* core clock */
528 if (off
== (ROMUSB_GLB
+ 0x24)) /* MN clock */
530 if (off
== (ROMUSB_GLB
+ 0x1c)) /* MS clock */
532 if ((off
& 0x0ff00000) == NETXEN_CRB_DDR_NET
)
534 if (off
== (NETXEN_CRB_PEG_NET_1
+ 0x18) &&
535 !NX_IS_REVISION_P3P(adapter
->ahw
.revision_id
))
536 buf
[i
].data
= 0x1020;
537 /* skip the function enable register */
538 if (off
== NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION
))
540 if (off
== NETXEN_PCIE_REG(PCIE_SETUP_FUNCTION2
))
542 if ((off
& 0x0ff00000) == NETXEN_CRB_SMB
)
547 /* After writing this register, HW needs time for CRB */
548 /* to quiet down (else crb_window returns 0xffffffff) */
549 if (off
== NETXEN_ROMUSB_GLB_SW_RESET
) {
551 if (NX_IS_REVISION_P2(adapter
->ahw
.revision_id
)) {
552 /* hold xdma in reset also */
553 buf
[i
].data
= NETXEN_NIC_XDMA_RESET
;
554 buf
[i
].data
= 0x8000ff;
558 NXWR32(adapter
, off
, buf
[i
].data
);
564 /* disable_peg_cache_all */
566 /* unreset_net_cache */
567 if (NX_IS_REVISION_P2(adapter
->ahw
.revision_id
)) {
568 val
= NXRD32(adapter
, NETXEN_ROMUSB_GLB_SW_RESET
);
569 NXWR32(adapter
, NETXEN_ROMUSB_GLB_SW_RESET
, (val
& 0xffffff0f));
572 /* p2dn replyCount */
573 NXWR32(adapter
, NETXEN_CRB_PEG_NET_D
+ 0xec, 0x1e);
574 /* disable_peg_cache 0 */
575 NXWR32(adapter
, NETXEN_CRB_PEG_NET_D
+ 0x4c, 8);
576 /* disable_peg_cache 1 */
577 NXWR32(adapter
, NETXEN_CRB_PEG_NET_I
+ 0x4c, 8);
582 NXWR32(adapter
, NETXEN_CRB_PEG_NET_0
+ 0x8, 0);
583 NXWR32(adapter
, NETXEN_CRB_PEG_NET_0
+ 0xc, 0);
585 NXWR32(adapter
, NETXEN_CRB_PEG_NET_1
+ 0x8, 0);
586 NXWR32(adapter
, NETXEN_CRB_PEG_NET_1
+ 0xc, 0);
588 NXWR32(adapter
, NETXEN_CRB_PEG_NET_2
+ 0x8, 0);
589 NXWR32(adapter
, NETXEN_CRB_PEG_NET_2
+ 0xc, 0);
591 NXWR32(adapter
, NETXEN_CRB_PEG_NET_3
+ 0x8, 0);
592 NXWR32(adapter
, NETXEN_CRB_PEG_NET_3
+ 0xc, 0);
596 static struct uni_table_desc
*nx_get_table_desc(const u8
*unirom
, int section
)
599 struct uni_table_desc
*directory
= (struct uni_table_desc
*) &unirom
[0];
600 __le32 entries
= cpu_to_le32(directory
->num_entries
);
602 for (i
= 0; i
< entries
; i
++) {
604 __le32 offs
= cpu_to_le32(directory
->findex
) +
605 (i
* cpu_to_le32(directory
->entry_size
));
606 __le32 tab_type
= cpu_to_le32(*((u32
*)&unirom
[offs
] + 8));
608 if (tab_type
== section
)
609 return (struct uni_table_desc
*) &unirom
[offs
];
615 #define QLCNIC_FILEHEADER_SIZE (14 * 4)
618 netxen_nic_validate_header(struct netxen_adapter
*adapter
)
620 const u8
*unirom
= adapter
->fw
->data
;
621 struct uni_table_desc
*directory
= (struct uni_table_desc
*) &unirom
[0];
622 u32 fw_file_size
= adapter
->fw
->size
;
627 if (fw_file_size
< QLCNIC_FILEHEADER_SIZE
)
630 entries
= cpu_to_le32(directory
->num_entries
);
631 entry_size
= cpu_to_le32(directory
->entry_size
);
632 tab_size
= cpu_to_le32(directory
->findex
) + (entries
* entry_size
);
634 if (fw_file_size
< tab_size
)
641 netxen_nic_validate_bootld(struct netxen_adapter
*adapter
)
643 struct uni_table_desc
*tab_desc
;
644 struct uni_data_desc
*descr
;
645 const u8
*unirom
= adapter
->fw
->data
;
646 __le32 idx
= cpu_to_le32(*((int *)&unirom
[adapter
->file_prd_off
] +
647 NX_UNI_BOOTLD_IDX_OFF
));
652 tab_desc
= nx_get_table_desc(unirom
, NX_UNI_DIR_SECT_BOOTLD
);
657 tab_size
= cpu_to_le32(tab_desc
->findex
) +
658 (cpu_to_le32(tab_desc
->entry_size
) * (idx
+ 1));
660 if (adapter
->fw
->size
< tab_size
)
663 offs
= cpu_to_le32(tab_desc
->findex
) +
664 (cpu_to_le32(tab_desc
->entry_size
) * (idx
));
665 descr
= (struct uni_data_desc
*)&unirom
[offs
];
667 data_size
= cpu_to_le32(descr
->findex
) + cpu_to_le32(descr
->size
);
669 if (adapter
->fw
->size
< data_size
)
676 netxen_nic_validate_fw(struct netxen_adapter
*adapter
)
678 struct uni_table_desc
*tab_desc
;
679 struct uni_data_desc
*descr
;
680 const u8
*unirom
= adapter
->fw
->data
;
681 __le32 idx
= cpu_to_le32(*((int *)&unirom
[adapter
->file_prd_off
] +
682 NX_UNI_FIRMWARE_IDX_OFF
));
687 tab_desc
= nx_get_table_desc(unirom
, NX_UNI_DIR_SECT_FW
);
692 tab_size
= cpu_to_le32(tab_desc
->findex
) +
693 (cpu_to_le32(tab_desc
->entry_size
) * (idx
+ 1));
695 if (adapter
->fw
->size
< tab_size
)
698 offs
= cpu_to_le32(tab_desc
->findex
) +
699 (cpu_to_le32(tab_desc
->entry_size
) * (idx
));
700 descr
= (struct uni_data_desc
*)&unirom
[offs
];
701 data_size
= cpu_to_le32(descr
->findex
) + cpu_to_le32(descr
->size
);
703 if (adapter
->fw
->size
< data_size
)
711 netxen_nic_validate_product_offs(struct netxen_adapter
*adapter
)
713 struct uni_table_desc
*ptab_descr
;
714 const u8
*unirom
= adapter
->fw
->data
;
715 int mn_present
= (NX_IS_REVISION_P2(adapter
->ahw
.revision_id
)) ?
716 1 : netxen_p3_has_mn(adapter
);
722 ptab_descr
= nx_get_table_desc(unirom
, NX_UNI_DIR_SECT_PRODUCT_TBL
);
723 if (ptab_descr
== NULL
)
726 entries
= cpu_to_le32(ptab_descr
->num_entries
);
727 entry_size
= cpu_to_le32(ptab_descr
->entry_size
);
728 tab_size
= cpu_to_le32(ptab_descr
->findex
) + (entries
* entry_size
);
730 if (adapter
->fw
->size
< tab_size
)
734 for (i
= 0; i
< entries
; i
++) {
736 __le32 flags
, file_chiprev
, offs
;
737 u8 chiprev
= adapter
->ahw
.revision_id
;
740 offs
= cpu_to_le32(ptab_descr
->findex
) +
741 (i
* cpu_to_le32(ptab_descr
->entry_size
));
742 flags
= cpu_to_le32(*((int *)&unirom
[offs
] + NX_UNI_FLAGS_OFF
));
743 file_chiprev
= cpu_to_le32(*((int *)&unirom
[offs
] +
744 NX_UNI_CHIP_REV_OFF
));
746 flagbit
= mn_present
? 1 : 2;
748 if ((chiprev
== file_chiprev
) &&
749 ((1ULL << flagbit
) & flags
)) {
750 adapter
->file_prd_off
= offs
;
755 if (mn_present
&& NX_IS_REVISION_P3(adapter
->ahw
.revision_id
)) {
764 netxen_nic_validate_unified_romimage(struct netxen_adapter
*adapter
)
766 if (netxen_nic_validate_header(adapter
)) {
767 dev_err(&adapter
->pdev
->dev
,
768 "unified image: header validation failed\n");
772 if (netxen_nic_validate_product_offs(adapter
)) {
773 dev_err(&adapter
->pdev
->dev
,
774 "unified image: product validation failed\n");
778 if (netxen_nic_validate_bootld(adapter
)) {
779 dev_err(&adapter
->pdev
->dev
,
780 "unified image: bootld validation failed\n");
784 if (netxen_nic_validate_fw(adapter
)) {
785 dev_err(&adapter
->pdev
->dev
,
786 "unified image: firmware validation failed\n");
793 static struct uni_data_desc
*nx_get_data_desc(struct netxen_adapter
*adapter
,
794 u32 section
, u32 idx_offset
)
796 const u8
*unirom
= adapter
->fw
->data
;
797 int idx
= cpu_to_le32(*((int *)&unirom
[adapter
->file_prd_off
] +
799 struct uni_table_desc
*tab_desc
;
802 tab_desc
= nx_get_table_desc(unirom
, section
);
804 if (tab_desc
== NULL
)
807 offs
= cpu_to_le32(tab_desc
->findex
) +
808 (cpu_to_le32(tab_desc
->entry_size
) * idx
);
810 return (struct uni_data_desc
*)&unirom
[offs
];
814 nx_get_bootld_offs(struct netxen_adapter
*adapter
)
816 u32 offs
= NETXEN_BOOTLD_START
;
818 if (adapter
->fw_type
== NX_UNIFIED_ROMIMAGE
)
819 offs
= cpu_to_le32((nx_get_data_desc(adapter
,
820 NX_UNI_DIR_SECT_BOOTLD
,
821 NX_UNI_BOOTLD_IDX_OFF
))->findex
);
823 return (u8
*)&adapter
->fw
->data
[offs
];
827 nx_get_fw_offs(struct netxen_adapter
*adapter
)
829 u32 offs
= NETXEN_IMAGE_START
;
831 if (adapter
->fw_type
== NX_UNIFIED_ROMIMAGE
)
832 offs
= cpu_to_le32((nx_get_data_desc(adapter
,
834 NX_UNI_FIRMWARE_IDX_OFF
))->findex
);
836 return (u8
*)&adapter
->fw
->data
[offs
];
840 nx_get_fw_size(struct netxen_adapter
*adapter
)
842 if (adapter
->fw_type
== NX_UNIFIED_ROMIMAGE
)
843 return cpu_to_le32((nx_get_data_desc(adapter
,
845 NX_UNI_FIRMWARE_IDX_OFF
))->size
);
848 *(u32
*)&adapter
->fw
->data
[NX_FW_SIZE_OFFSET
]);
852 nx_get_fw_version(struct netxen_adapter
*adapter
)
854 struct uni_data_desc
*fw_data_desc
;
855 const struct firmware
*fw
= adapter
->fw
;
856 __le32 major
, minor
, sub
;
860 if (adapter
->fw_type
== NX_UNIFIED_ROMIMAGE
) {
862 fw_data_desc
= nx_get_data_desc(adapter
,
863 NX_UNI_DIR_SECT_FW
, NX_UNI_FIRMWARE_IDX_OFF
);
864 ver_str
= fw
->data
+ cpu_to_le32(fw_data_desc
->findex
) +
865 cpu_to_le32(fw_data_desc
->size
) - 17;
867 for (i
= 0; i
< 12; i
++) {
868 if (!strncmp(&ver_str
[i
], "REV=", 4)) {
869 ret
= sscanf(&ver_str
[i
+4], "%u.%u.%u ",
870 &major
, &minor
, &sub
);
878 return major
+ (minor
<< 8) + (sub
<< 16);
881 return cpu_to_le32(*(u32
*)&fw
->data
[NX_FW_VERSION_OFFSET
]);
885 nx_get_bios_version(struct netxen_adapter
*adapter
)
887 const struct firmware
*fw
= adapter
->fw
;
888 __le32 bios_ver
, prd_off
= adapter
->file_prd_off
;
890 if (adapter
->fw_type
== NX_UNIFIED_ROMIMAGE
) {
891 bios_ver
= cpu_to_le32(*((u32
*) (&fw
->data
[prd_off
])
892 + NX_UNI_BIOS_VERSION_OFF
));
893 return (bios_ver
<< 16) + ((bios_ver
>> 8) & 0xff00) +
896 return cpu_to_le32(*(u32
*)&fw
->data
[NX_BIOS_VERSION_OFFSET
]);
901 netxen_need_fw_reset(struct netxen_adapter
*adapter
)
903 u32 count
, old_count
;
904 u32 val
, version
, major
, minor
, build
;
908 /* NX2031 firmware doesn't support heartbit */
909 if (NX_IS_REVISION_P2(adapter
->ahw
.revision_id
))
912 if (adapter
->need_fw_reset
)
915 /* last attempt had failed */
916 if (NXRD32(adapter
, CRB_CMDPEG_STATE
) == PHAN_INITIALIZE_FAILED
)
919 old_count
= NXRD32(adapter
, NETXEN_PEG_ALIVE_COUNTER
);
921 for (i
= 0; i
< 10; i
++) {
923 timeout
= msleep_interruptible(200);
925 NXWR32(adapter
, CRB_CMDPEG_STATE
,
926 PHAN_INITIALIZE_FAILED
);
930 count
= NXRD32(adapter
, NETXEN_PEG_ALIVE_COUNTER
);
931 if (count
!= old_count
)
935 /* firmware is dead */
936 if (count
== old_count
)
939 /* check if we have got newer or different file firmware */
942 val
= nx_get_fw_version(adapter
);
944 version
= NETXEN_DECODE_VERSION(val
);
946 major
= NXRD32(adapter
, NETXEN_FW_VERSION_MAJOR
);
947 minor
= NXRD32(adapter
, NETXEN_FW_VERSION_MINOR
);
948 build
= NXRD32(adapter
, NETXEN_FW_VERSION_SUB
);
950 if (version
> NETXEN_VERSION_CODE(major
, minor
, build
))
953 if (version
== NETXEN_VERSION_CODE(major
, minor
, build
) &&
954 adapter
->fw_type
!= NX_UNIFIED_ROMIMAGE
) {
956 val
= NXRD32(adapter
, NETXEN_MIU_MN_CONTROL
);
957 fw_type
= (val
& 0x4) ?
958 NX_P3_CT_ROMIMAGE
: NX_P3_MN_ROMIMAGE
;
960 if (adapter
->fw_type
!= fw_type
)
968 #define NETXEN_MIN_P3_FW_SUPP NETXEN_VERSION_CODE(4, 0, 505)
971 netxen_check_flash_fw_compatibility(struct netxen_adapter
*adapter
)
973 u32 flash_fw_ver
, min_fw_ver
;
975 if (NX_IS_REVISION_P2(adapter
->ahw
.revision_id
))
978 if (netxen_rom_fast_read(adapter
,
979 NX_FW_VERSION_OFFSET
, (int *)&flash_fw_ver
)) {
980 dev_err(&adapter
->pdev
->dev
, "Unable to read flash fw"
985 flash_fw_ver
= NETXEN_DECODE_VERSION(flash_fw_ver
);
986 min_fw_ver
= NETXEN_MIN_P3_FW_SUPP
;
987 if (flash_fw_ver
>= min_fw_ver
)
990 dev_info(&adapter
->pdev
->dev
, "Flash fw[%d.%d.%d] is < min fw supported"
991 "[4.0.505]. Please update firmware on flash\n",
992 _major(flash_fw_ver
), _minor(flash_fw_ver
),
993 _build(flash_fw_ver
));
997 static char *fw_name
[] = {
998 NX_P2_MN_ROMIMAGE_NAME
,
999 NX_P3_CT_ROMIMAGE_NAME
,
1000 NX_P3_MN_ROMIMAGE_NAME
,
1001 NX_UNIFIED_ROMIMAGE_NAME
,
1002 NX_FLASH_ROMIMAGE_NAME
,
1006 netxen_load_firmware(struct netxen_adapter
*adapter
)
1009 u32 i
, flashaddr
, size
;
1010 const struct firmware
*fw
= adapter
->fw
;
1011 struct pci_dev
*pdev
= adapter
->pdev
;
1013 dev_info(&pdev
->dev
, "loading firmware from %s\n",
1014 fw_name
[adapter
->fw_type
]);
1016 if (NX_IS_REVISION_P2(adapter
->ahw
.revision_id
))
1017 NXWR32(adapter
, NETXEN_ROMUSB_GLB_CAS_RST
, 1);
1022 size
= (NETXEN_IMAGE_START
- NETXEN_BOOTLD_START
) / 8;
1024 ptr64
= (u64
*)nx_get_bootld_offs(adapter
);
1025 flashaddr
= NETXEN_BOOTLD_START
;
1027 for (i
= 0; i
< size
; i
++) {
1028 data
= cpu_to_le64(ptr64
[i
]);
1030 if (adapter
->pci_mem_write(adapter
, flashaddr
, data
))
1036 size
= (__force u32
)nx_get_fw_size(adapter
) / 8;
1038 ptr64
= (u64
*)nx_get_fw_offs(adapter
);
1039 flashaddr
= NETXEN_IMAGE_START
;
1041 for (i
= 0; i
< size
; i
++) {
1042 data
= cpu_to_le64(ptr64
[i
]);
1044 if (adapter
->pci_mem_write(adapter
,
1051 size
= (__force u32
)nx_get_fw_size(adapter
) % 8;
1053 data
= cpu_to_le64(ptr64
[i
]);
1055 if (adapter
->pci_mem_write(adapter
,
1064 size
= (NETXEN_IMAGE_START
- NETXEN_BOOTLD_START
) / 8;
1065 flashaddr
= NETXEN_BOOTLD_START
;
1067 for (i
= 0; i
< size
; i
++) {
1068 if (netxen_rom_fast_read(adapter
,
1069 flashaddr
, (int *)&lo
) != 0)
1071 if (netxen_rom_fast_read(adapter
,
1072 flashaddr
+ 4, (int *)&hi
) != 0)
1075 /* hi, lo are already in host endian byteorder */
1076 data
= (((u64
)hi
<< 32) | lo
);
1078 if (adapter
->pci_mem_write(adapter
,
1087 if (NX_IS_REVISION_P3P(adapter
->ahw
.revision_id
)) {
1088 NXWR32(adapter
, NETXEN_CRB_PEG_NET_0
+ 0x18, 0x1020);
1089 NXWR32(adapter
, NETXEN_ROMUSB_GLB_SW_RESET
, 0x80001e);
1090 } else if (NX_IS_REVISION_P3(adapter
->ahw
.revision_id
))
1091 NXWR32(adapter
, NETXEN_ROMUSB_GLB_SW_RESET
, 0x80001d);
1093 NXWR32(adapter
, NETXEN_ROMUSB_GLB_CHIP_CLK_CTRL
, 0x3fff);
1094 NXWR32(adapter
, NETXEN_ROMUSB_GLB_CAS_RST
, 0);
1101 netxen_validate_firmware(struct netxen_adapter
*adapter
)
1104 __le32 flash_fw_ver
;
1105 u32 file_fw_ver
, min_ver
, bios
;
1106 struct pci_dev
*pdev
= adapter
->pdev
;
1107 const struct firmware
*fw
= adapter
->fw
;
1108 u8 fw_type
= adapter
->fw_type
;
1111 if (fw_type
== NX_UNIFIED_ROMIMAGE
) {
1112 if (netxen_nic_validate_unified_romimage(adapter
))
1115 val
= cpu_to_le32(*(u32
*)&fw
->data
[NX_FW_MAGIC_OFFSET
]);
1116 if ((__force u32
)val
!= NETXEN_BDINFO_MAGIC
)
1119 if (fw
->size
< NX_FW_MIN_SIZE
)
1123 val
= nx_get_fw_version(adapter
);
1125 if (NX_IS_REVISION_P3(adapter
->ahw
.revision_id
))
1126 min_ver
= NETXEN_MIN_P3_FW_SUPP
;
1128 min_ver
= NETXEN_VERSION_CODE(3, 4, 216);
1130 file_fw_ver
= NETXEN_DECODE_VERSION(val
);
1132 if ((_major(file_fw_ver
) > _NETXEN_NIC_LINUX_MAJOR
) ||
1133 (file_fw_ver
< min_ver
)) {
1135 "%s: firmware version %d.%d.%d unsupported\n",
1136 fw_name
[fw_type
], _major(file_fw_ver
), _minor(file_fw_ver
),
1137 _build(file_fw_ver
));
1141 val
= nx_get_bios_version(adapter
);
1142 netxen_rom_fast_read(adapter
, NX_BIOS_VERSION_OFFSET
, (int *)&bios
);
1143 if ((__force u32
)val
!= bios
) {
1144 dev_err(&pdev
->dev
, "%s: firmware bios is incompatible\n",
1149 if (netxen_rom_fast_read(adapter
,
1150 NX_FW_VERSION_OFFSET
, (int *)&flash_fw_ver
)) {
1151 dev_err(&pdev
->dev
, "Unable to read flash fw version\n");
1154 flash_fw_ver
= NETXEN_DECODE_VERSION(flash_fw_ver
);
1156 /* New fw from file is not allowed, if fw on flash is < 4.0.554 */
1157 crbinit_fix_fw
= NETXEN_VERSION_CODE(4, 0, 554);
1158 if (file_fw_ver
>= crbinit_fix_fw
&& flash_fw_ver
< crbinit_fix_fw
&&
1159 NX_IS_REVISION_P3(adapter
->ahw
.revision_id
)) {
1160 dev_err(&pdev
->dev
, "Incompatibility detected between driver "
1161 "and firmware version on flash. This configuration "
1162 "is not recommended. Please update the firmware on "
1163 "flash immediately\n");
1167 /* check if flashed firmware is newer only for no-mn and P2 case*/
1168 if (!netxen_p3_has_mn(adapter
) ||
1169 NX_IS_REVISION_P2(adapter
->ahw
.revision_id
)) {
1170 if (flash_fw_ver
> file_fw_ver
) {
1171 dev_info(&pdev
->dev
, "%s: firmware is older than flash\n",
1177 NXWR32(adapter
, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC
);
1182 nx_get_next_fwtype(struct netxen_adapter
*adapter
)
1186 switch (adapter
->fw_type
) {
1187 case NX_UNKNOWN_ROMIMAGE
:
1188 fw_type
= NX_UNIFIED_ROMIMAGE
;
1191 case NX_UNIFIED_ROMIMAGE
:
1192 if (NX_IS_REVISION_P3P(adapter
->ahw
.revision_id
))
1193 fw_type
= NX_FLASH_ROMIMAGE
;
1194 else if (NX_IS_REVISION_P2(adapter
->ahw
.revision_id
))
1195 fw_type
= NX_P2_MN_ROMIMAGE
;
1196 else if (netxen_p3_has_mn(adapter
))
1197 fw_type
= NX_P3_MN_ROMIMAGE
;
1199 fw_type
= NX_P3_CT_ROMIMAGE
;
1202 case NX_P3_MN_ROMIMAGE
:
1203 fw_type
= NX_P3_CT_ROMIMAGE
;
1206 case NX_P2_MN_ROMIMAGE
:
1207 case NX_P3_CT_ROMIMAGE
:
1209 fw_type
= NX_FLASH_ROMIMAGE
;
1213 adapter
->fw_type
= fw_type
;
1217 netxen_p3_has_mn(struct netxen_adapter
*adapter
)
1219 u32 capability
, flashed_ver
;
1222 /* NX2031 always had MN */
1223 if (NX_IS_REVISION_P2(adapter
->ahw
.revision_id
))
1226 netxen_rom_fast_read(adapter
,
1227 NX_FW_VERSION_OFFSET
, (int *)&flashed_ver
);
1228 flashed_ver
= NETXEN_DECODE_VERSION(flashed_ver
);
1230 if (flashed_ver
>= NETXEN_VERSION_CODE(4, 0, 220)) {
1232 capability
= NXRD32(adapter
, NX_PEG_TUNE_CAPABILITY
);
1233 if (capability
& NX_PEG_TUNE_MN_PRESENT
)
1239 void netxen_request_firmware(struct netxen_adapter
*adapter
)
1241 struct pci_dev
*pdev
= adapter
->pdev
;
1244 adapter
->fw_type
= NX_UNKNOWN_ROMIMAGE
;
1247 nx_get_next_fwtype(adapter
);
1249 if (adapter
->fw_type
== NX_FLASH_ROMIMAGE
) {
1252 rc
= request_firmware(&adapter
->fw
,
1253 fw_name
[adapter
->fw_type
], &pdev
->dev
);
1257 rc
= netxen_validate_firmware(adapter
);
1259 release_firmware(adapter
->fw
);
1268 netxen_release_firmware(struct netxen_adapter
*adapter
)
1271 release_firmware(adapter
->fw
);
1275 int netxen_init_dummy_dma(struct netxen_adapter
*adapter
)
1280 if (!NX_IS_REVISION_P2(adapter
->ahw
.revision_id
))
1283 adapter
->dummy_dma
.addr
= pci_alloc_consistent(adapter
->pdev
,
1284 NETXEN_HOST_DUMMY_DMA_SIZE
,
1285 &adapter
->dummy_dma
.phys_addr
);
1286 if (adapter
->dummy_dma
.addr
== NULL
) {
1287 dev_err(&adapter
->pdev
->dev
,
1288 "ERROR: Could not allocate dummy DMA memory\n");
1292 addr
= (uint64_t) adapter
->dummy_dma
.phys_addr
;
1293 hi
= (addr
>> 32) & 0xffffffff;
1294 lo
= addr
& 0xffffffff;
1296 NXWR32(adapter
, CRB_HOST_DUMMY_BUF_ADDR_HI
, hi
);
1297 NXWR32(adapter
, CRB_HOST_DUMMY_BUF_ADDR_LO
, lo
);
1303 * NetXen DMA watchdog control:
1305 * Bit 0 : enabled => R/O: 1 watchdog active, 0 inactive
1306 * Bit 1 : disable_request => 1 req disable dma watchdog
1307 * Bit 2 : enable_request => 1 req enable dma watchdog
1310 void netxen_free_dummy_dma(struct netxen_adapter
*adapter
)
1315 if (!NX_IS_REVISION_P2(adapter
->ahw
.revision_id
))
1318 if (!adapter
->dummy_dma
.addr
)
1321 ctrl
= NXRD32(adapter
, NETXEN_DMA_WATCHDOG_CTRL
);
1322 if ((ctrl
& 0x1) != 0) {
1323 NXWR32(adapter
, NETXEN_DMA_WATCHDOG_CTRL
, (ctrl
| 0x2));
1325 while ((ctrl
& 0x1) != 0) {
1329 ctrl
= NXRD32(adapter
, NETXEN_DMA_WATCHDOG_CTRL
);
1337 pci_free_consistent(adapter
->pdev
,
1338 NETXEN_HOST_DUMMY_DMA_SIZE
,
1339 adapter
->dummy_dma
.addr
,
1340 adapter
->dummy_dma
.phys_addr
);
1341 adapter
->dummy_dma
.addr
= NULL
;
1343 dev_err(&adapter
->pdev
->dev
, "dma_watchdog_shutdown failed\n");
1346 int netxen_phantom_init(struct netxen_adapter
*adapter
, int pegtune_val
)
1355 val
= NXRD32(adapter
, CRB_CMDPEG_STATE
);
1358 case PHAN_INITIALIZE_COMPLETE
:
1359 case PHAN_INITIALIZE_ACK
:
1361 case PHAN_INITIALIZE_FAILED
:
1369 } while (--retries
);
1371 NXWR32(adapter
, CRB_CMDPEG_STATE
, PHAN_INITIALIZE_FAILED
);
1374 dev_warn(&adapter
->pdev
->dev
, "firmware init failed\n");
1379 netxen_receive_peg_ready(struct netxen_adapter
*adapter
)
1385 val
= NXRD32(adapter
, CRB_RCVPEG_STATE
);
1387 if (val
== PHAN_PEG_RCV_INITIALIZED
)
1392 } while (--retries
);
1395 printk(KERN_ERR
"Receive Peg initialization not "
1396 "complete, state: 0x%x.\n", val
);
1403 int netxen_init_firmware(struct netxen_adapter
*adapter
)
1407 err
= netxen_receive_peg_ready(adapter
);
1411 NXWR32(adapter
, CRB_NIC_CAPABILITIES_HOST
, INTR_SCHEME_PERPORT
);
1412 NXWR32(adapter
, CRB_MPORT_MODE
, MPORT_MULTI_FUNCTION_MODE
);
1413 NXWR32(adapter
, CRB_CMDPEG_STATE
, PHAN_INITIALIZE_ACK
);
1415 if (NX_IS_REVISION_P2(adapter
->ahw
.revision_id
))
1416 NXWR32(adapter
, CRB_NIC_MSI_MODE_HOST
, MSI_MODE_MULTIFUNC
);
1422 netxen_handle_linkevent(struct netxen_adapter
*adapter
, nx_fw_msg_t
*msg
)
1427 u8 link_status
, module
, duplex
, autoneg
;
1428 struct net_device
*netdev
= adapter
->netdev
;
1430 adapter
->has_link_events
= 1;
1432 cable_OUI
= msg
->body
[1] & 0xffffffff;
1433 cable_len
= (msg
->body
[1] >> 32) & 0xffff;
1434 link_speed
= (msg
->body
[1] >> 48) & 0xffff;
1436 link_status
= msg
->body
[2] & 0xff;
1437 duplex
= (msg
->body
[2] >> 16) & 0xff;
1438 autoneg
= (msg
->body
[2] >> 24) & 0xff;
1440 module
= (msg
->body
[2] >> 8) & 0xff;
1441 if (module
== LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE
) {
1442 printk(KERN_INFO
"%s: unsupported cable: OUI 0x%x, length %d\n",
1443 netdev
->name
, cable_OUI
, cable_len
);
1444 } else if (module
== LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN
) {
1445 printk(KERN_INFO
"%s: unsupported cable length %d\n",
1446 netdev
->name
, cable_len
);
1449 netxen_advert_link_change(adapter
, link_status
);
1451 /* update link parameters */
1452 if (duplex
== LINKEVENT_FULL_DUPLEX
)
1453 adapter
->link_duplex
= DUPLEX_FULL
;
1455 adapter
->link_duplex
= DUPLEX_HALF
;
1456 adapter
->module_type
= module
;
1457 adapter
->link_autoneg
= autoneg
;
1458 adapter
->link_speed
= link_speed
;
1462 netxen_handle_fw_message(int desc_cnt
, int index
,
1463 struct nx_host_sds_ring
*sds_ring
)
1466 struct status_desc
*desc
;
1469 while (desc_cnt
> 0 && i
< 8) {
1470 desc
= &sds_ring
->desc_head
[index
];
1471 msg
.words
[i
++] = le64_to_cpu(desc
->status_desc_data
[0]);
1472 msg
.words
[i
++] = le64_to_cpu(desc
->status_desc_data
[1]);
1474 index
= get_next_index(index
, sds_ring
->num_desc
);
1478 opcode
= netxen_get_nic_msg_opcode(msg
.body
[0]);
1480 case NX_NIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE
:
1481 netxen_handle_linkevent(sds_ring
->adapter
, &msg
);
1489 netxen_alloc_rx_skb(struct netxen_adapter
*adapter
,
1490 struct nx_host_rds_ring
*rds_ring
,
1491 struct netxen_rx_buffer
*buffer
)
1493 struct sk_buff
*skb
;
1495 struct pci_dev
*pdev
= adapter
->pdev
;
1497 buffer
->skb
= dev_alloc_skb(rds_ring
->skb_size
);
1503 if (!adapter
->ahw
.cut_through
)
1504 skb_reserve(skb
, 2);
1506 dma
= pci_map_single(pdev
, skb
->data
,
1507 rds_ring
->dma_size
, PCI_DMA_FROMDEVICE
);
1509 if (pci_dma_mapping_error(pdev
, dma
)) {
1510 dev_kfree_skb_any(skb
);
1517 buffer
->state
= NETXEN_BUFFER_BUSY
;
1522 static struct sk_buff
*netxen_process_rxbuf(struct netxen_adapter
*adapter
,
1523 struct nx_host_rds_ring
*rds_ring
, u16 index
, u16 cksum
)
1525 struct netxen_rx_buffer
*buffer
;
1526 struct sk_buff
*skb
;
1528 buffer
= &rds_ring
->rx_buf_arr
[index
];
1530 pci_unmap_single(adapter
->pdev
, buffer
->dma
, rds_ring
->dma_size
,
1531 PCI_DMA_FROMDEVICE
);
1537 if (likely((adapter
->netdev
->features
& NETIF_F_RXCSUM
)
1538 && cksum
== STATUS_CKSUM_OK
)) {
1539 adapter
->stats
.csummed
++;
1540 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1542 skb
->ip_summed
= CHECKSUM_NONE
;
1544 skb
->dev
= adapter
->netdev
;
1548 buffer
->state
= NETXEN_BUFFER_FREE
;
1552 static struct netxen_rx_buffer
*
1553 netxen_process_rcv(struct netxen_adapter
*adapter
,
1554 struct nx_host_sds_ring
*sds_ring
,
1555 int ring
, u64 sts_data0
)
1557 struct net_device
*netdev
= adapter
->netdev
;
1558 struct netxen_recv_context
*recv_ctx
= &adapter
->recv_ctx
;
1559 struct netxen_rx_buffer
*buffer
;
1560 struct sk_buff
*skb
;
1561 struct nx_host_rds_ring
*rds_ring
;
1562 int index
, length
, cksum
, pkt_offset
;
1564 if (unlikely(ring
>= adapter
->max_rds_rings
))
1567 rds_ring
= &recv_ctx
->rds_rings
[ring
];
1569 index
= netxen_get_sts_refhandle(sts_data0
);
1570 if (unlikely(index
>= rds_ring
->num_desc
))
1573 buffer
= &rds_ring
->rx_buf_arr
[index
];
1575 length
= netxen_get_sts_totallength(sts_data0
);
1576 cksum
= netxen_get_sts_status(sts_data0
);
1577 pkt_offset
= netxen_get_sts_pkt_offset(sts_data0
);
1579 skb
= netxen_process_rxbuf(adapter
, rds_ring
, index
, cksum
);
1583 if (length
> rds_ring
->skb_size
)
1584 skb_put(skb
, rds_ring
->skb_size
);
1586 skb_put(skb
, length
);
1590 skb_pull(skb
, pkt_offset
);
1592 skb
->protocol
= eth_type_trans(skb
, netdev
);
1594 napi_gro_receive(&sds_ring
->napi
, skb
);
1596 adapter
->stats
.rx_pkts
++;
1597 adapter
->stats
.rxbytes
+= length
;
1602 #define TCP_HDR_SIZE 20
1603 #define TCP_TS_OPTION_SIZE 12
1604 #define TCP_TS_HDR_SIZE (TCP_HDR_SIZE + TCP_TS_OPTION_SIZE)
1606 static struct netxen_rx_buffer
*
1607 netxen_process_lro(struct netxen_adapter
*adapter
,
1608 struct nx_host_sds_ring
*sds_ring
,
1609 int ring
, u64 sts_data0
, u64 sts_data1
)
1611 struct net_device
*netdev
= adapter
->netdev
;
1612 struct netxen_recv_context
*recv_ctx
= &adapter
->recv_ctx
;
1613 struct netxen_rx_buffer
*buffer
;
1614 struct sk_buff
*skb
;
1615 struct nx_host_rds_ring
*rds_ring
;
1618 bool push
, timestamp
;
1619 int l2_hdr_offset
, l4_hdr_offset
;
1621 u16 lro_length
, length
, data_offset
;
1625 if (unlikely(ring
> adapter
->max_rds_rings
))
1628 rds_ring
= &recv_ctx
->rds_rings
[ring
];
1630 index
= netxen_get_lro_sts_refhandle(sts_data0
);
1631 if (unlikely(index
> rds_ring
->num_desc
))
1634 buffer
= &rds_ring
->rx_buf_arr
[index
];
1636 timestamp
= netxen_get_lro_sts_timestamp(sts_data0
);
1637 lro_length
= netxen_get_lro_sts_length(sts_data0
);
1638 l2_hdr_offset
= netxen_get_lro_sts_l2_hdr_offset(sts_data0
);
1639 l4_hdr_offset
= netxen_get_lro_sts_l4_hdr_offset(sts_data0
);
1640 push
= netxen_get_lro_sts_push_flag(sts_data0
);
1641 seq_number
= netxen_get_lro_sts_seq_number(sts_data1
);
1643 skb
= netxen_process_rxbuf(adapter
, rds_ring
, index
, STATUS_CKSUM_OK
);
1648 data_offset
= l4_hdr_offset
+ TCP_TS_HDR_SIZE
;
1650 data_offset
= l4_hdr_offset
+ TCP_HDR_SIZE
;
1652 skb_put(skb
, lro_length
+ data_offset
);
1654 skb_pull(skb
, l2_hdr_offset
);
1655 skb
->protocol
= eth_type_trans(skb
, netdev
);
1657 if (skb
->protocol
== htons(ETH_P_8021Q
))
1658 vhdr_len
= VLAN_HLEN
;
1659 iph
= (struct iphdr
*)(skb
->data
+ vhdr_len
);
1660 th
= (struct tcphdr
*)((skb
->data
+ vhdr_len
) + (iph
->ihl
<< 2));
1662 length
= (iph
->ihl
<< 2) + (th
->doff
<< 2) + lro_length
;
1663 iph
->tot_len
= htons(length
);
1665 iph
->check
= ip_fast_csum((unsigned char *)iph
, iph
->ihl
);
1667 th
->seq
= htonl(seq_number
);
1671 netif_receive_skb(skb
);
1673 adapter
->stats
.lro_pkts
++;
1674 adapter
->stats
.rxbytes
+= length
;
1679 #define netxen_merge_rx_buffers(list, head) \
1680 do { list_splice_tail_init(list, head); } while (0);
1683 netxen_process_rcv_ring(struct nx_host_sds_ring
*sds_ring
, int max
)
1685 struct netxen_adapter
*adapter
= sds_ring
->adapter
;
1687 struct list_head
*cur
;
1689 struct status_desc
*desc
;
1690 struct netxen_rx_buffer
*rxbuf
;
1692 u32 consumer
= sds_ring
->consumer
;
1695 u64 sts_data0
, sts_data1
;
1696 int opcode
, ring
= 0, desc_cnt
;
1698 while (count
< max
) {
1699 desc
= &sds_ring
->desc_head
[consumer
];
1700 sts_data0
= le64_to_cpu(desc
->status_desc_data
[0]);
1702 if (!(sts_data0
& STATUS_OWNER_HOST
))
1705 desc_cnt
= netxen_get_sts_desc_cnt(sts_data0
);
1707 opcode
= netxen_get_sts_opcode(sts_data0
);
1710 case NETXEN_NIC_RXPKT_DESC
:
1711 case NETXEN_OLD_RXPKT_DESC
:
1712 case NETXEN_NIC_SYN_OFFLOAD
:
1713 ring
= netxen_get_sts_type(sts_data0
);
1714 rxbuf
= netxen_process_rcv(adapter
, sds_ring
,
1717 case NETXEN_NIC_LRO_DESC
:
1718 ring
= netxen_get_lro_sts_type(sts_data0
);
1719 sts_data1
= le64_to_cpu(desc
->status_desc_data
[1]);
1720 rxbuf
= netxen_process_lro(adapter
, sds_ring
,
1721 ring
, sts_data0
, sts_data1
);
1723 case NETXEN_NIC_RESPONSE_DESC
:
1724 netxen_handle_fw_message(desc_cnt
, consumer
, sds_ring
);
1729 WARN_ON(desc_cnt
> 1);
1732 list_add_tail(&rxbuf
->list
, &sds_ring
->free_list
[ring
]);
1735 for (; desc_cnt
> 0; desc_cnt
--) {
1736 desc
= &sds_ring
->desc_head
[consumer
];
1737 desc
->status_desc_data
[0] =
1738 cpu_to_le64(STATUS_OWNER_PHANTOM
);
1739 consumer
= get_next_index(consumer
, sds_ring
->num_desc
);
1744 for (ring
= 0; ring
< adapter
->max_rds_rings
; ring
++) {
1745 struct nx_host_rds_ring
*rds_ring
=
1746 &adapter
->recv_ctx
.rds_rings
[ring
];
1748 if (!list_empty(&sds_ring
->free_list
[ring
])) {
1749 list_for_each(cur
, &sds_ring
->free_list
[ring
]) {
1750 rxbuf
= list_entry(cur
,
1751 struct netxen_rx_buffer
, list
);
1752 netxen_alloc_rx_skb(adapter
, rds_ring
, rxbuf
);
1754 spin_lock(&rds_ring
->lock
);
1755 netxen_merge_rx_buffers(&sds_ring
->free_list
[ring
],
1756 &rds_ring
->free_list
);
1757 spin_unlock(&rds_ring
->lock
);
1760 netxen_post_rx_buffers_nodb(adapter
, rds_ring
);
1764 sds_ring
->consumer
= consumer
;
1765 NXWRIO(adapter
, sds_ring
->crb_sts_consumer
, consumer
);
1771 /* Process Command status ring */
1772 int netxen_process_cmd_ring(struct netxen_adapter
*adapter
)
1774 u32 sw_consumer
, hw_consumer
;
1776 struct netxen_cmd_buffer
*buffer
;
1777 struct pci_dev
*pdev
= adapter
->pdev
;
1778 struct net_device
*netdev
= adapter
->netdev
;
1779 struct netxen_skb_frag
*frag
;
1781 struct nx_host_tx_ring
*tx_ring
= adapter
->tx_ring
;
1783 if (!spin_trylock(&adapter
->tx_clean_lock
))
1786 sw_consumer
= tx_ring
->sw_consumer
;
1787 hw_consumer
= le32_to_cpu(*(tx_ring
->hw_consumer
));
1789 while (sw_consumer
!= hw_consumer
) {
1790 buffer
= &tx_ring
->cmd_buf_arr
[sw_consumer
];
1792 frag
= &buffer
->frag_array
[0];
1793 pci_unmap_single(pdev
, frag
->dma
, frag
->length
,
1796 for (i
= 1; i
< buffer
->frag_count
; i
++) {
1797 frag
++; /* Get the next frag */
1798 pci_unmap_page(pdev
, frag
->dma
, frag
->length
,
1803 adapter
->stats
.xmitfinished
++;
1804 dev_kfree_skb_any(buffer
->skb
);
1808 sw_consumer
= get_next_index(sw_consumer
, tx_ring
->num_desc
);
1809 if (++count
>= MAX_STATUS_HANDLE
)
1813 if (count
&& netif_running(netdev
)) {
1814 tx_ring
->sw_consumer
= sw_consumer
;
1818 if (netif_queue_stopped(netdev
) && netif_carrier_ok(netdev
))
1819 if (netxen_tx_avail(tx_ring
) > TX_STOP_THRESH
)
1820 netif_wake_queue(netdev
);
1821 adapter
->tx_timeo_cnt
= 0;
1824 * If everything is freed up to consumer then check if the ring is full
1825 * If the ring is full then check if more needs to be freed and
1826 * schedule the call back again.
1828 * This happens when there are 2 CPUs. One could be freeing and the
1829 * other filling it. If the ring is full when we get out of here and
1830 * the card has already interrupted the host then the host can miss the
1833 * There is still a possible race condition and the host could miss an
1834 * interrupt. The card has to take care of this.
1836 hw_consumer
= le32_to_cpu(*(tx_ring
->hw_consumer
));
1837 done
= (sw_consumer
== hw_consumer
);
1838 spin_unlock(&adapter
->tx_clean_lock
);
1844 netxen_post_rx_buffers(struct netxen_adapter
*adapter
, u32 ringid
,
1845 struct nx_host_rds_ring
*rds_ring
)
1847 struct rcv_desc
*pdesc
;
1848 struct netxen_rx_buffer
*buffer
;
1849 int producer
, count
= 0;
1850 netxen_ctx_msg msg
= 0;
1851 struct list_head
*head
;
1853 producer
= rds_ring
->producer
;
1855 head
= &rds_ring
->free_list
;
1856 while (!list_empty(head
)) {
1858 buffer
= list_entry(head
->next
, struct netxen_rx_buffer
, list
);
1861 if (netxen_alloc_rx_skb(adapter
, rds_ring
, buffer
))
1866 list_del(&buffer
->list
);
1868 /* make a rcv descriptor */
1869 pdesc
= &rds_ring
->desc_head
[producer
];
1870 pdesc
->addr_buffer
= cpu_to_le64(buffer
->dma
);
1871 pdesc
->reference_handle
= cpu_to_le16(buffer
->ref_handle
);
1872 pdesc
->buffer_length
= cpu_to_le32(rds_ring
->dma_size
);
1874 producer
= get_next_index(producer
, rds_ring
->num_desc
);
1878 rds_ring
->producer
= producer
;
1879 NXWRIO(adapter
, rds_ring
->crb_rcv_producer
,
1880 (producer
-1) & (rds_ring
->num_desc
-1));
1882 if (NX_IS_REVISION_P2(adapter
->ahw
.revision_id
)) {
1884 * Write a doorbell msg to tell phanmon of change in
1885 * receive ring producer
1886 * Only for firmware version < 4.0.0
1888 netxen_set_msg_peg_id(msg
, NETXEN_RCV_PEG_DB_ID
);
1889 netxen_set_msg_privid(msg
);
1890 netxen_set_msg_count(msg
,
1892 (rds_ring
->num_desc
- 1)));
1893 netxen_set_msg_ctxid(msg
, adapter
->portnum
);
1894 netxen_set_msg_opcode(msg
, NETXEN_RCV_PRODUCER(ringid
));
1895 NXWRIO(adapter
, DB_NORMALIZE(adapter
,
1896 NETXEN_RCV_PRODUCER_OFFSET
), msg
);
1902 netxen_post_rx_buffers_nodb(struct netxen_adapter
*adapter
,
1903 struct nx_host_rds_ring
*rds_ring
)
1905 struct rcv_desc
*pdesc
;
1906 struct netxen_rx_buffer
*buffer
;
1907 int producer
, count
= 0;
1908 struct list_head
*head
;
1910 if (!spin_trylock(&rds_ring
->lock
))
1913 producer
= rds_ring
->producer
;
1915 head
= &rds_ring
->free_list
;
1916 while (!list_empty(head
)) {
1918 buffer
= list_entry(head
->next
, struct netxen_rx_buffer
, list
);
1921 if (netxen_alloc_rx_skb(adapter
, rds_ring
, buffer
))
1926 list_del(&buffer
->list
);
1928 /* make a rcv descriptor */
1929 pdesc
= &rds_ring
->desc_head
[producer
];
1930 pdesc
->reference_handle
= cpu_to_le16(buffer
->ref_handle
);
1931 pdesc
->buffer_length
= cpu_to_le32(rds_ring
->dma_size
);
1932 pdesc
->addr_buffer
= cpu_to_le64(buffer
->dma
);
1934 producer
= get_next_index(producer
, rds_ring
->num_desc
);
1938 rds_ring
->producer
= producer
;
1939 NXWRIO(adapter
, rds_ring
->crb_rcv_producer
,
1940 (producer
- 1) & (rds_ring
->num_desc
- 1));
1942 spin_unlock(&rds_ring
->lock
);
1945 void netxen_nic_clear_stats(struct netxen_adapter
*adapter
)
1947 memset(&adapter
->stats
, 0, sizeof(adapter
->stats
));