blob: c3e41f368554b6e3c321e21e66041fc77bd72013 [file] [log] [blame]
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001/*
2 * Copyright (C) 2003 - 2006 NetXen, Inc.
3 * All rights reserved.
Amit S. Kale80922fb2006-12-04 09:18:00 -08004 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -04005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
Amit S. Kale80922fb2006-12-04 09:18:00 -08009 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040010 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Amit S. Kale80922fb2006-12-04 09:18:00 -080014 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 * MA 02111-1307, USA.
Amit S. Kale80922fb2006-12-04 09:18:00 -080019 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040020 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
Amit S. Kale80922fb2006-12-04 09:18:00 -080022 *
Amit S. Kale3d396eb2006-10-21 15:33:03 -040023 * Contact Information:
24 * info@netxen.com
25 * NetXen,
26 * 3965 Freedom Circle, Fourth floor,
27 * Santa Clara, CA 95054
28 *
29 *
30 * Source file for NIC routines to initialize the Phantom Hardware
31 *
32 */
33
34#include <linux/netdevice.h>
35#include <linux/delay.h>
36#include "netxen_nic.h"
37#include "netxen_nic_hw.h"
Amit S. Kale3d396eb2006-10-21 15:33:03 -040038#include "netxen_nic_phan_reg.h"
39
40struct crb_addr_pair {
41 long addr;
42 long data;
43};
44
45#define NETXEN_MAX_CRB_XFORM 60
46static unsigned int crb_addr_xform[NETXEN_MAX_CRB_XFORM];
47#define NETXEN_ADDR_ERROR ((unsigned long ) 0xffffffff )
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
Amit S. Kalecb8011a2006-11-29 09:00:10 -080053#define NETXEN_NIC_XDMA_RESET 0x8000ff
54
Amit S. Kale3d396eb2006-10-21 15:33:03 -040055static inline void
56netxen_nic_locked_write_reg(struct netxen_adapter *adapter,
57 unsigned long off, int *data)
58{
Amit S. Kalecb8011a2006-11-29 09:00:10 -080059 void __iomem *addr = pci_base_offset(adapter, off);
Amit S. Kale3d396eb2006-10-21 15:33:03 -040060 writel(*data, addr);
61}
62
63static void crb_addr_transform_setup(void)
64{
65 crb_addr_transform(XDMA);
66 crb_addr_transform(TIMR);
67 crb_addr_transform(SRE);
68 crb_addr_transform(SQN3);
69 crb_addr_transform(SQN2);
70 crb_addr_transform(SQN1);
71 crb_addr_transform(SQN0);
72 crb_addr_transform(SQS3);
73 crb_addr_transform(SQS2);
74 crb_addr_transform(SQS1);
75 crb_addr_transform(SQS0);
76 crb_addr_transform(RPMX7);
77 crb_addr_transform(RPMX6);
78 crb_addr_transform(RPMX5);
79 crb_addr_transform(RPMX4);
80 crb_addr_transform(RPMX3);
81 crb_addr_transform(RPMX2);
82 crb_addr_transform(RPMX1);
83 crb_addr_transform(RPMX0);
84 crb_addr_transform(ROMUSB);
85 crb_addr_transform(SN);
86 crb_addr_transform(QMN);
87 crb_addr_transform(QMS);
88 crb_addr_transform(PGNI);
89 crb_addr_transform(PGND);
90 crb_addr_transform(PGN3);
91 crb_addr_transform(PGN2);
92 crb_addr_transform(PGN1);
93 crb_addr_transform(PGN0);
94 crb_addr_transform(PGSI);
95 crb_addr_transform(PGSD);
96 crb_addr_transform(PGS3);
97 crb_addr_transform(PGS2);
98 crb_addr_transform(PGS1);
99 crb_addr_transform(PGS0);
100 crb_addr_transform(PS);
101 crb_addr_transform(PH);
102 crb_addr_transform(NIU);
103 crb_addr_transform(I2Q);
104 crb_addr_transform(EG);
105 crb_addr_transform(MN);
106 crb_addr_transform(MS);
107 crb_addr_transform(CAS2);
108 crb_addr_transform(CAS1);
109 crb_addr_transform(CAS0);
110 crb_addr_transform(CAM);
111 crb_addr_transform(C2C1);
112 crb_addr_transform(C2C0);
113}
114
115int netxen_init_firmware(struct netxen_adapter *adapter)
116{
117 u32 state = 0, loops = 0, err = 0;
118
119 /* Window 1 call */
120 state = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
121
122 if (state == PHAN_INITIALIZE_ACK)
123 return 0;
124
125 while (state != PHAN_INITIALIZE_COMPLETE && loops < 2000) {
126 udelay(100);
127 /* Window 1 call */
128 state = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
129
130 loops++;
131 }
132 if (loops >= 2000) {
133 printk(KERN_ERR "Cmd Peg initialization not complete:%x.\n",
134 state);
135 err = -EIO;
136 return err;
137 }
138 /* Window 1 call */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800139 writel(MPORT_SINGLE_FUNCTION_MODE,
140 NETXEN_CRB_NORMALIZE(adapter, CRB_MPORT_MODE));
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400141 writel(PHAN_INITIALIZE_ACK,
142 NETXEN_CRB_NORMALIZE(adapter, CRB_CMDPEG_STATE));
143
144 return err;
145}
146
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800147#define NETXEN_ADDR_LIMIT 0xffffffffULL
148
149void *netxen_alloc(struct pci_dev *pdev, size_t sz, dma_addr_t * ptr,
150 struct pci_dev **used_dev)
151{
152 void *addr;
153
154 addr = pci_alloc_consistent(pdev, sz, ptr);
155 if ((unsigned long long)(*ptr) < NETXEN_ADDR_LIMIT) {
156 *used_dev = pdev;
157 return addr;
158 }
159 pci_free_consistent(pdev, sz, addr, *ptr);
160 addr = pci_alloc_consistent(NULL, sz, ptr);
161 *used_dev = NULL;
162 return addr;
163}
164
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400165void netxen_initialize_adapter_sw(struct netxen_adapter *adapter)
166{
167 int ctxid, ring;
168 u32 i;
169 u32 num_rx_bufs = 0;
170 struct netxen_rcv_desc_ctx *rcv_desc;
171
172 DPRINTK(INFO, "initializing some queues: %p\n", adapter);
173 for (ctxid = 0; ctxid < MAX_RCV_CTX; ++ctxid) {
174 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
175 struct netxen_rx_buffer *rx_buf;
176 rcv_desc = &adapter->recv_ctx[ctxid].rcv_desc[ring];
177 rcv_desc->rcv_free = rcv_desc->max_rx_desc_count;
178 rcv_desc->begin_alloc = 0;
179 rx_buf = rcv_desc->rx_buf_arr;
180 num_rx_bufs = rcv_desc->max_rx_desc_count;
181 /*
182 * Now go through all of them, set reference handles
183 * and put them in the queues.
184 */
185 for (i = 0; i < num_rx_bufs; i++) {
186 rx_buf->ref_handle = i;
187 rx_buf->state = NETXEN_BUFFER_FREE;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400188 DPRINTK(INFO, "Rx buf:ctx%d i(%d) rx_buf:"
189 "%p\n", ctxid, i, rx_buf);
190 rx_buf++;
191 }
192 }
193 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400194}
195
196void netxen_initialize_adapter_hw(struct netxen_adapter *adapter)
197{
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800198 int ports = 0;
199 struct netxen_board_info *board_info = &(adapter->ahw.boardcfg);
200
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400201 if (netxen_nic_get_board_info(adapter) != 0)
202 printk("%s: Error getting board config info.\n",
203 netxen_nic_driver_name);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800204 get_brd_port_by_type(board_info->board_type, &ports);
205 if (ports == 0)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400206 printk(KERN_ERR "%s: Unknown board type\n",
207 netxen_nic_driver_name);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800208 adapter->ahw.max_ports = ports;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400209}
210
211void netxen_initialize_adapter_ops(struct netxen_adapter *adapter)
212{
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400213 switch (adapter->ahw.board_type) {
214 case NETXEN_NIC_GBE:
Amit S. Kale80922fb2006-12-04 09:18:00 -0800215 adapter->enable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400216 netxen_niu_gbe_enable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800217 adapter->disable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400218 netxen_niu_gbe_disable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800219 adapter->handle_phy_intr = netxen_nic_gbe_handle_phy_intr;
220 adapter->macaddr_set = netxen_niu_macaddr_set;
221 adapter->set_mtu = netxen_nic_set_mtu_gb;
222 adapter->set_promisc = netxen_niu_set_promiscuous_mode;
223 adapter->unset_promisc = netxen_niu_set_promiscuous_mode;
224 adapter->phy_read = netxen_niu_gbe_phy_read;
225 adapter->phy_write = netxen_niu_gbe_phy_write;
226 adapter->init_port = netxen_niu_gbe_init_port;
227 adapter->init_niu = netxen_nic_init_niu_gb;
228 adapter->stop_port = netxen_niu_disable_gbe_port;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400229 break;
230
231 case NETXEN_NIC_XGBE:
Amit S. Kale80922fb2006-12-04 09:18:00 -0800232 adapter->enable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400233 netxen_niu_xgbe_enable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800234 adapter->disable_phy_interrupts =
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400235 netxen_niu_xgbe_disable_phy_interrupts;
Amit S. Kale80922fb2006-12-04 09:18:00 -0800236 adapter->handle_phy_intr = netxen_nic_xgbe_handle_phy_intr;
237 adapter->macaddr_set = netxen_niu_xg_macaddr_set;
238 adapter->set_mtu = netxen_nic_set_mtu_xgb;
239 adapter->init_port = netxen_niu_xg_init_port;
240 adapter->set_promisc = netxen_niu_xg_set_promiscuous_mode;
241 adapter->unset_promisc = netxen_niu_xg_set_promiscuous_mode;
242 adapter->stop_port = netxen_niu_disable_xg_port;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400243 break;
244
245 default:
246 break;
247 }
248}
249
250/*
251 * netxen_decode_crb_addr(0 - utility to translate from internal Phantom CRB
252 * address to external PCI CRB address.
253 */
254unsigned long netxen_decode_crb_addr(unsigned long addr)
255{
256 int i;
257 unsigned long base_addr, offset, pci_base;
258
259 crb_addr_transform_setup();
260
261 pci_base = NETXEN_ADDR_ERROR;
262 base_addr = addr & 0xfff00000;
263 offset = addr & 0x000fffff;
264
265 for (i = 0; i < NETXEN_MAX_CRB_XFORM; i++) {
266 if (crb_addr_xform[i] == base_addr) {
267 pci_base = i << 20;
268 break;
269 }
270 }
271 if (pci_base == NETXEN_ADDR_ERROR)
272 return pci_base;
273 else
274 return (pci_base + offset);
275}
276
277static long rom_max_timeout = 10000;
278static long rom_lock_timeout = 1000000;
279
280static inline int rom_lock(struct netxen_adapter *adapter)
281{
282 int iter;
283 u32 done = 0;
284 int timeout = 0;
285
286 while (!done) {
287 /* acquire semaphore2 from PCI HW block */
288 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_LOCK),
289 &done);
290 if (done == 1)
291 break;
292 if (timeout >= rom_lock_timeout)
293 return -EIO;
294
295 timeout++;
296 /*
297 * Yield CPU
298 */
299 if (!in_atomic())
300 schedule();
301 else {
302 for (iter = 0; iter < 20; iter++)
303 cpu_relax(); /*This a nop instr on i386 */
304 }
305 }
306 netxen_nic_reg_write(adapter, NETXEN_ROM_LOCK_ID, ROM_LOCK_DRIVER);
307 return 0;
308}
309
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400310int netxen_wait_rom_done(struct netxen_adapter *adapter)
311{
312 long timeout = 0;
313 long done = 0;
314
315 while (done == 0) {
316 done = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_GLB_STATUS);
317 done &= 2;
318 timeout++;
319 if (timeout >= rom_max_timeout) {
320 printk("Timeout reached waiting for rom done");
321 return -EIO;
322 }
323 }
324 return 0;
325}
326
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800327static inline int netxen_rom_wren(struct netxen_adapter *adapter)
328{
329 /* Set write enable latch in ROM status register */
330 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
331 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
332 M25P_INSTR_WREN);
333 if (netxen_wait_rom_done(adapter)) {
334 return -1;
335 }
336 return 0;
337}
338
339static inline unsigned int netxen_rdcrbreg(struct netxen_adapter *adapter,
340 unsigned int addr)
341{
342 unsigned int data = 0xdeaddead;
343 data = netxen_nic_reg_read(adapter, addr);
344 return data;
345}
346
347static inline int netxen_do_rom_rdsr(struct netxen_adapter *adapter)
348{
349 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
350 M25P_INSTR_RDSR);
351 if (netxen_wait_rom_done(adapter)) {
352 return -1;
353 }
354 return netxen_rdcrbreg(adapter, NETXEN_ROMUSB_ROM_RDATA);
355}
356
357static inline void netxen_rom_unlock(struct netxen_adapter *adapter)
358{
359 u32 val;
360
361 /* release semaphore2 */
362 netxen_nic_read_w0(adapter, NETXEN_PCIE_REG(PCIE_SEM2_UNLOCK), &val);
363
364}
365
366int netxen_rom_wip_poll(struct netxen_adapter *adapter)
367{
368 long timeout = 0;
369 long wip = 1;
370 int val;
371 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
372 while (wip != 0) {
373 val = netxen_do_rom_rdsr(adapter);
374 wip = val & 1;
375 timeout++;
376 if (timeout > rom_max_timeout) {
377 return -1;
378 }
379 }
380 return 0;
381}
382
Amit S. Kale80922fb2006-12-04 09:18:00 -0800383static inline int do_rom_fast_write(struct netxen_adapter *adapter, int addr,
384 int data)
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800385{
386 if (netxen_rom_wren(adapter)) {
387 return -1;
388 }
389 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_WDATA, data);
390 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
391 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
392 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
393 M25P_INSTR_PP);
394 if (netxen_wait_rom_done(adapter)) {
395 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
396 return -1;
397 }
398
399 return netxen_rom_wip_poll(adapter);
400}
401
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400402static inline int
403do_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
404{
405 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
406 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
407 udelay(100); /* prevent bursting on CRB */
408 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
409 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE, 0xb);
410 if (netxen_wait_rom_done(adapter)) {
411 printk("Error waiting for rom done\n");
412 return -EIO;
413 }
414 /* reset abyte_cnt and dummy_byte_cnt */
415 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
416 udelay(100); /* prevent bursting on CRB */
417 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
418
419 *valp = netxen_nic_reg_read(adapter, NETXEN_ROMUSB_ROM_RDATA);
420 return 0;
421}
422
423int netxen_rom_fast_read(struct netxen_adapter *adapter, int addr, int *valp)
424{
425 int ret;
426
427 if (rom_lock(adapter) != 0)
428 return -EIO;
429
430 ret = do_rom_fast_read(adapter, addr, valp);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800431 netxen_rom_unlock(adapter);
432 return ret;
433}
434
435int netxen_rom_fast_write(struct netxen_adapter *adapter, int addr, int data)
436{
437 int ret = 0;
438
439 if (rom_lock(adapter) != 0) {
440 return -1;
441 }
442 ret = do_rom_fast_write(adapter, addr, data);
443 netxen_rom_unlock(adapter);
444 return ret;
445}
446int netxen_do_rom_se(struct netxen_adapter *adapter, int addr)
447{
448 netxen_rom_wren(adapter);
449 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ADDRESS, addr);
450 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 3);
451 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_INSTR_OPCODE,
452 M25P_INSTR_SE);
453 if (netxen_wait_rom_done(adapter)) {
454 netxen_nic_reg_write(adapter, NETXEN_ROMUSB_ROM_ABYTE_CNT, 0);
455 return -1;
456 }
457 return netxen_rom_wip_poll(adapter);
458}
459
460int netxen_rom_se(struct netxen_adapter *adapter, int addr)
461{
462 int ret = 0;
463 if (rom_lock(adapter) != 0) {
464 return -1;
465 }
466 ret = netxen_do_rom_se(adapter, addr);
467 netxen_rom_unlock(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400468 return ret;
469}
470
471#define NETXEN_BOARDTYPE 0x4008
472#define NETXEN_BOARDNUM 0x400c
473#define NETXEN_CHIPNUM 0x4010
474#define NETXEN_ROMBUS_RESET 0xFFFFFFFF
475#define NETXEN_ROM_FIRST_BARRIER 0x800000000ULL
476#define NETXEN_ROM_FOUND_INIT 0x400
477
478int netxen_pinit_from_rom(struct netxen_adapter *adapter, int verbose)
479{
480 int addr, val, status;
481 int n, i;
482 int init_delay = 0;
483 struct crb_addr_pair *buf;
484 unsigned long off;
485
486 /* resetall */
487 status = netxen_nic_get_board_info(adapter);
488 if (status)
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800489 printk("%s: netxen_pinit_from_rom: Error getting board info\n",
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400490 netxen_nic_driver_name);
491
492 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
493 NETXEN_ROMBUS_RESET);
494
495 if (verbose) {
496 int val;
497 if (netxen_rom_fast_read(adapter, NETXEN_BOARDTYPE, &val) == 0)
498 printk("P2 ROM board type: 0x%08x\n", val);
499 else
500 printk("Could not read board type\n");
501 if (netxen_rom_fast_read(adapter, NETXEN_BOARDNUM, &val) == 0)
502 printk("P2 ROM board num: 0x%08x\n", val);
503 else
504 printk("Could not read board number\n");
505 if (netxen_rom_fast_read(adapter, NETXEN_CHIPNUM, &val) == 0)
506 printk("P2 ROM chip num: 0x%08x\n", val);
507 else
508 printk("Could not read chip number\n");
509 }
510
511 if (netxen_rom_fast_read(adapter, 0, &n) == 0
512 && (n & NETXEN_ROM_FIRST_BARRIER)) {
513 n &= ~NETXEN_ROM_ROUNDUP;
514 if (n < NETXEN_ROM_FOUND_INIT) {
515 if (verbose)
516 printk("%s: %d CRB init values found"
517 " in ROM.\n", netxen_nic_driver_name, n);
518 } else {
519 printk("%s:n=0x%x Error! NetXen card flash not"
520 " initialized.\n", __FUNCTION__, n);
521 return -EIO;
522 }
523 buf = kcalloc(n, sizeof(struct crb_addr_pair), GFP_KERNEL);
524 if (buf == NULL) {
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800525 printk("%s: netxen_pinit_from_rom: Unable to calloc "
526 "memory.\n", netxen_nic_driver_name);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400527 return -ENOMEM;
528 }
529 for (i = 0; i < n; i++) {
530 if (netxen_rom_fast_read(adapter, 8 * i + 4, &val) != 0
531 || netxen_rom_fast_read(adapter, 8 * i + 8,
532 &addr) != 0)
533 return -EIO;
534
535 buf[i].addr = addr;
536 buf[i].data = val;
537
538 if (verbose)
539 printk("%s: PCI: 0x%08x == 0x%08x\n",
540 netxen_nic_driver_name, (unsigned int)
541 netxen_decode_crb_addr((unsigned long)
542 addr), val);
543 }
544 for (i = 0; i < n; i++) {
545
546 off =
547 netxen_decode_crb_addr((unsigned long)buf[i].addr) +
548 NETXEN_PCI_CRBSPACE;
549 /* skipping cold reboot MAGIC */
550 if (off == NETXEN_CAM_RAM(0x1fc))
551 continue;
552
553 /* After writing this register, HW needs time for CRB */
554 /* to quiet down (else crb_window returns 0xffffffff) */
555 if (off == NETXEN_ROMUSB_GLB_SW_RESET) {
556 init_delay = 1;
557 /* hold xdma in reset also */
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800558 buf[i].data = NETXEN_NIC_XDMA_RESET;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400559 }
560
561 if (ADDR_IN_WINDOW1(off)) {
562 writel(buf[i].data,
563 NETXEN_CRB_NORMALIZE(adapter, off));
564 } else {
565 netxen_nic_pci_change_crbwindow(adapter, 0);
566 writel(buf[i].data,
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800567 pci_base_offset(adapter, off));
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400568
569 netxen_nic_pci_change_crbwindow(adapter, 1);
570 }
571 if (init_delay == 1) {
572 ssleep(1);
573 init_delay = 0;
574 }
575 msleep(1);
576 }
577 kfree(buf);
578
579 /* disable_peg_cache_all */
580
581 /* unreset_net_cache */
582 netxen_nic_hw_read_wx(adapter, NETXEN_ROMUSB_GLB_SW_RESET, &val,
583 4);
584 netxen_crb_writelit_adapter(adapter, NETXEN_ROMUSB_GLB_SW_RESET,
585 (val & 0xffffff0f));
586 /* p2dn replyCount */
587 netxen_crb_writelit_adapter(adapter,
588 NETXEN_CRB_PEG_NET_D + 0xec, 0x1e);
589 /* disable_peg_cache 0 */
590 netxen_crb_writelit_adapter(adapter,
591 NETXEN_CRB_PEG_NET_D + 0x4c, 8);
592 /* disable_peg_cache 1 */
593 netxen_crb_writelit_adapter(adapter,
594 NETXEN_CRB_PEG_NET_I + 0x4c, 8);
595
596 /* peg_clr_all */
597
598 /* peg_clr 0 */
599 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0x8,
600 0);
601 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_0 + 0xc,
602 0);
603 /* peg_clr 1 */
604 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0x8,
605 0);
606 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_1 + 0xc,
607 0);
608 /* peg_clr 2 */
609 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0x8,
610 0);
611 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_2 + 0xc,
612 0);
613 /* peg_clr 3 */
614 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0x8,
615 0);
616 netxen_crb_writelit_adapter(adapter, NETXEN_CRB_PEG_NET_3 + 0xc,
617 0);
618 }
619 return 0;
620}
621
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800622int netxen_initialize_adapter_offload(struct netxen_adapter *adapter)
623{
624 uint64_t addr;
625 uint32_t hi;
626 uint32_t lo;
627
628 adapter->dummy_dma.addr =
629 pci_alloc_consistent(adapter->ahw.pdev,
630 NETXEN_HOST_DUMMY_DMA_SIZE,
631 &adapter->dummy_dma.phys_addr);
632 if (adapter->dummy_dma.addr == NULL) {
633 printk("%s: ERROR: Could not allocate dummy DMA memory\n",
634 __FUNCTION__);
635 return -ENOMEM;
636 }
637
638 addr = (uint64_t) adapter->dummy_dma.phys_addr;
639 hi = (addr >> 32) & 0xffffffff;
640 lo = addr & 0xffffffff;
641
642 writel(hi, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_DUMMY_BUF_ADDR_HI));
643 writel(lo, NETXEN_CRB_NORMALIZE(adapter, CRB_HOST_DUMMY_BUF_ADDR_LO));
644
645 return 0;
646}
647
648void netxen_free_adapter_offload(struct netxen_adapter *adapter)
649{
650 if (adapter->dummy_dma.addr) {
651 pci_free_consistent(adapter->ahw.pdev,
652 NETXEN_HOST_DUMMY_DMA_SIZE,
653 adapter->dummy_dma.addr,
654 adapter->dummy_dma.phys_addr);
655 adapter->dummy_dma.addr = NULL;
656 }
657}
658
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800659void netxen_phantom_init(struct netxen_adapter *adapter, int pegtune_val)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400660{
661 u32 val = 0;
662 int loops = 0;
663
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800664 if (!pegtune_val) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400665 while (val != PHAN_INITIALIZE_COMPLETE && loops < 200000) {
666 udelay(100);
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800667 schedule();
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400668 val =
669 readl(NETXEN_CRB_NORMALIZE
670 (adapter, CRB_CMDPEG_STATE));
671 loops++;
672 }
673 if (val != PHAN_INITIALIZE_COMPLETE)
674 printk("WARNING: Initial boot wait loop failed...\n");
675 }
676}
677
678int netxen_nic_rx_has_work(struct netxen_adapter *adapter)
679{
680 int ctx;
681
682 for (ctx = 0; ctx < MAX_RCV_CTX; ++ctx) {
683 struct netxen_recv_context *recv_ctx =
684 &(adapter->recv_ctx[ctx]);
685 u32 consumer;
686 struct status_desc *desc_head;
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800687 struct status_desc *desc;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400688
689 consumer = recv_ctx->status_rx_consumer;
690 desc_head = recv_ctx->rcv_status_desc_head;
691 desc = &desc_head[consumer];
692
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800693 if (((le16_to_cpu(netxen_get_sts_owner(desc)))
694 & STATUS_OWNER_HOST))
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400695 return 1;
696 }
697
698 return 0;
699}
700
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800701static inline int netxen_nic_check_temp(struct netxen_adapter *adapter)
702{
703 int port_num;
704 struct netxen_port *port;
705 struct net_device *netdev;
706 uint32_t temp, temp_state, temp_val;
707 int rv = 0;
708
709 temp = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_TEMP_STATE));
710
711 temp_state = nx_get_temp_state(temp);
712 temp_val = nx_get_temp_val(temp);
713
714 if (temp_state == NX_TEMP_PANIC) {
715 printk(KERN_ALERT
716 "%s: Device temperature %d degrees C exceeds"
717 " maximum allowed. Hardware has been shut down.\n",
718 netxen_nic_driver_name, temp_val);
719 for (port_num = 0; port_num < adapter->ahw.max_ports;
720 port_num++) {
721 port = adapter->port[port_num];
722 netdev = port->netdev;
723
724 netif_carrier_off(netdev);
725 netif_stop_queue(netdev);
726 }
727 rv = 1;
728 } else if (temp_state == NX_TEMP_WARN) {
729 if (adapter->temp == NX_TEMP_NORMAL) {
730 printk(KERN_ALERT
731 "%s: Device temperature %d degrees C "
732 "exceeds operating range."
733 " Immediate action needed.\n",
734 netxen_nic_driver_name, temp_val);
735 }
736 } else {
737 if (adapter->temp == NX_TEMP_WARN) {
738 printk(KERN_INFO
739 "%s: Device temperature is now %d degrees C"
740 " in normal range.\n", netxen_nic_driver_name,
741 temp_val);
742 }
743 }
744 adapter->temp = temp_state;
745 return rv;
746}
747
David Howells6d5aefb2006-12-05 19:36:26 +0000748void netxen_watchdog_task(struct work_struct *work)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400749{
750 int port_num;
751 struct netxen_port *port;
752 struct net_device *netdev;
David Howells6d5aefb2006-12-05 19:36:26 +0000753 struct netxen_adapter *adapter =
754 container_of(work, struct netxen_adapter, watchdog_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400755
Amit S. Kalecb8011a2006-11-29 09:00:10 -0800756 if (netxen_nic_check_temp(adapter))
757 return;
758
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400759 for (port_num = 0; port_num < adapter->ahw.max_ports; port_num++) {
760 port = adapter->port[port_num];
761 netdev = port->netdev;
762
763 if ((netif_running(netdev)) && !netif_carrier_ok(netdev)) {
764 printk(KERN_INFO "%s port %d, %s carrier is now ok\n",
765 netxen_nic_driver_name, port_num, netdev->name);
766 netif_carrier_on(netdev);
767 }
768
769 if (netif_queue_stopped(netdev))
770 netif_wake_queue(netdev);
771 }
772
Amit S. Kale80922fb2006-12-04 09:18:00 -0800773 if (adapter->handle_phy_intr)
774 adapter->handle_phy_intr(adapter);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400775 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
776}
777
778/*
779 * netxen_process_rcv() send the received packet to the protocol stack.
780 * and if the number of receives exceeds RX_BUFFERS_REFILL, then we
781 * invoke the routine to send more rx buffers to the Phantom...
782 */
783void
784netxen_process_rcv(struct netxen_adapter *adapter, int ctxid,
785 struct status_desc *desc)
786{
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800787 struct netxen_port *port = adapter->port[netxen_get_sts_port(desc)];
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400788 struct pci_dev *pdev = port->pdev;
789 struct net_device *netdev = port->netdev;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800790 int index = le16_to_cpu(netxen_get_sts_refhandle(desc));
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400791 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
792 struct netxen_rx_buffer *buffer;
793 struct sk_buff *skb;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800794 u32 length = le16_to_cpu(netxen_get_sts_totallength(desc));
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400795 u32 desc_ctx;
796 struct netxen_rcv_desc_ctx *rcv_desc;
797 int ret;
798
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800799 desc_ctx = netxen_get_sts_type(desc);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400800 if (unlikely(desc_ctx >= NUM_RCV_DESC_RINGS)) {
801 printk("%s: %s Bad Rcv descriptor ring\n",
802 netxen_nic_driver_name, netdev->name);
803 return;
804 }
805
806 rcv_desc = &recv_ctx->rcv_desc[desc_ctx];
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800807 if (unlikely(index > rcv_desc->max_rx_desc_count)) {
808 DPRINTK(ERR, "Got a buffer index:%x Max is %x\n",
809 index, rcv_desc->max_rx_desc_count);
810 return;
811 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400812 buffer = &rcv_desc->rx_buf_arr[index];
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800813 if (desc_ctx == RCV_DESC_LRO_CTXID) {
814 buffer->lro_current_frags++;
815 if (netxen_get_sts_desc_lro_last_frag(desc)) {
816 buffer->lro_expected_frags =
817 netxen_get_sts_desc_lro_cnt(desc);
818 buffer->lro_length = length;
819 }
820 if (buffer->lro_current_frags != buffer->lro_expected_frags) {
821 if (buffer->lro_expected_frags != 0) {
822 printk("LRO: (refhandle:%x) recv frag."
823 "wait for last. flags: %x expected:%d"
824 "have:%d\n", index,
825 netxen_get_sts_desc_lro_last_frag(desc),
826 buffer->lro_expected_frags,
827 buffer->lro_current_frags);
828 }
829 return;
830 }
831 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400832
833 pci_unmap_single(pdev, buffer->dma, rcv_desc->dma_size,
834 PCI_DMA_FROMDEVICE);
835
836 skb = (struct sk_buff *)buffer->skb;
837
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800838 if (likely(netxen_get_sts_status(desc) == STATUS_CKSUM_OK)) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400839 port->stats.csummed++;
840 skb->ip_summed = CHECKSUM_UNNECESSARY;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800841 }
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400842 skb->dev = netdev;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800843 if (desc_ctx == RCV_DESC_LRO_CTXID) {
844 /* True length was only available on the last pkt */
845 skb_put(skb, buffer->lro_length);
846 } else {
847 skb_put(skb, length);
848 }
849
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400850 skb->protocol = eth_type_trans(skb, netdev);
851
852 ret = netif_receive_skb(skb);
853
854 /*
855 * RH: Do we need these stats on a regular basis. Can we get it from
856 * Linux stats.
857 */
858 switch (ret) {
859 case NET_RX_SUCCESS:
860 port->stats.uphappy++;
861 break;
862
863 case NET_RX_CN_LOW:
864 port->stats.uplcong++;
865 break;
866
867 case NET_RX_CN_MOD:
868 port->stats.upmcong++;
869 break;
870
871 case NET_RX_CN_HIGH:
872 port->stats.uphcong++;
873 break;
874
875 case NET_RX_DROP:
876 port->stats.updropped++;
877 break;
878
879 default:
880 port->stats.updunno++;
881 break;
882 }
883
884 netdev->last_rx = jiffies;
885
886 rcv_desc->rcv_free++;
887 rcv_desc->rcv_pending--;
888
889 /*
890 * We just consumed one buffer so post a buffer.
891 */
892 adapter->stats.post_called++;
893 buffer->skb = NULL;
894 buffer->state = NETXEN_BUFFER_FREE;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800895 buffer->lro_current_frags = 0;
896 buffer->lro_expected_frags = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400897
898 port->stats.no_rcv++;
899 port->stats.rxbytes += length;
900}
901
902/* Process Receive status ring */
903u32 netxen_process_rcv_ring(struct netxen_adapter *adapter, int ctxid, int max)
904{
905 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctxid]);
906 struct status_desc *desc_head = recv_ctx->rcv_status_desc_head;
907 struct status_desc *desc; /* used to read status desc here */
908 u32 consumer = recv_ctx->status_rx_consumer;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800909 u32 producer = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400910 int count = 0, ring;
911
912 DPRINTK(INFO, "procesing receive\n");
913 /*
914 * we assume in this case that there is only one port and that is
915 * port #1...changes need to be done in firmware to indicate port
916 * number as part of the descriptor. This way we will be able to get
917 * the netdev which is associated with that device.
918 */
919 while (count < max) {
920 desc = &desc_head[consumer];
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800921 if (!
922 (le16_to_cpu(netxen_get_sts_owner(desc)) &
923 STATUS_OWNER_HOST)) {
924 DPRINTK(ERR, "desc %p ownedby %x\n", desc,
925 netxen_get_sts_owner(desc));
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400926 break;
927 }
928 netxen_process_rcv(adapter, ctxid, desc);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800929 netxen_clear_sts_owner(desc);
Amit S. Kaleed33ebe2006-12-18 05:53:59 -0800930 netxen_set_sts_owner(desc, cpu_to_le16(STATUS_OWNER_PHANTOM));
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400931 consumer = (consumer + 1) & (adapter->max_rx_desc_count - 1);
932 count++;
933 }
934 if (count) {
935 for (ring = 0; ring < NUM_RCV_DESC_RINGS; ring++) {
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800936 netxen_post_rx_buffers_nodb(adapter, ctxid, ring);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400937 }
938 }
939
940 /* update the consumer index in phantom */
941 if (count) {
942 adapter->stats.process_rcv++;
943 recv_ctx->status_rx_consumer = consumer;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800944 recv_ctx->status_rx_producer = producer;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400945
946 /* Window = 1 */
947 writel(consumer,
948 NETXEN_CRB_NORMALIZE(adapter,
949 recv_crb_registers[ctxid].
950 crb_rcv_status_consumer));
951 }
952
953 return count;
954}
955
956/* Process Command status ring */
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800957int netxen_process_cmd_ring(unsigned long data)
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400958{
959 u32 last_consumer;
960 u32 consumer;
961 struct netxen_adapter *adapter = (struct netxen_adapter *)data;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800962 int count1 = 0;
963 int count2 = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400964 struct netxen_cmd_buffer *buffer;
965 struct netxen_port *port; /* port #1 */
966 struct netxen_port *nport;
967 struct pci_dev *pdev;
968 struct netxen_skb_frag *frag;
969 u32 i;
970 struct sk_buff *skb = NULL;
971 int p;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800972 int done;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400973
974 spin_lock(&adapter->tx_lock);
975 last_consumer = adapter->last_cmd_consumer;
976 DPRINTK(INFO, "procesing xmit complete\n");
977 /* we assume in this case that there is only one port and that is
978 * port #1...changes need to be done in firmware to indicate port
979 * number as part of the descriptor. This way we will be able to get
980 * the netdev which is associated with that device.
981 */
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400982
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800983 consumer = *(adapter->cmd_consumer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400984 if (last_consumer == consumer) { /* Ring is empty */
985 DPRINTK(INFO, "last_consumer %d == consumer %d\n",
986 last_consumer, consumer);
987 spin_unlock(&adapter->tx_lock);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800988 return 1;
Amit S. Kale3d396eb2006-10-21 15:33:03 -0400989 }
990
991 adapter->proc_cmd_buf_counter++;
992 adapter->stats.process_xmit++;
993 /*
994 * Not needed - does not seem to be used anywhere.
995 * adapter->cmd_consumer = consumer;
996 */
997 spin_unlock(&adapter->tx_lock);
998
Amit S. Kaleed25ffa2006-12-04 09:23:25 -0800999 while ((last_consumer != consumer) && (count1 < MAX_STATUS_HANDLE)) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001000 buffer = &adapter->cmd_buf_arr[last_consumer];
1001 port = adapter->port[buffer->port];
1002 pdev = port->pdev;
1003 frag = &buffer->frag_array[0];
1004 skb = buffer->skb;
1005 if (skb && (cmpxchg(&buffer->skb, skb, 0) == skb)) {
1006 pci_unmap_single(pdev, frag->dma, frag->length,
1007 PCI_DMA_TODEVICE);
1008 for (i = 1; i < buffer->frag_count; i++) {
1009 DPRINTK(INFO, "getting fragment no %d\n", i);
1010 frag++; /* Get the next frag */
1011 pci_unmap_page(pdev, frag->dma, frag->length,
1012 PCI_DMA_TODEVICE);
1013 }
1014
1015 port->stats.skbfreed++;
1016 dev_kfree_skb_any(skb);
1017 skb = NULL;
1018 } else if (adapter->proc_cmd_buf_counter == 1) {
1019 port->stats.txnullskb++;
1020 }
1021 if (unlikely(netif_queue_stopped(port->netdev)
1022 && netif_carrier_ok(port->netdev))
1023 && ((jiffies - port->netdev->trans_start) >
1024 port->netdev->watchdog_timeo)) {
Amit S. Kale6c586642006-12-18 05:53:36 -08001025 SCHEDULE_WORK(&port->tx_timeout_task);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001026 }
1027
1028 last_consumer = get_next_index(last_consumer,
1029 adapter->max_tx_desc_count);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001030 count1++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001031 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001032 adapter->stats.noxmitdone += count1;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001033
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001034 count2 = 0;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001035 spin_lock(&adapter->tx_lock);
1036 if ((--adapter->proc_cmd_buf_counter) == 0) {
1037 adapter->last_cmd_consumer = last_consumer;
1038 while ((adapter->last_cmd_consumer != consumer)
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001039 && (count2 < MAX_STATUS_HANDLE)) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001040 buffer =
1041 &adapter->cmd_buf_arr[adapter->last_cmd_consumer];
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001042 count2++;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001043 if (buffer->skb)
1044 break;
1045 else
1046 adapter->last_cmd_consumer =
1047 get_next_index(adapter->last_cmd_consumer,
1048 adapter->max_tx_desc_count);
1049 }
1050 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001051 if (count1 || count2) {
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001052 for (p = 0; p < adapter->ahw.max_ports; p++) {
1053 nport = adapter->port[p];
1054 if (netif_queue_stopped(nport->netdev)
1055 && (nport->flags & NETXEN_NETDEV_STATUS)) {
1056 netif_wake_queue(nport->netdev);
1057 nport->flags &= ~NETXEN_NETDEV_STATUS;
1058 }
1059 }
1060 }
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001061 /*
1062 * If everything is freed up to consumer then check if the ring is full
1063 * If the ring is full then check if more needs to be freed and
1064 * schedule the call back again.
1065 *
1066 * This happens when there are 2 CPUs. One could be freeing and the
1067 * other filling it. If the ring is full when we get out of here and
1068 * the card has already interrupted the host then the host can miss the
1069 * interrupt.
1070 *
1071 * There is still a possible race condition and the host could miss an
1072 * interrupt. The card has to take care of this.
1073 */
1074 if (adapter->last_cmd_consumer == consumer &&
1075 (((adapter->cmd_producer + 1) %
1076 adapter->max_tx_desc_count) == adapter->last_cmd_consumer)) {
1077 consumer = *(adapter->cmd_consumer);
1078 }
1079 done = (adapter->last_cmd_consumer == consumer);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001080
1081 spin_unlock(&adapter->tx_lock);
1082 DPRINTK(INFO, "last consumer is %d in %s\n", last_consumer,
1083 __FUNCTION__);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001084 return (done);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001085}
1086
1087/*
1088 * netxen_post_rx_buffers puts buffer in the Phantom memory
1089 */
1090void netxen_post_rx_buffers(struct netxen_adapter *adapter, u32 ctx, u32 ringid)
1091{
1092 struct pci_dev *pdev = adapter->ahw.pdev;
1093 struct sk_buff *skb;
1094 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1095 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001096 uint producer;
1097 struct rcv_desc *pdesc;
1098 struct netxen_rx_buffer *buffer;
1099 int count = 0;
1100 int index = 0;
1101 netxen_ctx_msg msg = 0;
1102 dma_addr_t dma;
1103
1104 adapter->stats.post_called++;
1105 rcv_desc = &recv_ctx->rcv_desc[ringid];
1106
1107 producer = rcv_desc->producer;
1108 index = rcv_desc->begin_alloc;
1109 buffer = &rcv_desc->rx_buf_arr[index];
1110 /* We can start writing rx descriptors into the phantom memory. */
1111 while (buffer->state == NETXEN_BUFFER_FREE) {
1112 skb = dev_alloc_skb(rcv_desc->skb_size);
1113 if (unlikely(!skb)) {
1114 /*
1115 * TODO
1116 * We need to schedule the posting of buffers to the pegs.
1117 */
1118 rcv_desc->begin_alloc = index;
1119 DPRINTK(ERR, "netxen_post_rx_buffers: "
1120 " allocated only %d buffers\n", count);
1121 break;
1122 }
1123
1124 count++; /* now there should be no failure */
1125 pdesc = &rcv_desc->desc_head[producer];
1126
1127#if defined(XGB_DEBUG)
1128 *(unsigned long *)(skb->head) = 0xc0debabe;
1129 if (skb_is_nonlinear(skb)) {
1130 printk("Allocated SKB @%p is nonlinear\n");
1131 }
1132#endif
1133 skb_reserve(skb, 2);
1134 /* This will be setup when we receive the
1135 * buffer after it has been filled FSL TBD TBD
1136 * skb->dev = netdev;
1137 */
1138 dma = pci_map_single(pdev, skb->data, rcv_desc->dma_size,
1139 PCI_DMA_FROMDEVICE);
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001140 pdesc->addr_buffer = cpu_to_le64(dma);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001141 buffer->skb = skb;
1142 buffer->state = NETXEN_BUFFER_BUSY;
1143 buffer->dma = dma;
1144 /* make a rcv descriptor */
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001145 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1146 pdesc->buffer_length = cpu_to_le32(rcv_desc->dma_size);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001147 DPRINTK(INFO, "done writing descripter\n");
1148 producer =
1149 get_next_index(producer, rcv_desc->max_rx_desc_count);
1150 index = get_next_index(index, rcv_desc->max_rx_desc_count);
1151 buffer = &rcv_desc->rx_buf_arr[index];
1152 }
1153 /* if we did allocate buffers, then write the count to Phantom */
1154 if (count) {
1155 rcv_desc->begin_alloc = index;
1156 rcv_desc->rcv_pending += count;
1157 adapter->stats.lastposted = count;
1158 adapter->stats.posted += count;
1159 rcv_desc->producer = producer;
1160 if (rcv_desc->rcv_free >= 32) {
1161 rcv_desc->rcv_free = 0;
1162 /* Window = 1 */
1163 writel((producer - 1) &
1164 (rcv_desc->max_rx_desc_count - 1),
1165 NETXEN_CRB_NORMALIZE(adapter,
1166 recv_crb_registers[0].
1167 rcv_desc_crb[ringid].
1168 crb_rcv_producer_offset));
1169 /*
1170 * Write a doorbell msg to tell phanmon of change in
1171 * receive ring producer
1172 */
1173 netxen_set_msg_peg_id(msg, NETXEN_RCV_PEG_DB_ID);
1174 netxen_set_msg_privid(msg);
1175 netxen_set_msg_count(msg,
1176 ((producer -
1177 1) & (rcv_desc->
1178 max_rx_desc_count - 1)));
1179 netxen_set_msg_ctxid(msg, 0);
1180 netxen_set_msg_opcode(msg, NETXEN_RCV_PRODUCER(ringid));
1181 writel(msg,
1182 DB_NORMALIZE(adapter,
1183 NETXEN_RCV_PRODUCER_OFFSET));
1184 }
1185 }
1186}
1187
1188void netxen_post_rx_buffers_nodb(struct netxen_adapter *adapter, uint32_t ctx,
1189 uint32_t ringid)
1190{
1191 struct pci_dev *pdev = adapter->ahw.pdev;
1192 struct sk_buff *skb;
1193 struct netxen_recv_context *recv_ctx = &(adapter->recv_ctx[ctx]);
1194 struct netxen_rcv_desc_ctx *rcv_desc = NULL;
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001195 u32 producer;
1196 struct rcv_desc *pdesc;
1197 struct netxen_rx_buffer *buffer;
1198 int count = 0;
1199 int index = 0;
1200
1201 adapter->stats.post_called++;
1202 rcv_desc = &recv_ctx->rcv_desc[ringid];
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001203
1204 producer = rcv_desc->producer;
1205 index = rcv_desc->begin_alloc;
1206 buffer = &rcv_desc->rx_buf_arr[index];
1207 /* We can start writing rx descriptors into the phantom memory. */
1208 while (buffer->state == NETXEN_BUFFER_FREE) {
1209 skb = dev_alloc_skb(rcv_desc->skb_size);
1210 if (unlikely(!skb)) {
1211 /*
1212 * We need to schedule the posting of buffers to the pegs.
1213 */
1214 rcv_desc->begin_alloc = index;
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001215 DPRINTK(ERR, "netxen_post_rx_buffers_nodb: "
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001216 " allocated only %d buffers\n", count);
1217 break;
1218 }
1219 count++; /* now there should be no failure */
1220 pdesc = &rcv_desc->desc_head[producer];
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001221 skb_reserve(skb, 2);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001222 /*
1223 * This will be setup when we receive the
1224 * buffer after it has been filled
1225 * skb->dev = netdev;
1226 */
1227 buffer->skb = skb;
1228 buffer->state = NETXEN_BUFFER_BUSY;
1229 buffer->dma = pci_map_single(pdev, skb->data,
1230 rcv_desc->dma_size,
1231 PCI_DMA_FROMDEVICE);
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001232
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001233 /* make a rcv descriptor */
Amit S. Kaleed33ebe2006-12-18 05:53:59 -08001234 pdesc->reference_handle = cpu_to_le16(buffer->ref_handle);
1235 pdesc->buffer_length = cpu_to_le16(rcv_desc->dma_size);
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001236 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1237 DPRINTK(INFO, "done writing descripter\n");
1238 producer =
1239 get_next_index(producer, rcv_desc->max_rx_desc_count);
1240 index = get_next_index(index, rcv_desc->max_rx_desc_count);
1241 buffer = &rcv_desc->rx_buf_arr[index];
1242 }
1243
1244 /* if we did allocate buffers, then write the count to Phantom */
1245 if (count) {
1246 rcv_desc->begin_alloc = index;
1247 rcv_desc->rcv_pending += count;
1248 adapter->stats.lastposted = count;
1249 adapter->stats.posted += count;
1250 rcv_desc->producer = producer;
1251 if (rcv_desc->rcv_free >= 32) {
1252 rcv_desc->rcv_free = 0;
1253 /* Window = 1 */
1254 writel((producer - 1) &
1255 (rcv_desc->max_rx_desc_count - 1),
1256 NETXEN_CRB_NORMALIZE(adapter,
Amit S. Kaleed25ffa2006-12-04 09:23:25 -08001257 recv_crb_registers[0].
1258 rcv_desc_crb[ringid].
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001259 crb_rcv_producer_offset));
1260 wmb();
1261 }
1262 }
1263}
1264
1265int netxen_nic_tx_has_work(struct netxen_adapter *adapter)
1266{
1267 if (find_diff_among(adapter->last_cmd_consumer,
1268 adapter->cmd_producer,
1269 adapter->max_tx_desc_count) > 0)
1270 return 1;
1271
1272 return 0;
1273}
1274
Amit S. Kale3d396eb2006-10-21 15:33:03 -04001275
1276void netxen_nic_clear_stats(struct netxen_adapter *adapter)
1277{
1278 struct netxen_port *port;
1279 int port_num;
1280
1281 memset(&adapter->stats, 0, sizeof(adapter->stats));
1282 for (port_num = 0; port_num < adapter->ahw.max_ports; port_num++) {
1283 port = adapter->port[port_num];
1284 memset(&port->stats, 0, sizeof(port->stats));
1285 }
1286}
1287