blob: bccee68bd48a53fb671f2634d2b7bc3dd64cb36e [file] [log] [blame]
Ron Mercer5a4faa82006-07-25 00:40:21 -07001/*
2 * QLogic QLA3xxx NIC HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
4 *
5 * See LICENSE.qla3xxx for copyright and licensing details.
6 */
7
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/types.h>
11#include <linux/module.h>
12#include <linux/list.h>
13#include <linux/pci.h>
14#include <linux/dma-mapping.h>
15#include <linux/sched.h>
16#include <linux/slab.h>
17#include <linux/dmapool.h>
18#include <linux/mempool.h>
19#include <linux/spinlock.h>
20#include <linux/kthread.h>
21#include <linux/interrupt.h>
22#include <linux/errno.h>
23#include <linux/ioport.h>
24#include <linux/ip.h>
Ron Mercerbd36b0a2007-01-03 16:26:08 -080025#include <linux/in.h>
Ron Mercer5a4faa82006-07-25 00:40:21 -070026#include <linux/if_arp.h>
27#include <linux/if_ether.h>
28#include <linux/netdevice.h>
29#include <linux/etherdevice.h>
30#include <linux/ethtool.h>
31#include <linux/skbuff.h>
32#include <linux/rtnetlink.h>
33#include <linux/if_vlan.h>
Ron Mercer5a4faa82006-07-25 00:40:21 -070034#include <linux/delay.h>
35#include <linux/mm.h>
36
37#include "qla3xxx.h"
38
39#define DRV_NAME "qla3xxx"
40#define DRV_STRING "QLogic ISP3XXX Network Driver"
Ron Mercer201f27e2007-03-26 13:43:03 -070041#define DRV_VERSION "v2.03.00-k4"
Ron Mercer5a4faa82006-07-25 00:40:21 -070042#define PFX DRV_NAME " "
43
44static const char ql3xxx_driver_name[] = DRV_NAME;
45static const char ql3xxx_driver_version[] = DRV_VERSION;
46
47MODULE_AUTHOR("QLogic Corporation");
48MODULE_DESCRIPTION("QLogic ISP3XXX Network Driver " DRV_VERSION " ");
49MODULE_LICENSE("GPL");
50MODULE_VERSION(DRV_VERSION);
51
52static const u32 default_msg
53 = NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK
54 | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN;
55
56static int debug = -1; /* defaults above */
57module_param(debug, int, 0);
58MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
59
60static int msi;
61module_param(msi, int, 0);
62MODULE_PARM_DESC(msi, "Turn on Message Signaled Interrupts.");
63
64static struct pci_device_id ql3xxx_pci_tbl[] __devinitdata = {
65 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QL3022_DEVICE_ID)},
Ron Mercerbd36b0a2007-01-03 16:26:08 -080066 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QL3032_DEVICE_ID)},
Ron Mercer5a4faa82006-07-25 00:40:21 -070067 /* required last entry */
68 {0,}
69};
70
71MODULE_DEVICE_TABLE(pci, ql3xxx_pci_tbl);
72
73/*
Ron Mercer3efedf22007-03-26 12:43:52 -070074 * These are the known PHY's which are used
75 */
76typedef enum {
77 PHY_TYPE_UNKNOWN = 0,
78 PHY_VITESSE_VSC8211,
79 PHY_AGERE_ET1011C,
80 MAX_PHY_DEV_TYPES
81} PHY_DEVICE_et;
82
83typedef struct {
Jeff Garzik9ddf7772007-10-03 13:52:23 -040084 PHY_DEVICE_et phyDevice;
Ron Mercer3efedf22007-03-26 12:43:52 -070085 u32 phyIdOUI;
86 u16 phyIdModel;
87 char *name;
88} PHY_DEVICE_INFO_t;
89
Adrian Bunkb1fc1fa2007-03-31 22:55:40 +020090static const PHY_DEVICE_INFO_t PHY_DEVICES[] =
Ron Mercer3efedf22007-03-26 12:43:52 -070091 {{PHY_TYPE_UNKNOWN, 0x000000, 0x0, "PHY_TYPE_UNKNOWN"},
92 {PHY_VITESSE_VSC8211, 0x0003f1, 0xb, "PHY_VITESSE_VSC8211"},
93 {PHY_AGERE_ET1011C, 0x00a0bc, 0x1, "PHY_AGERE_ET1011C"},
94};
95
96
97/*
Ron Mercer5a4faa82006-07-25 00:40:21 -070098 * Caller must take hw_lock.
99 */
100static int ql_sem_spinlock(struct ql3_adapter *qdev,
101 u32 sem_mask, u32 sem_bits)
102{
103 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
104 u32 value;
105 unsigned int seconds = 3;
106
107 do {
108 writel((sem_mask | sem_bits),
109 &port_regs->CommonRegs.semaphoreReg);
110 value = readl(&port_regs->CommonRegs.semaphoreReg);
111 if ((value & (sem_mask >> 16)) == sem_bits)
112 return 0;
113 ssleep(1);
114 } while(--seconds);
115 return -1;
116}
117
118static void ql_sem_unlock(struct ql3_adapter *qdev, u32 sem_mask)
119{
120 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
121 writel(sem_mask, &port_regs->CommonRegs.semaphoreReg);
122 readl(&port_regs->CommonRegs.semaphoreReg);
123}
124
125static int ql_sem_lock(struct ql3_adapter *qdev, u32 sem_mask, u32 sem_bits)
126{
127 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
128 u32 value;
129
130 writel((sem_mask | sem_bits), &port_regs->CommonRegs.semaphoreReg);
131 value = readl(&port_regs->CommonRegs.semaphoreReg);
132 return ((value & (sem_mask >> 16)) == sem_bits);
133}
134
135/*
136 * Caller holds hw_lock.
137 */
138static int ql_wait_for_drvr_lock(struct ql3_adapter *qdev)
139{
140 int i = 0;
141
142 while (1) {
143 if (!ql_sem_lock(qdev,
144 QL_DRVR_SEM_MASK,
145 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index)
146 * 2) << 1)) {
147 if (i < 10) {
148 ssleep(1);
149 i++;
150 } else {
151 printk(KERN_ERR PFX "%s: Timed out waiting for "
152 "driver lock...\n",
153 qdev->ndev->name);
154 return 0;
155 }
156 } else {
157 printk(KERN_DEBUG PFX
158 "%s: driver lock acquired.\n",
159 qdev->ndev->name);
160 return 1;
161 }
162 }
163}
164
165static void ql_set_register_page(struct ql3_adapter *qdev, u32 page)
166{
167 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
168
169 writel(((ISP_CONTROL_NP_MASK << 16) | page),
170 &port_regs->CommonRegs.ispControlStatus);
171 readl(&port_regs->CommonRegs.ispControlStatus);
172 qdev->current_page = page;
173}
174
175static u32 ql_read_common_reg_l(struct ql3_adapter *qdev,
176 u32 __iomem * reg)
177{
178 u32 value;
179 unsigned long hw_flags;
180
181 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
182 value = readl(reg);
183 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
184
185 return value;
186}
187
188static u32 ql_read_common_reg(struct ql3_adapter *qdev,
189 u32 __iomem * reg)
190{
191 return readl(reg);
192}
193
194static u32 ql_read_page0_reg_l(struct ql3_adapter *qdev, u32 __iomem *reg)
195{
196 u32 value;
197 unsigned long hw_flags;
198
199 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
200
201 if (qdev->current_page != 0)
202 ql_set_register_page(qdev,0);
203 value = readl(reg);
204
205 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
206 return value;
207}
208
209static u32 ql_read_page0_reg(struct ql3_adapter *qdev, u32 __iomem *reg)
210{
211 if (qdev->current_page != 0)
212 ql_set_register_page(qdev,0);
213 return readl(reg);
214}
215
216static void ql_write_common_reg_l(struct ql3_adapter *qdev,
Al Viroee111d12006-09-25 02:53:53 +0100217 u32 __iomem *reg, u32 value)
Ron Mercer5a4faa82006-07-25 00:40:21 -0700218{
219 unsigned long hw_flags;
220
221 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
Al Viroee111d12006-09-25 02:53:53 +0100222 writel(value, reg);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700223 readl(reg);
224 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
225 return;
226}
227
228static void ql_write_common_reg(struct ql3_adapter *qdev,
Al Viroee111d12006-09-25 02:53:53 +0100229 u32 __iomem *reg, u32 value)
Ron Mercer5a4faa82006-07-25 00:40:21 -0700230{
Al Viroee111d12006-09-25 02:53:53 +0100231 writel(value, reg);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700232 readl(reg);
233 return;
234}
235
Ron Mercer80b02e52007-01-03 16:26:07 -0800236static void ql_write_nvram_reg(struct ql3_adapter *qdev,
237 u32 __iomem *reg, u32 value)
238{
239 writel(value, reg);
240 readl(reg);
241 udelay(1);
242 return;
243}
244
Ron Mercer5a4faa82006-07-25 00:40:21 -0700245static void ql_write_page0_reg(struct ql3_adapter *qdev,
Al Viroee111d12006-09-25 02:53:53 +0100246 u32 __iomem *reg, u32 value)
Ron Mercer5a4faa82006-07-25 00:40:21 -0700247{
248 if (qdev->current_page != 0)
249 ql_set_register_page(qdev,0);
Al Viroee111d12006-09-25 02:53:53 +0100250 writel(value, reg);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700251 readl(reg);
252 return;
253}
254
255/*
256 * Caller holds hw_lock. Only called during init.
257 */
258static void ql_write_page1_reg(struct ql3_adapter *qdev,
Al Viroee111d12006-09-25 02:53:53 +0100259 u32 __iomem *reg, u32 value)
Ron Mercer5a4faa82006-07-25 00:40:21 -0700260{
261 if (qdev->current_page != 1)
262 ql_set_register_page(qdev,1);
Al Viroee111d12006-09-25 02:53:53 +0100263 writel(value, reg);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700264 readl(reg);
265 return;
266}
267
268/*
269 * Caller holds hw_lock. Only called during init.
270 */
271static void ql_write_page2_reg(struct ql3_adapter *qdev,
Al Viroee111d12006-09-25 02:53:53 +0100272 u32 __iomem *reg, u32 value)
Ron Mercer5a4faa82006-07-25 00:40:21 -0700273{
274 if (qdev->current_page != 2)
275 ql_set_register_page(qdev,2);
Al Viroee111d12006-09-25 02:53:53 +0100276 writel(value, reg);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700277 readl(reg);
278 return;
279}
280
281static void ql_disable_interrupts(struct ql3_adapter *qdev)
282{
283 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
284
285 ql_write_common_reg_l(qdev, &port_regs->CommonRegs.ispInterruptMaskReg,
286 (ISP_IMR_ENABLE_INT << 16));
287
288}
289
290static void ql_enable_interrupts(struct ql3_adapter *qdev)
291{
292 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
293
294 ql_write_common_reg_l(qdev, &port_regs->CommonRegs.ispInterruptMaskReg,
295 ((0xff << 16) | ISP_IMR_ENABLE_INT));
296
297}
298
299static void ql_release_to_lrg_buf_free_list(struct ql3_adapter *qdev,
300 struct ql_rcv_buf_cb *lrg_buf_cb)
301{
Benjamin Li0f8ab892007-02-26 11:06:40 -0800302 dma_addr_t map;
303 int err;
Ron Mercer5a4faa82006-07-25 00:40:21 -0700304 lrg_buf_cb->next = NULL;
305
306 if (qdev->lrg_buf_free_tail == NULL) { /* The list is empty */
307 qdev->lrg_buf_free_head = qdev->lrg_buf_free_tail = lrg_buf_cb;
308 } else {
309 qdev->lrg_buf_free_tail->next = lrg_buf_cb;
310 qdev->lrg_buf_free_tail = lrg_buf_cb;
311 }
312
313 if (!lrg_buf_cb->skb) {
Benjamin Licd238fa2007-02-26 11:06:33 -0800314 lrg_buf_cb->skb = netdev_alloc_skb(qdev->ndev,
315 qdev->lrg_buffer_len);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700316 if (unlikely(!lrg_buf_cb->skb)) {
Benjamin Licd238fa2007-02-26 11:06:33 -0800317 printk(KERN_ERR PFX "%s: failed netdev_alloc_skb().\n",
Ron Mercer5a4faa82006-07-25 00:40:21 -0700318 qdev->ndev->name);
319 qdev->lrg_buf_skb_check++;
320 } else {
321 /*
322 * We save some space to copy the ethhdr from first
323 * buffer
324 */
325 skb_reserve(lrg_buf_cb->skb, QL_HEADER_SPACE);
326 map = pci_map_single(qdev->pdev,
327 lrg_buf_cb->skb->data,
328 qdev->lrg_buffer_len -
329 QL_HEADER_SPACE,
330 PCI_DMA_FROMDEVICE);
Benjamin Li0f8ab892007-02-26 11:06:40 -0800331 err = pci_dma_mapping_error(map);
332 if(err) {
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400333 printk(KERN_ERR "%s: PCI mapping failed with error: %d\n",
Benjamin Li0f8ab892007-02-26 11:06:40 -0800334 qdev->ndev->name, err);
335 dev_kfree_skb(lrg_buf_cb->skb);
336 lrg_buf_cb->skb = NULL;
337
338 qdev->lrg_buf_skb_check++;
339 return;
340 }
341
Ron Mercer5a4faa82006-07-25 00:40:21 -0700342 lrg_buf_cb->buf_phy_addr_low =
343 cpu_to_le32(LS_64BITS(map));
344 lrg_buf_cb->buf_phy_addr_high =
345 cpu_to_le32(MS_64BITS(map));
346 pci_unmap_addr_set(lrg_buf_cb, mapaddr, map);
347 pci_unmap_len_set(lrg_buf_cb, maplen,
348 qdev->lrg_buffer_len -
349 QL_HEADER_SPACE);
350 }
351 }
352
353 qdev->lrg_buf_free_count++;
354}
355
356static struct ql_rcv_buf_cb *ql_get_from_lrg_buf_free_list(struct ql3_adapter
357 *qdev)
358{
359 struct ql_rcv_buf_cb *lrg_buf_cb;
360
361 if ((lrg_buf_cb = qdev->lrg_buf_free_head) != NULL) {
362 if ((qdev->lrg_buf_free_head = lrg_buf_cb->next) == NULL)
363 qdev->lrg_buf_free_tail = NULL;
364 qdev->lrg_buf_free_count--;
365 }
366
367 return lrg_buf_cb;
368}
369
370static u32 addrBits = EEPROM_NO_ADDR_BITS;
371static u32 dataBits = EEPROM_NO_DATA_BITS;
372
373static void fm93c56a_deselect(struct ql3_adapter *qdev);
374static void eeprom_readword(struct ql3_adapter *qdev, u32 eepromAddr,
375 unsigned short *value);
376
377/*
378 * Caller holds hw_lock.
379 */
380static void fm93c56a_select(struct ql3_adapter *qdev)
381{
382 struct ql3xxx_port_registers __iomem *port_regs =
383 qdev->mem_map_registers;
384
385 qdev->eeprom_cmd_data = AUBURN_EEPROM_CS_1;
Ron Mercer80b02e52007-01-03 16:26:07 -0800386 ql_write_nvram_reg(qdev, &port_regs->CommonRegs.serialPortInterfaceReg,
Ron Mercer5a4faa82006-07-25 00:40:21 -0700387 ISP_NVRAM_MASK | qdev->eeprom_cmd_data);
Ron Mercer80b02e52007-01-03 16:26:07 -0800388 ql_write_nvram_reg(qdev, &port_regs->CommonRegs.serialPortInterfaceReg,
Ron Mercer5a4faa82006-07-25 00:40:21 -0700389 ((ISP_NVRAM_MASK << 16) | qdev->eeprom_cmd_data));
390}
391
392/*
393 * Caller holds hw_lock.
394 */
395static void fm93c56a_cmd(struct ql3_adapter *qdev, u32 cmd, u32 eepromAddr)
396{
397 int i;
398 u32 mask;
399 u32 dataBit;
400 u32 previousBit;
401 struct ql3xxx_port_registers __iomem *port_regs =
402 qdev->mem_map_registers;
403
404 /* Clock in a zero, then do the start bit */
Ron Mercer80b02e52007-01-03 16:26:07 -0800405 ql_write_nvram_reg(qdev, &port_regs->CommonRegs.serialPortInterfaceReg,
Ron Mercer5a4faa82006-07-25 00:40:21 -0700406 ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
407 AUBURN_EEPROM_DO_1);
Ron Mercer80b02e52007-01-03 16:26:07 -0800408 ql_write_nvram_reg(qdev, &port_regs->CommonRegs.serialPortInterfaceReg,
Ron Mercer5a4faa82006-07-25 00:40:21 -0700409 ISP_NVRAM_MASK | qdev->
410 eeprom_cmd_data | AUBURN_EEPROM_DO_1 |
411 AUBURN_EEPROM_CLK_RISE);
Ron Mercer80b02e52007-01-03 16:26:07 -0800412 ql_write_nvram_reg(qdev, &port_regs->CommonRegs.serialPortInterfaceReg,
Ron Mercer5a4faa82006-07-25 00:40:21 -0700413 ISP_NVRAM_MASK | qdev->
414 eeprom_cmd_data | AUBURN_EEPROM_DO_1 |
415 AUBURN_EEPROM_CLK_FALL);
416
417 mask = 1 << (FM93C56A_CMD_BITS - 1);
418 /* Force the previous data bit to be different */
419 previousBit = 0xffff;
420 for (i = 0; i < FM93C56A_CMD_BITS; i++) {
421 dataBit =
422 (cmd & mask) ? AUBURN_EEPROM_DO_1 : AUBURN_EEPROM_DO_0;
423 if (previousBit != dataBit) {
424 /*
425 * If the bit changed, then change the DO state to
426 * match
427 */
Ron Mercer80b02e52007-01-03 16:26:07 -0800428 ql_write_nvram_reg(qdev,
Ron Mercer5a4faa82006-07-25 00:40:21 -0700429 &port_regs->CommonRegs.
430 serialPortInterfaceReg,
431 ISP_NVRAM_MASK | qdev->
432 eeprom_cmd_data | dataBit);
433 previousBit = dataBit;
434 }
Ron Mercer80b02e52007-01-03 16:26:07 -0800435 ql_write_nvram_reg(qdev,
Ron Mercer5a4faa82006-07-25 00:40:21 -0700436 &port_regs->CommonRegs.
437 serialPortInterfaceReg,
438 ISP_NVRAM_MASK | qdev->
439 eeprom_cmd_data | dataBit |
440 AUBURN_EEPROM_CLK_RISE);
Ron Mercer80b02e52007-01-03 16:26:07 -0800441 ql_write_nvram_reg(qdev,
Ron Mercer5a4faa82006-07-25 00:40:21 -0700442 &port_regs->CommonRegs.
443 serialPortInterfaceReg,
444 ISP_NVRAM_MASK | qdev->
445 eeprom_cmd_data | dataBit |
446 AUBURN_EEPROM_CLK_FALL);
447 cmd = cmd << 1;
448 }
449
450 mask = 1 << (addrBits - 1);
451 /* Force the previous data bit to be different */
452 previousBit = 0xffff;
453 for (i = 0; i < addrBits; i++) {
454 dataBit =
455 (eepromAddr & mask) ? AUBURN_EEPROM_DO_1 :
456 AUBURN_EEPROM_DO_0;
457 if (previousBit != dataBit) {
458 /*
459 * If the bit changed, then change the DO state to
460 * match
461 */
Ron Mercer80b02e52007-01-03 16:26:07 -0800462 ql_write_nvram_reg(qdev,
Ron Mercer5a4faa82006-07-25 00:40:21 -0700463 &port_regs->CommonRegs.
464 serialPortInterfaceReg,
465 ISP_NVRAM_MASK | qdev->
466 eeprom_cmd_data | dataBit);
467 previousBit = dataBit;
468 }
Ron Mercer80b02e52007-01-03 16:26:07 -0800469 ql_write_nvram_reg(qdev,
Ron Mercer5a4faa82006-07-25 00:40:21 -0700470 &port_regs->CommonRegs.
471 serialPortInterfaceReg,
472 ISP_NVRAM_MASK | qdev->
473 eeprom_cmd_data | dataBit |
474 AUBURN_EEPROM_CLK_RISE);
Ron Mercer80b02e52007-01-03 16:26:07 -0800475 ql_write_nvram_reg(qdev,
Ron Mercer5a4faa82006-07-25 00:40:21 -0700476 &port_regs->CommonRegs.
477 serialPortInterfaceReg,
478 ISP_NVRAM_MASK | qdev->
479 eeprom_cmd_data | dataBit |
480 AUBURN_EEPROM_CLK_FALL);
481 eepromAddr = eepromAddr << 1;
482 }
483}
484
485/*
486 * Caller holds hw_lock.
487 */
488static void fm93c56a_deselect(struct ql3_adapter *qdev)
489{
490 struct ql3xxx_port_registers __iomem *port_regs =
491 qdev->mem_map_registers;
492 qdev->eeprom_cmd_data = AUBURN_EEPROM_CS_0;
Ron Mercer80b02e52007-01-03 16:26:07 -0800493 ql_write_nvram_reg(qdev, &port_regs->CommonRegs.serialPortInterfaceReg,
Ron Mercer5a4faa82006-07-25 00:40:21 -0700494 ISP_NVRAM_MASK | qdev->eeprom_cmd_data);
495}
496
497/*
498 * Caller holds hw_lock.
499 */
500static void fm93c56a_datain(struct ql3_adapter *qdev, unsigned short *value)
501{
502 int i;
503 u32 data = 0;
504 u32 dataBit;
505 struct ql3xxx_port_registers __iomem *port_regs =
506 qdev->mem_map_registers;
507
508 /* Read the data bits */
509 /* The first bit is a dummy. Clock right over it. */
510 for (i = 0; i < dataBits; i++) {
Ron Mercer80b02e52007-01-03 16:26:07 -0800511 ql_write_nvram_reg(qdev,
Ron Mercer5a4faa82006-07-25 00:40:21 -0700512 &port_regs->CommonRegs.
513 serialPortInterfaceReg,
514 ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
515 AUBURN_EEPROM_CLK_RISE);
Ron Mercer80b02e52007-01-03 16:26:07 -0800516 ql_write_nvram_reg(qdev,
Ron Mercer5a4faa82006-07-25 00:40:21 -0700517 &port_regs->CommonRegs.
518 serialPortInterfaceReg,
519 ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
520 AUBURN_EEPROM_CLK_FALL);
521 dataBit =
522 (ql_read_common_reg
523 (qdev,
524 &port_regs->CommonRegs.
525 serialPortInterfaceReg) & AUBURN_EEPROM_DI_1) ? 1 : 0;
526 data = (data << 1) | dataBit;
527 }
528 *value = (u16) data;
529}
530
531/*
532 * Caller holds hw_lock.
533 */
534static void eeprom_readword(struct ql3_adapter *qdev,
535 u32 eepromAddr, unsigned short *value)
536{
537 fm93c56a_select(qdev);
538 fm93c56a_cmd(qdev, (int)FM93C56A_READ, eepromAddr);
539 fm93c56a_datain(qdev, value);
540 fm93c56a_deselect(qdev);
541}
542
Al Viro804d8542007-12-22 19:44:29 +0000543static void ql_set_mac_addr(struct net_device *ndev, u16 *addr)
Ron Mercer5a4faa82006-07-25 00:40:21 -0700544{
Al Viro804d8542007-12-22 19:44:29 +0000545 __le16 *p = (__le16 *)ndev->dev_addr;
546 p[0] = cpu_to_le16(addr[0]);
547 p[1] = cpu_to_le16(addr[1]);
548 p[2] = cpu_to_le16(addr[2]);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700549}
550
551static int ql_get_nvram_params(struct ql3_adapter *qdev)
552{
553 u16 *pEEPROMData;
554 u16 checksum = 0;
555 u32 index;
556 unsigned long hw_flags;
557
558 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
559
560 pEEPROMData = (u16 *) & qdev->nvram_data;
561 qdev->eeprom_cmd_data = 0;
562 if(ql_sem_spinlock(qdev, QL_NVRAM_SEM_MASK,
563 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
564 2) << 10)) {
565 printk(KERN_ERR PFX"%s: Failed ql_sem_spinlock().\n",
566 __func__);
567 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
568 return -1;
569 }
570
571 for (index = 0; index < EEPROM_SIZE; index++) {
572 eeprom_readword(qdev, index, pEEPROMData);
573 checksum += *pEEPROMData;
574 pEEPROMData++;
575 }
576 ql_sem_unlock(qdev, QL_NVRAM_SEM_MASK);
577
578 if (checksum != 0) {
579 printk(KERN_ERR PFX "%s: checksum should be zero, is %x!!\n",
580 qdev->ndev->name, checksum);
581 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
582 return -1;
583 }
584
Ron Mercer5a4faa82006-07-25 00:40:21 -0700585 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
586 return checksum;
587}
588
589static const u32 PHYAddr[2] = {
590 PORT0_PHY_ADDRESS, PORT1_PHY_ADDRESS
591};
592
593static int ql_wait_for_mii_ready(struct ql3_adapter *qdev)
594{
595 struct ql3xxx_port_registers __iomem *port_regs =
596 qdev->mem_map_registers;
597 u32 temp;
598 int count = 1000;
599
600 while (count) {
601 temp = ql_read_page0_reg(qdev, &port_regs->macMIIStatusReg);
602 if (!(temp & MAC_MII_STATUS_BSY))
603 return 0;
604 udelay(10);
605 count--;
606 }
607 return -1;
608}
609
610static void ql_mii_enable_scan_mode(struct ql3_adapter *qdev)
611{
612 struct ql3xxx_port_registers __iomem *port_regs =
613 qdev->mem_map_registers;
614 u32 scanControl;
615
616 if (qdev->numPorts > 1) {
617 /* Auto scan will cycle through multiple ports */
618 scanControl = MAC_MII_CONTROL_AS | MAC_MII_CONTROL_SC;
619 } else {
620 scanControl = MAC_MII_CONTROL_SC;
621 }
622
623 /*
624 * Scan register 1 of PHY/PETBI,
625 * Set up to scan both devices
626 * The autoscan starts from the first register, completes
627 * the last one before rolling over to the first
628 */
629 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
630 PHYAddr[0] | MII_SCAN_REGISTER);
631
632 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
633 (scanControl) |
634 ((MAC_MII_CONTROL_SC | MAC_MII_CONTROL_AS) << 16));
635}
636
637static u8 ql_mii_disable_scan_mode(struct ql3_adapter *qdev)
638{
639 u8 ret;
640 struct ql3xxx_port_registers __iomem *port_regs =
641 qdev->mem_map_registers;
642
643 /* See if scan mode is enabled before we turn it off */
644 if (ql_read_page0_reg(qdev, &port_regs->macMIIMgmtControlReg) &
645 (MAC_MII_CONTROL_AS | MAC_MII_CONTROL_SC)) {
646 /* Scan is enabled */
647 ret = 1;
648 } else {
649 /* Scan is disabled */
650 ret = 0;
651 }
652
653 /*
654 * When disabling scan mode you must first change the MII register
655 * address
656 */
657 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
658 PHYAddr[0] | MII_SCAN_REGISTER);
659
660 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
661 ((MAC_MII_CONTROL_SC | MAC_MII_CONTROL_AS |
662 MAC_MII_CONTROL_RC) << 16));
663
664 return ret;
665}
666
667static int ql_mii_write_reg_ex(struct ql3_adapter *qdev,
Ron Mercer3efedf22007-03-26 12:43:52 -0700668 u16 regAddr, u16 value, u32 phyAddr)
Ron Mercer5a4faa82006-07-25 00:40:21 -0700669{
670 struct ql3xxx_port_registers __iomem *port_regs =
671 qdev->mem_map_registers;
672 u8 scanWasEnabled;
673
674 scanWasEnabled = ql_mii_disable_scan_mode(qdev);
675
676 if (ql_wait_for_mii_ready(qdev)) {
677 if (netif_msg_link(qdev))
678 printk(KERN_WARNING PFX
679 "%s Timed out waiting for management port to "
680 "get free before issuing command.\n",
681 qdev->ndev->name);
682 return -1;
683 }
684
685 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
Ron Mercer3efedf22007-03-26 12:43:52 -0700686 phyAddr | regAddr);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700687
688 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtDataReg, value);
689
690 /* Wait for write to complete 9/10/04 SJP */
691 if (ql_wait_for_mii_ready(qdev)) {
692 if (netif_msg_link(qdev))
693 printk(KERN_WARNING PFX
Joe Perches24500222007-11-19 17:48:28 -0800694 "%s: Timed out waiting for management port to "
Ron Mercer5a4faa82006-07-25 00:40:21 -0700695 "get free before issuing command.\n",
696 qdev->ndev->name);
697 return -1;
698 }
699
700 if (scanWasEnabled)
701 ql_mii_enable_scan_mode(qdev);
702
703 return 0;
704}
705
706static int ql_mii_read_reg_ex(struct ql3_adapter *qdev, u16 regAddr,
Ron Mercer3efedf22007-03-26 12:43:52 -0700707 u16 * value, u32 phyAddr)
Ron Mercer5a4faa82006-07-25 00:40:21 -0700708{
709 struct ql3xxx_port_registers __iomem *port_regs =
710 qdev->mem_map_registers;
711 u8 scanWasEnabled;
712 u32 temp;
713
714 scanWasEnabled = ql_mii_disable_scan_mode(qdev);
715
716 if (ql_wait_for_mii_ready(qdev)) {
717 if (netif_msg_link(qdev))
718 printk(KERN_WARNING PFX
719 "%s: Timed out waiting for management port to "
720 "get free before issuing command.\n",
721 qdev->ndev->name);
722 return -1;
723 }
724
725 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
Ron Mercer3efedf22007-03-26 12:43:52 -0700726 phyAddr | regAddr);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700727
728 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
729 (MAC_MII_CONTROL_RC << 16));
730
731 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
732 (MAC_MII_CONTROL_RC << 16) | MAC_MII_CONTROL_RC);
733
734 /* Wait for the read to complete */
735 if (ql_wait_for_mii_ready(qdev)) {
736 if (netif_msg_link(qdev))
737 printk(KERN_WARNING PFX
738 "%s: Timed out waiting for management port to "
739 "get free after issuing command.\n",
740 qdev->ndev->name);
741 return -1;
742 }
743
744 temp = ql_read_page0_reg(qdev, &port_regs->macMIIMgmtDataReg);
745 *value = (u16) temp;
746
747 if (scanWasEnabled)
748 ql_mii_enable_scan_mode(qdev);
749
750 return 0;
751}
752
753static int ql_mii_write_reg(struct ql3_adapter *qdev, u16 regAddr, u16 value)
754{
755 struct ql3xxx_port_registers __iomem *port_regs =
756 qdev->mem_map_registers;
757
758 ql_mii_disable_scan_mode(qdev);
759
760 if (ql_wait_for_mii_ready(qdev)) {
761 if (netif_msg_link(qdev))
762 printk(KERN_WARNING PFX
763 "%s: Timed out waiting for management port to "
764 "get free before issuing command.\n",
765 qdev->ndev->name);
766 return -1;
767 }
768
769 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
770 qdev->PHYAddr | regAddr);
771
772 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtDataReg, value);
773
774 /* Wait for write to complete. */
775 if (ql_wait_for_mii_ready(qdev)) {
776 if (netif_msg_link(qdev))
777 printk(KERN_WARNING PFX
778 "%s: Timed out waiting for management port to "
779 "get free before issuing command.\n",
780 qdev->ndev->name);
781 return -1;
782 }
783
784 ql_mii_enable_scan_mode(qdev);
785
786 return 0;
787}
788
789static int ql_mii_read_reg(struct ql3_adapter *qdev, u16 regAddr, u16 *value)
790{
791 u32 temp;
792 struct ql3xxx_port_registers __iomem *port_regs =
793 qdev->mem_map_registers;
794
795 ql_mii_disable_scan_mode(qdev);
796
797 if (ql_wait_for_mii_ready(qdev)) {
798 if (netif_msg_link(qdev))
799 printk(KERN_WARNING PFX
800 "%s: Timed out waiting for management port to "
801 "get free before issuing command.\n",
802 qdev->ndev->name);
803 return -1;
804 }
805
806 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
807 qdev->PHYAddr | regAddr);
808
809 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
810 (MAC_MII_CONTROL_RC << 16));
811
812 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
813 (MAC_MII_CONTROL_RC << 16) | MAC_MII_CONTROL_RC);
814
815 /* Wait for the read to complete */
816 if (ql_wait_for_mii_ready(qdev)) {
817 if (netif_msg_link(qdev))
818 printk(KERN_WARNING PFX
819 "%s: Timed out waiting for management port to "
820 "get free before issuing command.\n",
821 qdev->ndev->name);
822 return -1;
823 }
824
825 temp = ql_read_page0_reg(qdev, &port_regs->macMIIMgmtDataReg);
826 *value = (u16) temp;
827
828 ql_mii_enable_scan_mode(qdev);
829
830 return 0;
831}
832
833static void ql_petbi_reset(struct ql3_adapter *qdev)
834{
835 ql_mii_write_reg(qdev, PETBI_CONTROL_REG, PETBI_CTRL_SOFT_RESET);
836}
837
838static void ql_petbi_start_neg(struct ql3_adapter *qdev)
839{
840 u16 reg;
841
842 /* Enable Auto-negotiation sense */
843 ql_mii_read_reg(qdev, PETBI_TBI_CTRL, &reg);
844 reg |= PETBI_TBI_AUTO_SENSE;
845 ql_mii_write_reg(qdev, PETBI_TBI_CTRL, reg);
846
847 ql_mii_write_reg(qdev, PETBI_NEG_ADVER,
848 PETBI_NEG_PAUSE | PETBI_NEG_DUPLEX);
849
850 ql_mii_write_reg(qdev, PETBI_CONTROL_REG,
851 PETBI_CTRL_AUTO_NEG | PETBI_CTRL_RESTART_NEG |
852 PETBI_CTRL_FULL_DUPLEX | PETBI_CTRL_SPEED_1000);
853
854}
855
Ron Mercer3efedf22007-03-26 12:43:52 -0700856static void ql_petbi_reset_ex(struct ql3_adapter *qdev)
Ron Mercer5a4faa82006-07-25 00:40:21 -0700857{
858 ql_mii_write_reg_ex(qdev, PETBI_CONTROL_REG, PETBI_CTRL_SOFT_RESET,
Ron Mercer3efedf22007-03-26 12:43:52 -0700859 PHYAddr[qdev->mac_index]);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700860}
861
Ron Mercer3efedf22007-03-26 12:43:52 -0700862static void ql_petbi_start_neg_ex(struct ql3_adapter *qdev)
Ron Mercer5a4faa82006-07-25 00:40:21 -0700863{
864 u16 reg;
865
866 /* Enable Auto-negotiation sense */
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400867 ql_mii_read_reg_ex(qdev, PETBI_TBI_CTRL, &reg,
Ron Mercer3efedf22007-03-26 12:43:52 -0700868 PHYAddr[qdev->mac_index]);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700869 reg |= PETBI_TBI_AUTO_SENSE;
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400870 ql_mii_write_reg_ex(qdev, PETBI_TBI_CTRL, reg,
Ron Mercer3efedf22007-03-26 12:43:52 -0700871 PHYAddr[qdev->mac_index]);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700872
873 ql_mii_write_reg_ex(qdev, PETBI_NEG_ADVER,
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400874 PETBI_NEG_PAUSE | PETBI_NEG_DUPLEX,
Ron Mercer3efedf22007-03-26 12:43:52 -0700875 PHYAddr[qdev->mac_index]);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700876
877 ql_mii_write_reg_ex(qdev, PETBI_CONTROL_REG,
878 PETBI_CTRL_AUTO_NEG | PETBI_CTRL_RESTART_NEG |
879 PETBI_CTRL_FULL_DUPLEX | PETBI_CTRL_SPEED_1000,
Ron Mercer3efedf22007-03-26 12:43:52 -0700880 PHYAddr[qdev->mac_index]);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700881}
882
883static void ql_petbi_init(struct ql3_adapter *qdev)
884{
885 ql_petbi_reset(qdev);
886 ql_petbi_start_neg(qdev);
887}
888
Ron Mercer3efedf22007-03-26 12:43:52 -0700889static void ql_petbi_init_ex(struct ql3_adapter *qdev)
Ron Mercer5a4faa82006-07-25 00:40:21 -0700890{
Ron Mercer3efedf22007-03-26 12:43:52 -0700891 ql_petbi_reset_ex(qdev);
892 ql_petbi_start_neg_ex(qdev);
Ron Mercer5a4faa82006-07-25 00:40:21 -0700893}
894
895static int ql_is_petbi_neg_pause(struct ql3_adapter *qdev)
896{
897 u16 reg;
898
899 if (ql_mii_read_reg(qdev, PETBI_NEG_PARTNER, &reg) < 0)
900 return 0;
901
902 return (reg & PETBI_NEG_PAUSE_MASK) == PETBI_NEG_PAUSE;
903}
904
Ron Mercer3efedf22007-03-26 12:43:52 -0700905static void phyAgereSpecificInit(struct ql3_adapter *qdev, u32 miiAddr)
906{
907 printk(KERN_INFO "%s: enabling Agere specific PHY\n", qdev->ndev->name);
908 /* power down device bit 11 = 1 */
909 ql_mii_write_reg_ex(qdev, 0x00, 0x1940, miiAddr);
910 /* enable diagnostic mode bit 2 = 1 */
911 ql_mii_write_reg_ex(qdev, 0x12, 0x840e, miiAddr);
912 /* 1000MB amplitude adjust (see Agere errata) */
913 ql_mii_write_reg_ex(qdev, 0x10, 0x8805, miiAddr);
914 /* 1000MB amplitude adjust (see Agere errata) */
915 ql_mii_write_reg_ex(qdev, 0x11, 0xf03e, miiAddr);
916 /* 100MB amplitude adjust (see Agere errata) */
917 ql_mii_write_reg_ex(qdev, 0x10, 0x8806, miiAddr);
918 /* 100MB amplitude adjust (see Agere errata) */
919 ql_mii_write_reg_ex(qdev, 0x11, 0x003e, miiAddr);
920 /* 10MB amplitude adjust (see Agere errata) */
921 ql_mii_write_reg_ex(qdev, 0x10, 0x8807, miiAddr);
922 /* 10MB amplitude adjust (see Agere errata) */
923 ql_mii_write_reg_ex(qdev, 0x11, 0x1f00, miiAddr);
924 /* point to hidden reg 0x2806 */
925 ql_mii_write_reg_ex(qdev, 0x10, 0x2806, miiAddr);
926 /* Write new PHYAD w/bit 5 set */
927 ql_mii_write_reg_ex(qdev, 0x11, 0x0020 | (PHYAddr[qdev->mac_index] >> 8), miiAddr);
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400928 /*
Ron Mercer3efedf22007-03-26 12:43:52 -0700929 * Disable diagnostic mode bit 2 = 0
930 * Power up device bit 11 = 0
931 * Link up (on) and activity (blink)
932 */
933 ql_mii_write_reg(qdev, 0x12, 0x840a);
934 ql_mii_write_reg(qdev, 0x00, 0x1140);
935 ql_mii_write_reg(qdev, 0x1c, 0xfaf0);
936}
937
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400938static PHY_DEVICE_et getPhyType (struct ql3_adapter *qdev,
Ron Mercer3efedf22007-03-26 12:43:52 -0700939 u16 phyIdReg0, u16 phyIdReg1)
940{
941 PHY_DEVICE_et result = PHY_TYPE_UNKNOWN;
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400942 u32 oui;
Ron Mercer3efedf22007-03-26 12:43:52 -0700943 u16 model;
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400944 int i;
Ron Mercer3efedf22007-03-26 12:43:52 -0700945
946 if (phyIdReg0 == 0xffff) {
947 return result;
948 }
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400949
Ron Mercer3efedf22007-03-26 12:43:52 -0700950 if (phyIdReg1 == 0xffff) {
951 return result;
952 }
953
954 /* oui is split between two registers */
955 oui = (phyIdReg0 << 6) | ((phyIdReg1 & PHY_OUI_1_MASK) >> 10);
956
957 model = (phyIdReg1 & PHY_MODEL_MASK) >> 4;
958
959 /* Scan table for this PHY */
960 for(i = 0; i < MAX_PHY_DEV_TYPES; i++) {
961 if ((oui == PHY_DEVICES[i].phyIdOUI) && (model == PHY_DEVICES[i].phyIdModel))
962 {
963 result = PHY_DEVICES[i].phyDevice;
964
965 printk(KERN_INFO "%s: Phy: %s\n",
966 qdev->ndev->name, PHY_DEVICES[i].name);
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400967
Ron Mercer3efedf22007-03-26 12:43:52 -0700968 break;
969 }
970 }
971
972 return result;
973}
974
Ron Mercer5a4faa82006-07-25 00:40:21 -0700975static int ql_phy_get_speed(struct ql3_adapter *qdev)
976{
977 u16 reg;
978
Ron Mercer3efedf22007-03-26 12:43:52 -0700979 switch(qdev->phyType) {
980 case PHY_AGERE_ET1011C:
981 {
982 if (ql_mii_read_reg(qdev, 0x1A, &reg) < 0)
983 return 0;
984
985 reg = (reg >> 8) & 3;
986 break;
987 }
988 default:
Ron Mercer5a4faa82006-07-25 00:40:21 -0700989 if (ql_mii_read_reg(qdev, AUX_CONTROL_STATUS, &reg) < 0)
990 return 0;
991
992 reg = (((reg & 0x18) >> 3) & 3);
Ron Mercer3efedf22007-03-26 12:43:52 -0700993 }
Ron Mercer5a4faa82006-07-25 00:40:21 -0700994
Ron Mercer3efedf22007-03-26 12:43:52 -0700995 switch(reg) {
996 case 2:
Ron Mercer5a4faa82006-07-25 00:40:21 -0700997 return SPEED_1000;
Ron Mercer3efedf22007-03-26 12:43:52 -0700998 case 1:
Ron Mercer5a4faa82006-07-25 00:40:21 -0700999 return SPEED_100;
Ron Mercer3efedf22007-03-26 12:43:52 -07001000 case 0:
Ron Mercer5a4faa82006-07-25 00:40:21 -07001001 return SPEED_10;
Ron Mercer3efedf22007-03-26 12:43:52 -07001002 default:
Ron Mercer5a4faa82006-07-25 00:40:21 -07001003 return -1;
Ron Mercer3efedf22007-03-26 12:43:52 -07001004 }
Ron Mercer5a4faa82006-07-25 00:40:21 -07001005}
1006
1007static int ql_is_full_dup(struct ql3_adapter *qdev)
1008{
1009 u16 reg;
1010
Ron Mercer3efedf22007-03-26 12:43:52 -07001011 switch(qdev->phyType) {
1012 case PHY_AGERE_ET1011C:
1013 {
1014 if (ql_mii_read_reg(qdev, 0x1A, &reg))
1015 return 0;
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001016
Ron Mercer3efedf22007-03-26 12:43:52 -07001017 return ((reg & 0x0080) && (reg & 0x1000)) != 0;
1018 }
1019 case PHY_VITESSE_VSC8211:
1020 default:
1021 {
1022 if (ql_mii_read_reg(qdev, AUX_CONTROL_STATUS, &reg) < 0)
1023 return 0;
1024 return (reg & PHY_AUX_DUPLEX_STAT) != 0;
1025 }
1026 }
Ron Mercer5a4faa82006-07-25 00:40:21 -07001027}
1028
1029static int ql_is_phy_neg_pause(struct ql3_adapter *qdev)
1030{
1031 u16 reg;
1032
1033 if (ql_mii_read_reg(qdev, PHY_NEG_PARTNER, &reg) < 0)
1034 return 0;
1035
1036 return (reg & PHY_NEG_PAUSE) != 0;
1037}
1038
Ron Mercer3efedf22007-03-26 12:43:52 -07001039static int PHY_Setup(struct ql3_adapter *qdev)
1040{
1041 u16 reg1;
1042 u16 reg2;
1043 bool agereAddrChangeNeeded = false;
1044 u32 miiAddr = 0;
1045 int err;
1046
1047 /* Determine the PHY we are using by reading the ID's */
1048 err = ql_mii_read_reg(qdev, PHY_ID_0_REG, &reg1);
1049 if(err != 0) {
1050 printk(KERN_ERR "%s: Could not read from reg PHY_ID_0_REG\n",
1051 qdev->ndev->name);
1052 return err;
1053 }
1054
1055 err = ql_mii_read_reg(qdev, PHY_ID_1_REG, &reg2);
1056 if(err != 0) {
1057 printk(KERN_ERR "%s: Could not read from reg PHY_ID_0_REG\n",
1058 qdev->ndev->name);
1059 return err;
1060 }
1061
1062 /* Check if we have a Agere PHY */
1063 if ((reg1 == 0xffff) || (reg2 == 0xffff)) {
1064
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001065 /* Determine which MII address we should be using
Ron Mercer3efedf22007-03-26 12:43:52 -07001066 determined by the index of the card */
1067 if (qdev->mac_index == 0) {
1068 miiAddr = MII_AGERE_ADDR_1;
1069 } else {
1070 miiAddr = MII_AGERE_ADDR_2;
1071 }
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001072
Ron Mercer3efedf22007-03-26 12:43:52 -07001073 err =ql_mii_read_reg_ex(qdev, PHY_ID_0_REG, &reg1, miiAddr);
1074 if(err != 0) {
1075 printk(KERN_ERR "%s: Could not read from reg PHY_ID_0_REG after Agere detected\n",
1076 qdev->ndev->name);
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001077 return err;
Ron Mercer3efedf22007-03-26 12:43:52 -07001078 }
1079
1080 err = ql_mii_read_reg_ex(qdev, PHY_ID_1_REG, &reg2, miiAddr);
1081 if(err != 0) {
1082 printk(KERN_ERR "%s: Could not read from reg PHY_ID_0_REG after Agere detected\n",
1083 qdev->ndev->name);
1084 return err;
1085 }
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001086
Ron Mercer3efedf22007-03-26 12:43:52 -07001087 /* We need to remember to initialize the Agere PHY */
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001088 agereAddrChangeNeeded = true;
Ron Mercer3efedf22007-03-26 12:43:52 -07001089 }
1090
1091 /* Determine the particular PHY we have on board to apply
1092 PHY specific initializations */
1093 qdev->phyType = getPhyType(qdev, reg1, reg2);
1094
1095 if ((qdev->phyType == PHY_AGERE_ET1011C) && agereAddrChangeNeeded) {
1096 /* need this here so address gets changed */
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001097 phyAgereSpecificInit(qdev, miiAddr);
Ron Mercer3efedf22007-03-26 12:43:52 -07001098 } else if (qdev->phyType == PHY_TYPE_UNKNOWN) {
1099 printk(KERN_ERR "%s: PHY is unknown\n", qdev->ndev->name);
1100 return -EIO;
1101 }
1102
1103 return 0;
1104}
1105
Ron Mercer5a4faa82006-07-25 00:40:21 -07001106/*
1107 * Caller holds hw_lock.
1108 */
1109static void ql_mac_enable(struct ql3_adapter *qdev, u32 enable)
1110{
1111 struct ql3xxx_port_registers __iomem *port_regs =
1112 qdev->mem_map_registers;
1113 u32 value;
1114
1115 if (enable)
1116 value = (MAC_CONFIG_REG_PE | (MAC_CONFIG_REG_PE << 16));
1117 else
1118 value = (MAC_CONFIG_REG_PE << 16);
1119
1120 if (qdev->mac_index)
1121 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1122 else
1123 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1124}
1125
1126/*
1127 * Caller holds hw_lock.
1128 */
1129static void ql_mac_cfg_soft_reset(struct ql3_adapter *qdev, u32 enable)
1130{
1131 struct ql3xxx_port_registers __iomem *port_regs =
1132 qdev->mem_map_registers;
1133 u32 value;
1134
1135 if (enable)
1136 value = (MAC_CONFIG_REG_SR | (MAC_CONFIG_REG_SR << 16));
1137 else
1138 value = (MAC_CONFIG_REG_SR << 16);
1139
1140 if (qdev->mac_index)
1141 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1142 else
1143 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1144}
1145
1146/*
1147 * Caller holds hw_lock.
1148 */
1149static void ql_mac_cfg_gig(struct ql3_adapter *qdev, u32 enable)
1150{
1151 struct ql3xxx_port_registers __iomem *port_regs =
1152 qdev->mem_map_registers;
1153 u32 value;
1154
1155 if (enable)
1156 value = (MAC_CONFIG_REG_GM | (MAC_CONFIG_REG_GM << 16));
1157 else
1158 value = (MAC_CONFIG_REG_GM << 16);
1159
1160 if (qdev->mac_index)
1161 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1162 else
1163 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1164}
1165
1166/*
1167 * Caller holds hw_lock.
1168 */
1169static void ql_mac_cfg_full_dup(struct ql3_adapter *qdev, u32 enable)
1170{
1171 struct ql3xxx_port_registers __iomem *port_regs =
1172 qdev->mem_map_registers;
1173 u32 value;
1174
1175 if (enable)
1176 value = (MAC_CONFIG_REG_FD | (MAC_CONFIG_REG_FD << 16));
1177 else
1178 value = (MAC_CONFIG_REG_FD << 16);
1179
1180 if (qdev->mac_index)
1181 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1182 else
1183 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1184}
1185
1186/*
1187 * Caller holds hw_lock.
1188 */
1189static void ql_mac_cfg_pause(struct ql3_adapter *qdev, u32 enable)
1190{
1191 struct ql3xxx_port_registers __iomem *port_regs =
1192 qdev->mem_map_registers;
1193 u32 value;
1194
1195 if (enable)
1196 value =
1197 ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) |
1198 ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) << 16));
1199 else
1200 value = ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) << 16);
1201
1202 if (qdev->mac_index)
1203 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1204 else
1205 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1206}
1207
1208/*
1209 * Caller holds hw_lock.
1210 */
1211static int ql_is_fiber(struct ql3_adapter *qdev)
1212{
1213 struct ql3xxx_port_registers __iomem *port_regs =
1214 qdev->mem_map_registers;
1215 u32 bitToCheck = 0;
1216 u32 temp;
1217
1218 switch (qdev->mac_index) {
1219 case 0:
1220 bitToCheck = PORT_STATUS_SM0;
1221 break;
1222 case 1:
1223 bitToCheck = PORT_STATUS_SM1;
1224 break;
1225 }
1226
1227 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1228 return (temp & bitToCheck) != 0;
1229}
1230
1231static int ql_is_auto_cfg(struct ql3_adapter *qdev)
1232{
1233 u16 reg;
1234 ql_mii_read_reg(qdev, 0x00, &reg);
1235 return (reg & 0x1000) != 0;
1236}
1237
1238/*
1239 * Caller holds hw_lock.
1240 */
1241static int ql_is_auto_neg_complete(struct ql3_adapter *qdev)
1242{
1243 struct ql3xxx_port_registers __iomem *port_regs =
1244 qdev->mem_map_registers;
1245 u32 bitToCheck = 0;
1246 u32 temp;
1247
1248 switch (qdev->mac_index) {
1249 case 0:
1250 bitToCheck = PORT_STATUS_AC0;
1251 break;
1252 case 1:
1253 bitToCheck = PORT_STATUS_AC1;
1254 break;
1255 }
1256
1257 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1258 if (temp & bitToCheck) {
1259 if (netif_msg_link(qdev))
1260 printk(KERN_INFO PFX
1261 "%s: Auto-Negotiate complete.\n",
1262 qdev->ndev->name);
1263 return 1;
1264 } else {
1265 if (netif_msg_link(qdev))
1266 printk(KERN_WARNING PFX
1267 "%s: Auto-Negotiate incomplete.\n",
1268 qdev->ndev->name);
1269 return 0;
1270 }
1271}
1272
1273/*
1274 * ql_is_neg_pause() returns 1 if pause was negotiated to be on
1275 */
1276static int ql_is_neg_pause(struct ql3_adapter *qdev)
1277{
1278 if (ql_is_fiber(qdev))
1279 return ql_is_petbi_neg_pause(qdev);
1280 else
1281 return ql_is_phy_neg_pause(qdev);
1282}
1283
1284static int ql_auto_neg_error(struct ql3_adapter *qdev)
1285{
1286 struct ql3xxx_port_registers __iomem *port_regs =
1287 qdev->mem_map_registers;
1288 u32 bitToCheck = 0;
1289 u32 temp;
1290
1291 switch (qdev->mac_index) {
1292 case 0:
1293 bitToCheck = PORT_STATUS_AE0;
1294 break;
1295 case 1:
1296 bitToCheck = PORT_STATUS_AE1;
1297 break;
1298 }
1299 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1300 return (temp & bitToCheck) != 0;
1301}
1302
1303static u32 ql_get_link_speed(struct ql3_adapter *qdev)
1304{
1305 if (ql_is_fiber(qdev))
1306 return SPEED_1000;
1307 else
1308 return ql_phy_get_speed(qdev);
1309}
1310
1311static int ql_is_link_full_dup(struct ql3_adapter *qdev)
1312{
1313 if (ql_is_fiber(qdev))
1314 return 1;
1315 else
1316 return ql_is_full_dup(qdev);
1317}
1318
1319/*
1320 * Caller holds hw_lock.
1321 */
1322static int ql_link_down_detect(struct ql3_adapter *qdev)
1323{
1324 struct ql3xxx_port_registers __iomem *port_regs =
1325 qdev->mem_map_registers;
1326 u32 bitToCheck = 0;
1327 u32 temp;
1328
1329 switch (qdev->mac_index) {
1330 case 0:
1331 bitToCheck = ISP_CONTROL_LINK_DN_0;
1332 break;
1333 case 1:
1334 bitToCheck = ISP_CONTROL_LINK_DN_1;
1335 break;
1336 }
1337
1338 temp =
1339 ql_read_common_reg(qdev, &port_regs->CommonRegs.ispControlStatus);
1340 return (temp & bitToCheck) != 0;
1341}
1342
1343/*
1344 * Caller holds hw_lock.
1345 */
1346static int ql_link_down_detect_clear(struct ql3_adapter *qdev)
1347{
1348 struct ql3xxx_port_registers __iomem *port_regs =
1349 qdev->mem_map_registers;
1350
1351 switch (qdev->mac_index) {
1352 case 0:
1353 ql_write_common_reg(qdev,
1354 &port_regs->CommonRegs.ispControlStatus,
1355 (ISP_CONTROL_LINK_DN_0) |
1356 (ISP_CONTROL_LINK_DN_0 << 16));
1357 break;
1358
1359 case 1:
1360 ql_write_common_reg(qdev,
1361 &port_regs->CommonRegs.ispControlStatus,
1362 (ISP_CONTROL_LINK_DN_1) |
1363 (ISP_CONTROL_LINK_DN_1 << 16));
1364 break;
1365
1366 default:
1367 return 1;
1368 }
1369
1370 return 0;
1371}
1372
1373/*
1374 * Caller holds hw_lock.
1375 */
Ron Mercer3efedf22007-03-26 12:43:52 -07001376static int ql_this_adapter_controls_port(struct ql3_adapter *qdev)
Ron Mercer5a4faa82006-07-25 00:40:21 -07001377{
1378 struct ql3xxx_port_registers __iomem *port_regs =
1379 qdev->mem_map_registers;
1380 u32 bitToCheck = 0;
1381 u32 temp;
1382
Ron Mercer3efedf22007-03-26 12:43:52 -07001383 switch (qdev->mac_index) {
Ron Mercer5a4faa82006-07-25 00:40:21 -07001384 case 0:
1385 bitToCheck = PORT_STATUS_F1_ENABLED;
1386 break;
1387 case 1:
1388 bitToCheck = PORT_STATUS_F3_ENABLED;
1389 break;
1390 default:
1391 break;
1392 }
1393
1394 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1395 if (temp & bitToCheck) {
1396 if (netif_msg_link(qdev))
1397 printk(KERN_DEBUG PFX
1398 "%s: is not link master.\n", qdev->ndev->name);
1399 return 0;
1400 } else {
1401 if (netif_msg_link(qdev))
1402 printk(KERN_DEBUG PFX
1403 "%s: is link master.\n", qdev->ndev->name);
1404 return 1;
1405 }
1406}
1407
Ron Mercer3efedf22007-03-26 12:43:52 -07001408static void ql_phy_reset_ex(struct ql3_adapter *qdev)
Ron Mercer5a4faa82006-07-25 00:40:21 -07001409{
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001410 ql_mii_write_reg_ex(qdev, CONTROL_REG, PHY_CTRL_SOFT_RESET,
Ron Mercer3efedf22007-03-26 12:43:52 -07001411 PHYAddr[qdev->mac_index]);
Ron Mercer5a4faa82006-07-25 00:40:21 -07001412}
1413
Ron Mercer3efedf22007-03-26 12:43:52 -07001414static void ql_phy_start_neg_ex(struct ql3_adapter *qdev)
Ron Mercer5a4faa82006-07-25 00:40:21 -07001415{
1416 u16 reg;
Ron Mercer3efedf22007-03-26 12:43:52 -07001417 u16 portConfiguration;
Ron Mercer5a4faa82006-07-25 00:40:21 -07001418
Ron Mercer3efedf22007-03-26 12:43:52 -07001419 if(qdev->phyType == PHY_AGERE_ET1011C) {
1420 /* turn off external loopback */
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001421 ql_mii_write_reg(qdev, 0x13, 0x0000);
Ron Mercer3efedf22007-03-26 12:43:52 -07001422 }
Ron Mercer5a4faa82006-07-25 00:40:21 -07001423
Ron Mercer3efedf22007-03-26 12:43:52 -07001424 if(qdev->mac_index == 0)
1425 portConfiguration = qdev->nvram_data.macCfg_port0.portConfiguration;
1426 else
1427 portConfiguration = qdev->nvram_data.macCfg_port1.portConfiguration;
1428
1429 /* Some HBA's in the field are set to 0 and they need to
1430 be reinterpreted with a default value */
1431 if(portConfiguration == 0)
1432 portConfiguration = PORT_CONFIG_DEFAULT;
1433
1434 /* Set the 1000 advertisements */
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001435 ql_mii_read_reg_ex(qdev, PHY_GIG_CONTROL, &reg,
Ron Mercer3efedf22007-03-26 12:43:52 -07001436 PHYAddr[qdev->mac_index]);
1437 reg &= ~PHY_GIG_ALL_PARAMS;
1438
Ron Mercerad4c9a092007-11-07 13:59:07 -08001439 if(portConfiguration & PORT_CONFIG_1000MB_SPEED) {
1440 if(portConfiguration & PORT_CONFIG_FULL_DUPLEX_ENABLED)
1441 reg |= PHY_GIG_ADV_1000F;
1442 else
1443 reg |= PHY_GIG_ADV_1000H;
Ron Mercer3efedf22007-03-26 12:43:52 -07001444 }
1445
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001446 ql_mii_write_reg_ex(qdev, PHY_GIG_CONTROL, reg,
Ron Mercer3efedf22007-03-26 12:43:52 -07001447 PHYAddr[qdev->mac_index]);
1448
1449 /* Set the 10/100 & pause negotiation advertisements */
1450 ql_mii_read_reg_ex(qdev, PHY_NEG_ADVER, &reg,
1451 PHYAddr[qdev->mac_index]);
1452 reg &= ~PHY_NEG_ALL_PARAMS;
1453
1454 if(portConfiguration & PORT_CONFIG_SYM_PAUSE_ENABLED)
1455 reg |= PHY_NEG_ASY_PAUSE | PHY_NEG_SYM_PAUSE;
1456
1457 if(portConfiguration & PORT_CONFIG_FULL_DUPLEX_ENABLED) {
1458 if(portConfiguration & PORT_CONFIG_100MB_SPEED)
1459 reg |= PHY_NEG_ADV_100F;
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001460
Ron Mercer3efedf22007-03-26 12:43:52 -07001461 if(portConfiguration & PORT_CONFIG_10MB_SPEED)
1462 reg |= PHY_NEG_ADV_10F;
1463 }
1464
1465 if(portConfiguration & PORT_CONFIG_HALF_DUPLEX_ENABLED) {
1466 if(portConfiguration & PORT_CONFIG_100MB_SPEED)
1467 reg |= PHY_NEG_ADV_100H;
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001468
Ron Mercer3efedf22007-03-26 12:43:52 -07001469 if(portConfiguration & PORT_CONFIG_10MB_SPEED)
1470 reg |= PHY_NEG_ADV_10H;
1471 }
1472
1473 if(portConfiguration &
1474 PORT_CONFIG_1000MB_SPEED) {
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001475 reg |= 1;
Ron Mercer3efedf22007-03-26 12:43:52 -07001476 }
1477
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001478 ql_mii_write_reg_ex(qdev, PHY_NEG_ADVER, reg,
Ron Mercer3efedf22007-03-26 12:43:52 -07001479 PHYAddr[qdev->mac_index]);
1480
1481 ql_mii_read_reg_ex(qdev, CONTROL_REG, &reg, PHYAddr[qdev->mac_index]);
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001482
1483 ql_mii_write_reg_ex(qdev, CONTROL_REG,
Ron Mercer3efedf22007-03-26 12:43:52 -07001484 reg | PHY_CTRL_RESTART_NEG | PHY_CTRL_AUTO_NEG,
1485 PHYAddr[qdev->mac_index]);
Ron Mercer5a4faa82006-07-25 00:40:21 -07001486}
1487
Ron Mercer3efedf22007-03-26 12:43:52 -07001488static void ql_phy_init_ex(struct ql3_adapter *qdev)
Ron Mercer5a4faa82006-07-25 00:40:21 -07001489{
Ron Mercer3efedf22007-03-26 12:43:52 -07001490 ql_phy_reset_ex(qdev);
1491 PHY_Setup(qdev);
1492 ql_phy_start_neg_ex(qdev);
Ron Mercer5a4faa82006-07-25 00:40:21 -07001493}
1494
1495/*
1496 * Caller holds hw_lock.
1497 */
1498static u32 ql_get_link_state(struct ql3_adapter *qdev)
1499{
1500 struct ql3xxx_port_registers __iomem *port_regs =
1501 qdev->mem_map_registers;
1502 u32 bitToCheck = 0;
1503 u32 temp, linkState;
1504
1505 switch (qdev->mac_index) {
1506 case 0:
1507 bitToCheck = PORT_STATUS_UP0;
1508 break;
1509 case 1:
1510 bitToCheck = PORT_STATUS_UP1;
1511 break;
1512 }
1513 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1514 if (temp & bitToCheck) {
1515 linkState = LS_UP;
1516 } else {
1517 linkState = LS_DOWN;
1518 if (netif_msg_link(qdev))
1519 printk(KERN_WARNING PFX
1520 "%s: Link is down.\n", qdev->ndev->name);
1521 }
1522 return linkState;
1523}
1524
1525static int ql_port_start(struct ql3_adapter *qdev)
1526{
1527 if(ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
1528 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
Ron Mercer3efedf22007-03-26 12:43:52 -07001529 2) << 7)) {
1530 printk(KERN_ERR "%s: Could not get hw lock for GIO\n",
1531 qdev->ndev->name);
Ron Mercer5a4faa82006-07-25 00:40:21 -07001532 return -1;
Ron Mercer3efedf22007-03-26 12:43:52 -07001533 }
Ron Mercer5a4faa82006-07-25 00:40:21 -07001534
1535 if (ql_is_fiber(qdev)) {
1536 ql_petbi_init(qdev);
1537 } else {
1538 /* Copper port */
Ron Mercer3efedf22007-03-26 12:43:52 -07001539 ql_phy_init_ex(qdev);
Ron Mercer5a4faa82006-07-25 00:40:21 -07001540 }
1541
1542 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1543 return 0;
1544}
1545
1546static int ql_finish_auto_neg(struct ql3_adapter *qdev)
1547{
1548
1549 if(ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
1550 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
1551 2) << 7))
1552 return -1;
1553
1554 if (!ql_auto_neg_error(qdev)) {
1555 if (test_bit(QL_LINK_MASTER,&qdev->flags)) {
1556 /* configure the MAC */
1557 if (netif_msg_link(qdev))
1558 printk(KERN_DEBUG PFX
1559 "%s: Configuring link.\n",
1560 qdev->ndev->
1561 name);
1562 ql_mac_cfg_soft_reset(qdev, 1);
1563 ql_mac_cfg_gig(qdev,
1564 (ql_get_link_speed
1565 (qdev) ==
1566 SPEED_1000));
1567 ql_mac_cfg_full_dup(qdev,
1568 ql_is_link_full_dup
1569 (qdev));
1570 ql_mac_cfg_pause(qdev,
1571 ql_is_neg_pause
1572 (qdev));
1573 ql_mac_cfg_soft_reset(qdev, 0);
1574
1575 /* enable the MAC */
1576 if (netif_msg_link(qdev))
1577 printk(KERN_DEBUG PFX
1578 "%s: Enabling mac.\n",
1579 qdev->ndev->
1580 name);
1581 ql_mac_enable(qdev, 1);
1582 }
1583
1584 if (netif_msg_link(qdev))
1585 printk(KERN_DEBUG PFX
1586 "%s: Change port_link_state LS_DOWN to LS_UP.\n",
1587 qdev->ndev->name);
1588 qdev->port_link_state = LS_UP;
1589 netif_start_queue(qdev->ndev);
1590 netif_carrier_on(qdev->ndev);
1591 if (netif_msg_link(qdev))
1592 printk(KERN_INFO PFX
1593 "%s: Link is up at %d Mbps, %s duplex.\n",
1594 qdev->ndev->name,
1595 ql_get_link_speed(qdev),
1596 ql_is_link_full_dup(qdev)
1597 ? "full" : "half");
1598
1599 } else { /* Remote error detected */
1600
1601 if (test_bit(QL_LINK_MASTER,&qdev->flags)) {
1602 if (netif_msg_link(qdev))
1603 printk(KERN_DEBUG PFX
1604 "%s: Remote error detected. "
1605 "Calling ql_port_start().\n",
1606 qdev->ndev->
1607 name);
1608 /*
1609 * ql_port_start() is shared code and needs
1610 * to lock the PHY on it's own.
1611 */
1612 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1613 if(ql_port_start(qdev)) {/* Restart port */
1614 return -1;
1615 } else
1616 return 0;
1617 }
1618 }
1619 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1620 return 0;
1621}
1622
Ron Mercer3e23b7d2007-11-07 13:59:06 -08001623static void ql_link_state_machine_work(struct work_struct *work)
Ron Mercer5a4faa82006-07-25 00:40:21 -07001624{
Ron Mercer3e23b7d2007-11-07 13:59:06 -08001625 struct ql3_adapter *qdev =
1626 container_of(work, struct ql3_adapter, link_state_work.work);
1627
Ron Mercer5a4faa82006-07-25 00:40:21 -07001628 u32 curr_link_state;
1629 unsigned long hw_flags;
1630
1631 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
1632
1633 curr_link_state = ql_get_link_state(qdev);
1634
1635 if (test_bit(QL_RESET_ACTIVE,&qdev->flags)) {
1636 if (netif_msg_link(qdev))
1637 printk(KERN_INFO PFX
1638 "%s: Reset in progress, skip processing link "
1639 "state.\n", qdev->ndev->name);
Benjamin Li04f10772007-02-26 11:06:35 -08001640
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001641 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercer3e23b7d2007-11-07 13:59:06 -08001642
1643 /* Restart timer on 2 second interval. */
1644 mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);\
1645
Ron Mercer5a4faa82006-07-25 00:40:21 -07001646 return;
1647 }
1648
1649 switch (qdev->port_link_state) {
1650 default:
1651 if (test_bit(QL_LINK_MASTER,&qdev->flags)) {
1652 ql_port_start(qdev);
1653 }
1654 qdev->port_link_state = LS_DOWN;
1655 /* Fall Through */
1656
1657 case LS_DOWN:
1658 if (netif_msg_link(qdev))
1659 printk(KERN_DEBUG PFX
1660 "%s: port_link_state = LS_DOWN.\n",
1661 qdev->ndev->name);
1662 if (curr_link_state == LS_UP) {
1663 if (netif_msg_link(qdev))
1664 printk(KERN_DEBUG PFX
1665 "%s: curr_link_state = LS_UP.\n",
1666 qdev->ndev->name);
1667 if (ql_is_auto_neg_complete(qdev))
1668 ql_finish_auto_neg(qdev);
1669
1670 if (qdev->port_link_state == LS_UP)
1671 ql_link_down_detect_clear(qdev);
1672
1673 }
1674 break;
1675
1676 case LS_UP:
1677 /*
1678 * See if the link is currently down or went down and came
1679 * back up
1680 */
1681 if ((curr_link_state == LS_DOWN) || ql_link_down_detect(qdev)) {
1682 if (netif_msg_link(qdev))
1683 printk(KERN_INFO PFX "%s: Link is down.\n",
1684 qdev->ndev->name);
1685 qdev->port_link_state = LS_DOWN;
1686 }
1687 break;
1688 }
1689 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercer3e23b7d2007-11-07 13:59:06 -08001690
1691 /* Restart timer on 2 second interval. */
1692 mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);
Ron Mercer5a4faa82006-07-25 00:40:21 -07001693}
1694
1695/*
1696 * Caller must take hw_lock and QL_PHY_GIO_SEM.
1697 */
1698static void ql_get_phy_owner(struct ql3_adapter *qdev)
1699{
Ron Mercer3efedf22007-03-26 12:43:52 -07001700 if (ql_this_adapter_controls_port(qdev))
Ron Mercer5a4faa82006-07-25 00:40:21 -07001701 set_bit(QL_LINK_MASTER,&qdev->flags);
1702 else
1703 clear_bit(QL_LINK_MASTER,&qdev->flags);
1704}
1705
1706/*
1707 * Caller must take hw_lock and QL_PHY_GIO_SEM.
1708 */
1709static void ql_init_scan_mode(struct ql3_adapter *qdev)
1710{
1711 ql_mii_enable_scan_mode(qdev);
1712
1713 if (test_bit(QL_LINK_OPTICAL,&qdev->flags)) {
Ron Mercer3efedf22007-03-26 12:43:52 -07001714 if (ql_this_adapter_controls_port(qdev))
1715 ql_petbi_init_ex(qdev);
Ron Mercer5a4faa82006-07-25 00:40:21 -07001716 } else {
Ron Mercer3efedf22007-03-26 12:43:52 -07001717 if (ql_this_adapter_controls_port(qdev))
1718 ql_phy_init_ex(qdev);
Ron Mercer5a4faa82006-07-25 00:40:21 -07001719 }
1720}
1721
1722/*
1723 * MII_Setup needs to be called before taking the PHY out of reset so that the
1724 * management interface clock speed can be set properly. It would be better if
1725 * we had a way to disable MDC until after the PHY is out of reset, but we
1726 * don't have that capability.
1727 */
1728static int ql_mii_setup(struct ql3_adapter *qdev)
1729{
1730 u32 reg;
1731 struct ql3xxx_port_registers __iomem *port_regs =
1732 qdev->mem_map_registers;
1733
1734 if(ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
1735 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
1736 2) << 7))
1737 return -1;
1738
Ron Mercerbd36b0a2007-01-03 16:26:08 -08001739 if (qdev->device_id == QL3032_DEVICE_ID)
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001740 ql_write_page0_reg(qdev,
Ron Mercerbd36b0a2007-01-03 16:26:08 -08001741 &port_regs->macMIIMgmtControlReg, 0x0f00000);
1742
Ron Mercer5a4faa82006-07-25 00:40:21 -07001743 /* Divide 125MHz clock by 28 to meet PHY timing requirements */
1744 reg = MAC_MII_CONTROL_CLK_SEL_DIV28;
1745
1746 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
1747 reg | ((MAC_MII_CONTROL_CLK_SEL_MASK) << 16));
1748
1749 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1750 return 0;
1751}
1752
1753static u32 ql_supported_modes(struct ql3_adapter *qdev)
1754{
1755 u32 supported;
1756
1757 if (test_bit(QL_LINK_OPTICAL,&qdev->flags)) {
1758 supported = SUPPORTED_1000baseT_Full | SUPPORTED_FIBRE
1759 | SUPPORTED_Autoneg;
1760 } else {
1761 supported = SUPPORTED_10baseT_Half
1762 | SUPPORTED_10baseT_Full
1763 | SUPPORTED_100baseT_Half
1764 | SUPPORTED_100baseT_Full
1765 | SUPPORTED_1000baseT_Half
1766 | SUPPORTED_1000baseT_Full
1767 | SUPPORTED_Autoneg | SUPPORTED_TP;
1768 }
1769
1770 return supported;
1771}
1772
1773static int ql_get_auto_cfg_status(struct ql3_adapter *qdev)
1774{
1775 int status;
1776 unsigned long hw_flags;
1777 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
1778 if(ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
1779 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
Benjamin Li04f10772007-02-26 11:06:35 -08001780 2) << 7)) {
1781 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercer5a4faa82006-07-25 00:40:21 -07001782 return 0;
Benjamin Li04f10772007-02-26 11:06:35 -08001783 }
Ron Mercer5a4faa82006-07-25 00:40:21 -07001784 status = ql_is_auto_cfg(qdev);
1785 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1786 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1787 return status;
1788}
1789
1790static u32 ql_get_speed(struct ql3_adapter *qdev)
1791{
1792 u32 status;
1793 unsigned long hw_flags;
1794 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
1795 if(ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
1796 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
Benjamin Li04f10772007-02-26 11:06:35 -08001797 2) << 7)) {
1798 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercer5a4faa82006-07-25 00:40:21 -07001799 return 0;
Benjamin Li04f10772007-02-26 11:06:35 -08001800 }
Ron Mercer5a4faa82006-07-25 00:40:21 -07001801 status = ql_get_link_speed(qdev);
1802 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1803 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1804 return status;
1805}
1806
1807static int ql_get_full_dup(struct ql3_adapter *qdev)
1808{
1809 int status;
1810 unsigned long hw_flags;
1811 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
1812 if(ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
1813 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
Benjamin Li04f10772007-02-26 11:06:35 -08001814 2) << 7)) {
1815 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercer5a4faa82006-07-25 00:40:21 -07001816 return 0;
Benjamin Li04f10772007-02-26 11:06:35 -08001817 }
Ron Mercer5a4faa82006-07-25 00:40:21 -07001818 status = ql_is_link_full_dup(qdev);
1819 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1820 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1821 return status;
1822}
1823
1824
1825static int ql_get_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1826{
1827 struct ql3_adapter *qdev = netdev_priv(ndev);
1828
1829 ecmd->transceiver = XCVR_INTERNAL;
1830 ecmd->supported = ql_supported_modes(qdev);
1831
1832 if (test_bit(QL_LINK_OPTICAL,&qdev->flags)) {
1833 ecmd->port = PORT_FIBRE;
1834 } else {
1835 ecmd->port = PORT_TP;
1836 ecmd->phy_address = qdev->PHYAddr;
1837 }
1838 ecmd->advertising = ql_supported_modes(qdev);
1839 ecmd->autoneg = ql_get_auto_cfg_status(qdev);
1840 ecmd->speed = ql_get_speed(qdev);
1841 ecmd->duplex = ql_get_full_dup(qdev);
1842 return 0;
1843}
1844
1845static void ql_get_drvinfo(struct net_device *ndev,
1846 struct ethtool_drvinfo *drvinfo)
1847{
1848 struct ql3_adapter *qdev = netdev_priv(ndev);
1849 strncpy(drvinfo->driver, ql3xxx_driver_name, 32);
1850 strncpy(drvinfo->version, ql3xxx_driver_version, 32);
1851 strncpy(drvinfo->fw_version, "N/A", 32);
1852 strncpy(drvinfo->bus_info, pci_name(qdev->pdev), 32);
Ron Mercer5a4faa82006-07-25 00:40:21 -07001853 drvinfo->regdump_len = 0;
1854 drvinfo->eedump_len = 0;
1855}
1856
1857static u32 ql_get_msglevel(struct net_device *ndev)
1858{
1859 struct ql3_adapter *qdev = netdev_priv(ndev);
1860 return qdev->msg_enable;
1861}
1862
1863static void ql_set_msglevel(struct net_device *ndev, u32 value)
1864{
1865 struct ql3_adapter *qdev = netdev_priv(ndev);
1866 qdev->msg_enable = value;
1867}
1868
Ron Mercerec826382007-03-26 13:43:01 -07001869static void ql_get_pauseparam(struct net_device *ndev,
1870 struct ethtool_pauseparam *pause)
1871{
1872 struct ql3_adapter *qdev = netdev_priv(ndev);
1873 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
1874
1875 u32 reg;
1876 if(qdev->mac_index == 0)
1877 reg = ql_read_page0_reg(qdev, &port_regs->mac0ConfigReg);
1878 else
1879 reg = ql_read_page0_reg(qdev, &port_regs->mac1ConfigReg);
1880
1881 pause->autoneg = ql_get_auto_cfg_status(qdev);
1882 pause->rx_pause = (reg & MAC_CONFIG_REG_RF) >> 2;
1883 pause->tx_pause = (reg & MAC_CONFIG_REG_TF) >> 1;
1884}
1885
Jeff Garzik7282d492006-09-13 14:30:00 -04001886static const struct ethtool_ops ql3xxx_ethtool_ops = {
Ron Mercer5a4faa82006-07-25 00:40:21 -07001887 .get_settings = ql_get_settings,
1888 .get_drvinfo = ql_get_drvinfo,
Ron Mercer5a4faa82006-07-25 00:40:21 -07001889 .get_link = ethtool_op_get_link,
1890 .get_msglevel = ql_get_msglevel,
1891 .set_msglevel = ql_set_msglevel,
Ron Mercerec826382007-03-26 13:43:01 -07001892 .get_pauseparam = ql_get_pauseparam,
Ron Mercer5a4faa82006-07-25 00:40:21 -07001893};
1894
1895static int ql_populate_free_queue(struct ql3_adapter *qdev)
1896{
1897 struct ql_rcv_buf_cb *lrg_buf_cb = qdev->lrg_buf_free_head;
Benjamin Li0f8ab892007-02-26 11:06:40 -08001898 dma_addr_t map;
1899 int err;
Ron Mercer5a4faa82006-07-25 00:40:21 -07001900
1901 while (lrg_buf_cb) {
1902 if (!lrg_buf_cb->skb) {
Benjamin Licd238fa2007-02-26 11:06:33 -08001903 lrg_buf_cb->skb = netdev_alloc_skb(qdev->ndev,
1904 qdev->lrg_buffer_len);
Ron Mercer5a4faa82006-07-25 00:40:21 -07001905 if (unlikely(!lrg_buf_cb->skb)) {
1906 printk(KERN_DEBUG PFX
Benjamin Licd238fa2007-02-26 11:06:33 -08001907 "%s: Failed netdev_alloc_skb().\n",
Ron Mercer5a4faa82006-07-25 00:40:21 -07001908 qdev->ndev->name);
1909 break;
1910 } else {
1911 /*
1912 * We save some space to copy the ethhdr from
1913 * first buffer
1914 */
1915 skb_reserve(lrg_buf_cb->skb, QL_HEADER_SPACE);
1916 map = pci_map_single(qdev->pdev,
1917 lrg_buf_cb->skb->data,
1918 qdev->lrg_buffer_len -
1919 QL_HEADER_SPACE,
1920 PCI_DMA_FROMDEVICE);
Benjamin Li0f8ab892007-02-26 11:06:40 -08001921
1922 err = pci_dma_mapping_error(map);
1923 if(err) {
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001924 printk(KERN_ERR "%s: PCI mapping failed with error: %d\n",
Benjamin Li0f8ab892007-02-26 11:06:40 -08001925 qdev->ndev->name, err);
1926 dev_kfree_skb(lrg_buf_cb->skb);
1927 lrg_buf_cb->skb = NULL;
1928 break;
1929 }
1930
1931
Ron Mercer5a4faa82006-07-25 00:40:21 -07001932 lrg_buf_cb->buf_phy_addr_low =
1933 cpu_to_le32(LS_64BITS(map));
1934 lrg_buf_cb->buf_phy_addr_high =
1935 cpu_to_le32(MS_64BITS(map));
1936 pci_unmap_addr_set(lrg_buf_cb, mapaddr, map);
1937 pci_unmap_len_set(lrg_buf_cb, maplen,
1938 qdev->lrg_buffer_len -
1939 QL_HEADER_SPACE);
1940 --qdev->lrg_buf_skb_check;
1941 if (!qdev->lrg_buf_skb_check)
1942 return 1;
1943 }
1944 }
1945 lrg_buf_cb = lrg_buf_cb->next;
1946 }
1947 return 0;
1948}
1949
1950/*
1951 * Caller holds hw_lock.
1952 */
Ron Mercerf67cac02007-03-26 13:42:59 -07001953static void ql_update_small_bufq_prod_index(struct ql3_adapter *qdev)
1954{
1955 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
1956 if (qdev->small_buf_release_cnt >= 16) {
1957 while (qdev->small_buf_release_cnt >= 16) {
1958 qdev->small_buf_q_producer_index++;
1959
1960 if (qdev->small_buf_q_producer_index ==
1961 NUM_SBUFQ_ENTRIES)
1962 qdev->small_buf_q_producer_index = 0;
1963 qdev->small_buf_release_cnt -= 8;
1964 }
1965 wmb();
1966 writel(qdev->small_buf_q_producer_index,
1967 &port_regs->CommonRegs.rxSmallQProducerIndex);
1968 }
1969}
1970
1971/*
1972 * Caller holds hw_lock.
1973 */
Ron Mercer5a4faa82006-07-25 00:40:21 -07001974static void ql_update_lrg_bufq_prod_index(struct ql3_adapter *qdev)
1975{
1976 struct bufq_addr_element *lrg_buf_q_ele;
1977 int i;
1978 struct ql_rcv_buf_cb *lrg_buf_cb;
1979 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
1980
1981 if ((qdev->lrg_buf_free_count >= 8)
1982 && (qdev->lrg_buf_release_cnt >= 16)) {
1983
1984 if (qdev->lrg_buf_skb_check)
1985 if (!ql_populate_free_queue(qdev))
1986 return;
1987
1988 lrg_buf_q_ele = qdev->lrg_buf_next_free;
1989
1990 while ((qdev->lrg_buf_release_cnt >= 16)
1991 && (qdev->lrg_buf_free_count >= 8)) {
1992
1993 for (i = 0; i < 8; i++) {
1994 lrg_buf_cb =
1995 ql_get_from_lrg_buf_free_list(qdev);
1996 lrg_buf_q_ele->addr_high =
1997 lrg_buf_cb->buf_phy_addr_high;
1998 lrg_buf_q_ele->addr_low =
1999 lrg_buf_cb->buf_phy_addr_low;
2000 lrg_buf_q_ele++;
2001
2002 qdev->lrg_buf_release_cnt--;
2003 }
2004
2005 qdev->lrg_buf_q_producer_index++;
2006
Ron Mercer1357bfc2007-02-26 11:06:37 -08002007 if (qdev->lrg_buf_q_producer_index == qdev->num_lbufq_entries)
Ron Mercer5a4faa82006-07-25 00:40:21 -07002008 qdev->lrg_buf_q_producer_index = 0;
2009
2010 if (qdev->lrg_buf_q_producer_index ==
Ron Mercer1357bfc2007-02-26 11:06:37 -08002011 (qdev->num_lbufq_entries - 1)) {
Ron Mercer5a4faa82006-07-25 00:40:21 -07002012 lrg_buf_q_ele = qdev->lrg_buf_q_virt_addr;
2013 }
2014 }
Ron Mercerf67cac02007-03-26 13:42:59 -07002015 wmb();
Ron Mercer5a4faa82006-07-25 00:40:21 -07002016 qdev->lrg_buf_next_free = lrg_buf_q_ele;
Ron Mercerf67cac02007-03-26 13:42:59 -07002017 writel(qdev->lrg_buf_q_producer_index,
2018 &port_regs->CommonRegs.rxLargeQProducerIndex);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002019 }
2020}
2021
2022static void ql_process_mac_tx_intr(struct ql3_adapter *qdev,
2023 struct ob_mac_iocb_rsp *mac_rsp)
2024{
2025 struct ql_tx_buf_cb *tx_cb;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002026 int i;
Benjamin Lie8f4df22007-02-26 11:06:42 -08002027 int retval = 0;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002028
Benjamin Lie8f4df22007-02-26 11:06:42 -08002029 if(mac_rsp->flags & OB_MAC_IOCB_RSP_S) {
2030 printk(KERN_WARNING "Frame short but, frame was padded and sent.\n");
2031 }
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002032
Ron Mercer5a4faa82006-07-25 00:40:21 -07002033 tx_cb = &qdev->tx_buf[mac_rsp->transaction_id];
Benjamin Lie8f4df22007-02-26 11:06:42 -08002034
2035 /* Check the transmit response flags for any errors */
2036 if(mac_rsp->flags & OB_MAC_IOCB_RSP_S) {
2037 printk(KERN_ERR "Frame too short to be legal, frame not sent.\n");
2038
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002039 qdev->ndev->stats.tx_errors++;
Benjamin Lie8f4df22007-02-26 11:06:42 -08002040 retval = -EIO;
2041 goto frame_not_sent;
2042 }
2043
2044 if(tx_cb->seg_count == 0) {
2045 printk(KERN_ERR "tx_cb->seg_count == 0: %d\n", mac_rsp->transaction_id);
2046
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002047 qdev->ndev->stats.tx_errors++;
Benjamin Lie8f4df22007-02-26 11:06:42 -08002048 retval = -EIO;
2049 goto invalid_seg_count;
2050 }
2051
Ron Mercer5a4faa82006-07-25 00:40:21 -07002052 pci_unmap_single(qdev->pdev,
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002053 pci_unmap_addr(&tx_cb->map[0], mapaddr),
2054 pci_unmap_len(&tx_cb->map[0], maplen),
2055 PCI_DMA_TODEVICE);
2056 tx_cb->seg_count--;
2057 if (tx_cb->seg_count) {
2058 for (i = 1; i < tx_cb->seg_count; i++) {
2059 pci_unmap_page(qdev->pdev,
2060 pci_unmap_addr(&tx_cb->map[i],
2061 mapaddr),
2062 pci_unmap_len(&tx_cb->map[i], maplen),
2063 PCI_DMA_TODEVICE);
2064 }
2065 }
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002066 qdev->ndev->stats.tx_packets++;
2067 qdev->ndev->stats.tx_bytes += tx_cb->skb->len;
Benjamin Lie8f4df22007-02-26 11:06:42 -08002068
2069frame_not_sent:
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002070 dev_kfree_skb_irq(tx_cb->skb);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002071 tx_cb->skb = NULL;
Benjamin Lie8f4df22007-02-26 11:06:42 -08002072
2073invalid_seg_count:
Ron Mercer5a4faa82006-07-25 00:40:21 -07002074 atomic_inc(&qdev->tx_count);
2075}
2076
Adrian Bunk36640062007-03-05 02:49:27 +01002077static void ql_get_sbuf(struct ql3_adapter *qdev)
Ron Mercer97916332007-02-26 11:06:38 -08002078{
2079 if (++qdev->small_buf_index == NUM_SMALL_BUFFERS)
2080 qdev->small_buf_index = 0;
2081 qdev->small_buf_release_cnt++;
2082}
2083
Adrian Bunk36640062007-03-05 02:49:27 +01002084static struct ql_rcv_buf_cb *ql_get_lbuf(struct ql3_adapter *qdev)
Ron Mercer97916332007-02-26 11:06:38 -08002085{
2086 struct ql_rcv_buf_cb *lrg_buf_cb = NULL;
2087 lrg_buf_cb = &qdev->lrg_buf[qdev->lrg_buf_index];
2088 qdev->lrg_buf_release_cnt++;
2089 if (++qdev->lrg_buf_index == qdev->num_large_buffers)
2090 qdev->lrg_buf_index = 0;
2091 return(lrg_buf_cb);
2092}
2093
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002094/*
2095 * The difference between 3022 and 3032 for inbound completions:
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002096 * 3022 uses two buffers per completion. The first buffer contains
2097 * (some) header info, the second the remainder of the headers plus
2098 * the data. For this chip we reserve some space at the top of the
2099 * receive buffer so that the header info in buffer one can be
2100 * prepended to the buffer two. Buffer two is the sent up while
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002101 * buffer one is returned to the hardware to be reused.
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002102 * 3032 receives all of it's data and headers in one buffer for a
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002103 * simpler process. 3032 also supports checksum verification as
2104 * can be seen in ql_process_macip_rx_intr().
2105 */
Ron Mercer5a4faa82006-07-25 00:40:21 -07002106static void ql_process_mac_rx_intr(struct ql3_adapter *qdev,
2107 struct ib_mac_iocb_rsp *ib_mac_rsp_ptr)
2108{
Ron Mercer5a4faa82006-07-25 00:40:21 -07002109 struct ql_rcv_buf_cb *lrg_buf_cb1 = NULL;
2110 struct ql_rcv_buf_cb *lrg_buf_cb2 = NULL;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002111 struct sk_buff *skb;
2112 u16 length = le16_to_cpu(ib_mac_rsp_ptr->length);
2113
2114 /*
2115 * Get the inbound address list (small buffer).
2116 */
Ron Mercer97916332007-02-26 11:06:38 -08002117 ql_get_sbuf(qdev);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002118
Ron Mercer97916332007-02-26 11:06:38 -08002119 if (qdev->device_id == QL3022_DEVICE_ID)
2120 lrg_buf_cb1 = ql_get_lbuf(qdev);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002121
2122 /* start of second buffer */
Ron Mercer97916332007-02-26 11:06:38 -08002123 lrg_buf_cb2 = ql_get_lbuf(qdev);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002124 skb = lrg_buf_cb2->skb;
2125
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002126 qdev->ndev->stats.rx_packets++;
2127 qdev->ndev->stats.rx_bytes += length;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002128
2129 skb_put(skb, length);
2130 pci_unmap_single(qdev->pdev,
2131 pci_unmap_addr(lrg_buf_cb2, mapaddr),
2132 pci_unmap_len(lrg_buf_cb2, maplen),
2133 PCI_DMA_FROMDEVICE);
2134 prefetch(skb->data);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002135 skb->ip_summed = CHECKSUM_NONE;
2136 skb->protocol = eth_type_trans(skb, qdev->ndev);
2137
2138 netif_receive_skb(skb);
2139 qdev->ndev->last_rx = jiffies;
2140 lrg_buf_cb2->skb = NULL;
2141
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002142 if (qdev->device_id == QL3022_DEVICE_ID)
2143 ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb1);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002144 ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb2);
2145}
2146
2147static void ql_process_macip_rx_intr(struct ql3_adapter *qdev,
2148 struct ib_ip_iocb_rsp *ib_ip_rsp_ptr)
2149{
Ron Mercer5a4faa82006-07-25 00:40:21 -07002150 struct ql_rcv_buf_cb *lrg_buf_cb1 = NULL;
2151 struct ql_rcv_buf_cb *lrg_buf_cb2 = NULL;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002152 struct sk_buff *skb1 = NULL, *skb2;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002153 struct net_device *ndev = qdev->ndev;
2154 u16 length = le16_to_cpu(ib_ip_rsp_ptr->length);
2155 u16 size = 0;
2156
2157 /*
2158 * Get the inbound address list (small buffer).
2159 */
2160
Ron Mercer97916332007-02-26 11:06:38 -08002161 ql_get_sbuf(qdev);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002162
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002163 if (qdev->device_id == QL3022_DEVICE_ID) {
2164 /* start of first buffer on 3022 */
Ron Mercer97916332007-02-26 11:06:38 -08002165 lrg_buf_cb1 = ql_get_lbuf(qdev);
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002166 skb1 = lrg_buf_cb1->skb;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002167 size = ETH_HLEN;
2168 if (*((u16 *) skb1->data) != 0xFFFF)
2169 size += VLAN_ETH_HLEN - ETH_HLEN;
2170 }
Ron Mercer5a4faa82006-07-25 00:40:21 -07002171
2172 /* start of second buffer */
Ron Mercer97916332007-02-26 11:06:38 -08002173 lrg_buf_cb2 = ql_get_lbuf(qdev);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002174 skb2 = lrg_buf_cb2->skb;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002175
Ron Mercer5a4faa82006-07-25 00:40:21 -07002176 skb_put(skb2, length); /* Just the second buffer length here. */
2177 pci_unmap_single(qdev->pdev,
2178 pci_unmap_addr(lrg_buf_cb2, mapaddr),
2179 pci_unmap_len(lrg_buf_cb2, maplen),
2180 PCI_DMA_FROMDEVICE);
2181 prefetch(skb2->data);
2182
Ron Mercer5a4faa82006-07-25 00:40:21 -07002183 skb2->ip_summed = CHECKSUM_NONE;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002184 if (qdev->device_id == QL3022_DEVICE_ID) {
2185 /*
2186 * Copy the ethhdr from first buffer to second. This
2187 * is necessary for 3022 IP completions.
2188 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002189 skb_copy_from_linear_data_offset(skb1, VLAN_ID_LEN,
2190 skb_push(skb2, size), size);
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002191 } else {
2192 u16 checksum = le16_to_cpu(ib_ip_rsp_ptr->checksum);
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002193 if (checksum &
2194 (IB_IP_IOCB_RSP_3032_ICE |
2195 IB_IP_IOCB_RSP_3032_CE)) {
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002196 printk(KERN_ERR
2197 "%s: Bad checksum for this %s packet, checksum = %x.\n",
2198 __func__,
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002199 ((checksum &
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002200 IB_IP_IOCB_RSP_3032_TCP) ? "TCP" :
2201 "UDP"),checksum);
Ron Mercerb3b15142007-03-26 13:43:00 -07002202 } else if ((checksum & IB_IP_IOCB_RSP_3032_TCP) ||
2203 (checksum & IB_IP_IOCB_RSP_3032_UDP &&
2204 !(checksum & IB_IP_IOCB_RSP_3032_NUC))) {
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002205 skb2->ip_summed = CHECKSUM_UNNECESSARY;
Ron Mercerb3b15142007-03-26 13:43:00 -07002206 }
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002207 }
Ron Mercer5a4faa82006-07-25 00:40:21 -07002208 skb2->protocol = eth_type_trans(skb2, qdev->ndev);
2209
2210 netif_receive_skb(skb2);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002211 ndev->stats.rx_packets++;
2212 ndev->stats.rx_bytes += length;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002213 ndev->last_rx = jiffies;
2214 lrg_buf_cb2->skb = NULL;
2215
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002216 if (qdev->device_id == QL3022_DEVICE_ID)
2217 ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb1);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002218 ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb2);
2219}
2220
2221static int ql_tx_rx_clean(struct ql3_adapter *qdev,
2222 int *tx_cleaned, int *rx_cleaned, int work_to_do)
2223{
Ron Mercer5a4faa82006-07-25 00:40:21 -07002224 struct net_rsp_iocb *net_rsp;
2225 struct net_device *ndev = qdev->ndev;
Ron Mercer63b66d12007-02-26 11:06:41 -08002226 int work_done = 0;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002227
2228 /* While there are entries in the completion queue. */
Ron Mercerf67cac02007-03-26 13:42:59 -07002229 while ((le32_to_cpu(*(qdev->prsp_producer_index)) !=
Ron Mercer63b66d12007-02-26 11:06:41 -08002230 qdev->rsp_consumer_index) && (work_done < work_to_do)) {
Ron Mercer5a4faa82006-07-25 00:40:21 -07002231
2232 net_rsp = qdev->rsp_current;
Ron Mercerb323e0e2007-10-01 11:43:22 -07002233 rmb();
Ron Mercer50626292007-10-01 11:43:23 -07002234 /*
2235 * Fix 4032 chipe undocumented "feature" where bit-8 is set if the
2236 * inbound completion is for a VLAN.
2237 */
2238 if (qdev->device_id == QL3032_DEVICE_ID)
2239 net_rsp->opcode &= 0x7f;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002240 switch (net_rsp->opcode) {
2241
2242 case OPCODE_OB_MAC_IOCB_FN0:
2243 case OPCODE_OB_MAC_IOCB_FN2:
2244 ql_process_mac_tx_intr(qdev, (struct ob_mac_iocb_rsp *)
2245 net_rsp);
2246 (*tx_cleaned)++;
2247 break;
2248
2249 case OPCODE_IB_MAC_IOCB:
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002250 case OPCODE_IB_3032_MAC_IOCB:
Ron Mercer5a4faa82006-07-25 00:40:21 -07002251 ql_process_mac_rx_intr(qdev, (struct ib_mac_iocb_rsp *)
2252 net_rsp);
2253 (*rx_cleaned)++;
2254 break;
2255
2256 case OPCODE_IB_IP_IOCB:
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002257 case OPCODE_IB_3032_IP_IOCB:
Ron Mercer5a4faa82006-07-25 00:40:21 -07002258 ql_process_macip_rx_intr(qdev, (struct ib_ip_iocb_rsp *)
2259 net_rsp);
2260 (*rx_cleaned)++;
2261 break;
2262 default:
2263 {
2264 u32 *tmp = (u32 *) net_rsp;
2265 printk(KERN_ERR PFX
2266 "%s: Hit default case, not "
2267 "handled!\n"
2268 " dropping the packet, opcode = "
2269 "%x.\n",
2270 ndev->name, net_rsp->opcode);
2271 printk(KERN_ERR PFX
2272 "0x%08lx 0x%08lx 0x%08lx 0x%08lx \n",
2273 (unsigned long int)tmp[0],
2274 (unsigned long int)tmp[1],
2275 (unsigned long int)tmp[2],
2276 (unsigned long int)tmp[3]);
2277 }
2278 }
2279
2280 qdev->rsp_consumer_index++;
2281
2282 if (qdev->rsp_consumer_index == NUM_RSP_Q_ENTRIES) {
2283 qdev->rsp_consumer_index = 0;
2284 qdev->rsp_current = qdev->rsp_q_virt_addr;
2285 } else {
2286 qdev->rsp_current++;
2287 }
Ron Mercer63b66d12007-02-26 11:06:41 -08002288
2289 work_done = *tx_cleaned + *rx_cleaned;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002290 }
2291
Ron Mercerf67cac02007-03-26 13:42:59 -07002292 return work_done;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002293}
2294
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002295static int ql_poll(struct napi_struct *napi, int budget)
Ron Mercer5a4faa82006-07-25 00:40:21 -07002296{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002297 struct ql3_adapter *qdev = container_of(napi, struct ql3_adapter, napi);
2298 struct net_device *ndev = qdev->ndev;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002299 int rx_cleaned = 0, tx_cleaned = 0;
Ron Mercer63b66d12007-02-26 11:06:41 -08002300 unsigned long hw_flags;
2301 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002302
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002303 ql_tx_rx_clean(qdev, &tx_cleaned, &rx_cleaned, budget);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002304
David S. Miller4ec24112008-01-07 20:48:21 -08002305 if (tx_cleaned + rx_cleaned != budget) {
Ron Mercer63b66d12007-02-26 11:06:41 -08002306 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002307 __netif_rx_complete(ndev, napi);
Ron Mercerf67cac02007-03-26 13:42:59 -07002308 ql_update_small_bufq_prod_index(qdev);
2309 ql_update_lrg_bufq_prod_index(qdev);
2310 writel(qdev->rsp_consumer_index,
2311 &port_regs->CommonRegs.rspQConsumerIndex);
Ron Mercer63b66d12007-02-26 11:06:41 -08002312 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
2313
Ron Mercer5a4faa82006-07-25 00:40:21 -07002314 ql_enable_interrupts(qdev);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002315 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002316 return tx_cleaned + rx_cleaned;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002317}
2318
David Howells7d12e782006-10-05 14:55:46 +01002319static irqreturn_t ql3xxx_isr(int irq, void *dev_id)
Ron Mercer5a4faa82006-07-25 00:40:21 -07002320{
2321
2322 struct net_device *ndev = dev_id;
2323 struct ql3_adapter *qdev = netdev_priv(ndev);
2324 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
2325 u32 value;
2326 int handled = 1;
2327 u32 var;
2328
2329 port_regs = qdev->mem_map_registers;
2330
2331 value =
2332 ql_read_common_reg_l(qdev, &port_regs->CommonRegs.ispControlStatus);
2333
2334 if (value & (ISP_CONTROL_FE | ISP_CONTROL_RI)) {
2335 spin_lock(&qdev->adapter_lock);
2336 netif_stop_queue(qdev->ndev);
2337 netif_carrier_off(qdev->ndev);
2338 ql_disable_interrupts(qdev);
2339 qdev->port_link_state = LS_DOWN;
2340 set_bit(QL_RESET_ACTIVE,&qdev->flags) ;
2341
2342 if (value & ISP_CONTROL_FE) {
2343 /*
2344 * Chip Fatal Error.
2345 */
2346 var =
2347 ql_read_page0_reg_l(qdev,
2348 &port_regs->PortFatalErrStatus);
2349 printk(KERN_WARNING PFX
2350 "%s: Resetting chip. PortFatalErrStatus "
2351 "register = 0x%x\n", ndev->name, var);
2352 set_bit(QL_RESET_START,&qdev->flags) ;
2353 } else {
2354 /*
2355 * Soft Reset Requested.
2356 */
2357 set_bit(QL_RESET_PER_SCSI,&qdev->flags) ;
2358 printk(KERN_ERR PFX
2359 "%s: Another function issued a reset to the "
2360 "chip. ISR value = %x.\n", ndev->name, value);
2361 }
David Howellsc4028952006-11-22 14:57:56 +00002362 queue_delayed_work(qdev->workqueue, &qdev->reset_work, 0);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002363 spin_unlock(&qdev->adapter_lock);
2364 } else if (value & ISP_IMR_DISABLE_CMPL_INT) {
Benjamin Lie8f4df22007-02-26 11:06:42 -08002365 ql_disable_interrupts(qdev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002366 if (likely(netif_rx_schedule_prep(ndev, &qdev->napi))) {
2367 __netif_rx_schedule(ndev, &qdev->napi);
Ron Mercer63b66d12007-02-26 11:06:41 -08002368 }
Ron Mercer5a4faa82006-07-25 00:40:21 -07002369 } else {
2370 return IRQ_NONE;
2371 }
2372
2373 return IRQ_RETVAL(handled);
2374}
2375
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002376/*
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002377 * Get the total number of segments needed for the
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002378 * given number of fragments. This is necessary because
2379 * outbound address lists (OAL) will be used when more than
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002380 * two frags are given. Each address list has 5 addr/len
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002381 * pairs. The 5th pair in each AOL is used to point to
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002382 * the next AOL if more frags are coming.
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002383 * That is why the frags:segment count ratio is not linear.
2384 */
Benjamin Lie8f4df22007-02-26 11:06:42 -08002385static int ql_get_seg_count(struct ql3_adapter *qdev,
2386 unsigned short frags)
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002387{
Benjamin Lie8f4df22007-02-26 11:06:42 -08002388 if (qdev->device_id == QL3022_DEVICE_ID)
2389 return 1;
2390
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002391 switch(frags) {
2392 case 0: return 1; /* just the skb->data seg */
2393 case 1: return 2; /* skb->data + 1 frag */
2394 case 2: return 3; /* skb->data + 2 frags */
2395 case 3: return 5; /* skb->data + 1 frag + 1 AOL containting 2 frags */
2396 case 4: return 6;
2397 case 5: return 7;
2398 case 6: return 8;
2399 case 7: return 10;
2400 case 8: return 11;
2401 case 9: return 12;
2402 case 10: return 13;
2403 case 11: return 15;
2404 case 12: return 16;
2405 case 13: return 17;
2406 case 14: return 18;
2407 case 15: return 20;
2408 case 16: return 21;
2409 case 17: return 22;
2410 case 18: return 23;
2411 }
2412 return -1;
2413}
2414
Stephen Hemminger91e745a2007-05-30 14:23:18 -07002415static void ql_hw_csum_setup(const struct sk_buff *skb,
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002416 struct ob_mac_iocb_req *mac_iocb_ptr)
2417{
Stephen Hemminger91e745a2007-05-30 14:23:18 -07002418 const struct iphdr *ip = ip_hdr(skb);
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002419
Stephen Hemminger91e745a2007-05-30 14:23:18 -07002420 mac_iocb_ptr->ip_hdr_off = skb_network_offset(skb);
2421 mac_iocb_ptr->ip_hdr_len = ip->ihl;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002422
Stephen Hemminger91e745a2007-05-30 14:23:18 -07002423 if (ip->protocol == IPPROTO_TCP) {
2424 mac_iocb_ptr->flags1 |= OB_3032MAC_IOCB_REQ_TC |
2425 OB_3032MAC_IOCB_REQ_IC;
2426 } else {
2427 mac_iocb_ptr->flags1 |= OB_3032MAC_IOCB_REQ_UC |
2428 OB_3032MAC_IOCB_REQ_IC;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002429 }
2430
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002431}
2432
2433/*
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002434 * Map the buffers for this transmit. This will return
2435 * NETDEV_TX_BUSY or NETDEV_TX_OK based on success.
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002436 */
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002437static int ql_send_map(struct ql3_adapter *qdev,
2438 struct ob_mac_iocb_req *mac_iocb_ptr,
2439 struct ql_tx_buf_cb *tx_cb,
2440 struct sk_buff *skb)
Ron Mercer5a4faa82006-07-25 00:40:21 -07002441{
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002442 struct oal *oal;
2443 struct oal_entry *oal_entry;
Ron Mercer63f77922007-02-28 16:42:17 -08002444 int len = skb_headlen(skb);
Benjamin Li0f8ab892007-02-26 11:06:40 -08002445 dma_addr_t map;
2446 int err;
2447 int completed_segs, i;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002448 int seg_cnt, seg = 0;
2449 int frag_cnt = (int)skb_shinfo(skb)->nr_frags;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002450
Ron Mercerb6967eb2007-03-26 13:42:58 -07002451 seg_cnt = tx_cb->seg_count;
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002452 /*
2453 * Map the skb buffer first.
2454 */
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002455 map = pci_map_single(qdev->pdev, skb->data, len, PCI_DMA_TODEVICE);
Benjamin Li0f8ab892007-02-26 11:06:40 -08002456
2457 err = pci_dma_mapping_error(map);
2458 if(err) {
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002459 printk(KERN_ERR "%s: PCI mapping failed with error: %d\n",
Benjamin Li0f8ab892007-02-26 11:06:40 -08002460 qdev->ndev->name, err);
2461
2462 return NETDEV_TX_BUSY;
2463 }
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002464
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002465 oal_entry = (struct oal_entry *)&mac_iocb_ptr->buf_addr0_low;
2466 oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
2467 oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
2468 oal_entry->len = cpu_to_le32(len);
2469 pci_unmap_addr_set(&tx_cb->map[seg], mapaddr, map);
2470 pci_unmap_len_set(&tx_cb->map[seg], maplen, len);
2471 seg++;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002472
Benjamin Lie8f4df22007-02-26 11:06:42 -08002473 if (seg_cnt == 1) {
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002474 /* Terminate the last segment. */
Marcin Slusarzb39b5a22008-03-04 15:19:20 -08002475 oal_entry->len |= cpu_to_le32(OAL_LAST_ENTRY);
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002476 } else {
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002477 oal = tx_cb->oal;
Benjamin Li0f8ab892007-02-26 11:06:40 -08002478 for (completed_segs=0; completed_segs<frag_cnt; completed_segs++,seg++) {
2479 skb_frag_t *frag = &skb_shinfo(skb)->frags[completed_segs];
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002480 oal_entry++;
2481 if ((seg == 2 && seg_cnt > 3) || /* Check for continuation */
2482 (seg == 7 && seg_cnt > 8) || /* requirements. It's strange */
2483 (seg == 12 && seg_cnt > 13) || /* but necessary. */
2484 (seg == 17 && seg_cnt > 18)) {
2485 /* Continuation entry points to outbound address list. */
2486 map = pci_map_single(qdev->pdev, oal,
2487 sizeof(struct oal),
2488 PCI_DMA_TODEVICE);
Benjamin Li0f8ab892007-02-26 11:06:40 -08002489
2490 err = pci_dma_mapping_error(map);
2491 if(err) {
2492
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002493 printk(KERN_ERR "%s: PCI mapping outbound address list with error: %d\n",
Benjamin Li0f8ab892007-02-26 11:06:40 -08002494 qdev->ndev->name, err);
2495 goto map_error;
2496 }
2497
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002498 oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
2499 oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
2500 oal_entry->len =
2501 cpu_to_le32(sizeof(struct oal) |
2502 OAL_CONT_ENTRY);
2503 pci_unmap_addr_set(&tx_cb->map[seg], mapaddr,
2504 map);
2505 pci_unmap_len_set(&tx_cb->map[seg], maplen,
Ron Mercerb6967eb2007-03-26 13:42:58 -07002506 sizeof(struct oal));
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002507 oal_entry = (struct oal_entry *)oal;
2508 oal++;
2509 seg++;
2510 }
2511
2512 map =
2513 pci_map_page(qdev->pdev, frag->page,
2514 frag->page_offset, frag->size,
2515 PCI_DMA_TODEVICE);
Benjamin Li0f8ab892007-02-26 11:06:40 -08002516
2517 err = pci_dma_mapping_error(map);
2518 if(err) {
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002519 printk(KERN_ERR "%s: PCI mapping frags failed with error: %d\n",
Benjamin Li0f8ab892007-02-26 11:06:40 -08002520 qdev->ndev->name, err);
2521 goto map_error;
2522 }
2523
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002524 oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
2525 oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
2526 oal_entry->len = cpu_to_le32(frag->size);
2527 pci_unmap_addr_set(&tx_cb->map[seg], mapaddr, map);
2528 pci_unmap_len_set(&tx_cb->map[seg], maplen,
2529 frag->size);
2530 }
2531 /* Terminate the last segment. */
Marcin Slusarzb39b5a22008-03-04 15:19:20 -08002532 oal_entry->len |= cpu_to_le32(OAL_LAST_ENTRY);
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002533 }
Benjamin Li0f8ab892007-02-26 11:06:40 -08002534
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002535 return NETDEV_TX_OK;
Benjamin Li0f8ab892007-02-26 11:06:40 -08002536
2537map_error:
2538 /* A PCI mapping failed and now we will need to back out
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002539 * We need to traverse through the oal's and associated pages which
Benjamin Li0f8ab892007-02-26 11:06:40 -08002540 * have been mapped and now we must unmap them to clean up properly
2541 */
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002542
Benjamin Li0f8ab892007-02-26 11:06:40 -08002543 seg = 1;
2544 oal_entry = (struct oal_entry *)&mac_iocb_ptr->buf_addr0_low;
2545 oal = tx_cb->oal;
2546 for (i=0; i<completed_segs; i++,seg++) {
2547 oal_entry++;
2548
2549 if((seg == 2 && seg_cnt > 3) || /* Check for continuation */
2550 (seg == 7 && seg_cnt > 8) || /* requirements. It's strange */
2551 (seg == 12 && seg_cnt > 13) || /* but necessary. */
2552 (seg == 17 && seg_cnt > 18)) {
2553 pci_unmap_single(qdev->pdev,
2554 pci_unmap_addr(&tx_cb->map[seg], mapaddr),
2555 pci_unmap_len(&tx_cb->map[seg], maplen),
2556 PCI_DMA_TODEVICE);
2557 oal++;
2558 seg++;
2559 }
2560
2561 pci_unmap_page(qdev->pdev,
2562 pci_unmap_addr(&tx_cb->map[seg], mapaddr),
2563 pci_unmap_len(&tx_cb->map[seg], maplen),
2564 PCI_DMA_TODEVICE);
2565 }
2566
2567 pci_unmap_single(qdev->pdev,
2568 pci_unmap_addr(&tx_cb->map[0], mapaddr),
2569 pci_unmap_addr(&tx_cb->map[0], maplen),
2570 PCI_DMA_TODEVICE);
2571
2572 return NETDEV_TX_BUSY;
2573
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002574}
2575
2576/*
2577 * The difference between 3022 and 3032 sends:
2578 * 3022 only supports a simple single segment transmission.
2579 * 3032 supports checksumming and scatter/gather lists (fragments).
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002580 * The 3032 supports sglists by using the 3 addr/len pairs (ALP)
2581 * in the IOCB plus a chain of outbound address lists (OAL) that
2582 * each contain 5 ALPs. The last ALP of the IOCB (3rd) or OAL (5th)
2583 * will used to point to an OAL when more ALP entries are required.
2584 * The IOCB is always the top of the chain followed by one or more
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002585 * OALs (when necessary).
2586 */
2587static int ql3xxx_send(struct sk_buff *skb, struct net_device *ndev)
2588{
2589 struct ql3_adapter *qdev = (struct ql3_adapter *)netdev_priv(ndev);
2590 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
2591 struct ql_tx_buf_cb *tx_cb;
2592 u32 tot_len = skb->len;
2593 struct ob_mac_iocb_req *mac_iocb_ptr;
2594
2595 if (unlikely(atomic_read(&qdev->tx_count) < 2)) {
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002596 return NETDEV_TX_BUSY;
2597 }
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002598
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002599 tx_cb = &qdev->tx_buf[qdev->req_producer_index] ;
Benjamin Lie8f4df22007-02-26 11:06:42 -08002600 if((tx_cb->seg_count = ql_get_seg_count(qdev,
2601 (skb_shinfo(skb)->nr_frags))) == -1) {
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002602 printk(KERN_ERR PFX"%s: invalid segment count!\n",__func__);
2603 return NETDEV_TX_OK;
2604 }
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002605
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002606 mac_iocb_ptr = tx_cb->queue_entry;
Ron Mercerd8a759f2007-03-26 13:42:57 -07002607 memset((void *)mac_iocb_ptr, 0, sizeof(struct ob_mac_iocb_req));
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002608 mac_iocb_ptr->opcode = qdev->mac_ob_opcode;
2609 mac_iocb_ptr->flags = OB_MAC_IOCB_REQ_X;
2610 mac_iocb_ptr->flags |= qdev->mb_bit_mask;
2611 mac_iocb_ptr->transaction_id = qdev->req_producer_index;
2612 mac_iocb_ptr->data_len = cpu_to_le16((u16) tot_len);
2613 tx_cb->skb = skb;
Benjamin Lie8f4df22007-02-26 11:06:42 -08002614 if (qdev->device_id == QL3032_DEVICE_ID &&
2615 skb->ip_summed == CHECKSUM_PARTIAL)
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002616 ql_hw_csum_setup(skb, mac_iocb_ptr);
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002617
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002618 if(ql_send_map(qdev,mac_iocb_ptr,tx_cb,skb) != NETDEV_TX_OK) {
2619 printk(KERN_ERR PFX"%s: Could not map the segments!\n",__func__);
2620 return NETDEV_TX_BUSY;
2621 }
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002622
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002623 wmb();
Ron Mercer5a4faa82006-07-25 00:40:21 -07002624 qdev->req_producer_index++;
2625 if (qdev->req_producer_index == NUM_REQ_Q_ENTRIES)
2626 qdev->req_producer_index = 0;
2627 wmb();
2628 ql_write_common_reg_l(qdev,
Al Viroee111d12006-09-25 02:53:53 +01002629 &port_regs->CommonRegs.reqQProducerIndex,
Ron Mercer5a4faa82006-07-25 00:40:21 -07002630 qdev->req_producer_index);
2631
2632 ndev->trans_start = jiffies;
2633 if (netif_msg_tx_queued(qdev))
2634 printk(KERN_DEBUG PFX "%s: tx queued, slot %d, len %d\n",
2635 ndev->name, qdev->req_producer_index, skb->len);
2636
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002637 atomic_dec(&qdev->tx_count);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002638 return NETDEV_TX_OK;
2639}
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002640
Ron Mercer5a4faa82006-07-25 00:40:21 -07002641static int ql_alloc_net_req_rsp_queues(struct ql3_adapter *qdev)
2642{
2643 qdev->req_q_size =
2644 (u32) (NUM_REQ_Q_ENTRIES * sizeof(struct ob_mac_iocb_req));
2645
2646 qdev->req_q_virt_addr =
2647 pci_alloc_consistent(qdev->pdev,
2648 (size_t) qdev->req_q_size,
2649 &qdev->req_q_phy_addr);
2650
2651 if ((qdev->req_q_virt_addr == NULL) ||
2652 LS_64BITS(qdev->req_q_phy_addr) & (qdev->req_q_size - 1)) {
2653 printk(KERN_ERR PFX "%s: reqQ failed.\n",
2654 qdev->ndev->name);
2655 return -ENOMEM;
2656 }
2657
2658 qdev->rsp_q_size = NUM_RSP_Q_ENTRIES * sizeof(struct net_rsp_iocb);
2659
2660 qdev->rsp_q_virt_addr =
2661 pci_alloc_consistent(qdev->pdev,
2662 (size_t) qdev->rsp_q_size,
2663 &qdev->rsp_q_phy_addr);
2664
2665 if ((qdev->rsp_q_virt_addr == NULL) ||
2666 LS_64BITS(qdev->rsp_q_phy_addr) & (qdev->rsp_q_size - 1)) {
2667 printk(KERN_ERR PFX
2668 "%s: rspQ allocation failed\n",
2669 qdev->ndev->name);
2670 pci_free_consistent(qdev->pdev, (size_t) qdev->req_q_size,
2671 qdev->req_q_virt_addr,
2672 qdev->req_q_phy_addr);
2673 return -ENOMEM;
2674 }
2675
2676 set_bit(QL_ALLOC_REQ_RSP_Q_DONE,&qdev->flags);
2677
2678 return 0;
2679}
2680
2681static void ql_free_net_req_rsp_queues(struct ql3_adapter *qdev)
2682{
2683 if (!test_bit(QL_ALLOC_REQ_RSP_Q_DONE,&qdev->flags)) {
2684 printk(KERN_INFO PFX
2685 "%s: Already done.\n", qdev->ndev->name);
2686 return;
2687 }
2688
2689 pci_free_consistent(qdev->pdev,
2690 qdev->req_q_size,
2691 qdev->req_q_virt_addr, qdev->req_q_phy_addr);
2692
2693 qdev->req_q_virt_addr = NULL;
2694
2695 pci_free_consistent(qdev->pdev,
2696 qdev->rsp_q_size,
2697 qdev->rsp_q_virt_addr, qdev->rsp_q_phy_addr);
2698
2699 qdev->rsp_q_virt_addr = NULL;
2700
2701 clear_bit(QL_ALLOC_REQ_RSP_Q_DONE,&qdev->flags);
2702}
2703
2704static int ql_alloc_buffer_queues(struct ql3_adapter *qdev)
2705{
2706 /* Create Large Buffer Queue */
2707 qdev->lrg_buf_q_size =
Ron Mercer1357bfc2007-02-26 11:06:37 -08002708 qdev->num_lbufq_entries * sizeof(struct lrg_buf_q_entry);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002709 if (qdev->lrg_buf_q_size < PAGE_SIZE)
2710 qdev->lrg_buf_q_alloc_size = PAGE_SIZE;
2711 else
2712 qdev->lrg_buf_q_alloc_size = qdev->lrg_buf_q_size * 2;
2713
Ron Mercer1357bfc2007-02-26 11:06:37 -08002714 qdev->lrg_buf = kmalloc(qdev->num_large_buffers * sizeof(struct ql_rcv_buf_cb),GFP_KERNEL);
2715 if (qdev->lrg_buf == NULL) {
2716 printk(KERN_ERR PFX
2717 "%s: qdev->lrg_buf alloc failed.\n", qdev->ndev->name);
2718 return -ENOMEM;
2719 }
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002720
Ron Mercer5a4faa82006-07-25 00:40:21 -07002721 qdev->lrg_buf_q_alloc_virt_addr =
2722 pci_alloc_consistent(qdev->pdev,
2723 qdev->lrg_buf_q_alloc_size,
2724 &qdev->lrg_buf_q_alloc_phy_addr);
2725
2726 if (qdev->lrg_buf_q_alloc_virt_addr == NULL) {
2727 printk(KERN_ERR PFX
2728 "%s: lBufQ failed\n", qdev->ndev->name);
2729 return -ENOMEM;
2730 }
2731 qdev->lrg_buf_q_virt_addr = qdev->lrg_buf_q_alloc_virt_addr;
2732 qdev->lrg_buf_q_phy_addr = qdev->lrg_buf_q_alloc_phy_addr;
2733
2734 /* Create Small Buffer Queue */
2735 qdev->small_buf_q_size =
2736 NUM_SBUFQ_ENTRIES * sizeof(struct lrg_buf_q_entry);
2737 if (qdev->small_buf_q_size < PAGE_SIZE)
2738 qdev->small_buf_q_alloc_size = PAGE_SIZE;
2739 else
2740 qdev->small_buf_q_alloc_size = qdev->small_buf_q_size * 2;
2741
2742 qdev->small_buf_q_alloc_virt_addr =
2743 pci_alloc_consistent(qdev->pdev,
2744 qdev->small_buf_q_alloc_size,
2745 &qdev->small_buf_q_alloc_phy_addr);
2746
2747 if (qdev->small_buf_q_alloc_virt_addr == NULL) {
2748 printk(KERN_ERR PFX
2749 "%s: Small Buffer Queue allocation failed.\n",
2750 qdev->ndev->name);
2751 pci_free_consistent(qdev->pdev, qdev->lrg_buf_q_alloc_size,
2752 qdev->lrg_buf_q_alloc_virt_addr,
2753 qdev->lrg_buf_q_alloc_phy_addr);
2754 return -ENOMEM;
2755 }
2756
2757 qdev->small_buf_q_virt_addr = qdev->small_buf_q_alloc_virt_addr;
2758 qdev->small_buf_q_phy_addr = qdev->small_buf_q_alloc_phy_addr;
2759 set_bit(QL_ALLOC_BUFQS_DONE,&qdev->flags);
2760 return 0;
2761}
2762
2763static void ql_free_buffer_queues(struct ql3_adapter *qdev)
2764{
2765 if (!test_bit(QL_ALLOC_BUFQS_DONE,&qdev->flags)) {
2766 printk(KERN_INFO PFX
2767 "%s: Already done.\n", qdev->ndev->name);
2768 return;
2769 }
Ron Mercer1357bfc2007-02-26 11:06:37 -08002770 if(qdev->lrg_buf) kfree(qdev->lrg_buf);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002771 pci_free_consistent(qdev->pdev,
2772 qdev->lrg_buf_q_alloc_size,
2773 qdev->lrg_buf_q_alloc_virt_addr,
2774 qdev->lrg_buf_q_alloc_phy_addr);
2775
2776 qdev->lrg_buf_q_virt_addr = NULL;
2777
2778 pci_free_consistent(qdev->pdev,
2779 qdev->small_buf_q_alloc_size,
2780 qdev->small_buf_q_alloc_virt_addr,
2781 qdev->small_buf_q_alloc_phy_addr);
2782
2783 qdev->small_buf_q_virt_addr = NULL;
2784
2785 clear_bit(QL_ALLOC_BUFQS_DONE,&qdev->flags);
2786}
2787
2788static int ql_alloc_small_buffers(struct ql3_adapter *qdev)
2789{
2790 int i;
2791 struct bufq_addr_element *small_buf_q_entry;
2792
2793 /* Currently we allocate on one of memory and use it for smallbuffers */
2794 qdev->small_buf_total_size =
2795 (QL_ADDR_ELE_PER_BUFQ_ENTRY * NUM_SBUFQ_ENTRIES *
2796 QL_SMALL_BUFFER_SIZE);
2797
2798 qdev->small_buf_virt_addr =
2799 pci_alloc_consistent(qdev->pdev,
2800 qdev->small_buf_total_size,
2801 &qdev->small_buf_phy_addr);
2802
2803 if (qdev->small_buf_virt_addr == NULL) {
2804 printk(KERN_ERR PFX
2805 "%s: Failed to get small buffer memory.\n",
2806 qdev->ndev->name);
2807 return -ENOMEM;
2808 }
2809
2810 qdev->small_buf_phy_addr_low = LS_64BITS(qdev->small_buf_phy_addr);
2811 qdev->small_buf_phy_addr_high = MS_64BITS(qdev->small_buf_phy_addr);
2812
2813 small_buf_q_entry = qdev->small_buf_q_virt_addr;
2814
Ron Mercer5a4faa82006-07-25 00:40:21 -07002815 /* Initialize the small buffer queue. */
2816 for (i = 0; i < (QL_ADDR_ELE_PER_BUFQ_ENTRY * NUM_SBUFQ_ENTRIES); i++) {
2817 small_buf_q_entry->addr_high =
2818 cpu_to_le32(qdev->small_buf_phy_addr_high);
2819 small_buf_q_entry->addr_low =
2820 cpu_to_le32(qdev->small_buf_phy_addr_low +
2821 (i * QL_SMALL_BUFFER_SIZE));
2822 small_buf_q_entry++;
2823 }
2824 qdev->small_buf_index = 0;
2825 set_bit(QL_ALLOC_SMALL_BUF_DONE,&qdev->flags);
2826 return 0;
2827}
2828
2829static void ql_free_small_buffers(struct ql3_adapter *qdev)
2830{
2831 if (!test_bit(QL_ALLOC_SMALL_BUF_DONE,&qdev->flags)) {
2832 printk(KERN_INFO PFX
2833 "%s: Already done.\n", qdev->ndev->name);
2834 return;
2835 }
2836 if (qdev->small_buf_virt_addr != NULL) {
2837 pci_free_consistent(qdev->pdev,
2838 qdev->small_buf_total_size,
2839 qdev->small_buf_virt_addr,
2840 qdev->small_buf_phy_addr);
2841
2842 qdev->small_buf_virt_addr = NULL;
2843 }
2844}
2845
2846static void ql_free_large_buffers(struct ql3_adapter *qdev)
2847{
2848 int i = 0;
2849 struct ql_rcv_buf_cb *lrg_buf_cb;
2850
Ron Mercer1357bfc2007-02-26 11:06:37 -08002851 for (i = 0; i < qdev->num_large_buffers; i++) {
Ron Mercer5a4faa82006-07-25 00:40:21 -07002852 lrg_buf_cb = &qdev->lrg_buf[i];
2853 if (lrg_buf_cb->skb) {
2854 dev_kfree_skb(lrg_buf_cb->skb);
2855 pci_unmap_single(qdev->pdev,
2856 pci_unmap_addr(lrg_buf_cb, mapaddr),
2857 pci_unmap_len(lrg_buf_cb, maplen),
2858 PCI_DMA_FROMDEVICE);
2859 memset(lrg_buf_cb, 0, sizeof(struct ql_rcv_buf_cb));
2860 } else {
2861 break;
2862 }
2863 }
2864}
2865
2866static void ql_init_large_buffers(struct ql3_adapter *qdev)
2867{
2868 int i;
2869 struct ql_rcv_buf_cb *lrg_buf_cb;
2870 struct bufq_addr_element *buf_addr_ele = qdev->lrg_buf_q_virt_addr;
2871
Ron Mercer1357bfc2007-02-26 11:06:37 -08002872 for (i = 0; i < qdev->num_large_buffers; i++) {
Ron Mercer5a4faa82006-07-25 00:40:21 -07002873 lrg_buf_cb = &qdev->lrg_buf[i];
2874 buf_addr_ele->addr_high = lrg_buf_cb->buf_phy_addr_high;
2875 buf_addr_ele->addr_low = lrg_buf_cb->buf_phy_addr_low;
2876 buf_addr_ele++;
2877 }
2878 qdev->lrg_buf_index = 0;
2879 qdev->lrg_buf_skb_check = 0;
2880}
2881
2882static int ql_alloc_large_buffers(struct ql3_adapter *qdev)
2883{
2884 int i;
2885 struct ql_rcv_buf_cb *lrg_buf_cb;
2886 struct sk_buff *skb;
Benjamin Li0f8ab892007-02-26 11:06:40 -08002887 dma_addr_t map;
2888 int err;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002889
Ron Mercer1357bfc2007-02-26 11:06:37 -08002890 for (i = 0; i < qdev->num_large_buffers; i++) {
Benjamin Licd238fa2007-02-26 11:06:33 -08002891 skb = netdev_alloc_skb(qdev->ndev,
2892 qdev->lrg_buffer_len);
Ron Mercer5a4faa82006-07-25 00:40:21 -07002893 if (unlikely(!skb)) {
2894 /* Better luck next round */
2895 printk(KERN_ERR PFX
2896 "%s: large buff alloc failed, "
2897 "for %d bytes at index %d.\n",
2898 qdev->ndev->name,
2899 qdev->lrg_buffer_len * 2, i);
2900 ql_free_large_buffers(qdev);
2901 return -ENOMEM;
2902 } else {
2903
2904 lrg_buf_cb = &qdev->lrg_buf[i];
2905 memset(lrg_buf_cb, 0, sizeof(struct ql_rcv_buf_cb));
2906 lrg_buf_cb->index = i;
2907 lrg_buf_cb->skb = skb;
2908 /*
2909 * We save some space to copy the ethhdr from first
2910 * buffer
2911 */
2912 skb_reserve(skb, QL_HEADER_SPACE);
2913 map = pci_map_single(qdev->pdev,
2914 skb->data,
2915 qdev->lrg_buffer_len -
2916 QL_HEADER_SPACE,
2917 PCI_DMA_FROMDEVICE);
Benjamin Li0f8ab892007-02-26 11:06:40 -08002918
2919 err = pci_dma_mapping_error(map);
2920 if(err) {
2921 printk(KERN_ERR "%s: PCI mapping failed with error: %d\n",
2922 qdev->ndev->name, err);
2923 ql_free_large_buffers(qdev);
2924 return -ENOMEM;
2925 }
2926
Ron Mercer5a4faa82006-07-25 00:40:21 -07002927 pci_unmap_addr_set(lrg_buf_cb, mapaddr, map);
2928 pci_unmap_len_set(lrg_buf_cb, maplen,
2929 qdev->lrg_buffer_len -
2930 QL_HEADER_SPACE);
2931 lrg_buf_cb->buf_phy_addr_low =
2932 cpu_to_le32(LS_64BITS(map));
2933 lrg_buf_cb->buf_phy_addr_high =
2934 cpu_to_le32(MS_64BITS(map));
2935 }
2936 }
2937 return 0;
2938}
2939
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002940static void ql_free_send_free_list(struct ql3_adapter *qdev)
2941{
2942 struct ql_tx_buf_cb *tx_cb;
2943 int i;
2944
2945 tx_cb = &qdev->tx_buf[0];
2946 for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
2947 if (tx_cb->oal) {
2948 kfree(tx_cb->oal);
2949 tx_cb->oal = NULL;
2950 }
2951 tx_cb++;
2952 }
2953}
2954
2955static int ql_create_send_free_list(struct ql3_adapter *qdev)
Ron Mercer5a4faa82006-07-25 00:40:21 -07002956{
2957 struct ql_tx_buf_cb *tx_cb;
2958 int i;
2959 struct ob_mac_iocb_req *req_q_curr =
2960 qdev->req_q_virt_addr;
2961
2962 /* Create free list of transmit buffers */
2963 for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002964
Ron Mercer5a4faa82006-07-25 00:40:21 -07002965 tx_cb = &qdev->tx_buf[i];
2966 tx_cb->skb = NULL;
2967 tx_cb->queue_entry = req_q_curr;
2968 req_q_curr++;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002969 tx_cb->oal = kmalloc(512, GFP_KERNEL);
2970 if (tx_cb->oal == NULL)
2971 return -1;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002972 }
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002973 return 0;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002974}
2975
2976static int ql_alloc_mem_resources(struct ql3_adapter *qdev)
2977{
Ron Mercer1357bfc2007-02-26 11:06:37 -08002978 if (qdev->ndev->mtu == NORMAL_MTU_SIZE) {
2979 qdev->num_lbufq_entries = NUM_LBUFQ_ENTRIES;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002980 qdev->lrg_buffer_len = NORMAL_MTU_SIZE;
Ron Mercer1357bfc2007-02-26 11:06:37 -08002981 }
Ron Mercer5a4faa82006-07-25 00:40:21 -07002982 else if (qdev->ndev->mtu == JUMBO_MTU_SIZE) {
Ron Mercer1357bfc2007-02-26 11:06:37 -08002983 /*
2984 * Bigger buffers, so less of them.
2985 */
2986 qdev->num_lbufq_entries = JUMBO_NUM_LBUFQ_ENTRIES;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002987 qdev->lrg_buffer_len = JUMBO_MTU_SIZE;
2988 } else {
2989 printk(KERN_ERR PFX
2990 "%s: Invalid mtu size. Only 1500 and 9000 are accepted.\n",
2991 qdev->ndev->name);
2992 return -ENOMEM;
2993 }
Ron Mercer1357bfc2007-02-26 11:06:37 -08002994 qdev->num_large_buffers = qdev->num_lbufq_entries * QL_ADDR_ELE_PER_BUFQ_ENTRY;
Ron Mercer5a4faa82006-07-25 00:40:21 -07002995 qdev->lrg_buffer_len += VLAN_ETH_HLEN + VLAN_ID_LEN + QL_HEADER_SPACE;
2996 qdev->max_frame_size =
2997 (qdev->lrg_buffer_len - QL_HEADER_SPACE) + ETHERNET_CRC_SIZE;
2998
2999 /*
3000 * First allocate a page of shared memory and use it for shadow
3001 * locations of Network Request Queue Consumer Address Register and
3002 * Network Completion Queue Producer Index Register
3003 */
3004 qdev->shadow_reg_virt_addr =
3005 pci_alloc_consistent(qdev->pdev,
3006 PAGE_SIZE, &qdev->shadow_reg_phy_addr);
3007
3008 if (qdev->shadow_reg_virt_addr != NULL) {
3009 qdev->preq_consumer_index = (u16 *) qdev->shadow_reg_virt_addr;
3010 qdev->req_consumer_index_phy_addr_high =
3011 MS_64BITS(qdev->shadow_reg_phy_addr);
3012 qdev->req_consumer_index_phy_addr_low =
3013 LS_64BITS(qdev->shadow_reg_phy_addr);
3014
3015 qdev->prsp_producer_index =
Al Viro804d8542007-12-22 19:44:29 +00003016 (__le32 *) (((u8 *) qdev->preq_consumer_index) + 8);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003017 qdev->rsp_producer_index_phy_addr_high =
3018 qdev->req_consumer_index_phy_addr_high;
3019 qdev->rsp_producer_index_phy_addr_low =
3020 qdev->req_consumer_index_phy_addr_low + 8;
3021 } else {
3022 printk(KERN_ERR PFX
3023 "%s: shadowReg Alloc failed.\n", qdev->ndev->name);
3024 return -ENOMEM;
3025 }
3026
3027 if (ql_alloc_net_req_rsp_queues(qdev) != 0) {
3028 printk(KERN_ERR PFX
3029 "%s: ql_alloc_net_req_rsp_queues failed.\n",
3030 qdev->ndev->name);
3031 goto err_req_rsp;
3032 }
3033
3034 if (ql_alloc_buffer_queues(qdev) != 0) {
3035 printk(KERN_ERR PFX
3036 "%s: ql_alloc_buffer_queues failed.\n",
3037 qdev->ndev->name);
3038 goto err_buffer_queues;
3039 }
3040
3041 if (ql_alloc_small_buffers(qdev) != 0) {
3042 printk(KERN_ERR PFX
3043 "%s: ql_alloc_small_buffers failed\n", qdev->ndev->name);
3044 goto err_small_buffers;
3045 }
3046
3047 if (ql_alloc_large_buffers(qdev) != 0) {
3048 printk(KERN_ERR PFX
3049 "%s: ql_alloc_large_buffers failed\n", qdev->ndev->name);
3050 goto err_small_buffers;
3051 }
3052
3053 /* Initialize the large buffer queue. */
3054 ql_init_large_buffers(qdev);
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003055 if (ql_create_send_free_list(qdev))
3056 goto err_free_list;
Ron Mercer5a4faa82006-07-25 00:40:21 -07003057
3058 qdev->rsp_current = qdev->rsp_q_virt_addr;
3059
3060 return 0;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003061err_free_list:
3062 ql_free_send_free_list(qdev);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003063err_small_buffers:
3064 ql_free_buffer_queues(qdev);
3065err_buffer_queues:
3066 ql_free_net_req_rsp_queues(qdev);
3067err_req_rsp:
3068 pci_free_consistent(qdev->pdev,
3069 PAGE_SIZE,
3070 qdev->shadow_reg_virt_addr,
3071 qdev->shadow_reg_phy_addr);
3072
3073 return -ENOMEM;
3074}
3075
3076static void ql_free_mem_resources(struct ql3_adapter *qdev)
3077{
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003078 ql_free_send_free_list(qdev);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003079 ql_free_large_buffers(qdev);
3080 ql_free_small_buffers(qdev);
3081 ql_free_buffer_queues(qdev);
3082 ql_free_net_req_rsp_queues(qdev);
3083 if (qdev->shadow_reg_virt_addr != NULL) {
3084 pci_free_consistent(qdev->pdev,
3085 PAGE_SIZE,
3086 qdev->shadow_reg_virt_addr,
3087 qdev->shadow_reg_phy_addr);
3088 qdev->shadow_reg_virt_addr = NULL;
3089 }
3090}
3091
3092static int ql_init_misc_registers(struct ql3_adapter *qdev)
3093{
Al Viroee111d12006-09-25 02:53:53 +01003094 struct ql3xxx_local_ram_registers __iomem *local_ram =
3095 (void __iomem *)qdev->mem_map_registers;
Ron Mercer5a4faa82006-07-25 00:40:21 -07003096
3097 if(ql_sem_spinlock(qdev, QL_DDR_RAM_SEM_MASK,
3098 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
3099 2) << 4))
3100 return -1;
3101
3102 ql_write_page2_reg(qdev,
3103 &local_ram->bufletSize, qdev->nvram_data.bufletSize);
3104
3105 ql_write_page2_reg(qdev,
3106 &local_ram->maxBufletCount,
3107 qdev->nvram_data.bufletCount);
3108
3109 ql_write_page2_reg(qdev,
3110 &local_ram->freeBufletThresholdLow,
3111 (qdev->nvram_data.tcpWindowThreshold25 << 16) |
3112 (qdev->nvram_data.tcpWindowThreshold0));
3113
3114 ql_write_page2_reg(qdev,
3115 &local_ram->freeBufletThresholdHigh,
3116 qdev->nvram_data.tcpWindowThreshold50);
3117
3118 ql_write_page2_reg(qdev,
3119 &local_ram->ipHashTableBase,
3120 (qdev->nvram_data.ipHashTableBaseHi << 16) |
3121 qdev->nvram_data.ipHashTableBaseLo);
3122 ql_write_page2_reg(qdev,
3123 &local_ram->ipHashTableCount,
3124 qdev->nvram_data.ipHashTableSize);
3125 ql_write_page2_reg(qdev,
3126 &local_ram->tcpHashTableBase,
3127 (qdev->nvram_data.tcpHashTableBaseHi << 16) |
3128 qdev->nvram_data.tcpHashTableBaseLo);
3129 ql_write_page2_reg(qdev,
3130 &local_ram->tcpHashTableCount,
3131 qdev->nvram_data.tcpHashTableSize);
3132 ql_write_page2_reg(qdev,
3133 &local_ram->ncbBase,
3134 (qdev->nvram_data.ncbTableBaseHi << 16) |
3135 qdev->nvram_data.ncbTableBaseLo);
3136 ql_write_page2_reg(qdev,
3137 &local_ram->maxNcbCount,
3138 qdev->nvram_data.ncbTableSize);
3139 ql_write_page2_reg(qdev,
3140 &local_ram->drbBase,
3141 (qdev->nvram_data.drbTableBaseHi << 16) |
3142 qdev->nvram_data.drbTableBaseLo);
3143 ql_write_page2_reg(qdev,
3144 &local_ram->maxDrbCount,
3145 qdev->nvram_data.drbTableSize);
3146 ql_sem_unlock(qdev, QL_DDR_RAM_SEM_MASK);
3147 return 0;
3148}
3149
3150static int ql_adapter_initialize(struct ql3_adapter *qdev)
3151{
3152 u32 value;
3153 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
3154 struct ql3xxx_host_memory_registers __iomem *hmem_regs =
Al Viroee111d12006-09-25 02:53:53 +01003155 (void __iomem *)port_regs;
Ron Mercer5a4faa82006-07-25 00:40:21 -07003156 u32 delay = 10;
3157 int status = 0;
3158
3159 if(ql_mii_setup(qdev))
3160 return -1;
3161
3162 /* Bring out PHY out of reset */
3163 ql_write_common_reg(qdev, &port_regs->CommonRegs.serialPortInterfaceReg,
3164 (ISP_SERIAL_PORT_IF_WE |
3165 (ISP_SERIAL_PORT_IF_WE << 16)));
3166
3167 qdev->port_link_state = LS_DOWN;
3168 netif_carrier_off(qdev->ndev);
3169
3170 /* V2 chip fix for ARS-39168. */
3171 ql_write_common_reg(qdev, &port_regs->CommonRegs.serialPortInterfaceReg,
3172 (ISP_SERIAL_PORT_IF_SDE |
3173 (ISP_SERIAL_PORT_IF_SDE << 16)));
3174
3175 /* Request Queue Registers */
3176 *((u32 *) (qdev->preq_consumer_index)) = 0;
3177 atomic_set(&qdev->tx_count,NUM_REQ_Q_ENTRIES);
3178 qdev->req_producer_index = 0;
3179
3180 ql_write_page1_reg(qdev,
3181 &hmem_regs->reqConsumerIndexAddrHigh,
3182 qdev->req_consumer_index_phy_addr_high);
3183 ql_write_page1_reg(qdev,
3184 &hmem_regs->reqConsumerIndexAddrLow,
3185 qdev->req_consumer_index_phy_addr_low);
3186
3187 ql_write_page1_reg(qdev,
3188 &hmem_regs->reqBaseAddrHigh,
3189 MS_64BITS(qdev->req_q_phy_addr));
3190 ql_write_page1_reg(qdev,
3191 &hmem_regs->reqBaseAddrLow,
3192 LS_64BITS(qdev->req_q_phy_addr));
3193 ql_write_page1_reg(qdev, &hmem_regs->reqLength, NUM_REQ_Q_ENTRIES);
3194
3195 /* Response Queue Registers */
Al Viro804d8542007-12-22 19:44:29 +00003196 *((__le16 *) (qdev->prsp_producer_index)) = 0;
Ron Mercer5a4faa82006-07-25 00:40:21 -07003197 qdev->rsp_consumer_index = 0;
3198 qdev->rsp_current = qdev->rsp_q_virt_addr;
3199
3200 ql_write_page1_reg(qdev,
3201 &hmem_regs->rspProducerIndexAddrHigh,
3202 qdev->rsp_producer_index_phy_addr_high);
3203
3204 ql_write_page1_reg(qdev,
3205 &hmem_regs->rspProducerIndexAddrLow,
3206 qdev->rsp_producer_index_phy_addr_low);
3207
3208 ql_write_page1_reg(qdev,
3209 &hmem_regs->rspBaseAddrHigh,
3210 MS_64BITS(qdev->rsp_q_phy_addr));
3211
3212 ql_write_page1_reg(qdev,
3213 &hmem_regs->rspBaseAddrLow,
3214 LS_64BITS(qdev->rsp_q_phy_addr));
3215
3216 ql_write_page1_reg(qdev, &hmem_regs->rspLength, NUM_RSP_Q_ENTRIES);
3217
3218 /* Large Buffer Queue */
3219 ql_write_page1_reg(qdev,
3220 &hmem_regs->rxLargeQBaseAddrHigh,
3221 MS_64BITS(qdev->lrg_buf_q_phy_addr));
3222
3223 ql_write_page1_reg(qdev,
3224 &hmem_regs->rxLargeQBaseAddrLow,
3225 LS_64BITS(qdev->lrg_buf_q_phy_addr));
3226
Ron Mercer1357bfc2007-02-26 11:06:37 -08003227 ql_write_page1_reg(qdev, &hmem_regs->rxLargeQLength, qdev->num_lbufq_entries);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003228
3229 ql_write_page1_reg(qdev,
3230 &hmem_regs->rxLargeBufferLength,
3231 qdev->lrg_buffer_len);
3232
3233 /* Small Buffer Queue */
3234 ql_write_page1_reg(qdev,
3235 &hmem_regs->rxSmallQBaseAddrHigh,
3236 MS_64BITS(qdev->small_buf_q_phy_addr));
3237
3238 ql_write_page1_reg(qdev,
3239 &hmem_regs->rxSmallQBaseAddrLow,
3240 LS_64BITS(qdev->small_buf_q_phy_addr));
3241
3242 ql_write_page1_reg(qdev, &hmem_regs->rxSmallQLength, NUM_SBUFQ_ENTRIES);
3243 ql_write_page1_reg(qdev,
3244 &hmem_regs->rxSmallBufferLength,
3245 QL_SMALL_BUFFER_SIZE);
3246
3247 qdev->small_buf_q_producer_index = NUM_SBUFQ_ENTRIES - 1;
3248 qdev->small_buf_release_cnt = 8;
Ron Mercer1357bfc2007-02-26 11:06:37 -08003249 qdev->lrg_buf_q_producer_index = qdev->num_lbufq_entries - 1;
Ron Mercer5a4faa82006-07-25 00:40:21 -07003250 qdev->lrg_buf_release_cnt = 8;
3251 qdev->lrg_buf_next_free =
3252 (struct bufq_addr_element *)qdev->lrg_buf_q_virt_addr;
3253 qdev->small_buf_index = 0;
3254 qdev->lrg_buf_index = 0;
3255 qdev->lrg_buf_free_count = 0;
3256 qdev->lrg_buf_free_head = NULL;
3257 qdev->lrg_buf_free_tail = NULL;
3258
3259 ql_write_common_reg(qdev,
Al Viroee111d12006-09-25 02:53:53 +01003260 &port_regs->CommonRegs.
Ron Mercer5a4faa82006-07-25 00:40:21 -07003261 rxSmallQProducerIndex,
3262 qdev->small_buf_q_producer_index);
3263 ql_write_common_reg(qdev,
Al Viroee111d12006-09-25 02:53:53 +01003264 &port_regs->CommonRegs.
Ron Mercer5a4faa82006-07-25 00:40:21 -07003265 rxLargeQProducerIndex,
3266 qdev->lrg_buf_q_producer_index);
3267
3268 /*
3269 * Find out if the chip has already been initialized. If it has, then
3270 * we skip some of the initialization.
3271 */
3272 clear_bit(QL_LINK_MASTER, &qdev->flags);
3273 value = ql_read_page0_reg(qdev, &port_regs->portStatus);
3274 if ((value & PORT_STATUS_IC) == 0) {
3275
3276 /* Chip has not been configured yet, so let it rip. */
3277 if(ql_init_misc_registers(qdev)) {
3278 status = -1;
3279 goto out;
3280 }
3281
Ron Mercer5a4faa82006-07-25 00:40:21 -07003282 value = qdev->nvram_data.tcpMaxWindowSize;
3283 ql_write_page0_reg(qdev, &port_regs->tcpMaxWindow, value);
3284
3285 value = (0xFFFF << 16) | qdev->nvram_data.extHwConfig;
3286
3287 if(ql_sem_spinlock(qdev, QL_FLASH_SEM_MASK,
3288 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index)
3289 * 2) << 13)) {
3290 status = -1;
3291 goto out;
3292 }
3293 ql_write_page0_reg(qdev, &port_regs->ExternalHWConfig, value);
3294 ql_write_page0_reg(qdev, &port_regs->InternalChipConfig,
3295 (((INTERNAL_CHIP_SD | INTERNAL_CHIP_WE) <<
3296 16) | (INTERNAL_CHIP_SD |
3297 INTERNAL_CHIP_WE)));
3298 ql_sem_unlock(qdev, QL_FLASH_SEM_MASK);
3299 }
3300
Ron Mercerb3b15142007-03-26 13:43:00 -07003301 if (qdev->mac_index)
3302 ql_write_page0_reg(qdev,
3303 &port_regs->mac1MaxFrameLengthReg,
3304 qdev->max_frame_size);
3305 else
3306 ql_write_page0_reg(qdev,
3307 &port_regs->mac0MaxFrameLengthReg,
3308 qdev->max_frame_size);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003309
3310 if(ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
3311 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
3312 2) << 7)) {
3313 status = -1;
3314 goto out;
3315 }
3316
Ron Mercer3efedf22007-03-26 12:43:52 -07003317 PHY_Setup(qdev);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003318 ql_init_scan_mode(qdev);
3319 ql_get_phy_owner(qdev);
3320
3321 /* Load the MAC Configuration */
3322
3323 /* Program lower 32 bits of the MAC address */
3324 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3325 (MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16));
3326 ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
3327 ((qdev->ndev->dev_addr[2] << 24)
3328 | (qdev->ndev->dev_addr[3] << 16)
3329 | (qdev->ndev->dev_addr[4] << 8)
3330 | qdev->ndev->dev_addr[5]));
3331
3332 /* Program top 16 bits of the MAC address */
3333 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3334 ((MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16) | 1));
3335 ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
3336 ((qdev->ndev->dev_addr[0] << 8)
3337 | qdev->ndev->dev_addr[1]));
3338
3339 /* Enable Primary MAC */
3340 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3341 ((MAC_ADDR_INDIRECT_PTR_REG_PE << 16) |
3342 MAC_ADDR_INDIRECT_PTR_REG_PE));
3343
3344 /* Clear Primary and Secondary IP addresses */
3345 ql_write_page0_reg(qdev, &port_regs->ipAddrIndexReg,
3346 ((IP_ADDR_INDEX_REG_MASK << 16) |
3347 (qdev->mac_index << 2)));
3348 ql_write_page0_reg(qdev, &port_regs->ipAddrDataReg, 0);
3349
3350 ql_write_page0_reg(qdev, &port_regs->ipAddrIndexReg,
3351 ((IP_ADDR_INDEX_REG_MASK << 16) |
3352 ((qdev->mac_index << 2) + 1)));
3353 ql_write_page0_reg(qdev, &port_regs->ipAddrDataReg, 0);
3354
3355 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
3356
3357 /* Indicate Configuration Complete */
3358 ql_write_page0_reg(qdev,
3359 &port_regs->portControl,
3360 ((PORT_CONTROL_CC << 16) | PORT_CONTROL_CC));
3361
3362 do {
3363 value = ql_read_page0_reg(qdev, &port_regs->portStatus);
3364 if (value & PORT_STATUS_IC)
3365 break;
3366 msleep(500);
3367 } while (--delay);
3368
3369 if (delay == 0) {
3370 printk(KERN_ERR PFX
3371 "%s: Hw Initialization timeout.\n", qdev->ndev->name);
3372 status = -1;
3373 goto out;
3374 }
3375
3376 /* Enable Ethernet Function */
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003377 if (qdev->device_id == QL3032_DEVICE_ID) {
3378 value =
3379 (QL3032_PORT_CONTROL_EF | QL3032_PORT_CONTROL_KIE |
Ron Mercerb3b15142007-03-26 13:43:00 -07003380 QL3032_PORT_CONTROL_EIv6 | QL3032_PORT_CONTROL_EIv4 |
3381 QL3032_PORT_CONTROL_ET);
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003382 ql_write_page0_reg(qdev, &port_regs->functionControl,
3383 ((value << 16) | value));
3384 } else {
3385 value =
3386 (PORT_CONTROL_EF | PORT_CONTROL_ET | PORT_CONTROL_EI |
3387 PORT_CONTROL_HH);
3388 ql_write_page0_reg(qdev, &port_regs->portControl,
3389 ((value << 16) | value));
3390 }
3391
Ron Mercer5a4faa82006-07-25 00:40:21 -07003392
3393out:
3394 return status;
3395}
3396
3397/*
3398 * Caller holds hw_lock.
3399 */
3400static int ql_adapter_reset(struct ql3_adapter *qdev)
3401{
3402 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
3403 int status = 0;
3404 u16 value;
3405 int max_wait_time;
3406
3407 set_bit(QL_RESET_ACTIVE, &qdev->flags);
3408 clear_bit(QL_RESET_DONE, &qdev->flags);
3409
3410 /*
3411 * Issue soft reset to chip.
3412 */
3413 printk(KERN_DEBUG PFX
3414 "%s: Issue soft reset to chip.\n",
3415 qdev->ndev->name);
3416 ql_write_common_reg(qdev,
Al Viroee111d12006-09-25 02:53:53 +01003417 &port_regs->CommonRegs.ispControlStatus,
Ron Mercer5a4faa82006-07-25 00:40:21 -07003418 ((ISP_CONTROL_SR << 16) | ISP_CONTROL_SR));
3419
3420 /* Wait 3 seconds for reset to complete. */
3421 printk(KERN_DEBUG PFX
3422 "%s: Wait 10 milliseconds for reset to complete.\n",
3423 qdev->ndev->name);
3424
3425 /* Wait until the firmware tells us the Soft Reset is done */
3426 max_wait_time = 5;
3427 do {
3428 value =
3429 ql_read_common_reg(qdev,
3430 &port_regs->CommonRegs.ispControlStatus);
3431 if ((value & ISP_CONTROL_SR) == 0)
3432 break;
3433
3434 ssleep(1);
3435 } while ((--max_wait_time));
3436
3437 /*
3438 * Also, make sure that the Network Reset Interrupt bit has been
3439 * cleared after the soft reset has taken place.
3440 */
3441 value =
3442 ql_read_common_reg(qdev, &port_regs->CommonRegs.ispControlStatus);
3443 if (value & ISP_CONTROL_RI) {
3444 printk(KERN_DEBUG PFX
3445 "ql_adapter_reset: clearing RI after reset.\n");
3446 ql_write_common_reg(qdev,
Al Viroee111d12006-09-25 02:53:53 +01003447 &port_regs->CommonRegs.
Ron Mercer5a4faa82006-07-25 00:40:21 -07003448 ispControlStatus,
3449 ((ISP_CONTROL_RI << 16) | ISP_CONTROL_RI));
3450 }
3451
3452 if (max_wait_time == 0) {
3453 /* Issue Force Soft Reset */
3454 ql_write_common_reg(qdev,
Al Viroee111d12006-09-25 02:53:53 +01003455 &port_regs->CommonRegs.
Ron Mercer5a4faa82006-07-25 00:40:21 -07003456 ispControlStatus,
3457 ((ISP_CONTROL_FSR << 16) |
3458 ISP_CONTROL_FSR));
3459 /*
3460 * Wait until the firmware tells us the Force Soft Reset is
3461 * done
3462 */
3463 max_wait_time = 5;
3464 do {
3465 value =
3466 ql_read_common_reg(qdev,
3467 &port_regs->CommonRegs.
3468 ispControlStatus);
3469 if ((value & ISP_CONTROL_FSR) == 0) {
3470 break;
3471 }
3472 ssleep(1);
3473 } while ((--max_wait_time));
3474 }
3475 if (max_wait_time == 0)
3476 status = 1;
3477
3478 clear_bit(QL_RESET_ACTIVE, &qdev->flags);
3479 set_bit(QL_RESET_DONE, &qdev->flags);
3480 return status;
3481}
3482
3483static void ql_set_mac_info(struct ql3_adapter *qdev)
3484{
3485 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
3486 u32 value, port_status;
3487 u8 func_number;
3488
3489 /* Get the function number */
3490 value =
3491 ql_read_common_reg_l(qdev, &port_regs->CommonRegs.ispControlStatus);
3492 func_number = (u8) ((value >> 4) & OPCODE_FUNC_ID_MASK);
3493 port_status = ql_read_page0_reg(qdev, &port_regs->portStatus);
3494 switch (value & ISP_CONTROL_FN_MASK) {
3495 case ISP_CONTROL_FN0_NET:
3496 qdev->mac_index = 0;
3497 qdev->mac_ob_opcode = OUTBOUND_MAC_IOCB | func_number;
3498 qdev->tcp_ob_opcode = OUTBOUND_TCP_IOCB | func_number;
3499 qdev->update_ob_opcode = UPDATE_NCB_IOCB | func_number;
3500 qdev->mb_bit_mask = FN0_MA_BITS_MASK;
3501 qdev->PHYAddr = PORT0_PHY_ADDRESS;
3502 if (port_status & PORT_STATUS_SM0)
3503 set_bit(QL_LINK_OPTICAL,&qdev->flags);
3504 else
3505 clear_bit(QL_LINK_OPTICAL,&qdev->flags);
3506 break;
3507
3508 case ISP_CONTROL_FN1_NET:
3509 qdev->mac_index = 1;
3510 qdev->mac_ob_opcode = OUTBOUND_MAC_IOCB | func_number;
3511 qdev->tcp_ob_opcode = OUTBOUND_TCP_IOCB | func_number;
3512 qdev->update_ob_opcode = UPDATE_NCB_IOCB | func_number;
3513 qdev->mb_bit_mask = FN1_MA_BITS_MASK;
3514 qdev->PHYAddr = PORT1_PHY_ADDRESS;
3515 if (port_status & PORT_STATUS_SM1)
3516 set_bit(QL_LINK_OPTICAL,&qdev->flags);
3517 else
3518 clear_bit(QL_LINK_OPTICAL,&qdev->flags);
3519 break;
3520
3521 case ISP_CONTROL_FN0_SCSI:
3522 case ISP_CONTROL_FN1_SCSI:
3523 default:
3524 printk(KERN_DEBUG PFX
3525 "%s: Invalid function number, ispControlStatus = 0x%x\n",
3526 qdev->ndev->name,value);
3527 break;
3528 }
Al Viro804d8542007-12-22 19:44:29 +00003529 qdev->numPorts = qdev->nvram_data.version_and_numPorts >> 8;
Ron Mercer5a4faa82006-07-25 00:40:21 -07003530}
3531
3532static void ql_display_dev_info(struct net_device *ndev)
3533{
3534 struct ql3_adapter *qdev = (struct ql3_adapter *)netdev_priv(ndev);
3535 struct pci_dev *pdev = qdev->pdev;
Joe Perches0795af52007-10-03 17:59:30 -07003536 DECLARE_MAC_BUF(mac);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003537
3538 printk(KERN_INFO PFX
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003539 "\n%s Adapter %d RevisionID %d found %s on PCI slot %d.\n",
3540 DRV_NAME, qdev->index, qdev->chip_rev_id,
3541 (qdev->device_id == QL3032_DEVICE_ID) ? "QLA3032" : "QLA3022",
3542 qdev->pci_slot);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003543 printk(KERN_INFO PFX
3544 "%s Interface.\n",
3545 test_bit(QL_LINK_OPTICAL,&qdev->flags) ? "OPTICAL" : "COPPER");
3546
3547 /*
3548 * Print PCI bus width/type.
3549 */
3550 printk(KERN_INFO PFX
3551 "Bus interface is %s %s.\n",
3552 ((qdev->pci_width == 64) ? "64-bit" : "32-bit"),
3553 ((qdev->pci_x) ? "PCI-X" : "PCI"));
3554
3555 printk(KERN_INFO PFX
3556 "mem IO base address adjusted = 0x%p\n",
3557 qdev->mem_map_registers);
3558 printk(KERN_INFO PFX "Interrupt number = %d\n", pdev->irq);
3559
3560 if (netif_msg_probe(qdev))
3561 printk(KERN_INFO PFX
Joe Perches0795af52007-10-03 17:59:30 -07003562 "%s: MAC address %s\n",
3563 ndev->name, print_mac(mac, ndev->dev_addr));
Ron Mercer5a4faa82006-07-25 00:40:21 -07003564}
3565
3566static int ql_adapter_down(struct ql3_adapter *qdev, int do_reset)
3567{
3568 struct net_device *ndev = qdev->ndev;
3569 int retval = 0;
3570
3571 netif_stop_queue(ndev);
3572 netif_carrier_off(ndev);
3573
3574 clear_bit(QL_ADAPTER_UP,&qdev->flags);
3575 clear_bit(QL_LINK_MASTER,&qdev->flags);
3576
3577 ql_disable_interrupts(qdev);
3578
3579 free_irq(qdev->pdev->irq, ndev);
3580
3581 if (qdev->msi && test_bit(QL_MSI_ENABLED,&qdev->flags)) {
3582 printk(KERN_INFO PFX
3583 "%s: calling pci_disable_msi().\n", qdev->ndev->name);
3584 clear_bit(QL_MSI_ENABLED,&qdev->flags);
3585 pci_disable_msi(qdev->pdev);
3586 }
3587
3588 del_timer_sync(&qdev->adapter_timer);
3589
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003590 napi_disable(&qdev->napi);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003591
3592 if (do_reset) {
3593 int soft_reset;
3594 unsigned long hw_flags;
3595
3596 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
3597 if (ql_wait_for_drvr_lock(qdev)) {
3598 if ((soft_reset = ql_adapter_reset(qdev))) {
3599 printk(KERN_ERR PFX
3600 "%s: ql_adapter_reset(%d) FAILED!\n",
3601 ndev->name, qdev->index);
3602 }
3603 printk(KERN_ERR PFX
3604 "%s: Releaseing driver lock via chip reset.\n",ndev->name);
3605 } else {
3606 printk(KERN_ERR PFX
3607 "%s: Could not acquire driver lock to do "
3608 "reset!\n", ndev->name);
3609 retval = -1;
3610 }
3611 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
3612 }
3613 ql_free_mem_resources(qdev);
3614 return retval;
3615}
3616
3617static int ql_adapter_up(struct ql3_adapter *qdev)
3618{
3619 struct net_device *ndev = qdev->ndev;
3620 int err;
Thomas Gleixner38515e92007-02-14 00:33:16 -08003621 unsigned long irq_flags = IRQF_SAMPLE_RANDOM | IRQF_SHARED;
Ron Mercer5a4faa82006-07-25 00:40:21 -07003622 unsigned long hw_flags;
3623
3624 if (ql_alloc_mem_resources(qdev)) {
3625 printk(KERN_ERR PFX
3626 "%s Unable to allocate buffers.\n", ndev->name);
3627 return -ENOMEM;
3628 }
3629
3630 if (qdev->msi) {
3631 if (pci_enable_msi(qdev->pdev)) {
3632 printk(KERN_ERR PFX
3633 "%s: User requested MSI, but MSI failed to "
3634 "initialize. Continuing without MSI.\n",
3635 qdev->ndev->name);
3636 qdev->msi = 0;
3637 } else {
3638 printk(KERN_INFO PFX "%s: MSI Enabled...\n", qdev->ndev->name);
3639 set_bit(QL_MSI_ENABLED,&qdev->flags);
Thomas Gleixner38515e92007-02-14 00:33:16 -08003640 irq_flags &= ~IRQF_SHARED;
Ron Mercer5a4faa82006-07-25 00:40:21 -07003641 }
3642 }
3643
3644 if ((err = request_irq(qdev->pdev->irq,
3645 ql3xxx_isr,
3646 irq_flags, ndev->name, ndev))) {
3647 printk(KERN_ERR PFX
3648 "%s: Failed to reserve interrupt %d already in use.\n",
3649 ndev->name, qdev->pdev->irq);
3650 goto err_irq;
3651 }
3652
3653 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
3654
3655 if ((err = ql_wait_for_drvr_lock(qdev))) {
3656 if ((err = ql_adapter_initialize(qdev))) {
3657 printk(KERN_ERR PFX
3658 "%s: Unable to initialize adapter.\n",
3659 ndev->name);
3660 goto err_init;
3661 }
3662 printk(KERN_ERR PFX
3663 "%s: Releaseing driver lock.\n",ndev->name);
3664 ql_sem_unlock(qdev, QL_DRVR_SEM_MASK);
3665 } else {
3666 printk(KERN_ERR PFX
3667 "%s: Could not aquire driver lock.\n",
3668 ndev->name);
3669 goto err_lock;
3670 }
3671
3672 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
3673
3674 set_bit(QL_ADAPTER_UP,&qdev->flags);
3675
3676 mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);
3677
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003678 napi_enable(&qdev->napi);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003679 ql_enable_interrupts(qdev);
3680 return 0;
3681
3682err_init:
3683 ql_sem_unlock(qdev, QL_DRVR_SEM_MASK);
3684err_lock:
Benjamin Li04f10772007-02-26 11:06:35 -08003685 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003686 free_irq(qdev->pdev->irq, ndev);
3687err_irq:
3688 if (qdev->msi && test_bit(QL_MSI_ENABLED,&qdev->flags)) {
3689 printk(KERN_INFO PFX
3690 "%s: calling pci_disable_msi().\n",
3691 qdev->ndev->name);
3692 clear_bit(QL_MSI_ENABLED,&qdev->flags);
3693 pci_disable_msi(qdev->pdev);
3694 }
3695 return err;
3696}
3697
3698static int ql_cycle_adapter(struct ql3_adapter *qdev, int reset)
3699{
3700 if( ql_adapter_down(qdev,reset) || ql_adapter_up(qdev)) {
3701 printk(KERN_ERR PFX
3702 "%s: Driver up/down cycle failed, "
3703 "closing device\n",qdev->ndev->name);
Ben Hutchingsc81ec802008-05-06 19:36:26 +01003704 rtnl_lock();
Ron Mercer5a4faa82006-07-25 00:40:21 -07003705 dev_close(qdev->ndev);
Ben Hutchingsc81ec802008-05-06 19:36:26 +01003706 rtnl_unlock();
Ron Mercer5a4faa82006-07-25 00:40:21 -07003707 return -1;
3708 }
3709 return 0;
3710}
3711
3712static int ql3xxx_close(struct net_device *ndev)
3713{
3714 struct ql3_adapter *qdev = netdev_priv(ndev);
3715
3716 /*
3717 * Wait for device to recover from a reset.
3718 * (Rarely happens, but possible.)
3719 */
3720 while (!test_bit(QL_ADAPTER_UP,&qdev->flags))
3721 msleep(50);
3722
3723 ql_adapter_down(qdev,QL_DO_RESET);
3724 return 0;
3725}
3726
3727static int ql3xxx_open(struct net_device *ndev)
3728{
3729 struct ql3_adapter *qdev = netdev_priv(ndev);
3730 return (ql_adapter_up(qdev));
3731}
3732
Ron Mercer5a4faa82006-07-25 00:40:21 -07003733static void ql3xxx_set_multicast_list(struct net_device *ndev)
3734{
3735 /*
3736 * We are manually parsing the list in the net_device structure.
3737 */
3738 return;
3739}
3740
3741static int ql3xxx_set_mac_address(struct net_device *ndev, void *p)
3742{
3743 struct ql3_adapter *qdev = (struct ql3_adapter *)netdev_priv(ndev);
3744 struct ql3xxx_port_registers __iomem *port_regs =
3745 qdev->mem_map_registers;
3746 struct sockaddr *addr = p;
3747 unsigned long hw_flags;
3748
3749 if (netif_running(ndev))
3750 return -EBUSY;
3751
3752 if (!is_valid_ether_addr(addr->sa_data))
3753 return -EADDRNOTAVAIL;
3754
3755 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
3756
3757 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
3758 /* Program lower 32 bits of the MAC address */
3759 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3760 (MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16));
3761 ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
3762 ((ndev->dev_addr[2] << 24) | (ndev->
3763 dev_addr[3] << 16) |
3764 (ndev->dev_addr[4] << 8) | ndev->dev_addr[5]));
3765
3766 /* Program top 16 bits of the MAC address */
3767 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3768 ((MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16) | 1));
3769 ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
3770 ((ndev->dev_addr[0] << 8) | ndev->dev_addr[1]));
3771 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
3772
3773 return 0;
3774}
3775
3776static void ql3xxx_tx_timeout(struct net_device *ndev)
3777{
3778 struct ql3_adapter *qdev = (struct ql3_adapter *)netdev_priv(ndev);
3779
3780 printk(KERN_ERR PFX "%s: Resetting...\n", ndev->name);
3781 /*
3782 * Stop the queues, we've got a problem.
3783 */
3784 netif_stop_queue(ndev);
3785
3786 /*
3787 * Wake up the worker to process this event.
3788 */
David Howellsc4028952006-11-22 14:57:56 +00003789 queue_delayed_work(qdev->workqueue, &qdev->tx_timeout_work, 0);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003790}
3791
David Howellsc4028952006-11-22 14:57:56 +00003792static void ql_reset_work(struct work_struct *work)
Ron Mercer5a4faa82006-07-25 00:40:21 -07003793{
David Howellsc4028952006-11-22 14:57:56 +00003794 struct ql3_adapter *qdev =
3795 container_of(work, struct ql3_adapter, reset_work.work);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003796 struct net_device *ndev = qdev->ndev;
3797 u32 value;
3798 struct ql_tx_buf_cb *tx_cb;
3799 int max_wait_time, i;
3800 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
3801 unsigned long hw_flags;
3802
3803 if (test_bit((QL_RESET_PER_SCSI | QL_RESET_START),&qdev->flags)) {
3804 clear_bit(QL_LINK_MASTER,&qdev->flags);
3805
3806 /*
3807 * Loop through the active list and return the skb.
3808 */
3809 for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003810 int j;
Ron Mercer5a4faa82006-07-25 00:40:21 -07003811 tx_cb = &qdev->tx_buf[i];
3812 if (tx_cb->skb) {
Ron Mercer5a4faa82006-07-25 00:40:21 -07003813 printk(KERN_DEBUG PFX
3814 "%s: Freeing lost SKB.\n",
3815 qdev->ndev->name);
3816 pci_unmap_single(qdev->pdev,
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003817 pci_unmap_addr(&tx_cb->map[0], mapaddr),
3818 pci_unmap_len(&tx_cb->map[0], maplen),
3819 PCI_DMA_TODEVICE);
3820 for(j=1;j<tx_cb->seg_count;j++) {
3821 pci_unmap_page(qdev->pdev,
3822 pci_unmap_addr(&tx_cb->map[j],mapaddr),
3823 pci_unmap_len(&tx_cb->map[j],maplen),
3824 PCI_DMA_TODEVICE);
3825 }
Ron Mercer5a4faa82006-07-25 00:40:21 -07003826 dev_kfree_skb(tx_cb->skb);
3827 tx_cb->skb = NULL;
3828 }
3829 }
3830
3831 printk(KERN_ERR PFX
3832 "%s: Clearing NRI after reset.\n", qdev->ndev->name);
3833 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
3834 ql_write_common_reg(qdev,
3835 &port_regs->CommonRegs.
3836 ispControlStatus,
3837 ((ISP_CONTROL_RI << 16) | ISP_CONTROL_RI));
3838 /*
3839 * Wait the for Soft Reset to Complete.
3840 */
3841 max_wait_time = 10;
3842 do {
3843 value = ql_read_common_reg(qdev,
3844 &port_regs->CommonRegs.
3845
3846 ispControlStatus);
3847 if ((value & ISP_CONTROL_SR) == 0) {
3848 printk(KERN_DEBUG PFX
3849 "%s: reset completed.\n",
3850 qdev->ndev->name);
3851 break;
3852 }
3853
3854 if (value & ISP_CONTROL_RI) {
3855 printk(KERN_DEBUG PFX
3856 "%s: clearing NRI after reset.\n",
3857 qdev->ndev->name);
3858 ql_write_common_reg(qdev,
Al Viroee111d12006-09-25 02:53:53 +01003859 &port_regs->
Ron Mercer5a4faa82006-07-25 00:40:21 -07003860 CommonRegs.
3861 ispControlStatus,
3862 ((ISP_CONTROL_RI <<
3863 16) | ISP_CONTROL_RI));
3864 }
3865
3866 ssleep(1);
3867 } while (--max_wait_time);
3868 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
3869
3870 if (value & ISP_CONTROL_SR) {
3871
3872 /*
3873 * Set the reset flags and clear the board again.
3874 * Nothing else to do...
3875 */
3876 printk(KERN_ERR PFX
3877 "%s: Timed out waiting for reset to "
3878 "complete.\n", ndev->name);
3879 printk(KERN_ERR PFX
3880 "%s: Do a reset.\n", ndev->name);
3881 clear_bit(QL_RESET_PER_SCSI,&qdev->flags);
3882 clear_bit(QL_RESET_START,&qdev->flags);
3883 ql_cycle_adapter(qdev,QL_DO_RESET);
3884 return;
3885 }
3886
3887 clear_bit(QL_RESET_ACTIVE,&qdev->flags);
3888 clear_bit(QL_RESET_PER_SCSI,&qdev->flags);
3889 clear_bit(QL_RESET_START,&qdev->flags);
3890 ql_cycle_adapter(qdev,QL_NO_RESET);
3891 }
3892}
3893
David Howellsc4028952006-11-22 14:57:56 +00003894static void ql_tx_timeout_work(struct work_struct *work)
Ron Mercer5a4faa82006-07-25 00:40:21 -07003895{
David Howellsc4028952006-11-22 14:57:56 +00003896 struct ql3_adapter *qdev =
3897 container_of(work, struct ql3_adapter, tx_timeout_work.work);
3898
3899 ql_cycle_adapter(qdev, QL_DO_RESET);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003900}
3901
3902static void ql_get_board_info(struct ql3_adapter *qdev)
3903{
3904 struct ql3xxx_port_registers __iomem *port_regs = qdev->mem_map_registers;
3905 u32 value;
3906
3907 value = ql_read_page0_reg_l(qdev, &port_regs->portStatus);
3908
3909 qdev->chip_rev_id = ((value & PORT_STATUS_REV_ID_MASK) >> 12);
3910 if (value & PORT_STATUS_64)
3911 qdev->pci_width = 64;
3912 else
3913 qdev->pci_width = 32;
3914 if (value & PORT_STATUS_X)
3915 qdev->pci_x = 1;
3916 else
3917 qdev->pci_x = 0;
3918 qdev->pci_slot = (u8) PCI_SLOT(qdev->pdev->devfn);
3919}
3920
3921static void ql3xxx_timer(unsigned long ptr)
3922{
3923 struct ql3_adapter *qdev = (struct ql3_adapter *)ptr;
Ron Mercer3e23b7d2007-11-07 13:59:06 -08003924 queue_delayed_work(qdev->workqueue, &qdev->link_state_work, 0);
Ron Mercer5a4faa82006-07-25 00:40:21 -07003925}
3926
3927static int __devinit ql3xxx_probe(struct pci_dev *pdev,
3928 const struct pci_device_id *pci_entry)
3929{
3930 struct net_device *ndev = NULL;
3931 struct ql3_adapter *qdev = NULL;
3932 static int cards_found = 0;
3933 int pci_using_dac, err;
3934
3935 err = pci_enable_device(pdev);
3936 if (err) {
3937 printk(KERN_ERR PFX "%s cannot enable PCI device\n",
3938 pci_name(pdev));
3939 goto err_out;
3940 }
3941
3942 err = pci_request_regions(pdev, DRV_NAME);
3943 if (err) {
3944 printk(KERN_ERR PFX "%s cannot obtain PCI resources\n",
3945 pci_name(pdev));
3946 goto err_out_disable_pdev;
3947 }
3948
3949 pci_set_master(pdev);
3950
3951 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
3952 pci_using_dac = 1;
3953 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
3954 } else if (!(err = pci_set_dma_mask(pdev, DMA_32BIT_MASK))) {
3955 pci_using_dac = 0;
3956 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
3957 }
3958
3959 if (err) {
3960 printk(KERN_ERR PFX "%s no usable DMA configuration\n",
3961 pci_name(pdev));
3962 goto err_out_free_regions;
3963 }
3964
3965 ndev = alloc_etherdev(sizeof(struct ql3_adapter));
Benjamin Li546faf02007-02-26 11:06:31 -08003966 if (!ndev) {
3967 printk(KERN_ERR PFX "%s could not alloc etherdev\n",
3968 pci_name(pdev));
3969 err = -ENOMEM;
Ron Mercer5a4faa82006-07-25 00:40:21 -07003970 goto err_out_free_regions;
Benjamin Li546faf02007-02-26 11:06:31 -08003971 }
Ron Mercer5a4faa82006-07-25 00:40:21 -07003972
Ron Mercer5a4faa82006-07-25 00:40:21 -07003973 SET_NETDEV_DEV(ndev, &pdev->dev);
3974
Ron Mercer5a4faa82006-07-25 00:40:21 -07003975 pci_set_drvdata(pdev, ndev);
3976
3977 qdev = netdev_priv(ndev);
3978 qdev->index = cards_found;
3979 qdev->ndev = ndev;
3980 qdev->pdev = pdev;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003981 qdev->device_id = pci_entry->device;
Ron Mercer5a4faa82006-07-25 00:40:21 -07003982 qdev->port_link_state = LS_DOWN;
3983 if (msi)
3984 qdev->msi = 1;
3985
3986 qdev->msg_enable = netif_msg_init(debug, default_msg);
3987
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003988 if (pci_using_dac)
3989 ndev->features |= NETIF_F_HIGHDMA;
3990 if (qdev->device_id == QL3032_DEVICE_ID)
Stephen Hemmingere68a8c12007-05-30 14:23:17 -07003991 ndev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003992
Ron Mercer5a4faa82006-07-25 00:40:21 -07003993 qdev->mem_map_registers =
3994 ioremap_nocache(pci_resource_start(pdev, 1),
3995 pci_resource_len(qdev->pdev, 1));
3996 if (!qdev->mem_map_registers) {
3997 printk(KERN_ERR PFX "%s: cannot map device registers\n",
3998 pci_name(pdev));
Benjamin Li546faf02007-02-26 11:06:31 -08003999 err = -EIO;
Ron Mercer5a4faa82006-07-25 00:40:21 -07004000 goto err_out_free_ndev;
4001 }
4002
4003 spin_lock_init(&qdev->adapter_lock);
4004 spin_lock_init(&qdev->hw_lock);
4005
4006 /* Set driver entry points */
4007 ndev->open = ql3xxx_open;
4008 ndev->hard_start_xmit = ql3xxx_send;
4009 ndev->stop = ql3xxx_close;
Ron Mercer5a4faa82006-07-25 00:40:21 -07004010 ndev->set_multicast_list = ql3xxx_set_multicast_list;
4011 SET_ETHTOOL_OPS(ndev, &ql3xxx_ethtool_ops);
4012 ndev->set_mac_address = ql3xxx_set_mac_address;
4013 ndev->tx_timeout = ql3xxx_tx_timeout;
4014 ndev->watchdog_timeo = 5 * HZ;
4015
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004016 netif_napi_add(ndev, &qdev->napi, ql_poll, 64);
Ron Mercer5a4faa82006-07-25 00:40:21 -07004017
4018 ndev->irq = pdev->irq;
4019
4020 /* make sure the EEPROM is good */
4021 if (ql_get_nvram_params(qdev)) {
4022 printk(KERN_ALERT PFX
4023 "ql3xxx_probe: Adapter #%d, Invalid NVRAM parameters.\n",
4024 qdev->index);
Benjamin Li546faf02007-02-26 11:06:31 -08004025 err = -EIO;
Ron Mercer5a4faa82006-07-25 00:40:21 -07004026 goto err_out_iounmap;
4027 }
4028
4029 ql_set_mac_info(qdev);
4030
4031 /* Validate and set parameters */
4032 if (qdev->mac_index) {
Ron Mercercb8bac12007-02-26 11:06:36 -08004033 ndev->mtu = qdev->nvram_data.macCfg_port1.etherMtu_mac ;
Al Viro804d8542007-12-22 19:44:29 +00004034 ql_set_mac_addr(ndev, qdev->nvram_data.funcCfg_fn2.macAddress);
Ron Mercer5a4faa82006-07-25 00:40:21 -07004035 } else {
Ron Mercercb8bac12007-02-26 11:06:36 -08004036 ndev->mtu = qdev->nvram_data.macCfg_port0.etherMtu_mac ;
Al Viro804d8542007-12-22 19:44:29 +00004037 ql_set_mac_addr(ndev, qdev->nvram_data.funcCfg_fn0.macAddress);
Ron Mercer5a4faa82006-07-25 00:40:21 -07004038 }
4039 memcpy(ndev->perm_addr, ndev->dev_addr, ndev->addr_len);
4040
4041 ndev->tx_queue_len = NUM_REQ_Q_ENTRIES;
4042
4043 /* Turn off support for multicasting */
4044 ndev->flags &= ~IFF_MULTICAST;
4045
4046 /* Record PCI bus information. */
4047 ql_get_board_info(qdev);
4048
4049 /*
4050 * Set the Maximum Memory Read Byte Count value. We do this to handle
4051 * jumbo frames.
4052 */
4053 if (qdev->pci_x) {
4054 pci_write_config_word(pdev, (int)0x4e, (u16) 0x0036);
4055 }
4056
4057 err = register_netdev(ndev);
4058 if (err) {
4059 printk(KERN_ERR PFX "%s: cannot register net device\n",
4060 pci_name(pdev));
4061 goto err_out_iounmap;
4062 }
4063
4064 /* we're going to reset, so assume we have no link for now */
4065
4066 netif_carrier_off(ndev);
4067 netif_stop_queue(ndev);
4068
4069 qdev->workqueue = create_singlethread_workqueue(ndev->name);
David Howellsc4028952006-11-22 14:57:56 +00004070 INIT_DELAYED_WORK(&qdev->reset_work, ql_reset_work);
4071 INIT_DELAYED_WORK(&qdev->tx_timeout_work, ql_tx_timeout_work);
Ron Mercer3e23b7d2007-11-07 13:59:06 -08004072 INIT_DELAYED_WORK(&qdev->link_state_work, ql_link_state_machine_work);
Ron Mercer5a4faa82006-07-25 00:40:21 -07004073
4074 init_timer(&qdev->adapter_timer);
4075 qdev->adapter_timer.function = ql3xxx_timer;
4076 qdev->adapter_timer.expires = jiffies + HZ * 2; /* two second delay */
4077 qdev->adapter_timer.data = (unsigned long)qdev;
4078
4079 if(!cards_found) {
4080 printk(KERN_ALERT PFX "%s\n", DRV_STRING);
4081 printk(KERN_ALERT PFX "Driver name: %s, Version: %s.\n",
4082 DRV_NAME, DRV_VERSION);
4083 }
4084 ql_display_dev_info(ndev);
4085
4086 cards_found++;
4087 return 0;
4088
4089err_out_iounmap:
4090 iounmap(qdev->mem_map_registers);
4091err_out_free_ndev:
4092 free_netdev(ndev);
4093err_out_free_regions:
4094 pci_release_regions(pdev);
4095err_out_disable_pdev:
4096 pci_disable_device(pdev);
4097 pci_set_drvdata(pdev, NULL);
4098err_out:
4099 return err;
4100}
4101
4102static void __devexit ql3xxx_remove(struct pci_dev *pdev)
4103{
4104 struct net_device *ndev = pci_get_drvdata(pdev);
4105 struct ql3_adapter *qdev = netdev_priv(ndev);
4106
4107 unregister_netdev(ndev);
4108 qdev = netdev_priv(ndev);
4109
4110 ql_disable_interrupts(qdev);
4111
4112 if (qdev->workqueue) {
4113 cancel_delayed_work(&qdev->reset_work);
4114 cancel_delayed_work(&qdev->tx_timeout_work);
4115 destroy_workqueue(qdev->workqueue);
4116 qdev->workqueue = NULL;
4117 }
4118
Al Viro855fc732006-09-25 02:54:46 +01004119 iounmap(qdev->mem_map_registers);
Ron Mercer5a4faa82006-07-25 00:40:21 -07004120 pci_release_regions(pdev);
4121 pci_set_drvdata(pdev, NULL);
4122 free_netdev(ndev);
4123}
4124
4125static struct pci_driver ql3xxx_driver = {
4126
4127 .name = DRV_NAME,
4128 .id_table = ql3xxx_pci_tbl,
4129 .probe = ql3xxx_probe,
4130 .remove = __devexit_p(ql3xxx_remove),
4131};
4132
4133static int __init ql3xxx_init_module(void)
4134{
4135 return pci_register_driver(&ql3xxx_driver);
4136}
4137
4138static void __exit ql3xxx_exit(void)
4139{
4140 pci_unregister_driver(&ql3xxx_driver);
4141}
4142
4143module_init(ql3xxx_init_module);
4144module_exit(ql3xxx_exit);