drivers/net/netxen/: cleanups
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / net / netxen / netxen_nic_init.c
blob68c985f3e0b7df7e9f30a57c504d59457043a438
1 /*
2 * Copyright (C) 2003 - 2006 NetXen, Inc.
3 * All rights reserved.
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.
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.
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.
20 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
23 * Contact Information:
24 * info@netxen.com
25 * NetXen,
26 * 3965 Freedom Circle, Fourth floor,
27 * Santa Clara, CA 95054
30 * Source file for NIC routines to initialize the Phantom Hardware
34 #include <linux/netdevice.h>
35 #include <linux/delay.h>
36 #include "netxen_nic.h"
37 #include "netxen_nic_hw.h"
38 #include "netxen_nic_phan_reg.h"
40 struct crb_addr_pair {
41 u32 addr;
42 u32 data;
45 unsigned long last_schedule_time;
47 #define NETXEN_MAX_CRB_XFORM 60
48 static unsigned int crb_addr_xform[NETXEN_MAX_CRB_XFORM];
49 #define NETXEN_ADDR_ERROR (0xffffffff)
51 #define crb_addr_transform(name) \
52 crb_addr_xform[NETXEN_HW_PX_MAP_CRB_##name] = \
53 NETXEN_HW_CRB_HUB_AGT_ADR_##name << 20
55 #define NETXEN_NIC_XDMA_RESET 0x8000ff
57 static void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
58 uint32_t ctx, uint32_t ringid);
60 #if 0
61 static void netxen_nic_locked_write_reg(struct netxen_adapter *adapter,
62 unsigned long off, int *data)
64 void __iomem *addr = pci_base_offset(adapter, off);
65 writel(*data, addr);
67 #endif /* 0 */
69 static void crb_addr_transform_setup(void)
71 crb_addr_transform(XDMA);
72 crb_addr_transform(TIMR);
73 crb_addr_transform(SRE);
74 crb_addr_transform(SQN3);
75 crb_addr_transform(SQN2);
76 crb_addr_transform(SQN1);
77 crb_addr_transform(SQN0);
78 crb_addr_transform(SQS3);
79 crb_addr_transform(SQS2);
80 crb_addr_transform(SQS1);
81 crb_addr_transform(SQS0);
82 crb_addr_transform(RPMX7);
83 crb_addr_transform(RPMX6);
84 crb_addr_transform(RPMX5);
85 crb_addr_transform(RPMX4);
86 crb_addr_transform(RPMX3);
87 crb_addr_transform(RPMX2);
88 crb_addr_transform(RPMX1);
89 crb_addr_transform(RPMX0);
90 crb_addr_transform(ROMUSB);
91 crb_addr_transform(SN);
92 crb_addr_transform(QMN);
93 crb_addr_transform(QMS);
94 crb_addr_transform(PGNI);
95 crb_addr_transform(PGND);
96 crb_addr_transform(PGN3);
97 crb_addr_transform(PGN2);
98 crb_addr_transform(PGN1);
99 crb_addr_transform(PGN0);
100 crb_addr_transform(PGSI);
101 crb_addr_transform(PGSD);
102 crb_addr_transform(PGS3);
103 crb_addr_transform(PGS2);
104 crb_addr_transform(PGS1);
105 crb_addr_transform(PGS0);
106 crb_addr_transform(PS);
107 crb_addr_transform(PH);
108 crb_addr_transform(NIU);
109 crb_addr_transform(I2Q);
110 crb_addr_transform(EG);
111 crb_addr_transform(MN);
112 crb_addr_transform(MS);
113 crb_addr_transform(CAS2);
114 crb_addr_transform(CAS1);
115 crb_addr_transform(CAS0);
116 crb_addr_transform(CAM);
117 crb_addr_transform(C2C1);
118 crb_addr_transform(C2C0);
119 crb_addr_transform(SMB);
122 int netxen_init_firmware(struct netxen_adapter *adapter)
124 u32 state = 0, loops = 0, err = 0;
126 /* Window 1 call */
127 state = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
129 if (state == PHAN_INITIALIZE_ACK)
130 return 0;
132 while (state != PHAN_INITIALIZE_COMPLETE && loops < 2000) {
133 udelay(100);
134 /* Window 1 call */
135 state = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
137 loops++;
139 if (loops >= 2000) {
140 printk(KERN_ERR "Cmd Peg initialization not complete:%x.\n",
141 state);
142 err = -EIO;
143 return err;
145 /* Window 1 call */
146 writel(INTR_SCHEME_PERPORT,
147 NETXEN_CRB_NORMALIZE(adapter, CRB_NIC_CAPABILITIES_HOST));
148 writel(MPORT_MULTI_FUNCTION_MODE,
149 NETXEN_CRB_NORMALIZE(adapter, CRB_MPORT_MODE));
150 writel(PHAN_INITIALIZE_ACK,
151 NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
153 return err;
156 #define NETXEN_ADDR_LIMIT 0xffffffffULL
158 void *netxen_alloc(struct pci_dev *pdev, size_t sz, dma_addr_t * ptr,
159 struct pci_dev **used_dev)
161 void *addr;
163 addr = pci_alloc_consistent(pdev, sz, ptr);
164 if ((unsigned long long)(*ptr) < NETXEN_ADDR_LIMIT) {
165 *used_dev = pdev;
166 return addr;
168 pci_free_consistent(pdev, sz, addr, *ptr);
169 addr = pci_alloc_consistent(NULL, sz, ptr);
170 *used_dev = NULL;
171 return addr;
174 void netxen_initialize_adapter_sw(struct netxen_adapter *adapter)
176 int ctxid, ring;
177 u32 i;
178 u32 num_rx_bufs = 0;
179 struct netxen_rcv_desc_ctx *rcv_desc;
181 DPRINTK(INFO, "initializing some queues: %p\n", adapter);
182 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
183 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
184 struct netxen_rx_buffer *rx_buf;
185 rcv_desc = &adapter->recv_ctx[ctxid].rcv_desc[ring];
186 rcv_desc->rcv_free = rcv_desc->max_rx_desc_count;
187 rcv_desc->begin_alloc = 0;
188 rx_buf = rcv_desc->rx_buf_arr;
189 num_rx_bufs = rcv_desc->max_rx_desc_count;
191 * Now go through all of them, set reference handles
192 * and put them in the queues.
194 for (i = 0; i < num_rx_bufs; i++) {
195 rx_buf->ref_handle = i;
196 rx_buf->state = NETXEN_BUFFER_FREE;
197 DPRINTK(INFO, "Rx buf:ctx%d i(%d) rx_buf:"
198 "%p\n", ctxid, i, rx_buf);
199 rx_buf++;
205 void netxen_initialize_adapter_hw(struct netxen_adapter *adapter)
207 int ports = 0;
208 struct netxen_board_info *board_info = &(adapter->ahw.boardcfg);
210 if (netxen_nic_get_board_info(adapter) != 0)
211 printk("%s: Error getting board config info.\n",
212 netxen_nic_driver_name);
213 get_brd_port_by_type(board_info->board_type, &ports);
214 if (ports == 0)
215 printk(KERN_ERR "%s: Unknown board type\n",
216 netxen_nic_driver_name);
217 adapter->ahw.max_ports = ports;
220 void netxen_initialize_adapter_ops(struct netxen_adapter *adapter)
222 switch (adapter->ahw.board_type) {
223 case NETXEN_NIC_GBE:
224 adapter->enable_phy_interrupts =
225 netxen_niu_gbe_enable_phy_interrupts;
226 adapter->disable_phy_interrupts =
227 netxen_niu_gbe_disable_phy_interrupts;
228 adapter->handle_phy_intr = netxen_nic_gbe_handle_phy_intr;
229 adapter->macaddr_set = netxen_niu_macaddr_set;
230 adapter->set_mtu = netxen_nic_set_mtu_gb;
231 adapter->set_promisc = netxen_niu_set_promiscuous_mode;
232 adapter->unset_promisc = netxen_niu_set_promiscuous_mode;
233 adapter->phy_read = netxen_niu_gbe_phy_read;
234 adapter->phy_write = netxen_niu_gbe_phy_write;
235 adapter->init_niu = netxen_nic_init_niu_gb;
236 adapter->stop_port = netxen_niu_disable_gbe_port;
237 break;
239 case NETXEN_NIC_XGBE:
240 adapter->enable_phy_interrupts =
241 netxen_niu_xgbe_enable_phy_interrupts;
242 adapter->disable_phy_interrupts =
243 netxen_niu_xgbe_disable_phy_interrupts;
244 adapter->handle_phy_intr = netxen_nic_xgbe_handle_phy_intr;
245 adapter->macaddr_set = netxen_niu_xg_macaddr_set;
246 adapter->set_mtu = netxen_nic_set_mtu_xgb;
247 adapter->init_port = netxen_niu_xg_init_port;
248 adapter->set_promisc = netxen_niu_xg_set_promiscuous_mode;
249 adapter->unset_promisc = netxen_niu_xg_set_promiscuous_mode;
250 adapter->stop_port = netxen_niu_disable_xg_port;
251 break;
253 default:
254 break;
259 * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
260 * address to external PCI CRB address.
262 static u32 netxen_decode_crb_addr(u32 addr)
264 int i;
265 u32 base_addr, offset, pci_base;
267 crb_addr_transform_setup();
269 pci_base = NETXEN_ADDR_ERROR;
270 base_addr = addr & 0xfff00000;
271 offset = addr & 0x000fffff;
273 for (i = 0; i < NETXEN_MAX_CRB_XFORM; i++) {
274 if (crb_addr_xform[i] == base_addr) {
275 pci_base = i << 20;
276 break;
279 if (pci_base == NETXEN_ADDR_ERROR)
280 return pci_base;
281 else
282 return (pci_base + offset);
285 static long rom_max_timeout = 100;
286 static long rom_lock_timeout = 10000;
287 static long rom_write_timeout = 700;
289 static int rom_lock(struct netxen_adapter *adapter)
291 int iter;
292 u32 done = 0;
293 int timeout = 0;
295 while (!done) {
296 /* acquire semaphore2 from PCI HW block */
297 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_LOCK),
298 &done);
299 if (done == 1)
300 break;
301 if (timeout >= rom_lock_timeout)
302 return -EIO;
304 timeout++;
306 * Yield CPU
308 if (!in_atomic())
309 schedule();
310 else {
311 for (iter = 0; iter < 20; iter++)
312 cpu_relax(); /*This a nop instr on i386 */
315 netxen_nic_reg_write(adapter, NETXEN_ROM_LOCK_ID, ROM_LOCK_DRIVER);
316 return 0;
319 static int netxen_wait_rom_done(struct netxen_adapter *adapter)
321 long timeout = 0;
322 long done = 0;
324 while (done == 0) {
325 done = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_GLB_STATUS);
326 done &= 2;
327 timeout++;
328 if (timeout >= rom_max_timeout) {
329 printk("Timeout reached waiting for rom done");
330 return -EIO;
333 return 0;
336 static int netxen_rom_wren(struct netxen_adapter *adapter)
338 /* Set write enable latch in ROM status register */
339 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
340 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
341 M25P_INSTR_WREN);
342 if (netxen_wait_rom_done(adapter)) {
343 return -1;
345 return 0;
348 static unsigned int netxen_rdcrbreg(struct netxen_adapter *adapter,
349 unsigned int addr)
351 unsigned int data = 0xdeaddead;
352 data = netxen_nic_reg_read(adapter, addr);
353 return data;
356 static int netxen_do_rom_rdsr(struct netxen_adapter *adapter)
358 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
359 M25P_INSTR_RDSR);
360 if (netxen_wait_rom_done(adapter)) {
361 return -1;
363 return netxen_rdcrbreg(adapter, NETXEN_ROMUSB_ROM_RDATA);
366 static void netxen_rom_unlock(struct netxen_adapter *adapter)
368 u32 val;
370 /* release semaphore2 */
371 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_UNLOCK), &val);
375 static int netxen_rom_wip_poll(struct netxen_adapter *adapter)
377 long timeout = 0;
378 long wip = 1;
379 int val;
380 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
381 while (wip != 0) {
382 val = netxen_do_rom_rdsr(adapter);
383 wip = val & 1;
384 timeout++;
385 if (timeout > rom_max_timeout) {
386 return -1;
389 return 0;
392 static int do_rom_fast_write(struct netxen_adapter *adapter, int addr,
393 int data)
395 if (netxen_rom_wren(adapter)) {
396 return -1;
398 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
399 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
400 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
401 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
402 M25P_INSTR_PP);
403 if (netxen_wait_rom_done(adapter)) {
404 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
405 return -1;
408 return netxen_rom_wip_poll(adapter);
411 static int do_rom_fast_read(struct netxen_adapter *adapter,
412 int addr, int *valp)
414 cond_resched();
416 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
417 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
418 udelay(100); /* prevent bursting on CRB */
419 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
420 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0xb);
421 if (netxen_wait_rom_done(adapter)) {
422 printk("Error waiting for rom done\n");
423 return -EIO;
425 /* reset abyte_cnt and dummy_byte_cnt */
426 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
427 udelay(100); /* prevent bursting on CRB */
428 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
430 *valp = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_ROM_RDATA);
431 return 0;
434 static int do_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
435 u8 *bytes, size_t size)
437 int addridx;
438 int ret = 0;
440 for (addridx = addr; addridx < (addr + size); addridx += 4) {
441 ret = do_rom_fast_read(adapter, addridx, (int *)bytes);
442 if (ret != 0)
443 break;
444 *(int *)bytes = cpu_to_le32(*(int *)bytes);
445 bytes += 4;
448 return ret;
452 netxen_rom_fast_read_words(struct netxen_adapter *adapter, int addr,
453 u8 *bytes, size_t size)
455 int ret;
457 ret = rom_lock(adapter);
458 if (ret < 0)
459 return ret;
461 ret = do_rom_fast_read_words(adapter, addr, bytes, size);
463 netxen_rom_unlock(adapter);
464 return ret;
467 int netxen_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
469 int ret;
471 if (rom_lock(adapter) != 0)
472 return -EIO;
474 ret = do_rom_fast_read(adapter, addr, valp);
475 netxen_rom_unlock(adapter);
476 return ret;
479 #if 0
480 int netxen_rom_fast_write(struct netxen_adapter *adapter, int addr, int data)
482 int ret = 0;
484 if (rom_lock(adapter) != 0) {
485 return -1;
487 ret = do_rom_fast_write(adapter, addr, data);
488 netxen_rom_unlock(adapter);
489 return ret;
491 #endif /* 0 */
493 static int do_rom_fast_write_words(struct netxen_adapter *adapter,
494 int addr, u8 *bytes, size_t size)
496 int addridx = addr;
497 int ret = 0;
499 while (addridx < (addr + size)) {
500 int last_attempt = 0;
501 int timeout = 0;
502 int data;
504 data = le32_to_cpu((*(u32*)bytes));
505 ret = do_rom_fast_write(adapter, addridx, data);
506 if (ret < 0)
507 return ret;
509 while(1) {
510 int data1;
512 ret = do_rom_fast_read(adapter, addridx, &data1);
513 if (ret < 0)
514 return ret;
516 if (data1 == data)
517 break;
519 if (timeout++ >= rom_write_timeout) {
520 if (last_attempt++ < 4) {
521 ret = do_rom_fast_write(adapter,
522 addridx, data);
523 if (ret < 0)
524 return ret;
526 else {
527 printk(KERN_INFO "Data write did not "
528 "succeed at address 0x%x\n", addridx);
529 break;
534 bytes += 4;
535 addridx += 4;
538 return ret;
541 int netxen_rom_fast_write_words(struct netxen_adapter *adapter, int addr,
542 u8 *bytes, size_t size)
544 int ret = 0;
546 ret = rom_lock(adapter);
547 if (ret < 0)
548 return ret;
550 ret = do_rom_fast_write_words(adapter, addr, bytes, size);
551 netxen_rom_unlock(adapter);
553 return ret;
556 static int netxen_rom_wrsr(struct netxen_adapter *adapter, int data)
558 int ret;
560 ret = netxen_rom_wren(adapter);
561 if (ret < 0)
562 return ret;
564 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
565 netxen_crb_writelit_adapter(adapter,
566 NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0x1);
568 ret = netxen_wait_rom_done(adapter);
569 if (ret < 0)
570 return ret;
572 return netxen_rom_wip_poll(adapter);
575 static int netxen_rom_rdsr(struct netxen_adapter *adapter)
577 int ret;
579 ret = rom_lock(adapter);
580 if (ret < 0)
581 return ret;
583 ret = netxen_do_rom_rdsr(adapter);
584 netxen_rom_unlock(adapter);
585 return ret;
588 int netxen_backup_crbinit(struct netxen_adapter *adapter)
590 int ret = FLASH_SUCCESS;
591 int val;
592 char *buffer = kmalloc(NETXEN_FLASH_SECTOR_SIZE, GFP_KERNEL);
594 if (!buffer)
595 return -ENOMEM;
596 /* unlock sector 63 */
597 val = netxen_rom_rdsr(adapter);
598 val = val & 0xe3;
599 ret = netxen_rom_wrsr(adapter, val);
600 if (ret != FLASH_SUCCESS)
601 goto out_kfree;
603 ret = netxen_rom_wip_poll(adapter);
604 if (ret != FLASH_SUCCESS)
605 goto out_kfree;
607 /* copy sector 0 to sector 63 */
608 ret = netxen_rom_fast_read_words(adapter, NETXEN_CRBINIT_START,
609 buffer, NETXEN_FLASH_SECTOR_SIZE);
610 if (ret != FLASH_SUCCESS)
611 goto out_kfree;
613 ret = netxen_rom_fast_write_words(adapter, NETXEN_FIXED_START,
614 buffer, NETXEN_FLASH_SECTOR_SIZE);
615 if (ret != FLASH_SUCCESS)
616 goto out_kfree;
618 /* lock sector 63 */
619 val = netxen_rom_rdsr(adapter);
620 if (!(val & 0x8)) {
621 val |= (0x1 << 2);
622 /* lock sector 63 */
623 if (netxen_rom_wrsr(adapter, val) == 0) {
624 ret = netxen_rom_wip_poll(adapter);
625 if (ret != FLASH_SUCCESS)
626 goto out_kfree;
628 /* lock SR writes */
629 ret = netxen_rom_wip_poll(adapter);
630 if (ret != FLASH_SUCCESS)
631 goto out_kfree;
635 out_kfree:
636 kfree(buffer);
637 return ret;
640 static int netxen_do_rom_se(struct netxen_adapter *adapter, int addr)
642 netxen_rom_wren(adapter);
643 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
644 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
645 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
646 M25P_INSTR_SE);
647 if (netxen_wait_rom_done(adapter)) {
648 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
649 return -1;
651 return netxen_rom_wip_poll(adapter);
654 static void check_erased_flash(struct netxen_adapter *adapter, int addr)
656 int i;
657 int val;
658 int count = 0, erased_errors = 0;
659 int range;
661 range = (addr == NETXEN_USER_START) ?
662 NETXEN_FIXED_START : addr + NETXEN_FLASH_SECTOR_SIZE;
664 for (i = addr; i < range; i += 4) {
665 netxen_rom_fast_read(adapter, i, &val);
666 if (val != 0xffffffff)
667 erased_errors++;
668 count++;
671 if (erased_errors)
672 printk(KERN_INFO "0x%x out of 0x%x words fail to be erased "
673 "for sector address: %x\n", erased_errors, count, addr);
676 int netxen_rom_se(struct netxen_adapter *adapter, int addr)
678 int ret = 0;
679 if (rom_lock(adapter) != 0) {
680 return -1;
682 ret = netxen_do_rom_se(adapter, addr);
683 netxen_rom_unlock(adapter);
684 msleep(30);
685 check_erased_flash(adapter, addr);
687 return ret;
690 static int netxen_flash_erase_sections(struct netxen_adapter *adapter,
691 int start, int end)
693 int ret = FLASH_SUCCESS;
694 int i;
696 for (i = start; i < end; i++) {
697 ret = netxen_rom_se(adapter, i * NETXEN_FLASH_SECTOR_SIZE);
698 if (ret)
699 break;
700 ret = netxen_rom_wip_poll(adapter);
701 if (ret < 0)
702 return ret;
705 return ret;
709 netxen_flash_erase_secondary(struct netxen_adapter *adapter)
711 int ret = FLASH_SUCCESS;
712 int start, end;
714 start = NETXEN_SECONDARY_START / NETXEN_FLASH_SECTOR_SIZE;
715 end = NETXEN_USER_START / NETXEN_FLASH_SECTOR_SIZE;
716 ret = netxen_flash_erase_sections(adapter, start, end);
718 return ret;
722 netxen_flash_erase_primary(struct netxen_adapter *adapter)
724 int ret = FLASH_SUCCESS;
725 int start, end;
727 start = NETXEN_PRIMARY_START / NETXEN_FLASH_SECTOR_SIZE;
728 end = NETXEN_SECONDARY_START / NETXEN_FLASH_SECTOR_SIZE;
729 ret = netxen_flash_erase_sections(adapter, start, end);
731 return ret;
734 void netxen_halt_pegs(struct netxen_adapter *adapter)
736 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c, 1);
737 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c, 1);
738 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c, 1);
739 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c, 1);
742 int netxen_flash_unlock(struct netxen_adapter *adapter)
744 int ret = 0;
746 ret = netxen_rom_wrsr(adapter, 0);
747 if (ret < 0)
748 return ret;
750 ret = netxen_rom_wren(adapter);
751 if (ret < 0)
752 return ret;
754 return ret;
757 #define NETXEN_BOARDTYPE 0x4008
758 #define NETXEN_BOARDNUM 0x400c
759 #define NETXEN_CHIPNUM 0x4010
760 #define NETXEN_ROMBUS_RESET 0xFFFFFFFF
761 #define NETXEN_ROM_FIRST_BARRIER 0x800000000ULL
762 #define NETXEN_ROM_FOUND_INIT 0x400
764 int netxen_pinit_from_rom(struct netxen_adapter *adapter, int verbose)
766 int addr, val, status;
767 int n, i;
768 int init_delay = 0;
769 struct crb_addr_pair *buf;
770 u32 off;
772 /* resetall */
773 status = netxen_nic_get_board_info(adapter);
774 if (status)
775 printk("%s: netxen_pinit_from_rom: Error getting board info\n",
776 netxen_nic_driver_name);
778 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
779 NETXEN_ROMBUS_RESET);
781 if (verbose) {
782 int val;
783 if (netxen_rom_fast_read(adapter, NETXEN_BOARDTYPE, &val) == 0)
784 printk("P2 ROM board type: 0x%08x\n", val);
785 else
786 printk("Could not read board type\n");
787 if (netxen_rom_fast_read(adapter, NETXEN_BOARDNUM, &val) == 0)
788 printk("P2 ROM board num: 0x%08x\n", val);
789 else
790 printk("Could not read board number\n");
791 if (netxen_rom_fast_read(adapter, NETXEN_CHIPNUM, &val) == 0)
792 printk("P2 ROM chip num: 0x%08x\n", val);
793 else
794 printk("Could not read chip number\n");
797 if (netxen_rom_fast_read(adapter, 0, &n) == 0
798 && (n & NETXEN_ROM_FIRST_BARRIER)) {
799 n &= ~NETXEN_ROM_ROUNDUP;
800 if (n < NETXEN_ROM_FOUND_INIT) {
801 if (verbose)
802 printk("%s: %d CRB init values found"
803 " in ROM.\n", netxen_nic_driver_name, n);
804 } else {
805 printk("%s:n=0x%x Error! NetXen card flash not"
806 " initialized.\n", __FUNCTION__, n);
807 return -EIO;
809 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
810 if (buf == NULL) {
811 printk("%s: netxen_pinit_from_rom: Unable to calloc "
812 "memory.\n", netxen_nic_driver_name);
813 return -ENOMEM;
815 for (i = 0; i < n; i++) {
816 if (netxen_rom_fast_read(adapter, 8 * i + 4, &val) != 0
817 || netxen_rom_fast_read(adapter, 8 * i + 8,
818 &addr) != 0)
819 return -EIO;
821 buf[i].addr = addr;
822 buf[i].data = val;
824 if (verbose)
825 printk("%s: PCI: 0x%08x == 0x%08x\n",
826 netxen_nic_driver_name, (unsigned int)
827 netxen_decode_crb_addr(addr), val);
829 for (i = 0; i < n; i++) {
831 off = netxen_decode_crb_addr(buf[i].addr);
832 if (off == NETXEN_ADDR_ERROR) {
833 printk(KERN_ERR"CRB init value out of range %x\n",
834 buf[i].addr);
835 continue;
837 off += NETXEN_PCI_CRBSPACE;
838 /* skipping cold reboot MAGIC */
839 if (off == NETXEN_CAM_RAM(0x1fc))
840 continue;
842 /* After writing this register, HW needs time for CRB */
843 /* to quiet down (else crb_window returns 0xffffffff) */
844 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
845 init_delay = 1;
846 /* hold xdma in reset also */
847 buf[i].data = NETXEN_NIC_XDMA_RESET;
850 if (ADDR_IN_WINDOW1(off)) {
851 writel(buf[i].data,
852 NETXEN_CRB_NORMALIZE(adapter, off));
853 } else {
854 netxen_nic_pci_change_crbwindow(adapter, 0);
855 writel(buf[i].data,
856 pci_base_offset(adapter, off));
858 netxen_nic_pci_change_crbwindow(adapter, 1);
860 if (init_delay == 1) {
861 msleep(2000);
862 init_delay = 0;
864 msleep(20);
866 kfree(buf);
868 /* disable_peg_cache_all */
870 /* unreset_net_cache */
871 netxen_nic_hw_read_wx(adapter, NETXEN_ROMUSB_GLB_SW_RESET, &val,
873 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
874 (val & 0xffffff0f));
875 /* p2dn replyCount */
876 netxen_crb_writelit_adapter(adapter,
877 NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
878 /* disable_peg_cache 0 */
879 netxen_crb_writelit_adapter(adapter,
880 NETXEN_CRB_PEG_NET_D + 0x4c, 8);
881 /* disable_peg_cache 1 */
882 netxen_crb_writelit_adapter(adapter,
883 NETXEN_CRB_PEG_NET_I + 0x4c, 8);
885 /* peg_clr_all */
887 /* peg_clr 0 */
888 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x8,
890 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0xc,
892 /* peg_clr 1 */
893 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x8,
895 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0xc,
897 /* peg_clr 2 */
898 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x8,
900 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0xc,
902 /* peg_clr 3 */
903 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x8,
905 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0xc,
908 return 0;
911 int netxen_initialize_adapter_offload(struct netxen_adapter *adapter)
913 uint64_t addr;
914 uint32_t hi;
915 uint32_t lo;
917 adapter->dummy_dma.addr =
918 pci_alloc_consistent(adapter->ahw.pdev,
919 NETXEN_HOST_DUMMY_DMA_SIZE,
920 &adapter->dummy_dma.phys_addr);
921 if (adapter->dummy_dma.addr == NULL) {
922 printk("%s: ERROR: Could not allocate dummy DMA memory\n",
923 __FUNCTION__);
924 return -ENOMEM;
927 addr = (uint64_t) adapter->dummy_dma.phys_addr;
928 hi = (addr >> 32) & 0xffffffff;
929 lo = addr & 0xffffffff;
931 writel(hi, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI));
932 writel(lo, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO));
934 return 0;
937 void netxen_free_adapter_offload(struct netxen_adapter *adapter)
939 if (adapter->dummy_dma.addr) {
940 pci_free_consistent(adapter->ahw.pdev,
941 NETXEN_HOST_DUMMY_DMA_SIZE,
942 adapter->dummy_dma.addr,
943 adapter->dummy_dma.phys_addr);
944 adapter->dummy_dma.addr = NULL;
948 int netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
950 u32 val = 0;
951 int retries = 30;
953 if (!pegtune_val) {
954 do {
955 val = readl(NETXEN_CRB_NORMALIZE
956 (adapter, CRB_CMDPEG_STATE));
957 pegtune_val = readl(NETXEN_CRB_NORMALIZE
958 (adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE));
960 if (val == PHAN_INITIALIZE_COMPLETE ||
961 val == PHAN_INITIALIZE_ACK)
962 return 0;
964 msleep(1000);
965 } while (--retries);
966 if (!retries) {
967 printk(KERN_WARNING "netxen_phantom_init: init failed, "
968 "pegtune_val=%x\n", pegtune_val);
969 return -1;
973 return 0;
976 int netxen_nic_rx_has_work(struct netxen_adapter *adapter)
978 int ctx;
980 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
981 struct netxen_recv_context *recv_ctx =
982 &(adapter->recv_ctx[ctx]);
983 u32 consumer;
984 struct status_desc *desc_head;
985 struct status_desc *desc;
987 consumer = recv_ctx->status_rx_consumer;
988 desc_head = recv_ctx->rcv_status_desc_head;
989 desc = &desc_head[consumer];
991 if (netxen_get_sts_owner(desc) & STATUS_OWNER_HOST)
992 return 1;
995 return 0;
998 static int netxen_nic_check_temp(struct netxen_adapter *adapter)
1000 struct net_device *netdev = adapter->netdev;
1001 uint32_t temp, temp_state, temp_val;
1002 int rv = 0;
1004 temp = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_TEMP_STATE));
1006 temp_state = nx_get_temp_state(temp);
1007 temp_val = nx_get_temp_val(temp);
1009 if (temp_state == NX_TEMP_PANIC) {
1010 printk(KERN_ALERT
1011 "%s: Device temperature %d degrees C exceeds"
1012 " maximum allowed. Hardware has been shut down.\n",
1013 netxen_nic_driver_name, temp_val);
1015 netif_carrier_off(netdev);
1016 netif_stop_queue(netdev);
1017 rv = 1;
1018 } else if (temp_state == NX_TEMP_WARN) {
1019 if (adapter->temp == NX_TEMP_NORMAL) {
1020 printk(KERN_ALERT
1021 "%s: Device temperature %d degrees C "
1022 "exceeds operating range."
1023 " Immediate action needed.\n",
1024 netxen_nic_driver_name, temp_val);
1026 } else {
1027 if (adapter->temp == NX_TEMP_WARN) {
1028 printk(KERN_INFO
1029 "%s: Device temperature is now %d degrees C"
1030 " in normal range.\n", netxen_nic_driver_name,
1031 temp_val);
1034 adapter->temp = temp_state;
1035 return rv;
1038 void netxen_watchdog_task(struct work_struct *work)
1040 struct net_device *netdev;
1041 struct netxen_adapter *adapter =
1042 container_of(work, struct netxen_adapter, watchdog_task);
1044 if ((adapter->portnum == 0) && netxen_nic_check_temp(adapter))
1045 return;
1047 if (adapter->handle_phy_intr)
1048 adapter->handle_phy_intr(adapter);
1050 netdev = adapter->netdev;
1051 if ((netif_running(netdev)) && !netif_carrier_ok(netdev) &&
1052 netxen_nic_link_ok(adapter) ) {
1053 printk(KERN_INFO "%s %s (port %d), Link is up\n",
1054 netxen_nic_driver_name, netdev->name, adapter->portnum);
1055 netif_carrier_on(netdev);
1056 netif_wake_queue(netdev);
1057 } else if(!(netif_running(netdev)) && netif_carrier_ok(netdev)) {
1058 printk(KERN_ERR "%s %s Link is Down\n",
1059 netxen_nic_driver_name, netdev->name);
1060 netif_carrier_off(netdev);
1061 netif_stop_queue(netdev);
1064 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
1068 * netxen_process_rcv() send the received packet to the protocol stack.
1069 * and if the number of receives exceeds RX_BUFFERS_REFILL, then we
1070 * invoke the routine to send more rx buffers to the Phantom...
1072 static void netxen_process_rcv(struct netxen_adapter *adapter, int ctxid,
1073 struct status_desc *desc)
1075 struct pci_dev *pdev = adapter->pdev;
1076 struct net_device *netdev = adapter->netdev;
1077 u64 sts_data = le64_to_cpu(desc->status_desc_data);
1078 int index = netxen_get_sts_refhandle(sts_data);
1079 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1080 struct netxen_rx_buffer *buffer;
1081 struct sk_buff *skb;
1082 u32 length = netxen_get_sts_totallength(sts_data);
1083 u32 desc_ctx;
1084 struct netxen_rcv_desc_ctx *rcv_desc;
1085 int ret;
1087 desc_ctx = netxen_get_sts_type(sts_data);
1088 if (unlikely(desc_ctx >= NUM_RCV_DESC_RINGS)) {
1089 printk("%s: %s Bad Rcv descriptor ring\n",
1090 netxen_nic_driver_name, netdev->name);
1091 return;
1094 rcv_desc = &recv_ctx->rcv_desc[desc_ctx];
1095 if (unlikely(index > rcv_desc->max_rx_desc_count)) {
1096 DPRINTK(ERR, "Got a buffer index:%x Max is %x\n",
1097 index, rcv_desc->max_rx_desc_count);
1098 return;
1100 buffer = &rcv_desc->rx_buf_arr[index];
1101 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1102 buffer->lro_current_frags++;
1103 if (netxen_get_sts_desc_lro_last_frag(desc)) {
1104 buffer->lro_expected_frags =
1105 netxen_get_sts_desc_lro_cnt(desc);
1106 buffer->lro_length = length;
1108 if (buffer->lro_current_frags != buffer->lro_expected_frags) {
1109 if (buffer->lro_expected_frags != 0) {
1110 printk("LRO: (refhandle:%x) recv frag."
1111 "wait for last. flags: %x expected:%d"
1112 "have:%d\n", index,
1113 netxen_get_sts_desc_lro_last_frag(desc),
1114 buffer->lro_expected_frags,
1115 buffer->lro_current_frags);
1117 return;
1121 pci_unmap_single(pdev, buffer->dma, rcv_desc->dma_size,
1122 PCI_DMA_FROMDEVICE);
1124 skb = (struct sk_buff *)buffer->skb;
1126 if (likely(adapter->rx_csum &&
1127 netxen_get_sts_status(sts_data) == STATUS_CKSUM_OK)) {
1128 adapter->stats.csummed++;
1129 skb->ip_summed = CHECKSUM_UNNECESSARY;
1130 } else
1131 skb->ip_summed = CHECKSUM_NONE;
1133 skb->dev = netdev;
1134 if (desc_ctx == RCV_DESC_LRO_CTXID) {
1135 /* True length was only available on the last pkt */
1136 skb_put(skb, buffer->lro_length);
1137 } else {
1138 skb_put(skb, length);
1141 skb->protocol = eth_type_trans(skb, netdev);
1143 ret = netif_receive_skb(skb);
1146 * RH: Do we need these stats on a regular basis. Can we get it from
1147 * Linux stats.
1149 switch (ret) {
1150 case NET_RX_SUCCESS:
1151 adapter->stats.uphappy++;
1152 break;
1154 case NET_RX_CN_LOW:
1155 adapter->stats.uplcong++;
1156 break;
1158 case NET_RX_CN_MOD:
1159 adapter->stats.upmcong++;
1160 break;
1162 case NET_RX_CN_HIGH:
1163 adapter->stats.uphcong++;
1164 break;
1166 case NET_RX_DROP:
1167 adapter->stats.updropped++;
1168 break;
1170 default:
1171 adapter->stats.updunno++;
1172 break;
1175 netdev->last_rx = jiffies;
1177 rcv_desc->rcv_free++;
1178 rcv_desc->rcv_pending--;
1181 * We just consumed one buffer so post a buffer.
1183 buffer->skb = NULL;
1184 buffer->state = NETXEN_BUFFER_FREE;
1185 buffer->lro_current_frags = 0;
1186 buffer->lro_expected_frags = 0;
1188 adapter->stats.no_rcv++;
1189 adapter->stats.rxbytes += length;
1192 /* Process Receive status ring */
1193 u32 netxen_process_rcv_ring(struct netxen_adapter *adapter, int ctxid, int max)
1195 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
1196 struct status_desc *desc_head = recv_ctx->rcv_status_desc_head;
1197 struct status_desc *desc; /* used to read status desc here */
1198 u32 consumer = recv_ctx->status_rx_consumer;
1199 u32 producer = 0;
1200 int count = 0, ring;
1202 DPRINTK(INFO, "procesing receive\n");
1204 * we assume in this case that there is only one port and that is
1205 * port #1...changes need to be done in firmware to indicate port
1206 * number as part of the descriptor. This way we will be able to get
1207 * the netdev which is associated with that device.
1209 while (count < max) {
1210 desc = &desc_head[consumer];
1211 if (!(netxen_get_sts_owner(desc) & STATUS_OWNER_HOST)) {
1212 DPRINTK(ERR, "desc %p ownedby %x\n", desc,
1213 netxen_get_sts_owner(desc));
1214 break;
1216 netxen_process_rcv(adapter, ctxid, desc);
1217 netxen_set_sts_owner(desc, STATUS_OWNER_PHANTOM);
1218 consumer = (consumer + 1) & (adapter->max_rx_desc_count - 1);
1219 count++;
1221 if (count) {
1222 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
1223 netxen_post_rx_buffers_nodb(adapter, ctxid, ring);
1227 /* update the consumer index in phantom */
1228 if (count) {
1229 recv_ctx->status_rx_consumer = consumer;
1230 recv_ctx->status_rx_producer = producer;
1232 /* Window = 1 */
1233 writel(consumer,
1234 NETXEN_CRB_NORMALIZE(adapter,
1235 recv_crb_registers[adapter->portnum].
1236 crb_rcv_status_consumer));
1237 wmb();
1240 return count;
1243 /* Process Command status ring */
1244 int netxen_process_cmd_ring(unsigned long data)
1246 u32 last_consumer;
1247 u32 consumer;
1248 struct netxen_adapter *adapter = (struct netxen_adapter *)data;
1249 int count1 = 0;
1250 int count2 = 0;
1251 struct netxen_cmd_buffer *buffer;
1252 struct pci_dev *pdev;
1253 struct netxen_skb_frag *frag;
1254 u32 i;
1255 int done;
1257 spin_lock(&adapter->tx_lock);
1258 last_consumer = adapter->last_cmd_consumer;
1259 DPRINTK(INFO, "procesing xmit complete\n");
1260 /* we assume in this case that there is only one port and that is
1261 * port #1...changes need to be done in firmware to indicate port
1262 * number as part of the descriptor. This way we will be able to get
1263 * the netdev which is associated with that device.
1266 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1267 if (last_consumer == consumer) { /* Ring is empty */
1268 DPRINTK(INFO, "last_consumer %d == consumer %d\n",
1269 last_consumer, consumer);
1270 spin_unlock(&adapter->tx_lock);
1271 return 1;
1274 adapter->proc_cmd_buf_counter++;
1276 * Not needed - does not seem to be used anywhere.
1277 * adapter->cmd_consumer = consumer;
1279 spin_unlock(&adapter->tx_lock);
1281 while ((last_consumer != consumer) && (count1 < MAX_STATUS_HANDLE)) {
1282 buffer = &adapter->cmd_buf_arr[last_consumer];
1283 pdev = adapter->pdev;
1284 if (buffer->skb) {
1285 frag = &buffer->frag_array[0];
1286 pci_unmap_single(pdev, frag->dma, frag->length,
1287 PCI_DMA_TODEVICE);
1288 frag->dma = 0ULL;
1289 for (i = 1; i < buffer->frag_count; i++) {
1290 DPRINTK(INFO, "getting fragment no %d\n", i);
1291 frag++; /* Get the next frag */
1292 pci_unmap_page(pdev, frag->dma, frag->length,
1293 PCI_DMA_TODEVICE);
1294 frag->dma = 0ULL;
1297 adapter->stats.skbfreed++;
1298 dev_kfree_skb_any(buffer->skb);
1299 buffer->skb = NULL;
1300 } else if (adapter->proc_cmd_buf_counter == 1) {
1301 adapter->stats.txnullskb++;
1303 if (unlikely(netif_queue_stopped(adapter->netdev)
1304 && netif_carrier_ok(adapter->netdev))
1305 && ((jiffies - adapter->netdev->trans_start) >
1306 adapter->netdev->watchdog_timeo)) {
1307 SCHEDULE_WORK(&adapter->tx_timeout_task);
1310 last_consumer = get_next_index(last_consumer,
1311 adapter->max_tx_desc_count);
1312 count1++;
1315 count2 = 0;
1316 spin_lock(&adapter->tx_lock);
1317 if ((--adapter->proc_cmd_buf_counter) == 0) {
1318 adapter->last_cmd_consumer = last_consumer;
1319 while ((adapter->last_cmd_consumer != consumer)
1320 && (count2 < MAX_STATUS_HANDLE)) {
1321 buffer =
1322 &adapter->cmd_buf_arr[adapter->last_cmd_consumer];
1323 count2++;
1324 if (buffer->skb)
1325 break;
1326 else
1327 adapter->last_cmd_consumer =
1328 get_next_index(adapter->last_cmd_consumer,
1329 adapter->max_tx_desc_count);
1332 if (count1 || count2) {
1333 if (netif_queue_stopped(adapter->netdev)
1334 && (adapter->flags & NETXEN_NETDEV_STATUS)) {
1335 netif_wake_queue(adapter->netdev);
1336 adapter->flags &= ~NETXEN_NETDEV_STATUS;
1340 * If everything is freed up to consumer then check if the ring is full
1341 * If the ring is full then check if more needs to be freed and
1342 * schedule the call back again.
1344 * This happens when there are 2 CPUs. One could be freeing and the
1345 * other filling it. If the ring is full when we get out of here and
1346 * the card has already interrupted the host then the host can miss the
1347 * interrupt.
1349 * There is still a possible race condition and the host could miss an
1350 * interrupt. The card has to take care of this.
1352 if (adapter->last_cmd_consumer == consumer &&
1353 (((adapter->cmd_producer + 1) %
1354 adapter->max_tx_desc_count) == adapter->last_cmd_consumer)) {
1355 consumer = le32_to_cpu(*(adapter->cmd_consumer));
1357 done = (adapter->last_cmd_consumer == consumer);
1359 spin_unlock(&adapter->tx_lock);
1360 DPRINTK(INFO, "last consumer is %d in %s\n", last_consumer,
1361 __FUNCTION__);
1362 return (done);
1366 * netxen_post_rx_buffers puts buffer in the Phantom memory
1368 void netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ctx, u32 ringid)
1370 struct pci_dev *pdev = adapter->ahw.pdev;
1371 struct sk_buff *skb;
1372 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1373 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
1374 uint producer;
1375 struct rcv_desc *pdesc;
1376 struct netxen_rx_buffer *buffer;
1377 int count = 0;
1378 int index = 0;
1379 netxen_ctx_msg msg = 0;
1380 dma_addr_t dma;
1382 rcv_desc = &recv_ctx->rcv_desc[ringid];
1384 producer = rcv_desc->producer;
1385 index = rcv_desc->begin_alloc;
1386 buffer = &rcv_desc->rx_buf_arr[index];
1387 /* We can start writing rx descriptors into the phantom memory. */
1388 while (buffer->state == NETXEN_BUFFER_FREE) {
1389 skb = dev_alloc_skb(rcv_desc->skb_size);
1390 if (unlikely(!skb)) {
1392 * TODO
1393 * We need to schedule the posting of buffers to the pegs.
1395 rcv_desc->begin_alloc = index;
1396 DPRINTK(ERR, "netxen_post_rx_buffers: "
1397 " allocated only %d buffers\n", count);
1398 break;
1401 count++; /* now there should be no failure */
1402 pdesc = &rcv_desc->desc_head[producer];
1404 #if defined(XGB_DEBUG)
1405 *(unsigned long *)(skb->head) = 0xc0debabe;
1406 if (skb_is_nonlinear(skb)) {
1407 printk("Allocated SKB @%p is nonlinear\n");
1409 #endif
1410 skb_reserve(skb, 2);
1411 /* This will be setup when we receive the
1412 * buffer after it has been filled FSL TBD TBD
1413 * skb->dev = netdev;
1415 dma = pci_map_single(pdev, skb->data, rcv_desc->dma_size,
1416 PCI_DMA_FROMDEVICE);
1417 pdesc->addr_buffer = cpu_to_le64(dma);
1418 buffer->skb = skb;
1419 buffer->state = NETXEN_BUFFER_BUSY;
1420 buffer->dma = dma;
1421 /* make a rcv descriptor */
1422 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1423 pdesc->buffer_length = cpu_to_le32(rcv_desc->dma_size);
1424 DPRINTK(INFO, "done writing descripter\n");
1425 producer =
1426 get_next_index(producer, rcv_desc->max_rx_desc_count);
1427 index = get_next_index(index, rcv_desc->max_rx_desc_count);
1428 buffer = &rcv_desc->rx_buf_arr[index];
1430 /* if we did allocate buffers, then write the count to Phantom */
1431 if (count) {
1432 rcv_desc->begin_alloc = index;
1433 rcv_desc->rcv_pending += count;
1434 rcv_desc->producer = producer;
1435 if (rcv_desc->rcv_free >= 32) {
1436 rcv_desc->rcv_free = 0;
1437 /* Window = 1 */
1438 writel((producer - 1) &
1439 (rcv_desc->max_rx_desc_count - 1),
1440 NETXEN_CRB_NORMALIZE(adapter,
1441 recv_crb_registers[
1442 adapter->portnum].
1443 rcv_desc_crb[ringid].
1444 crb_rcv_producer_offset));
1446 * Write a doorbell msg to tell phanmon of change in
1447 * receive ring producer
1449 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1450 netxen_set_msg_privid(msg);
1451 netxen_set_msg_count(msg,
1452 ((producer -
1453 1) & (rcv_desc->
1454 max_rx_desc_count - 1)));
1455 netxen_set_msg_ctxid(msg, adapter->portnum);
1456 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1457 writel(msg,
1458 DB_NORMALIZE(adapter,
1459 NETXEN_RCV_PRODUCER_OFFSET));
1460 wmb();
1465 static void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter,
1466 uint32_t ctx, uint32_t ringid)
1468 struct pci_dev *pdev = adapter->ahw.pdev;
1469 struct sk_buff *skb;
1470 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1471 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
1472 u32 producer;
1473 struct rcv_desc *pdesc;
1474 struct netxen_rx_buffer *buffer;
1475 int count = 0;
1476 int index = 0;
1478 rcv_desc = &recv_ctx->rcv_desc[ringid];
1480 producer = rcv_desc->producer;
1481 index = rcv_desc->begin_alloc;
1482 buffer = &rcv_desc->rx_buf_arr[index];
1483 /* We can start writing rx descriptors into the phantom memory. */
1484 while (buffer->state == NETXEN_BUFFER_FREE) {
1485 skb = dev_alloc_skb(rcv_desc->skb_size);
1486 if (unlikely(!skb)) {
1488 * We need to schedule the posting of buffers to the pegs.
1490 rcv_desc->begin_alloc = index;
1491 DPRINTK(ERR, "netxen_post_rx_buffers_nodb: "
1492 " allocated only %d buffers\n", count);
1493 break;
1495 count++; /* now there should be no failure */
1496 pdesc = &rcv_desc->desc_head[producer];
1497 skb_reserve(skb, 2);
1499 * This will be setup when we receive the
1500 * buffer after it has been filled
1501 * skb->dev = netdev;
1503 buffer->skb = skb;
1504 buffer->state = NETXEN_BUFFER_BUSY;
1505 buffer->dma = pci_map_single(pdev, skb->data,
1506 rcv_desc->dma_size,
1507 PCI_DMA_FROMDEVICE);
1509 /* make a rcv descriptor */
1510 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1511 pdesc->buffer_length = cpu_to_le32(rcv_desc->dma_size);
1512 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1513 DPRINTK(INFO, "done writing descripter\n");
1514 producer =
1515 get_next_index(producer, rcv_desc->max_rx_desc_count);
1516 index = get_next_index(index, rcv_desc->max_rx_desc_count);
1517 buffer = &rcv_desc->rx_buf_arr[index];
1520 /* if we did allocate buffers, then write the count to Phantom */
1521 if (count) {
1522 rcv_desc->begin_alloc = index;
1523 rcv_desc->rcv_pending += count;
1524 rcv_desc->producer = producer;
1525 if (rcv_desc->rcv_free >= 32) {
1526 rcv_desc->rcv_free = 0;
1527 /* Window = 1 */
1528 writel((producer - 1) &
1529 (rcv_desc->max_rx_desc_count - 1),
1530 NETXEN_CRB_NORMALIZE(adapter,
1531 recv_crb_registers[
1532 adapter->portnum].
1533 rcv_desc_crb[ringid].
1534 crb_rcv_producer_offset));
1535 wmb();
1540 int netxen_nic_tx_has_work(struct netxen_adapter *adapter)
1542 if (find_diff_among(adapter->last_cmd_consumer,
1543 adapter->cmd_producer,
1544 adapter->max_tx_desc_count) > 0)
1545 return 1;
1547 return 0;
1551 void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1553 memset(&adapter->stats, 0, sizeof(adapter->stats));
1554 return;