blob: 8fd38cb6d26a54c502ab10d3250c781859a30801 [file] [log] [blame]
Ron Mercer5a4faa872006-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
Joe Percheseddc5fb2010-07-22 12:33:31 +00008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Ron Mercer5a4faa872006-07-25 00:40:21 -070010#include <linux/kernel.h>
11#include <linux/init.h>
12#include <linux/types.h>
13#include <linux/module.h>
14#include <linux/list.h>
15#include <linux/pci.h>
16#include <linux/dma-mapping.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/dmapool.h>
20#include <linux/mempool.h>
21#include <linux/spinlock.h>
22#include <linux/kthread.h>
23#include <linux/interrupt.h>
24#include <linux/errno.h>
25#include <linux/ioport.h>
26#include <linux/ip.h>
Ron Mercerbd36b0a2007-01-03 16:26:08 -080027#include <linux/in.h>
Ron Mercer5a4faa872006-07-25 00:40:21 -070028#include <linux/if_arp.h>
29#include <linux/if_ether.h>
30#include <linux/netdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/ethtool.h>
33#include <linux/skbuff.h>
34#include <linux/rtnetlink.h>
35#include <linux/if_vlan.h>
Ron Mercer5a4faa872006-07-25 00:40:21 -070036#include <linux/delay.h>
37#include <linux/mm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040038#include <linux/prefetch.h>
Ron Mercer5a4faa872006-07-25 00:40:21 -070039
40#include "qla3xxx.h"
41
Joe Perchesd7f61772010-07-22 15:36:17 +000042#define DRV_NAME "qla3xxx"
43#define DRV_STRING "QLogic ISP3XXX Network Driver"
rootb08c42b2008-07-31 13:46:08 -070044#define DRV_VERSION "v2.03.00-k5"
Ron Mercer5a4faa872006-07-25 00:40:21 -070045
46static const char ql3xxx_driver_name[] = DRV_NAME;
47static const char ql3xxx_driver_version[] = DRV_VERSION;
48
Joe Percheseddc5fb2010-07-22 12:33:31 +000049#define TIMED_OUT_MSG \
50"Timed out waiting for management port to get free before issuing command\n"
51
Ron Mercer5a4faa872006-07-25 00:40:21 -070052MODULE_AUTHOR("QLogic Corporation");
53MODULE_DESCRIPTION("QLogic ISP3XXX Network Driver " DRV_VERSION " ");
54MODULE_LICENSE("GPL");
55MODULE_VERSION(DRV_VERSION);
56
57static const u32 default_msg
58 = NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK
59 | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN;
60
61static int debug = -1; /* defaults above */
62module_param(debug, int, 0);
63MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
64
65static int msi;
66module_param(msi, int, 0);
67MODULE_PARM_DESC(msi, "Turn on Message Signaled Interrupts.");
68
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000069static DEFINE_PCI_DEVICE_TABLE(ql3xxx_pci_tbl) = {
Ron Mercer5a4faa872006-07-25 00:40:21 -070070 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QL3022_DEVICE_ID)},
Ron Mercerbd36b0a2007-01-03 16:26:08 -080071 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QL3032_DEVICE_ID)},
Ron Mercer5a4faa872006-07-25 00:40:21 -070072 /* required last entry */
73 {0,}
74};
75
76MODULE_DEVICE_TABLE(pci, ql3xxx_pci_tbl);
77
78/*
Ron Mercer3efedf22007-03-26 12:43:52 -070079 * These are the known PHY's which are used
80 */
Joe Perchesd7f61772010-07-22 15:36:17 +000081enum PHY_DEVICE_TYPE {
Ron Mercer3efedf22007-03-26 12:43:52 -070082 PHY_TYPE_UNKNOWN = 0,
83 PHY_VITESSE_VSC8211,
84 PHY_AGERE_ET1011C,
85 MAX_PHY_DEV_TYPES
Joe Perchesd7f61772010-07-22 15:36:17 +000086};
Ron Mercer3efedf22007-03-26 12:43:52 -070087
Joe Perchesd7f61772010-07-22 15:36:17 +000088struct PHY_DEVICE_INFO {
89 const enum PHY_DEVICE_TYPE phyDevice;
90 const u32 phyIdOUI;
91 const u16 phyIdModel;
92 const char *name;
93};
Ron Mercer3efedf22007-03-26 12:43:52 -070094
Joe Perchesd7f61772010-07-22 15:36:17 +000095static const struct PHY_DEVICE_INFO PHY_DEVICES[] = {
96 {PHY_TYPE_UNKNOWN, 0x000000, 0x0, "PHY_TYPE_UNKNOWN"},
97 {PHY_VITESSE_VSC8211, 0x0003f1, 0xb, "PHY_VITESSE_VSC8211"},
98 {PHY_AGERE_ET1011C, 0x00a0bc, 0x1, "PHY_AGERE_ET1011C"},
Ron Mercer3efedf22007-03-26 12:43:52 -070099};
100
101
102/*
Ron Mercer5a4faa872006-07-25 00:40:21 -0700103 * Caller must take hw_lock.
104 */
105static int ql_sem_spinlock(struct ql3_adapter *qdev,
106 u32 sem_mask, u32 sem_bits)
107{
Joe Perchesd7f61772010-07-22 15:36:17 +0000108 struct ql3xxx_port_registers __iomem *port_regs =
109 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700110 u32 value;
111 unsigned int seconds = 3;
112
113 do {
114 writel((sem_mask | sem_bits),
115 &port_regs->CommonRegs.semaphoreReg);
116 value = readl(&port_regs->CommonRegs.semaphoreReg);
117 if ((value & (sem_mask >> 16)) == sem_bits)
118 return 0;
119 ssleep(1);
Joe Perchesd7f61772010-07-22 15:36:17 +0000120 } while (--seconds);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700121 return -1;
122}
123
124static void ql_sem_unlock(struct ql3_adapter *qdev, u32 sem_mask)
125{
Joe Perchesd7f61772010-07-22 15:36:17 +0000126 struct ql3xxx_port_registers __iomem *port_regs =
127 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700128 writel(sem_mask, &port_regs->CommonRegs.semaphoreReg);
129 readl(&port_regs->CommonRegs.semaphoreReg);
130}
131
132static int ql_sem_lock(struct ql3_adapter *qdev, u32 sem_mask, u32 sem_bits)
133{
Joe Perchesd7f61772010-07-22 15:36:17 +0000134 struct ql3xxx_port_registers __iomem *port_regs =
135 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700136 u32 value;
137
138 writel((sem_mask | sem_bits), &port_regs->CommonRegs.semaphoreReg);
139 value = readl(&port_regs->CommonRegs.semaphoreReg);
140 return ((value & (sem_mask >> 16)) == sem_bits);
141}
142
143/*
144 * Caller holds hw_lock.
145 */
146static int ql_wait_for_drvr_lock(struct ql3_adapter *qdev)
147{
148 int i = 0;
149
Joe Percheseddc5fb2010-07-22 12:33:31 +0000150 while (i < 10) {
151 if (i)
152 ssleep(1);
153
154 if (ql_sem_lock(qdev,
155 QL_DRVR_SEM_MASK,
156 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index)
157 * 2) << 1)) {
158 netdev_printk(KERN_DEBUG, qdev->ndev,
159 "driver lock acquired\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -0700160 return 1;
161 }
162 }
Joe Percheseddc5fb2010-07-22 12:33:31 +0000163
164 netdev_err(qdev->ndev, "Timed out waiting for driver lock...\n");
165 return 0;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700166}
167
168static void ql_set_register_page(struct ql3_adapter *qdev, u32 page)
169{
Joe Perchesd7f61772010-07-22 15:36:17 +0000170 struct ql3xxx_port_registers __iomem *port_regs =
171 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700172
173 writel(((ISP_CONTROL_NP_MASK << 16) | page),
174 &port_regs->CommonRegs.ispControlStatus);
175 readl(&port_regs->CommonRegs.ispControlStatus);
176 qdev->current_page = page;
177}
178
Joe Perchesd7f61772010-07-22 15:36:17 +0000179static u32 ql_read_common_reg_l(struct ql3_adapter *qdev, u32 __iomem *reg)
Ron Mercer5a4faa872006-07-25 00:40:21 -0700180{
181 u32 value;
182 unsigned long hw_flags;
183
184 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
185 value = readl(reg);
186 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
187
188 return value;
189}
190
Joe Perchesd7f61772010-07-22 15:36:17 +0000191static u32 ql_read_common_reg(struct ql3_adapter *qdev, u32 __iomem *reg)
Ron Mercer5a4faa872006-07-25 00:40:21 -0700192{
193 return readl(reg);
194}
195
196static u32 ql_read_page0_reg_l(struct ql3_adapter *qdev, u32 __iomem *reg)
197{
198 u32 value;
199 unsigned long hw_flags;
200
201 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
202
203 if (qdev->current_page != 0)
Joe Perchesd7f61772010-07-22 15:36:17 +0000204 ql_set_register_page(qdev, 0);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700205 value = readl(reg);
206
207 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
208 return value;
209}
210
211static u32 ql_read_page0_reg(struct ql3_adapter *qdev, u32 __iomem *reg)
212{
213 if (qdev->current_page != 0)
Joe Perchesd7f61772010-07-22 15:36:17 +0000214 ql_set_register_page(qdev, 0);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700215 return readl(reg);
216}
217
218static void ql_write_common_reg_l(struct ql3_adapter *qdev,
Al Viroee111d12006-09-25 02:53:53 +0100219 u32 __iomem *reg, u32 value)
Ron Mercer5a4faa872006-07-25 00:40:21 -0700220{
221 unsigned long hw_flags;
222
223 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
Al Viroee111d12006-09-25 02:53:53 +0100224 writel(value, reg);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700225 readl(reg);
226 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700227}
228
229static void ql_write_common_reg(struct ql3_adapter *qdev,
Al Viroee111d12006-09-25 02:53:53 +0100230 u32 __iomem *reg, u32 value)
Ron Mercer5a4faa872006-07-25 00:40:21 -0700231{
Al Viroee111d12006-09-25 02:53:53 +0100232 writel(value, reg);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700233 readl(reg);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700234}
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);
Ron Mercer80b02e52007-01-03 16:26:07 -0800242}
243
Ron Mercer5a4faa872006-07-25 00:40:21 -0700244static void ql_write_page0_reg(struct ql3_adapter *qdev,
Al Viroee111d12006-09-25 02:53:53 +0100245 u32 __iomem *reg, u32 value)
Ron Mercer5a4faa872006-07-25 00:40:21 -0700246{
247 if (qdev->current_page != 0)
Joe Perchesd7f61772010-07-22 15:36:17 +0000248 ql_set_register_page(qdev, 0);
Al Viroee111d12006-09-25 02:53:53 +0100249 writel(value, reg);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700250 readl(reg);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700251}
252
253/*
254 * Caller holds hw_lock. Only called during init.
255 */
256static void ql_write_page1_reg(struct ql3_adapter *qdev,
Al Viroee111d12006-09-25 02:53:53 +0100257 u32 __iomem *reg, u32 value)
Ron Mercer5a4faa872006-07-25 00:40:21 -0700258{
259 if (qdev->current_page != 1)
Joe Perchesd7f61772010-07-22 15:36:17 +0000260 ql_set_register_page(qdev, 1);
Al Viroee111d12006-09-25 02:53:53 +0100261 writel(value, reg);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700262 readl(reg);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700263}
264
265/*
266 * Caller holds hw_lock. Only called during init.
267 */
268static void ql_write_page2_reg(struct ql3_adapter *qdev,
Al Viroee111d12006-09-25 02:53:53 +0100269 u32 __iomem *reg, u32 value)
Ron Mercer5a4faa872006-07-25 00:40:21 -0700270{
271 if (qdev->current_page != 2)
Joe Perchesd7f61772010-07-22 15:36:17 +0000272 ql_set_register_page(qdev, 2);
Al Viroee111d12006-09-25 02:53:53 +0100273 writel(value, reg);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700274 readl(reg);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700275}
276
277static void ql_disable_interrupts(struct ql3_adapter *qdev)
278{
Joe Perchesd7f61772010-07-22 15:36:17 +0000279 struct ql3xxx_port_registers __iomem *port_regs =
280 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700281
282 ql_write_common_reg_l(qdev, &port_regs->CommonRegs.ispInterruptMaskReg,
283 (ISP_IMR_ENABLE_INT << 16));
284
285}
286
287static void ql_enable_interrupts(struct ql3_adapter *qdev)
288{
Joe Perchesd7f61772010-07-22 15:36:17 +0000289 struct ql3xxx_port_registers __iomem *port_regs =
290 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700291
292 ql_write_common_reg_l(qdev, &port_regs->CommonRegs.ispInterruptMaskReg,
293 ((0xff << 16) | ISP_IMR_ENABLE_INT));
294
295}
296
297static void ql_release_to_lrg_buf_free_list(struct ql3_adapter *qdev,
298 struct ql_rcv_buf_cb *lrg_buf_cb)
299{
Benjamin Li0f8ab892007-02-26 11:06:40 -0800300 dma_addr_t map;
301 int err;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700302 lrg_buf_cb->next = NULL;
303
304 if (qdev->lrg_buf_free_tail == NULL) { /* The list is empty */
305 qdev->lrg_buf_free_head = qdev->lrg_buf_free_tail = lrg_buf_cb;
306 } else {
307 qdev->lrg_buf_free_tail->next = lrg_buf_cb;
308 qdev->lrg_buf_free_tail = lrg_buf_cb;
309 }
310
311 if (!lrg_buf_cb->skb) {
Benjamin Licd238fa2007-02-26 11:06:33 -0800312 lrg_buf_cb->skb = netdev_alloc_skb(qdev->ndev,
313 qdev->lrg_buffer_len);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700314 if (unlikely(!lrg_buf_cb->skb)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000315 netdev_err(qdev->ndev, "failed netdev_alloc_skb()\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -0700316 qdev->lrg_buf_skb_check++;
317 } else {
318 /*
319 * We save some space to copy the ethhdr from first
320 * buffer
321 */
322 skb_reserve(lrg_buf_cb->skb, QL_HEADER_SPACE);
323 map = pci_map_single(qdev->pdev,
324 lrg_buf_cb->skb->data,
325 qdev->lrg_buffer_len -
326 QL_HEADER_SPACE,
327 PCI_DMA_FROMDEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700328 err = pci_dma_mapping_error(qdev->pdev, map);
Joe Perchesd7f61772010-07-22 15:36:17 +0000329 if (err) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000330 netdev_err(qdev->ndev,
331 "PCI mapping failed with error: %d\n",
332 err);
Benjamin Li0f8ab892007-02-26 11:06:40 -0800333 dev_kfree_skb(lrg_buf_cb->skb);
334 lrg_buf_cb->skb = NULL;
335
336 qdev->lrg_buf_skb_check++;
337 return;
338 }
339
Ron Mercer5a4faa872006-07-25 00:40:21 -0700340 lrg_buf_cb->buf_phy_addr_low =
341 cpu_to_le32(LS_64BITS(map));
342 lrg_buf_cb->buf_phy_addr_high =
343 cpu_to_le32(MS_64BITS(map));
FUJITA Tomonori87196eb2010-04-12 14:32:13 +0000344 dma_unmap_addr_set(lrg_buf_cb, mapaddr, map);
345 dma_unmap_len_set(lrg_buf_cb, maplen,
Ron Mercer5a4faa872006-07-25 00:40:21 -0700346 qdev->lrg_buffer_len -
347 QL_HEADER_SPACE);
348 }
349 }
350
351 qdev->lrg_buf_free_count++;
352}
353
354static struct ql_rcv_buf_cb *ql_get_from_lrg_buf_free_list(struct ql3_adapter
355 *qdev)
356{
Joe Perchesd7f61772010-07-22 15:36:17 +0000357 struct ql_rcv_buf_cb *lrg_buf_cb = qdev->lrg_buf_free_head;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700358
Joe Perchesd7f61772010-07-22 15:36:17 +0000359 if (lrg_buf_cb != NULL) {
360 qdev->lrg_buf_free_head = lrg_buf_cb->next;
361 if (qdev->lrg_buf_free_head == NULL)
Ron Mercer5a4faa872006-07-25 00:40:21 -0700362 qdev->lrg_buf_free_tail = NULL;
363 qdev->lrg_buf_free_count--;
364 }
365
366 return lrg_buf_cb;
367}
368
369static u32 addrBits = EEPROM_NO_ADDR_BITS;
370static u32 dataBits = EEPROM_NO_DATA_BITS;
371
372static void fm93c56a_deselect(struct ql3_adapter *qdev);
373static void eeprom_readword(struct ql3_adapter *qdev, u32 eepromAddr,
374 unsigned short *value);
375
376/*
377 * Caller holds hw_lock.
378 */
379static void fm93c56a_select(struct ql3_adapter *qdev)
380{
381 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +0000382 qdev->mem_map_registers;
stephen hemminger6f2e1542011-02-23 07:54:27 +0000383 __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700384
385 qdev->eeprom_cmd_data = AUBURN_EEPROM_CS_1;
Joe Perchesd7f61772010-07-22 15:36:17 +0000386 ql_write_nvram_reg(qdev, spir, ISP_NVRAM_MASK | qdev->eeprom_cmd_data);
387 ql_write_nvram_reg(qdev, spir,
388 ((ISP_NVRAM_MASK << 16) | qdev->eeprom_cmd_data));
Ron Mercer5a4faa872006-07-25 00:40:21 -0700389}
390
391/*
392 * Caller holds hw_lock.
393 */
394static void fm93c56a_cmd(struct ql3_adapter *qdev, u32 cmd, u32 eepromAddr)
395{
396 int i;
397 u32 mask;
398 u32 dataBit;
399 u32 previousBit;
400 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +0000401 qdev->mem_map_registers;
stephen hemminger6f2e1542011-02-23 07:54:27 +0000402 __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700403
404 /* Clock in a zero, then do the start bit */
Joe Perchesd7f61772010-07-22 15:36:17 +0000405 ql_write_nvram_reg(qdev, spir,
406 (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
407 AUBURN_EEPROM_DO_1));
408 ql_write_nvram_reg(qdev, spir,
409 (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
410 AUBURN_EEPROM_DO_1 | AUBURN_EEPROM_CLK_RISE));
411 ql_write_nvram_reg(qdev, spir,
412 (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
413 AUBURN_EEPROM_DO_1 | AUBURN_EEPROM_CLK_FALL));
Ron Mercer5a4faa872006-07-25 00:40:21 -0700414
415 mask = 1 << (FM93C56A_CMD_BITS - 1);
416 /* Force the previous data bit to be different */
417 previousBit = 0xffff;
418 for (i = 0; i < FM93C56A_CMD_BITS; i++) {
Joe Perchesd7f61772010-07-22 15:36:17 +0000419 dataBit = (cmd & mask)
420 ? AUBURN_EEPROM_DO_1
421 : AUBURN_EEPROM_DO_0;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700422 if (previousBit != dataBit) {
Joe Perchesd7f61772010-07-22 15:36:17 +0000423 /* If the bit changed, change the DO state to match */
424 ql_write_nvram_reg(qdev, spir,
425 (ISP_NVRAM_MASK |
426 qdev->eeprom_cmd_data | dataBit));
Ron Mercer5a4faa872006-07-25 00:40:21 -0700427 previousBit = dataBit;
428 }
Joe Perchesd7f61772010-07-22 15:36:17 +0000429 ql_write_nvram_reg(qdev, spir,
430 (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
431 dataBit | AUBURN_EEPROM_CLK_RISE));
432 ql_write_nvram_reg(qdev, spir,
433 (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
434 dataBit | AUBURN_EEPROM_CLK_FALL));
Ron Mercer5a4faa872006-07-25 00:40:21 -0700435 cmd = cmd << 1;
436 }
437
438 mask = 1 << (addrBits - 1);
439 /* Force the previous data bit to be different */
440 previousBit = 0xffff;
441 for (i = 0; i < addrBits; i++) {
Joe Perchesd7f61772010-07-22 15:36:17 +0000442 dataBit = (eepromAddr & mask) ? AUBURN_EEPROM_DO_1
443 : AUBURN_EEPROM_DO_0;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700444 if (previousBit != dataBit) {
445 /*
446 * If the bit changed, then change the DO state to
447 * match
448 */
Joe Perchesd7f61772010-07-22 15:36:17 +0000449 ql_write_nvram_reg(qdev, spir,
450 (ISP_NVRAM_MASK |
451 qdev->eeprom_cmd_data | dataBit));
Ron Mercer5a4faa872006-07-25 00:40:21 -0700452 previousBit = dataBit;
453 }
Joe Perchesd7f61772010-07-22 15:36:17 +0000454 ql_write_nvram_reg(qdev, spir,
455 (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
456 dataBit | AUBURN_EEPROM_CLK_RISE));
457 ql_write_nvram_reg(qdev, spir,
458 (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
459 dataBit | AUBURN_EEPROM_CLK_FALL));
Ron Mercer5a4faa872006-07-25 00:40:21 -0700460 eepromAddr = eepromAddr << 1;
461 }
462}
463
464/*
465 * Caller holds hw_lock.
466 */
467static void fm93c56a_deselect(struct ql3_adapter *qdev)
468{
469 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +0000470 qdev->mem_map_registers;
stephen hemminger6f2e1542011-02-23 07:54:27 +0000471 __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
Joe Perchesd7f61772010-07-22 15:36:17 +0000472
Ron Mercer5a4faa872006-07-25 00:40:21 -0700473 qdev->eeprom_cmd_data = AUBURN_EEPROM_CS_0;
Joe Perchesd7f61772010-07-22 15:36:17 +0000474 ql_write_nvram_reg(qdev, spir, ISP_NVRAM_MASK | qdev->eeprom_cmd_data);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700475}
476
477/*
478 * Caller holds hw_lock.
479 */
480static void fm93c56a_datain(struct ql3_adapter *qdev, unsigned short *value)
481{
482 int i;
483 u32 data = 0;
484 u32 dataBit;
485 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +0000486 qdev->mem_map_registers;
stephen hemminger6f2e1542011-02-23 07:54:27 +0000487 __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700488
489 /* Read the data bits */
490 /* The first bit is a dummy. Clock right over it. */
491 for (i = 0; i < dataBits; i++) {
Joe Perchesd7f61772010-07-22 15:36:17 +0000492 ql_write_nvram_reg(qdev, spir,
493 ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
494 AUBURN_EEPROM_CLK_RISE);
495 ql_write_nvram_reg(qdev, spir,
496 ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
497 AUBURN_EEPROM_CLK_FALL);
498 dataBit = (ql_read_common_reg(qdev, spir) &
499 AUBURN_EEPROM_DI_1) ? 1 : 0;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700500 data = (data << 1) | dataBit;
501 }
Joe Perchesd7f61772010-07-22 15:36:17 +0000502 *value = (u16)data;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700503}
504
505/*
506 * Caller holds hw_lock.
507 */
508static void eeprom_readword(struct ql3_adapter *qdev,
509 u32 eepromAddr, unsigned short *value)
510{
511 fm93c56a_select(qdev);
512 fm93c56a_cmd(qdev, (int)FM93C56A_READ, eepromAddr);
513 fm93c56a_datain(qdev, value);
514 fm93c56a_deselect(qdev);
515}
516
Al Viro804d8542007-12-22 19:44:29 +0000517static void ql_set_mac_addr(struct net_device *ndev, u16 *addr)
Ron Mercer5a4faa872006-07-25 00:40:21 -0700518{
Al Viro804d8542007-12-22 19:44:29 +0000519 __le16 *p = (__le16 *)ndev->dev_addr;
520 p[0] = cpu_to_le16(addr[0]);
521 p[1] = cpu_to_le16(addr[1]);
522 p[2] = cpu_to_le16(addr[2]);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700523}
524
525static int ql_get_nvram_params(struct ql3_adapter *qdev)
526{
527 u16 *pEEPROMData;
528 u16 checksum = 0;
529 u32 index;
530 unsigned long hw_flags;
531
532 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
533
Joe Perchesd7f61772010-07-22 15:36:17 +0000534 pEEPROMData = (u16 *)&qdev->nvram_data;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700535 qdev->eeprom_cmd_data = 0;
Joe Perchesd7f61772010-07-22 15:36:17 +0000536 if (ql_sem_spinlock(qdev, QL_NVRAM_SEM_MASK,
Ron Mercer5a4faa872006-07-25 00:40:21 -0700537 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
538 2) << 10)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000539 pr_err("%s: Failed ql_sem_spinlock()\n", __func__);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700540 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
541 return -1;
542 }
543
544 for (index = 0; index < EEPROM_SIZE; index++) {
545 eeprom_readword(qdev, index, pEEPROMData);
546 checksum += *pEEPROMData;
547 pEEPROMData++;
548 }
549 ql_sem_unlock(qdev, QL_NVRAM_SEM_MASK);
550
551 if (checksum != 0) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000552 netdev_err(qdev->ndev, "checksum should be zero, is %x!!\n",
553 checksum);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700554 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
555 return -1;
556 }
557
Ron Mercer5a4faa872006-07-25 00:40:21 -0700558 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
559 return checksum;
560}
561
562static const u32 PHYAddr[2] = {
563 PORT0_PHY_ADDRESS, PORT1_PHY_ADDRESS
564};
565
566static int ql_wait_for_mii_ready(struct ql3_adapter *qdev)
567{
568 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +0000569 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700570 u32 temp;
571 int count = 1000;
572
573 while (count) {
574 temp = ql_read_page0_reg(qdev, &port_regs->macMIIStatusReg);
575 if (!(temp & MAC_MII_STATUS_BSY))
576 return 0;
577 udelay(10);
578 count--;
579 }
580 return -1;
581}
582
583static void ql_mii_enable_scan_mode(struct ql3_adapter *qdev)
584{
585 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +0000586 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700587 u32 scanControl;
588
589 if (qdev->numPorts > 1) {
590 /* Auto scan will cycle through multiple ports */
591 scanControl = MAC_MII_CONTROL_AS | MAC_MII_CONTROL_SC;
592 } else {
593 scanControl = MAC_MII_CONTROL_SC;
594 }
595
596 /*
597 * Scan register 1 of PHY/PETBI,
598 * Set up to scan both devices
599 * The autoscan starts from the first register, completes
600 * the last one before rolling over to the first
601 */
602 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
603 PHYAddr[0] | MII_SCAN_REGISTER);
604
605 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
606 (scanControl) |
607 ((MAC_MII_CONTROL_SC | MAC_MII_CONTROL_AS) << 16));
608}
609
610static u8 ql_mii_disable_scan_mode(struct ql3_adapter *qdev)
611{
612 u8 ret;
613 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +0000614 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700615
616 /* See if scan mode is enabled before we turn it off */
617 if (ql_read_page0_reg(qdev, &port_regs->macMIIMgmtControlReg) &
618 (MAC_MII_CONTROL_AS | MAC_MII_CONTROL_SC)) {
619 /* Scan is enabled */
620 ret = 1;
621 } else {
622 /* Scan is disabled */
623 ret = 0;
624 }
625
626 /*
627 * When disabling scan mode you must first change the MII register
628 * address
629 */
630 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
631 PHYAddr[0] | MII_SCAN_REGISTER);
632
633 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
634 ((MAC_MII_CONTROL_SC | MAC_MII_CONTROL_AS |
635 MAC_MII_CONTROL_RC) << 16));
636
637 return ret;
638}
639
640static int ql_mii_write_reg_ex(struct ql3_adapter *qdev,
Ron Mercer3efedf22007-03-26 12:43:52 -0700641 u16 regAddr, u16 value, u32 phyAddr)
Ron Mercer5a4faa872006-07-25 00:40:21 -0700642{
643 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +0000644 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700645 u8 scanWasEnabled;
646
647 scanWasEnabled = ql_mii_disable_scan_mode(qdev);
648
649 if (ql_wait_for_mii_ready(qdev)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000650 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700651 return -1;
652 }
653
654 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
Ron Mercer3efedf22007-03-26 12:43:52 -0700655 phyAddr | regAddr);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700656
657 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtDataReg, value);
658
659 /* Wait for write to complete 9/10/04 SJP */
660 if (ql_wait_for_mii_ready(qdev)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000661 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700662 return -1;
663 }
664
665 if (scanWasEnabled)
666 ql_mii_enable_scan_mode(qdev);
667
668 return 0;
669}
670
671static int ql_mii_read_reg_ex(struct ql3_adapter *qdev, u16 regAddr,
Joe Perchesd7f61772010-07-22 15:36:17 +0000672 u16 *value, u32 phyAddr)
Ron Mercer5a4faa872006-07-25 00:40:21 -0700673{
674 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +0000675 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700676 u8 scanWasEnabled;
677 u32 temp;
678
679 scanWasEnabled = ql_mii_disable_scan_mode(qdev);
680
681 if (ql_wait_for_mii_ready(qdev)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000682 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700683 return -1;
684 }
685
686 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
Ron Mercer3efedf22007-03-26 12:43:52 -0700687 phyAddr | regAddr);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700688
689 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
690 (MAC_MII_CONTROL_RC << 16));
691
692 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
693 (MAC_MII_CONTROL_RC << 16) | MAC_MII_CONTROL_RC);
694
695 /* Wait for the read to complete */
696 if (ql_wait_for_mii_ready(qdev)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000697 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700698 return -1;
699 }
700
701 temp = ql_read_page0_reg(qdev, &port_regs->macMIIMgmtDataReg);
702 *value = (u16) temp;
703
704 if (scanWasEnabled)
705 ql_mii_enable_scan_mode(qdev);
706
707 return 0;
708}
709
710static int ql_mii_write_reg(struct ql3_adapter *qdev, u16 regAddr, u16 value)
711{
712 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +0000713 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700714
715 ql_mii_disable_scan_mode(qdev);
716
717 if (ql_wait_for_mii_ready(qdev)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000718 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700719 return -1;
720 }
721
722 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
723 qdev->PHYAddr | regAddr);
724
725 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtDataReg, value);
726
727 /* Wait for write to complete. */
728 if (ql_wait_for_mii_ready(qdev)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000729 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700730 return -1;
731 }
732
733 ql_mii_enable_scan_mode(qdev);
734
735 return 0;
736}
737
738static int ql_mii_read_reg(struct ql3_adapter *qdev, u16 regAddr, u16 *value)
739{
740 u32 temp;
741 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +0000742 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700743
744 ql_mii_disable_scan_mode(qdev);
745
746 if (ql_wait_for_mii_ready(qdev)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000747 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700748 return -1;
749 }
750
751 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
752 qdev->PHYAddr | regAddr);
753
754 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
755 (MAC_MII_CONTROL_RC << 16));
756
757 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
758 (MAC_MII_CONTROL_RC << 16) | MAC_MII_CONTROL_RC);
759
760 /* Wait for the read to complete */
761 if (ql_wait_for_mii_ready(qdev)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000762 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700763 return -1;
764 }
765
766 temp = ql_read_page0_reg(qdev, &port_regs->macMIIMgmtDataReg);
767 *value = (u16) temp;
768
769 ql_mii_enable_scan_mode(qdev);
770
771 return 0;
772}
773
774static void ql_petbi_reset(struct ql3_adapter *qdev)
775{
776 ql_mii_write_reg(qdev, PETBI_CONTROL_REG, PETBI_CTRL_SOFT_RESET);
777}
778
779static void ql_petbi_start_neg(struct ql3_adapter *qdev)
780{
781 u16 reg;
782
783 /* Enable Auto-negotiation sense */
784 ql_mii_read_reg(qdev, PETBI_TBI_CTRL, &reg);
785 reg |= PETBI_TBI_AUTO_SENSE;
786 ql_mii_write_reg(qdev, PETBI_TBI_CTRL, reg);
787
788 ql_mii_write_reg(qdev, PETBI_NEG_ADVER,
789 PETBI_NEG_PAUSE | PETBI_NEG_DUPLEX);
790
791 ql_mii_write_reg(qdev, PETBI_CONTROL_REG,
792 PETBI_CTRL_AUTO_NEG | PETBI_CTRL_RESTART_NEG |
793 PETBI_CTRL_FULL_DUPLEX | PETBI_CTRL_SPEED_1000);
794
795}
796
Ron Mercer3efedf22007-03-26 12:43:52 -0700797static void ql_petbi_reset_ex(struct ql3_adapter *qdev)
Ron Mercer5a4faa872006-07-25 00:40:21 -0700798{
799 ql_mii_write_reg_ex(qdev, PETBI_CONTROL_REG, PETBI_CTRL_SOFT_RESET,
Ron Mercer3efedf22007-03-26 12:43:52 -0700800 PHYAddr[qdev->mac_index]);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700801}
802
Ron Mercer3efedf22007-03-26 12:43:52 -0700803static void ql_petbi_start_neg_ex(struct ql3_adapter *qdev)
Ron Mercer5a4faa872006-07-25 00:40:21 -0700804{
805 u16 reg;
806
807 /* Enable Auto-negotiation sense */
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400808 ql_mii_read_reg_ex(qdev, PETBI_TBI_CTRL, &reg,
Ron Mercer3efedf22007-03-26 12:43:52 -0700809 PHYAddr[qdev->mac_index]);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700810 reg |= PETBI_TBI_AUTO_SENSE;
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400811 ql_mii_write_reg_ex(qdev, PETBI_TBI_CTRL, reg,
Ron Mercer3efedf22007-03-26 12:43:52 -0700812 PHYAddr[qdev->mac_index]);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700813
814 ql_mii_write_reg_ex(qdev, PETBI_NEG_ADVER,
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400815 PETBI_NEG_PAUSE | PETBI_NEG_DUPLEX,
Ron Mercer3efedf22007-03-26 12:43:52 -0700816 PHYAddr[qdev->mac_index]);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700817
818 ql_mii_write_reg_ex(qdev, PETBI_CONTROL_REG,
819 PETBI_CTRL_AUTO_NEG | PETBI_CTRL_RESTART_NEG |
820 PETBI_CTRL_FULL_DUPLEX | PETBI_CTRL_SPEED_1000,
Ron Mercer3efedf22007-03-26 12:43:52 -0700821 PHYAddr[qdev->mac_index]);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700822}
823
824static void ql_petbi_init(struct ql3_adapter *qdev)
825{
826 ql_petbi_reset(qdev);
827 ql_petbi_start_neg(qdev);
828}
829
Ron Mercer3efedf22007-03-26 12:43:52 -0700830static void ql_petbi_init_ex(struct ql3_adapter *qdev)
Ron Mercer5a4faa872006-07-25 00:40:21 -0700831{
Ron Mercer3efedf22007-03-26 12:43:52 -0700832 ql_petbi_reset_ex(qdev);
833 ql_petbi_start_neg_ex(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -0700834}
835
836static int ql_is_petbi_neg_pause(struct ql3_adapter *qdev)
837{
838 u16 reg;
839
840 if (ql_mii_read_reg(qdev, PETBI_NEG_PARTNER, &reg) < 0)
841 return 0;
842
843 return (reg & PETBI_NEG_PAUSE_MASK) == PETBI_NEG_PAUSE;
844}
845
Ron Mercer3efedf22007-03-26 12:43:52 -0700846static void phyAgereSpecificInit(struct ql3_adapter *qdev, u32 miiAddr)
847{
Joe Percheseddc5fb2010-07-22 12:33:31 +0000848 netdev_info(qdev->ndev, "enabling Agere specific PHY\n");
Ron Mercer3efedf22007-03-26 12:43:52 -0700849 /* power down device bit 11 = 1 */
850 ql_mii_write_reg_ex(qdev, 0x00, 0x1940, miiAddr);
851 /* enable diagnostic mode bit 2 = 1 */
852 ql_mii_write_reg_ex(qdev, 0x12, 0x840e, miiAddr);
853 /* 1000MB amplitude adjust (see Agere errata) */
854 ql_mii_write_reg_ex(qdev, 0x10, 0x8805, miiAddr);
855 /* 1000MB amplitude adjust (see Agere errata) */
856 ql_mii_write_reg_ex(qdev, 0x11, 0xf03e, miiAddr);
857 /* 100MB amplitude adjust (see Agere errata) */
858 ql_mii_write_reg_ex(qdev, 0x10, 0x8806, miiAddr);
859 /* 100MB amplitude adjust (see Agere errata) */
860 ql_mii_write_reg_ex(qdev, 0x11, 0x003e, miiAddr);
861 /* 10MB amplitude adjust (see Agere errata) */
862 ql_mii_write_reg_ex(qdev, 0x10, 0x8807, miiAddr);
863 /* 10MB amplitude adjust (see Agere errata) */
864 ql_mii_write_reg_ex(qdev, 0x11, 0x1f00, miiAddr);
865 /* point to hidden reg 0x2806 */
866 ql_mii_write_reg_ex(qdev, 0x10, 0x2806, miiAddr);
867 /* Write new PHYAD w/bit 5 set */
Joe Perchesd7f61772010-07-22 15:36:17 +0000868 ql_mii_write_reg_ex(qdev, 0x11,
869 0x0020 | (PHYAddr[qdev->mac_index] >> 8), miiAddr);
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400870 /*
Ron Mercer3efedf22007-03-26 12:43:52 -0700871 * Disable diagnostic mode bit 2 = 0
872 * Power up device bit 11 = 0
873 * Link up (on) and activity (blink)
874 */
875 ql_mii_write_reg(qdev, 0x12, 0x840a);
876 ql_mii_write_reg(qdev, 0x00, 0x1140);
877 ql_mii_write_reg(qdev, 0x1c, 0xfaf0);
878}
879
Joe Perchesd7f61772010-07-22 15:36:17 +0000880static enum PHY_DEVICE_TYPE getPhyType(struct ql3_adapter *qdev,
881 u16 phyIdReg0, u16 phyIdReg1)
Ron Mercer3efedf22007-03-26 12:43:52 -0700882{
Joe Perchesd7f61772010-07-22 15:36:17 +0000883 enum PHY_DEVICE_TYPE result = PHY_TYPE_UNKNOWN;
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400884 u32 oui;
Ron Mercer3efedf22007-03-26 12:43:52 -0700885 u16 model;
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400886 int i;
Ron Mercer3efedf22007-03-26 12:43:52 -0700887
Joe Perchesd7f61772010-07-22 15:36:17 +0000888 if (phyIdReg0 == 0xffff)
Ron Mercer3efedf22007-03-26 12:43:52 -0700889 return result;
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400890
Joe Perchesd7f61772010-07-22 15:36:17 +0000891 if (phyIdReg1 == 0xffff)
Ron Mercer3efedf22007-03-26 12:43:52 -0700892 return result;
Ron Mercer3efedf22007-03-26 12:43:52 -0700893
894 /* oui is split between two registers */
895 oui = (phyIdReg0 << 6) | ((phyIdReg1 & PHY_OUI_1_MASK) >> 10);
896
897 model = (phyIdReg1 & PHY_MODEL_MASK) >> 4;
898
899 /* Scan table for this PHY */
Joe Perchesd7f61772010-07-22 15:36:17 +0000900 for (i = 0; i < MAX_PHY_DEV_TYPES; i++) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000901 if ((oui == PHY_DEVICES[i].phyIdOUI) &&
902 (model == PHY_DEVICES[i].phyIdModel)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000903 netdev_info(qdev->ndev, "Phy: %s\n",
904 PHY_DEVICES[i].name);
Joe Perchesd7f61772010-07-22 15:36:17 +0000905 result = PHY_DEVICES[i].phyDevice;
906 break;
Ron Mercer3efedf22007-03-26 12:43:52 -0700907 }
908 }
909
910 return result;
911}
912
Ron Mercer5a4faa872006-07-25 00:40:21 -0700913static int ql_phy_get_speed(struct ql3_adapter *qdev)
914{
915 u16 reg;
916
Joe Perchesd7f61772010-07-22 15:36:17 +0000917 switch (qdev->phyType) {
918 case PHY_AGERE_ET1011C: {
Ron Mercer3efedf22007-03-26 12:43:52 -0700919 if (ql_mii_read_reg(qdev, 0x1A, &reg) < 0)
920 return 0;
921
922 reg = (reg >> 8) & 3;
923 break;
924 }
925 default:
Joe Perchesd7f61772010-07-22 15:36:17 +0000926 if (ql_mii_read_reg(qdev, AUX_CONTROL_STATUS, &reg) < 0)
927 return 0;
Ron Mercer5a4faa872006-07-25 00:40:21 -0700928
Joe Perchesd7f61772010-07-22 15:36:17 +0000929 reg = (((reg & 0x18) >> 3) & 3);
Ron Mercer3efedf22007-03-26 12:43:52 -0700930 }
Ron Mercer5a4faa872006-07-25 00:40:21 -0700931
Joe Perchesd7f61772010-07-22 15:36:17 +0000932 switch (reg) {
933 case 2:
Ron Mercer5a4faa872006-07-25 00:40:21 -0700934 return SPEED_1000;
Joe Perchesd7f61772010-07-22 15:36:17 +0000935 case 1:
Ron Mercer5a4faa872006-07-25 00:40:21 -0700936 return SPEED_100;
Joe Perchesd7f61772010-07-22 15:36:17 +0000937 case 0:
Ron Mercer5a4faa872006-07-25 00:40:21 -0700938 return SPEED_10;
Joe Perchesd7f61772010-07-22 15:36:17 +0000939 default:
Ron Mercer5a4faa872006-07-25 00:40:21 -0700940 return -1;
Ron Mercer3efedf22007-03-26 12:43:52 -0700941 }
Ron Mercer5a4faa872006-07-25 00:40:21 -0700942}
943
944static int ql_is_full_dup(struct ql3_adapter *qdev)
945{
946 u16 reg;
947
Joe Perchesd7f61772010-07-22 15:36:17 +0000948 switch (qdev->phyType) {
949 case PHY_AGERE_ET1011C: {
Ron Mercer3efedf22007-03-26 12:43:52 -0700950 if (ql_mii_read_reg(qdev, 0x1A, &reg))
951 return 0;
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400952
Ron Mercer3efedf22007-03-26 12:43:52 -0700953 return ((reg & 0x0080) && (reg & 0x1000)) != 0;
954 }
955 case PHY_VITESSE_VSC8211:
Joe Perchesd7f61772010-07-22 15:36:17 +0000956 default: {
Ron Mercer3efedf22007-03-26 12:43:52 -0700957 if (ql_mii_read_reg(qdev, AUX_CONTROL_STATUS, &reg) < 0)
958 return 0;
959 return (reg & PHY_AUX_DUPLEX_STAT) != 0;
960 }
961 }
Ron Mercer5a4faa872006-07-25 00:40:21 -0700962}
963
964static int ql_is_phy_neg_pause(struct ql3_adapter *qdev)
965{
966 u16 reg;
967
968 if (ql_mii_read_reg(qdev, PHY_NEG_PARTNER, &reg) < 0)
969 return 0;
970
971 return (reg & PHY_NEG_PAUSE) != 0;
972}
973
Ron Mercer3efedf22007-03-26 12:43:52 -0700974static int PHY_Setup(struct ql3_adapter *qdev)
975{
976 u16 reg1;
977 u16 reg2;
978 bool agereAddrChangeNeeded = false;
979 u32 miiAddr = 0;
980 int err;
981
982 /* Determine the PHY we are using by reading the ID's */
983 err = ql_mii_read_reg(qdev, PHY_ID_0_REG, &reg1);
Joe Perchesd7f61772010-07-22 15:36:17 +0000984 if (err != 0) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000985 netdev_err(qdev->ndev, "Could not read from reg PHY_ID_0_REG\n");
Joe Perchesd7f61772010-07-22 15:36:17 +0000986 return err;
Ron Mercer3efedf22007-03-26 12:43:52 -0700987 }
988
989 err = ql_mii_read_reg(qdev, PHY_ID_1_REG, &reg2);
Joe Perchesd7f61772010-07-22 15:36:17 +0000990 if (err != 0) {
Joe Percheseddc5fb2010-07-22 12:33:31 +0000991 netdev_err(qdev->ndev, "Could not read from reg PHY_ID_1_REG\n");
Joe Perchesd7f61772010-07-22 15:36:17 +0000992 return err;
Ron Mercer3efedf22007-03-26 12:43:52 -0700993 }
994
995 /* Check if we have a Agere PHY */
996 if ((reg1 == 0xffff) || (reg2 == 0xffff)) {
997
Jeff Garzik9ddf7772007-10-03 13:52:23 -0400998 /* Determine which MII address we should be using
Ron Mercer3efedf22007-03-26 12:43:52 -0700999 determined by the index of the card */
Joe Perchesd7f61772010-07-22 15:36:17 +00001000 if (qdev->mac_index == 0)
Ron Mercer3efedf22007-03-26 12:43:52 -07001001 miiAddr = MII_AGERE_ADDR_1;
Joe Perchesd7f61772010-07-22 15:36:17 +00001002 else
Ron Mercer3efedf22007-03-26 12:43:52 -07001003 miiAddr = MII_AGERE_ADDR_2;
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001004
Joe Perchesd7f61772010-07-22 15:36:17 +00001005 err = ql_mii_read_reg_ex(qdev, PHY_ID_0_REG, &reg1, miiAddr);
1006 if (err != 0) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001007 netdev_err(qdev->ndev,
1008 "Could not read from reg PHY_ID_0_REG after Agere detected\n");
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001009 return err;
Ron Mercer3efedf22007-03-26 12:43:52 -07001010 }
1011
1012 err = ql_mii_read_reg_ex(qdev, PHY_ID_1_REG, &reg2, miiAddr);
Joe Perchesd7f61772010-07-22 15:36:17 +00001013 if (err != 0) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001014 netdev_err(qdev->ndev, "Could not read from reg PHY_ID_1_REG after Agere detected\n");
Joe Perchesd7f61772010-07-22 15:36:17 +00001015 return err;
Ron Mercer3efedf22007-03-26 12:43:52 -07001016 }
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001017
Ron Mercer3efedf22007-03-26 12:43:52 -07001018 /* We need to remember to initialize the Agere PHY */
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001019 agereAddrChangeNeeded = true;
Ron Mercer3efedf22007-03-26 12:43:52 -07001020 }
1021
1022 /* Determine the particular PHY we have on board to apply
1023 PHY specific initializations */
1024 qdev->phyType = getPhyType(qdev, reg1, reg2);
1025
1026 if ((qdev->phyType == PHY_AGERE_ET1011C) && agereAddrChangeNeeded) {
1027 /* need this here so address gets changed */
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001028 phyAgereSpecificInit(qdev, miiAddr);
Ron Mercer3efedf22007-03-26 12:43:52 -07001029 } else if (qdev->phyType == PHY_TYPE_UNKNOWN) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001030 netdev_err(qdev->ndev, "PHY is unknown\n");
Ron Mercer3efedf22007-03-26 12:43:52 -07001031 return -EIO;
1032 }
1033
1034 return 0;
1035}
1036
Ron Mercer5a4faa872006-07-25 00:40:21 -07001037/*
1038 * Caller holds hw_lock.
1039 */
1040static void ql_mac_enable(struct ql3_adapter *qdev, u32 enable)
1041{
1042 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00001043 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001044 u32 value;
1045
1046 if (enable)
1047 value = (MAC_CONFIG_REG_PE | (MAC_CONFIG_REG_PE << 16));
1048 else
1049 value = (MAC_CONFIG_REG_PE << 16);
1050
1051 if (qdev->mac_index)
1052 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1053 else
1054 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1055}
1056
1057/*
1058 * Caller holds hw_lock.
1059 */
1060static void ql_mac_cfg_soft_reset(struct ql3_adapter *qdev, u32 enable)
1061{
1062 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00001063 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001064 u32 value;
1065
1066 if (enable)
1067 value = (MAC_CONFIG_REG_SR | (MAC_CONFIG_REG_SR << 16));
1068 else
1069 value = (MAC_CONFIG_REG_SR << 16);
1070
1071 if (qdev->mac_index)
1072 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1073 else
1074 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1075}
1076
1077/*
1078 * Caller holds hw_lock.
1079 */
1080static void ql_mac_cfg_gig(struct ql3_adapter *qdev, u32 enable)
1081{
1082 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00001083 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001084 u32 value;
1085
1086 if (enable)
1087 value = (MAC_CONFIG_REG_GM | (MAC_CONFIG_REG_GM << 16));
1088 else
1089 value = (MAC_CONFIG_REG_GM << 16);
1090
1091 if (qdev->mac_index)
1092 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1093 else
1094 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1095}
1096
1097/*
1098 * Caller holds hw_lock.
1099 */
1100static void ql_mac_cfg_full_dup(struct ql3_adapter *qdev, u32 enable)
1101{
1102 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00001103 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001104 u32 value;
1105
1106 if (enable)
1107 value = (MAC_CONFIG_REG_FD | (MAC_CONFIG_REG_FD << 16));
1108 else
1109 value = (MAC_CONFIG_REG_FD << 16);
1110
1111 if (qdev->mac_index)
1112 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1113 else
1114 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1115}
1116
1117/*
1118 * Caller holds hw_lock.
1119 */
1120static void ql_mac_cfg_pause(struct ql3_adapter *qdev, u32 enable)
1121{
1122 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00001123 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001124 u32 value;
1125
1126 if (enable)
1127 value =
1128 ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) |
1129 ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) << 16));
1130 else
1131 value = ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) << 16);
1132
1133 if (qdev->mac_index)
1134 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1135 else
1136 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1137}
1138
1139/*
1140 * Caller holds hw_lock.
1141 */
1142static int ql_is_fiber(struct ql3_adapter *qdev)
1143{
1144 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00001145 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001146 u32 bitToCheck = 0;
1147 u32 temp;
1148
1149 switch (qdev->mac_index) {
1150 case 0:
1151 bitToCheck = PORT_STATUS_SM0;
1152 break;
1153 case 1:
1154 bitToCheck = PORT_STATUS_SM1;
1155 break;
1156 }
1157
1158 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1159 return (temp & bitToCheck) != 0;
1160}
1161
1162static int ql_is_auto_cfg(struct ql3_adapter *qdev)
1163{
1164 u16 reg;
1165 ql_mii_read_reg(qdev, 0x00, &reg);
1166 return (reg & 0x1000) != 0;
1167}
1168
1169/*
1170 * Caller holds hw_lock.
1171 */
1172static int ql_is_auto_neg_complete(struct ql3_adapter *qdev)
1173{
1174 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00001175 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001176 u32 bitToCheck = 0;
1177 u32 temp;
1178
1179 switch (qdev->mac_index) {
1180 case 0:
1181 bitToCheck = PORT_STATUS_AC0;
1182 break;
1183 case 1:
1184 bitToCheck = PORT_STATUS_AC1;
1185 break;
1186 }
1187
1188 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1189 if (temp & bitToCheck) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001190 netif_info(qdev, link, qdev->ndev, "Auto-Negotiate complete\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07001191 return 1;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001192 }
Joe Percheseddc5fb2010-07-22 12:33:31 +00001193 netif_info(qdev, link, qdev->ndev, "Auto-Negotiate incomplete\n");
1194 return 0;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001195}
1196
1197/*
1198 * ql_is_neg_pause() returns 1 if pause was negotiated to be on
1199 */
1200static int ql_is_neg_pause(struct ql3_adapter *qdev)
1201{
1202 if (ql_is_fiber(qdev))
1203 return ql_is_petbi_neg_pause(qdev);
1204 else
1205 return ql_is_phy_neg_pause(qdev);
1206}
1207
1208static int ql_auto_neg_error(struct ql3_adapter *qdev)
1209{
1210 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00001211 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001212 u32 bitToCheck = 0;
1213 u32 temp;
1214
1215 switch (qdev->mac_index) {
1216 case 0:
1217 bitToCheck = PORT_STATUS_AE0;
1218 break;
1219 case 1:
1220 bitToCheck = PORT_STATUS_AE1;
1221 break;
1222 }
1223 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1224 return (temp & bitToCheck) != 0;
1225}
1226
1227static u32 ql_get_link_speed(struct ql3_adapter *qdev)
1228{
1229 if (ql_is_fiber(qdev))
1230 return SPEED_1000;
1231 else
1232 return ql_phy_get_speed(qdev);
1233}
1234
1235static int ql_is_link_full_dup(struct ql3_adapter *qdev)
1236{
1237 if (ql_is_fiber(qdev))
1238 return 1;
1239 else
1240 return ql_is_full_dup(qdev);
1241}
1242
1243/*
1244 * Caller holds hw_lock.
1245 */
1246static int ql_link_down_detect(struct ql3_adapter *qdev)
1247{
1248 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00001249 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001250 u32 bitToCheck = 0;
1251 u32 temp;
1252
1253 switch (qdev->mac_index) {
1254 case 0:
1255 bitToCheck = ISP_CONTROL_LINK_DN_0;
1256 break;
1257 case 1:
1258 bitToCheck = ISP_CONTROL_LINK_DN_1;
1259 break;
1260 }
1261
1262 temp =
1263 ql_read_common_reg(qdev, &port_regs->CommonRegs.ispControlStatus);
1264 return (temp & bitToCheck) != 0;
1265}
1266
1267/*
1268 * Caller holds hw_lock.
1269 */
1270static int ql_link_down_detect_clear(struct ql3_adapter *qdev)
1271{
1272 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00001273 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001274
1275 switch (qdev->mac_index) {
1276 case 0:
1277 ql_write_common_reg(qdev,
1278 &port_regs->CommonRegs.ispControlStatus,
1279 (ISP_CONTROL_LINK_DN_0) |
1280 (ISP_CONTROL_LINK_DN_0 << 16));
1281 break;
1282
1283 case 1:
1284 ql_write_common_reg(qdev,
1285 &port_regs->CommonRegs.ispControlStatus,
1286 (ISP_CONTROL_LINK_DN_1) |
1287 (ISP_CONTROL_LINK_DN_1 << 16));
1288 break;
1289
1290 default:
1291 return 1;
1292 }
1293
1294 return 0;
1295}
1296
1297/*
1298 * Caller holds hw_lock.
1299 */
Ron Mercer3efedf22007-03-26 12:43:52 -07001300static int ql_this_adapter_controls_port(struct ql3_adapter *qdev)
Ron Mercer5a4faa872006-07-25 00:40:21 -07001301{
1302 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00001303 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001304 u32 bitToCheck = 0;
1305 u32 temp;
1306
Ron Mercer3efedf22007-03-26 12:43:52 -07001307 switch (qdev->mac_index) {
Ron Mercer5a4faa872006-07-25 00:40:21 -07001308 case 0:
1309 bitToCheck = PORT_STATUS_F1_ENABLED;
1310 break;
1311 case 1:
1312 bitToCheck = PORT_STATUS_F3_ENABLED;
1313 break;
1314 default:
1315 break;
1316 }
1317
1318 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1319 if (temp & bitToCheck) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001320 netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
1321 "not link master\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07001322 return 0;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001323 }
Joe Percheseddc5fb2010-07-22 12:33:31 +00001324
1325 netif_printk(qdev, link, KERN_DEBUG, qdev->ndev, "link master\n");
1326 return 1;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001327}
1328
Ron Mercer3efedf22007-03-26 12:43:52 -07001329static void ql_phy_reset_ex(struct ql3_adapter *qdev)
Ron Mercer5a4faa872006-07-25 00:40:21 -07001330{
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001331 ql_mii_write_reg_ex(qdev, CONTROL_REG, PHY_CTRL_SOFT_RESET,
Ron Mercer3efedf22007-03-26 12:43:52 -07001332 PHYAddr[qdev->mac_index]);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001333}
1334
Ron Mercer3efedf22007-03-26 12:43:52 -07001335static void ql_phy_start_neg_ex(struct ql3_adapter *qdev)
Ron Mercer5a4faa872006-07-25 00:40:21 -07001336{
1337 u16 reg;
Ron Mercer3efedf22007-03-26 12:43:52 -07001338 u16 portConfiguration;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001339
Joe Perchesd7f61772010-07-22 15:36:17 +00001340 if (qdev->phyType == PHY_AGERE_ET1011C)
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001341 ql_mii_write_reg(qdev, 0x13, 0x0000);
Joe Perchesd7f61772010-07-22 15:36:17 +00001342 /* turn off external loopback */
Ron Mercer5a4faa872006-07-25 00:40:21 -07001343
Joe Perchesd7f61772010-07-22 15:36:17 +00001344 if (qdev->mac_index == 0)
1345 portConfiguration =
1346 qdev->nvram_data.macCfg_port0.portConfiguration;
Ron Mercer3efedf22007-03-26 12:43:52 -07001347 else
Joe Perchesd7f61772010-07-22 15:36:17 +00001348 portConfiguration =
1349 qdev->nvram_data.macCfg_port1.portConfiguration;
Ron Mercer3efedf22007-03-26 12:43:52 -07001350
1351 /* Some HBA's in the field are set to 0 and they need to
1352 be reinterpreted with a default value */
Joe Perchesd7f61772010-07-22 15:36:17 +00001353 if (portConfiguration == 0)
Ron Mercer3efedf22007-03-26 12:43:52 -07001354 portConfiguration = PORT_CONFIG_DEFAULT;
1355
1356 /* Set the 1000 advertisements */
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001357 ql_mii_read_reg_ex(qdev, PHY_GIG_CONTROL, &reg,
Ron Mercer3efedf22007-03-26 12:43:52 -07001358 PHYAddr[qdev->mac_index]);
1359 reg &= ~PHY_GIG_ALL_PARAMS;
1360
Joe Perchesd7f61772010-07-22 15:36:17 +00001361 if (portConfiguration & PORT_CONFIG_1000MB_SPEED) {
1362 if (portConfiguration & PORT_CONFIG_FULL_DUPLEX_ENABLED)
Ron Mercerad4c9a02007-11-07 13:59:07 -08001363 reg |= PHY_GIG_ADV_1000F;
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -04001364 else
Ron Mercerad4c9a02007-11-07 13:59:07 -08001365 reg |= PHY_GIG_ADV_1000H;
Ron Mercer3efedf22007-03-26 12:43:52 -07001366 }
1367
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001368 ql_mii_write_reg_ex(qdev, PHY_GIG_CONTROL, reg,
Ron Mercer3efedf22007-03-26 12:43:52 -07001369 PHYAddr[qdev->mac_index]);
1370
1371 /* Set the 10/100 & pause negotiation advertisements */
1372 ql_mii_read_reg_ex(qdev, PHY_NEG_ADVER, &reg,
1373 PHYAddr[qdev->mac_index]);
1374 reg &= ~PHY_NEG_ALL_PARAMS;
1375
Joe Perchesd7f61772010-07-22 15:36:17 +00001376 if (portConfiguration & PORT_CONFIG_SYM_PAUSE_ENABLED)
Ron Mercer3efedf22007-03-26 12:43:52 -07001377 reg |= PHY_NEG_ASY_PAUSE | PHY_NEG_SYM_PAUSE;
1378
Joe Perchesd7f61772010-07-22 15:36:17 +00001379 if (portConfiguration & PORT_CONFIG_FULL_DUPLEX_ENABLED) {
1380 if (portConfiguration & PORT_CONFIG_100MB_SPEED)
Ron Mercer3efedf22007-03-26 12:43:52 -07001381 reg |= PHY_NEG_ADV_100F;
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001382
Joe Perchesd7f61772010-07-22 15:36:17 +00001383 if (portConfiguration & PORT_CONFIG_10MB_SPEED)
Ron Mercer3efedf22007-03-26 12:43:52 -07001384 reg |= PHY_NEG_ADV_10F;
1385 }
1386
Joe Perchesd7f61772010-07-22 15:36:17 +00001387 if (portConfiguration & PORT_CONFIG_HALF_DUPLEX_ENABLED) {
1388 if (portConfiguration & PORT_CONFIG_100MB_SPEED)
Ron Mercer3efedf22007-03-26 12:43:52 -07001389 reg |= PHY_NEG_ADV_100H;
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001390
Joe Perchesd7f61772010-07-22 15:36:17 +00001391 if (portConfiguration & PORT_CONFIG_10MB_SPEED)
Ron Mercer3efedf22007-03-26 12:43:52 -07001392 reg |= PHY_NEG_ADV_10H;
1393 }
1394
Joe Perchesd7f61772010-07-22 15:36:17 +00001395 if (portConfiguration & PORT_CONFIG_1000MB_SPEED)
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001396 reg |= 1;
Ron Mercer3efedf22007-03-26 12:43:52 -07001397
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001398 ql_mii_write_reg_ex(qdev, PHY_NEG_ADVER, reg,
Ron Mercer3efedf22007-03-26 12:43:52 -07001399 PHYAddr[qdev->mac_index]);
1400
1401 ql_mii_read_reg_ex(qdev, CONTROL_REG, &reg, PHYAddr[qdev->mac_index]);
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001402
1403 ql_mii_write_reg_ex(qdev, CONTROL_REG,
Ron Mercer3efedf22007-03-26 12:43:52 -07001404 reg | PHY_CTRL_RESTART_NEG | PHY_CTRL_AUTO_NEG,
1405 PHYAddr[qdev->mac_index]);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001406}
1407
Ron Mercer3efedf22007-03-26 12:43:52 -07001408static void ql_phy_init_ex(struct ql3_adapter *qdev)
Ron Mercer5a4faa872006-07-25 00:40:21 -07001409{
Ron Mercer3efedf22007-03-26 12:43:52 -07001410 ql_phy_reset_ex(qdev);
1411 PHY_Setup(qdev);
1412 ql_phy_start_neg_ex(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001413}
1414
1415/*
1416 * Caller holds hw_lock.
1417 */
1418static u32 ql_get_link_state(struct ql3_adapter *qdev)
1419{
1420 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00001421 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001422 u32 bitToCheck = 0;
1423 u32 temp, linkState;
1424
1425 switch (qdev->mac_index) {
1426 case 0:
1427 bitToCheck = PORT_STATUS_UP0;
1428 break;
1429 case 1:
1430 bitToCheck = PORT_STATUS_UP1;
1431 break;
1432 }
Joe Perchesd7f61772010-07-22 15:36:17 +00001433
Ron Mercer5a4faa872006-07-25 00:40:21 -07001434 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
Joe Perchesd7f61772010-07-22 15:36:17 +00001435 if (temp & bitToCheck)
Ron Mercer5a4faa872006-07-25 00:40:21 -07001436 linkState = LS_UP;
Joe Perchesd7f61772010-07-22 15:36:17 +00001437 else
Ron Mercer5a4faa872006-07-25 00:40:21 -07001438 linkState = LS_DOWN;
Joe Perchesd7f61772010-07-22 15:36:17 +00001439
Ron Mercer5a4faa872006-07-25 00:40:21 -07001440 return linkState;
1441}
1442
1443static int ql_port_start(struct ql3_adapter *qdev)
1444{
Joe Perchesd7f61772010-07-22 15:36:17 +00001445 if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
Ron Mercer5a4faa872006-07-25 00:40:21 -07001446 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
Ron Mercer3efedf22007-03-26 12:43:52 -07001447 2) << 7)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001448 netdev_err(qdev->ndev, "Could not get hw lock for GIO\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07001449 return -1;
Ron Mercer3efedf22007-03-26 12:43:52 -07001450 }
Ron Mercer5a4faa872006-07-25 00:40:21 -07001451
1452 if (ql_is_fiber(qdev)) {
1453 ql_petbi_init(qdev);
1454 } else {
1455 /* Copper port */
Ron Mercer3efedf22007-03-26 12:43:52 -07001456 ql_phy_init_ex(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001457 }
1458
1459 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1460 return 0;
1461}
1462
1463static int ql_finish_auto_neg(struct ql3_adapter *qdev)
1464{
1465
Joe Perchesd7f61772010-07-22 15:36:17 +00001466 if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
Ron Mercer5a4faa872006-07-25 00:40:21 -07001467 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
1468 2) << 7))
1469 return -1;
1470
1471 if (!ql_auto_neg_error(qdev)) {
Joe Perchesd7f61772010-07-22 15:36:17 +00001472 if (test_bit(QL_LINK_MASTER, &qdev->flags)) {
Ron Mercer5a4faa872006-07-25 00:40:21 -07001473 /* configure the MAC */
Joe Percheseddc5fb2010-07-22 12:33:31 +00001474 netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
1475 "Configuring link\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07001476 ql_mac_cfg_soft_reset(qdev, 1);
1477 ql_mac_cfg_gig(qdev,
1478 (ql_get_link_speed
1479 (qdev) ==
1480 SPEED_1000));
1481 ql_mac_cfg_full_dup(qdev,
1482 ql_is_link_full_dup
1483 (qdev));
1484 ql_mac_cfg_pause(qdev,
1485 ql_is_neg_pause
1486 (qdev));
1487 ql_mac_cfg_soft_reset(qdev, 0);
1488
1489 /* enable the MAC */
Joe Percheseddc5fb2010-07-22 12:33:31 +00001490 netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
1491 "Enabling mac\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07001492 ql_mac_enable(qdev, 1);
1493 }
1494
Ron Mercer5a4faa872006-07-25 00:40:21 -07001495 qdev->port_link_state = LS_UP;
1496 netif_start_queue(qdev->ndev);
1497 netif_carrier_on(qdev->ndev);
Joe Percheseddc5fb2010-07-22 12:33:31 +00001498 netif_info(qdev, link, qdev->ndev,
1499 "Link is up at %d Mbps, %s duplex\n",
1500 ql_get_link_speed(qdev),
1501 ql_is_link_full_dup(qdev) ? "full" : "half");
Ron Mercer5a4faa872006-07-25 00:40:21 -07001502
1503 } else { /* Remote error detected */
1504
Joe Perchesd7f61772010-07-22 15:36:17 +00001505 if (test_bit(QL_LINK_MASTER, &qdev->flags)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001506 netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
1507 "Remote error detected. Calling ql_port_start()\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07001508 /*
1509 * ql_port_start() is shared code and needs
1510 * to lock the PHY on it's own.
1511 */
1512 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
Joe Perchesd7f61772010-07-22 15:36:17 +00001513 if (ql_port_start(qdev)) /* Restart port */
Ron Mercer5a4faa872006-07-25 00:40:21 -07001514 return -1;
Joe Perchesd7f61772010-07-22 15:36:17 +00001515 return 0;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001516 }
1517 }
1518 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1519 return 0;
1520}
1521
Ron Mercer3e23b7d2007-11-07 13:59:06 -08001522static void ql_link_state_machine_work(struct work_struct *work)
Ron Mercer5a4faa872006-07-25 00:40:21 -07001523{
Ron Mercer3e23b7d2007-11-07 13:59:06 -08001524 struct ql3_adapter *qdev =
1525 container_of(work, struct ql3_adapter, link_state_work.work);
1526
Ron Mercer5a4faa872006-07-25 00:40:21 -07001527 u32 curr_link_state;
1528 unsigned long hw_flags;
1529
1530 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
1531
1532 curr_link_state = ql_get_link_state(qdev);
1533
Joe Perchesd7f61772010-07-22 15:36:17 +00001534 if (test_bit(QL_RESET_ACTIVE, &qdev->flags)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001535 netif_info(qdev, link, qdev->ndev,
1536 "Reset in progress, skip processing link state\n");
Benjamin Li04f10772007-02-26 11:06:35 -08001537
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001538 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercer3e23b7d2007-11-07 13:59:06 -08001539
1540 /* Restart timer on 2 second interval. */
Joe Percheseddc5fb2010-07-22 12:33:31 +00001541 mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);
Ron Mercer3e23b7d2007-11-07 13:59:06 -08001542
Ron Mercer5a4faa872006-07-25 00:40:21 -07001543 return;
1544 }
1545
1546 switch (qdev->port_link_state) {
1547 default:
Joe Perchesd7f61772010-07-22 15:36:17 +00001548 if (test_bit(QL_LINK_MASTER, &qdev->flags))
Ron Mercer5a4faa872006-07-25 00:40:21 -07001549 ql_port_start(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001550 qdev->port_link_state = LS_DOWN;
1551 /* Fall Through */
1552
1553 case LS_DOWN:
Ron Mercer5a4faa872006-07-25 00:40:21 -07001554 if (curr_link_state == LS_UP) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001555 netif_info(qdev, link, qdev->ndev, "Link is up\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07001556 if (ql_is_auto_neg_complete(qdev))
1557 ql_finish_auto_neg(qdev);
1558
1559 if (qdev->port_link_state == LS_UP)
1560 ql_link_down_detect_clear(qdev);
1561
Ron Mercer0f807042008-11-11 07:54:54 +00001562 qdev->port_link_state = LS_UP;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001563 }
1564 break;
1565
1566 case LS_UP:
1567 /*
1568 * See if the link is currently down or went down and came
1569 * back up
1570 */
Ron Mercer0f807042008-11-11 07:54:54 +00001571 if (curr_link_state == LS_DOWN) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001572 netif_info(qdev, link, qdev->ndev, "Link is down\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07001573 qdev->port_link_state = LS_DOWN;
1574 }
Ron Mercer0f807042008-11-11 07:54:54 +00001575 if (ql_link_down_detect(qdev))
1576 qdev->port_link_state = LS_DOWN;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001577 break;
1578 }
1579 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercer3e23b7d2007-11-07 13:59:06 -08001580
1581 /* Restart timer on 2 second interval. */
1582 mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001583}
1584
1585/*
1586 * Caller must take hw_lock and QL_PHY_GIO_SEM.
1587 */
1588static void ql_get_phy_owner(struct ql3_adapter *qdev)
1589{
Ron Mercer3efedf22007-03-26 12:43:52 -07001590 if (ql_this_adapter_controls_port(qdev))
Joe Perchesd7f61772010-07-22 15:36:17 +00001591 set_bit(QL_LINK_MASTER, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001592 else
Joe Perchesd7f61772010-07-22 15:36:17 +00001593 clear_bit(QL_LINK_MASTER, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001594}
1595
1596/*
1597 * Caller must take hw_lock and QL_PHY_GIO_SEM.
1598 */
1599static void ql_init_scan_mode(struct ql3_adapter *qdev)
1600{
1601 ql_mii_enable_scan_mode(qdev);
1602
Joe Perchesd7f61772010-07-22 15:36:17 +00001603 if (test_bit(QL_LINK_OPTICAL, &qdev->flags)) {
Ron Mercer3efedf22007-03-26 12:43:52 -07001604 if (ql_this_adapter_controls_port(qdev))
1605 ql_petbi_init_ex(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001606 } else {
Ron Mercer3efedf22007-03-26 12:43:52 -07001607 if (ql_this_adapter_controls_port(qdev))
1608 ql_phy_init_ex(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001609 }
1610}
1611
1612/*
Joe Perchesd7f61772010-07-22 15:36:17 +00001613 * MII_Setup needs to be called before taking the PHY out of reset
1614 * so that the management interface clock speed can be set properly.
1615 * It would be better if we had a way to disable MDC until after the
1616 * PHY is out of reset, but we don't have that capability.
Ron Mercer5a4faa872006-07-25 00:40:21 -07001617 */
1618static int ql_mii_setup(struct ql3_adapter *qdev)
1619{
1620 u32 reg;
1621 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00001622 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001623
Joe Perchesd7f61772010-07-22 15:36:17 +00001624 if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
Ron Mercer5a4faa872006-07-25 00:40:21 -07001625 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
1626 2) << 7))
1627 return -1;
1628
Ron Mercerbd36b0a2007-01-03 16:26:08 -08001629 if (qdev->device_id == QL3032_DEVICE_ID)
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001630 ql_write_page0_reg(qdev,
Ron Mercerbd36b0a2007-01-03 16:26:08 -08001631 &port_regs->macMIIMgmtControlReg, 0x0f00000);
1632
Ron Mercer5a4faa872006-07-25 00:40:21 -07001633 /* Divide 125MHz clock by 28 to meet PHY timing requirements */
1634 reg = MAC_MII_CONTROL_CLK_SEL_DIV28;
1635
1636 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
1637 reg | ((MAC_MII_CONTROL_CLK_SEL_MASK) << 16));
1638
1639 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1640 return 0;
1641}
1642
Joe Perchesd7f61772010-07-22 15:36:17 +00001643#define SUPPORTED_OPTICAL_MODES (SUPPORTED_1000baseT_Full | \
1644 SUPPORTED_FIBRE | \
1645 SUPPORTED_Autoneg)
1646#define SUPPORTED_TP_MODES (SUPPORTED_10baseT_Half | \
1647 SUPPORTED_10baseT_Full | \
1648 SUPPORTED_100baseT_Half | \
1649 SUPPORTED_100baseT_Full | \
1650 SUPPORTED_1000baseT_Half | \
1651 SUPPORTED_1000baseT_Full | \
1652 SUPPORTED_Autoneg | \
Dan Carpenter8a4cadc2011-08-06 04:26:41 +00001653 SUPPORTED_TP) \
Joe Perchesd7f61772010-07-22 15:36:17 +00001654
Ron Mercer5a4faa872006-07-25 00:40:21 -07001655static u32 ql_supported_modes(struct ql3_adapter *qdev)
1656{
Joe Perchesd7f61772010-07-22 15:36:17 +00001657 if (test_bit(QL_LINK_OPTICAL, &qdev->flags))
1658 return SUPPORTED_OPTICAL_MODES;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001659
Joe Perchesd7f61772010-07-22 15:36:17 +00001660 return SUPPORTED_TP_MODES;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001661}
1662
1663static int ql_get_auto_cfg_status(struct ql3_adapter *qdev)
1664{
1665 int status;
1666 unsigned long hw_flags;
1667 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
Joe Perchesd7f61772010-07-22 15:36:17 +00001668 if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
1669 (QL_RESOURCE_BITS_BASE_CODE |
1670 (qdev->mac_index) * 2) << 7)) {
Benjamin Li04f10772007-02-26 11:06:35 -08001671 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001672 return 0;
Benjamin Li04f10772007-02-26 11:06:35 -08001673 }
Ron Mercer5a4faa872006-07-25 00:40:21 -07001674 status = ql_is_auto_cfg(qdev);
1675 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1676 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1677 return status;
1678}
1679
1680static u32 ql_get_speed(struct ql3_adapter *qdev)
1681{
1682 u32 status;
1683 unsigned long hw_flags;
1684 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
Joe Perchesd7f61772010-07-22 15:36:17 +00001685 if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
1686 (QL_RESOURCE_BITS_BASE_CODE |
1687 (qdev->mac_index) * 2) << 7)) {
Benjamin Li04f10772007-02-26 11:06:35 -08001688 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001689 return 0;
Benjamin Li04f10772007-02-26 11:06:35 -08001690 }
Ron Mercer5a4faa872006-07-25 00:40:21 -07001691 status = ql_get_link_speed(qdev);
1692 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1693 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1694 return status;
1695}
1696
1697static int ql_get_full_dup(struct ql3_adapter *qdev)
1698{
1699 int status;
1700 unsigned long hw_flags;
1701 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
Joe Perchesd7f61772010-07-22 15:36:17 +00001702 if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
1703 (QL_RESOURCE_BITS_BASE_CODE |
1704 (qdev->mac_index) * 2) << 7)) {
Benjamin Li04f10772007-02-26 11:06:35 -08001705 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001706 return 0;
Benjamin Li04f10772007-02-26 11:06:35 -08001707 }
Ron Mercer5a4faa872006-07-25 00:40:21 -07001708 status = ql_is_link_full_dup(qdev);
1709 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1710 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1711 return status;
1712}
1713
Ron Mercer5a4faa872006-07-25 00:40:21 -07001714static int ql_get_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1715{
1716 struct ql3_adapter *qdev = netdev_priv(ndev);
1717
1718 ecmd->transceiver = XCVR_INTERNAL;
1719 ecmd->supported = ql_supported_modes(qdev);
1720
Joe Perchesd7f61772010-07-22 15:36:17 +00001721 if (test_bit(QL_LINK_OPTICAL, &qdev->flags)) {
Ron Mercer5a4faa872006-07-25 00:40:21 -07001722 ecmd->port = PORT_FIBRE;
1723 } else {
1724 ecmd->port = PORT_TP;
1725 ecmd->phy_address = qdev->PHYAddr;
1726 }
1727 ecmd->advertising = ql_supported_modes(qdev);
1728 ecmd->autoneg = ql_get_auto_cfg_status(qdev);
David Decotigny70739492011-04-27 18:32:40 +00001729 ethtool_cmd_speed_set(ecmd, ql_get_speed(qdev));
Ron Mercer5a4faa872006-07-25 00:40:21 -07001730 ecmd->duplex = ql_get_full_dup(qdev);
1731 return 0;
1732}
1733
1734static void ql_get_drvinfo(struct net_device *ndev,
1735 struct ethtool_drvinfo *drvinfo)
1736{
1737 struct ql3_adapter *qdev = netdev_priv(ndev);
Rick Jones68aad782011-11-07 13:29:27 +00001738 strlcpy(drvinfo->driver, ql3xxx_driver_name, sizeof(drvinfo->driver));
1739 strlcpy(drvinfo->version, ql3xxx_driver_version,
1740 sizeof(drvinfo->version));
Rick Jones68aad782011-11-07 13:29:27 +00001741 strlcpy(drvinfo->bus_info, pci_name(qdev->pdev),
1742 sizeof(drvinfo->bus_info));
Ron Mercer5a4faa872006-07-25 00:40:21 -07001743 drvinfo->regdump_len = 0;
1744 drvinfo->eedump_len = 0;
1745}
1746
1747static u32 ql_get_msglevel(struct net_device *ndev)
1748{
1749 struct ql3_adapter *qdev = netdev_priv(ndev);
1750 return qdev->msg_enable;
1751}
1752
1753static void ql_set_msglevel(struct net_device *ndev, u32 value)
1754{
1755 struct ql3_adapter *qdev = netdev_priv(ndev);
1756 qdev->msg_enable = value;
1757}
1758
Ron Mercerec826382007-03-26 13:43:01 -07001759static void ql_get_pauseparam(struct net_device *ndev,
1760 struct ethtool_pauseparam *pause)
1761{
1762 struct ql3_adapter *qdev = netdev_priv(ndev);
Joe Perchesd7f61772010-07-22 15:36:17 +00001763 struct ql3xxx_port_registers __iomem *port_regs =
1764 qdev->mem_map_registers;
Ron Mercerec826382007-03-26 13:43:01 -07001765
1766 u32 reg;
Joe Perchesd7f61772010-07-22 15:36:17 +00001767 if (qdev->mac_index == 0)
Ron Mercerec826382007-03-26 13:43:01 -07001768 reg = ql_read_page0_reg(qdev, &port_regs->mac0ConfigReg);
1769 else
1770 reg = ql_read_page0_reg(qdev, &port_regs->mac1ConfigReg);
1771
1772 pause->autoneg = ql_get_auto_cfg_status(qdev);
1773 pause->rx_pause = (reg & MAC_CONFIG_REG_RF) >> 2;
1774 pause->tx_pause = (reg & MAC_CONFIG_REG_TF) >> 1;
1775}
1776
Jeff Garzik7282d492006-09-13 14:30:00 -04001777static const struct ethtool_ops ql3xxx_ethtool_ops = {
Ron Mercer5a4faa872006-07-25 00:40:21 -07001778 .get_settings = ql_get_settings,
1779 .get_drvinfo = ql_get_drvinfo,
Ron Mercer5a4faa872006-07-25 00:40:21 -07001780 .get_link = ethtool_op_get_link,
1781 .get_msglevel = ql_get_msglevel,
1782 .set_msglevel = ql_set_msglevel,
Ron Mercerec826382007-03-26 13:43:01 -07001783 .get_pauseparam = ql_get_pauseparam,
Ron Mercer5a4faa872006-07-25 00:40:21 -07001784};
1785
1786static int ql_populate_free_queue(struct ql3_adapter *qdev)
1787{
1788 struct ql_rcv_buf_cb *lrg_buf_cb = qdev->lrg_buf_free_head;
Benjamin Li0f8ab892007-02-26 11:06:40 -08001789 dma_addr_t map;
1790 int err;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001791
1792 while (lrg_buf_cb) {
1793 if (!lrg_buf_cb->skb) {
Joe Perchesd7f61772010-07-22 15:36:17 +00001794 lrg_buf_cb->skb =
1795 netdev_alloc_skb(qdev->ndev,
1796 qdev->lrg_buffer_len);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001797 if (unlikely(!lrg_buf_cb->skb)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001798 netdev_printk(KERN_DEBUG, qdev->ndev,
1799 "Failed netdev_alloc_skb()\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07001800 break;
1801 } else {
1802 /*
1803 * We save some space to copy the ethhdr from
1804 * first buffer
1805 */
1806 skb_reserve(lrg_buf_cb->skb, QL_HEADER_SPACE);
1807 map = pci_map_single(qdev->pdev,
1808 lrg_buf_cb->skb->data,
1809 qdev->lrg_buffer_len -
1810 QL_HEADER_SPACE,
1811 PCI_DMA_FROMDEVICE);
Benjamin Li0f8ab892007-02-26 11:06:40 -08001812
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001813 err = pci_dma_mapping_error(qdev->pdev, map);
Joe Perchesd7f61772010-07-22 15:36:17 +00001814 if (err) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001815 netdev_err(qdev->ndev,
1816 "PCI mapping failed with error: %d\n",
1817 err);
Benjamin Li0f8ab892007-02-26 11:06:40 -08001818 dev_kfree_skb(lrg_buf_cb->skb);
1819 lrg_buf_cb->skb = NULL;
1820 break;
1821 }
1822
1823
Ron Mercer5a4faa872006-07-25 00:40:21 -07001824 lrg_buf_cb->buf_phy_addr_low =
Joe Perchesd7f61772010-07-22 15:36:17 +00001825 cpu_to_le32(LS_64BITS(map));
Ron Mercer5a4faa872006-07-25 00:40:21 -07001826 lrg_buf_cb->buf_phy_addr_high =
Joe Perchesd7f61772010-07-22 15:36:17 +00001827 cpu_to_le32(MS_64BITS(map));
FUJITA Tomonori87196eb2010-04-12 14:32:13 +00001828 dma_unmap_addr_set(lrg_buf_cb, mapaddr, map);
1829 dma_unmap_len_set(lrg_buf_cb, maplen,
Ron Mercer5a4faa872006-07-25 00:40:21 -07001830 qdev->lrg_buffer_len -
1831 QL_HEADER_SPACE);
1832 --qdev->lrg_buf_skb_check;
1833 if (!qdev->lrg_buf_skb_check)
1834 return 1;
1835 }
1836 }
1837 lrg_buf_cb = lrg_buf_cb->next;
1838 }
1839 return 0;
1840}
1841
1842/*
1843 * Caller holds hw_lock.
1844 */
Ron Mercerf67cac02007-03-26 13:42:59 -07001845static void ql_update_small_bufq_prod_index(struct ql3_adapter *qdev)
1846{
Joe Perchesd7f61772010-07-22 15:36:17 +00001847 struct ql3xxx_port_registers __iomem *port_regs =
1848 qdev->mem_map_registers;
1849
Ron Mercerf67cac02007-03-26 13:42:59 -07001850 if (qdev->small_buf_release_cnt >= 16) {
1851 while (qdev->small_buf_release_cnt >= 16) {
1852 qdev->small_buf_q_producer_index++;
1853
1854 if (qdev->small_buf_q_producer_index ==
1855 NUM_SBUFQ_ENTRIES)
1856 qdev->small_buf_q_producer_index = 0;
1857 qdev->small_buf_release_cnt -= 8;
1858 }
1859 wmb();
1860 writel(qdev->small_buf_q_producer_index,
1861 &port_regs->CommonRegs.rxSmallQProducerIndex);
1862 }
1863}
1864
1865/*
1866 * Caller holds hw_lock.
1867 */
Ron Mercer5a4faa872006-07-25 00:40:21 -07001868static void ql_update_lrg_bufq_prod_index(struct ql3_adapter *qdev)
1869{
1870 struct bufq_addr_element *lrg_buf_q_ele;
1871 int i;
1872 struct ql_rcv_buf_cb *lrg_buf_cb;
Joe Perchesd7f61772010-07-22 15:36:17 +00001873 struct ql3xxx_port_registers __iomem *port_regs =
1874 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001875
Joe Perches8e95a202009-12-03 07:58:21 +00001876 if ((qdev->lrg_buf_free_count >= 8) &&
1877 (qdev->lrg_buf_release_cnt >= 16)) {
Ron Mercer5a4faa872006-07-25 00:40:21 -07001878
1879 if (qdev->lrg_buf_skb_check)
1880 if (!ql_populate_free_queue(qdev))
1881 return;
1882
1883 lrg_buf_q_ele = qdev->lrg_buf_next_free;
1884
Joe Perches8e95a202009-12-03 07:58:21 +00001885 while ((qdev->lrg_buf_release_cnt >= 16) &&
1886 (qdev->lrg_buf_free_count >= 8)) {
Ron Mercer5a4faa872006-07-25 00:40:21 -07001887
1888 for (i = 0; i < 8; i++) {
1889 lrg_buf_cb =
1890 ql_get_from_lrg_buf_free_list(qdev);
1891 lrg_buf_q_ele->addr_high =
1892 lrg_buf_cb->buf_phy_addr_high;
1893 lrg_buf_q_ele->addr_low =
1894 lrg_buf_cb->buf_phy_addr_low;
1895 lrg_buf_q_ele++;
1896
1897 qdev->lrg_buf_release_cnt--;
1898 }
1899
1900 qdev->lrg_buf_q_producer_index++;
1901
Joe Perchesd7f61772010-07-22 15:36:17 +00001902 if (qdev->lrg_buf_q_producer_index ==
1903 qdev->num_lbufq_entries)
Ron Mercer5a4faa872006-07-25 00:40:21 -07001904 qdev->lrg_buf_q_producer_index = 0;
1905
1906 if (qdev->lrg_buf_q_producer_index ==
Ron Mercer1357bfc2007-02-26 11:06:37 -08001907 (qdev->num_lbufq_entries - 1)) {
Ron Mercer5a4faa872006-07-25 00:40:21 -07001908 lrg_buf_q_ele = qdev->lrg_buf_q_virt_addr;
1909 }
1910 }
Ron Mercerf67cac02007-03-26 13:42:59 -07001911 wmb();
Ron Mercer5a4faa872006-07-25 00:40:21 -07001912 qdev->lrg_buf_next_free = lrg_buf_q_ele;
Ron Mercerf67cac02007-03-26 13:42:59 -07001913 writel(qdev->lrg_buf_q_producer_index,
1914 &port_regs->CommonRegs.rxLargeQProducerIndex);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001915 }
1916}
1917
1918static void ql_process_mac_tx_intr(struct ql3_adapter *qdev,
1919 struct ob_mac_iocb_rsp *mac_rsp)
1920{
1921 struct ql_tx_buf_cb *tx_cb;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08001922 int i;
Ron Mercer5a4faa872006-07-25 00:40:21 -07001923
Joe Perchesd7f61772010-07-22 15:36:17 +00001924 if (mac_rsp->flags & OB_MAC_IOCB_RSP_S) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001925 netdev_warn(qdev->ndev,
1926 "Frame too short but it was padded and sent\n");
Benjamin Lie8f4df22007-02-26 11:06:42 -08001927 }
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001928
Ron Mercer5a4faa872006-07-25 00:40:21 -07001929 tx_cb = &qdev->tx_buf[mac_rsp->transaction_id];
Benjamin Lie8f4df22007-02-26 11:06:42 -08001930
1931 /* Check the transmit response flags for any errors */
Joe Perchesd7f61772010-07-22 15:36:17 +00001932 if (mac_rsp->flags & OB_MAC_IOCB_RSP_S) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001933 netdev_err(qdev->ndev,
1934 "Frame too short to be legal, frame not sent\n");
Benjamin Lie8f4df22007-02-26 11:06:42 -08001935
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001936 qdev->ndev->stats.tx_errors++;
Benjamin Lie8f4df22007-02-26 11:06:42 -08001937 goto frame_not_sent;
1938 }
1939
Joe Perchesd7f61772010-07-22 15:36:17 +00001940 if (tx_cb->seg_count == 0) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00001941 netdev_err(qdev->ndev, "tx_cb->seg_count == 0: %d\n",
1942 mac_rsp->transaction_id);
Benjamin Lie8f4df22007-02-26 11:06:42 -08001943
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001944 qdev->ndev->stats.tx_errors++;
Benjamin Lie8f4df22007-02-26 11:06:42 -08001945 goto invalid_seg_count;
1946 }
1947
Ron Mercer5a4faa872006-07-25 00:40:21 -07001948 pci_unmap_single(qdev->pdev,
FUJITA Tomonori87196eb2010-04-12 14:32:13 +00001949 dma_unmap_addr(&tx_cb->map[0], mapaddr),
1950 dma_unmap_len(&tx_cb->map[0], maplen),
Ron Mercerbd36b0a2007-01-03 16:26:08 -08001951 PCI_DMA_TODEVICE);
1952 tx_cb->seg_count--;
1953 if (tx_cb->seg_count) {
1954 for (i = 1; i < tx_cb->seg_count; i++) {
1955 pci_unmap_page(qdev->pdev,
FUJITA Tomonori87196eb2010-04-12 14:32:13 +00001956 dma_unmap_addr(&tx_cb->map[i],
Ron Mercerbd36b0a2007-01-03 16:26:08 -08001957 mapaddr),
FUJITA Tomonori87196eb2010-04-12 14:32:13 +00001958 dma_unmap_len(&tx_cb->map[i], maplen),
Ron Mercerbd36b0a2007-01-03 16:26:08 -08001959 PCI_DMA_TODEVICE);
1960 }
1961 }
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001962 qdev->ndev->stats.tx_packets++;
1963 qdev->ndev->stats.tx_bytes += tx_cb->skb->len;
Benjamin Lie8f4df22007-02-26 11:06:42 -08001964
1965frame_not_sent:
Ron Mercerbd36b0a2007-01-03 16:26:08 -08001966 dev_kfree_skb_irq(tx_cb->skb);
Ron Mercer5a4faa872006-07-25 00:40:21 -07001967 tx_cb->skb = NULL;
Benjamin Lie8f4df22007-02-26 11:06:42 -08001968
1969invalid_seg_count:
Ron Mercer5a4faa872006-07-25 00:40:21 -07001970 atomic_inc(&qdev->tx_count);
1971}
1972
Adrian Bunk36640062007-03-05 02:49:27 +01001973static void ql_get_sbuf(struct ql3_adapter *qdev)
Ron Mercer97916332007-02-26 11:06:38 -08001974{
1975 if (++qdev->small_buf_index == NUM_SMALL_BUFFERS)
1976 qdev->small_buf_index = 0;
1977 qdev->small_buf_release_cnt++;
1978}
1979
Adrian Bunk36640062007-03-05 02:49:27 +01001980static struct ql_rcv_buf_cb *ql_get_lbuf(struct ql3_adapter *qdev)
Ron Mercer97916332007-02-26 11:06:38 -08001981{
1982 struct ql_rcv_buf_cb *lrg_buf_cb = NULL;
1983 lrg_buf_cb = &qdev->lrg_buf[qdev->lrg_buf_index];
1984 qdev->lrg_buf_release_cnt++;
1985 if (++qdev->lrg_buf_index == qdev->num_large_buffers)
1986 qdev->lrg_buf_index = 0;
Joe Perchesd7f61772010-07-22 15:36:17 +00001987 return lrg_buf_cb;
Ron Mercer97916332007-02-26 11:06:38 -08001988}
1989
Ron Mercerbd36b0a2007-01-03 16:26:08 -08001990/*
1991 * The difference between 3022 and 3032 for inbound completions:
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001992 * 3022 uses two buffers per completion. The first buffer contains
1993 * (some) header info, the second the remainder of the headers plus
1994 * the data. For this chip we reserve some space at the top of the
1995 * receive buffer so that the header info in buffer one can be
1996 * prepended to the buffer two. Buffer two is the sent up while
Ron Mercerbd36b0a2007-01-03 16:26:08 -08001997 * buffer one is returned to the hardware to be reused.
Jeff Garzik9ddf7772007-10-03 13:52:23 -04001998 * 3032 receives all of it's data and headers in one buffer for a
Ron Mercerbd36b0a2007-01-03 16:26:08 -08001999 * simpler process. 3032 also supports checksum verification as
2000 * can be seen in ql_process_macip_rx_intr().
2001 */
Ron Mercer5a4faa872006-07-25 00:40:21 -07002002static void ql_process_mac_rx_intr(struct ql3_adapter *qdev,
2003 struct ib_mac_iocb_rsp *ib_mac_rsp_ptr)
2004{
Ron Mercer5a4faa872006-07-25 00:40:21 -07002005 struct ql_rcv_buf_cb *lrg_buf_cb1 = NULL;
2006 struct ql_rcv_buf_cb *lrg_buf_cb2 = NULL;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002007 struct sk_buff *skb;
2008 u16 length = le16_to_cpu(ib_mac_rsp_ptr->length);
2009
2010 /*
2011 * Get the inbound address list (small buffer).
2012 */
Ron Mercer97916332007-02-26 11:06:38 -08002013 ql_get_sbuf(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002014
Ron Mercer97916332007-02-26 11:06:38 -08002015 if (qdev->device_id == QL3022_DEVICE_ID)
2016 lrg_buf_cb1 = ql_get_lbuf(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002017
2018 /* start of second buffer */
Ron Mercer97916332007-02-26 11:06:38 -08002019 lrg_buf_cb2 = ql_get_lbuf(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002020 skb = lrg_buf_cb2->skb;
2021
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002022 qdev->ndev->stats.rx_packets++;
2023 qdev->ndev->stats.rx_bytes += length;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002024
2025 skb_put(skb, length);
2026 pci_unmap_single(qdev->pdev,
FUJITA Tomonori87196eb2010-04-12 14:32:13 +00002027 dma_unmap_addr(lrg_buf_cb2, mapaddr),
2028 dma_unmap_len(lrg_buf_cb2, maplen),
Ron Mercer5a4faa872006-07-25 00:40:21 -07002029 PCI_DMA_FROMDEVICE);
2030 prefetch(skb->data);
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002031 skb_checksum_none_assert(skb);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002032 skb->protocol = eth_type_trans(skb, qdev->ndev);
2033
2034 netif_receive_skb(skb);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002035 lrg_buf_cb2->skb = NULL;
2036
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002037 if (qdev->device_id == QL3022_DEVICE_ID)
2038 ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb1);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002039 ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb2);
2040}
2041
2042static void ql_process_macip_rx_intr(struct ql3_adapter *qdev,
2043 struct ib_ip_iocb_rsp *ib_ip_rsp_ptr)
2044{
Ron Mercer5a4faa872006-07-25 00:40:21 -07002045 struct ql_rcv_buf_cb *lrg_buf_cb1 = NULL;
2046 struct ql_rcv_buf_cb *lrg_buf_cb2 = NULL;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002047 struct sk_buff *skb1 = NULL, *skb2;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002048 struct net_device *ndev = qdev->ndev;
2049 u16 length = le16_to_cpu(ib_ip_rsp_ptr->length);
2050 u16 size = 0;
2051
2052 /*
2053 * Get the inbound address list (small buffer).
2054 */
2055
Ron Mercer97916332007-02-26 11:06:38 -08002056 ql_get_sbuf(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002057
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002058 if (qdev->device_id == QL3022_DEVICE_ID) {
2059 /* start of first buffer on 3022 */
Ron Mercer97916332007-02-26 11:06:38 -08002060 lrg_buf_cb1 = ql_get_lbuf(qdev);
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002061 skb1 = lrg_buf_cb1->skb;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002062 size = ETH_HLEN;
2063 if (*((u16 *) skb1->data) != 0xFFFF)
2064 size += VLAN_ETH_HLEN - ETH_HLEN;
2065 }
Ron Mercer5a4faa872006-07-25 00:40:21 -07002066
2067 /* start of second buffer */
Ron Mercer97916332007-02-26 11:06:38 -08002068 lrg_buf_cb2 = ql_get_lbuf(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002069 skb2 = lrg_buf_cb2->skb;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002070
Ron Mercer5a4faa872006-07-25 00:40:21 -07002071 skb_put(skb2, length); /* Just the second buffer length here. */
2072 pci_unmap_single(qdev->pdev,
FUJITA Tomonori87196eb2010-04-12 14:32:13 +00002073 dma_unmap_addr(lrg_buf_cb2, mapaddr),
2074 dma_unmap_len(lrg_buf_cb2, maplen),
Ron Mercer5a4faa872006-07-25 00:40:21 -07002075 PCI_DMA_FROMDEVICE);
2076 prefetch(skb2->data);
2077
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002078 skb_checksum_none_assert(skb2);
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002079 if (qdev->device_id == QL3022_DEVICE_ID) {
2080 /*
2081 * Copy the ethhdr from first buffer to second. This
2082 * is necessary for 3022 IP completions.
2083 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03002084 skb_copy_from_linear_data_offset(skb1, VLAN_ID_LEN,
2085 skb_push(skb2, size), size);
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002086 } else {
2087 u16 checksum = le16_to_cpu(ib_ip_rsp_ptr->checksum);
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002088 if (checksum &
2089 (IB_IP_IOCB_RSP_3032_ICE |
2090 IB_IP_IOCB_RSP_3032_CE)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002091 netdev_err(ndev,
2092 "%s: Bad checksum for this %s packet, checksum = %x\n",
2093 __func__,
2094 ((checksum & IB_IP_IOCB_RSP_3032_TCP) ?
2095 "TCP" : "UDP"), checksum);
Ron Mercerb3b15142007-03-26 13:43:00 -07002096 } else if ((checksum & IB_IP_IOCB_RSP_3032_TCP) ||
2097 (checksum & IB_IP_IOCB_RSP_3032_UDP &&
2098 !(checksum & IB_IP_IOCB_RSP_3032_NUC))) {
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002099 skb2->ip_summed = CHECKSUM_UNNECESSARY;
Ron Mercerb3b15142007-03-26 13:43:00 -07002100 }
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002101 }
Ron Mercer5a4faa872006-07-25 00:40:21 -07002102 skb2->protocol = eth_type_trans(skb2, qdev->ndev);
2103
2104 netif_receive_skb(skb2);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002105 ndev->stats.rx_packets++;
2106 ndev->stats.rx_bytes += length;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002107 lrg_buf_cb2->skb = NULL;
2108
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002109 if (qdev->device_id == QL3022_DEVICE_ID)
2110 ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb1);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002111 ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb2);
2112}
2113
2114static int ql_tx_rx_clean(struct ql3_adapter *qdev,
2115 int *tx_cleaned, int *rx_cleaned, int work_to_do)
2116{
Ron Mercer5a4faa872006-07-25 00:40:21 -07002117 struct net_rsp_iocb *net_rsp;
2118 struct net_device *ndev = qdev->ndev;
Ron Mercer63b66d12007-02-26 11:06:41 -08002119 int work_done = 0;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002120
2121 /* While there are entries in the completion queue. */
Ron Mercerf67cac02007-03-26 13:42:59 -07002122 while ((le32_to_cpu(*(qdev->prsp_producer_index)) !=
Ron Mercer63b66d12007-02-26 11:06:41 -08002123 qdev->rsp_consumer_index) && (work_done < work_to_do)) {
Ron Mercer5a4faa872006-07-25 00:40:21 -07002124
2125 net_rsp = qdev->rsp_current;
Ron Mercerb323e0e2007-10-01 11:43:22 -07002126 rmb();
Ron Mercer50626292007-10-01 11:43:23 -07002127 /*
Joe Perchesd7f61772010-07-22 15:36:17 +00002128 * Fix 4032 chip's undocumented "feature" where bit-8 is set
2129 * if the inbound completion is for a VLAN.
Ron Mercer50626292007-10-01 11:43:23 -07002130 */
2131 if (qdev->device_id == QL3032_DEVICE_ID)
2132 net_rsp->opcode &= 0x7f;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002133 switch (net_rsp->opcode) {
2134
2135 case OPCODE_OB_MAC_IOCB_FN0:
2136 case OPCODE_OB_MAC_IOCB_FN2:
2137 ql_process_mac_tx_intr(qdev, (struct ob_mac_iocb_rsp *)
2138 net_rsp);
2139 (*tx_cleaned)++;
2140 break;
2141
2142 case OPCODE_IB_MAC_IOCB:
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002143 case OPCODE_IB_3032_MAC_IOCB:
Ron Mercer5a4faa872006-07-25 00:40:21 -07002144 ql_process_mac_rx_intr(qdev, (struct ib_mac_iocb_rsp *)
2145 net_rsp);
2146 (*rx_cleaned)++;
2147 break;
2148
2149 case OPCODE_IB_IP_IOCB:
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002150 case OPCODE_IB_3032_IP_IOCB:
Ron Mercer5a4faa872006-07-25 00:40:21 -07002151 ql_process_macip_rx_intr(qdev, (struct ib_ip_iocb_rsp *)
2152 net_rsp);
2153 (*rx_cleaned)++;
2154 break;
Joe Perchesd7f61772010-07-22 15:36:17 +00002155 default: {
2156 u32 *tmp = (u32 *)net_rsp;
2157 netdev_err(ndev,
2158 "Hit default case, not handled!\n"
2159 " dropping the packet, opcode = %x\n"
2160 "0x%08lx 0x%08lx 0x%08lx 0x%08lx\n",
2161 net_rsp->opcode,
2162 (unsigned long int)tmp[0],
2163 (unsigned long int)tmp[1],
2164 (unsigned long int)tmp[2],
2165 (unsigned long int)tmp[3]);
2166 }
Ron Mercer5a4faa872006-07-25 00:40:21 -07002167 }
2168
2169 qdev->rsp_consumer_index++;
2170
2171 if (qdev->rsp_consumer_index == NUM_RSP_Q_ENTRIES) {
2172 qdev->rsp_consumer_index = 0;
2173 qdev->rsp_current = qdev->rsp_q_virt_addr;
2174 } else {
2175 qdev->rsp_current++;
2176 }
Ron Mercer63b66d12007-02-26 11:06:41 -08002177
2178 work_done = *tx_cleaned + *rx_cleaned;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002179 }
2180
Ron Mercerf67cac02007-03-26 13:42:59 -07002181 return work_done;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002182}
2183
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002184static int ql_poll(struct napi_struct *napi, int budget)
Ron Mercer5a4faa872006-07-25 00:40:21 -07002185{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002186 struct ql3_adapter *qdev = container_of(napi, struct ql3_adapter, napi);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002187 int rx_cleaned = 0, tx_cleaned = 0;
Ron Mercer63b66d12007-02-26 11:06:41 -08002188 unsigned long hw_flags;
Joe Perchesd7f61772010-07-22 15:36:17 +00002189 struct ql3xxx_port_registers __iomem *port_regs =
2190 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002191
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002192 ql_tx_rx_clean(qdev, &tx_cleaned, &rx_cleaned, budget);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002193
David S. Miller4ec24112008-01-07 20:48:21 -08002194 if (tx_cleaned + rx_cleaned != budget) {
Ron Mercer63b66d12007-02-26 11:06:41 -08002195 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
Ben Hutchings288379f2009-01-19 16:43:59 -08002196 __napi_complete(napi);
Ron Mercerf67cac02007-03-26 13:42:59 -07002197 ql_update_small_bufq_prod_index(qdev);
2198 ql_update_lrg_bufq_prod_index(qdev);
2199 writel(qdev->rsp_consumer_index,
2200 &port_regs->CommonRegs.rspQConsumerIndex);
Ron Mercer63b66d12007-02-26 11:06:41 -08002201 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
2202
Ron Mercer5a4faa872006-07-25 00:40:21 -07002203 ql_enable_interrupts(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002204 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002205 return tx_cleaned + rx_cleaned;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002206}
2207
David Howells7d12e782006-10-05 14:55:46 +01002208static irqreturn_t ql3xxx_isr(int irq, void *dev_id)
Ron Mercer5a4faa872006-07-25 00:40:21 -07002209{
2210
2211 struct net_device *ndev = dev_id;
2212 struct ql3_adapter *qdev = netdev_priv(ndev);
Joe Perchesd7f61772010-07-22 15:36:17 +00002213 struct ql3xxx_port_registers __iomem *port_regs =
2214 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002215 u32 value;
2216 int handled = 1;
2217 u32 var;
2218
Joe Perchesd7f61772010-07-22 15:36:17 +00002219 value = ql_read_common_reg_l(qdev,
2220 &port_regs->CommonRegs.ispControlStatus);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002221
2222 if (value & (ISP_CONTROL_FE | ISP_CONTROL_RI)) {
2223 spin_lock(&qdev->adapter_lock);
2224 netif_stop_queue(qdev->ndev);
2225 netif_carrier_off(qdev->ndev);
2226 ql_disable_interrupts(qdev);
2227 qdev->port_link_state = LS_DOWN;
Joe Perchesd7f61772010-07-22 15:36:17 +00002228 set_bit(QL_RESET_ACTIVE, &qdev->flags) ;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002229
2230 if (value & ISP_CONTROL_FE) {
2231 /*
2232 * Chip Fatal Error.
2233 */
2234 var =
2235 ql_read_page0_reg_l(qdev,
2236 &port_regs->PortFatalErrStatus);
Joe Percheseddc5fb2010-07-22 12:33:31 +00002237 netdev_warn(ndev,
2238 "Resetting chip. PortFatalErrStatus register = 0x%x\n",
2239 var);
Joe Perchesd7f61772010-07-22 15:36:17 +00002240 set_bit(QL_RESET_START, &qdev->flags) ;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002241 } else {
2242 /*
2243 * Soft Reset Requested.
2244 */
Joe Perchesd7f61772010-07-22 15:36:17 +00002245 set_bit(QL_RESET_PER_SCSI, &qdev->flags) ;
Joe Percheseddc5fb2010-07-22 12:33:31 +00002246 netdev_err(ndev,
2247 "Another function issued a reset to the chip. ISR value = %x\n",
2248 value);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002249 }
David Howellsc4028952006-11-22 14:57:56 +00002250 queue_delayed_work(qdev->workqueue, &qdev->reset_work, 0);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002251 spin_unlock(&qdev->adapter_lock);
2252 } else if (value & ISP_IMR_DISABLE_CMPL_INT) {
Benjamin Lie8f4df22007-02-26 11:06:42 -08002253 ql_disable_interrupts(qdev);
Joe Perchesd7f61772010-07-22 15:36:17 +00002254 if (likely(napi_schedule_prep(&qdev->napi)))
Ben Hutchings288379f2009-01-19 16:43:59 -08002255 __napi_schedule(&qdev->napi);
Joe Perchesd7f61772010-07-22 15:36:17 +00002256 } else
Ron Mercer5a4faa872006-07-25 00:40:21 -07002257 return IRQ_NONE;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002258
2259 return IRQ_RETVAL(handled);
2260}
2261
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002262/*
Joe Perchesd7f61772010-07-22 15:36:17 +00002263 * Get the total number of segments needed for the given number of fragments.
2264 * This is necessary because outbound address lists (OAL) will be used when
2265 * more than two frags are given. Each address list has 5 addr/len pairs.
2266 * The 5th pair in each OAL is used to point to the next OAL if more frags
2267 * are coming. That is why the frags:segment count ratio is not linear.
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002268 */
Joe Perchesd7f61772010-07-22 15:36:17 +00002269static int ql_get_seg_count(struct ql3_adapter *qdev, unsigned short frags)
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002270{
Benjamin Lie8f4df22007-02-26 11:06:42 -08002271 if (qdev->device_id == QL3022_DEVICE_ID)
2272 return 1;
2273
Joe Perchesd7f61772010-07-22 15:36:17 +00002274 if (frags <= 2)
2275 return frags + 1;
2276 else if (frags <= 6)
2277 return frags + 2;
2278 else if (frags <= 10)
2279 return frags + 3;
2280 else if (frags <= 14)
2281 return frags + 4;
2282 else if (frags <= 18)
2283 return frags + 5;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002284 return -1;
2285}
2286
Stephen Hemminger91e745a2007-05-30 14:23:18 -07002287static void ql_hw_csum_setup(const struct sk_buff *skb,
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002288 struct ob_mac_iocb_req *mac_iocb_ptr)
2289{
Stephen Hemminger91e745a2007-05-30 14:23:18 -07002290 const struct iphdr *ip = ip_hdr(skb);
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002291
Stephen Hemminger91e745a2007-05-30 14:23:18 -07002292 mac_iocb_ptr->ip_hdr_off = skb_network_offset(skb);
2293 mac_iocb_ptr->ip_hdr_len = ip->ihl;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002294
Stephen Hemminger91e745a2007-05-30 14:23:18 -07002295 if (ip->protocol == IPPROTO_TCP) {
2296 mac_iocb_ptr->flags1 |= OB_3032MAC_IOCB_REQ_TC |
2297 OB_3032MAC_IOCB_REQ_IC;
2298 } else {
2299 mac_iocb_ptr->flags1 |= OB_3032MAC_IOCB_REQ_UC |
2300 OB_3032MAC_IOCB_REQ_IC;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002301 }
2302
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002303}
2304
2305/*
Joe Perchesd7f61772010-07-22 15:36:17 +00002306 * Map the buffers for this transmit.
2307 * This will return NETDEV_TX_BUSY or NETDEV_TX_OK based on success.
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002308 */
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002309static int ql_send_map(struct ql3_adapter *qdev,
2310 struct ob_mac_iocb_req *mac_iocb_ptr,
2311 struct ql_tx_buf_cb *tx_cb,
2312 struct sk_buff *skb)
Ron Mercer5a4faa872006-07-25 00:40:21 -07002313{
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002314 struct oal *oal;
2315 struct oal_entry *oal_entry;
Ron Mercer63f779262007-02-28 16:42:17 -08002316 int len = skb_headlen(skb);
Benjamin Li0f8ab892007-02-26 11:06:40 -08002317 dma_addr_t map;
2318 int err;
2319 int completed_segs, i;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002320 int seg_cnt, seg = 0;
2321 int frag_cnt = (int)skb_shinfo(skb)->nr_frags;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002322
Ron Mercerb6967eb2007-03-26 13:42:58 -07002323 seg_cnt = tx_cb->seg_count;
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002324 /*
2325 * Map the skb buffer first.
2326 */
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002327 map = pci_map_single(qdev->pdev, skb->data, len, PCI_DMA_TODEVICE);
Benjamin Li0f8ab892007-02-26 11:06:40 -08002328
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07002329 err = pci_dma_mapping_error(qdev->pdev, map);
Joe Perchesd7f61772010-07-22 15:36:17 +00002330 if (err) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002331 netdev_err(qdev->ndev, "PCI mapping failed with error: %d\n",
2332 err);
Benjamin Li0f8ab892007-02-26 11:06:40 -08002333
2334 return NETDEV_TX_BUSY;
2335 }
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002336
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002337 oal_entry = (struct oal_entry *)&mac_iocb_ptr->buf_addr0_low;
2338 oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
2339 oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
2340 oal_entry->len = cpu_to_le32(len);
FUJITA Tomonori87196eb2010-04-12 14:32:13 +00002341 dma_unmap_addr_set(&tx_cb->map[seg], mapaddr, map);
2342 dma_unmap_len_set(&tx_cb->map[seg], maplen, len);
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002343 seg++;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002344
Benjamin Lie8f4df22007-02-26 11:06:42 -08002345 if (seg_cnt == 1) {
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002346 /* Terminate the last segment. */
Marcin Slusarzb39b5a22008-03-04 15:19:20 -08002347 oal_entry->len |= cpu_to_le32(OAL_LAST_ENTRY);
Joe Perchesd7f61772010-07-22 15:36:17 +00002348 return NETDEV_TX_OK;
2349 }
2350 oal = tx_cb->oal;
2351 for (completed_segs = 0;
2352 completed_segs < frag_cnt;
2353 completed_segs++, seg++) {
2354 skb_frag_t *frag = &skb_shinfo(skb)->frags[completed_segs];
2355 oal_entry++;
2356 /*
2357 * Check for continuation requirements.
2358 * It's strange but necessary.
2359 * Continuation entry points to outbound address list.
2360 */
2361 if ((seg == 2 && seg_cnt > 3) ||
2362 (seg == 7 && seg_cnt > 8) ||
2363 (seg == 12 && seg_cnt > 13) ||
2364 (seg == 17 && seg_cnt > 18)) {
2365 map = pci_map_single(qdev->pdev, oal,
2366 sizeof(struct oal),
2367 PCI_DMA_TODEVICE);
Benjamin Li0f8ab892007-02-26 11:06:40 -08002368
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07002369 err = pci_dma_mapping_error(qdev->pdev, map);
Joe Perchesd7f61772010-07-22 15:36:17 +00002370 if (err) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002371 netdev_err(qdev->ndev,
Joe Perchesd7f61772010-07-22 15:36:17 +00002372 "PCI mapping outbound address list with error: %d\n",
Joe Percheseddc5fb2010-07-22 12:33:31 +00002373 err);
Benjamin Li0f8ab892007-02-26 11:06:40 -08002374 goto map_error;
2375 }
2376
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002377 oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
2378 oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
Joe Perchesd7f61772010-07-22 15:36:17 +00002379 oal_entry->len = cpu_to_le32(sizeof(struct oal) |
2380 OAL_CONT_ENTRY);
FUJITA Tomonori87196eb2010-04-12 14:32:13 +00002381 dma_unmap_addr_set(&tx_cb->map[seg], mapaddr, map);
2382 dma_unmap_len_set(&tx_cb->map[seg], maplen,
Joe Perchesd7f61772010-07-22 15:36:17 +00002383 sizeof(struct oal));
2384 oal_entry = (struct oal_entry *)oal;
2385 oal++;
2386 seg++;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002387 }
Benjamin Li0f8ab892007-02-26 11:06:40 -08002388
Eric Dumazet9e903e02011-10-18 21:00:24 +00002389 map = skb_frag_dma_map(&qdev->pdev->dev, frag, 0, skb_frag_size(frag),
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01002390 DMA_TO_DEVICE);
Joe Perchesd7f61772010-07-22 15:36:17 +00002391
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01002392 err = dma_mapping_error(&qdev->pdev->dev, map);
Joe Perchesd7f61772010-07-22 15:36:17 +00002393 if (err) {
2394 netdev_err(qdev->ndev,
2395 "PCI mapping frags failed with error: %d\n",
2396 err);
2397 goto map_error;
2398 }
2399
2400 oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
2401 oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
Eric Dumazet9e903e02011-10-18 21:00:24 +00002402 oal_entry->len = cpu_to_le32(skb_frag_size(frag));
Joe Perchesd7f61772010-07-22 15:36:17 +00002403 dma_unmap_addr_set(&tx_cb->map[seg], mapaddr, map);
Eric Dumazet9e903e02011-10-18 21:00:24 +00002404 dma_unmap_len_set(&tx_cb->map[seg], maplen, skb_frag_size(frag));
Joe Perchesd7f61772010-07-22 15:36:17 +00002405 }
2406 /* Terminate the last segment. */
2407 oal_entry->len |= cpu_to_le32(OAL_LAST_ENTRY);
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002408 return NETDEV_TX_OK;
Benjamin Li0f8ab892007-02-26 11:06:40 -08002409
2410map_error:
2411 /* A PCI mapping failed and now we will need to back out
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002412 * We need to traverse through the oal's and associated pages which
Benjamin Li0f8ab892007-02-26 11:06:40 -08002413 * have been mapped and now we must unmap them to clean up properly
2414 */
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002415
Benjamin Li0f8ab892007-02-26 11:06:40 -08002416 seg = 1;
2417 oal_entry = (struct oal_entry *)&mac_iocb_ptr->buf_addr0_low;
2418 oal = tx_cb->oal;
Joe Perchesd7f61772010-07-22 15:36:17 +00002419 for (i = 0; i < completed_segs; i++, seg++) {
Benjamin Li0f8ab892007-02-26 11:06:40 -08002420 oal_entry++;
2421
Joe Perchesd7f61772010-07-22 15:36:17 +00002422 /*
2423 * Check for continuation requirements.
2424 * It's strange but necessary.
2425 */
2426
2427 if ((seg == 2 && seg_cnt > 3) ||
2428 (seg == 7 && seg_cnt > 8) ||
2429 (seg == 12 && seg_cnt > 13) ||
2430 (seg == 17 && seg_cnt > 18)) {
Benjamin Li0f8ab892007-02-26 11:06:40 -08002431 pci_unmap_single(qdev->pdev,
FUJITA Tomonori87196eb2010-04-12 14:32:13 +00002432 dma_unmap_addr(&tx_cb->map[seg], mapaddr),
2433 dma_unmap_len(&tx_cb->map[seg], maplen),
Benjamin Li0f8ab892007-02-26 11:06:40 -08002434 PCI_DMA_TODEVICE);
2435 oal++;
2436 seg++;
2437 }
2438
2439 pci_unmap_page(qdev->pdev,
FUJITA Tomonori87196eb2010-04-12 14:32:13 +00002440 dma_unmap_addr(&tx_cb->map[seg], mapaddr),
2441 dma_unmap_len(&tx_cb->map[seg], maplen),
Benjamin Li0f8ab892007-02-26 11:06:40 -08002442 PCI_DMA_TODEVICE);
2443 }
2444
2445 pci_unmap_single(qdev->pdev,
FUJITA Tomonori87196eb2010-04-12 14:32:13 +00002446 dma_unmap_addr(&tx_cb->map[0], mapaddr),
2447 dma_unmap_addr(&tx_cb->map[0], maplen),
Benjamin Li0f8ab892007-02-26 11:06:40 -08002448 PCI_DMA_TODEVICE);
2449
2450 return NETDEV_TX_BUSY;
2451
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002452}
2453
2454/*
2455 * The difference between 3022 and 3032 sends:
2456 * 3022 only supports a simple single segment transmission.
2457 * 3032 supports checksumming and scatter/gather lists (fragments).
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002458 * The 3032 supports sglists by using the 3 addr/len pairs (ALP)
2459 * in the IOCB plus a chain of outbound address lists (OAL) that
2460 * each contain 5 ALPs. The last ALP of the IOCB (3rd) or OAL (5th)
Gilles Espinasse177b2412011-01-09 08:59:49 +01002461 * will be used to point to an OAL when more ALP entries are required.
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002462 * The IOCB is always the top of the chain followed by one or more
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002463 * OALs (when necessary).
2464 */
Stephen Hemminger613573252009-08-31 19:50:58 +00002465static netdev_tx_t ql3xxx_send(struct sk_buff *skb,
2466 struct net_device *ndev)
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002467{
Joe Perches4dd15182010-11-15 11:12:27 +00002468 struct ql3_adapter *qdev = netdev_priv(ndev);
Joe Perchesd7f61772010-07-22 15:36:17 +00002469 struct ql3xxx_port_registers __iomem *port_regs =
2470 qdev->mem_map_registers;
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002471 struct ql_tx_buf_cb *tx_cb;
2472 u32 tot_len = skb->len;
2473 struct ob_mac_iocb_req *mac_iocb_ptr;
2474
Joe Perchesd7f61772010-07-22 15:36:17 +00002475 if (unlikely(atomic_read(&qdev->tx_count) < 2))
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002476 return NETDEV_TX_BUSY;
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002477
Joe Perchesd7f61772010-07-22 15:36:17 +00002478 tx_cb = &qdev->tx_buf[qdev->req_producer_index];
2479 tx_cb->seg_count = ql_get_seg_count(qdev,
2480 skb_shinfo(skb)->nr_frags);
2481 if (tx_cb->seg_count == -1) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002482 netdev_err(ndev, "%s: invalid segment count!\n", __func__);
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002483 return NETDEV_TX_OK;
2484 }
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002485
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002486 mac_iocb_ptr = tx_cb->queue_entry;
Ron Mercerd8a759f2007-03-26 13:42:57 -07002487 memset((void *)mac_iocb_ptr, 0, sizeof(struct ob_mac_iocb_req));
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002488 mac_iocb_ptr->opcode = qdev->mac_ob_opcode;
2489 mac_iocb_ptr->flags = OB_MAC_IOCB_REQ_X;
2490 mac_iocb_ptr->flags |= qdev->mb_bit_mask;
2491 mac_iocb_ptr->transaction_id = qdev->req_producer_index;
2492 mac_iocb_ptr->data_len = cpu_to_le16((u16) tot_len);
2493 tx_cb->skb = skb;
Benjamin Lie8f4df22007-02-26 11:06:42 -08002494 if (qdev->device_id == QL3032_DEVICE_ID &&
2495 skb->ip_summed == CHECKSUM_PARTIAL)
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002496 ql_hw_csum_setup(skb, mac_iocb_ptr);
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002497
Joe Perchesd7f61772010-07-22 15:36:17 +00002498 if (ql_send_map(qdev, mac_iocb_ptr, tx_cb, skb) != NETDEV_TX_OK) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002499 netdev_err(ndev, "%s: Could not map the segments!\n", __func__);
Ron Mercer3e71f6d2007-02-26 11:06:39 -08002500 return NETDEV_TX_BUSY;
2501 }
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002502
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002503 wmb();
Ron Mercer5a4faa872006-07-25 00:40:21 -07002504 qdev->req_producer_index++;
2505 if (qdev->req_producer_index == NUM_REQ_Q_ENTRIES)
2506 qdev->req_producer_index = 0;
2507 wmb();
2508 ql_write_common_reg_l(qdev,
Al Viroee111d12006-09-25 02:53:53 +01002509 &port_regs->CommonRegs.reqQProducerIndex,
Ron Mercer5a4faa872006-07-25 00:40:21 -07002510 qdev->req_producer_index);
2511
Joe Percheseddc5fb2010-07-22 12:33:31 +00002512 netif_printk(qdev, tx_queued, KERN_DEBUG, ndev,
2513 "tx queued, slot %d, len %d\n",
2514 qdev->req_producer_index, skb->len);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002515
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002516 atomic_dec(&qdev->tx_count);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002517 return NETDEV_TX_OK;
2518}
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002519
Ron Mercer5a4faa872006-07-25 00:40:21 -07002520static int ql_alloc_net_req_rsp_queues(struct ql3_adapter *qdev)
2521{
2522 qdev->req_q_size =
2523 (u32) (NUM_REQ_Q_ENTRIES * sizeof(struct ob_mac_iocb_req));
2524
Joe Jin8a6e29d2012-10-21 14:40:36 +00002525 qdev->rsp_q_size = NUM_RSP_Q_ENTRIES * sizeof(struct net_rsp_iocb);
2526
2527 /* The barrier is required to ensure request and response queue
2528 * addr writes to the registers.
2529 */
2530 wmb();
2531
Ron Mercer5a4faa872006-07-25 00:40:21 -07002532 qdev->req_q_virt_addr =
2533 pci_alloc_consistent(qdev->pdev,
2534 (size_t) qdev->req_q_size,
2535 &qdev->req_q_phy_addr);
2536
2537 if ((qdev->req_q_virt_addr == NULL) ||
2538 LS_64BITS(qdev->req_q_phy_addr) & (qdev->req_q_size - 1)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002539 netdev_err(qdev->ndev, "reqQ failed\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07002540 return -ENOMEM;
2541 }
2542
Ron Mercer5a4faa872006-07-25 00:40:21 -07002543 qdev->rsp_q_virt_addr =
2544 pci_alloc_consistent(qdev->pdev,
2545 (size_t) qdev->rsp_q_size,
2546 &qdev->rsp_q_phy_addr);
2547
2548 if ((qdev->rsp_q_virt_addr == NULL) ||
2549 LS_64BITS(qdev->rsp_q_phy_addr) & (qdev->rsp_q_size - 1)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002550 netdev_err(qdev->ndev, "rspQ allocation failed\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07002551 pci_free_consistent(qdev->pdev, (size_t) qdev->req_q_size,
2552 qdev->req_q_virt_addr,
2553 qdev->req_q_phy_addr);
2554 return -ENOMEM;
2555 }
2556
Joe Perchesd7f61772010-07-22 15:36:17 +00002557 set_bit(QL_ALLOC_REQ_RSP_Q_DONE, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002558
2559 return 0;
2560}
2561
2562static void ql_free_net_req_rsp_queues(struct ql3_adapter *qdev)
2563{
Joe Perchesd7f61772010-07-22 15:36:17 +00002564 if (!test_bit(QL_ALLOC_REQ_RSP_Q_DONE, &qdev->flags)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002565 netdev_info(qdev->ndev, "Already done\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07002566 return;
2567 }
2568
2569 pci_free_consistent(qdev->pdev,
2570 qdev->req_q_size,
2571 qdev->req_q_virt_addr, qdev->req_q_phy_addr);
2572
2573 qdev->req_q_virt_addr = NULL;
2574
2575 pci_free_consistent(qdev->pdev,
2576 qdev->rsp_q_size,
2577 qdev->rsp_q_virt_addr, qdev->rsp_q_phy_addr);
2578
2579 qdev->rsp_q_virt_addr = NULL;
2580
Joe Perchesd7f61772010-07-22 15:36:17 +00002581 clear_bit(QL_ALLOC_REQ_RSP_Q_DONE, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002582}
2583
2584static int ql_alloc_buffer_queues(struct ql3_adapter *qdev)
2585{
2586 /* Create Large Buffer Queue */
2587 qdev->lrg_buf_q_size =
Joe Perchesd7f61772010-07-22 15:36:17 +00002588 qdev->num_lbufq_entries * sizeof(struct lrg_buf_q_entry);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002589 if (qdev->lrg_buf_q_size < PAGE_SIZE)
2590 qdev->lrg_buf_q_alloc_size = PAGE_SIZE;
2591 else
2592 qdev->lrg_buf_q_alloc_size = qdev->lrg_buf_q_size * 2;
2593
Joe Perchesb2adaca2013-02-03 17:43:58 +00002594 qdev->lrg_buf = kmalloc_array(qdev->num_large_buffers,
2595 sizeof(struct ql_rcv_buf_cb),
2596 GFP_KERNEL);
2597 if (qdev->lrg_buf == NULL)
Ron Mercer1357bfc2007-02-26 11:06:37 -08002598 return -ENOMEM;
Jeff Garzik9ddf7772007-10-03 13:52:23 -04002599
Ron Mercer5a4faa872006-07-25 00:40:21 -07002600 qdev->lrg_buf_q_alloc_virt_addr =
Joe Perchesd7f61772010-07-22 15:36:17 +00002601 pci_alloc_consistent(qdev->pdev,
2602 qdev->lrg_buf_q_alloc_size,
2603 &qdev->lrg_buf_q_alloc_phy_addr);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002604
2605 if (qdev->lrg_buf_q_alloc_virt_addr == NULL) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002606 netdev_err(qdev->ndev, "lBufQ failed\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07002607 return -ENOMEM;
2608 }
2609 qdev->lrg_buf_q_virt_addr = qdev->lrg_buf_q_alloc_virt_addr;
2610 qdev->lrg_buf_q_phy_addr = qdev->lrg_buf_q_alloc_phy_addr;
2611
2612 /* Create Small Buffer Queue */
2613 qdev->small_buf_q_size =
Joe Perchesd7f61772010-07-22 15:36:17 +00002614 NUM_SBUFQ_ENTRIES * sizeof(struct lrg_buf_q_entry);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002615 if (qdev->small_buf_q_size < PAGE_SIZE)
2616 qdev->small_buf_q_alloc_size = PAGE_SIZE;
2617 else
2618 qdev->small_buf_q_alloc_size = qdev->small_buf_q_size * 2;
2619
2620 qdev->small_buf_q_alloc_virt_addr =
Joe Perchesd7f61772010-07-22 15:36:17 +00002621 pci_alloc_consistent(qdev->pdev,
2622 qdev->small_buf_q_alloc_size,
2623 &qdev->small_buf_q_alloc_phy_addr);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002624
2625 if (qdev->small_buf_q_alloc_virt_addr == NULL) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002626 netdev_err(qdev->ndev, "Small Buffer Queue allocation failed\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07002627 pci_free_consistent(qdev->pdev, qdev->lrg_buf_q_alloc_size,
2628 qdev->lrg_buf_q_alloc_virt_addr,
2629 qdev->lrg_buf_q_alloc_phy_addr);
2630 return -ENOMEM;
2631 }
2632
2633 qdev->small_buf_q_virt_addr = qdev->small_buf_q_alloc_virt_addr;
2634 qdev->small_buf_q_phy_addr = qdev->small_buf_q_alloc_phy_addr;
Joe Perchesd7f61772010-07-22 15:36:17 +00002635 set_bit(QL_ALLOC_BUFQS_DONE, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002636 return 0;
2637}
2638
2639static void ql_free_buffer_queues(struct ql3_adapter *qdev)
2640{
Joe Perchesd7f61772010-07-22 15:36:17 +00002641 if (!test_bit(QL_ALLOC_BUFQS_DONE, &qdev->flags)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002642 netdev_info(qdev->ndev, "Already done\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07002643 return;
2644 }
Joe Perchesd7f61772010-07-22 15:36:17 +00002645 kfree(qdev->lrg_buf);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002646 pci_free_consistent(qdev->pdev,
2647 qdev->lrg_buf_q_alloc_size,
2648 qdev->lrg_buf_q_alloc_virt_addr,
2649 qdev->lrg_buf_q_alloc_phy_addr);
2650
2651 qdev->lrg_buf_q_virt_addr = NULL;
2652
2653 pci_free_consistent(qdev->pdev,
2654 qdev->small_buf_q_alloc_size,
2655 qdev->small_buf_q_alloc_virt_addr,
2656 qdev->small_buf_q_alloc_phy_addr);
2657
2658 qdev->small_buf_q_virt_addr = NULL;
2659
Joe Perchesd7f61772010-07-22 15:36:17 +00002660 clear_bit(QL_ALLOC_BUFQS_DONE, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002661}
2662
2663static int ql_alloc_small_buffers(struct ql3_adapter *qdev)
2664{
2665 int i;
2666 struct bufq_addr_element *small_buf_q_entry;
2667
2668 /* Currently we allocate on one of memory and use it for smallbuffers */
2669 qdev->small_buf_total_size =
Joe Perchesd7f61772010-07-22 15:36:17 +00002670 (QL_ADDR_ELE_PER_BUFQ_ENTRY * NUM_SBUFQ_ENTRIES *
2671 QL_SMALL_BUFFER_SIZE);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002672
2673 qdev->small_buf_virt_addr =
Joe Perchesd7f61772010-07-22 15:36:17 +00002674 pci_alloc_consistent(qdev->pdev,
2675 qdev->small_buf_total_size,
2676 &qdev->small_buf_phy_addr);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002677
2678 if (qdev->small_buf_virt_addr == NULL) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002679 netdev_err(qdev->ndev, "Failed to get small buffer memory\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07002680 return -ENOMEM;
2681 }
2682
2683 qdev->small_buf_phy_addr_low = LS_64BITS(qdev->small_buf_phy_addr);
2684 qdev->small_buf_phy_addr_high = MS_64BITS(qdev->small_buf_phy_addr);
2685
2686 small_buf_q_entry = qdev->small_buf_q_virt_addr;
2687
Ron Mercer5a4faa872006-07-25 00:40:21 -07002688 /* Initialize the small buffer queue. */
2689 for (i = 0; i < (QL_ADDR_ELE_PER_BUFQ_ENTRY * NUM_SBUFQ_ENTRIES); i++) {
2690 small_buf_q_entry->addr_high =
2691 cpu_to_le32(qdev->small_buf_phy_addr_high);
2692 small_buf_q_entry->addr_low =
2693 cpu_to_le32(qdev->small_buf_phy_addr_low +
2694 (i * QL_SMALL_BUFFER_SIZE));
2695 small_buf_q_entry++;
2696 }
2697 qdev->small_buf_index = 0;
Joe Perchesd7f61772010-07-22 15:36:17 +00002698 set_bit(QL_ALLOC_SMALL_BUF_DONE, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002699 return 0;
2700}
2701
2702static void ql_free_small_buffers(struct ql3_adapter *qdev)
2703{
Joe Perchesd7f61772010-07-22 15:36:17 +00002704 if (!test_bit(QL_ALLOC_SMALL_BUF_DONE, &qdev->flags)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002705 netdev_info(qdev->ndev, "Already done\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07002706 return;
2707 }
2708 if (qdev->small_buf_virt_addr != NULL) {
2709 pci_free_consistent(qdev->pdev,
2710 qdev->small_buf_total_size,
2711 qdev->small_buf_virt_addr,
2712 qdev->small_buf_phy_addr);
2713
2714 qdev->small_buf_virt_addr = NULL;
2715 }
2716}
2717
2718static void ql_free_large_buffers(struct ql3_adapter *qdev)
2719{
2720 int i = 0;
2721 struct ql_rcv_buf_cb *lrg_buf_cb;
2722
Ron Mercer1357bfc2007-02-26 11:06:37 -08002723 for (i = 0; i < qdev->num_large_buffers; i++) {
Ron Mercer5a4faa872006-07-25 00:40:21 -07002724 lrg_buf_cb = &qdev->lrg_buf[i];
2725 if (lrg_buf_cb->skb) {
2726 dev_kfree_skb(lrg_buf_cb->skb);
2727 pci_unmap_single(qdev->pdev,
FUJITA Tomonori87196eb2010-04-12 14:32:13 +00002728 dma_unmap_addr(lrg_buf_cb, mapaddr),
2729 dma_unmap_len(lrg_buf_cb, maplen),
Ron Mercer5a4faa872006-07-25 00:40:21 -07002730 PCI_DMA_FROMDEVICE);
2731 memset(lrg_buf_cb, 0, sizeof(struct ql_rcv_buf_cb));
2732 } else {
2733 break;
2734 }
2735 }
2736}
2737
2738static void ql_init_large_buffers(struct ql3_adapter *qdev)
2739{
2740 int i;
2741 struct ql_rcv_buf_cb *lrg_buf_cb;
2742 struct bufq_addr_element *buf_addr_ele = qdev->lrg_buf_q_virt_addr;
2743
Ron Mercer1357bfc2007-02-26 11:06:37 -08002744 for (i = 0; i < qdev->num_large_buffers; i++) {
Ron Mercer5a4faa872006-07-25 00:40:21 -07002745 lrg_buf_cb = &qdev->lrg_buf[i];
2746 buf_addr_ele->addr_high = lrg_buf_cb->buf_phy_addr_high;
2747 buf_addr_ele->addr_low = lrg_buf_cb->buf_phy_addr_low;
2748 buf_addr_ele++;
2749 }
2750 qdev->lrg_buf_index = 0;
2751 qdev->lrg_buf_skb_check = 0;
2752}
2753
2754static int ql_alloc_large_buffers(struct ql3_adapter *qdev)
2755{
2756 int i;
2757 struct ql_rcv_buf_cb *lrg_buf_cb;
2758 struct sk_buff *skb;
Benjamin Li0f8ab892007-02-26 11:06:40 -08002759 dma_addr_t map;
2760 int err;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002761
Ron Mercer1357bfc2007-02-26 11:06:37 -08002762 for (i = 0; i < qdev->num_large_buffers; i++) {
Benjamin Licd238fa2007-02-26 11:06:33 -08002763 skb = netdev_alloc_skb(qdev->ndev,
2764 qdev->lrg_buffer_len);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002765 if (unlikely(!skb)) {
2766 /* Better luck next round */
Joe Percheseddc5fb2010-07-22 12:33:31 +00002767 netdev_err(qdev->ndev,
2768 "large buff alloc failed for %d bytes at index %d\n",
2769 qdev->lrg_buffer_len * 2, i);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002770 ql_free_large_buffers(qdev);
2771 return -ENOMEM;
2772 } else {
2773
2774 lrg_buf_cb = &qdev->lrg_buf[i];
2775 memset(lrg_buf_cb, 0, sizeof(struct ql_rcv_buf_cb));
2776 lrg_buf_cb->index = i;
2777 lrg_buf_cb->skb = skb;
2778 /*
2779 * We save some space to copy the ethhdr from first
2780 * buffer
2781 */
2782 skb_reserve(skb, QL_HEADER_SPACE);
2783 map = pci_map_single(qdev->pdev,
2784 skb->data,
2785 qdev->lrg_buffer_len -
2786 QL_HEADER_SPACE,
2787 PCI_DMA_FROMDEVICE);
Benjamin Li0f8ab892007-02-26 11:06:40 -08002788
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07002789 err = pci_dma_mapping_error(qdev->pdev, map);
Joe Perchesd7f61772010-07-22 15:36:17 +00002790 if (err) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002791 netdev_err(qdev->ndev,
2792 "PCI mapping failed with error: %d\n",
2793 err);
Benjamin Li0f8ab892007-02-26 11:06:40 -08002794 ql_free_large_buffers(qdev);
2795 return -ENOMEM;
2796 }
2797
FUJITA Tomonori87196eb2010-04-12 14:32:13 +00002798 dma_unmap_addr_set(lrg_buf_cb, mapaddr, map);
2799 dma_unmap_len_set(lrg_buf_cb, maplen,
Ron Mercer5a4faa872006-07-25 00:40:21 -07002800 qdev->lrg_buffer_len -
2801 QL_HEADER_SPACE);
2802 lrg_buf_cb->buf_phy_addr_low =
2803 cpu_to_le32(LS_64BITS(map));
2804 lrg_buf_cb->buf_phy_addr_high =
2805 cpu_to_le32(MS_64BITS(map));
2806 }
2807 }
2808 return 0;
2809}
2810
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002811static void ql_free_send_free_list(struct ql3_adapter *qdev)
2812{
2813 struct ql_tx_buf_cb *tx_cb;
2814 int i;
2815
2816 tx_cb = &qdev->tx_buf[0];
2817 for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
Joe Perchesd7f61772010-07-22 15:36:17 +00002818 kfree(tx_cb->oal);
2819 tx_cb->oal = NULL;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002820 tx_cb++;
2821 }
2822}
2823
2824static int ql_create_send_free_list(struct ql3_adapter *qdev)
Ron Mercer5a4faa872006-07-25 00:40:21 -07002825{
2826 struct ql_tx_buf_cb *tx_cb;
2827 int i;
Joe Perchesd7f61772010-07-22 15:36:17 +00002828 struct ob_mac_iocb_req *req_q_curr = qdev->req_q_virt_addr;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002829
2830 /* Create free list of transmit buffers */
2831 for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002832
Ron Mercer5a4faa872006-07-25 00:40:21 -07002833 tx_cb = &qdev->tx_buf[i];
2834 tx_cb->skb = NULL;
2835 tx_cb->queue_entry = req_q_curr;
2836 req_q_curr++;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002837 tx_cb->oal = kmalloc(512, GFP_KERNEL);
2838 if (tx_cb->oal == NULL)
Santosh Nayak6975f4c2012-03-02 05:09:39 +00002839 return -ENOMEM;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002840 }
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002841 return 0;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002842}
2843
2844static int ql_alloc_mem_resources(struct ql3_adapter *qdev)
2845{
Ron Mercer1357bfc2007-02-26 11:06:37 -08002846 if (qdev->ndev->mtu == NORMAL_MTU_SIZE) {
2847 qdev->num_lbufq_entries = NUM_LBUFQ_ENTRIES;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002848 qdev->lrg_buffer_len = NORMAL_MTU_SIZE;
Joe Perchesd7f61772010-07-22 15:36:17 +00002849 } else if (qdev->ndev->mtu == JUMBO_MTU_SIZE) {
Ron Mercer1357bfc2007-02-26 11:06:37 -08002850 /*
2851 * Bigger buffers, so less of them.
2852 */
2853 qdev->num_lbufq_entries = JUMBO_NUM_LBUFQ_ENTRIES;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002854 qdev->lrg_buffer_len = JUMBO_MTU_SIZE;
2855 } else {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002856 netdev_err(qdev->ndev, "Invalid mtu size: %d. Only %d and %d are accepted.\n",
2857 qdev->ndev->mtu, NORMAL_MTU_SIZE, JUMBO_MTU_SIZE);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002858 return -ENOMEM;
2859 }
Joe Perchesd7f61772010-07-22 15:36:17 +00002860 qdev->num_large_buffers =
2861 qdev->num_lbufq_entries * QL_ADDR_ELE_PER_BUFQ_ENTRY;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002862 qdev->lrg_buffer_len += VLAN_ETH_HLEN + VLAN_ID_LEN + QL_HEADER_SPACE;
2863 qdev->max_frame_size =
Joe Perchesd7f61772010-07-22 15:36:17 +00002864 (qdev->lrg_buffer_len - QL_HEADER_SPACE) + ETHERNET_CRC_SIZE;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002865
2866 /*
2867 * First allocate a page of shared memory and use it for shadow
2868 * locations of Network Request Queue Consumer Address Register and
2869 * Network Completion Queue Producer Index Register
2870 */
2871 qdev->shadow_reg_virt_addr =
Joe Perchesd7f61772010-07-22 15:36:17 +00002872 pci_alloc_consistent(qdev->pdev,
2873 PAGE_SIZE, &qdev->shadow_reg_phy_addr);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002874
2875 if (qdev->shadow_reg_virt_addr != NULL) {
Joe Perches43d620c2011-06-16 19:08:06 +00002876 qdev->preq_consumer_index = qdev->shadow_reg_virt_addr;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002877 qdev->req_consumer_index_phy_addr_high =
Joe Perchesd7f61772010-07-22 15:36:17 +00002878 MS_64BITS(qdev->shadow_reg_phy_addr);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002879 qdev->req_consumer_index_phy_addr_low =
Joe Perchesd7f61772010-07-22 15:36:17 +00002880 LS_64BITS(qdev->shadow_reg_phy_addr);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002881
2882 qdev->prsp_producer_index =
Joe Perchesd7f61772010-07-22 15:36:17 +00002883 (__le32 *) (((u8 *) qdev->preq_consumer_index) + 8);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002884 qdev->rsp_producer_index_phy_addr_high =
Joe Perchesd7f61772010-07-22 15:36:17 +00002885 qdev->req_consumer_index_phy_addr_high;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002886 qdev->rsp_producer_index_phy_addr_low =
Joe Perchesd7f61772010-07-22 15:36:17 +00002887 qdev->req_consumer_index_phy_addr_low + 8;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002888 } else {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002889 netdev_err(qdev->ndev, "shadowReg Alloc failed\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07002890 return -ENOMEM;
2891 }
2892
2893 if (ql_alloc_net_req_rsp_queues(qdev) != 0) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002894 netdev_err(qdev->ndev, "ql_alloc_net_req_rsp_queues failed\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07002895 goto err_req_rsp;
2896 }
2897
2898 if (ql_alloc_buffer_queues(qdev) != 0) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002899 netdev_err(qdev->ndev, "ql_alloc_buffer_queues failed\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07002900 goto err_buffer_queues;
2901 }
2902
2903 if (ql_alloc_small_buffers(qdev) != 0) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002904 netdev_err(qdev->ndev, "ql_alloc_small_buffers failed\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07002905 goto err_small_buffers;
2906 }
2907
2908 if (ql_alloc_large_buffers(qdev) != 0) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00002909 netdev_err(qdev->ndev, "ql_alloc_large_buffers failed\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07002910 goto err_small_buffers;
2911 }
2912
2913 /* Initialize the large buffer queue. */
2914 ql_init_large_buffers(qdev);
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002915 if (ql_create_send_free_list(qdev))
2916 goto err_free_list;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002917
2918 qdev->rsp_current = qdev->rsp_q_virt_addr;
2919
2920 return 0;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002921err_free_list:
2922 ql_free_send_free_list(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002923err_small_buffers:
2924 ql_free_buffer_queues(qdev);
2925err_buffer_queues:
2926 ql_free_net_req_rsp_queues(qdev);
2927err_req_rsp:
2928 pci_free_consistent(qdev->pdev,
2929 PAGE_SIZE,
2930 qdev->shadow_reg_virt_addr,
2931 qdev->shadow_reg_phy_addr);
2932
2933 return -ENOMEM;
2934}
2935
2936static void ql_free_mem_resources(struct ql3_adapter *qdev)
2937{
Ron Mercerbd36b0a2007-01-03 16:26:08 -08002938 ql_free_send_free_list(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07002939 ql_free_large_buffers(qdev);
2940 ql_free_small_buffers(qdev);
2941 ql_free_buffer_queues(qdev);
2942 ql_free_net_req_rsp_queues(qdev);
2943 if (qdev->shadow_reg_virt_addr != NULL) {
2944 pci_free_consistent(qdev->pdev,
2945 PAGE_SIZE,
2946 qdev->shadow_reg_virt_addr,
2947 qdev->shadow_reg_phy_addr);
2948 qdev->shadow_reg_virt_addr = NULL;
2949 }
2950}
2951
2952static int ql_init_misc_registers(struct ql3_adapter *qdev)
2953{
Al Viroee111d12006-09-25 02:53:53 +01002954 struct ql3xxx_local_ram_registers __iomem *local_ram =
2955 (void __iomem *)qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07002956
Joe Perchesd7f61772010-07-22 15:36:17 +00002957 if (ql_sem_spinlock(qdev, QL_DDR_RAM_SEM_MASK,
Ron Mercer5a4faa872006-07-25 00:40:21 -07002958 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
2959 2) << 4))
2960 return -1;
2961
2962 ql_write_page2_reg(qdev,
2963 &local_ram->bufletSize, qdev->nvram_data.bufletSize);
2964
2965 ql_write_page2_reg(qdev,
2966 &local_ram->maxBufletCount,
2967 qdev->nvram_data.bufletCount);
2968
2969 ql_write_page2_reg(qdev,
2970 &local_ram->freeBufletThresholdLow,
2971 (qdev->nvram_data.tcpWindowThreshold25 << 16) |
2972 (qdev->nvram_data.tcpWindowThreshold0));
2973
2974 ql_write_page2_reg(qdev,
2975 &local_ram->freeBufletThresholdHigh,
2976 qdev->nvram_data.tcpWindowThreshold50);
2977
2978 ql_write_page2_reg(qdev,
2979 &local_ram->ipHashTableBase,
2980 (qdev->nvram_data.ipHashTableBaseHi << 16) |
2981 qdev->nvram_data.ipHashTableBaseLo);
2982 ql_write_page2_reg(qdev,
2983 &local_ram->ipHashTableCount,
2984 qdev->nvram_data.ipHashTableSize);
2985 ql_write_page2_reg(qdev,
2986 &local_ram->tcpHashTableBase,
2987 (qdev->nvram_data.tcpHashTableBaseHi << 16) |
2988 qdev->nvram_data.tcpHashTableBaseLo);
2989 ql_write_page2_reg(qdev,
2990 &local_ram->tcpHashTableCount,
2991 qdev->nvram_data.tcpHashTableSize);
2992 ql_write_page2_reg(qdev,
2993 &local_ram->ncbBase,
2994 (qdev->nvram_data.ncbTableBaseHi << 16) |
2995 qdev->nvram_data.ncbTableBaseLo);
2996 ql_write_page2_reg(qdev,
2997 &local_ram->maxNcbCount,
2998 qdev->nvram_data.ncbTableSize);
2999 ql_write_page2_reg(qdev,
3000 &local_ram->drbBase,
3001 (qdev->nvram_data.drbTableBaseHi << 16) |
3002 qdev->nvram_data.drbTableBaseLo);
3003 ql_write_page2_reg(qdev,
3004 &local_ram->maxDrbCount,
3005 qdev->nvram_data.drbTableSize);
3006 ql_sem_unlock(qdev, QL_DDR_RAM_SEM_MASK);
3007 return 0;
3008}
3009
3010static int ql_adapter_initialize(struct ql3_adapter *qdev)
3011{
3012 u32 value;
Joe Perchesd7f61772010-07-22 15:36:17 +00003013 struct ql3xxx_port_registers __iomem *port_regs =
3014 qdev->mem_map_registers;
stephen hemminger6f2e1542011-02-23 07:54:27 +00003015 __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003016 struct ql3xxx_host_memory_registers __iomem *hmem_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00003017 (void __iomem *)port_regs;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003018 u32 delay = 10;
3019 int status = 0;
3020
Joe Perchesd7f61772010-07-22 15:36:17 +00003021 if (ql_mii_setup(qdev))
Ron Mercer5a4faa872006-07-25 00:40:21 -07003022 return -1;
3023
3024 /* Bring out PHY out of reset */
Joe Perchesd7f61772010-07-22 15:36:17 +00003025 ql_write_common_reg(qdev, spir,
Ron Mercer5a4faa872006-07-25 00:40:21 -07003026 (ISP_SERIAL_PORT_IF_WE |
3027 (ISP_SERIAL_PORT_IF_WE << 16)));
Ron Mercere5a67372009-06-23 09:00:01 +00003028 /* Give the PHY time to come out of reset. */
3029 mdelay(100);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003030 qdev->port_link_state = LS_DOWN;
3031 netif_carrier_off(qdev->ndev);
3032
3033 /* V2 chip fix for ARS-39168. */
Joe Perchesd7f61772010-07-22 15:36:17 +00003034 ql_write_common_reg(qdev, spir,
Ron Mercer5a4faa872006-07-25 00:40:21 -07003035 (ISP_SERIAL_PORT_IF_SDE |
3036 (ISP_SERIAL_PORT_IF_SDE << 16)));
3037
3038 /* Request Queue Registers */
Joe Perchesd7f61772010-07-22 15:36:17 +00003039 *((u32 *)(qdev->preq_consumer_index)) = 0;
3040 atomic_set(&qdev->tx_count, NUM_REQ_Q_ENTRIES);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003041 qdev->req_producer_index = 0;
3042
3043 ql_write_page1_reg(qdev,
3044 &hmem_regs->reqConsumerIndexAddrHigh,
3045 qdev->req_consumer_index_phy_addr_high);
3046 ql_write_page1_reg(qdev,
3047 &hmem_regs->reqConsumerIndexAddrLow,
3048 qdev->req_consumer_index_phy_addr_low);
3049
3050 ql_write_page1_reg(qdev,
3051 &hmem_regs->reqBaseAddrHigh,
3052 MS_64BITS(qdev->req_q_phy_addr));
3053 ql_write_page1_reg(qdev,
3054 &hmem_regs->reqBaseAddrLow,
3055 LS_64BITS(qdev->req_q_phy_addr));
3056 ql_write_page1_reg(qdev, &hmem_regs->reqLength, NUM_REQ_Q_ENTRIES);
3057
3058 /* Response Queue Registers */
Al Viro804d8542007-12-22 19:44:29 +00003059 *((__le16 *) (qdev->prsp_producer_index)) = 0;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003060 qdev->rsp_consumer_index = 0;
3061 qdev->rsp_current = qdev->rsp_q_virt_addr;
3062
3063 ql_write_page1_reg(qdev,
3064 &hmem_regs->rspProducerIndexAddrHigh,
3065 qdev->rsp_producer_index_phy_addr_high);
3066
3067 ql_write_page1_reg(qdev,
3068 &hmem_regs->rspProducerIndexAddrLow,
3069 qdev->rsp_producer_index_phy_addr_low);
3070
3071 ql_write_page1_reg(qdev,
3072 &hmem_regs->rspBaseAddrHigh,
3073 MS_64BITS(qdev->rsp_q_phy_addr));
3074
3075 ql_write_page1_reg(qdev,
3076 &hmem_regs->rspBaseAddrLow,
3077 LS_64BITS(qdev->rsp_q_phy_addr));
3078
3079 ql_write_page1_reg(qdev, &hmem_regs->rspLength, NUM_RSP_Q_ENTRIES);
3080
3081 /* Large Buffer Queue */
3082 ql_write_page1_reg(qdev,
3083 &hmem_regs->rxLargeQBaseAddrHigh,
3084 MS_64BITS(qdev->lrg_buf_q_phy_addr));
3085
3086 ql_write_page1_reg(qdev,
3087 &hmem_regs->rxLargeQBaseAddrLow,
3088 LS_64BITS(qdev->lrg_buf_q_phy_addr));
3089
Joe Perchesd7f61772010-07-22 15:36:17 +00003090 ql_write_page1_reg(qdev,
3091 &hmem_regs->rxLargeQLength,
3092 qdev->num_lbufq_entries);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003093
3094 ql_write_page1_reg(qdev,
3095 &hmem_regs->rxLargeBufferLength,
3096 qdev->lrg_buffer_len);
3097
3098 /* Small Buffer Queue */
3099 ql_write_page1_reg(qdev,
3100 &hmem_regs->rxSmallQBaseAddrHigh,
3101 MS_64BITS(qdev->small_buf_q_phy_addr));
3102
3103 ql_write_page1_reg(qdev,
3104 &hmem_regs->rxSmallQBaseAddrLow,
3105 LS_64BITS(qdev->small_buf_q_phy_addr));
3106
3107 ql_write_page1_reg(qdev, &hmem_regs->rxSmallQLength, NUM_SBUFQ_ENTRIES);
3108 ql_write_page1_reg(qdev,
3109 &hmem_regs->rxSmallBufferLength,
3110 QL_SMALL_BUFFER_SIZE);
3111
3112 qdev->small_buf_q_producer_index = NUM_SBUFQ_ENTRIES - 1;
3113 qdev->small_buf_release_cnt = 8;
Ron Mercer1357bfc2007-02-26 11:06:37 -08003114 qdev->lrg_buf_q_producer_index = qdev->num_lbufq_entries - 1;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003115 qdev->lrg_buf_release_cnt = 8;
Joe Perches43d620c2011-06-16 19:08:06 +00003116 qdev->lrg_buf_next_free = qdev->lrg_buf_q_virt_addr;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003117 qdev->small_buf_index = 0;
3118 qdev->lrg_buf_index = 0;
3119 qdev->lrg_buf_free_count = 0;
3120 qdev->lrg_buf_free_head = NULL;
3121 qdev->lrg_buf_free_tail = NULL;
3122
3123 ql_write_common_reg(qdev,
Al Viroee111d12006-09-25 02:53:53 +01003124 &port_regs->CommonRegs.
Ron Mercer5a4faa872006-07-25 00:40:21 -07003125 rxSmallQProducerIndex,
3126 qdev->small_buf_q_producer_index);
3127 ql_write_common_reg(qdev,
Al Viroee111d12006-09-25 02:53:53 +01003128 &port_regs->CommonRegs.
Ron Mercer5a4faa872006-07-25 00:40:21 -07003129 rxLargeQProducerIndex,
3130 qdev->lrg_buf_q_producer_index);
3131
3132 /*
3133 * Find out if the chip has already been initialized. If it has, then
3134 * we skip some of the initialization.
3135 */
3136 clear_bit(QL_LINK_MASTER, &qdev->flags);
3137 value = ql_read_page0_reg(qdev, &port_regs->portStatus);
3138 if ((value & PORT_STATUS_IC) == 0) {
3139
3140 /* Chip has not been configured yet, so let it rip. */
Joe Perchesd7f61772010-07-22 15:36:17 +00003141 if (ql_init_misc_registers(qdev)) {
Ron Mercer5a4faa872006-07-25 00:40:21 -07003142 status = -1;
3143 goto out;
3144 }
3145
Ron Mercer5a4faa872006-07-25 00:40:21 -07003146 value = qdev->nvram_data.tcpMaxWindowSize;
3147 ql_write_page0_reg(qdev, &port_regs->tcpMaxWindow, value);
3148
3149 value = (0xFFFF << 16) | qdev->nvram_data.extHwConfig;
3150
Joe Perchesd7f61772010-07-22 15:36:17 +00003151 if (ql_sem_spinlock(qdev, QL_FLASH_SEM_MASK,
Ron Mercer5a4faa872006-07-25 00:40:21 -07003152 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index)
3153 * 2) << 13)) {
3154 status = -1;
3155 goto out;
3156 }
3157 ql_write_page0_reg(qdev, &port_regs->ExternalHWConfig, value);
3158 ql_write_page0_reg(qdev, &port_regs->InternalChipConfig,
3159 (((INTERNAL_CHIP_SD | INTERNAL_CHIP_WE) <<
3160 16) | (INTERNAL_CHIP_SD |
3161 INTERNAL_CHIP_WE)));
3162 ql_sem_unlock(qdev, QL_FLASH_SEM_MASK);
3163 }
3164
Ron Mercerb3b15142007-03-26 13:43:00 -07003165 if (qdev->mac_index)
3166 ql_write_page0_reg(qdev,
3167 &port_regs->mac1MaxFrameLengthReg,
3168 qdev->max_frame_size);
3169 else
3170 ql_write_page0_reg(qdev,
3171 &port_regs->mac0MaxFrameLengthReg,
3172 qdev->max_frame_size);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003173
Joe Perchesd7f61772010-07-22 15:36:17 +00003174 if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
Ron Mercer5a4faa872006-07-25 00:40:21 -07003175 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
3176 2) << 7)) {
3177 status = -1;
3178 goto out;
3179 }
3180
Ron Mercer3efedf22007-03-26 12:43:52 -07003181 PHY_Setup(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003182 ql_init_scan_mode(qdev);
3183 ql_get_phy_owner(qdev);
3184
3185 /* Load the MAC Configuration */
3186
3187 /* Program lower 32 bits of the MAC address */
3188 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3189 (MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16));
3190 ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
3191 ((qdev->ndev->dev_addr[2] << 24)
3192 | (qdev->ndev->dev_addr[3] << 16)
3193 | (qdev->ndev->dev_addr[4] << 8)
3194 | qdev->ndev->dev_addr[5]));
3195
3196 /* Program top 16 bits of the MAC address */
3197 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3198 ((MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16) | 1));
3199 ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
3200 ((qdev->ndev->dev_addr[0] << 8)
3201 | qdev->ndev->dev_addr[1]));
3202
3203 /* Enable Primary MAC */
3204 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3205 ((MAC_ADDR_INDIRECT_PTR_REG_PE << 16) |
3206 MAC_ADDR_INDIRECT_PTR_REG_PE));
3207
3208 /* Clear Primary and Secondary IP addresses */
3209 ql_write_page0_reg(qdev, &port_regs->ipAddrIndexReg,
3210 ((IP_ADDR_INDEX_REG_MASK << 16) |
3211 (qdev->mac_index << 2)));
3212 ql_write_page0_reg(qdev, &port_regs->ipAddrDataReg, 0);
3213
3214 ql_write_page0_reg(qdev, &port_regs->ipAddrIndexReg,
3215 ((IP_ADDR_INDEX_REG_MASK << 16) |
3216 ((qdev->mac_index << 2) + 1)));
3217 ql_write_page0_reg(qdev, &port_regs->ipAddrDataReg, 0);
3218
3219 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
3220
3221 /* Indicate Configuration Complete */
3222 ql_write_page0_reg(qdev,
3223 &port_regs->portControl,
3224 ((PORT_CONTROL_CC << 16) | PORT_CONTROL_CC));
3225
3226 do {
3227 value = ql_read_page0_reg(qdev, &port_regs->portStatus);
3228 if (value & PORT_STATUS_IC)
3229 break;
Santosh Nayak9d1dfc02012-03-02 05:09:05 +00003230 spin_unlock_irq(&qdev->hw_lock);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003231 msleep(500);
Santosh Nayak9d1dfc02012-03-02 05:09:05 +00003232 spin_lock_irq(&qdev->hw_lock);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003233 } while (--delay);
3234
3235 if (delay == 0) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003236 netdev_err(qdev->ndev, "Hw Initialization timeout\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003237 status = -1;
3238 goto out;
3239 }
3240
3241 /* Enable Ethernet Function */
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003242 if (qdev->device_id == QL3032_DEVICE_ID) {
3243 value =
3244 (QL3032_PORT_CONTROL_EF | QL3032_PORT_CONTROL_KIE |
Ron Mercerb3b15142007-03-26 13:43:00 -07003245 QL3032_PORT_CONTROL_EIv6 | QL3032_PORT_CONTROL_EIv4 |
3246 QL3032_PORT_CONTROL_ET);
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003247 ql_write_page0_reg(qdev, &port_regs->functionControl,
3248 ((value << 16) | value));
3249 } else {
3250 value =
3251 (PORT_CONTROL_EF | PORT_CONTROL_ET | PORT_CONTROL_EI |
3252 PORT_CONTROL_HH);
3253 ql_write_page0_reg(qdev, &port_regs->portControl,
3254 ((value << 16) | value));
3255 }
3256
Ron Mercer5a4faa872006-07-25 00:40:21 -07003257
3258out:
3259 return status;
3260}
3261
3262/*
3263 * Caller holds hw_lock.
3264 */
3265static int ql_adapter_reset(struct ql3_adapter *qdev)
3266{
Joe Perchesd7f61772010-07-22 15:36:17 +00003267 struct ql3xxx_port_registers __iomem *port_regs =
3268 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003269 int status = 0;
3270 u16 value;
3271 int max_wait_time;
3272
3273 set_bit(QL_RESET_ACTIVE, &qdev->flags);
3274 clear_bit(QL_RESET_DONE, &qdev->flags);
3275
3276 /*
3277 * Issue soft reset to chip.
3278 */
Joe Percheseddc5fb2010-07-22 12:33:31 +00003279 netdev_printk(KERN_DEBUG, qdev->ndev, "Issue soft reset to chip\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003280 ql_write_common_reg(qdev,
Al Viroee111d12006-09-25 02:53:53 +01003281 &port_regs->CommonRegs.ispControlStatus,
Ron Mercer5a4faa872006-07-25 00:40:21 -07003282 ((ISP_CONTROL_SR << 16) | ISP_CONTROL_SR));
3283
3284 /* Wait 3 seconds for reset to complete. */
Joe Percheseddc5fb2010-07-22 12:33:31 +00003285 netdev_printk(KERN_DEBUG, qdev->ndev,
3286 "Wait 10 milliseconds for reset to complete\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003287
3288 /* Wait until the firmware tells us the Soft Reset is done */
3289 max_wait_time = 5;
3290 do {
3291 value =
3292 ql_read_common_reg(qdev,
3293 &port_regs->CommonRegs.ispControlStatus);
3294 if ((value & ISP_CONTROL_SR) == 0)
3295 break;
3296
3297 ssleep(1);
3298 } while ((--max_wait_time));
3299
3300 /*
3301 * Also, make sure that the Network Reset Interrupt bit has been
3302 * cleared after the soft reset has taken place.
3303 */
3304 value =
3305 ql_read_common_reg(qdev, &port_regs->CommonRegs.ispControlStatus);
3306 if (value & ISP_CONTROL_RI) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003307 netdev_printk(KERN_DEBUG, qdev->ndev,
3308 "clearing RI after reset\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003309 ql_write_common_reg(qdev,
Al Viroee111d12006-09-25 02:53:53 +01003310 &port_regs->CommonRegs.
Ron Mercer5a4faa872006-07-25 00:40:21 -07003311 ispControlStatus,
3312 ((ISP_CONTROL_RI << 16) | ISP_CONTROL_RI));
3313 }
3314
3315 if (max_wait_time == 0) {
3316 /* Issue Force Soft Reset */
3317 ql_write_common_reg(qdev,
Al Viroee111d12006-09-25 02:53:53 +01003318 &port_regs->CommonRegs.
Ron Mercer5a4faa872006-07-25 00:40:21 -07003319 ispControlStatus,
3320 ((ISP_CONTROL_FSR << 16) |
3321 ISP_CONTROL_FSR));
3322 /*
3323 * Wait until the firmware tells us the Force Soft Reset is
3324 * done
3325 */
3326 max_wait_time = 5;
3327 do {
Joe Perchesd7f61772010-07-22 15:36:17 +00003328 value = ql_read_common_reg(qdev,
3329 &port_regs->CommonRegs.
3330 ispControlStatus);
3331 if ((value & ISP_CONTROL_FSR) == 0)
Ron Mercer5a4faa872006-07-25 00:40:21 -07003332 break;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003333 ssleep(1);
3334 } while ((--max_wait_time));
3335 }
3336 if (max_wait_time == 0)
3337 status = 1;
3338
3339 clear_bit(QL_RESET_ACTIVE, &qdev->flags);
3340 set_bit(QL_RESET_DONE, &qdev->flags);
3341 return status;
3342}
3343
3344static void ql_set_mac_info(struct ql3_adapter *qdev)
3345{
Joe Perchesd7f61772010-07-22 15:36:17 +00003346 struct ql3xxx_port_registers __iomem *port_regs =
3347 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003348 u32 value, port_status;
3349 u8 func_number;
3350
3351 /* Get the function number */
3352 value =
3353 ql_read_common_reg_l(qdev, &port_regs->CommonRegs.ispControlStatus);
3354 func_number = (u8) ((value >> 4) & OPCODE_FUNC_ID_MASK);
3355 port_status = ql_read_page0_reg(qdev, &port_regs->portStatus);
3356 switch (value & ISP_CONTROL_FN_MASK) {
3357 case ISP_CONTROL_FN0_NET:
3358 qdev->mac_index = 0;
3359 qdev->mac_ob_opcode = OUTBOUND_MAC_IOCB | func_number;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003360 qdev->mb_bit_mask = FN0_MA_BITS_MASK;
3361 qdev->PHYAddr = PORT0_PHY_ADDRESS;
3362 if (port_status & PORT_STATUS_SM0)
Joe Perchesd7f61772010-07-22 15:36:17 +00003363 set_bit(QL_LINK_OPTICAL, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003364 else
Joe Perchesd7f61772010-07-22 15:36:17 +00003365 clear_bit(QL_LINK_OPTICAL, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003366 break;
3367
3368 case ISP_CONTROL_FN1_NET:
3369 qdev->mac_index = 1;
3370 qdev->mac_ob_opcode = OUTBOUND_MAC_IOCB | func_number;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003371 qdev->mb_bit_mask = FN1_MA_BITS_MASK;
3372 qdev->PHYAddr = PORT1_PHY_ADDRESS;
3373 if (port_status & PORT_STATUS_SM1)
Joe Perchesd7f61772010-07-22 15:36:17 +00003374 set_bit(QL_LINK_OPTICAL, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003375 else
Joe Perchesd7f61772010-07-22 15:36:17 +00003376 clear_bit(QL_LINK_OPTICAL, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003377 break;
3378
3379 case ISP_CONTROL_FN0_SCSI:
3380 case ISP_CONTROL_FN1_SCSI:
3381 default:
Joe Percheseddc5fb2010-07-22 12:33:31 +00003382 netdev_printk(KERN_DEBUG, qdev->ndev,
3383 "Invalid function number, ispControlStatus = 0x%x\n",
3384 value);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003385 break;
3386 }
Al Viro804d8542007-12-22 19:44:29 +00003387 qdev->numPorts = qdev->nvram_data.version_and_numPorts >> 8;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003388}
3389
3390static void ql_display_dev_info(struct net_device *ndev)
3391{
Joe Perches4dd15182010-11-15 11:12:27 +00003392 struct ql3_adapter *qdev = netdev_priv(ndev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003393 struct pci_dev *pdev = qdev->pdev;
3394
Joe Percheseddc5fb2010-07-22 12:33:31 +00003395 netdev_info(ndev,
3396 "%s Adapter %d RevisionID %d found %s on PCI slot %d\n",
3397 DRV_NAME, qdev->index, qdev->chip_rev_id,
Joe Perchesd7f61772010-07-22 15:36:17 +00003398 qdev->device_id == QL3032_DEVICE_ID ? "QLA3032" : "QLA3022",
Joe Percheseddc5fb2010-07-22 12:33:31 +00003399 qdev->pci_slot);
3400 netdev_info(ndev, "%s Interface\n",
3401 test_bit(QL_LINK_OPTICAL, &qdev->flags) ? "OPTICAL" : "COPPER");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003402
3403 /*
3404 * Print PCI bus width/type.
3405 */
Joe Percheseddc5fb2010-07-22 12:33:31 +00003406 netdev_info(ndev, "Bus interface is %s %s\n",
3407 ((qdev->pci_width == 64) ? "64-bit" : "32-bit"),
3408 ((qdev->pci_x) ? "PCI-X" : "PCI"));
Ron Mercer5a4faa872006-07-25 00:40:21 -07003409
Joe Percheseddc5fb2010-07-22 12:33:31 +00003410 netdev_info(ndev, "mem IO base address adjusted = 0x%p\n",
3411 qdev->mem_map_registers);
3412 netdev_info(ndev, "Interrupt number = %d\n", pdev->irq);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003413
Joe Percheseddc5fb2010-07-22 12:33:31 +00003414 netif_info(qdev, probe, ndev, "MAC address %pM\n", ndev->dev_addr);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003415}
3416
3417static int ql_adapter_down(struct ql3_adapter *qdev, int do_reset)
3418{
3419 struct net_device *ndev = qdev->ndev;
3420 int retval = 0;
3421
3422 netif_stop_queue(ndev);
3423 netif_carrier_off(ndev);
3424
Joe Perchesd7f61772010-07-22 15:36:17 +00003425 clear_bit(QL_ADAPTER_UP, &qdev->flags);
3426 clear_bit(QL_LINK_MASTER, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003427
3428 ql_disable_interrupts(qdev);
3429
3430 free_irq(qdev->pdev->irq, ndev);
3431
Joe Perchesd7f61772010-07-22 15:36:17 +00003432 if (qdev->msi && test_bit(QL_MSI_ENABLED, &qdev->flags)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003433 netdev_info(qdev->ndev, "calling pci_disable_msi()\n");
Joe Perchesd7f61772010-07-22 15:36:17 +00003434 clear_bit(QL_MSI_ENABLED, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003435 pci_disable_msi(qdev->pdev);
3436 }
3437
3438 del_timer_sync(&qdev->adapter_timer);
3439
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003440 napi_disable(&qdev->napi);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003441
3442 if (do_reset) {
3443 int soft_reset;
3444 unsigned long hw_flags;
3445
3446 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
3447 if (ql_wait_for_drvr_lock(qdev)) {
Joe Perchesd7f61772010-07-22 15:36:17 +00003448 soft_reset = ql_adapter_reset(qdev);
3449 if (soft_reset) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003450 netdev_err(ndev, "ql_adapter_reset(%d) FAILED!\n",
3451 qdev->index);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003452 }
Joe Percheseddc5fb2010-07-22 12:33:31 +00003453 netdev_err(ndev,
3454 "Releasing driver lock via chip reset\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003455 } else {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003456 netdev_err(ndev,
3457 "Could not acquire driver lock to do reset!\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003458 retval = -1;
3459 }
3460 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
3461 }
3462 ql_free_mem_resources(qdev);
3463 return retval;
3464}
3465
3466static int ql_adapter_up(struct ql3_adapter *qdev)
3467{
3468 struct net_device *ndev = qdev->ndev;
3469 int err;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00003470 unsigned long irq_flags = IRQF_SHARED;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003471 unsigned long hw_flags;
3472
3473 if (ql_alloc_mem_resources(qdev)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003474 netdev_err(ndev, "Unable to allocate buffers\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003475 return -ENOMEM;
3476 }
3477
3478 if (qdev->msi) {
3479 if (pci_enable_msi(qdev->pdev)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003480 netdev_err(ndev,
3481 "User requested MSI, but MSI failed to initialize. Continuing without MSI.\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003482 qdev->msi = 0;
3483 } else {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003484 netdev_info(ndev, "MSI Enabled...\n");
Joe Perchesd7f61772010-07-22 15:36:17 +00003485 set_bit(QL_MSI_ENABLED, &qdev->flags);
Thomas Gleixner38515e92007-02-14 00:33:16 -08003486 irq_flags &= ~IRQF_SHARED;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003487 }
3488 }
3489
Joe Perchesd7f61772010-07-22 15:36:17 +00003490 err = request_irq(qdev->pdev->irq, ql3xxx_isr,
3491 irq_flags, ndev->name, ndev);
3492 if (err) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003493 netdev_err(ndev,
Joe Perchesd7f61772010-07-22 15:36:17 +00003494 "Failed to reserve interrupt %d - already in use\n",
Joe Percheseddc5fb2010-07-22 12:33:31 +00003495 qdev->pdev->irq);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003496 goto err_irq;
3497 }
3498
3499 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
3500
Joe Perchesd7f61772010-07-22 15:36:17 +00003501 err = ql_wait_for_drvr_lock(qdev);
3502 if (err) {
3503 err = ql_adapter_initialize(qdev);
3504 if (err) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003505 netdev_err(ndev, "Unable to initialize adapter\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003506 goto err_init;
3507 }
Joe Percheseddc5fb2010-07-22 12:33:31 +00003508 netdev_err(ndev, "Releasing driver lock\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003509 ql_sem_unlock(qdev, QL_DRVR_SEM_MASK);
3510 } else {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003511 netdev_err(ndev, "Could not acquire driver lock\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003512 goto err_lock;
3513 }
3514
3515 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
3516
Joe Perchesd7f61772010-07-22 15:36:17 +00003517 set_bit(QL_ADAPTER_UP, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003518
3519 mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);
3520
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003521 napi_enable(&qdev->napi);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003522 ql_enable_interrupts(qdev);
3523 return 0;
3524
3525err_init:
3526 ql_sem_unlock(qdev, QL_DRVR_SEM_MASK);
3527err_lock:
Benjamin Li04f10772007-02-26 11:06:35 -08003528 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003529 free_irq(qdev->pdev->irq, ndev);
3530err_irq:
Joe Perchesd7f61772010-07-22 15:36:17 +00003531 if (qdev->msi && test_bit(QL_MSI_ENABLED, &qdev->flags)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003532 netdev_info(ndev, "calling pci_disable_msi()\n");
Joe Perchesd7f61772010-07-22 15:36:17 +00003533 clear_bit(QL_MSI_ENABLED, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003534 pci_disable_msi(qdev->pdev);
3535 }
3536 return err;
3537}
3538
3539static int ql_cycle_adapter(struct ql3_adapter *qdev, int reset)
3540{
Joe Perchesd7f61772010-07-22 15:36:17 +00003541 if (ql_adapter_down(qdev, reset) || ql_adapter_up(qdev)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003542 netdev_err(qdev->ndev,
3543 "Driver up/down cycle failed, closing device\n");
Ben Hutchingsc81ec802008-05-06 19:36:26 +01003544 rtnl_lock();
Ron Mercer5a4faa872006-07-25 00:40:21 -07003545 dev_close(qdev->ndev);
Ben Hutchingsc81ec802008-05-06 19:36:26 +01003546 rtnl_unlock();
Ron Mercer5a4faa872006-07-25 00:40:21 -07003547 return -1;
3548 }
3549 return 0;
3550}
3551
3552static int ql3xxx_close(struct net_device *ndev)
3553{
3554 struct ql3_adapter *qdev = netdev_priv(ndev);
3555
3556 /*
3557 * Wait for device to recover from a reset.
3558 * (Rarely happens, but possible.)
3559 */
Joe Perchesd7f61772010-07-22 15:36:17 +00003560 while (!test_bit(QL_ADAPTER_UP, &qdev->flags))
Ron Mercer5a4faa872006-07-25 00:40:21 -07003561 msleep(50);
3562
Joe Perchesd7f61772010-07-22 15:36:17 +00003563 ql_adapter_down(qdev, QL_DO_RESET);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003564 return 0;
3565}
3566
3567static int ql3xxx_open(struct net_device *ndev)
3568{
3569 struct ql3_adapter *qdev = netdev_priv(ndev);
Joe Perchesd7f61772010-07-22 15:36:17 +00003570 return ql_adapter_up(qdev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003571}
3572
Ron Mercer5a4faa872006-07-25 00:40:21 -07003573static int ql3xxx_set_mac_address(struct net_device *ndev, void *p)
3574{
Joe Perches4dd15182010-11-15 11:12:27 +00003575 struct ql3_adapter *qdev = netdev_priv(ndev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003576 struct ql3xxx_port_registers __iomem *port_regs =
Joe Perchesd7f61772010-07-22 15:36:17 +00003577 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003578 struct sockaddr *addr = p;
3579 unsigned long hw_flags;
3580
3581 if (netif_running(ndev))
3582 return -EBUSY;
3583
3584 if (!is_valid_ether_addr(addr->sa_data))
3585 return -EADDRNOTAVAIL;
3586
3587 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
3588
3589 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
3590 /* Program lower 32 bits of the MAC address */
3591 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3592 (MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16));
3593 ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
3594 ((ndev->dev_addr[2] << 24) | (ndev->
3595 dev_addr[3] << 16) |
3596 (ndev->dev_addr[4] << 8) | ndev->dev_addr[5]));
3597
3598 /* Program top 16 bits of the MAC address */
3599 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3600 ((MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16) | 1));
3601 ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
3602 ((ndev->dev_addr[0] << 8) | ndev->dev_addr[1]));
3603 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
3604
3605 return 0;
3606}
3607
3608static void ql3xxx_tx_timeout(struct net_device *ndev)
3609{
Joe Perches4dd15182010-11-15 11:12:27 +00003610 struct ql3_adapter *qdev = netdev_priv(ndev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003611
Joe Percheseddc5fb2010-07-22 12:33:31 +00003612 netdev_err(ndev, "Resetting...\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003613 /*
3614 * Stop the queues, we've got a problem.
3615 */
3616 netif_stop_queue(ndev);
3617
3618 /*
3619 * Wake up the worker to process this event.
3620 */
David Howellsc4028952006-11-22 14:57:56 +00003621 queue_delayed_work(qdev->workqueue, &qdev->tx_timeout_work, 0);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003622}
3623
David Howellsc4028952006-11-22 14:57:56 +00003624static void ql_reset_work(struct work_struct *work)
Ron Mercer5a4faa872006-07-25 00:40:21 -07003625{
David Howellsc4028952006-11-22 14:57:56 +00003626 struct ql3_adapter *qdev =
3627 container_of(work, struct ql3_adapter, reset_work.work);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003628 struct net_device *ndev = qdev->ndev;
3629 u32 value;
3630 struct ql_tx_buf_cb *tx_cb;
3631 int max_wait_time, i;
Joe Perchesd7f61772010-07-22 15:36:17 +00003632 struct ql3xxx_port_registers __iomem *port_regs =
3633 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003634 unsigned long hw_flags;
3635
Joe Perchesd7f61772010-07-22 15:36:17 +00003636 if (test_bit((QL_RESET_PER_SCSI | QL_RESET_START), &qdev->flags)) {
3637 clear_bit(QL_LINK_MASTER, &qdev->flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003638
3639 /*
3640 * Loop through the active list and return the skb.
3641 */
3642 for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003643 int j;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003644 tx_cb = &qdev->tx_buf[i];
3645 if (tx_cb->skb) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003646 netdev_printk(KERN_DEBUG, ndev,
3647 "Freeing lost SKB\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003648 pci_unmap_single(qdev->pdev,
Joe Perchesd7f61772010-07-22 15:36:17 +00003649 dma_unmap_addr(&tx_cb->map[0],
3650 mapaddr),
FUJITA Tomonori87196eb2010-04-12 14:32:13 +00003651 dma_unmap_len(&tx_cb->map[0], maplen),
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003652 PCI_DMA_TODEVICE);
Joe Perchesd7f61772010-07-22 15:36:17 +00003653 for (j = 1; j < tx_cb->seg_count; j++) {
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003654 pci_unmap_page(qdev->pdev,
Joe Perchesd7f61772010-07-22 15:36:17 +00003655 dma_unmap_addr(&tx_cb->map[j],
3656 mapaddr),
3657 dma_unmap_len(&tx_cb->map[j],
3658 maplen),
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003659 PCI_DMA_TODEVICE);
3660 }
Ron Mercer5a4faa872006-07-25 00:40:21 -07003661 dev_kfree_skb(tx_cb->skb);
3662 tx_cb->skb = NULL;
3663 }
3664 }
3665
Joe Percheseddc5fb2010-07-22 12:33:31 +00003666 netdev_err(ndev, "Clearing NRI after reset\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003667 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
3668 ql_write_common_reg(qdev,
3669 &port_regs->CommonRegs.
3670 ispControlStatus,
3671 ((ISP_CONTROL_RI << 16) | ISP_CONTROL_RI));
3672 /*
3673 * Wait the for Soft Reset to Complete.
3674 */
3675 max_wait_time = 10;
3676 do {
3677 value = ql_read_common_reg(qdev,
3678 &port_regs->CommonRegs.
3679
3680 ispControlStatus);
3681 if ((value & ISP_CONTROL_SR) == 0) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003682 netdev_printk(KERN_DEBUG, ndev,
3683 "reset completed\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003684 break;
3685 }
3686
3687 if (value & ISP_CONTROL_RI) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003688 netdev_printk(KERN_DEBUG, ndev,
3689 "clearing NRI after reset\n");
Ron Mercer5a4faa872006-07-25 00:40:21 -07003690 ql_write_common_reg(qdev,
Al Viroee111d12006-09-25 02:53:53 +01003691 &port_regs->
Ron Mercer5a4faa872006-07-25 00:40:21 -07003692 CommonRegs.
3693 ispControlStatus,
3694 ((ISP_CONTROL_RI <<
3695 16) | ISP_CONTROL_RI));
3696 }
3697
Jiri Slaby83b462c2009-06-20 01:20:30 -07003698 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003699 ssleep(1);
Jiri Slaby83b462c2009-06-20 01:20:30 -07003700 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003701 } while (--max_wait_time);
3702 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
3703
3704 if (value & ISP_CONTROL_SR) {
3705
3706 /*
3707 * Set the reset flags and clear the board again.
3708 * Nothing else to do...
3709 */
Joe Percheseddc5fb2010-07-22 12:33:31 +00003710 netdev_err(ndev,
3711 "Timed out waiting for reset to complete\n");
3712 netdev_err(ndev, "Do a reset\n");
Joe Perchesd7f61772010-07-22 15:36:17 +00003713 clear_bit(QL_RESET_PER_SCSI, &qdev->flags);
3714 clear_bit(QL_RESET_START, &qdev->flags);
3715 ql_cycle_adapter(qdev, QL_DO_RESET);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003716 return;
3717 }
3718
Joe Perchesd7f61772010-07-22 15:36:17 +00003719 clear_bit(QL_RESET_ACTIVE, &qdev->flags);
3720 clear_bit(QL_RESET_PER_SCSI, &qdev->flags);
3721 clear_bit(QL_RESET_START, &qdev->flags);
3722 ql_cycle_adapter(qdev, QL_NO_RESET);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003723 }
3724}
3725
David Howellsc4028952006-11-22 14:57:56 +00003726static void ql_tx_timeout_work(struct work_struct *work)
Ron Mercer5a4faa872006-07-25 00:40:21 -07003727{
David Howellsc4028952006-11-22 14:57:56 +00003728 struct ql3_adapter *qdev =
3729 container_of(work, struct ql3_adapter, tx_timeout_work.work);
3730
3731 ql_cycle_adapter(qdev, QL_DO_RESET);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003732}
3733
3734static void ql_get_board_info(struct ql3_adapter *qdev)
3735{
Joe Perchesd7f61772010-07-22 15:36:17 +00003736 struct ql3xxx_port_registers __iomem *port_regs =
3737 qdev->mem_map_registers;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003738 u32 value;
3739
3740 value = ql_read_page0_reg_l(qdev, &port_regs->portStatus);
3741
3742 qdev->chip_rev_id = ((value & PORT_STATUS_REV_ID_MASK) >> 12);
3743 if (value & PORT_STATUS_64)
3744 qdev->pci_width = 64;
3745 else
3746 qdev->pci_width = 32;
3747 if (value & PORT_STATUS_X)
3748 qdev->pci_x = 1;
3749 else
3750 qdev->pci_x = 0;
3751 qdev->pci_slot = (u8) PCI_SLOT(qdev->pdev->devfn);
3752}
3753
3754static void ql3xxx_timer(unsigned long ptr)
3755{
3756 struct ql3_adapter *qdev = (struct ql3_adapter *)ptr;
Ron Mercer3e23b7d2007-11-07 13:59:06 -08003757 queue_delayed_work(qdev->workqueue, &qdev->link_state_work, 0);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003758}
3759
Stephen Hemmingerda1c14a2008-11-21 17:36:58 -08003760static const struct net_device_ops ql3xxx_netdev_ops = {
3761 .ndo_open = ql3xxx_open,
3762 .ndo_start_xmit = ql3xxx_send,
3763 .ndo_stop = ql3xxx_close,
Stephen Hemmingerda1c14a2008-11-21 17:36:58 -08003764 .ndo_change_mtu = eth_change_mtu,
3765 .ndo_validate_addr = eth_validate_addr,
3766 .ndo_set_mac_address = ql3xxx_set_mac_address,
3767 .ndo_tx_timeout = ql3xxx_tx_timeout,
3768};
3769
Bill Pembertone3a582f2012-12-03 09:23:26 -05003770static int ql3xxx_probe(struct pci_dev *pdev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00003771 const struct pci_device_id *pci_entry)
Ron Mercer5a4faa872006-07-25 00:40:21 -07003772{
3773 struct net_device *ndev = NULL;
3774 struct ql3_adapter *qdev = NULL;
Joe Perchesd7f61772010-07-22 15:36:17 +00003775 static int cards_found;
Ingo Molnarbe5a3c62008-11-25 16:49:07 -08003776 int uninitialized_var(pci_using_dac), err;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003777
3778 err = pci_enable_device(pdev);
3779 if (err) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003780 pr_err("%s cannot enable PCI device\n", pci_name(pdev));
Ron Mercer5a4faa872006-07-25 00:40:21 -07003781 goto err_out;
3782 }
3783
3784 err = pci_request_regions(pdev, DRV_NAME);
3785 if (err) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003786 pr_err("%s cannot obtain PCI resources\n", pci_name(pdev));
Ron Mercer5a4faa872006-07-25 00:40:21 -07003787 goto err_out_disable_pdev;
3788 }
3789
3790 pci_set_master(pdev);
3791
Yang Hongyang6a355282009-04-06 19:01:13 -07003792 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ron Mercer5a4faa872006-07-25 00:40:21 -07003793 pci_using_dac = 1;
Yang Hongyang6a355282009-04-06 19:01:13 -07003794 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
Yang Hongyang284901a2009-04-06 19:01:15 -07003795 } else if (!(err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))) {
Ron Mercer5a4faa872006-07-25 00:40:21 -07003796 pci_using_dac = 0;
Yang Hongyang284901a2009-04-06 19:01:15 -07003797 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Ron Mercer5a4faa872006-07-25 00:40:21 -07003798 }
3799
3800 if (err) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003801 pr_err("%s no usable DMA configuration\n", pci_name(pdev));
Ron Mercer5a4faa872006-07-25 00:40:21 -07003802 goto err_out_free_regions;
3803 }
3804
3805 ndev = alloc_etherdev(sizeof(struct ql3_adapter));
Benjamin Li546faf02007-02-26 11:06:31 -08003806 if (!ndev) {
Benjamin Li546faf02007-02-26 11:06:31 -08003807 err = -ENOMEM;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003808 goto err_out_free_regions;
Benjamin Li546faf02007-02-26 11:06:31 -08003809 }
Ron Mercer5a4faa872006-07-25 00:40:21 -07003810
Ron Mercer5a4faa872006-07-25 00:40:21 -07003811 SET_NETDEV_DEV(ndev, &pdev->dev);
3812
Ron Mercer5a4faa872006-07-25 00:40:21 -07003813 pci_set_drvdata(pdev, ndev);
3814
3815 qdev = netdev_priv(ndev);
3816 qdev->index = cards_found;
3817 qdev->ndev = ndev;
3818 qdev->pdev = pdev;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003819 qdev->device_id = pci_entry->device;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003820 qdev->port_link_state = LS_DOWN;
3821 if (msi)
3822 qdev->msi = 1;
3823
3824 qdev->msg_enable = netif_msg_init(debug, default_msg);
3825
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003826 if (pci_using_dac)
3827 ndev->features |= NETIF_F_HIGHDMA;
3828 if (qdev->device_id == QL3032_DEVICE_ID)
Stephen Hemmingere68a8c12007-05-30 14:23:17 -07003829 ndev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
Ron Mercerbd36b0a2007-01-03 16:26:08 -08003830
Arjan van de Ven275f1652008-10-20 21:42:39 -07003831 qdev->mem_map_registers = pci_ioremap_bar(pdev, 1);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003832 if (!qdev->mem_map_registers) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003833 pr_err("%s: cannot map device registers\n", pci_name(pdev));
Benjamin Li546faf02007-02-26 11:06:31 -08003834 err = -EIO;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003835 goto err_out_free_ndev;
3836 }
3837
3838 spin_lock_init(&qdev->adapter_lock);
3839 spin_lock_init(&qdev->hw_lock);
3840
3841 /* Set driver entry points */
Stephen Hemmingerda1c14a2008-11-21 17:36:58 -08003842 ndev->netdev_ops = &ql3xxx_netdev_ops;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003843 SET_ETHTOOL_OPS(ndev, &ql3xxx_ethtool_ops);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003844 ndev->watchdog_timeo = 5 * HZ;
3845
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003846 netif_napi_add(ndev, &qdev->napi, ql_poll, 64);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003847
3848 ndev->irq = pdev->irq;
3849
3850 /* make sure the EEPROM is good */
3851 if (ql_get_nvram_params(qdev)) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003852 pr_alert("%s: Adapter #%d, Invalid NVRAM parameters\n",
3853 __func__, qdev->index);
Benjamin Li546faf02007-02-26 11:06:31 -08003854 err = -EIO;
Ron Mercer5a4faa872006-07-25 00:40:21 -07003855 goto err_out_iounmap;
3856 }
3857
3858 ql_set_mac_info(qdev);
3859
3860 /* Validate and set parameters */
3861 if (qdev->mac_index) {
Ron Mercercb8bac12007-02-26 11:06:36 -08003862 ndev->mtu = qdev->nvram_data.macCfg_port1.etherMtu_mac ;
Al Viro804d8542007-12-22 19:44:29 +00003863 ql_set_mac_addr(ndev, qdev->nvram_data.funcCfg_fn2.macAddress);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003864 } else {
Ron Mercercb8bac12007-02-26 11:06:36 -08003865 ndev->mtu = qdev->nvram_data.macCfg_port0.etherMtu_mac ;
Al Viro804d8542007-12-22 19:44:29 +00003866 ql_set_mac_addr(ndev, qdev->nvram_data.funcCfg_fn0.macAddress);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003867 }
Ron Mercer5a4faa872006-07-25 00:40:21 -07003868
3869 ndev->tx_queue_len = NUM_REQ_Q_ENTRIES;
3870
Ron Mercer5a4faa872006-07-25 00:40:21 -07003871 /* Record PCI bus information. */
3872 ql_get_board_info(qdev);
3873
3874 /*
3875 * Set the Maximum Memory Read Byte Count value. We do this to handle
3876 * jumbo frames.
3877 */
Joe Perchesd7f61772010-07-22 15:36:17 +00003878 if (qdev->pci_x)
Ron Mercer5a4faa872006-07-25 00:40:21 -07003879 pci_write_config_word(pdev, (int)0x4e, (u16) 0x0036);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003880
3881 err = register_netdev(ndev);
3882 if (err) {
Joe Percheseddc5fb2010-07-22 12:33:31 +00003883 pr_err("%s: cannot register net device\n", pci_name(pdev));
Ron Mercer5a4faa872006-07-25 00:40:21 -07003884 goto err_out_iounmap;
3885 }
3886
3887 /* we're going to reset, so assume we have no link for now */
3888
3889 netif_carrier_off(ndev);
3890 netif_stop_queue(ndev);
3891
3892 qdev->workqueue = create_singlethread_workqueue(ndev->name);
David Howellsc4028952006-11-22 14:57:56 +00003893 INIT_DELAYED_WORK(&qdev->reset_work, ql_reset_work);
3894 INIT_DELAYED_WORK(&qdev->tx_timeout_work, ql_tx_timeout_work);
Ron Mercer3e23b7d2007-11-07 13:59:06 -08003895 INIT_DELAYED_WORK(&qdev->link_state_work, ql_link_state_machine_work);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003896
3897 init_timer(&qdev->adapter_timer);
3898 qdev->adapter_timer.function = ql3xxx_timer;
3899 qdev->adapter_timer.expires = jiffies + HZ * 2; /* two second delay */
3900 qdev->adapter_timer.data = (unsigned long)qdev;
3901
Joe Percheseddc5fb2010-07-22 12:33:31 +00003902 if (!cards_found) {
3903 pr_alert("%s\n", DRV_STRING);
3904 pr_alert("Driver name: %s, Version: %s\n",
3905 DRV_NAME, DRV_VERSION);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003906 }
3907 ql_display_dev_info(ndev);
3908
3909 cards_found++;
3910 return 0;
3911
3912err_out_iounmap:
3913 iounmap(qdev->mem_map_registers);
3914err_out_free_ndev:
3915 free_netdev(ndev);
3916err_out_free_regions:
3917 pci_release_regions(pdev);
3918err_out_disable_pdev:
3919 pci_disable_device(pdev);
3920 pci_set_drvdata(pdev, NULL);
3921err_out:
3922 return err;
3923}
3924
Bill Pembertone3a582f2012-12-03 09:23:26 -05003925static void ql3xxx_remove(struct pci_dev *pdev)
Ron Mercer5a4faa872006-07-25 00:40:21 -07003926{
3927 struct net_device *ndev = pci_get_drvdata(pdev);
3928 struct ql3_adapter *qdev = netdev_priv(ndev);
3929
3930 unregister_netdev(ndev);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003931
3932 ql_disable_interrupts(qdev);
3933
3934 if (qdev->workqueue) {
3935 cancel_delayed_work(&qdev->reset_work);
3936 cancel_delayed_work(&qdev->tx_timeout_work);
3937 destroy_workqueue(qdev->workqueue);
3938 qdev->workqueue = NULL;
3939 }
3940
Al Viro855fc732006-09-25 02:54:46 +01003941 iounmap(qdev->mem_map_registers);
Ron Mercer5a4faa872006-07-25 00:40:21 -07003942 pci_release_regions(pdev);
3943 pci_set_drvdata(pdev, NULL);
3944 free_netdev(ndev);
3945}
3946
3947static struct pci_driver ql3xxx_driver = {
3948
3949 .name = DRV_NAME,
3950 .id_table = ql3xxx_pci_tbl,
3951 .probe = ql3xxx_probe,
Bill Pembertone3a582f2012-12-03 09:23:26 -05003952 .remove = ql3xxx_remove,
Ron Mercer5a4faa872006-07-25 00:40:21 -07003953};
3954
Wei Yongjun680d8662012-10-26 05:02:30 +00003955module_pci_driver(ql3xxx_driver);