blob: 3d52538df6c4b834a62b6758799e7ec0d1c93d09 [file] [log] [blame]
Michael Buesch753f4922007-09-19 14:20:30 -07001/* b44.c: Broadcom 44xx/47xx Fast Ethernet device driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
Michael Buesch753f4922007-09-19 14:20:30 -07004 * Copyright (C) 2004 Pekka Pietikainen (pp@ee.oulu.fi)
5 * Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
6 * Copyright (C) 2006 Felix Fietkau (nbd@openwrt.org)
Gary Zambrano8056bfa2006-04-10 12:05:40 -07007 * Copyright (C) 2006 Broadcom Corporation.
Michael Buesch753f4922007-09-19 14:20:30 -07008 * Copyright (C) 2007 Michael Buesch <mb@bu3sch.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * Distribute under GPL.
11 */
12
Joe Perches2fc96ff2010-02-17 15:01:50 +000013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/types.h>
19#include <linux/netdevice.h>
20#include <linux/ethtool.h>
21#include <linux/mii.h>
22#include <linux/if_ether.h>
Stephen Hemminger72f48612007-06-04 13:25:39 -070023#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/etherdevice.h>
25#include <linux/pci.h>
26#include <linux/delay.h>
27#include <linux/init.h>
Andrew Morton89358f92005-10-28 16:38:02 -040028#include <linux/dma-mapping.h>
Michael Buesch753f4922007-09-19 14:20:30 -070029#include <linux/ssb/ssb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/uaccess.h>
33#include <asm/io.h>
34#include <asm/irq.h>
35
Michael Buesch753f4922007-09-19 14:20:30 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "b44.h"
38
39#define DRV_MODULE_NAME "b44"
Michael Buesch753f4922007-09-19 14:20:30 -070040#define DRV_MODULE_VERSION "2.0"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define B44_DEF_MSG_ENABLE \
43 (NETIF_MSG_DRV | \
44 NETIF_MSG_PROBE | \
45 NETIF_MSG_LINK | \
46 NETIF_MSG_TIMER | \
47 NETIF_MSG_IFDOWN | \
48 NETIF_MSG_IFUP | \
49 NETIF_MSG_RX_ERR | \
50 NETIF_MSG_TX_ERR)
51
52/* length of time before we decide the hardware is borked,
53 * and dev->tx_timeout() should be called to fix the problem
54 */
55#define B44_TX_TIMEOUT (5 * HZ)
56
57/* hardware minimum and maximum for a single frame's data payload */
58#define B44_MIN_MTU 60
59#define B44_MAX_MTU 1500
60
61#define B44_RX_RING_SIZE 512
62#define B44_DEF_RX_RING_PENDING 200
63#define B44_RX_RING_BYTES (sizeof(struct dma_desc) * \
64 B44_RX_RING_SIZE)
65#define B44_TX_RING_SIZE 512
66#define B44_DEF_TX_RING_PENDING (B44_TX_RING_SIZE - 1)
67#define B44_TX_RING_BYTES (sizeof(struct dma_desc) * \
68 B44_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#define TX_RING_GAP(BP) \
71 (B44_TX_RING_SIZE - (BP)->tx_pending)
72#define TX_BUFFS_AVAIL(BP) \
73 (((BP)->tx_cons <= (BP)->tx_prod) ? \
74 (BP)->tx_cons + (BP)->tx_pending - (BP)->tx_prod : \
75 (BP)->tx_cons - (BP)->tx_prod - TX_RING_GAP(BP))
76#define NEXT_TX(N) (((N) + 1) & (B44_TX_RING_SIZE - 1))
77
Felix Fietkau4ca85792009-01-09 02:39:57 +000078#define RX_PKT_OFFSET (RX_HEADER_LEN + 2)
79#define RX_PKT_BUF_SZ (1536 + RX_PKT_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* minimum number of free TX descriptors required to wake up TX process */
82#define B44_TX_WAKEUP_THRESH (B44_TX_RING_SIZE / 4)
83
Gary Zambrano725ad802006-06-20 15:34:36 -070084/* b44 internal pattern match filter info */
85#define B44_PATTERN_BASE 0x400
86#define B44_PATTERN_SIZE 0x80
87#define B44_PMASK_BASE 0x600
88#define B44_PMASK_SIZE 0x10
89#define B44_MAX_PATTERNS 16
90#define B44_ETHIPV6UDP_HLEN 62
91#define B44_ETHIPV4UDP_HLEN 42
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static char version[] __devinitdata =
Michael Buesch753f4922007-09-19 14:20:30 -070094 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Michael Buesch753f4922007-09-19 14:20:30 -070096MODULE_AUTHOR("Felix Fietkau, Florian Schirmer, Pekka Pietikainen, David S. Miller");
97MODULE_DESCRIPTION("Broadcom 44xx/47xx 10/100 PCI ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070098MODULE_LICENSE("GPL");
99MODULE_VERSION(DRV_MODULE_VERSION);
100
101static int b44_debug = -1; /* -1 == use B44_DEF_MSG_ENABLE as value */
102module_param(b44_debug, int, 0);
103MODULE_PARM_DESC(b44_debug, "B44 bitmapped debugging message enable value");
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Michael Buesch753f4922007-09-19 14:20:30 -0700106#ifdef CONFIG_B44_PCI
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000107static DEFINE_PCI_DEVICE_TABLE(b44_pci_tbl) = {
Michael Buesch753f4922007-09-19 14:20:30 -0700108 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_BCM4401) },
109 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_BCM4401B0) },
110 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_BCM4401B1) },
111 { 0 } /* terminate list with empty entry */
112};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113MODULE_DEVICE_TABLE(pci, b44_pci_tbl);
114
Michael Buesch753f4922007-09-19 14:20:30 -0700115static struct pci_driver b44_pci_driver = {
116 .name = DRV_MODULE_NAME,
117 .id_table = b44_pci_tbl,
118};
119#endif /* CONFIG_B44_PCI */
120
121static const struct ssb_device_id b44_ssb_tbl[] = {
122 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_ETHERNET, SSB_ANY_REV),
123 SSB_DEVTABLE_END
124};
125MODULE_DEVICE_TABLE(ssb, b44_ssb_tbl);
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static void b44_halt(struct b44 *);
128static void b44_init_rings(struct b44 *);
Michael Chan5fc7d612007-01-26 23:59:57 -0800129
130#define B44_FULL_RESET 1
131#define B44_FULL_RESET_SKIP_PHY 2
132#define B44_PARTIAL_RESET 3
Miguel Botónfedb0ee2008-01-01 01:17:54 +0100133#define B44_CHIP_RESET_FULL 4
134#define B44_CHIP_RESET_PARTIAL 5
Michael Chan5fc7d612007-01-26 23:59:57 -0800135
Gary Zambrano00e8b3a2006-06-20 15:34:26 -0700136static void b44_init_hw(struct b44 *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
John W. Linville9f38c632005-10-18 21:30:59 -0400138static int dma_desc_align_mask;
139static int dma_desc_sync_size;
Michael Buesch753f4922007-09-19 14:20:30 -0700140static int instance;
John W. Linville9f38c632005-10-18 21:30:59 -0400141
Francois Romieu33539302005-11-07 01:51:34 +0100142static const char b44_gstrings[][ETH_GSTRING_LEN] = {
143#define _B44(x...) # x,
144B44_STAT_REG_DECLARE
145#undef _B44
146};
147
Michael Buesch753f4922007-09-19 14:20:30 -0700148static inline void b44_sync_dma_desc_for_device(struct ssb_device *sdev,
149 dma_addr_t dma_base,
150 unsigned long offset,
151 enum dma_data_direction dir)
John W. Linville9f38c632005-10-18 21:30:59 -0400152{
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700153 dma_sync_single_for_device(sdev->dma_dev, dma_base + offset,
154 dma_desc_sync_size, dir);
John W. Linville9f38c632005-10-18 21:30:59 -0400155}
156
Michael Buesch753f4922007-09-19 14:20:30 -0700157static inline void b44_sync_dma_desc_for_cpu(struct ssb_device *sdev,
158 dma_addr_t dma_base,
159 unsigned long offset,
160 enum dma_data_direction dir)
John W. Linville9f38c632005-10-18 21:30:59 -0400161{
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700162 dma_sync_single_for_cpu(sdev->dma_dev, dma_base + offset,
163 dma_desc_sync_size, dir);
John W. Linville9f38c632005-10-18 21:30:59 -0400164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static inline unsigned long br32(const struct b44 *bp, unsigned long reg)
167{
Michael Buesch753f4922007-09-19 14:20:30 -0700168 return ssb_read32(bp->sdev, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Jeff Garzik10badc22006-04-12 18:04:32 -0400171static inline void bw32(const struct b44 *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned long reg, unsigned long val)
173{
Michael Buesch753f4922007-09-19 14:20:30 -0700174 ssb_write32(bp->sdev, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177static int b44_wait_bit(struct b44 *bp, unsigned long reg,
178 u32 bit, unsigned long timeout, const int clear)
179{
180 unsigned long i;
181
182 for (i = 0; i < timeout; i++) {
183 u32 val = br32(bp, reg);
184
185 if (clear && !(val & bit))
186 break;
187 if (!clear && (val & bit))
188 break;
189 udelay(10);
190 }
191 if (i == timeout) {
Jochen Friedrichf6ca0572010-02-12 10:11:54 +0000192 if (net_ratelimit())
Joe Perches2fc96ff2010-02-17 15:01:50 +0000193 netdev_err(bp->dev, "BUG! Timeout waiting for bit %08x of register %lx to %s\n",
194 bit, reg, clear ? "clear" : "set");
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -ENODEV;
197 }
198 return 0;
199}
200
Michael Buesch753f4922007-09-19 14:20:30 -0700201static inline void __b44_cam_read(struct b44 *bp, unsigned char *data, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 u32 val;
204
Michael Buesch753f4922007-09-19 14:20:30 -0700205 bw32(bp, B44_CAM_CTRL, (CAM_CTRL_READ |
206 (index << CAM_CTRL_INDEX_SHIFT)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Michael Buesch753f4922007-09-19 14:20:30 -0700208 b44_wait_bit(bp, B44_CAM_CTRL, CAM_CTRL_BUSY, 100, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Michael Buesch753f4922007-09-19 14:20:30 -0700210 val = br32(bp, B44_CAM_DATA_LO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Michael Buesch753f4922007-09-19 14:20:30 -0700212 data[2] = (val >> 24) & 0xFF;
213 data[3] = (val >> 16) & 0xFF;
214 data[4] = (val >> 8) & 0xFF;
215 data[5] = (val >> 0) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Michael Buesch753f4922007-09-19 14:20:30 -0700217 val = br32(bp, B44_CAM_DATA_HI);
218
219 data[0] = (val >> 8) & 0xFF;
220 data[1] = (val >> 0) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Michael Buesch753f4922007-09-19 14:20:30 -0700223static inline void __b44_cam_write(struct b44 *bp, unsigned char *data, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 u32 val;
226
227 val = ((u32) data[2]) << 24;
228 val |= ((u32) data[3]) << 16;
229 val |= ((u32) data[4]) << 8;
230 val |= ((u32) data[5]) << 0;
231 bw32(bp, B44_CAM_DATA_LO, val);
Jeff Garzik10badc22006-04-12 18:04:32 -0400232 val = (CAM_DATA_HI_VALID |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 (((u32) data[0]) << 8) |
234 (((u32) data[1]) << 0));
235 bw32(bp, B44_CAM_DATA_HI, val);
236 bw32(bp, B44_CAM_CTRL, (CAM_CTRL_WRITE |
237 (index << CAM_CTRL_INDEX_SHIFT)));
Jeff Garzik10badc22006-04-12 18:04:32 -0400238 b44_wait_bit(bp, B44_CAM_CTRL, CAM_CTRL_BUSY, 100, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241static inline void __b44_disable_ints(struct b44 *bp)
242{
243 bw32(bp, B44_IMASK, 0);
244}
245
246static void b44_disable_ints(struct b44 *bp)
247{
248 __b44_disable_ints(bp);
249
250 /* Flush posted writes. */
251 br32(bp, B44_IMASK);
252}
253
254static void b44_enable_ints(struct b44 *bp)
255{
256 bw32(bp, B44_IMASK, bp->imask);
257}
258
Michael Buesch753f4922007-09-19 14:20:30 -0700259static int __b44_readphy(struct b44 *bp, int phy_addr, int reg, u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 int err;
262
263 bw32(bp, B44_EMAC_ISTAT, EMAC_INT_MII);
264 bw32(bp, B44_MDIO_DATA, (MDIO_DATA_SB_START |
265 (MDIO_OP_READ << MDIO_DATA_OP_SHIFT) |
Michael Buesch753f4922007-09-19 14:20:30 -0700266 (phy_addr << MDIO_DATA_PMD_SHIFT) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 (reg << MDIO_DATA_RA_SHIFT) |
268 (MDIO_TA_VALID << MDIO_DATA_TA_SHIFT)));
269 err = b44_wait_bit(bp, B44_EMAC_ISTAT, EMAC_INT_MII, 100, 0);
270 *val = br32(bp, B44_MDIO_DATA) & MDIO_DATA_DATA;
271
272 return err;
273}
274
Michael Buesch753f4922007-09-19 14:20:30 -0700275static int __b44_writephy(struct b44 *bp, int phy_addr, int reg, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 bw32(bp, B44_EMAC_ISTAT, EMAC_INT_MII);
278 bw32(bp, B44_MDIO_DATA, (MDIO_DATA_SB_START |
279 (MDIO_OP_WRITE << MDIO_DATA_OP_SHIFT) |
Michael Buesch753f4922007-09-19 14:20:30 -0700280 (phy_addr << MDIO_DATA_PMD_SHIFT) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 (reg << MDIO_DATA_RA_SHIFT) |
282 (MDIO_TA_VALID << MDIO_DATA_TA_SHIFT) |
283 (val & MDIO_DATA_DATA)));
284 return b44_wait_bit(bp, B44_EMAC_ISTAT, EMAC_INT_MII, 100, 0);
285}
286
Michael Buesch753f4922007-09-19 14:20:30 -0700287static inline int b44_readphy(struct b44 *bp, int reg, u32 *val)
288{
289 if (bp->phy_addr == B44_PHY_ADDR_NO_PHY)
290 return 0;
291
292 return __b44_readphy(bp, bp->phy_addr, reg, val);
293}
294
295static inline int b44_writephy(struct b44 *bp, int reg, u32 val)
296{
297 if (bp->phy_addr == B44_PHY_ADDR_NO_PHY)
298 return 0;
299
300 return __b44_writephy(bp, bp->phy_addr, reg, val);
301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/* miilib interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static int b44_mii_read(struct net_device *dev, int phy_id, int location)
305{
306 u32 val;
307 struct b44 *bp = netdev_priv(dev);
Michael Buesch753f4922007-09-19 14:20:30 -0700308 int rc = __b44_readphy(bp, phy_id, location, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (rc)
310 return 0xffffffff;
311 return val;
312}
313
314static void b44_mii_write(struct net_device *dev, int phy_id, int location,
315 int val)
316{
317 struct b44 *bp = netdev_priv(dev);
Michael Buesch753f4922007-09-19 14:20:30 -0700318 __b44_writephy(bp, phy_id, location, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321static int b44_phy_reset(struct b44 *bp)
322{
323 u32 val;
324 int err;
325
Michael Buesch753f4922007-09-19 14:20:30 -0700326 if (bp->phy_addr == B44_PHY_ADDR_NO_PHY)
327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 err = b44_writephy(bp, MII_BMCR, BMCR_RESET);
329 if (err)
330 return err;
331 udelay(100);
332 err = b44_readphy(bp, MII_BMCR, &val);
333 if (!err) {
334 if (val & BMCR_RESET) {
Joe Perches2fc96ff2010-02-17 15:01:50 +0000335 netdev_err(bp->dev, "PHY Reset would not complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 err = -ENODEV;
337 }
338 }
339
Hauke Mehrtens8850dce2010-02-20 10:55:25 +0000340 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343static void __b44_set_flow_ctrl(struct b44 *bp, u32 pause_flags)
344{
345 u32 val;
346
347 bp->flags &= ~(B44_FLAG_TX_PAUSE | B44_FLAG_RX_PAUSE);
348 bp->flags |= pause_flags;
349
350 val = br32(bp, B44_RXCONFIG);
351 if (pause_flags & B44_FLAG_RX_PAUSE)
352 val |= RXCONFIG_FLOW;
353 else
354 val &= ~RXCONFIG_FLOW;
355 bw32(bp, B44_RXCONFIG, val);
356
357 val = br32(bp, B44_MAC_FLOW);
358 if (pause_flags & B44_FLAG_TX_PAUSE)
359 val |= (MAC_FLOW_PAUSE_ENAB |
360 (0xc0 & MAC_FLOW_RX_HI_WATER));
361 else
362 val &= ~MAC_FLOW_PAUSE_ENAB;
363 bw32(bp, B44_MAC_FLOW, val);
364}
365
366static void b44_set_flow_ctrl(struct b44 *bp, u32 local, u32 remote)
367{
Jeff Garzik10badc22006-04-12 18:04:32 -0400368 u32 pause_enab = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Gary Zambrano2b474cf2006-04-10 12:02:21 -0700370 /* The driver supports only rx pause by default because
Jeff Garzik10badc22006-04-12 18:04:32 -0400371 the b44 mac tx pause mechanism generates excessive
372 pause frames.
Gary Zambrano2b474cf2006-04-10 12:02:21 -0700373 Use ethtool to turn on b44 tx pause if necessary.
374 */
375 if ((local & ADVERTISE_PAUSE_CAP) &&
Jeff Garzik10badc22006-04-12 18:04:32 -0400376 (local & ADVERTISE_PAUSE_ASYM)){
Gary Zambrano2b474cf2006-04-10 12:02:21 -0700377 if ((remote & LPA_PAUSE_ASYM) &&
378 !(remote & LPA_PAUSE_CAP))
379 pause_enab |= B44_FLAG_RX_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
382 __b44_set_flow_ctrl(bp, pause_enab);
383}
384
Michael Buesch753f4922007-09-19 14:20:30 -0700385#ifdef SSB_DRIVER_MIPS
386extern char *nvram_get(char *name);
387static void b44_wap54g10_workaround(struct b44 *bp)
388{
389 const char *str;
390 u32 val;
391 int err;
392
393 /*
394 * workaround for bad hardware design in Linksys WAP54G v1.0
395 * see https://dev.openwrt.org/ticket/146
396 * check and reset bit "isolate"
397 */
398 str = nvram_get("boardnum");
399 if (!str)
400 return;
401 if (simple_strtoul(str, NULL, 0) == 2) {
402 err = __b44_readphy(bp, 0, MII_BMCR, &val);
403 if (err)
404 goto error;
405 if (!(val & BMCR_ISOLATE))
406 return;
407 val &= ~BMCR_ISOLATE;
408 err = __b44_writephy(bp, 0, MII_BMCR, val);
409 if (err)
410 goto error;
411 }
412 return;
413error:
Joe Perches2fc96ff2010-02-17 15:01:50 +0000414 pr_warning("PHY: cannot reset MII transceiver isolate bit\n");
Michael Buesch753f4922007-09-19 14:20:30 -0700415}
416#else
417static inline void b44_wap54g10_workaround(struct b44 *bp)
418{
419}
420#endif
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static int b44_setup_phy(struct b44 *bp)
423{
424 u32 val;
425 int err;
426
Michael Buesch753f4922007-09-19 14:20:30 -0700427 b44_wap54g10_workaround(bp);
428
429 if (bp->phy_addr == B44_PHY_ADDR_NO_PHY)
430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if ((err = b44_readphy(bp, B44_MII_ALEDCTRL, &val)) != 0)
432 goto out;
433 if ((err = b44_writephy(bp, B44_MII_ALEDCTRL,
434 val & MII_ALEDCTRL_ALLMSK)) != 0)
435 goto out;
436 if ((err = b44_readphy(bp, B44_MII_TLEDCTRL, &val)) != 0)
437 goto out;
438 if ((err = b44_writephy(bp, B44_MII_TLEDCTRL,
439 val | MII_TLEDCTRL_ENABLE)) != 0)
440 goto out;
441
442 if (!(bp->flags & B44_FLAG_FORCE_LINK)) {
443 u32 adv = ADVERTISE_CSMA;
444
445 if (bp->flags & B44_FLAG_ADV_10HALF)
446 adv |= ADVERTISE_10HALF;
447 if (bp->flags & B44_FLAG_ADV_10FULL)
448 adv |= ADVERTISE_10FULL;
449 if (bp->flags & B44_FLAG_ADV_100HALF)
450 adv |= ADVERTISE_100HALF;
451 if (bp->flags & B44_FLAG_ADV_100FULL)
452 adv |= ADVERTISE_100FULL;
453
454 if (bp->flags & B44_FLAG_PAUSE_AUTO)
455 adv |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
456
457 if ((err = b44_writephy(bp, MII_ADVERTISE, adv)) != 0)
458 goto out;
459 if ((err = b44_writephy(bp, MII_BMCR, (BMCR_ANENABLE |
460 BMCR_ANRESTART))) != 0)
461 goto out;
462 } else {
463 u32 bmcr;
464
465 if ((err = b44_readphy(bp, MII_BMCR, &bmcr)) != 0)
466 goto out;
467 bmcr &= ~(BMCR_FULLDPLX | BMCR_ANENABLE | BMCR_SPEED100);
468 if (bp->flags & B44_FLAG_100_BASE_T)
469 bmcr |= BMCR_SPEED100;
470 if (bp->flags & B44_FLAG_FULL_DUPLEX)
471 bmcr |= BMCR_FULLDPLX;
472 if ((err = b44_writephy(bp, MII_BMCR, bmcr)) != 0)
473 goto out;
474
475 /* Since we will not be negotiating there is no safe way
476 * to determine if the link partner supports flow control
477 * or not. So just disable it completely in this case.
478 */
479 b44_set_flow_ctrl(bp, 0, 0);
480 }
481
482out:
483 return err;
484}
485
486static void b44_stats_update(struct b44 *bp)
487{
488 unsigned long reg;
489 u32 *val;
490
491 val = &bp->hw_stats.tx_good_octets;
492 for (reg = B44_TX_GOOD_O; reg <= B44_TX_PAUSE; reg += 4UL) {
493 *val++ += br32(bp, reg);
494 }
Francois Romieu33539302005-11-07 01:51:34 +0100495
496 /* Pad */
497 reg += 8*4UL;
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 for (reg = B44_RX_GOOD_O; reg <= B44_RX_NPAUSE; reg += 4UL) {
500 *val++ += br32(bp, reg);
501 }
502}
503
504static void b44_link_report(struct b44 *bp)
505{
506 if (!netif_carrier_ok(bp->dev)) {
Joe Perches2fc96ff2010-02-17 15:01:50 +0000507 netdev_info(bp->dev, "Link is down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 } else {
Joe Perches2fc96ff2010-02-17 15:01:50 +0000509 netdev_info(bp->dev, "Link is up at %d Mbps, %s duplex\n",
510 (bp->flags & B44_FLAG_100_BASE_T) ? 100 : 10,
511 (bp->flags & B44_FLAG_FULL_DUPLEX) ? "full" : "half");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Joe Perches2fc96ff2010-02-17 15:01:50 +0000513 netdev_info(bp->dev, "Flow control is %s for TX and %s for RX\n",
514 (bp->flags & B44_FLAG_TX_PAUSE) ? "on" : "off",
515 (bp->flags & B44_FLAG_RX_PAUSE) ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517}
518
519static void b44_check_phy(struct b44 *bp)
520{
521 u32 bmsr, aux;
522
Michael Buesch753f4922007-09-19 14:20:30 -0700523 if (bp->phy_addr == B44_PHY_ADDR_NO_PHY) {
524 bp->flags |= B44_FLAG_100_BASE_T;
525 bp->flags |= B44_FLAG_FULL_DUPLEX;
526 if (!netif_carrier_ok(bp->dev)) {
527 u32 val = br32(bp, B44_TX_CTRL);
528 val |= TX_CTRL_DUPLEX;
529 bw32(bp, B44_TX_CTRL, val);
530 netif_carrier_on(bp->dev);
531 b44_link_report(bp);
532 }
533 return;
534 }
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (!b44_readphy(bp, MII_BMSR, &bmsr) &&
537 !b44_readphy(bp, B44_MII_AUXCTRL, &aux) &&
538 (bmsr != 0xffff)) {
539 if (aux & MII_AUXCTRL_SPEED)
540 bp->flags |= B44_FLAG_100_BASE_T;
541 else
542 bp->flags &= ~B44_FLAG_100_BASE_T;
543 if (aux & MII_AUXCTRL_DUPLEX)
544 bp->flags |= B44_FLAG_FULL_DUPLEX;
545 else
546 bp->flags &= ~B44_FLAG_FULL_DUPLEX;
547
548 if (!netif_carrier_ok(bp->dev) &&
549 (bmsr & BMSR_LSTATUS)) {
550 u32 val = br32(bp, B44_TX_CTRL);
551 u32 local_adv, remote_adv;
552
553 if (bp->flags & B44_FLAG_FULL_DUPLEX)
554 val |= TX_CTRL_DUPLEX;
555 else
556 val &= ~TX_CTRL_DUPLEX;
557 bw32(bp, B44_TX_CTRL, val);
558
559 if (!(bp->flags & B44_FLAG_FORCE_LINK) &&
560 !b44_readphy(bp, MII_ADVERTISE, &local_adv) &&
561 !b44_readphy(bp, MII_LPA, &remote_adv))
562 b44_set_flow_ctrl(bp, local_adv, remote_adv);
563
564 /* Link now up */
565 netif_carrier_on(bp->dev);
566 b44_link_report(bp);
567 } else if (netif_carrier_ok(bp->dev) && !(bmsr & BMSR_LSTATUS)) {
568 /* Link now down */
569 netif_carrier_off(bp->dev);
570 b44_link_report(bp);
571 }
572
573 if (bmsr & BMSR_RFAULT)
Joe Perches2fc96ff2010-02-17 15:01:50 +0000574 netdev_warn(bp->dev, "Remote fault detected in PHY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (bmsr & BMSR_JCD)
Joe Perches2fc96ff2010-02-17 15:01:50 +0000576 netdev_warn(bp->dev, "Jabber detected in PHY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578}
579
580static void b44_timer(unsigned long __opaque)
581{
582 struct b44 *bp = (struct b44 *) __opaque;
583
584 spin_lock_irq(&bp->lock);
585
586 b44_check_phy(bp);
587
588 b44_stats_update(bp);
589
590 spin_unlock_irq(&bp->lock);
591
Stephen Hemmingera72a8172007-06-04 13:25:37 -0700592 mod_timer(&bp->timer, round_jiffies(jiffies + HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595static void b44_tx(struct b44 *bp)
596{
597 u32 cur, cons;
598
599 cur = br32(bp, B44_DMATX_STAT) & DMATX_STAT_CDMASK;
600 cur /= sizeof(struct dma_desc);
601
602 /* XXX needs updating when NETIF_F_SG is supported */
603 for (cons = bp->tx_cons; cons != cur; cons = NEXT_TX(cons)) {
604 struct ring_info *rp = &bp->tx_buffers[cons];
605 struct sk_buff *skb = rp->skb;
606
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +0200607 BUG_ON(skb == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700609 dma_unmap_single(bp->sdev->dma_dev,
610 rp->mapping,
611 skb->len,
612 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 rp->skb = NULL;
614 dev_kfree_skb_irq(skb);
615 }
616
617 bp->tx_cons = cons;
618 if (netif_queue_stopped(bp->dev) &&
619 TX_BUFFS_AVAIL(bp) > B44_TX_WAKEUP_THRESH)
620 netif_wake_queue(bp->dev);
621
622 bw32(bp, B44_GPTIMER, 0);
623}
624
625/* Works like this. This chip writes a 'struct rx_header" 30 bytes
626 * before the DMA address you give it. So we allocate 30 more bytes
627 * for the RX buffer, DMA map all of it, skb_reserve the 30 bytes, then
628 * point the chip at 30 bytes past where the rx_header will go.
629 */
630static int b44_alloc_rx_skb(struct b44 *bp, int src_idx, u32 dest_idx_unmasked)
631{
632 struct dma_desc *dp;
633 struct ring_info *src_map, *map;
634 struct rx_header *rh;
635 struct sk_buff *skb;
636 dma_addr_t mapping;
637 int dest_idx;
638 u32 ctrl;
639
640 src_map = NULL;
641 if (src_idx >= 0)
642 src_map = &bp->rx_buffers[src_idx];
643 dest_idx = dest_idx_unmasked & (B44_RX_RING_SIZE - 1);
644 map = &bp->rx_buffers[dest_idx];
Stephen Hemmingerbf0dcbd2007-06-04 13:25:40 -0700645 skb = netdev_alloc_skb(bp->dev, RX_PKT_BUF_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (skb == NULL)
647 return -ENOMEM;
648
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700649 mapping = dma_map_single(bp->sdev->dma_dev, skb->data,
650 RX_PKT_BUF_SZ,
651 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 /* Hardware bug work-around, the chip is unable to do PCI DMA
654 to/from anything above 1GB :-( */
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700655 if (dma_mapping_error(bp->sdev->dma_dev, mapping) ||
Yang Hongyang28b76792009-04-06 19:01:17 -0700656 mapping + RX_PKT_BUF_SZ > DMA_BIT_MASK(30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /* Sigh... */
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700658 if (!dma_mapping_error(bp->sdev->dma_dev, mapping))
659 dma_unmap_single(bp->sdev->dma_dev, mapping,
Michael Bueschf2257632008-06-20 11:50:29 +0200660 RX_PKT_BUF_SZ, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 dev_kfree_skb_any(skb);
Stephen Hemmingerbf0dcbd2007-06-04 13:25:40 -0700662 skb = __netdev_alloc_skb(bp->dev, RX_PKT_BUF_SZ, GFP_ATOMIC|GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (skb == NULL)
664 return -ENOMEM;
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700665 mapping = dma_map_single(bp->sdev->dma_dev, skb->data,
666 RX_PKT_BUF_SZ,
667 DMA_FROM_DEVICE);
668 if (dma_mapping_error(bp->sdev->dma_dev, mapping) ||
669 mapping + RX_PKT_BUF_SZ > DMA_BIT_MASK(30)) {
670 if (!dma_mapping_error(bp->sdev->dma_dev, mapping))
671 dma_unmap_single(bp->sdev->dma_dev, mapping, RX_PKT_BUF_SZ,DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 dev_kfree_skb_any(skb);
673 return -ENOMEM;
674 }
Eric Dumazeta58c8912009-01-15 15:29:35 -0800675 bp->force_copybreak = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
Stephen Hemminger72f48612007-06-04 13:25:39 -0700678 rh = (struct rx_header *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 rh->len = 0;
681 rh->flags = 0;
682
683 map->skb = skb;
Michael Buesch753f4922007-09-19 14:20:30 -0700684 map->mapping = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (src_map != NULL)
687 src_map->skb = NULL;
688
Felix Fietkau4ca85792009-01-09 02:39:57 +0000689 ctrl = (DESC_CTRL_LEN & RX_PKT_BUF_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (dest_idx == (B44_RX_RING_SIZE - 1))
691 ctrl |= DESC_CTRL_EOT;
692
693 dp = &bp->rx_ring[dest_idx];
694 dp->ctrl = cpu_to_le32(ctrl);
Felix Fietkau4ca85792009-01-09 02:39:57 +0000695 dp->addr = cpu_to_le32((u32) mapping + bp->dma_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
John W. Linville9f38c632005-10-18 21:30:59 -0400697 if (bp->flags & B44_FLAG_RX_RING_HACK)
Michael Buesch753f4922007-09-19 14:20:30 -0700698 b44_sync_dma_desc_for_device(bp->sdev, bp->rx_ring_dma,
Michael Buesch5d4d9e82009-04-03 00:01:30 +0000699 dest_idx * sizeof(*dp),
Michael Buesch753f4922007-09-19 14:20:30 -0700700 DMA_BIDIRECTIONAL);
John W. Linville9f38c632005-10-18 21:30:59 -0400701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return RX_PKT_BUF_SZ;
703}
704
705static void b44_recycle_rx(struct b44 *bp, int src_idx, u32 dest_idx_unmasked)
706{
707 struct dma_desc *src_desc, *dest_desc;
708 struct ring_info *src_map, *dest_map;
709 struct rx_header *rh;
710 int dest_idx;
Al Viroa7bed272007-01-29 15:36:54 -0500711 __le32 ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 dest_idx = dest_idx_unmasked & (B44_RX_RING_SIZE - 1);
714 dest_desc = &bp->rx_ring[dest_idx];
715 dest_map = &bp->rx_buffers[dest_idx];
716 src_desc = &bp->rx_ring[src_idx];
717 src_map = &bp->rx_buffers[src_idx];
718
719 dest_map->skb = src_map->skb;
720 rh = (struct rx_header *) src_map->skb->data;
721 rh->len = 0;
722 rh->flags = 0;
Michael Buesch753f4922007-09-19 14:20:30 -0700723 dest_map->mapping = src_map->mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
John W. Linville9f38c632005-10-18 21:30:59 -0400725 if (bp->flags & B44_FLAG_RX_RING_HACK)
Michael Buesch753f4922007-09-19 14:20:30 -0700726 b44_sync_dma_desc_for_cpu(bp->sdev, bp->rx_ring_dma,
Michael Buesch5d4d9e82009-04-03 00:01:30 +0000727 src_idx * sizeof(*src_desc),
Michael Buesch753f4922007-09-19 14:20:30 -0700728 DMA_BIDIRECTIONAL);
John W. Linville9f38c632005-10-18 21:30:59 -0400729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 ctrl = src_desc->ctrl;
731 if (dest_idx == (B44_RX_RING_SIZE - 1))
732 ctrl |= cpu_to_le32(DESC_CTRL_EOT);
733 else
734 ctrl &= cpu_to_le32(~DESC_CTRL_EOT);
735
736 dest_desc->ctrl = ctrl;
737 dest_desc->addr = src_desc->addr;
John W. Linville9f38c632005-10-18 21:30:59 -0400738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 src_map->skb = NULL;
740
John W. Linville9f38c632005-10-18 21:30:59 -0400741 if (bp->flags & B44_FLAG_RX_RING_HACK)
Michael Buesch753f4922007-09-19 14:20:30 -0700742 b44_sync_dma_desc_for_device(bp->sdev, bp->rx_ring_dma,
Michael Buesch5d4d9e82009-04-03 00:01:30 +0000743 dest_idx * sizeof(*dest_desc),
Michael Buesch753f4922007-09-19 14:20:30 -0700744 DMA_BIDIRECTIONAL);
John W. Linville9f38c632005-10-18 21:30:59 -0400745
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700746 dma_sync_single_for_device(bp->sdev->dma_dev, dest_map->mapping,
747 RX_PKT_BUF_SZ,
748 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751static int b44_rx(struct b44 *bp, int budget)
752{
753 int received;
754 u32 cons, prod;
755
756 received = 0;
757 prod = br32(bp, B44_DMARX_STAT) & DMARX_STAT_CDMASK;
758 prod /= sizeof(struct dma_desc);
759 cons = bp->rx_cons;
760
761 while (cons != prod && budget > 0) {
762 struct ring_info *rp = &bp->rx_buffers[cons];
763 struct sk_buff *skb = rp->skb;
Michael Buesch753f4922007-09-19 14:20:30 -0700764 dma_addr_t map = rp->mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct rx_header *rh;
766 u16 len;
767
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700768 dma_sync_single_for_cpu(bp->sdev->dma_dev, map,
769 RX_PKT_BUF_SZ,
770 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 rh = (struct rx_header *) skb->data;
Al Viroa7bed272007-01-29 15:36:54 -0500772 len = le16_to_cpu(rh->len);
Stephen Hemminger72f48612007-06-04 13:25:39 -0700773 if ((len > (RX_PKT_BUF_SZ - RX_PKT_OFFSET)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 (rh->flags & cpu_to_le16(RX_FLAG_ERRORS))) {
775 drop_it:
776 b44_recycle_rx(bp, cons, bp->rx_prod);
777 drop_it_no_recycle:
Eric Dumazet553e2332009-05-27 10:34:50 +0000778 bp->dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 goto next_pkt;
780 }
781
782 if (len == 0) {
783 int i = 0;
784
785 do {
786 udelay(2);
787 barrier();
Al Viroa7bed272007-01-29 15:36:54 -0500788 len = le16_to_cpu(rh->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 } while (len == 0 && i++ < 5);
790 if (len == 0)
791 goto drop_it;
792 }
793
794 /* Omit CRC. */
795 len -= 4;
796
Eric Dumazeta58c8912009-01-15 15:29:35 -0800797 if (!bp->force_copybreak && len > RX_COPY_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 int skb_size;
799 skb_size = b44_alloc_rx_skb(bp, cons, bp->rx_prod);
800 if (skb_size < 0)
801 goto drop_it;
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700802 dma_unmap_single(bp->sdev->dma_dev, map,
803 skb_size, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* Leave out rx_header */
Felix Fietkau4ca85792009-01-09 02:39:57 +0000805 skb_put(skb, len + RX_PKT_OFFSET);
806 skb_pull(skb, RX_PKT_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 } else {
808 struct sk_buff *copy_skb;
809
810 b44_recycle_rx(bp, cons, bp->rx_prod);
Hauke Mehrtens53639202010-02-20 10:55:26 +0000811 copy_skb = netdev_alloc_skb(bp->dev, len + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (copy_skb == NULL)
813 goto drop_it_no_recycle;
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 skb_reserve(copy_skb, 2);
816 skb_put(copy_skb, len);
817 /* DMA sync done above, copy just the actual packet */
Stephen Hemminger72f48612007-06-04 13:25:39 -0700818 skb_copy_from_linear_data_offset(skb, RX_PKT_OFFSET,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300819 copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 skb = copy_skb;
821 }
822 skb->ip_summed = CHECKSUM_NONE;
823 skb->protocol = eth_type_trans(skb, bp->dev);
824 netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 received++;
826 budget--;
827 next_pkt:
828 bp->rx_prod = (bp->rx_prod + 1) &
829 (B44_RX_RING_SIZE - 1);
830 cons = (cons + 1) & (B44_RX_RING_SIZE - 1);
831 }
832
833 bp->rx_cons = cons;
834 bw32(bp, B44_DMARX_PTR, cons * sizeof(struct dma_desc));
835
836 return received;
837}
838
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700839static int b44_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700841 struct b44 *bp = container_of(napi, struct b44, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700842 int work_done;
Dongdong Denge99b1f02009-09-16 16:10:47 +0000843 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Dongdong Denge99b1f02009-09-16 16:10:47 +0000845 spin_lock_irqsave(&bp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 if (bp->istat & (ISTAT_TX | ISTAT_TO)) {
848 /* spin_lock(&bp->tx_lock); */
849 b44_tx(bp);
850 /* spin_unlock(&bp->tx_lock); */
851 }
Dongdong Denge99b1f02009-09-16 16:10:47 +0000852 spin_unlock_irqrestore(&bp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700854 work_done = 0;
855 if (bp->istat & ISTAT_RX)
856 work_done += b44_rx(bp, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 if (bp->istat & ISTAT_ERRORS) {
Francois Romieud15e9c42006-12-17 23:03:15 +0100859 spin_lock_irqsave(&bp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 b44_halt(bp);
861 b44_init_rings(bp);
Michael Chan5fc7d612007-01-26 23:59:57 -0800862 b44_init_hw(bp, B44_FULL_RESET_SKIP_PHY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 netif_wake_queue(bp->dev);
Francois Romieud15e9c42006-12-17 23:03:15 +0100864 spin_unlock_irqrestore(&bp->lock, flags);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700865 work_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700868 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800869 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 b44_enable_ints(bp);
871 }
872
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700873 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
David Howells7d12e782006-10-05 14:55:46 +0100876static irqreturn_t b44_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 struct net_device *dev = dev_id;
879 struct b44 *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 u32 istat, imask;
881 int handled = 0;
882
Francois Romieu65b984f2005-11-07 01:52:06 +0100883 spin_lock(&bp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 istat = br32(bp, B44_ISTAT);
886 imask = br32(bp, B44_IMASK);
887
Johannes Berge78181f2006-11-06 23:17:20 +0100888 /* The interrupt mask register controls which interrupt bits
889 * will actually raise an interrupt to the CPU when set by hw/firmware,
890 * but doesn't mask off the bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 */
892 istat &= imask;
893 if (istat) {
894 handled = 1;
Francois Romieuba5eec92005-11-08 23:37:12 +0100895
896 if (unlikely(!netif_running(dev))) {
Joe Perches2fc96ff2010-02-17 15:01:50 +0000897 netdev_info(dev, "late interrupt\n");
Francois Romieuba5eec92005-11-08 23:37:12 +0100898 goto irq_ack;
899 }
900
Ben Hutchings288379f2009-01-19 16:43:59 -0800901 if (napi_schedule_prep(&bp->napi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* NOTE: These writes are posted by the readback of
903 * the ISTAT register below.
904 */
905 bp->istat = istat;
906 __b44_disable_ints(bp);
Ben Hutchings288379f2009-01-19 16:43:59 -0800907 __napi_schedule(&bp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909
Francois Romieuba5eec92005-11-08 23:37:12 +0100910irq_ack:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 bw32(bp, B44_ISTAT, istat);
912 br32(bp, B44_ISTAT);
913 }
Francois Romieu65b984f2005-11-07 01:52:06 +0100914 spin_unlock(&bp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return IRQ_RETVAL(handled);
916}
917
918static void b44_tx_timeout(struct net_device *dev)
919{
920 struct b44 *bp = netdev_priv(dev);
921
Joe Perches2fc96ff2010-02-17 15:01:50 +0000922 netdev_err(dev, "transmit timed out, resetting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 spin_lock_irq(&bp->lock);
925
926 b44_halt(bp);
927 b44_init_rings(bp);
Michael Chan5fc7d612007-01-26 23:59:57 -0800928 b44_init_hw(bp, B44_FULL_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 spin_unlock_irq(&bp->lock);
931
932 b44_enable_ints(bp);
933
934 netif_wake_queue(dev);
935}
936
Stephen Hemminger613573252009-08-31 19:50:58 +0000937static netdev_tx_t b44_start_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 struct b44 *bp = netdev_priv(dev);
Francois Romieuc7193692005-11-07 01:50:03 +0100940 int rc = NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 dma_addr_t mapping;
942 u32 len, entry, ctrl;
Dongdong Deng22580f82009-08-13 19:12:31 +0000943 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 len = skb->len;
Dongdong Deng22580f82009-08-13 19:12:31 +0000946 spin_lock_irqsave(&bp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 /* This is a hard error, log it. */
949 if (unlikely(TX_BUFFS_AVAIL(bp) < 1)) {
950 netif_stop_queue(dev);
Joe Perches2fc96ff2010-02-17 15:01:50 +0000951 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
Francois Romieuc7193692005-11-07 01:50:03 +0100952 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700955 mapping = dma_map_single(bp->sdev->dma_dev, skb->data, len, DMA_TO_DEVICE);
956 if (dma_mapping_error(bp->sdev->dma_dev, mapping) || mapping + len > DMA_BIT_MASK(30)) {
Stephen Hemmingerf65a7172007-06-04 13:25:38 -0700957 struct sk_buff *bounce_skb;
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 /* Chip can't handle DMA to/from >1GB, use bounce buffer */
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700960 if (!dma_mapping_error(bp->sdev->dma_dev, mapping))
961 dma_unmap_single(bp->sdev->dma_dev, mapping, len,
Michael Bueschf2257632008-06-20 11:50:29 +0200962 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
David S. Miller9034f772009-02-10 01:56:45 -0800964 bounce_skb = __netdev_alloc_skb(dev, len, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (!bounce_skb)
Francois Romieuc7193692005-11-07 01:50:03 +0100966 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700968 mapping = dma_map_single(bp->sdev->dma_dev, bounce_skb->data,
969 len, DMA_TO_DEVICE);
970 if (dma_mapping_error(bp->sdev->dma_dev, mapping) || mapping + len > DMA_BIT_MASK(30)) {
971 if (!dma_mapping_error(bp->sdev->dma_dev, mapping))
972 dma_unmap_single(bp->sdev->dma_dev, mapping,
Michael Bueschf2257632008-06-20 11:50:29 +0200973 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 dev_kfree_skb_any(bounce_skb);
Francois Romieuc7193692005-11-07 01:50:03 +0100975 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
Stephen Hemmingerf65a7172007-06-04 13:25:38 -0700978 skb_copy_from_linear_data(skb, skb_put(bounce_skb, len), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 dev_kfree_skb_any(skb);
980 skb = bounce_skb;
981 }
982
983 entry = bp->tx_prod;
984 bp->tx_buffers[entry].skb = skb;
Michael Buesch753f4922007-09-19 14:20:30 -0700985 bp->tx_buffers[entry].mapping = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 ctrl = (len & DESC_CTRL_LEN);
988 ctrl |= DESC_CTRL_IOC | DESC_CTRL_SOF | DESC_CTRL_EOF;
989 if (entry == (B44_TX_RING_SIZE - 1))
990 ctrl |= DESC_CTRL_EOT;
991
992 bp->tx_ring[entry].ctrl = cpu_to_le32(ctrl);
993 bp->tx_ring[entry].addr = cpu_to_le32((u32) mapping+bp->dma_offset);
994
John W. Linville9f38c632005-10-18 21:30:59 -0400995 if (bp->flags & B44_FLAG_TX_RING_HACK)
Michael Buesch753f4922007-09-19 14:20:30 -0700996 b44_sync_dma_desc_for_device(bp->sdev, bp->tx_ring_dma,
997 entry * sizeof(bp->tx_ring[0]),
998 DMA_TO_DEVICE);
John W. Linville9f38c632005-10-18 21:30:59 -0400999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 entry = NEXT_TX(entry);
1001
1002 bp->tx_prod = entry;
1003
1004 wmb();
1005
1006 bw32(bp, B44_DMATX_PTR, entry * sizeof(struct dma_desc));
1007 if (bp->flags & B44_FLAG_BUGGY_TXPTR)
1008 bw32(bp, B44_DMATX_PTR, entry * sizeof(struct dma_desc));
1009 if (bp->flags & B44_FLAG_REORDER_BUG)
1010 br32(bp, B44_DMATX_PTR);
1011
1012 if (TX_BUFFS_AVAIL(bp) < 1)
1013 netif_stop_queue(dev);
1014
Francois Romieuc7193692005-11-07 01:50:03 +01001015out_unlock:
Dongdong Deng22580f82009-08-13 19:12:31 +00001016 spin_unlock_irqrestore(&bp->lock, flags);
Francois Romieuc7193692005-11-07 01:50:03 +01001017
1018 return rc;
1019
1020err_out:
1021 rc = NETDEV_TX_BUSY;
1022 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
1025static int b44_change_mtu(struct net_device *dev, int new_mtu)
1026{
1027 struct b44 *bp = netdev_priv(dev);
1028
1029 if (new_mtu < B44_MIN_MTU || new_mtu > B44_MAX_MTU)
1030 return -EINVAL;
1031
1032 if (!netif_running(dev)) {
1033 /* We'll just catch it later when the
1034 * device is up'd.
1035 */
1036 dev->mtu = new_mtu;
1037 return 0;
1038 }
1039
1040 spin_lock_irq(&bp->lock);
1041 b44_halt(bp);
1042 dev->mtu = new_mtu;
1043 b44_init_rings(bp);
Michael Chan5fc7d612007-01-26 23:59:57 -08001044 b44_init_hw(bp, B44_FULL_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 spin_unlock_irq(&bp->lock);
1046
1047 b44_enable_ints(bp);
Jeff Garzik10badc22006-04-12 18:04:32 -04001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 0;
1050}
1051
1052/* Free up pending packets in all rx/tx rings.
1053 *
1054 * The chip has been shut down and the driver detached from
1055 * the networking, so no interrupts or new tx packets will
1056 * end up in the driver. bp->lock is not held and we are not
1057 * in an interrupt context and thus may sleep.
1058 */
1059static void b44_free_rings(struct b44 *bp)
1060{
1061 struct ring_info *rp;
1062 int i;
1063
1064 for (i = 0; i < B44_RX_RING_SIZE; i++) {
1065 rp = &bp->rx_buffers[i];
1066
1067 if (rp->skb == NULL)
1068 continue;
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001069 dma_unmap_single(bp->sdev->dma_dev, rp->mapping, RX_PKT_BUF_SZ,
1070 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 dev_kfree_skb_any(rp->skb);
1072 rp->skb = NULL;
1073 }
1074
1075 /* XXX needs changes once NETIF_F_SG is set... */
1076 for (i = 0; i < B44_TX_RING_SIZE; i++) {
1077 rp = &bp->tx_buffers[i];
1078
1079 if (rp->skb == NULL)
1080 continue;
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001081 dma_unmap_single(bp->sdev->dma_dev, rp->mapping, rp->skb->len,
1082 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 dev_kfree_skb_any(rp->skb);
1084 rp->skb = NULL;
1085 }
1086}
1087
1088/* Initialize tx/rx rings for packet processing.
1089 *
1090 * The chip has been shut down and the driver detached from
1091 * the networking, so no interrupts or new tx packets will
Francois Romieu874a6212005-11-07 01:50:46 +01001092 * end up in the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 */
1094static void b44_init_rings(struct b44 *bp)
1095{
1096 int i;
1097
1098 b44_free_rings(bp);
1099
1100 memset(bp->rx_ring, 0, B44_RX_RING_BYTES);
1101 memset(bp->tx_ring, 0, B44_TX_RING_BYTES);
1102
John W. Linville9f38c632005-10-18 21:30:59 -04001103 if (bp->flags & B44_FLAG_RX_RING_HACK)
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001104 dma_sync_single_for_device(bp->sdev->dma_dev, bp->rx_ring_dma,
1105 DMA_TABLE_BYTES, DMA_BIDIRECTIONAL);
John W. Linville9f38c632005-10-18 21:30:59 -04001106
1107 if (bp->flags & B44_FLAG_TX_RING_HACK)
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001108 dma_sync_single_for_device(bp->sdev->dma_dev, bp->tx_ring_dma,
1109 DMA_TABLE_BYTES, DMA_TO_DEVICE);
John W. Linville9f38c632005-10-18 21:30:59 -04001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 for (i = 0; i < bp->rx_pending; i++) {
1112 if (b44_alloc_rx_skb(bp, -1, i) < 0)
1113 break;
1114 }
1115}
1116
1117/*
1118 * Must not be invoked with interrupt sources disabled and
1119 * the hardware shutdown down.
1120 */
1121static void b44_free_consistent(struct b44 *bp)
1122{
Jesper Juhlb4558ea2005-10-28 16:53:13 -04001123 kfree(bp->rx_buffers);
1124 bp->rx_buffers = NULL;
1125 kfree(bp->tx_buffers);
1126 bp->tx_buffers = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (bp->rx_ring) {
John W. Linville9f38c632005-10-18 21:30:59 -04001128 if (bp->flags & B44_FLAG_RX_RING_HACK) {
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001129 dma_unmap_single(bp->sdev->dma_dev, bp->rx_ring_dma,
1130 DMA_TABLE_BYTES, DMA_BIDIRECTIONAL);
John W. Linville9f38c632005-10-18 21:30:59 -04001131 kfree(bp->rx_ring);
1132 } else
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001133 dma_free_coherent(bp->sdev->dma_dev, DMA_TABLE_BYTES,
1134 bp->rx_ring, bp->rx_ring_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 bp->rx_ring = NULL;
John W. Linville9f38c632005-10-18 21:30:59 -04001136 bp->flags &= ~B44_FLAG_RX_RING_HACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 if (bp->tx_ring) {
John W. Linville9f38c632005-10-18 21:30:59 -04001139 if (bp->flags & B44_FLAG_TX_RING_HACK) {
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001140 dma_unmap_single(bp->sdev->dma_dev, bp->tx_ring_dma,
1141 DMA_TABLE_BYTES, DMA_TO_DEVICE);
John W. Linville9f38c632005-10-18 21:30:59 -04001142 kfree(bp->tx_ring);
1143 } else
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001144 dma_free_coherent(bp->sdev->dma_dev, DMA_TABLE_BYTES,
1145 bp->tx_ring, bp->tx_ring_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 bp->tx_ring = NULL;
John W. Linville9f38c632005-10-18 21:30:59 -04001147 bp->flags &= ~B44_FLAG_TX_RING_HACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149}
1150
1151/*
1152 * Must not be invoked with interrupt sources disabled and
1153 * the hardware shutdown down. Can sleep.
1154 */
Michael Buesch753f4922007-09-19 14:20:30 -07001155static int b44_alloc_consistent(struct b44 *bp, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 int size;
1158
1159 size = B44_RX_RING_SIZE * sizeof(struct ring_info);
Michael Buesch753f4922007-09-19 14:20:30 -07001160 bp->rx_buffers = kzalloc(size, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (!bp->rx_buffers)
1162 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 size = B44_TX_RING_SIZE * sizeof(struct ring_info);
Michael Buesch753f4922007-09-19 14:20:30 -07001165 bp->tx_buffers = kzalloc(size, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (!bp->tx_buffers)
1167 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 size = DMA_TABLE_BYTES;
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001170 bp->rx_ring = dma_alloc_coherent(bp->sdev->dma_dev, size,
1171 &bp->rx_ring_dma, gfp);
John W. Linville9f38c632005-10-18 21:30:59 -04001172 if (!bp->rx_ring) {
1173 /* Allocation may have failed due to pci_alloc_consistent
1174 insisting on use of GFP_DMA, which is more restrictive
1175 than necessary... */
1176 struct dma_desc *rx_ring;
1177 dma_addr_t rx_ring_dma;
1178
Michael Buesch753f4922007-09-19 14:20:30 -07001179 rx_ring = kzalloc(size, gfp);
Francois Romieu874a6212005-11-07 01:50:46 +01001180 if (!rx_ring)
John W. Linville9f38c632005-10-18 21:30:59 -04001181 goto out_err;
1182
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001183 rx_ring_dma = dma_map_single(bp->sdev->dma_dev, rx_ring,
1184 DMA_TABLE_BYTES,
1185 DMA_BIDIRECTIONAL);
John W. Linville9f38c632005-10-18 21:30:59 -04001186
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001187 if (dma_mapping_error(bp->sdev->dma_dev, rx_ring_dma) ||
Yang Hongyang28b76792009-04-06 19:01:17 -07001188 rx_ring_dma + size > DMA_BIT_MASK(30)) {
John W. Linville9f38c632005-10-18 21:30:59 -04001189 kfree(rx_ring);
1190 goto out_err;
1191 }
1192
1193 bp->rx_ring = rx_ring;
1194 bp->rx_ring_dma = rx_ring_dma;
1195 bp->flags |= B44_FLAG_RX_RING_HACK;
1196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001198 bp->tx_ring = dma_alloc_coherent(bp->sdev->dma_dev, size,
1199 &bp->tx_ring_dma, gfp);
John W. Linville9f38c632005-10-18 21:30:59 -04001200 if (!bp->tx_ring) {
Michael Bueschf2257632008-06-20 11:50:29 +02001201 /* Allocation may have failed due to ssb_dma_alloc_consistent
John W. Linville9f38c632005-10-18 21:30:59 -04001202 insisting on use of GFP_DMA, which is more restrictive
1203 than necessary... */
1204 struct dma_desc *tx_ring;
1205 dma_addr_t tx_ring_dma;
1206
Michael Buesch753f4922007-09-19 14:20:30 -07001207 tx_ring = kzalloc(size, gfp);
Francois Romieu874a6212005-11-07 01:50:46 +01001208 if (!tx_ring)
John W. Linville9f38c632005-10-18 21:30:59 -04001209 goto out_err;
1210
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001211 tx_ring_dma = dma_map_single(bp->sdev->dma_dev, tx_ring,
1212 DMA_TABLE_BYTES,
1213 DMA_TO_DEVICE);
John W. Linville9f38c632005-10-18 21:30:59 -04001214
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001215 if (dma_mapping_error(bp->sdev->dma_dev, tx_ring_dma) ||
Yang Hongyang28b76792009-04-06 19:01:17 -07001216 tx_ring_dma + size > DMA_BIT_MASK(30)) {
John W. Linville9f38c632005-10-18 21:30:59 -04001217 kfree(tx_ring);
1218 goto out_err;
1219 }
1220
1221 bp->tx_ring = tx_ring;
1222 bp->tx_ring_dma = tx_ring_dma;
1223 bp->flags |= B44_FLAG_TX_RING_HACK;
1224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 return 0;
1227
1228out_err:
1229 b44_free_consistent(bp);
1230 return -ENOMEM;
1231}
1232
1233/* bp->lock is held. */
1234static void b44_clear_stats(struct b44 *bp)
1235{
1236 unsigned long reg;
1237
1238 bw32(bp, B44_MIB_CTRL, MIB_CTRL_CLR_ON_READ);
1239 for (reg = B44_TX_GOOD_O; reg <= B44_TX_PAUSE; reg += 4UL)
1240 br32(bp, reg);
1241 for (reg = B44_RX_GOOD_O; reg <= B44_RX_NPAUSE; reg += 4UL)
1242 br32(bp, reg);
1243}
1244
1245/* bp->lock is held. */
Miguel Botónfedb0ee2008-01-01 01:17:54 +01001246static void b44_chip_reset(struct b44 *bp, int reset_kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Michael Buesch753f4922007-09-19 14:20:30 -07001248 struct ssb_device *sdev = bp->sdev;
Michael Bueschf8af11a2009-02-26 22:33:00 -08001249 bool was_enabled;
Michael Buesch753f4922007-09-19 14:20:30 -07001250
Michael Bueschf8af11a2009-02-26 22:33:00 -08001251 was_enabled = ssb_device_is_enabled(bp->sdev);
1252
1253 ssb_device_enable(bp->sdev, 0);
1254 ssb_pcicore_dev_irqvecs_enable(&sdev->bus->pcicore, sdev);
1255
1256 if (was_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 bw32(bp, B44_RCV_LAZY, 0);
1258 bw32(bp, B44_ENET_CTRL, ENET_CTRL_DISABLE);
Gary Zambrano40ee8c72007-02-16 13:27:27 -08001259 b44_wait_bit(bp, B44_ENET_CTRL, ENET_CTRL_DISABLE, 200, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 bw32(bp, B44_DMATX_CTRL, 0);
1261 bp->tx_prod = bp->tx_cons = 0;
1262 if (br32(bp, B44_DMARX_STAT) & DMARX_STAT_EMASK) {
1263 b44_wait_bit(bp, B44_DMARX_STAT, DMARX_STAT_SIDLE,
1264 100, 0);
1265 }
1266 bw32(bp, B44_DMARX_CTRL, 0);
1267 bp->rx_prod = bp->rx_cons = 0;
Michael Bueschf8af11a2009-02-26 22:33:00 -08001268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 b44_clear_stats(bp);
1271
Miguel Botónfedb0ee2008-01-01 01:17:54 +01001272 /*
1273 * Don't enable PHY if we are doing a partial reset
1274 * we are probably going to power down
1275 */
1276 if (reset_kind == B44_CHIP_RESET_PARTIAL)
1277 return;
1278
Michael Buesch753f4922007-09-19 14:20:30 -07001279 switch (sdev->bus->bustype) {
1280 case SSB_BUSTYPE_SSB:
1281 bw32(bp, B44_MDIO_CTRL, (MDIO_CTRL_PREAMBLE |
Julia Lawall39506a52009-08-01 09:51:06 +00001282 (DIV_ROUND_CLOSEST(ssb_clockspeed(sdev->bus),
1283 B44_MDC_RATIO)
Michael Buesch753f4922007-09-19 14:20:30 -07001284 & MDIO_CTRL_MAXF_MASK)));
1285 break;
1286 case SSB_BUSTYPE_PCI:
Michael Buesch753f4922007-09-19 14:20:30 -07001287 bw32(bp, B44_MDIO_CTRL, (MDIO_CTRL_PREAMBLE |
1288 (0x0d & MDIO_CTRL_MAXF_MASK)));
1289 break;
Michael Buesch98a1e2a2009-09-08 19:33:31 +02001290 case SSB_BUSTYPE_PCMCIA:
1291 case SSB_BUSTYPE_SDIO:
1292 WARN_ON(1); /* A device with this bus does not exist. */
1293 break;
Michael Buesch753f4922007-09-19 14:20:30 -07001294 }
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 br32(bp, B44_MDIO_CTRL);
1297
1298 if (!(br32(bp, B44_DEVCTRL) & DEVCTRL_IPP)) {
1299 bw32(bp, B44_ENET_CTRL, ENET_CTRL_EPSEL);
1300 br32(bp, B44_ENET_CTRL);
1301 bp->flags &= ~B44_FLAG_INTERNAL_PHY;
1302 } else {
1303 u32 val = br32(bp, B44_DEVCTRL);
1304
1305 if (val & DEVCTRL_EPR) {
1306 bw32(bp, B44_DEVCTRL, (val & ~DEVCTRL_EPR));
1307 br32(bp, B44_DEVCTRL);
1308 udelay(100);
1309 }
1310 bp->flags |= B44_FLAG_INTERNAL_PHY;
1311 }
1312}
1313
1314/* bp->lock is held. */
1315static void b44_halt(struct b44 *bp)
1316{
1317 b44_disable_ints(bp);
Miguel Botónfedb0ee2008-01-01 01:17:54 +01001318 /* reset PHY */
1319 b44_phy_reset(bp);
1320 /* power down PHY */
Joe Perches2fc96ff2010-02-17 15:01:50 +00001321 netdev_info(bp->dev, "powering down PHY\n");
Miguel Botónfedb0ee2008-01-01 01:17:54 +01001322 bw32(bp, B44_MAC_CTRL, MAC_CTRL_PHY_PDOWN);
1323 /* now reset the chip, but without enabling the MAC&PHY
1324 * part of it. This has to be done _after_ we shut down the PHY */
1325 b44_chip_reset(bp, B44_CHIP_RESET_PARTIAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
1328/* bp->lock is held. */
1329static void __b44_set_mac_addr(struct b44 *bp)
1330{
1331 bw32(bp, B44_CAM_CTRL, 0);
1332 if (!(bp->dev->flags & IFF_PROMISC)) {
1333 u32 val;
1334
1335 __b44_cam_write(bp, bp->dev->dev_addr, 0);
1336 val = br32(bp, B44_CAM_CTRL);
1337 bw32(bp, B44_CAM_CTRL, val | CAM_CTRL_ENABLE);
1338 }
1339}
1340
1341static int b44_set_mac_addr(struct net_device *dev, void *p)
1342{
1343 struct b44 *bp = netdev_priv(dev);
1344 struct sockaddr *addr = p;
Michael Buesch753f4922007-09-19 14:20:30 -07001345 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 if (netif_running(dev))
1348 return -EBUSY;
1349
Gary Zambrano391fc092006-03-28 14:57:38 -08001350 if (!is_valid_ether_addr(addr->sa_data))
1351 return -EINVAL;
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1354
1355 spin_lock_irq(&bp->lock);
Michael Buesch753f4922007-09-19 14:20:30 -07001356
1357 val = br32(bp, B44_RXCONFIG);
1358 if (!(val & RXCONFIG_CAM_ABSENT))
1359 __b44_set_mac_addr(bp);
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 spin_unlock_irq(&bp->lock);
1362
1363 return 0;
1364}
1365
1366/* Called at device open time to get the chip ready for
1367 * packet processing. Invoked with bp->lock held.
1368 */
1369static void __b44_set_rx_mode(struct net_device *);
Michael Chan5fc7d612007-01-26 23:59:57 -08001370static void b44_init_hw(struct b44 *bp, int reset_kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 u32 val;
1373
Miguel Botónfedb0ee2008-01-01 01:17:54 +01001374 b44_chip_reset(bp, B44_CHIP_RESET_FULL);
Michael Chan5fc7d612007-01-26 23:59:57 -08001375 if (reset_kind == B44_FULL_RESET) {
Gary Zambrano00e8b3a2006-06-20 15:34:26 -07001376 b44_phy_reset(bp);
1377 b44_setup_phy(bp);
1378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /* Enable CRC32, set proper LED modes and power on PHY */
1381 bw32(bp, B44_MAC_CTRL, MAC_CTRL_CRC32_ENAB | MAC_CTRL_PHY_LEDCTRL);
1382 bw32(bp, B44_RCV_LAZY, (1 << RCV_LAZY_FC_SHIFT));
1383
1384 /* This sets the MAC address too. */
1385 __b44_set_rx_mode(bp->dev);
1386
1387 /* MTU + eth header + possible VLAN tag + struct rx_header */
1388 bw32(bp, B44_RXMAXLEN, bp->dev->mtu + ETH_HLEN + 8 + RX_HEADER_LEN);
1389 bw32(bp, B44_TXMAXLEN, bp->dev->mtu + ETH_HLEN + 8 + RX_HEADER_LEN);
1390
1391 bw32(bp, B44_TX_WMARK, 56); /* XXX magic */
Michael Chan5fc7d612007-01-26 23:59:57 -08001392 if (reset_kind == B44_PARTIAL_RESET) {
1393 bw32(bp, B44_DMARX_CTRL, (DMARX_CTRL_ENABLE |
Stephen Hemminger72f48612007-06-04 13:25:39 -07001394 (RX_PKT_OFFSET << DMARX_CTRL_ROSHIFT)));
Michael Chan5fc7d612007-01-26 23:59:57 -08001395 } else {
Gary Zambrano00e8b3a2006-06-20 15:34:26 -07001396 bw32(bp, B44_DMATX_CTRL, DMATX_CTRL_ENABLE);
1397 bw32(bp, B44_DMATX_ADDR, bp->tx_ring_dma + bp->dma_offset);
1398 bw32(bp, B44_DMARX_CTRL, (DMARX_CTRL_ENABLE |
Stephen Hemminger72f48612007-06-04 13:25:39 -07001399 (RX_PKT_OFFSET << DMARX_CTRL_ROSHIFT)));
Gary Zambrano00e8b3a2006-06-20 15:34:26 -07001400 bw32(bp, B44_DMARX_ADDR, bp->rx_ring_dma + bp->dma_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Gary Zambrano00e8b3a2006-06-20 15:34:26 -07001402 bw32(bp, B44_DMARX_PTR, bp->rx_pending);
1403 bp->rx_prod = bp->rx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Gary Zambrano00e8b3a2006-06-20 15:34:26 -07001405 bw32(bp, B44_MIB_CTRL, MIB_CTRL_CLR_ON_READ);
Gary Zambrano00e8b3a2006-06-20 15:34:26 -07001406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 val = br32(bp, B44_ENET_CTRL);
1409 bw32(bp, B44_ENET_CTRL, (val | ENET_CTRL_ENABLE));
1410}
1411
1412static int b44_open(struct net_device *dev)
1413{
1414 struct b44 *bp = netdev_priv(dev);
1415 int err;
1416
Michael Buesch753f4922007-09-19 14:20:30 -07001417 err = b44_alloc_consistent(bp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (err)
Francois Romieu6c2f4262005-11-07 01:52:57 +01001419 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001421 napi_enable(&bp->napi);
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 b44_init_rings(bp);
Michael Chan5fc7d612007-01-26 23:59:57 -08001424 b44_init_hw(bp, B44_FULL_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
John W. Linvillee254e9b2005-06-08 15:11:57 -04001426 b44_check_phy(bp);
1427
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001428 err = request_irq(dev->irq, b44_interrupt, IRQF_SHARED, dev->name, dev);
Francois Romieu6c2f4262005-11-07 01:52:57 +01001429 if (unlikely(err < 0)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001430 napi_disable(&bp->napi);
Miguel Botónfedb0ee2008-01-01 01:17:54 +01001431 b44_chip_reset(bp, B44_CHIP_RESET_PARTIAL);
Francois Romieu6c2f4262005-11-07 01:52:57 +01001432 b44_free_rings(bp);
1433 b44_free_consistent(bp);
1434 goto out;
1435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 init_timer(&bp->timer);
1438 bp->timer.expires = jiffies + HZ;
1439 bp->timer.data = (unsigned long) bp;
1440 bp->timer.function = b44_timer;
1441 add_timer(&bp->timer);
1442
1443 b44_enable_ints(bp);
Mark Lordd9e2d182005-11-30 22:30:23 +01001444 netif_start_queue(dev);
Francois Romieu6c2f4262005-11-07 01:52:57 +01001445out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return err;
1447}
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449#ifdef CONFIG_NET_POLL_CONTROLLER
1450/*
1451 * Polling receive - used by netconsole and other diagnostic tools
1452 * to allow network i/o with interrupts disabled.
1453 */
1454static void b44_poll_controller(struct net_device *dev)
1455{
1456 disable_irq(dev->irq);
David Howells7d12e782006-10-05 14:55:46 +01001457 b44_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 enable_irq(dev->irq);
1459}
1460#endif
1461
Gary Zambrano725ad802006-06-20 15:34:36 -07001462static void bwfilter_table(struct b44 *bp, u8 *pp, u32 bytes, u32 table_offset)
1463{
1464 u32 i;
1465 u32 *pattern = (u32 *) pp;
1466
1467 for (i = 0; i < bytes; i += sizeof(u32)) {
1468 bw32(bp, B44_FILT_ADDR, table_offset + i);
1469 bw32(bp, B44_FILT_DATA, pattern[i / sizeof(u32)]);
1470 }
1471}
1472
1473static int b44_magic_pattern(u8 *macaddr, u8 *ppattern, u8 *pmask, int offset)
1474{
1475 int magicsync = 6;
1476 int k, j, len = offset;
1477 int ethaddr_bytes = ETH_ALEN;
1478
1479 memset(ppattern + offset, 0xff, magicsync);
1480 for (j = 0; j < magicsync; j++)
1481 set_bit(len++, (unsigned long *) pmask);
1482
1483 for (j = 0; j < B44_MAX_PATTERNS; j++) {
1484 if ((B44_PATTERN_SIZE - len) >= ETH_ALEN)
1485 ethaddr_bytes = ETH_ALEN;
1486 else
1487 ethaddr_bytes = B44_PATTERN_SIZE - len;
1488 if (ethaddr_bytes <=0)
1489 break;
1490 for (k = 0; k< ethaddr_bytes; k++) {
1491 ppattern[offset + magicsync +
1492 (j * ETH_ALEN) + k] = macaddr[k];
Stanislav Brabece0188822009-12-08 21:00:22 -08001493 set_bit(len++, (unsigned long *) pmask);
Gary Zambrano725ad802006-06-20 15:34:36 -07001494 }
1495 }
1496 return len - 1;
1497}
1498
1499/* Setup magic packet patterns in the b44 WOL
1500 * pattern matching filter.
1501 */
1502static void b44_setup_pseudo_magicp(struct b44 *bp)
1503{
1504
1505 u32 val;
1506 int plen0, plen1, plen2;
1507 u8 *pwol_pattern;
1508 u8 pwol_mask[B44_PMASK_SIZE];
1509
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001510 pwol_pattern = kzalloc(B44_PATTERN_SIZE, GFP_KERNEL);
Gary Zambrano725ad802006-06-20 15:34:36 -07001511 if (!pwol_pattern) {
Joe Perches2fc96ff2010-02-17 15:01:50 +00001512 pr_err("Memory not available for WOL\n");
Gary Zambrano725ad802006-06-20 15:34:36 -07001513 return;
1514 }
1515
1516 /* Ipv4 magic packet pattern - pattern 0.*/
Gary Zambrano725ad802006-06-20 15:34:36 -07001517 memset(pwol_mask, 0, B44_PMASK_SIZE);
1518 plen0 = b44_magic_pattern(bp->dev->dev_addr, pwol_pattern, pwol_mask,
1519 B44_ETHIPV4UDP_HLEN);
1520
1521 bwfilter_table(bp, pwol_pattern, B44_PATTERN_SIZE, B44_PATTERN_BASE);
1522 bwfilter_table(bp, pwol_mask, B44_PMASK_SIZE, B44_PMASK_BASE);
1523
1524 /* Raw ethernet II magic packet pattern - pattern 1 */
1525 memset(pwol_pattern, 0, B44_PATTERN_SIZE);
1526 memset(pwol_mask, 0, B44_PMASK_SIZE);
1527 plen1 = b44_magic_pattern(bp->dev->dev_addr, pwol_pattern, pwol_mask,
1528 ETH_HLEN);
1529
1530 bwfilter_table(bp, pwol_pattern, B44_PATTERN_SIZE,
1531 B44_PATTERN_BASE + B44_PATTERN_SIZE);
1532 bwfilter_table(bp, pwol_mask, B44_PMASK_SIZE,
1533 B44_PMASK_BASE + B44_PMASK_SIZE);
1534
1535 /* Ipv6 magic packet pattern - pattern 2 */
1536 memset(pwol_pattern, 0, B44_PATTERN_SIZE);
1537 memset(pwol_mask, 0, B44_PMASK_SIZE);
1538 plen2 = b44_magic_pattern(bp->dev->dev_addr, pwol_pattern, pwol_mask,
1539 B44_ETHIPV6UDP_HLEN);
1540
1541 bwfilter_table(bp, pwol_pattern, B44_PATTERN_SIZE,
1542 B44_PATTERN_BASE + B44_PATTERN_SIZE + B44_PATTERN_SIZE);
1543 bwfilter_table(bp, pwol_mask, B44_PMASK_SIZE,
1544 B44_PMASK_BASE + B44_PMASK_SIZE + B44_PMASK_SIZE);
1545
1546 kfree(pwol_pattern);
1547
1548 /* set these pattern's lengths: one less than each real length */
1549 val = plen0 | (plen1 << 8) | (plen2 << 16) | WKUP_LEN_ENABLE_THREE;
1550 bw32(bp, B44_WKUP_LEN, val);
1551
1552 /* enable wakeup pattern matching */
1553 val = br32(bp, B44_DEVCTRL);
1554 bw32(bp, B44_DEVCTRL, val | DEVCTRL_PFE);
1555
1556}
Gary Zambrano52cafd92006-06-20 15:34:23 -07001557
Michael Buesch753f4922007-09-19 14:20:30 -07001558#ifdef CONFIG_B44_PCI
1559static void b44_setup_wol_pci(struct b44 *bp)
1560{
1561 u16 val;
1562
1563 if (bp->sdev->bus->bustype != SSB_BUSTYPE_SSB) {
1564 bw32(bp, SSB_TMSLOW, br32(bp, SSB_TMSLOW) | SSB_TMSLOW_PE);
1565 pci_read_config_word(bp->sdev->bus->host_pci, SSB_PMCSR, &val);
1566 pci_write_config_word(bp->sdev->bus->host_pci, SSB_PMCSR, val | SSB_PE);
1567 }
1568}
1569#else
1570static inline void b44_setup_wol_pci(struct b44 *bp) { }
1571#endif /* CONFIG_B44_PCI */
1572
Gary Zambrano52cafd92006-06-20 15:34:23 -07001573static void b44_setup_wol(struct b44 *bp)
1574{
1575 u32 val;
Gary Zambrano52cafd92006-06-20 15:34:23 -07001576
1577 bw32(bp, B44_RXCONFIG, RXCONFIG_ALLMULTI);
1578
1579 if (bp->flags & B44_FLAG_B0_ANDLATER) {
1580
1581 bw32(bp, B44_WKUP_LEN, WKUP_LEN_DISABLE);
1582
1583 val = bp->dev->dev_addr[2] << 24 |
1584 bp->dev->dev_addr[3] << 16 |
1585 bp->dev->dev_addr[4] << 8 |
1586 bp->dev->dev_addr[5];
1587 bw32(bp, B44_ADDR_LO, val);
1588
1589 val = bp->dev->dev_addr[0] << 8 |
1590 bp->dev->dev_addr[1];
1591 bw32(bp, B44_ADDR_HI, val);
1592
1593 val = br32(bp, B44_DEVCTRL);
1594 bw32(bp, B44_DEVCTRL, val | DEVCTRL_MPM | DEVCTRL_PFE);
1595
Gary Zambrano725ad802006-06-20 15:34:36 -07001596 } else {
1597 b44_setup_pseudo_magicp(bp);
1598 }
Michael Buesch753f4922007-09-19 14:20:30 -07001599 b44_setup_wol_pci(bp);
Gary Zambrano52cafd92006-06-20 15:34:23 -07001600}
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602static int b44_close(struct net_device *dev)
1603{
1604 struct b44 *bp = netdev_priv(dev);
1605
1606 netif_stop_queue(dev);
1607
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001608 napi_disable(&bp->napi);
Francois Romieuba5eec92005-11-08 23:37:12 +01001609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 del_timer_sync(&bp->timer);
1611
1612 spin_lock_irq(&bp->lock);
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 b44_halt(bp);
1615 b44_free_rings(bp);
Stephen Hemmingerc35ca392006-01-20 21:13:17 -08001616 netif_carrier_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 spin_unlock_irq(&bp->lock);
1619
1620 free_irq(dev->irq, dev);
1621
Gary Zambrano52cafd92006-06-20 15:34:23 -07001622 if (bp->flags & B44_FLAG_WOL_ENABLE) {
Michael Chan5fc7d612007-01-26 23:59:57 -08001623 b44_init_hw(bp, B44_PARTIAL_RESET);
Gary Zambrano52cafd92006-06-20 15:34:23 -07001624 b44_setup_wol(bp);
1625 }
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 b44_free_consistent(bp);
1628
1629 return 0;
1630}
1631
1632static struct net_device_stats *b44_get_stats(struct net_device *dev)
1633{
1634 struct b44 *bp = netdev_priv(dev);
Eric Dumazet553e2332009-05-27 10:34:50 +00001635 struct net_device_stats *nstat = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 struct b44_hw_stats *hwstat = &bp->hw_stats;
1637
1638 /* Convert HW stats into netdevice stats. */
1639 nstat->rx_packets = hwstat->rx_pkts;
1640 nstat->tx_packets = hwstat->tx_pkts;
1641 nstat->rx_bytes = hwstat->rx_octets;
1642 nstat->tx_bytes = hwstat->tx_octets;
1643 nstat->tx_errors = (hwstat->tx_jabber_pkts +
1644 hwstat->tx_oversize_pkts +
1645 hwstat->tx_underruns +
1646 hwstat->tx_excessive_cols +
1647 hwstat->tx_late_cols);
1648 nstat->multicast = hwstat->tx_multicast_pkts;
1649 nstat->collisions = hwstat->tx_total_cols;
1650
1651 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1652 hwstat->rx_undersize);
1653 nstat->rx_over_errors = hwstat->rx_missed_pkts;
1654 nstat->rx_frame_errors = hwstat->rx_align_errs;
1655 nstat->rx_crc_errors = hwstat->rx_crc_errs;
1656 nstat->rx_errors = (hwstat->rx_jabber_pkts +
1657 hwstat->rx_oversize_pkts +
1658 hwstat->rx_missed_pkts +
1659 hwstat->rx_crc_align_errs +
1660 hwstat->rx_undersize +
1661 hwstat->rx_crc_errs +
1662 hwstat->rx_align_errs +
1663 hwstat->rx_symbol_errs);
1664
1665 nstat->tx_aborted_errors = hwstat->tx_underruns;
1666#if 0
1667 /* Carrier lost counter seems to be broken for some devices */
1668 nstat->tx_carrier_errors = hwstat->tx_carrier_lost;
1669#endif
1670
1671 return nstat;
1672}
1673
1674static int __b44_load_mcast(struct b44 *bp, struct net_device *dev)
1675{
Jiri Pirko22bedad2010-04-01 21:22:57 +00001676 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 int i, num_ents;
1678
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001679 num_ents = min_t(int, netdev_mc_count(dev), B44_MCAST_TABLE_SIZE);
Jiri Pirko0ddf4772010-02-20 00:13:58 +00001680 i = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00001681 netdev_for_each_mc_addr(ha, dev) {
Jiri Pirko0ddf4772010-02-20 00:13:58 +00001682 if (i == num_ents)
1683 break;
Jiri Pirko22bedad2010-04-01 21:22:57 +00001684 __b44_cam_write(bp, ha->addr, i++ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686 return i+1;
1687}
1688
1689static void __b44_set_rx_mode(struct net_device *dev)
1690{
1691 struct b44 *bp = netdev_priv(dev);
1692 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 val = br32(bp, B44_RXCONFIG);
1695 val &= ~(RXCONFIG_PROMISC | RXCONFIG_ALLMULTI);
Michael Buesch753f4922007-09-19 14:20:30 -07001696 if ((dev->flags & IFF_PROMISC) || (val & RXCONFIG_CAM_ABSENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 val |= RXCONFIG_PROMISC;
1698 bw32(bp, B44_RXCONFIG, val);
1699 } else {
Francois Romieu874a6212005-11-07 01:50:46 +01001700 unsigned char zero[6] = {0, 0, 0, 0, 0, 0};
Bill Helfinstinecda22aa2007-04-01 13:10:28 -04001701 int i = 1;
Francois Romieu874a6212005-11-07 01:50:46 +01001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 __b44_set_mac_addr(bp);
1704
Jeff Garzik2f614fe2006-10-05 07:10:38 -04001705 if ((dev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001706 (netdev_mc_count(dev) > B44_MCAST_TABLE_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 val |= RXCONFIG_ALLMULTI;
1708 else
Francois Romieu874a6212005-11-07 01:50:46 +01001709 i = __b44_load_mcast(bp, dev);
Jeff Garzik10badc22006-04-12 18:04:32 -04001710
Jeff Garzik2f614fe2006-10-05 07:10:38 -04001711 for (; i < 64; i++)
Jeff Garzik10badc22006-04-12 18:04:32 -04001712 __b44_cam_write(bp, zero, i);
Jeff Garzik2f614fe2006-10-05 07:10:38 -04001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 bw32(bp, B44_RXCONFIG, val);
1715 val = br32(bp, B44_CAM_CTRL);
1716 bw32(bp, B44_CAM_CTRL, val | CAM_CTRL_ENABLE);
1717 }
1718}
1719
1720static void b44_set_rx_mode(struct net_device *dev)
1721{
1722 struct b44 *bp = netdev_priv(dev);
1723
1724 spin_lock_irq(&bp->lock);
1725 __b44_set_rx_mode(dev);
1726 spin_unlock_irq(&bp->lock);
1727}
1728
1729static u32 b44_get_msglevel(struct net_device *dev)
1730{
1731 struct b44 *bp = netdev_priv(dev);
1732 return bp->msg_enable;
1733}
1734
1735static void b44_set_msglevel(struct net_device *dev, u32 value)
1736{
1737 struct b44 *bp = netdev_priv(dev);
1738 bp->msg_enable = value;
1739}
1740
1741static void b44_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
1742{
1743 struct b44 *bp = netdev_priv(dev);
Michael Buesch753f4922007-09-19 14:20:30 -07001744 struct ssb_bus *bus = bp->sdev->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
roel kluin27e09552009-07-17 08:01:54 +00001746 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
1747 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
Michael Buesch753f4922007-09-19 14:20:30 -07001748 switch (bus->bustype) {
1749 case SSB_BUSTYPE_PCI:
roel kluin27e09552009-07-17 08:01:54 +00001750 strlcpy(info->bus_info, pci_name(bus->host_pci), sizeof(info->bus_info));
Michael Buesch753f4922007-09-19 14:20:30 -07001751 break;
Michael Buesch753f4922007-09-19 14:20:30 -07001752 case SSB_BUSTYPE_SSB:
roel kluin27e09552009-07-17 08:01:54 +00001753 strlcpy(info->bus_info, "SSB", sizeof(info->bus_info));
Michael Buesch753f4922007-09-19 14:20:30 -07001754 break;
Michael Buesch98a1e2a2009-09-08 19:33:31 +02001755 case SSB_BUSTYPE_PCMCIA:
1756 case SSB_BUSTYPE_SDIO:
1757 WARN_ON(1); /* A device with this bus does not exist. */
1758 break;
Michael Buesch753f4922007-09-19 14:20:30 -07001759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
1762static int b44_nway_reset(struct net_device *dev)
1763{
1764 struct b44 *bp = netdev_priv(dev);
1765 u32 bmcr;
1766 int r;
1767
1768 spin_lock_irq(&bp->lock);
1769 b44_readphy(bp, MII_BMCR, &bmcr);
1770 b44_readphy(bp, MII_BMCR, &bmcr);
1771 r = -EINVAL;
1772 if (bmcr & BMCR_ANENABLE) {
1773 b44_writephy(bp, MII_BMCR,
1774 bmcr | BMCR_ANRESTART);
1775 r = 0;
1776 }
1777 spin_unlock_irq(&bp->lock);
1778
1779 return r;
1780}
1781
1782static int b44_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1783{
1784 struct b44 *bp = netdev_priv(dev);
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 cmd->supported = (SUPPORTED_Autoneg);
1787 cmd->supported |= (SUPPORTED_100baseT_Half |
1788 SUPPORTED_100baseT_Full |
1789 SUPPORTED_10baseT_Half |
1790 SUPPORTED_10baseT_Full |
1791 SUPPORTED_MII);
1792
1793 cmd->advertising = 0;
1794 if (bp->flags & B44_FLAG_ADV_10HALF)
Matthew Wilcoxadf6e002005-10-04 11:25:17 -06001795 cmd->advertising |= ADVERTISED_10baseT_Half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (bp->flags & B44_FLAG_ADV_10FULL)
Matthew Wilcoxadf6e002005-10-04 11:25:17 -06001797 cmd->advertising |= ADVERTISED_10baseT_Full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (bp->flags & B44_FLAG_ADV_100HALF)
Matthew Wilcoxadf6e002005-10-04 11:25:17 -06001799 cmd->advertising |= ADVERTISED_100baseT_Half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (bp->flags & B44_FLAG_ADV_100FULL)
Matthew Wilcoxadf6e002005-10-04 11:25:17 -06001801 cmd->advertising |= ADVERTISED_100baseT_Full;
1802 cmd->advertising |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 cmd->speed = (bp->flags & B44_FLAG_100_BASE_T) ?
1804 SPEED_100 : SPEED_10;
1805 cmd->duplex = (bp->flags & B44_FLAG_FULL_DUPLEX) ?
1806 DUPLEX_FULL : DUPLEX_HALF;
1807 cmd->port = 0;
1808 cmd->phy_address = bp->phy_addr;
1809 cmd->transceiver = (bp->flags & B44_FLAG_INTERNAL_PHY) ?
1810 XCVR_INTERNAL : XCVR_EXTERNAL;
1811 cmd->autoneg = (bp->flags & B44_FLAG_FORCE_LINK) ?
1812 AUTONEG_DISABLE : AUTONEG_ENABLE;
Gary Zambrano47b9c3b2006-06-20 15:34:15 -07001813 if (cmd->autoneg == AUTONEG_ENABLE)
1814 cmd->advertising |= ADVERTISED_Autoneg;
1815 if (!netif_running(dev)){
1816 cmd->speed = 0;
1817 cmd->duplex = 0xff;
1818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 cmd->maxtxpkt = 0;
1820 cmd->maxrxpkt = 0;
1821 return 0;
1822}
1823
1824static int b44_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1825{
1826 struct b44 *bp = netdev_priv(dev);
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* We do not support gigabit. */
1829 if (cmd->autoneg == AUTONEG_ENABLE) {
1830 if (cmd->advertising &
1831 (ADVERTISED_1000baseT_Half |
1832 ADVERTISED_1000baseT_Full))
1833 return -EINVAL;
1834 } else if ((cmd->speed != SPEED_100 &&
1835 cmd->speed != SPEED_10) ||
1836 (cmd->duplex != DUPLEX_HALF &&
1837 cmd->duplex != DUPLEX_FULL)) {
1838 return -EINVAL;
1839 }
1840
1841 spin_lock_irq(&bp->lock);
1842
1843 if (cmd->autoneg == AUTONEG_ENABLE) {
Gary Zambrano47b9c3b2006-06-20 15:34:15 -07001844 bp->flags &= ~(B44_FLAG_FORCE_LINK |
1845 B44_FLAG_100_BASE_T |
1846 B44_FLAG_FULL_DUPLEX |
1847 B44_FLAG_ADV_10HALF |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 B44_FLAG_ADV_10FULL |
1849 B44_FLAG_ADV_100HALF |
1850 B44_FLAG_ADV_100FULL);
Gary Zambrano47b9c3b2006-06-20 15:34:15 -07001851 if (cmd->advertising == 0) {
1852 bp->flags |= (B44_FLAG_ADV_10HALF |
1853 B44_FLAG_ADV_10FULL |
1854 B44_FLAG_ADV_100HALF |
1855 B44_FLAG_ADV_100FULL);
1856 } else {
1857 if (cmd->advertising & ADVERTISED_10baseT_Half)
1858 bp->flags |= B44_FLAG_ADV_10HALF;
1859 if (cmd->advertising & ADVERTISED_10baseT_Full)
1860 bp->flags |= B44_FLAG_ADV_10FULL;
1861 if (cmd->advertising & ADVERTISED_100baseT_Half)
1862 bp->flags |= B44_FLAG_ADV_100HALF;
1863 if (cmd->advertising & ADVERTISED_100baseT_Full)
1864 bp->flags |= B44_FLAG_ADV_100FULL;
1865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 } else {
1867 bp->flags |= B44_FLAG_FORCE_LINK;
Gary Zambrano47b9c3b2006-06-20 15:34:15 -07001868 bp->flags &= ~(B44_FLAG_100_BASE_T | B44_FLAG_FULL_DUPLEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (cmd->speed == SPEED_100)
1870 bp->flags |= B44_FLAG_100_BASE_T;
1871 if (cmd->duplex == DUPLEX_FULL)
1872 bp->flags |= B44_FLAG_FULL_DUPLEX;
1873 }
1874
Gary Zambrano47b9c3b2006-06-20 15:34:15 -07001875 if (netif_running(dev))
1876 b44_setup_phy(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 spin_unlock_irq(&bp->lock);
1879
1880 return 0;
1881}
1882
1883static void b44_get_ringparam(struct net_device *dev,
1884 struct ethtool_ringparam *ering)
1885{
1886 struct b44 *bp = netdev_priv(dev);
1887
1888 ering->rx_max_pending = B44_RX_RING_SIZE - 1;
1889 ering->rx_pending = bp->rx_pending;
1890
1891 /* XXX ethtool lacks a tx_max_pending, oops... */
1892}
1893
1894static int b44_set_ringparam(struct net_device *dev,
1895 struct ethtool_ringparam *ering)
1896{
1897 struct b44 *bp = netdev_priv(dev);
1898
1899 if ((ering->rx_pending > B44_RX_RING_SIZE - 1) ||
1900 (ering->rx_mini_pending != 0) ||
1901 (ering->rx_jumbo_pending != 0) ||
1902 (ering->tx_pending > B44_TX_RING_SIZE - 1))
1903 return -EINVAL;
1904
1905 spin_lock_irq(&bp->lock);
1906
1907 bp->rx_pending = ering->rx_pending;
1908 bp->tx_pending = ering->tx_pending;
1909
1910 b44_halt(bp);
1911 b44_init_rings(bp);
Michael Chan5fc7d612007-01-26 23:59:57 -08001912 b44_init_hw(bp, B44_FULL_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 netif_wake_queue(bp->dev);
1914 spin_unlock_irq(&bp->lock);
1915
1916 b44_enable_ints(bp);
Jeff Garzik10badc22006-04-12 18:04:32 -04001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return 0;
1919}
1920
1921static void b44_get_pauseparam(struct net_device *dev,
1922 struct ethtool_pauseparam *epause)
1923{
1924 struct b44 *bp = netdev_priv(dev);
1925
1926 epause->autoneg =
1927 (bp->flags & B44_FLAG_PAUSE_AUTO) != 0;
1928 epause->rx_pause =
1929 (bp->flags & B44_FLAG_RX_PAUSE) != 0;
1930 epause->tx_pause =
1931 (bp->flags & B44_FLAG_TX_PAUSE) != 0;
1932}
1933
1934static int b44_set_pauseparam(struct net_device *dev,
1935 struct ethtool_pauseparam *epause)
1936{
1937 struct b44 *bp = netdev_priv(dev);
1938
1939 spin_lock_irq(&bp->lock);
1940 if (epause->autoneg)
1941 bp->flags |= B44_FLAG_PAUSE_AUTO;
1942 else
1943 bp->flags &= ~B44_FLAG_PAUSE_AUTO;
1944 if (epause->rx_pause)
1945 bp->flags |= B44_FLAG_RX_PAUSE;
1946 else
1947 bp->flags &= ~B44_FLAG_RX_PAUSE;
1948 if (epause->tx_pause)
1949 bp->flags |= B44_FLAG_TX_PAUSE;
1950 else
1951 bp->flags &= ~B44_FLAG_TX_PAUSE;
1952 if (bp->flags & B44_FLAG_PAUSE_AUTO) {
1953 b44_halt(bp);
1954 b44_init_rings(bp);
Michael Chan5fc7d612007-01-26 23:59:57 -08001955 b44_init_hw(bp, B44_FULL_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 } else {
1957 __b44_set_flow_ctrl(bp, bp->flags);
1958 }
1959 spin_unlock_irq(&bp->lock);
1960
1961 b44_enable_ints(bp);
Jeff Garzik10badc22006-04-12 18:04:32 -04001962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return 0;
1964}
1965
Francois Romieu33539302005-11-07 01:51:34 +01001966static void b44_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1967{
1968 switch(stringset) {
1969 case ETH_SS_STATS:
1970 memcpy(data, *b44_gstrings, sizeof(b44_gstrings));
1971 break;
1972 }
1973}
1974
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001975static int b44_get_sset_count(struct net_device *dev, int sset)
Francois Romieu33539302005-11-07 01:51:34 +01001976{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001977 switch (sset) {
1978 case ETH_SS_STATS:
1979 return ARRAY_SIZE(b44_gstrings);
1980 default:
1981 return -EOPNOTSUPP;
1982 }
Francois Romieu33539302005-11-07 01:51:34 +01001983}
1984
1985static void b44_get_ethtool_stats(struct net_device *dev,
1986 struct ethtool_stats *stats, u64 *data)
1987{
1988 struct b44 *bp = netdev_priv(dev);
1989 u32 *val = &bp->hw_stats.tx_good_octets;
1990 u32 i;
1991
1992 spin_lock_irq(&bp->lock);
1993
1994 b44_stats_update(bp);
1995
1996 for (i = 0; i < ARRAY_SIZE(b44_gstrings); i++)
1997 *data++ = *val++;
1998
1999 spin_unlock_irq(&bp->lock);
2000}
2001
Gary Zambrano52cafd92006-06-20 15:34:23 -07002002static void b44_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2003{
2004 struct b44 *bp = netdev_priv(dev);
2005
2006 wol->supported = WAKE_MAGIC;
2007 if (bp->flags & B44_FLAG_WOL_ENABLE)
2008 wol->wolopts = WAKE_MAGIC;
2009 else
2010 wol->wolopts = 0;
2011 memset(&wol->sopass, 0, sizeof(wol->sopass));
2012}
2013
2014static int b44_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2015{
2016 struct b44 *bp = netdev_priv(dev);
2017
2018 spin_lock_irq(&bp->lock);
2019 if (wol->wolopts & WAKE_MAGIC)
2020 bp->flags |= B44_FLAG_WOL_ENABLE;
2021 else
2022 bp->flags &= ~B44_FLAG_WOL_ENABLE;
2023 spin_unlock_irq(&bp->lock);
2024
2025 return 0;
2026}
2027
Jeff Garzik7282d492006-09-13 14:30:00 -04002028static const struct ethtool_ops b44_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 .get_drvinfo = b44_get_drvinfo,
2030 .get_settings = b44_get_settings,
2031 .set_settings = b44_set_settings,
2032 .nway_reset = b44_nway_reset,
2033 .get_link = ethtool_op_get_link,
Gary Zambrano52cafd92006-06-20 15:34:23 -07002034 .get_wol = b44_get_wol,
2035 .set_wol = b44_set_wol,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 .get_ringparam = b44_get_ringparam,
2037 .set_ringparam = b44_set_ringparam,
2038 .get_pauseparam = b44_get_pauseparam,
2039 .set_pauseparam = b44_set_pauseparam,
2040 .get_msglevel = b44_get_msglevel,
2041 .set_msglevel = b44_set_msglevel,
Francois Romieu33539302005-11-07 01:51:34 +01002042 .get_strings = b44_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002043 .get_sset_count = b44_get_sset_count,
Francois Romieu33539302005-11-07 01:51:34 +01002044 .get_ethtool_stats = b44_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045};
2046
2047static int b44_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2048{
2049 struct mii_ioctl_data *data = if_mii(ifr);
2050 struct b44 *bp = netdev_priv(dev);
Francois Romieu34105722005-11-30 22:32:13 +01002051 int err = -EINVAL;
2052
2053 if (!netif_running(dev))
2054 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 spin_lock_irq(&bp->lock);
2057 err = generic_mii_ioctl(&bp->mii_if, data, cmd, NULL);
2058 spin_unlock_irq(&bp->lock);
Francois Romieu34105722005-11-30 22:32:13 +01002059out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return err;
2061}
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063static int __devinit b44_get_invariants(struct b44 *bp)
2064{
Michael Buesch753f4922007-09-19 14:20:30 -07002065 struct ssb_device *sdev = bp->sdev;
2066 int err = 0;
2067 u8 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Michael Buesch753f4922007-09-19 14:20:30 -07002069 bp->dma_offset = ssb_dma_translation(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Michael Buesch753f4922007-09-19 14:20:30 -07002071 if (sdev->bus->bustype == SSB_BUSTYPE_SSB &&
2072 instance > 1) {
Larry Finger458414b2007-11-09 16:56:10 -06002073 addr = sdev->bus->sprom.et1mac;
2074 bp->phy_addr = sdev->bus->sprom.et1phyaddr;
Michael Buesch753f4922007-09-19 14:20:30 -07002075 } else {
Larry Finger458414b2007-11-09 16:56:10 -06002076 addr = sdev->bus->sprom.et0mac;
2077 bp->phy_addr = sdev->bus->sprom.et0phyaddr;
Michael Buesch753f4922007-09-19 14:20:30 -07002078 }
Michael Buesch5ea79632008-03-25 18:04:46 +01002079 /* Some ROMs have buggy PHY addresses with the high
2080 * bits set (sign extension?). Truncate them to a
2081 * valid PHY address. */
2082 bp->phy_addr &= 0x1F;
2083
Michael Buesch753f4922007-09-19 14:20:30 -07002084 memcpy(bp->dev->dev_addr, addr, 6);
Gary Zambrano391fc092006-03-28 14:57:38 -08002085
2086 if (!is_valid_ether_addr(&bp->dev->dev_addr[0])){
Joe Perches2fc96ff2010-02-17 15:01:50 +00002087 pr_err("Invalid MAC address found in EEPROM\n");
Gary Zambrano391fc092006-03-28 14:57:38 -08002088 return -EINVAL;
2089 }
2090
John W. Linville2160de52005-09-12 10:48:55 -04002091 memcpy(bp->dev->perm_addr, bp->dev->dev_addr, bp->dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 bp->imask = IMASK_DEF;
2094
Jeff Garzik10badc22006-04-12 18:04:32 -04002095 /* XXX - really required?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 bp->flags |= B44_FLAG_BUGGY_TXPTR;
Michael Buesch753f4922007-09-19 14:20:30 -07002097 */
Gary Zambrano52cafd92006-06-20 15:34:23 -07002098
Michael Buesch753f4922007-09-19 14:20:30 -07002099 if (bp->sdev->id.revision >= 7)
2100 bp->flags |= B44_FLAG_B0_ANDLATER;
Gary Zambrano52cafd92006-06-20 15:34:23 -07002101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return err;
2103}
2104
Stephen Hemminger403413e2009-01-07 18:10:49 -08002105static const struct net_device_ops b44_netdev_ops = {
2106 .ndo_open = b44_open,
2107 .ndo_stop = b44_close,
2108 .ndo_start_xmit = b44_start_xmit,
2109 .ndo_get_stats = b44_get_stats,
2110 .ndo_set_multicast_list = b44_set_rx_mode,
2111 .ndo_set_mac_address = b44_set_mac_addr,
2112 .ndo_validate_addr = eth_validate_addr,
2113 .ndo_do_ioctl = b44_ioctl,
2114 .ndo_tx_timeout = b44_tx_timeout,
2115 .ndo_change_mtu = b44_change_mtu,
2116#ifdef CONFIG_NET_POLL_CONTROLLER
2117 .ndo_poll_controller = b44_poll_controller,
2118#endif
2119};
2120
Michael Buesch753f4922007-09-19 14:20:30 -07002121static int __devinit b44_init_one(struct ssb_device *sdev,
2122 const struct ssb_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
2124 static int b44_version_printed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 struct net_device *dev;
2126 struct b44 *bp;
Joe Perches0795af52007-10-03 17:59:30 -07002127 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Michael Buesch753f4922007-09-19 14:20:30 -07002129 instance++;
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (b44_version_printed++ == 0)
Joe Perches2fc96ff2010-02-17 15:01:50 +00002132 pr_info("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 dev = alloc_etherdev(sizeof(*bp));
2136 if (!dev) {
Joe Perches2fc96ff2010-02-17 15:01:50 +00002137 dev_err(sdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 err = -ENOMEM;
Michael Buesch753f4922007-09-19 14:20:30 -07002139 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141
Michael Buesch753f4922007-09-19 14:20:30 -07002142 SET_NETDEV_DEV(dev, sdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 /* No interesting netdevice features in this card... */
2145 dev->features |= 0;
2146
2147 bp = netdev_priv(dev);
Michael Buesch753f4922007-09-19 14:20:30 -07002148 bp->sdev = sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 bp->dev = dev;
Eric Dumazeta58c8912009-01-15 15:29:35 -08002150 bp->force_copybreak = 0;
Francois Romieu874a6212005-11-07 01:50:46 +01002151
2152 bp->msg_enable = netif_msg_init(b44_debug, B44_DEF_MSG_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 spin_lock_init(&bp->lock);
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 bp->rx_pending = B44_DEF_RX_RING_PENDING;
2157 bp->tx_pending = B44_DEF_TX_RING_PENDING;
2158
Stephen Hemminger403413e2009-01-07 18:10:49 -08002159 dev->netdev_ops = &b44_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002160 netif_napi_add(dev, &bp->napi, b44_poll, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 dev->watchdog_timeo = B44_TX_TIMEOUT;
Michael Buesch753f4922007-09-19 14:20:30 -07002162 dev->irq = sdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 SET_ETHTOOL_OPS(dev, &b44_ethtool_ops);
2164
Stephen Hemmingerc35ca392006-01-20 21:13:17 -08002165 netif_carrier_off(dev);
2166
Michael Buesch753f4922007-09-19 14:20:30 -07002167 err = ssb_bus_powerup(sdev->bus, 0);
2168 if (err) {
2169 dev_err(sdev->dev,
2170 "Failed to powerup the bus\n");
2171 goto err_out_free_dev;
2172 }
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07002173
2174 if (dma_set_mask(sdev->dma_dev, DMA_BIT_MASK(30)) ||
2175 dma_set_coherent_mask(sdev->dma_dev, DMA_BIT_MASK(30))) {
Michael Buesch753f4922007-09-19 14:20:30 -07002176 dev_err(sdev->dev,
Joe Perches2fc96ff2010-02-17 15:01:50 +00002177 "Required 30BIT DMA mask unsupported by the system\n");
Michael Buesch753f4922007-09-19 14:20:30 -07002178 goto err_out_powerdown;
2179 }
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07002180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 err = b44_get_invariants(bp);
2182 if (err) {
Michael Buesch753f4922007-09-19 14:20:30 -07002183 dev_err(sdev->dev,
Joe Perches2fc96ff2010-02-17 15:01:50 +00002184 "Problem fetching invariants of chip, aborting\n");
Michael Buesch753f4922007-09-19 14:20:30 -07002185 goto err_out_powerdown;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
2187
2188 bp->mii_if.dev = dev;
2189 bp->mii_if.mdio_read = b44_mii_read;
2190 bp->mii_if.mdio_write = b44_mii_write;
2191 bp->mii_if.phy_id = bp->phy_addr;
2192 bp->mii_if.phy_id_mask = 0x1f;
2193 bp->mii_if.reg_num_mask = 0x1f;
2194
2195 /* By default, advertise all speed/duplex settings. */
2196 bp->flags |= (B44_FLAG_ADV_10HALF | B44_FLAG_ADV_10FULL |
2197 B44_FLAG_ADV_100HALF | B44_FLAG_ADV_100FULL);
2198
2199 /* By default, auto-negotiate PAUSE. */
2200 bp->flags |= B44_FLAG_PAUSE_AUTO;
2201
2202 err = register_netdev(dev);
2203 if (err) {
Joe Perches2fc96ff2010-02-17 15:01:50 +00002204 dev_err(sdev->dev, "Cannot register net device, aborting\n");
Michael Buesch753f4922007-09-19 14:20:30 -07002205 goto err_out_powerdown;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
2207
Michael Buesch753f4922007-09-19 14:20:30 -07002208 ssb_set_drvdata(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Jeff Garzik10badc22006-04-12 18:04:32 -04002210 /* Chip reset provides power to the b44 MAC & PCI cores, which
Gary Zambrano5c513122006-03-29 17:12:05 -05002211 * is necessary for MAC register access.
Jeff Garzik10badc22006-04-12 18:04:32 -04002212 */
Miguel Botónfedb0ee2008-01-01 01:17:54 +01002213 b44_chip_reset(bp, B44_CHIP_RESET_FULL);
Gary Zambrano5c513122006-03-29 17:12:05 -05002214
Hauke Mehrtens8850dce2010-02-20 10:55:25 +00002215 /* do a phy reset to test if there is an active phy */
2216 if (b44_phy_reset(bp) < 0)
2217 bp->phy_addr = B44_PHY_ADDR_NO_PHY;
2218
Joe Perches2fc96ff2010-02-17 15:01:50 +00002219 netdev_info(dev, "Broadcom 44xx/47xx 10/100BaseT Ethernet %pM\n",
2220 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 return 0;
2223
Michael Buesch753f4922007-09-19 14:20:30 -07002224err_out_powerdown:
2225 ssb_bus_may_powerdown(sdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227err_out_free_dev:
2228 free_netdev(dev);
2229
Michael Buesch753f4922007-09-19 14:20:30 -07002230out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return err;
2232}
2233
Michael Buesch753f4922007-09-19 14:20:30 -07002234static void __devexit b44_remove_one(struct ssb_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Michael Buesch753f4922007-09-19 14:20:30 -07002236 struct net_device *dev = ssb_get_drvdata(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Francois Romieu874a6212005-11-07 01:50:46 +01002238 unregister_netdev(dev);
Michael Buesche92aa632009-02-26 22:35:02 -08002239 ssb_device_disable(sdev, 0);
Michael Buesch753f4922007-09-19 14:20:30 -07002240 ssb_bus_may_powerdown(sdev->bus);
Francois Romieu874a6212005-11-07 01:50:46 +01002241 free_netdev(dev);
Miguel Botónfedb0ee2008-01-01 01:17:54 +01002242 ssb_pcihost_set_power_state(sdev, PCI_D3hot);
Michael Buesch753f4922007-09-19 14:20:30 -07002243 ssb_set_drvdata(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
Michael Buesch753f4922007-09-19 14:20:30 -07002246static int b44_suspend(struct ssb_device *sdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Michael Buesch753f4922007-09-19 14:20:30 -07002248 struct net_device *dev = ssb_get_drvdata(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 struct b44 *bp = netdev_priv(dev);
2250
Michael Buesch753f4922007-09-19 14:20:30 -07002251 if (!netif_running(dev))
2252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 del_timer_sync(&bp->timer);
2255
Jeff Garzik10badc22006-04-12 18:04:32 -04002256 spin_lock_irq(&bp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 b44_halt(bp);
Jeff Garzik10badc22006-04-12 18:04:32 -04002259 netif_carrier_off(bp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 netif_device_detach(bp->dev);
2261 b44_free_rings(bp);
2262
2263 spin_unlock_irq(&bp->lock);
Pavel Machek46e17852005-10-28 15:14:47 -07002264
2265 free_irq(dev->irq, dev);
Gary Zambrano52cafd92006-06-20 15:34:23 -07002266 if (bp->flags & B44_FLAG_WOL_ENABLE) {
Michael Chan5fc7d612007-01-26 23:59:57 -08002267 b44_init_hw(bp, B44_PARTIAL_RESET);
Gary Zambrano52cafd92006-06-20 15:34:23 -07002268 b44_setup_wol(bp);
2269 }
Michael Buesch753f4922007-09-19 14:20:30 -07002270
Miguel Botónfedb0ee2008-01-01 01:17:54 +01002271 ssb_pcihost_set_power_state(sdev, PCI_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return 0;
2273}
2274
Michael Buesch753f4922007-09-19 14:20:30 -07002275static int b44_resume(struct ssb_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
Michael Buesch753f4922007-09-19 14:20:30 -07002277 struct net_device *dev = ssb_get_drvdata(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 struct b44 *bp = netdev_priv(dev);
Dmitriy Monakhov90afd0e2007-01-27 00:00:03 -08002279 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Michael Buesch753f4922007-09-19 14:20:30 -07002281 rc = ssb_bus_powerup(sdev->bus, 0);
Dmitriy Monakhov90afd0e2007-01-27 00:00:03 -08002282 if (rc) {
Michael Buesch753f4922007-09-19 14:20:30 -07002283 dev_err(sdev->dev,
2284 "Failed to powerup the bus\n");
Dmitriy Monakhov90afd0e2007-01-27 00:00:03 -08002285 return rc;
2286 }
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (!netif_running(dev))
2289 return 0;
2290
Dmitriy Monakhov90afd0e2007-01-27 00:00:03 -08002291 rc = request_irq(dev->irq, b44_interrupt, IRQF_SHARED, dev->name, dev);
2292 if (rc) {
Joe Perches2fc96ff2010-02-17 15:01:50 +00002293 netdev_err(dev, "request_irq failed\n");
Dmitriy Monakhov90afd0e2007-01-27 00:00:03 -08002294 return rc;
2295 }
Pavel Machek46e17852005-10-28 15:14:47 -07002296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 spin_lock_irq(&bp->lock);
2298
2299 b44_init_rings(bp);
Michael Chan5fc7d612007-01-26 23:59:57 -08002300 b44_init_hw(bp, B44_FULL_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 netif_device_attach(bp->dev);
2302 spin_unlock_irq(&bp->lock);
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 b44_enable_ints(bp);
Mark Lordd9e2d182005-11-30 22:30:23 +01002305 netif_wake_queue(dev);
Stephen Hemmingera72a8172007-06-04 13:25:37 -07002306
2307 mod_timer(&bp->timer, jiffies + 1);
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return 0;
2310}
2311
Michael Buesch753f4922007-09-19 14:20:30 -07002312static struct ssb_driver b44_ssb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 .name = DRV_MODULE_NAME,
Michael Buesch753f4922007-09-19 14:20:30 -07002314 .id_table = b44_ssb_tbl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 .probe = b44_init_one,
2316 .remove = __devexit_p(b44_remove_one),
Michael Buesch753f4922007-09-19 14:20:30 -07002317 .suspend = b44_suspend,
2318 .resume = b44_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319};
2320
Michael Buesch753f4922007-09-19 14:20:30 -07002321static inline int b44_pci_init(void)
2322{
2323 int err = 0;
2324#ifdef CONFIG_B44_PCI
2325 err = ssb_pcihost_register(&b44_pci_driver);
2326#endif
2327 return err;
2328}
2329
2330static inline void b44_pci_exit(void)
2331{
2332#ifdef CONFIG_B44_PCI
2333 ssb_pcihost_unregister(&b44_pci_driver);
2334#endif
2335}
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337static int __init b44_init(void)
2338{
John W. Linville9f38c632005-10-18 21:30:59 -04002339 unsigned int dma_desc_align_size = dma_get_cache_alignment();
Michael Buesch753f4922007-09-19 14:20:30 -07002340 int err;
John W. Linville9f38c632005-10-18 21:30:59 -04002341
2342 /* Setup paramaters for syncing RX/TX DMA descriptors */
2343 dma_desc_align_mask = ~(dma_desc_align_size - 1);
Alan Cox22d4d772006-01-17 17:53:56 +00002344 dma_desc_sync_size = max_t(unsigned int, dma_desc_align_size, sizeof(struct dma_desc));
John W. Linville9f38c632005-10-18 21:30:59 -04002345
Michael Buesch753f4922007-09-19 14:20:30 -07002346 err = b44_pci_init();
2347 if (err)
2348 return err;
2349 err = ssb_driver_register(&b44_ssb_driver);
2350 if (err)
2351 b44_pci_exit();
2352 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354
2355static void __exit b44_cleanup(void)
2356{
Michael Buesch753f4922007-09-19 14:20:30 -07002357 ssb_driver_unregister(&b44_ssb_driver);
2358 b44_pci_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
2361module_init(b44_init);
2362module_exit(b44_cleanup);
2363