blob: 3c7909e9c3eef3aa55efc075871ce5cdbb03c137 [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 Büscheb032b92011-07-04 20:50:05 +02008 * Copyright (C) 2007 Michael Buesch <m@bues.ch>
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>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000028#include <linux/interrupt.h>
Andrew Morton89358f92005-10-28 16:38:02 -040029#include <linux/dma-mapping.h>
Michael Buesch753f4922007-09-19 14:20:30 -070030#include <linux/ssb/ssb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/uaccess.h>
34#include <asm/io.h>
35#include <asm/irq.h>
36
Michael Buesch753f4922007-09-19 14:20:30 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "b44.h"
39
40#define DRV_MODULE_NAME "b44"
Michael Buesch753f4922007-09-19 14:20:30 -070041#define DRV_MODULE_VERSION "2.0"
Joe Perches7329f0d2011-07-05 07:43:46 +000042#define DRV_DESCRIPTION "Broadcom 44xx/47xx 10/100 PCI ethernet driver"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define B44_DEF_MSG_ENABLE \
45 (NETIF_MSG_DRV | \
46 NETIF_MSG_PROBE | \
47 NETIF_MSG_LINK | \
48 NETIF_MSG_TIMER | \
49 NETIF_MSG_IFDOWN | \
50 NETIF_MSG_IFUP | \
51 NETIF_MSG_RX_ERR | \
52 NETIF_MSG_TX_ERR)
53
54/* length of time before we decide the hardware is borked,
55 * and dev->tx_timeout() should be called to fix the problem
56 */
57#define B44_TX_TIMEOUT (5 * HZ)
58
59/* hardware minimum and maximum for a single frame's data payload */
60#define B44_MIN_MTU 60
61#define B44_MAX_MTU 1500
62
63#define B44_RX_RING_SIZE 512
64#define B44_DEF_RX_RING_PENDING 200
65#define B44_RX_RING_BYTES (sizeof(struct dma_desc) * \
66 B44_RX_RING_SIZE)
67#define B44_TX_RING_SIZE 512
68#define B44_DEF_TX_RING_PENDING (B44_TX_RING_SIZE - 1)
69#define B44_TX_RING_BYTES (sizeof(struct dma_desc) * \
70 B44_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#define TX_RING_GAP(BP) \
73 (B44_TX_RING_SIZE - (BP)->tx_pending)
74#define TX_BUFFS_AVAIL(BP) \
75 (((BP)->tx_cons <= (BP)->tx_prod) ? \
76 (BP)->tx_cons + (BP)->tx_pending - (BP)->tx_prod : \
77 (BP)->tx_cons - (BP)->tx_prod - TX_RING_GAP(BP))
78#define NEXT_TX(N) (((N) + 1) & (B44_TX_RING_SIZE - 1))
79
Felix Fietkau4ca85792009-01-09 02:39:57 +000080#define RX_PKT_OFFSET (RX_HEADER_LEN + 2)
81#define RX_PKT_BUF_SZ (1536 + RX_PKT_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/* minimum number of free TX descriptors required to wake up TX process */
84#define B44_TX_WAKEUP_THRESH (B44_TX_RING_SIZE / 4)
85
Gary Zambrano725ad802006-06-20 15:34:36 -070086/* b44 internal pattern match filter info */
87#define B44_PATTERN_BASE 0x400
88#define B44_PATTERN_SIZE 0x80
89#define B44_PMASK_BASE 0x600
90#define B44_PMASK_SIZE 0x10
91#define B44_MAX_PATTERNS 16
92#define B44_ETHIPV6UDP_HLEN 62
93#define B44_ETHIPV4UDP_HLEN 42
94
Michael Buesch753f4922007-09-19 14:20:30 -070095MODULE_AUTHOR("Felix Fietkau, Florian Schirmer, Pekka Pietikainen, David S. Miller");
Joe Perches7329f0d2011-07-05 07:43:46 +000096MODULE_DESCRIPTION(DRV_DESCRIPTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097MODULE_LICENSE("GPL");
98MODULE_VERSION(DRV_MODULE_VERSION);
99
100static int b44_debug = -1; /* -1 == use B44_DEF_MSG_ENABLE as value */
101module_param(b44_debug, int, 0);
102MODULE_PARM_DESC(b44_debug, "B44 bitmapped debugging message enable value");
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Michael Buesch753f4922007-09-19 14:20:30 -0700105#ifdef CONFIG_B44_PCI
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000106static DEFINE_PCI_DEVICE_TABLE(b44_pci_tbl) = {
Michael Buesch753f4922007-09-19 14:20:30 -0700107 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_BCM4401) },
108 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_BCM4401B0) },
109 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_BCM4401B1) },
110 { 0 } /* terminate list with empty entry */
111};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112MODULE_DEVICE_TABLE(pci, b44_pci_tbl);
113
Michael Buesch753f4922007-09-19 14:20:30 -0700114static struct pci_driver b44_pci_driver = {
115 .name = DRV_MODULE_NAME,
116 .id_table = b44_pci_tbl,
117};
118#endif /* CONFIG_B44_PCI */
119
120static const struct ssb_device_id b44_ssb_tbl[] = {
121 SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_ETHERNET, SSB_ANY_REV),
122 SSB_DEVTABLE_END
123};
124MODULE_DEVICE_TABLE(ssb, b44_ssb_tbl);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static void b44_halt(struct b44 *);
127static void b44_init_rings(struct b44 *);
Michael Chan5fc7d612007-01-26 23:59:57 -0800128
129#define B44_FULL_RESET 1
130#define B44_FULL_RESET_SKIP_PHY 2
131#define B44_PARTIAL_RESET 3
Miguel Botónfedb0ee2008-01-01 01:17:54 +0100132#define B44_CHIP_RESET_FULL 4
133#define B44_CHIP_RESET_PARTIAL 5
Michael Chan5fc7d612007-01-26 23:59:57 -0800134
Gary Zambrano00e8b3a2006-06-20 15:34:26 -0700135static void b44_init_hw(struct b44 *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
John W. Linville9f38c632005-10-18 21:30:59 -0400137static int dma_desc_sync_size;
Michael Buesch753f4922007-09-19 14:20:30 -0700138static int instance;
John W. Linville9f38c632005-10-18 21:30:59 -0400139
Francois Romieu33539302005-11-07 01:51:34 +0100140static const char b44_gstrings[][ETH_GSTRING_LEN] = {
141#define _B44(x...) # x,
142B44_STAT_REG_DECLARE
143#undef _B44
144};
145
Michael Buesch753f4922007-09-19 14:20:30 -0700146static inline void b44_sync_dma_desc_for_device(struct ssb_device *sdev,
147 dma_addr_t dma_base,
148 unsigned long offset,
149 enum dma_data_direction dir)
John W. Linville9f38c632005-10-18 21:30:59 -0400150{
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700151 dma_sync_single_for_device(sdev->dma_dev, dma_base + offset,
152 dma_desc_sync_size, dir);
John W. Linville9f38c632005-10-18 21:30:59 -0400153}
154
Michael Buesch753f4922007-09-19 14:20:30 -0700155static inline void b44_sync_dma_desc_for_cpu(struct ssb_device *sdev,
156 dma_addr_t dma_base,
157 unsigned long offset,
158 enum dma_data_direction dir)
John W. Linville9f38c632005-10-18 21:30:59 -0400159{
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700160 dma_sync_single_for_cpu(sdev->dma_dev, dma_base + offset,
161 dma_desc_sync_size, dir);
John W. Linville9f38c632005-10-18 21:30:59 -0400162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static inline unsigned long br32(const struct b44 *bp, unsigned long reg)
165{
Michael Buesch753f4922007-09-19 14:20:30 -0700166 return ssb_read32(bp->sdev, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Jeff Garzik10badc22006-04-12 18:04:32 -0400169static inline void bw32(const struct b44 *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned long reg, unsigned long val)
171{
Michael Buesch753f4922007-09-19 14:20:30 -0700172 ssb_write32(bp->sdev, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175static int b44_wait_bit(struct b44 *bp, unsigned long reg,
176 u32 bit, unsigned long timeout, const int clear)
177{
178 unsigned long i;
179
180 for (i = 0; i < timeout; i++) {
181 u32 val = br32(bp, reg);
182
183 if (clear && !(val & bit))
184 break;
185 if (!clear && (val & bit))
186 break;
187 udelay(10);
188 }
189 if (i == timeout) {
Jochen Friedrichf6ca0572010-02-12 10:11:54 +0000190 if (net_ratelimit())
Joe Perches2fc96ff2010-02-17 15:01:50 +0000191 netdev_err(bp->dev, "BUG! Timeout waiting for bit %08x of register %lx to %s\n",
192 bit, reg, clear ? "clear" : "set");
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return -ENODEV;
195 }
196 return 0;
197}
198
Michael Buesch753f4922007-09-19 14:20:30 -0700199static inline void __b44_cam_read(struct b44 *bp, unsigned char *data, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 u32 val;
202
Michael Buesch753f4922007-09-19 14:20:30 -0700203 bw32(bp, B44_CAM_CTRL, (CAM_CTRL_READ |
204 (index << CAM_CTRL_INDEX_SHIFT)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Michael Buesch753f4922007-09-19 14:20:30 -0700206 b44_wait_bit(bp, B44_CAM_CTRL, CAM_CTRL_BUSY, 100, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Michael Buesch753f4922007-09-19 14:20:30 -0700208 val = br32(bp, B44_CAM_DATA_LO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Michael Buesch753f4922007-09-19 14:20:30 -0700210 data[2] = (val >> 24) & 0xFF;
211 data[3] = (val >> 16) & 0xFF;
212 data[4] = (val >> 8) & 0xFF;
213 data[5] = (val >> 0) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Michael Buesch753f4922007-09-19 14:20:30 -0700215 val = br32(bp, B44_CAM_DATA_HI);
216
217 data[0] = (val >> 8) & 0xFF;
218 data[1] = (val >> 0) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Michael Buesch753f4922007-09-19 14:20:30 -0700221static inline void __b44_cam_write(struct b44 *bp, unsigned char *data, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 u32 val;
224
225 val = ((u32) data[2]) << 24;
226 val |= ((u32) data[3]) << 16;
227 val |= ((u32) data[4]) << 8;
228 val |= ((u32) data[5]) << 0;
229 bw32(bp, B44_CAM_DATA_LO, val);
Jeff Garzik10badc22006-04-12 18:04:32 -0400230 val = (CAM_DATA_HI_VALID |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 (((u32) data[0]) << 8) |
232 (((u32) data[1]) << 0));
233 bw32(bp, B44_CAM_DATA_HI, val);
234 bw32(bp, B44_CAM_CTRL, (CAM_CTRL_WRITE |
235 (index << CAM_CTRL_INDEX_SHIFT)));
Jeff Garzik10badc22006-04-12 18:04:32 -0400236 b44_wait_bit(bp, B44_CAM_CTRL, CAM_CTRL_BUSY, 100, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239static inline void __b44_disable_ints(struct b44 *bp)
240{
241 bw32(bp, B44_IMASK, 0);
242}
243
244static void b44_disable_ints(struct b44 *bp)
245{
246 __b44_disable_ints(bp);
247
248 /* Flush posted writes. */
249 br32(bp, B44_IMASK);
250}
251
252static void b44_enable_ints(struct b44 *bp)
253{
254 bw32(bp, B44_IMASK, bp->imask);
255}
256
Michael Buesch753f4922007-09-19 14:20:30 -0700257static int __b44_readphy(struct b44 *bp, int phy_addr, int reg, u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 int err;
260
261 bw32(bp, B44_EMAC_ISTAT, EMAC_INT_MII);
262 bw32(bp, B44_MDIO_DATA, (MDIO_DATA_SB_START |
263 (MDIO_OP_READ << MDIO_DATA_OP_SHIFT) |
Michael Buesch753f4922007-09-19 14:20:30 -0700264 (phy_addr << MDIO_DATA_PMD_SHIFT) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 (reg << MDIO_DATA_RA_SHIFT) |
266 (MDIO_TA_VALID << MDIO_DATA_TA_SHIFT)));
267 err = b44_wait_bit(bp, B44_EMAC_ISTAT, EMAC_INT_MII, 100, 0);
268 *val = br32(bp, B44_MDIO_DATA) & MDIO_DATA_DATA;
269
270 return err;
271}
272
Michael Buesch753f4922007-09-19 14:20:30 -0700273static int __b44_writephy(struct b44 *bp, int phy_addr, int reg, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 bw32(bp, B44_EMAC_ISTAT, EMAC_INT_MII);
276 bw32(bp, B44_MDIO_DATA, (MDIO_DATA_SB_START |
277 (MDIO_OP_WRITE << MDIO_DATA_OP_SHIFT) |
Michael Buesch753f4922007-09-19 14:20:30 -0700278 (phy_addr << MDIO_DATA_PMD_SHIFT) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 (reg << MDIO_DATA_RA_SHIFT) |
280 (MDIO_TA_VALID << MDIO_DATA_TA_SHIFT) |
281 (val & MDIO_DATA_DATA)));
282 return b44_wait_bit(bp, B44_EMAC_ISTAT, EMAC_INT_MII, 100, 0);
283}
284
Michael Buesch753f4922007-09-19 14:20:30 -0700285static inline int b44_readphy(struct b44 *bp, int reg, u32 *val)
286{
Hauke Mehrtensd6194192013-12-20 02:16:06 +0100287 if (bp->flags & B44_FLAG_EXTERNAL_PHY)
Michael Buesch753f4922007-09-19 14:20:30 -0700288 return 0;
289
290 return __b44_readphy(bp, bp->phy_addr, reg, val);
291}
292
293static inline int b44_writephy(struct b44 *bp, int reg, u32 val)
294{
Hauke Mehrtensd6194192013-12-20 02:16:06 +0100295 if (bp->flags & B44_FLAG_EXTERNAL_PHY)
Michael Buesch753f4922007-09-19 14:20:30 -0700296 return 0;
297
298 return __b44_writephy(bp, bp->phy_addr, reg, val);
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/* miilib interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static int b44_mii_read(struct net_device *dev, int phy_id, int location)
303{
304 u32 val;
305 struct b44 *bp = netdev_priv(dev);
Michael Buesch753f4922007-09-19 14:20:30 -0700306 int rc = __b44_readphy(bp, phy_id, location, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (rc)
308 return 0xffffffff;
309 return val;
310}
311
312static void b44_mii_write(struct net_device *dev, int phy_id, int location,
313 int val)
314{
315 struct b44 *bp = netdev_priv(dev);
Michael Buesch753f4922007-09-19 14:20:30 -0700316 __b44_writephy(bp, phy_id, location, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319static int b44_phy_reset(struct b44 *bp)
320{
321 u32 val;
322 int err;
323
Hauke Mehrtensd6194192013-12-20 02:16:06 +0100324 if (bp->flags & B44_FLAG_EXTERNAL_PHY)
Michael Buesch753f4922007-09-19 14:20:30 -0700325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 err = b44_writephy(bp, MII_BMCR, BMCR_RESET);
327 if (err)
328 return err;
329 udelay(100);
330 err = b44_readphy(bp, MII_BMCR, &val);
331 if (!err) {
332 if (val & BMCR_RESET) {
Joe Perches2fc96ff2010-02-17 15:01:50 +0000333 netdev_err(bp->dev, "PHY Reset would not complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 err = -ENODEV;
335 }
336 }
337
Hauke Mehrtens8850dce2010-02-20 10:55:25 +0000338 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341static void __b44_set_flow_ctrl(struct b44 *bp, u32 pause_flags)
342{
343 u32 val;
344
345 bp->flags &= ~(B44_FLAG_TX_PAUSE | B44_FLAG_RX_PAUSE);
346 bp->flags |= pause_flags;
347
348 val = br32(bp, B44_RXCONFIG);
349 if (pause_flags & B44_FLAG_RX_PAUSE)
350 val |= RXCONFIG_FLOW;
351 else
352 val &= ~RXCONFIG_FLOW;
353 bw32(bp, B44_RXCONFIG, val);
354
355 val = br32(bp, B44_MAC_FLOW);
356 if (pause_flags & B44_FLAG_TX_PAUSE)
357 val |= (MAC_FLOW_PAUSE_ENAB |
358 (0xc0 & MAC_FLOW_RX_HI_WATER));
359 else
360 val &= ~MAC_FLOW_PAUSE_ENAB;
361 bw32(bp, B44_MAC_FLOW, val);
362}
363
364static void b44_set_flow_ctrl(struct b44 *bp, u32 local, u32 remote)
365{
Jeff Garzik10badc22006-04-12 18:04:32 -0400366 u32 pause_enab = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Gary Zambrano2b474cf2006-04-10 12:02:21 -0700368 /* The driver supports only rx pause by default because
Jeff Garzik10badc22006-04-12 18:04:32 -0400369 the b44 mac tx pause mechanism generates excessive
370 pause frames.
Gary Zambrano2b474cf2006-04-10 12:02:21 -0700371 Use ethtool to turn on b44 tx pause if necessary.
372 */
373 if ((local & ADVERTISE_PAUSE_CAP) &&
Jeff Garzik10badc22006-04-12 18:04:32 -0400374 (local & ADVERTISE_PAUSE_ASYM)){
Gary Zambrano2b474cf2006-04-10 12:02:21 -0700375 if ((remote & LPA_PAUSE_ASYM) &&
376 !(remote & LPA_PAUSE_CAP))
377 pause_enab |= B44_FLAG_RX_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
380 __b44_set_flow_ctrl(bp, pause_enab);
381}
382
Hauke Mehrtens6c08af02010-11-27 06:47:43 +0000383#ifdef CONFIG_BCM47XX
Hauke Mehrtens111bd982012-12-26 19:51:14 +0000384#include <bcm47xx_nvram.h>
Michael Buesch753f4922007-09-19 14:20:30 -0700385static void b44_wap54g10_workaround(struct b44 *bp)
386{
Hauke Mehrtens6c08af02010-11-27 06:47:43 +0000387 char buf[20];
Michael Buesch753f4922007-09-19 14:20:30 -0700388 u32 val;
389 int err;
390
391 /*
392 * workaround for bad hardware design in Linksys WAP54G v1.0
393 * see https://dev.openwrt.org/ticket/146
394 * check and reset bit "isolate"
395 */
Hauke Mehrtens111bd982012-12-26 19:51:14 +0000396 if (bcm47xx_nvram_getenv("boardnum", buf, sizeof(buf)) < 0)
Michael Buesch753f4922007-09-19 14:20:30 -0700397 return;
Hauke Mehrtens6c08af02010-11-27 06:47:43 +0000398 if (simple_strtoul(buf, NULL, 0) == 2) {
Michael Buesch753f4922007-09-19 14:20:30 -0700399 err = __b44_readphy(bp, 0, MII_BMCR, &val);
400 if (err)
401 goto error;
402 if (!(val & BMCR_ISOLATE))
403 return;
404 val &= ~BMCR_ISOLATE;
405 err = __b44_writephy(bp, 0, MII_BMCR, val);
406 if (err)
407 goto error;
408 }
409 return;
410error:
Joe Perches2fc96ff2010-02-17 15:01:50 +0000411 pr_warning("PHY: cannot reset MII transceiver isolate bit\n");
Michael Buesch753f4922007-09-19 14:20:30 -0700412}
413#else
414static inline void b44_wap54g10_workaround(struct b44 *bp)
415{
416}
417#endif
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static int b44_setup_phy(struct b44 *bp)
420{
421 u32 val;
422 int err;
423
Michael Buesch753f4922007-09-19 14:20:30 -0700424 b44_wap54g10_workaround(bp);
425
Hauke Mehrtensd6194192013-12-20 02:16:06 +0100426 if (bp->flags & B44_FLAG_EXTERNAL_PHY)
Michael Buesch753f4922007-09-19 14:20:30 -0700427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if ((err = b44_readphy(bp, B44_MII_ALEDCTRL, &val)) != 0)
429 goto out;
430 if ((err = b44_writephy(bp, B44_MII_ALEDCTRL,
431 val & MII_ALEDCTRL_ALLMSK)) != 0)
432 goto out;
433 if ((err = b44_readphy(bp, B44_MII_TLEDCTRL, &val)) != 0)
434 goto out;
435 if ((err = b44_writephy(bp, B44_MII_TLEDCTRL,
436 val | MII_TLEDCTRL_ENABLE)) != 0)
437 goto out;
438
439 if (!(bp->flags & B44_FLAG_FORCE_LINK)) {
440 u32 adv = ADVERTISE_CSMA;
441
442 if (bp->flags & B44_FLAG_ADV_10HALF)
443 adv |= ADVERTISE_10HALF;
444 if (bp->flags & B44_FLAG_ADV_10FULL)
445 adv |= ADVERTISE_10FULL;
446 if (bp->flags & B44_FLAG_ADV_100HALF)
447 adv |= ADVERTISE_100HALF;
448 if (bp->flags & B44_FLAG_ADV_100FULL)
449 adv |= ADVERTISE_100FULL;
450
451 if (bp->flags & B44_FLAG_PAUSE_AUTO)
452 adv |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
453
454 if ((err = b44_writephy(bp, MII_ADVERTISE, adv)) != 0)
455 goto out;
456 if ((err = b44_writephy(bp, MII_BMCR, (BMCR_ANENABLE |
457 BMCR_ANRESTART))) != 0)
458 goto out;
459 } else {
460 u32 bmcr;
461
462 if ((err = b44_readphy(bp, MII_BMCR, &bmcr)) != 0)
463 goto out;
464 bmcr &= ~(BMCR_FULLDPLX | BMCR_ANENABLE | BMCR_SPEED100);
465 if (bp->flags & B44_FLAG_100_BASE_T)
466 bmcr |= BMCR_SPEED100;
467 if (bp->flags & B44_FLAG_FULL_DUPLEX)
468 bmcr |= BMCR_FULLDPLX;
469 if ((err = b44_writephy(bp, MII_BMCR, bmcr)) != 0)
470 goto out;
471
472 /* Since we will not be negotiating there is no safe way
473 * to determine if the link partner supports flow control
474 * or not. So just disable it completely in this case.
475 */
476 b44_set_flow_ctrl(bp, 0, 0);
477 }
478
479out:
480 return err;
481}
482
483static void b44_stats_update(struct b44 *bp)
484{
485 unsigned long reg;
Kevin Groeneveldeeda8582012-07-17 17:46:01 +0000486 u64 *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 val = &bp->hw_stats.tx_good_octets;
Kevin Groeneveldeeda8582012-07-17 17:46:01 +0000489 u64_stats_update_begin(&bp->hw_stats.syncp);
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 for (reg = B44_TX_GOOD_O; reg <= B44_TX_PAUSE; reg += 4UL) {
492 *val++ += br32(bp, reg);
493 }
Francois Romieu33539302005-11-07 01:51:34 +0100494
495 /* Pad */
496 reg += 8*4UL;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 for (reg = B44_RX_GOOD_O; reg <= B44_RX_NPAUSE; reg += 4UL) {
499 *val++ += br32(bp, reg);
500 }
Kevin Groeneveldeeda8582012-07-17 17:46:01 +0000501
502 u64_stats_update_end(&bp->hw_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505static void b44_link_report(struct b44 *bp)
506{
507 if (!netif_carrier_ok(bp->dev)) {
Joe Perches2fc96ff2010-02-17 15:01:50 +0000508 netdev_info(bp->dev, "Link is down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 } else {
Joe Perches2fc96ff2010-02-17 15:01:50 +0000510 netdev_info(bp->dev, "Link is up at %d Mbps, %s duplex\n",
511 (bp->flags & B44_FLAG_100_BASE_T) ? 100 : 10,
512 (bp->flags & B44_FLAG_FULL_DUPLEX) ? "full" : "half");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Joe Perches2fc96ff2010-02-17 15:01:50 +0000514 netdev_info(bp->dev, "Flow control is %s for TX and %s for RX\n",
515 (bp->flags & B44_FLAG_TX_PAUSE) ? "on" : "off",
516 (bp->flags & B44_FLAG_RX_PAUSE) ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518}
519
520static void b44_check_phy(struct b44 *bp)
521{
522 u32 bmsr, aux;
523
Hauke Mehrtensd6194192013-12-20 02:16:06 +0100524 if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
Michael Buesch753f4922007-09-19 14:20:30 -0700525 bp->flags |= B44_FLAG_100_BASE_T;
526 bp->flags |= B44_FLAG_FULL_DUPLEX;
527 if (!netif_carrier_ok(bp->dev)) {
528 u32 val = br32(bp, B44_TX_CTRL);
529 val |= TX_CTRL_DUPLEX;
530 bw32(bp, B44_TX_CTRL, val);
531 netif_carrier_on(bp->dev);
532 b44_link_report(bp);
533 }
534 return;
535 }
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (!b44_readphy(bp, MII_BMSR, &bmsr) &&
538 !b44_readphy(bp, B44_MII_AUXCTRL, &aux) &&
539 (bmsr != 0xffff)) {
540 if (aux & MII_AUXCTRL_SPEED)
541 bp->flags |= B44_FLAG_100_BASE_T;
542 else
543 bp->flags &= ~B44_FLAG_100_BASE_T;
544 if (aux & MII_AUXCTRL_DUPLEX)
545 bp->flags |= B44_FLAG_FULL_DUPLEX;
546 else
547 bp->flags &= ~B44_FLAG_FULL_DUPLEX;
548
549 if (!netif_carrier_ok(bp->dev) &&
550 (bmsr & BMSR_LSTATUS)) {
551 u32 val = br32(bp, B44_TX_CTRL);
552 u32 local_adv, remote_adv;
553
554 if (bp->flags & B44_FLAG_FULL_DUPLEX)
555 val |= TX_CTRL_DUPLEX;
556 else
557 val &= ~TX_CTRL_DUPLEX;
558 bw32(bp, B44_TX_CTRL, val);
559
560 if (!(bp->flags & B44_FLAG_FORCE_LINK) &&
561 !b44_readphy(bp, MII_ADVERTISE, &local_adv) &&
562 !b44_readphy(bp, MII_LPA, &remote_adv))
563 b44_set_flow_ctrl(bp, local_adv, remote_adv);
564
565 /* Link now up */
566 netif_carrier_on(bp->dev);
567 b44_link_report(bp);
568 } else if (netif_carrier_ok(bp->dev) && !(bmsr & BMSR_LSTATUS)) {
569 /* Link now down */
570 netif_carrier_off(bp->dev);
571 b44_link_report(bp);
572 }
573
574 if (bmsr & BMSR_RFAULT)
Joe Perches2fc96ff2010-02-17 15:01:50 +0000575 netdev_warn(bp->dev, "Remote fault detected in PHY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (bmsr & BMSR_JCD)
Joe Perches2fc96ff2010-02-17 15:01:50 +0000577 netdev_warn(bp->dev, "Jabber detected in PHY\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579}
580
581static void b44_timer(unsigned long __opaque)
582{
583 struct b44 *bp = (struct b44 *) __opaque;
584
585 spin_lock_irq(&bp->lock);
586
587 b44_check_phy(bp);
588
589 b44_stats_update(bp);
590
591 spin_unlock_irq(&bp->lock);
592
Stephen Hemmingera72a8172007-06-04 13:25:37 -0700593 mod_timer(&bp->timer, round_jiffies(jiffies + HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
596static void b44_tx(struct b44 *bp)
597{
598 u32 cur, cons;
Hauke Mehrtens50555442013-09-28 23:22:18 +0200599 unsigned bytes_compl = 0, pkts_compl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 cur = br32(bp, B44_DMATX_STAT) & DMATX_STAT_CDMASK;
602 cur /= sizeof(struct dma_desc);
603
604 /* XXX needs updating when NETIF_F_SG is supported */
605 for (cons = bp->tx_cons; cons != cur; cons = NEXT_TX(cons)) {
606 struct ring_info *rp = &bp->tx_buffers[cons];
607 struct sk_buff *skb = rp->skb;
608
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +0200609 BUG_ON(skb == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700611 dma_unmap_single(bp->sdev->dma_dev,
612 rp->mapping,
613 skb->len,
614 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 rp->skb = NULL;
Hauke Mehrtens50555442013-09-28 23:22:18 +0200616
617 bytes_compl += skb->len;
618 pkts_compl++;
619
Xander Hover15ac2b08a2011-11-23 16:40:31 -0500620 dev_kfree_skb_irq(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
Hauke Mehrtens50555442013-09-28 23:22:18 +0200623 netdev_completed_queue(bp->dev, pkts_compl, bytes_compl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 bp->tx_cons = cons;
625 if (netif_queue_stopped(bp->dev) &&
626 TX_BUFFS_AVAIL(bp) > B44_TX_WAKEUP_THRESH)
627 netif_wake_queue(bp->dev);
628
629 bw32(bp, B44_GPTIMER, 0);
630}
631
632/* Works like this. This chip writes a 'struct rx_header" 30 bytes
633 * before the DMA address you give it. So we allocate 30 more bytes
634 * for the RX buffer, DMA map all of it, skb_reserve the 30 bytes, then
635 * point the chip at 30 bytes past where the rx_header will go.
636 */
637static int b44_alloc_rx_skb(struct b44 *bp, int src_idx, u32 dest_idx_unmasked)
638{
639 struct dma_desc *dp;
640 struct ring_info *src_map, *map;
641 struct rx_header *rh;
642 struct sk_buff *skb;
643 dma_addr_t mapping;
644 int dest_idx;
645 u32 ctrl;
646
647 src_map = NULL;
648 if (src_idx >= 0)
649 src_map = &bp->rx_buffers[src_idx];
650 dest_idx = dest_idx_unmasked & (B44_RX_RING_SIZE - 1);
651 map = &bp->rx_buffers[dest_idx];
Stephen Hemmingerbf0dcbd2007-06-04 13:25:40 -0700652 skb = netdev_alloc_skb(bp->dev, RX_PKT_BUF_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (skb == NULL)
654 return -ENOMEM;
655
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700656 mapping = dma_map_single(bp->sdev->dma_dev, skb->data,
657 RX_PKT_BUF_SZ,
658 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /* Hardware bug work-around, the chip is unable to do PCI DMA
661 to/from anything above 1GB :-( */
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700662 if (dma_mapping_error(bp->sdev->dma_dev, mapping) ||
Yang Hongyang28b76792009-04-06 19:01:17 -0700663 mapping + RX_PKT_BUF_SZ > DMA_BIT_MASK(30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /* Sigh... */
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700665 if (!dma_mapping_error(bp->sdev->dma_dev, mapping))
666 dma_unmap_single(bp->sdev->dma_dev, mapping,
Michael Bueschf2257632008-06-20 11:50:29 +0200667 RX_PKT_BUF_SZ, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 dev_kfree_skb_any(skb);
Eric Dumazetacfa9e92012-07-02 08:36:12 +0000669 skb = alloc_skb(RX_PKT_BUF_SZ, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (skb == NULL)
671 return -ENOMEM;
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700672 mapping = dma_map_single(bp->sdev->dma_dev, skb->data,
673 RX_PKT_BUF_SZ,
674 DMA_FROM_DEVICE);
675 if (dma_mapping_error(bp->sdev->dma_dev, mapping) ||
676 mapping + RX_PKT_BUF_SZ > DMA_BIT_MASK(30)) {
677 if (!dma_mapping_error(bp->sdev->dma_dev, mapping))
678 dma_unmap_single(bp->sdev->dma_dev, mapping, RX_PKT_BUF_SZ,DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 dev_kfree_skb_any(skb);
680 return -ENOMEM;
681 }
Eric Dumazeta58c8912009-01-15 15:29:35 -0800682 bp->force_copybreak = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
Stephen Hemminger72f48612007-06-04 13:25:39 -0700685 rh = (struct rx_header *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 rh->len = 0;
688 rh->flags = 0;
689
690 map->skb = skb;
Michael Buesch753f4922007-09-19 14:20:30 -0700691 map->mapping = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (src_map != NULL)
694 src_map->skb = NULL;
695
Felix Fietkau4ca85792009-01-09 02:39:57 +0000696 ctrl = (DESC_CTRL_LEN & RX_PKT_BUF_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (dest_idx == (B44_RX_RING_SIZE - 1))
698 ctrl |= DESC_CTRL_EOT;
699
700 dp = &bp->rx_ring[dest_idx];
701 dp->ctrl = cpu_to_le32(ctrl);
Felix Fietkau4ca85792009-01-09 02:39:57 +0000702 dp->addr = cpu_to_le32((u32) mapping + bp->dma_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
John W. Linville9f38c632005-10-18 21:30:59 -0400704 if (bp->flags & B44_FLAG_RX_RING_HACK)
Michael Buesch753f4922007-09-19 14:20:30 -0700705 b44_sync_dma_desc_for_device(bp->sdev, bp->rx_ring_dma,
Michael Buesch5d4d9e82009-04-03 00:01:30 +0000706 dest_idx * sizeof(*dp),
Michael Buesch753f4922007-09-19 14:20:30 -0700707 DMA_BIDIRECTIONAL);
John W. Linville9f38c632005-10-18 21:30:59 -0400708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return RX_PKT_BUF_SZ;
710}
711
712static void b44_recycle_rx(struct b44 *bp, int src_idx, u32 dest_idx_unmasked)
713{
714 struct dma_desc *src_desc, *dest_desc;
715 struct ring_info *src_map, *dest_map;
716 struct rx_header *rh;
717 int dest_idx;
Al Viroa7bed27d2007-01-29 15:36:54 -0500718 __le32 ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 dest_idx = dest_idx_unmasked & (B44_RX_RING_SIZE - 1);
721 dest_desc = &bp->rx_ring[dest_idx];
722 dest_map = &bp->rx_buffers[dest_idx];
723 src_desc = &bp->rx_ring[src_idx];
724 src_map = &bp->rx_buffers[src_idx];
725
726 dest_map->skb = src_map->skb;
727 rh = (struct rx_header *) src_map->skb->data;
728 rh->len = 0;
729 rh->flags = 0;
Michael Buesch753f4922007-09-19 14:20:30 -0700730 dest_map->mapping = src_map->mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
John W. Linville9f38c632005-10-18 21:30:59 -0400732 if (bp->flags & B44_FLAG_RX_RING_HACK)
Michael Buesch753f4922007-09-19 14:20:30 -0700733 b44_sync_dma_desc_for_cpu(bp->sdev, bp->rx_ring_dma,
Michael Buesch5d4d9e82009-04-03 00:01:30 +0000734 src_idx * sizeof(*src_desc),
Michael Buesch753f4922007-09-19 14:20:30 -0700735 DMA_BIDIRECTIONAL);
John W. Linville9f38c632005-10-18 21:30:59 -0400736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 ctrl = src_desc->ctrl;
738 if (dest_idx == (B44_RX_RING_SIZE - 1))
739 ctrl |= cpu_to_le32(DESC_CTRL_EOT);
740 else
741 ctrl &= cpu_to_le32(~DESC_CTRL_EOT);
742
743 dest_desc->ctrl = ctrl;
744 dest_desc->addr = src_desc->addr;
John W. Linville9f38c632005-10-18 21:30:59 -0400745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 src_map->skb = NULL;
747
John W. Linville9f38c632005-10-18 21:30:59 -0400748 if (bp->flags & B44_FLAG_RX_RING_HACK)
Michael Buesch753f4922007-09-19 14:20:30 -0700749 b44_sync_dma_desc_for_device(bp->sdev, bp->rx_ring_dma,
Michael Buesch5d4d9e82009-04-03 00:01:30 +0000750 dest_idx * sizeof(*dest_desc),
Michael Buesch753f4922007-09-19 14:20:30 -0700751 DMA_BIDIRECTIONAL);
John W. Linville9f38c632005-10-18 21:30:59 -0400752
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700753 dma_sync_single_for_device(bp->sdev->dma_dev, dest_map->mapping,
754 RX_PKT_BUF_SZ,
755 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758static int b44_rx(struct b44 *bp, int budget)
759{
760 int received;
761 u32 cons, prod;
762
763 received = 0;
764 prod = br32(bp, B44_DMARX_STAT) & DMARX_STAT_CDMASK;
765 prod /= sizeof(struct dma_desc);
766 cons = bp->rx_cons;
767
768 while (cons != prod && budget > 0) {
769 struct ring_info *rp = &bp->rx_buffers[cons];
770 struct sk_buff *skb = rp->skb;
Michael Buesch753f4922007-09-19 14:20:30 -0700771 dma_addr_t map = rp->mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct rx_header *rh;
773 u16 len;
774
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700775 dma_sync_single_for_cpu(bp->sdev->dma_dev, map,
776 RX_PKT_BUF_SZ,
777 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 rh = (struct rx_header *) skb->data;
Al Viroa7bed27d2007-01-29 15:36:54 -0500779 len = le16_to_cpu(rh->len);
Stephen Hemminger72f48612007-06-04 13:25:39 -0700780 if ((len > (RX_PKT_BUF_SZ - RX_PKT_OFFSET)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 (rh->flags & cpu_to_le16(RX_FLAG_ERRORS))) {
782 drop_it:
783 b44_recycle_rx(bp, cons, bp->rx_prod);
784 drop_it_no_recycle:
Eric Dumazet553e2332009-05-27 10:34:50 +0000785 bp->dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto next_pkt;
787 }
788
789 if (len == 0) {
790 int i = 0;
791
792 do {
793 udelay(2);
794 barrier();
Al Viroa7bed27d2007-01-29 15:36:54 -0500795 len = le16_to_cpu(rh->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 } while (len == 0 && i++ < 5);
797 if (len == 0)
798 goto drop_it;
799 }
800
801 /* Omit CRC. */
802 len -= 4;
803
Eric Dumazeta58c8912009-01-15 15:29:35 -0800804 if (!bp->force_copybreak && len > RX_COPY_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 int skb_size;
806 skb_size = b44_alloc_rx_skb(bp, cons, bp->rx_prod);
807 if (skb_size < 0)
808 goto drop_it;
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700809 dma_unmap_single(bp->sdev->dma_dev, map,
810 skb_size, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* Leave out rx_header */
Felix Fietkau4ca85792009-01-09 02:39:57 +0000812 skb_put(skb, len + RX_PKT_OFFSET);
813 skb_pull(skb, RX_PKT_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 } else {
815 struct sk_buff *copy_skb;
816
817 b44_recycle_rx(bp, cons, bp->rx_prod);
Hauke Mehrtens7341a732013-02-18 10:49:56 +0000818 copy_skb = netdev_alloc_skb_ip_align(bp->dev, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (copy_skb == NULL)
820 goto drop_it_no_recycle;
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 skb_put(copy_skb, len);
823 /* DMA sync done above, copy just the actual packet */
Stephen Hemminger72f48612007-06-04 13:25:39 -0700824 skb_copy_from_linear_data_offset(skb, RX_PKT_OFFSET,
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300825 copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 skb = copy_skb;
827 }
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700828 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 skb->protocol = eth_type_trans(skb, bp->dev);
830 netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 received++;
832 budget--;
833 next_pkt:
834 bp->rx_prod = (bp->rx_prod + 1) &
835 (B44_RX_RING_SIZE - 1);
836 cons = (cons + 1) & (B44_RX_RING_SIZE - 1);
837 }
838
839 bp->rx_cons = cons;
840 bw32(bp, B44_DMARX_PTR, cons * sizeof(struct dma_desc));
841
842 return received;
843}
844
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700845static int b44_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700847 struct b44 *bp = container_of(napi, struct b44, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700848 int work_done;
Dongdong Denge99b1f02009-09-16 16:10:47 +0000849 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Dongdong Denge99b1f02009-09-16 16:10:47 +0000851 spin_lock_irqsave(&bp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 if (bp->istat & (ISTAT_TX | ISTAT_TO)) {
854 /* spin_lock(&bp->tx_lock); */
855 b44_tx(bp);
856 /* spin_unlock(&bp->tx_lock); */
857 }
Mark Lord32737e92010-09-04 14:17:59 +0000858 if (bp->istat & ISTAT_RFO) { /* fast recovery, in ~20msec */
859 bp->istat &= ~ISTAT_RFO;
860 b44_disable_ints(bp);
861 ssb_device_enable(bp->sdev, 0); /* resets ISTAT_RFO */
862 b44_init_rings(bp);
863 b44_init_hw(bp, B44_FULL_RESET_SKIP_PHY);
864 netif_wake_queue(bp->dev);
865 }
866
Dongdong Denge99b1f02009-09-16 16:10:47 +0000867 spin_unlock_irqrestore(&bp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700869 work_done = 0;
870 if (bp->istat & ISTAT_RX)
871 work_done += b44_rx(bp, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 if (bp->istat & ISTAT_ERRORS) {
Francois Romieud15e9c42006-12-17 23:03:15 +0100874 spin_lock_irqsave(&bp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 b44_halt(bp);
876 b44_init_rings(bp);
Michael Chan5fc7d612007-01-26 23:59:57 -0800877 b44_init_hw(bp, B44_FULL_RESET_SKIP_PHY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 netif_wake_queue(bp->dev);
Francois Romieud15e9c42006-12-17 23:03:15 +0100879 spin_unlock_irqrestore(&bp->lock, flags);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700880 work_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700883 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800884 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 b44_enable_ints(bp);
886 }
887
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700888 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
David Howells7d12e782006-10-05 14:55:46 +0100891static irqreturn_t b44_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 struct net_device *dev = dev_id;
894 struct b44 *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 u32 istat, imask;
896 int handled = 0;
897
Francois Romieu65b984f2005-11-07 01:52:06 +0100898 spin_lock(&bp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 istat = br32(bp, B44_ISTAT);
901 imask = br32(bp, B44_IMASK);
902
Johannes Berge78181f2006-11-06 23:17:20 +0100903 /* The interrupt mask register controls which interrupt bits
904 * will actually raise an interrupt to the CPU when set by hw/firmware,
905 * but doesn't mask off the bits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 */
907 istat &= imask;
908 if (istat) {
909 handled = 1;
Francois Romieuba5eec92005-11-08 23:37:12 +0100910
911 if (unlikely(!netif_running(dev))) {
Joe Perches2fc96ff2010-02-17 15:01:50 +0000912 netdev_info(dev, "late interrupt\n");
Francois Romieuba5eec92005-11-08 23:37:12 +0100913 goto irq_ack;
914 }
915
Ben Hutchings288379f2009-01-19 16:43:59 -0800916 if (napi_schedule_prep(&bp->napi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /* NOTE: These writes are posted by the readback of
918 * the ISTAT register below.
919 */
920 bp->istat = istat;
921 __b44_disable_ints(bp);
Ben Hutchings288379f2009-01-19 16:43:59 -0800922 __napi_schedule(&bp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
Francois Romieuba5eec92005-11-08 23:37:12 +0100925irq_ack:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 bw32(bp, B44_ISTAT, istat);
927 br32(bp, B44_ISTAT);
928 }
Francois Romieu65b984f2005-11-07 01:52:06 +0100929 spin_unlock(&bp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return IRQ_RETVAL(handled);
931}
932
933static void b44_tx_timeout(struct net_device *dev)
934{
935 struct b44 *bp = netdev_priv(dev);
936
Joe Perches2fc96ff2010-02-17 15:01:50 +0000937 netdev_err(dev, "transmit timed out, resetting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 spin_lock_irq(&bp->lock);
940
941 b44_halt(bp);
942 b44_init_rings(bp);
Michael Chan5fc7d612007-01-26 23:59:57 -0800943 b44_init_hw(bp, B44_FULL_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 spin_unlock_irq(&bp->lock);
946
947 b44_enable_ints(bp);
948
949 netif_wake_queue(dev);
950}
951
Stephen Hemminger613573252009-08-31 19:50:58 +0000952static netdev_tx_t b44_start_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 struct b44 *bp = netdev_priv(dev);
Francois Romieuc7193692005-11-07 01:50:03 +0100955 int rc = NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 dma_addr_t mapping;
957 u32 len, entry, ctrl;
Dongdong Deng22580f82009-08-13 19:12:31 +0000958 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 len = skb->len;
Dongdong Deng22580f82009-08-13 19:12:31 +0000961 spin_lock_irqsave(&bp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 /* This is a hard error, log it. */
964 if (unlikely(TX_BUFFS_AVAIL(bp) < 1)) {
965 netif_stop_queue(dev);
Joe Perches2fc96ff2010-02-17 15:01:50 +0000966 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
Francois Romieuc7193692005-11-07 01:50:03 +0100967 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700970 mapping = dma_map_single(bp->sdev->dma_dev, skb->data, len, DMA_TO_DEVICE);
971 if (dma_mapping_error(bp->sdev->dma_dev, mapping) || mapping + len > DMA_BIT_MASK(30)) {
Stephen Hemmingerf65a7172007-06-04 13:25:38 -0700972 struct sk_buff *bounce_skb;
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /* Chip can't handle DMA to/from >1GB, use bounce buffer */
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700975 if (!dma_mapping_error(bp->sdev->dma_dev, mapping))
976 dma_unmap_single(bp->sdev->dma_dev, mapping, len,
Michael Bueschf2257632008-06-20 11:50:29 +0200977 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Eric Dumazetacfa9e92012-07-02 08:36:12 +0000979 bounce_skb = alloc_skb(len, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (!bounce_skb)
Francois Romieuc7193692005-11-07 01:50:03 +0100981 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -0700983 mapping = dma_map_single(bp->sdev->dma_dev, bounce_skb->data,
984 len, DMA_TO_DEVICE);
985 if (dma_mapping_error(bp->sdev->dma_dev, mapping) || mapping + len > DMA_BIT_MASK(30)) {
986 if (!dma_mapping_error(bp->sdev->dma_dev, mapping))
987 dma_unmap_single(bp->sdev->dma_dev, mapping,
Michael Bueschf2257632008-06-20 11:50:29 +0200988 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 dev_kfree_skb_any(bounce_skb);
Francois Romieuc7193692005-11-07 01:50:03 +0100990 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992
Stephen Hemmingerf65a7172007-06-04 13:25:38 -0700993 skb_copy_from_linear_data(skb, skb_put(bounce_skb, len), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 dev_kfree_skb_any(skb);
995 skb = bounce_skb;
996 }
997
998 entry = bp->tx_prod;
999 bp->tx_buffers[entry].skb = skb;
Michael Buesch753f4922007-09-19 14:20:30 -07001000 bp->tx_buffers[entry].mapping = mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 ctrl = (len & DESC_CTRL_LEN);
1003 ctrl |= DESC_CTRL_IOC | DESC_CTRL_SOF | DESC_CTRL_EOF;
1004 if (entry == (B44_TX_RING_SIZE - 1))
1005 ctrl |= DESC_CTRL_EOT;
1006
1007 bp->tx_ring[entry].ctrl = cpu_to_le32(ctrl);
1008 bp->tx_ring[entry].addr = cpu_to_le32((u32) mapping+bp->dma_offset);
1009
John W. Linville9f38c632005-10-18 21:30:59 -04001010 if (bp->flags & B44_FLAG_TX_RING_HACK)
Michael Buesch753f4922007-09-19 14:20:30 -07001011 b44_sync_dma_desc_for_device(bp->sdev, bp->tx_ring_dma,
1012 entry * sizeof(bp->tx_ring[0]),
1013 DMA_TO_DEVICE);
John W. Linville9f38c632005-10-18 21:30:59 -04001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 entry = NEXT_TX(entry);
1016
1017 bp->tx_prod = entry;
1018
1019 wmb();
1020
1021 bw32(bp, B44_DMATX_PTR, entry * sizeof(struct dma_desc));
1022 if (bp->flags & B44_FLAG_BUGGY_TXPTR)
1023 bw32(bp, B44_DMATX_PTR, entry * sizeof(struct dma_desc));
1024 if (bp->flags & B44_FLAG_REORDER_BUG)
1025 br32(bp, B44_DMATX_PTR);
1026
Hauke Mehrtens50555442013-09-28 23:22:18 +02001027 netdev_sent_queue(dev, skb->len);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (TX_BUFFS_AVAIL(bp) < 1)
1030 netif_stop_queue(dev);
1031
Francois Romieuc7193692005-11-07 01:50:03 +01001032out_unlock:
Dongdong Deng22580f82009-08-13 19:12:31 +00001033 spin_unlock_irqrestore(&bp->lock, flags);
Francois Romieuc7193692005-11-07 01:50:03 +01001034
1035 return rc;
1036
1037err_out:
1038 rc = NETDEV_TX_BUSY;
1039 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042static int b44_change_mtu(struct net_device *dev, int new_mtu)
1043{
1044 struct b44 *bp = netdev_priv(dev);
1045
1046 if (new_mtu < B44_MIN_MTU || new_mtu > B44_MAX_MTU)
1047 return -EINVAL;
1048
1049 if (!netif_running(dev)) {
1050 /* We'll just catch it later when the
1051 * device is up'd.
1052 */
1053 dev->mtu = new_mtu;
1054 return 0;
1055 }
1056
1057 spin_lock_irq(&bp->lock);
1058 b44_halt(bp);
1059 dev->mtu = new_mtu;
1060 b44_init_rings(bp);
Michael Chan5fc7d612007-01-26 23:59:57 -08001061 b44_init_hw(bp, B44_FULL_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 spin_unlock_irq(&bp->lock);
1063
1064 b44_enable_ints(bp);
Jeff Garzik10badc22006-04-12 18:04:32 -04001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return 0;
1067}
1068
1069/* Free up pending packets in all rx/tx rings.
1070 *
1071 * The chip has been shut down and the driver detached from
1072 * the networking, so no interrupts or new tx packets will
1073 * end up in the driver. bp->lock is not held and we are not
1074 * in an interrupt context and thus may sleep.
1075 */
1076static void b44_free_rings(struct b44 *bp)
1077{
1078 struct ring_info *rp;
1079 int i;
1080
1081 for (i = 0; i < B44_RX_RING_SIZE; i++) {
1082 rp = &bp->rx_buffers[i];
1083
1084 if (rp->skb == NULL)
1085 continue;
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001086 dma_unmap_single(bp->sdev->dma_dev, rp->mapping, RX_PKT_BUF_SZ,
1087 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 dev_kfree_skb_any(rp->skb);
1089 rp->skb = NULL;
1090 }
1091
1092 /* XXX needs changes once NETIF_F_SG is set... */
1093 for (i = 0; i < B44_TX_RING_SIZE; i++) {
1094 rp = &bp->tx_buffers[i];
1095
1096 if (rp->skb == NULL)
1097 continue;
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001098 dma_unmap_single(bp->sdev->dma_dev, rp->mapping, rp->skb->len,
1099 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 dev_kfree_skb_any(rp->skb);
1101 rp->skb = NULL;
1102 }
1103}
1104
1105/* Initialize tx/rx rings for packet processing.
1106 *
1107 * The chip has been shut down and the driver detached from
1108 * the networking, so no interrupts or new tx packets will
Francois Romieu874a6212005-11-07 01:50:46 +01001109 * end up in the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
1111static void b44_init_rings(struct b44 *bp)
1112{
1113 int i;
1114
1115 b44_free_rings(bp);
1116
1117 memset(bp->rx_ring, 0, B44_RX_RING_BYTES);
1118 memset(bp->tx_ring, 0, B44_TX_RING_BYTES);
1119
John W. Linville9f38c632005-10-18 21:30:59 -04001120 if (bp->flags & B44_FLAG_RX_RING_HACK)
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001121 dma_sync_single_for_device(bp->sdev->dma_dev, bp->rx_ring_dma,
1122 DMA_TABLE_BYTES, DMA_BIDIRECTIONAL);
John W. Linville9f38c632005-10-18 21:30:59 -04001123
1124 if (bp->flags & B44_FLAG_TX_RING_HACK)
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001125 dma_sync_single_for_device(bp->sdev->dma_dev, bp->tx_ring_dma,
1126 DMA_TABLE_BYTES, DMA_TO_DEVICE);
John W. Linville9f38c632005-10-18 21:30:59 -04001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 for (i = 0; i < bp->rx_pending; i++) {
1129 if (b44_alloc_rx_skb(bp, -1, i) < 0)
1130 break;
1131 }
1132}
1133
1134/*
1135 * Must not be invoked with interrupt sources disabled and
1136 * the hardware shutdown down.
1137 */
1138static void b44_free_consistent(struct b44 *bp)
1139{
Jesper Juhlb4558ea2005-10-28 16:53:13 -04001140 kfree(bp->rx_buffers);
1141 bp->rx_buffers = NULL;
1142 kfree(bp->tx_buffers);
1143 bp->tx_buffers = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (bp->rx_ring) {
John W. Linville9f38c632005-10-18 21:30:59 -04001145 if (bp->flags & B44_FLAG_RX_RING_HACK) {
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001146 dma_unmap_single(bp->sdev->dma_dev, bp->rx_ring_dma,
1147 DMA_TABLE_BYTES, DMA_BIDIRECTIONAL);
John W. Linville9f38c632005-10-18 21:30:59 -04001148 kfree(bp->rx_ring);
1149 } else
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001150 dma_free_coherent(bp->sdev->dma_dev, DMA_TABLE_BYTES,
1151 bp->rx_ring, bp->rx_ring_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 bp->rx_ring = NULL;
John W. Linville9f38c632005-10-18 21:30:59 -04001153 bp->flags &= ~B44_FLAG_RX_RING_HACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155 if (bp->tx_ring) {
John W. Linville9f38c632005-10-18 21:30:59 -04001156 if (bp->flags & B44_FLAG_TX_RING_HACK) {
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001157 dma_unmap_single(bp->sdev->dma_dev, bp->tx_ring_dma,
1158 DMA_TABLE_BYTES, DMA_TO_DEVICE);
John W. Linville9f38c632005-10-18 21:30:59 -04001159 kfree(bp->tx_ring);
1160 } else
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001161 dma_free_coherent(bp->sdev->dma_dev, DMA_TABLE_BYTES,
1162 bp->tx_ring, bp->tx_ring_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 bp->tx_ring = NULL;
John W. Linville9f38c632005-10-18 21:30:59 -04001164 bp->flags &= ~B44_FLAG_TX_RING_HACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166}
1167
1168/*
1169 * Must not be invoked with interrupt sources disabled and
1170 * the hardware shutdown down. Can sleep.
1171 */
Michael Buesch753f4922007-09-19 14:20:30 -07001172static int b44_alloc_consistent(struct b44 *bp, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
1174 int size;
1175
1176 size = B44_RX_RING_SIZE * sizeof(struct ring_info);
Michael Buesch753f4922007-09-19 14:20:30 -07001177 bp->rx_buffers = kzalloc(size, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (!bp->rx_buffers)
1179 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 size = B44_TX_RING_SIZE * sizeof(struct ring_info);
Michael Buesch753f4922007-09-19 14:20:30 -07001182 bp->tx_buffers = kzalloc(size, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (!bp->tx_buffers)
1184 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 size = DMA_TABLE_BYTES;
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001187 bp->rx_ring = dma_alloc_coherent(bp->sdev->dma_dev, size,
1188 &bp->rx_ring_dma, gfp);
John W. Linville9f38c632005-10-18 21:30:59 -04001189 if (!bp->rx_ring) {
1190 /* Allocation may have failed due to pci_alloc_consistent
1191 insisting on use of GFP_DMA, which is more restrictive
1192 than necessary... */
1193 struct dma_desc *rx_ring;
1194 dma_addr_t rx_ring_dma;
1195
Michael Buesch753f4922007-09-19 14:20:30 -07001196 rx_ring = kzalloc(size, gfp);
Francois Romieu874a6212005-11-07 01:50:46 +01001197 if (!rx_ring)
John W. Linville9f38c632005-10-18 21:30:59 -04001198 goto out_err;
1199
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001200 rx_ring_dma = dma_map_single(bp->sdev->dma_dev, rx_ring,
1201 DMA_TABLE_BYTES,
1202 DMA_BIDIRECTIONAL);
John W. Linville9f38c632005-10-18 21:30:59 -04001203
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001204 if (dma_mapping_error(bp->sdev->dma_dev, rx_ring_dma) ||
Yang Hongyang28b76792009-04-06 19:01:17 -07001205 rx_ring_dma + size > DMA_BIT_MASK(30)) {
John W. Linville9f38c632005-10-18 21:30:59 -04001206 kfree(rx_ring);
1207 goto out_err;
1208 }
1209
1210 bp->rx_ring = rx_ring;
1211 bp->rx_ring_dma = rx_ring_dma;
1212 bp->flags |= B44_FLAG_RX_RING_HACK;
1213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001215 bp->tx_ring = dma_alloc_coherent(bp->sdev->dma_dev, size,
1216 &bp->tx_ring_dma, gfp);
John W. Linville9f38c632005-10-18 21:30:59 -04001217 if (!bp->tx_ring) {
Michael Bueschf2257632008-06-20 11:50:29 +02001218 /* Allocation may have failed due to ssb_dma_alloc_consistent
John W. Linville9f38c632005-10-18 21:30:59 -04001219 insisting on use of GFP_DMA, which is more restrictive
1220 than necessary... */
1221 struct dma_desc *tx_ring;
1222 dma_addr_t tx_ring_dma;
1223
Michael Buesch753f4922007-09-19 14:20:30 -07001224 tx_ring = kzalloc(size, gfp);
Francois Romieu874a6212005-11-07 01:50:46 +01001225 if (!tx_ring)
John W. Linville9f38c632005-10-18 21:30:59 -04001226 goto out_err;
1227
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001228 tx_ring_dma = dma_map_single(bp->sdev->dma_dev, tx_ring,
1229 DMA_TABLE_BYTES,
1230 DMA_TO_DEVICE);
John W. Linville9f38c632005-10-18 21:30:59 -04001231
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07001232 if (dma_mapping_error(bp->sdev->dma_dev, tx_ring_dma) ||
Yang Hongyang28b76792009-04-06 19:01:17 -07001233 tx_ring_dma + size > DMA_BIT_MASK(30)) {
John W. Linville9f38c632005-10-18 21:30:59 -04001234 kfree(tx_ring);
1235 goto out_err;
1236 }
1237
1238 bp->tx_ring = tx_ring;
1239 bp->tx_ring_dma = tx_ring_dma;
1240 bp->flags |= B44_FLAG_TX_RING_HACK;
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 return 0;
1244
1245out_err:
1246 b44_free_consistent(bp);
1247 return -ENOMEM;
1248}
1249
1250/* bp->lock is held. */
1251static void b44_clear_stats(struct b44 *bp)
1252{
1253 unsigned long reg;
1254
1255 bw32(bp, B44_MIB_CTRL, MIB_CTRL_CLR_ON_READ);
1256 for (reg = B44_TX_GOOD_O; reg <= B44_TX_PAUSE; reg += 4UL)
1257 br32(bp, reg);
1258 for (reg = B44_RX_GOOD_O; reg <= B44_RX_NPAUSE; reg += 4UL)
1259 br32(bp, reg);
1260}
1261
1262/* bp->lock is held. */
Miguel Botónfedb0ee2008-01-01 01:17:54 +01001263static void b44_chip_reset(struct b44 *bp, int reset_kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Michael Buesch753f4922007-09-19 14:20:30 -07001265 struct ssb_device *sdev = bp->sdev;
Michael Bueschf8af11a2009-02-26 22:33:00 -08001266 bool was_enabled;
Michael Buesch753f4922007-09-19 14:20:30 -07001267
Michael Bueschf8af11a2009-02-26 22:33:00 -08001268 was_enabled = ssb_device_is_enabled(bp->sdev);
1269
1270 ssb_device_enable(bp->sdev, 0);
1271 ssb_pcicore_dev_irqvecs_enable(&sdev->bus->pcicore, sdev);
1272
1273 if (was_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 bw32(bp, B44_RCV_LAZY, 0);
1275 bw32(bp, B44_ENET_CTRL, ENET_CTRL_DISABLE);
Gary Zambrano40ee8c72007-02-16 13:27:27 -08001276 b44_wait_bit(bp, B44_ENET_CTRL, ENET_CTRL_DISABLE, 200, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 bw32(bp, B44_DMATX_CTRL, 0);
1278 bp->tx_prod = bp->tx_cons = 0;
1279 if (br32(bp, B44_DMARX_STAT) & DMARX_STAT_EMASK) {
1280 b44_wait_bit(bp, B44_DMARX_STAT, DMARX_STAT_SIDLE,
1281 100, 0);
1282 }
1283 bw32(bp, B44_DMARX_CTRL, 0);
1284 bp->rx_prod = bp->rx_cons = 0;
Michael Bueschf8af11a2009-02-26 22:33:00 -08001285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 b44_clear_stats(bp);
1288
Miguel Botónfedb0ee2008-01-01 01:17:54 +01001289 /*
1290 * Don't enable PHY if we are doing a partial reset
1291 * we are probably going to power down
1292 */
1293 if (reset_kind == B44_CHIP_RESET_PARTIAL)
1294 return;
1295
Michael Buesch753f4922007-09-19 14:20:30 -07001296 switch (sdev->bus->bustype) {
1297 case SSB_BUSTYPE_SSB:
1298 bw32(bp, B44_MDIO_CTRL, (MDIO_CTRL_PREAMBLE |
Julia Lawall39506a52009-08-01 09:51:06 +00001299 (DIV_ROUND_CLOSEST(ssb_clockspeed(sdev->bus),
1300 B44_MDC_RATIO)
Michael Buesch753f4922007-09-19 14:20:30 -07001301 & MDIO_CTRL_MAXF_MASK)));
1302 break;
1303 case SSB_BUSTYPE_PCI:
Michael Buesch753f4922007-09-19 14:20:30 -07001304 bw32(bp, B44_MDIO_CTRL, (MDIO_CTRL_PREAMBLE |
1305 (0x0d & MDIO_CTRL_MAXF_MASK)));
1306 break;
Michael Buesch98a1e2a2009-09-08 19:33:31 +02001307 case SSB_BUSTYPE_PCMCIA:
1308 case SSB_BUSTYPE_SDIO:
1309 WARN_ON(1); /* A device with this bus does not exist. */
1310 break;
Michael Buesch753f4922007-09-19 14:20:30 -07001311 }
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 br32(bp, B44_MDIO_CTRL);
1314
1315 if (!(br32(bp, B44_DEVCTRL) & DEVCTRL_IPP)) {
1316 bw32(bp, B44_ENET_CTRL, ENET_CTRL_EPSEL);
1317 br32(bp, B44_ENET_CTRL);
Hauke Mehrtensd6194192013-12-20 02:16:06 +01001318 bp->flags |= B44_FLAG_EXTERNAL_PHY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 } else {
1320 u32 val = br32(bp, B44_DEVCTRL);
1321
1322 if (val & DEVCTRL_EPR) {
1323 bw32(bp, B44_DEVCTRL, (val & ~DEVCTRL_EPR));
1324 br32(bp, B44_DEVCTRL);
1325 udelay(100);
1326 }
Hauke Mehrtensd6194192013-12-20 02:16:06 +01001327 bp->flags &= ~B44_FLAG_EXTERNAL_PHY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329}
1330
1331/* bp->lock is held. */
1332static void b44_halt(struct b44 *bp)
1333{
1334 b44_disable_ints(bp);
Miguel Botónfedb0ee2008-01-01 01:17:54 +01001335 /* reset PHY */
1336 b44_phy_reset(bp);
1337 /* power down PHY */
Joe Perches2fc96ff2010-02-17 15:01:50 +00001338 netdev_info(bp->dev, "powering down PHY\n");
Miguel Botónfedb0ee2008-01-01 01:17:54 +01001339 bw32(bp, B44_MAC_CTRL, MAC_CTRL_PHY_PDOWN);
1340 /* now reset the chip, but without enabling the MAC&PHY
1341 * part of it. This has to be done _after_ we shut down the PHY */
1342 b44_chip_reset(bp, B44_CHIP_RESET_PARTIAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
1345/* bp->lock is held. */
1346static void __b44_set_mac_addr(struct b44 *bp)
1347{
1348 bw32(bp, B44_CAM_CTRL, 0);
1349 if (!(bp->dev->flags & IFF_PROMISC)) {
1350 u32 val;
1351
1352 __b44_cam_write(bp, bp->dev->dev_addr, 0);
1353 val = br32(bp, B44_CAM_CTRL);
1354 bw32(bp, B44_CAM_CTRL, val | CAM_CTRL_ENABLE);
1355 }
1356}
1357
1358static int b44_set_mac_addr(struct net_device *dev, void *p)
1359{
1360 struct b44 *bp = netdev_priv(dev);
1361 struct sockaddr *addr = p;
Michael Buesch753f4922007-09-19 14:20:30 -07001362 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 if (netif_running(dev))
1365 return -EBUSY;
1366
Gary Zambrano391fc092006-03-28 14:57:38 -08001367 if (!is_valid_ether_addr(addr->sa_data))
1368 return -EINVAL;
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1371
1372 spin_lock_irq(&bp->lock);
Michael Buesch753f4922007-09-19 14:20:30 -07001373
1374 val = br32(bp, B44_RXCONFIG);
1375 if (!(val & RXCONFIG_CAM_ABSENT))
1376 __b44_set_mac_addr(bp);
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 spin_unlock_irq(&bp->lock);
1379
1380 return 0;
1381}
1382
1383/* Called at device open time to get the chip ready for
1384 * packet processing. Invoked with bp->lock held.
1385 */
1386static void __b44_set_rx_mode(struct net_device *);
Michael Chan5fc7d612007-01-26 23:59:57 -08001387static void b44_init_hw(struct b44 *bp, int reset_kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
1389 u32 val;
1390
Miguel Botónfedb0ee2008-01-01 01:17:54 +01001391 b44_chip_reset(bp, B44_CHIP_RESET_FULL);
Michael Chan5fc7d612007-01-26 23:59:57 -08001392 if (reset_kind == B44_FULL_RESET) {
Gary Zambrano00e8b3a2006-06-20 15:34:26 -07001393 b44_phy_reset(bp);
1394 b44_setup_phy(bp);
1395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 /* Enable CRC32, set proper LED modes and power on PHY */
1398 bw32(bp, B44_MAC_CTRL, MAC_CTRL_CRC32_ENAB | MAC_CTRL_PHY_LEDCTRL);
1399 bw32(bp, B44_RCV_LAZY, (1 << RCV_LAZY_FC_SHIFT));
1400
1401 /* This sets the MAC address too. */
1402 __b44_set_rx_mode(bp->dev);
1403
1404 /* MTU + eth header + possible VLAN tag + struct rx_header */
1405 bw32(bp, B44_RXMAXLEN, bp->dev->mtu + ETH_HLEN + 8 + RX_HEADER_LEN);
1406 bw32(bp, B44_TXMAXLEN, bp->dev->mtu + ETH_HLEN + 8 + RX_HEADER_LEN);
1407
1408 bw32(bp, B44_TX_WMARK, 56); /* XXX magic */
Michael Chan5fc7d612007-01-26 23:59:57 -08001409 if (reset_kind == B44_PARTIAL_RESET) {
1410 bw32(bp, B44_DMARX_CTRL, (DMARX_CTRL_ENABLE |
Stephen Hemminger72f48612007-06-04 13:25:39 -07001411 (RX_PKT_OFFSET << DMARX_CTRL_ROSHIFT)));
Michael Chan5fc7d612007-01-26 23:59:57 -08001412 } else {
Gary Zambrano00e8b3a2006-06-20 15:34:26 -07001413 bw32(bp, B44_DMATX_CTRL, DMATX_CTRL_ENABLE);
1414 bw32(bp, B44_DMATX_ADDR, bp->tx_ring_dma + bp->dma_offset);
1415 bw32(bp, B44_DMARX_CTRL, (DMARX_CTRL_ENABLE |
Stephen Hemminger72f48612007-06-04 13:25:39 -07001416 (RX_PKT_OFFSET << DMARX_CTRL_ROSHIFT)));
Gary Zambrano00e8b3a2006-06-20 15:34:26 -07001417 bw32(bp, B44_DMARX_ADDR, bp->rx_ring_dma + bp->dma_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Gary Zambrano00e8b3a2006-06-20 15:34:26 -07001419 bw32(bp, B44_DMARX_PTR, bp->rx_pending);
1420 bp->rx_prod = bp->rx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Gary Zambrano00e8b3a2006-06-20 15:34:26 -07001422 bw32(bp, B44_MIB_CTRL, MIB_CTRL_CLR_ON_READ);
Gary Zambrano00e8b3a2006-06-20 15:34:26 -07001423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 val = br32(bp, B44_ENET_CTRL);
1426 bw32(bp, B44_ENET_CTRL, (val | ENET_CTRL_ENABLE));
Hauke Mehrtens50555442013-09-28 23:22:18 +02001427
1428 netdev_reset_queue(bp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
1431static int b44_open(struct net_device *dev)
1432{
1433 struct b44 *bp = netdev_priv(dev);
1434 int err;
1435
Michael Buesch753f4922007-09-19 14:20:30 -07001436 err = b44_alloc_consistent(bp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (err)
Francois Romieu6c2f4262005-11-07 01:52:57 +01001438 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001440 napi_enable(&bp->napi);
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 b44_init_rings(bp);
Michael Chan5fc7d612007-01-26 23:59:57 -08001443 b44_init_hw(bp, B44_FULL_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
John W. Linvillee254e9b2005-06-08 15:11:57 -04001445 b44_check_phy(bp);
1446
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001447 err = request_irq(dev->irq, b44_interrupt, IRQF_SHARED, dev->name, dev);
Francois Romieu6c2f4262005-11-07 01:52:57 +01001448 if (unlikely(err < 0)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001449 napi_disable(&bp->napi);
Miguel Botónfedb0ee2008-01-01 01:17:54 +01001450 b44_chip_reset(bp, B44_CHIP_RESET_PARTIAL);
Francois Romieu6c2f4262005-11-07 01:52:57 +01001451 b44_free_rings(bp);
1452 b44_free_consistent(bp);
1453 goto out;
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 init_timer(&bp->timer);
1457 bp->timer.expires = jiffies + HZ;
1458 bp->timer.data = (unsigned long) bp;
1459 bp->timer.function = b44_timer;
1460 add_timer(&bp->timer);
1461
1462 b44_enable_ints(bp);
Mark Lordd9e2d182005-11-30 22:30:23 +01001463 netif_start_queue(dev);
Francois Romieu6c2f4262005-11-07 01:52:57 +01001464out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return err;
1466}
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468#ifdef CONFIG_NET_POLL_CONTROLLER
1469/*
1470 * Polling receive - used by netconsole and other diagnostic tools
1471 * to allow network i/o with interrupts disabled.
1472 */
1473static void b44_poll_controller(struct net_device *dev)
1474{
1475 disable_irq(dev->irq);
David Howells7d12e782006-10-05 14:55:46 +01001476 b44_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 enable_irq(dev->irq);
1478}
1479#endif
1480
Gary Zambrano725ad802006-06-20 15:34:36 -07001481static void bwfilter_table(struct b44 *bp, u8 *pp, u32 bytes, u32 table_offset)
1482{
1483 u32 i;
1484 u32 *pattern = (u32 *) pp;
1485
1486 for (i = 0; i < bytes; i += sizeof(u32)) {
1487 bw32(bp, B44_FILT_ADDR, table_offset + i);
1488 bw32(bp, B44_FILT_DATA, pattern[i / sizeof(u32)]);
1489 }
1490}
1491
1492static int b44_magic_pattern(u8 *macaddr, u8 *ppattern, u8 *pmask, int offset)
1493{
1494 int magicsync = 6;
1495 int k, j, len = offset;
1496 int ethaddr_bytes = ETH_ALEN;
1497
1498 memset(ppattern + offset, 0xff, magicsync);
1499 for (j = 0; j < magicsync; j++)
1500 set_bit(len++, (unsigned long *) pmask);
1501
1502 for (j = 0; j < B44_MAX_PATTERNS; j++) {
1503 if ((B44_PATTERN_SIZE - len) >= ETH_ALEN)
1504 ethaddr_bytes = ETH_ALEN;
1505 else
1506 ethaddr_bytes = B44_PATTERN_SIZE - len;
1507 if (ethaddr_bytes <=0)
1508 break;
1509 for (k = 0; k< ethaddr_bytes; k++) {
1510 ppattern[offset + magicsync +
1511 (j * ETH_ALEN) + k] = macaddr[k];
Stanislav Brabece0188822009-12-08 21:00:22 -08001512 set_bit(len++, (unsigned long *) pmask);
Gary Zambrano725ad802006-06-20 15:34:36 -07001513 }
1514 }
1515 return len - 1;
1516}
1517
1518/* Setup magic packet patterns in the b44 WOL
1519 * pattern matching filter.
1520 */
1521static void b44_setup_pseudo_magicp(struct b44 *bp)
1522{
1523
1524 u32 val;
1525 int plen0, plen1, plen2;
1526 u8 *pwol_pattern;
1527 u8 pwol_mask[B44_PMASK_SIZE];
1528
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001529 pwol_pattern = kzalloc(B44_PATTERN_SIZE, GFP_KERNEL);
Joe Perchesb2adaca2013-02-03 17:43:58 +00001530 if (!pwol_pattern)
Gary Zambrano725ad802006-06-20 15:34:36 -07001531 return;
Gary Zambrano725ad802006-06-20 15:34:36 -07001532
1533 /* Ipv4 magic packet pattern - pattern 0.*/
Gary Zambrano725ad802006-06-20 15:34:36 -07001534 memset(pwol_mask, 0, B44_PMASK_SIZE);
1535 plen0 = b44_magic_pattern(bp->dev->dev_addr, pwol_pattern, pwol_mask,
1536 B44_ETHIPV4UDP_HLEN);
1537
1538 bwfilter_table(bp, pwol_pattern, B44_PATTERN_SIZE, B44_PATTERN_BASE);
1539 bwfilter_table(bp, pwol_mask, B44_PMASK_SIZE, B44_PMASK_BASE);
1540
1541 /* Raw ethernet II magic packet pattern - pattern 1 */
1542 memset(pwol_pattern, 0, B44_PATTERN_SIZE);
1543 memset(pwol_mask, 0, B44_PMASK_SIZE);
1544 plen1 = b44_magic_pattern(bp->dev->dev_addr, pwol_pattern, pwol_mask,
1545 ETH_HLEN);
1546
1547 bwfilter_table(bp, pwol_pattern, B44_PATTERN_SIZE,
1548 B44_PATTERN_BASE + B44_PATTERN_SIZE);
1549 bwfilter_table(bp, pwol_mask, B44_PMASK_SIZE,
1550 B44_PMASK_BASE + B44_PMASK_SIZE);
1551
1552 /* Ipv6 magic packet pattern - pattern 2 */
1553 memset(pwol_pattern, 0, B44_PATTERN_SIZE);
1554 memset(pwol_mask, 0, B44_PMASK_SIZE);
1555 plen2 = b44_magic_pattern(bp->dev->dev_addr, pwol_pattern, pwol_mask,
1556 B44_ETHIPV6UDP_HLEN);
1557
1558 bwfilter_table(bp, pwol_pattern, B44_PATTERN_SIZE,
1559 B44_PATTERN_BASE + B44_PATTERN_SIZE + B44_PATTERN_SIZE);
1560 bwfilter_table(bp, pwol_mask, B44_PMASK_SIZE,
1561 B44_PMASK_BASE + B44_PMASK_SIZE + B44_PMASK_SIZE);
1562
1563 kfree(pwol_pattern);
1564
1565 /* set these pattern's lengths: one less than each real length */
1566 val = plen0 | (plen1 << 8) | (plen2 << 16) | WKUP_LEN_ENABLE_THREE;
1567 bw32(bp, B44_WKUP_LEN, val);
1568
1569 /* enable wakeup pattern matching */
1570 val = br32(bp, B44_DEVCTRL);
1571 bw32(bp, B44_DEVCTRL, val | DEVCTRL_PFE);
1572
1573}
Gary Zambrano52cafd92006-06-20 15:34:23 -07001574
Michael Buesch753f4922007-09-19 14:20:30 -07001575#ifdef CONFIG_B44_PCI
1576static void b44_setup_wol_pci(struct b44 *bp)
1577{
1578 u16 val;
1579
1580 if (bp->sdev->bus->bustype != SSB_BUSTYPE_SSB) {
1581 bw32(bp, SSB_TMSLOW, br32(bp, SSB_TMSLOW) | SSB_TMSLOW_PE);
1582 pci_read_config_word(bp->sdev->bus->host_pci, SSB_PMCSR, &val);
1583 pci_write_config_word(bp->sdev->bus->host_pci, SSB_PMCSR, val | SSB_PE);
1584 }
1585}
1586#else
1587static inline void b44_setup_wol_pci(struct b44 *bp) { }
1588#endif /* CONFIG_B44_PCI */
1589
Gary Zambrano52cafd92006-06-20 15:34:23 -07001590static void b44_setup_wol(struct b44 *bp)
1591{
1592 u32 val;
Gary Zambrano52cafd92006-06-20 15:34:23 -07001593
1594 bw32(bp, B44_RXCONFIG, RXCONFIG_ALLMULTI);
1595
1596 if (bp->flags & B44_FLAG_B0_ANDLATER) {
1597
1598 bw32(bp, B44_WKUP_LEN, WKUP_LEN_DISABLE);
1599
1600 val = bp->dev->dev_addr[2] << 24 |
1601 bp->dev->dev_addr[3] << 16 |
1602 bp->dev->dev_addr[4] << 8 |
1603 bp->dev->dev_addr[5];
1604 bw32(bp, B44_ADDR_LO, val);
1605
1606 val = bp->dev->dev_addr[0] << 8 |
1607 bp->dev->dev_addr[1];
1608 bw32(bp, B44_ADDR_HI, val);
1609
1610 val = br32(bp, B44_DEVCTRL);
1611 bw32(bp, B44_DEVCTRL, val | DEVCTRL_MPM | DEVCTRL_PFE);
1612
Gary Zambrano725ad802006-06-20 15:34:36 -07001613 } else {
1614 b44_setup_pseudo_magicp(bp);
1615 }
Michael Buesch753f4922007-09-19 14:20:30 -07001616 b44_setup_wol_pci(bp);
Gary Zambrano52cafd92006-06-20 15:34:23 -07001617}
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619static int b44_close(struct net_device *dev)
1620{
1621 struct b44 *bp = netdev_priv(dev);
1622
1623 netif_stop_queue(dev);
1624
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001625 napi_disable(&bp->napi);
Francois Romieuba5eec92005-11-08 23:37:12 +01001626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 del_timer_sync(&bp->timer);
1628
1629 spin_lock_irq(&bp->lock);
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 b44_halt(bp);
1632 b44_free_rings(bp);
Stephen Hemmingerc35ca392006-01-20 21:13:17 -08001633 netif_carrier_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 spin_unlock_irq(&bp->lock);
1636
1637 free_irq(dev->irq, dev);
1638
Gary Zambrano52cafd92006-06-20 15:34:23 -07001639 if (bp->flags & B44_FLAG_WOL_ENABLE) {
Michael Chan5fc7d612007-01-26 23:59:57 -08001640 b44_init_hw(bp, B44_PARTIAL_RESET);
Gary Zambrano52cafd92006-06-20 15:34:23 -07001641 b44_setup_wol(bp);
1642 }
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 b44_free_consistent(bp);
1645
1646 return 0;
1647}
1648
Kevin Groeneveldeeda8582012-07-17 17:46:01 +00001649static struct rtnl_link_stats64 *b44_get_stats64(struct net_device *dev,
1650 struct rtnl_link_stats64 *nstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 struct b44 *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 struct b44_hw_stats *hwstat = &bp->hw_stats;
Kevin Groeneveldeeda8582012-07-17 17:46:01 +00001654 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Kevin Groeneveldeeda8582012-07-17 17:46:01 +00001656 do {
1657 start = u64_stats_fetch_begin_bh(&hwstat->syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Kevin Groeneveldeeda8582012-07-17 17:46:01 +00001659 /* Convert HW stats into rtnl_link_stats64 stats. */
1660 nstat->rx_packets = hwstat->rx_pkts;
1661 nstat->tx_packets = hwstat->tx_pkts;
1662 nstat->rx_bytes = hwstat->rx_octets;
1663 nstat->tx_bytes = hwstat->tx_octets;
1664 nstat->tx_errors = (hwstat->tx_jabber_pkts +
1665 hwstat->tx_oversize_pkts +
1666 hwstat->tx_underruns +
1667 hwstat->tx_excessive_cols +
1668 hwstat->tx_late_cols);
1669 nstat->multicast = hwstat->tx_multicast_pkts;
1670 nstat->collisions = hwstat->tx_total_cols;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Kevin Groeneveldeeda8582012-07-17 17:46:01 +00001672 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1673 hwstat->rx_undersize);
1674 nstat->rx_over_errors = hwstat->rx_missed_pkts;
1675 nstat->rx_frame_errors = hwstat->rx_align_errs;
1676 nstat->rx_crc_errors = hwstat->rx_crc_errs;
1677 nstat->rx_errors = (hwstat->rx_jabber_pkts +
1678 hwstat->rx_oversize_pkts +
1679 hwstat->rx_missed_pkts +
1680 hwstat->rx_crc_align_errs +
1681 hwstat->rx_undersize +
1682 hwstat->rx_crc_errs +
1683 hwstat->rx_align_errs +
1684 hwstat->rx_symbol_errs);
1685
1686 nstat->tx_aborted_errors = hwstat->tx_underruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687#if 0
Kevin Groeneveldeeda8582012-07-17 17:46:01 +00001688 /* Carrier lost counter seems to be broken for some devices */
1689 nstat->tx_carrier_errors = hwstat->tx_carrier_lost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690#endif
Kevin Groeneveldeeda8582012-07-17 17:46:01 +00001691 } while (u64_stats_fetch_retry_bh(&hwstat->syncp, start));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 return nstat;
1694}
1695
1696static int __b44_load_mcast(struct b44 *bp, struct net_device *dev)
1697{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001698 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 int i, num_ents;
1700
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001701 num_ents = min_t(int, netdev_mc_count(dev), B44_MCAST_TABLE_SIZE);
Jiri Pirko0ddf4772010-02-20 00:13:58 +00001702 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001703 netdev_for_each_mc_addr(ha, dev) {
Jiri Pirko0ddf4772010-02-20 00:13:58 +00001704 if (i == num_ents)
1705 break;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001706 __b44_cam_write(bp, ha->addr, i++ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 }
1708 return i+1;
1709}
1710
1711static void __b44_set_rx_mode(struct net_device *dev)
1712{
1713 struct b44 *bp = netdev_priv(dev);
1714 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
1716 val = br32(bp, B44_RXCONFIG);
1717 val &= ~(RXCONFIG_PROMISC | RXCONFIG_ALLMULTI);
Michael Buesch753f4922007-09-19 14:20:30 -07001718 if ((dev->flags & IFF_PROMISC) || (val & RXCONFIG_CAM_ABSENT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 val |= RXCONFIG_PROMISC;
1720 bw32(bp, B44_RXCONFIG, val);
1721 } else {
Francois Romieu874a6212005-11-07 01:50:46 +01001722 unsigned char zero[6] = {0, 0, 0, 0, 0, 0};
Bill Helfinstinecda22aa2007-04-01 13:10:28 -04001723 int i = 1;
Francois Romieu874a6212005-11-07 01:50:46 +01001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 __b44_set_mac_addr(bp);
1726
Jeff Garzik2f614fe2006-10-05 07:10:38 -04001727 if ((dev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001728 (netdev_mc_count(dev) > B44_MCAST_TABLE_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 val |= RXCONFIG_ALLMULTI;
1730 else
Francois Romieu874a6212005-11-07 01:50:46 +01001731 i = __b44_load_mcast(bp, dev);
Jeff Garzik10badc22006-04-12 18:04:32 -04001732
Jeff Garzik2f614fe2006-10-05 07:10:38 -04001733 for (; i < 64; i++)
Jeff Garzik10badc22006-04-12 18:04:32 -04001734 __b44_cam_write(bp, zero, i);
Jeff Garzik2f614fe2006-10-05 07:10:38 -04001735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 bw32(bp, B44_RXCONFIG, val);
1737 val = br32(bp, B44_CAM_CTRL);
1738 bw32(bp, B44_CAM_CTRL, val | CAM_CTRL_ENABLE);
1739 }
1740}
1741
1742static void b44_set_rx_mode(struct net_device *dev)
1743{
1744 struct b44 *bp = netdev_priv(dev);
1745
1746 spin_lock_irq(&bp->lock);
1747 __b44_set_rx_mode(dev);
1748 spin_unlock_irq(&bp->lock);
1749}
1750
1751static u32 b44_get_msglevel(struct net_device *dev)
1752{
1753 struct b44 *bp = netdev_priv(dev);
1754 return bp->msg_enable;
1755}
1756
1757static void b44_set_msglevel(struct net_device *dev, u32 value)
1758{
1759 struct b44 *bp = netdev_priv(dev);
1760 bp->msg_enable = value;
1761}
1762
1763static void b44_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
1764{
1765 struct b44 *bp = netdev_priv(dev);
Michael Buesch753f4922007-09-19 14:20:30 -07001766 struct ssb_bus *bus = bp->sdev->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
roel kluin27e09552009-07-17 08:01:54 +00001768 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
1769 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
Michael Buesch753f4922007-09-19 14:20:30 -07001770 switch (bus->bustype) {
1771 case SSB_BUSTYPE_PCI:
roel kluin27e09552009-07-17 08:01:54 +00001772 strlcpy(info->bus_info, pci_name(bus->host_pci), sizeof(info->bus_info));
Michael Buesch753f4922007-09-19 14:20:30 -07001773 break;
Michael Buesch753f4922007-09-19 14:20:30 -07001774 case SSB_BUSTYPE_SSB:
roel kluin27e09552009-07-17 08:01:54 +00001775 strlcpy(info->bus_info, "SSB", sizeof(info->bus_info));
Michael Buesch753f4922007-09-19 14:20:30 -07001776 break;
Michael Buesch98a1e2a2009-09-08 19:33:31 +02001777 case SSB_BUSTYPE_PCMCIA:
1778 case SSB_BUSTYPE_SDIO:
1779 WARN_ON(1); /* A device with this bus does not exist. */
1780 break;
Michael Buesch753f4922007-09-19 14:20:30 -07001781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
1784static int b44_nway_reset(struct net_device *dev)
1785{
1786 struct b44 *bp = netdev_priv(dev);
1787 u32 bmcr;
1788 int r;
1789
1790 spin_lock_irq(&bp->lock);
1791 b44_readphy(bp, MII_BMCR, &bmcr);
1792 b44_readphy(bp, MII_BMCR, &bmcr);
1793 r = -EINVAL;
1794 if (bmcr & BMCR_ANENABLE) {
1795 b44_writephy(bp, MII_BMCR,
1796 bmcr | BMCR_ANRESTART);
1797 r = 0;
1798 }
1799 spin_unlock_irq(&bp->lock);
1800
1801 return r;
1802}
1803
1804static int b44_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1805{
1806 struct b44 *bp = netdev_priv(dev);
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 cmd->supported = (SUPPORTED_Autoneg);
1809 cmd->supported |= (SUPPORTED_100baseT_Half |
1810 SUPPORTED_100baseT_Full |
1811 SUPPORTED_10baseT_Half |
1812 SUPPORTED_10baseT_Full |
1813 SUPPORTED_MII);
1814
1815 cmd->advertising = 0;
1816 if (bp->flags & B44_FLAG_ADV_10HALF)
Matthew Wilcoxadf6e002005-10-04 11:25:17 -06001817 cmd->advertising |= ADVERTISED_10baseT_Half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (bp->flags & B44_FLAG_ADV_10FULL)
Matthew Wilcoxadf6e002005-10-04 11:25:17 -06001819 cmd->advertising |= ADVERTISED_10baseT_Full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (bp->flags & B44_FLAG_ADV_100HALF)
Matthew Wilcoxadf6e002005-10-04 11:25:17 -06001821 cmd->advertising |= ADVERTISED_100baseT_Half;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (bp->flags & B44_FLAG_ADV_100FULL)
Matthew Wilcoxadf6e002005-10-04 11:25:17 -06001823 cmd->advertising |= ADVERTISED_100baseT_Full;
1824 cmd->advertising |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
David Decotigny70739492011-04-27 18:32:40 +00001825 ethtool_cmd_speed_set(cmd, ((bp->flags & B44_FLAG_100_BASE_T) ?
1826 SPEED_100 : SPEED_10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 cmd->duplex = (bp->flags & B44_FLAG_FULL_DUPLEX) ?
1828 DUPLEX_FULL : DUPLEX_HALF;
1829 cmd->port = 0;
1830 cmd->phy_address = bp->phy_addr;
Hauke Mehrtensd6194192013-12-20 02:16:06 +01001831 cmd->transceiver = (bp->flags & B44_FLAG_EXTERNAL_PHY) ?
1832 XCVR_EXTERNAL : XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 cmd->autoneg = (bp->flags & B44_FLAG_FORCE_LINK) ?
1834 AUTONEG_DISABLE : AUTONEG_ENABLE;
Gary Zambrano47b9c3b12006-06-20 15:34:15 -07001835 if (cmd->autoneg == AUTONEG_ENABLE)
1836 cmd->advertising |= ADVERTISED_Autoneg;
1837 if (!netif_running(dev)){
David Decotigny70739492011-04-27 18:32:40 +00001838 ethtool_cmd_speed_set(cmd, 0);
Gary Zambrano47b9c3b12006-06-20 15:34:15 -07001839 cmd->duplex = 0xff;
1840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 cmd->maxtxpkt = 0;
1842 cmd->maxrxpkt = 0;
1843 return 0;
1844}
1845
1846static int b44_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1847{
1848 struct b44 *bp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +00001849 u32 speed = ethtool_cmd_speed(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 /* We do not support gigabit. */
1852 if (cmd->autoneg == AUTONEG_ENABLE) {
1853 if (cmd->advertising &
1854 (ADVERTISED_1000baseT_Half |
1855 ADVERTISED_1000baseT_Full))
1856 return -EINVAL;
David Decotigny25db0332011-04-27 18:32:39 +00001857 } else if ((speed != SPEED_100 &&
1858 speed != SPEED_10) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 (cmd->duplex != DUPLEX_HALF &&
1860 cmd->duplex != DUPLEX_FULL)) {
1861 return -EINVAL;
1862 }
1863
1864 spin_lock_irq(&bp->lock);
1865
1866 if (cmd->autoneg == AUTONEG_ENABLE) {
Gary Zambrano47b9c3b12006-06-20 15:34:15 -07001867 bp->flags &= ~(B44_FLAG_FORCE_LINK |
1868 B44_FLAG_100_BASE_T |
1869 B44_FLAG_FULL_DUPLEX |
1870 B44_FLAG_ADV_10HALF |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 B44_FLAG_ADV_10FULL |
1872 B44_FLAG_ADV_100HALF |
1873 B44_FLAG_ADV_100FULL);
Gary Zambrano47b9c3b12006-06-20 15:34:15 -07001874 if (cmd->advertising == 0) {
1875 bp->flags |= (B44_FLAG_ADV_10HALF |
1876 B44_FLAG_ADV_10FULL |
1877 B44_FLAG_ADV_100HALF |
1878 B44_FLAG_ADV_100FULL);
1879 } else {
1880 if (cmd->advertising & ADVERTISED_10baseT_Half)
1881 bp->flags |= B44_FLAG_ADV_10HALF;
1882 if (cmd->advertising & ADVERTISED_10baseT_Full)
1883 bp->flags |= B44_FLAG_ADV_10FULL;
1884 if (cmd->advertising & ADVERTISED_100baseT_Half)
1885 bp->flags |= B44_FLAG_ADV_100HALF;
1886 if (cmd->advertising & ADVERTISED_100baseT_Full)
1887 bp->flags |= B44_FLAG_ADV_100FULL;
1888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 } else {
1890 bp->flags |= B44_FLAG_FORCE_LINK;
Gary Zambrano47b9c3b12006-06-20 15:34:15 -07001891 bp->flags &= ~(B44_FLAG_100_BASE_T | B44_FLAG_FULL_DUPLEX);
David Decotigny25db0332011-04-27 18:32:39 +00001892 if (speed == SPEED_100)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 bp->flags |= B44_FLAG_100_BASE_T;
1894 if (cmd->duplex == DUPLEX_FULL)
1895 bp->flags |= B44_FLAG_FULL_DUPLEX;
1896 }
1897
Gary Zambrano47b9c3b12006-06-20 15:34:15 -07001898 if (netif_running(dev))
1899 b44_setup_phy(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 spin_unlock_irq(&bp->lock);
1902
1903 return 0;
1904}
1905
1906static void b44_get_ringparam(struct net_device *dev,
1907 struct ethtool_ringparam *ering)
1908{
1909 struct b44 *bp = netdev_priv(dev);
1910
1911 ering->rx_max_pending = B44_RX_RING_SIZE - 1;
1912 ering->rx_pending = bp->rx_pending;
1913
1914 /* XXX ethtool lacks a tx_max_pending, oops... */
1915}
1916
1917static int b44_set_ringparam(struct net_device *dev,
1918 struct ethtool_ringparam *ering)
1919{
1920 struct b44 *bp = netdev_priv(dev);
1921
1922 if ((ering->rx_pending > B44_RX_RING_SIZE - 1) ||
1923 (ering->rx_mini_pending != 0) ||
1924 (ering->rx_jumbo_pending != 0) ||
1925 (ering->tx_pending > B44_TX_RING_SIZE - 1))
1926 return -EINVAL;
1927
1928 spin_lock_irq(&bp->lock);
1929
1930 bp->rx_pending = ering->rx_pending;
1931 bp->tx_pending = ering->tx_pending;
1932
1933 b44_halt(bp);
1934 b44_init_rings(bp);
Michael Chan5fc7d612007-01-26 23:59:57 -08001935 b44_init_hw(bp, B44_FULL_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 netif_wake_queue(bp->dev);
1937 spin_unlock_irq(&bp->lock);
1938
1939 b44_enable_ints(bp);
Jeff Garzik10badc22006-04-12 18:04:32 -04001940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return 0;
1942}
1943
1944static void b44_get_pauseparam(struct net_device *dev,
1945 struct ethtool_pauseparam *epause)
1946{
1947 struct b44 *bp = netdev_priv(dev);
1948
1949 epause->autoneg =
1950 (bp->flags & B44_FLAG_PAUSE_AUTO) != 0;
1951 epause->rx_pause =
1952 (bp->flags & B44_FLAG_RX_PAUSE) != 0;
1953 epause->tx_pause =
1954 (bp->flags & B44_FLAG_TX_PAUSE) != 0;
1955}
1956
1957static int b44_set_pauseparam(struct net_device *dev,
1958 struct ethtool_pauseparam *epause)
1959{
1960 struct b44 *bp = netdev_priv(dev);
1961
1962 spin_lock_irq(&bp->lock);
1963 if (epause->autoneg)
1964 bp->flags |= B44_FLAG_PAUSE_AUTO;
1965 else
1966 bp->flags &= ~B44_FLAG_PAUSE_AUTO;
1967 if (epause->rx_pause)
1968 bp->flags |= B44_FLAG_RX_PAUSE;
1969 else
1970 bp->flags &= ~B44_FLAG_RX_PAUSE;
1971 if (epause->tx_pause)
1972 bp->flags |= B44_FLAG_TX_PAUSE;
1973 else
1974 bp->flags &= ~B44_FLAG_TX_PAUSE;
1975 if (bp->flags & B44_FLAG_PAUSE_AUTO) {
1976 b44_halt(bp);
1977 b44_init_rings(bp);
Michael Chan5fc7d612007-01-26 23:59:57 -08001978 b44_init_hw(bp, B44_FULL_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 } else {
1980 __b44_set_flow_ctrl(bp, bp->flags);
1981 }
1982 spin_unlock_irq(&bp->lock);
1983
1984 b44_enable_ints(bp);
Jeff Garzik10badc22006-04-12 18:04:32 -04001985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return 0;
1987}
1988
Francois Romieu33539302005-11-07 01:51:34 +01001989static void b44_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1990{
1991 switch(stringset) {
1992 case ETH_SS_STATS:
1993 memcpy(data, *b44_gstrings, sizeof(b44_gstrings));
1994 break;
1995 }
1996}
1997
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001998static int b44_get_sset_count(struct net_device *dev, int sset)
Francois Romieu33539302005-11-07 01:51:34 +01001999{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002000 switch (sset) {
2001 case ETH_SS_STATS:
2002 return ARRAY_SIZE(b44_gstrings);
2003 default:
2004 return -EOPNOTSUPP;
2005 }
Francois Romieu33539302005-11-07 01:51:34 +01002006}
2007
2008static void b44_get_ethtool_stats(struct net_device *dev,
2009 struct ethtool_stats *stats, u64 *data)
2010{
2011 struct b44 *bp = netdev_priv(dev);
Kevin Groeneveldeeda8582012-07-17 17:46:01 +00002012 struct b44_hw_stats *hwstat = &bp->hw_stats;
2013 u64 *data_src, *data_dst;
2014 unsigned int start;
Francois Romieu33539302005-11-07 01:51:34 +01002015 u32 i;
2016
2017 spin_lock_irq(&bp->lock);
Francois Romieu33539302005-11-07 01:51:34 +01002018 b44_stats_update(bp);
Francois Romieu33539302005-11-07 01:51:34 +01002019 spin_unlock_irq(&bp->lock);
Kevin Groeneveldeeda8582012-07-17 17:46:01 +00002020
2021 do {
2022 data_src = &hwstat->tx_good_octets;
2023 data_dst = data;
2024 start = u64_stats_fetch_begin_bh(&hwstat->syncp);
2025
2026 for (i = 0; i < ARRAY_SIZE(b44_gstrings); i++)
2027 *data_dst++ = *data_src++;
2028
2029 } while (u64_stats_fetch_retry_bh(&hwstat->syncp, start));
Francois Romieu33539302005-11-07 01:51:34 +01002030}
2031
Gary Zambrano52cafd92006-06-20 15:34:23 -07002032static void b44_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2033{
2034 struct b44 *bp = netdev_priv(dev);
2035
2036 wol->supported = WAKE_MAGIC;
2037 if (bp->flags & B44_FLAG_WOL_ENABLE)
2038 wol->wolopts = WAKE_MAGIC;
2039 else
2040 wol->wolopts = 0;
2041 memset(&wol->sopass, 0, sizeof(wol->sopass));
2042}
2043
2044static int b44_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2045{
2046 struct b44 *bp = netdev_priv(dev);
2047
2048 spin_lock_irq(&bp->lock);
2049 if (wol->wolopts & WAKE_MAGIC)
2050 bp->flags |= B44_FLAG_WOL_ENABLE;
2051 else
2052 bp->flags &= ~B44_FLAG_WOL_ENABLE;
2053 spin_unlock_irq(&bp->lock);
2054
2055 return 0;
2056}
2057
Jeff Garzik7282d492006-09-13 14:30:00 -04002058static const struct ethtool_ops b44_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 .get_drvinfo = b44_get_drvinfo,
2060 .get_settings = b44_get_settings,
2061 .set_settings = b44_set_settings,
2062 .nway_reset = b44_nway_reset,
2063 .get_link = ethtool_op_get_link,
Gary Zambrano52cafd92006-06-20 15:34:23 -07002064 .get_wol = b44_get_wol,
2065 .set_wol = b44_set_wol,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 .get_ringparam = b44_get_ringparam,
2067 .set_ringparam = b44_set_ringparam,
2068 .get_pauseparam = b44_get_pauseparam,
2069 .set_pauseparam = b44_set_pauseparam,
2070 .get_msglevel = b44_get_msglevel,
2071 .set_msglevel = b44_set_msglevel,
Francois Romieu33539302005-11-07 01:51:34 +01002072 .get_strings = b44_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002073 .get_sset_count = b44_get_sset_count,
Francois Romieu33539302005-11-07 01:51:34 +01002074 .get_ethtool_stats = b44_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075};
2076
2077static int b44_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2078{
2079 struct mii_ioctl_data *data = if_mii(ifr);
2080 struct b44 *bp = netdev_priv(dev);
Francois Romieu34105722005-11-30 22:32:13 +01002081 int err = -EINVAL;
2082
2083 if (!netif_running(dev))
2084 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 spin_lock_irq(&bp->lock);
2087 err = generic_mii_ioctl(&bp->mii_if, data, cmd, NULL);
2088 spin_unlock_irq(&bp->lock);
Francois Romieu34105722005-11-30 22:32:13 +01002089out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return err;
2091}
2092
Bill Pemberton23971882012-12-03 09:22:57 -05002093static int b44_get_invariants(struct b44 *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Michael Buesch753f4922007-09-19 14:20:30 -07002095 struct ssb_device *sdev = bp->sdev;
2096 int err = 0;
2097 u8 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Michael Buesch753f4922007-09-19 14:20:30 -07002099 bp->dma_offset = ssb_dma_translation(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Michael Buesch753f4922007-09-19 14:20:30 -07002101 if (sdev->bus->bustype == SSB_BUSTYPE_SSB &&
2102 instance > 1) {
Larry Finger458414b2007-11-09 16:56:10 -06002103 addr = sdev->bus->sprom.et1mac;
2104 bp->phy_addr = sdev->bus->sprom.et1phyaddr;
Michael Buesch753f4922007-09-19 14:20:30 -07002105 } else {
Larry Finger458414b2007-11-09 16:56:10 -06002106 addr = sdev->bus->sprom.et0mac;
2107 bp->phy_addr = sdev->bus->sprom.et0phyaddr;
Michael Buesch753f4922007-09-19 14:20:30 -07002108 }
Michael Buesch5ea79632008-03-25 18:04:46 +01002109 /* Some ROMs have buggy PHY addresses with the high
2110 * bits set (sign extension?). Truncate them to a
2111 * valid PHY address. */
2112 bp->phy_addr &= 0x1F;
2113
Joe Perchesd458cdf2013-10-01 19:04:40 -07002114 memcpy(bp->dev->dev_addr, addr, ETH_ALEN);
Gary Zambrano391fc092006-03-28 14:57:38 -08002115
2116 if (!is_valid_ether_addr(&bp->dev->dev_addr[0])){
Joe Perches2fc96ff2010-02-17 15:01:50 +00002117 pr_err("Invalid MAC address found in EEPROM\n");
Gary Zambrano391fc092006-03-28 14:57:38 -08002118 return -EINVAL;
2119 }
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 bp->imask = IMASK_DEF;
2122
Jeff Garzik10badc22006-04-12 18:04:32 -04002123 /* XXX - really required?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 bp->flags |= B44_FLAG_BUGGY_TXPTR;
Michael Buesch753f4922007-09-19 14:20:30 -07002125 */
Gary Zambrano52cafd92006-06-20 15:34:23 -07002126
Michael Buesch753f4922007-09-19 14:20:30 -07002127 if (bp->sdev->id.revision >= 7)
2128 bp->flags |= B44_FLAG_B0_ANDLATER;
Gary Zambrano52cafd92006-06-20 15:34:23 -07002129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return err;
2131}
2132
Stephen Hemminger403413e2009-01-07 18:10:49 -08002133static const struct net_device_ops b44_netdev_ops = {
2134 .ndo_open = b44_open,
2135 .ndo_stop = b44_close,
2136 .ndo_start_xmit = b44_start_xmit,
Kevin Groeneveldeeda8582012-07-17 17:46:01 +00002137 .ndo_get_stats64 = b44_get_stats64,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002138 .ndo_set_rx_mode = b44_set_rx_mode,
Stephen Hemminger403413e2009-01-07 18:10:49 -08002139 .ndo_set_mac_address = b44_set_mac_addr,
2140 .ndo_validate_addr = eth_validate_addr,
2141 .ndo_do_ioctl = b44_ioctl,
2142 .ndo_tx_timeout = b44_tx_timeout,
2143 .ndo_change_mtu = b44_change_mtu,
2144#ifdef CONFIG_NET_POLL_CONTROLLER
2145 .ndo_poll_controller = b44_poll_controller,
2146#endif
2147};
2148
Bill Pemberton23971882012-12-03 09:22:57 -05002149static int b44_init_one(struct ssb_device *sdev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002150 const struct ssb_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 struct net_device *dev;
2153 struct b44 *bp;
Joe Perches0795af52007-10-03 17:59:30 -07002154 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Michael Buesch753f4922007-09-19 14:20:30 -07002156 instance++;
2157
Joe Perches7329f0d2011-07-05 07:43:46 +00002158 pr_info_once("%s version %s\n", DRV_DESCRIPTION, DRV_MODULE_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 dev = alloc_etherdev(sizeof(*bp));
2161 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 err = -ENOMEM;
Michael Buesch753f4922007-09-19 14:20:30 -07002163 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165
Michael Buesch753f4922007-09-19 14:20:30 -07002166 SET_NETDEV_DEV(dev, sdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 /* No interesting netdevice features in this card... */
2169 dev->features |= 0;
2170
2171 bp = netdev_priv(dev);
Michael Buesch753f4922007-09-19 14:20:30 -07002172 bp->sdev = sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 bp->dev = dev;
Eric Dumazeta58c8912009-01-15 15:29:35 -08002174 bp->force_copybreak = 0;
Francois Romieu874a6212005-11-07 01:50:46 +01002175
2176 bp->msg_enable = netif_msg_init(b44_debug, B44_DEF_MSG_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 spin_lock_init(&bp->lock);
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 bp->rx_pending = B44_DEF_RX_RING_PENDING;
2181 bp->tx_pending = B44_DEF_TX_RING_PENDING;
2182
Stephen Hemminger403413e2009-01-07 18:10:49 -08002183 dev->netdev_ops = &b44_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002184 netif_napi_add(dev, &bp->napi, b44_poll, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 dev->watchdog_timeo = B44_TX_TIMEOUT;
Michael Buesch753f4922007-09-19 14:20:30 -07002186 dev->irq = sdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 SET_ETHTOOL_OPS(dev, &b44_ethtool_ops);
2188
Michael Buesch753f4922007-09-19 14:20:30 -07002189 err = ssb_bus_powerup(sdev->bus, 0);
2190 if (err) {
2191 dev_err(sdev->dev,
2192 "Failed to powerup the bus\n");
2193 goto err_out_free_dev;
2194 }
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07002195
Russell King4011f9f2013-06-26 19:04:29 +01002196 if (dma_set_mask_and_coherent(sdev->dma_dev, DMA_BIT_MASK(30))) {
Michael Buesch753f4922007-09-19 14:20:30 -07002197 dev_err(sdev->dev,
Joe Perches2fc96ff2010-02-17 15:01:50 +00002198 "Required 30BIT DMA mask unsupported by the system\n");
Michael Buesch753f4922007-09-19 14:20:30 -07002199 goto err_out_powerdown;
2200 }
FUJITA Tomonori39a6f4b2010-06-03 19:37:40 -07002201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 err = b44_get_invariants(bp);
2203 if (err) {
Michael Buesch753f4922007-09-19 14:20:30 -07002204 dev_err(sdev->dev,
Joe Perches2fc96ff2010-02-17 15:01:50 +00002205 "Problem fetching invariants of chip, aborting\n");
Michael Buesch753f4922007-09-19 14:20:30 -07002206 goto err_out_powerdown;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
2208
2209 bp->mii_if.dev = dev;
2210 bp->mii_if.mdio_read = b44_mii_read;
2211 bp->mii_if.mdio_write = b44_mii_write;
2212 bp->mii_if.phy_id = bp->phy_addr;
2213 bp->mii_if.phy_id_mask = 0x1f;
2214 bp->mii_if.reg_num_mask = 0x1f;
2215
2216 /* By default, advertise all speed/duplex settings. */
2217 bp->flags |= (B44_FLAG_ADV_10HALF | B44_FLAG_ADV_10FULL |
2218 B44_FLAG_ADV_100HALF | B44_FLAG_ADV_100FULL);
2219
2220 /* By default, auto-negotiate PAUSE. */
2221 bp->flags |= B44_FLAG_PAUSE_AUTO;
2222
2223 err = register_netdev(dev);
2224 if (err) {
Joe Perches2fc96ff2010-02-17 15:01:50 +00002225 dev_err(sdev->dev, "Cannot register net device, aborting\n");
Michael Buesch753f4922007-09-19 14:20:30 -07002226 goto err_out_powerdown;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
2228
Paul Fertserbcf64aa2010-10-11 15:45:35 -07002229 netif_carrier_off(dev);
2230
Michael Buesch753f4922007-09-19 14:20:30 -07002231 ssb_set_drvdata(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Jeff Garzik10badc22006-04-12 18:04:32 -04002233 /* Chip reset provides power to the b44 MAC & PCI cores, which
Gary Zambrano5c513122006-03-29 17:12:05 -05002234 * is necessary for MAC register access.
Jeff Garzik10badc22006-04-12 18:04:32 -04002235 */
Miguel Botónfedb0ee2008-01-01 01:17:54 +01002236 b44_chip_reset(bp, B44_CHIP_RESET_FULL);
Gary Zambrano5c513122006-03-29 17:12:05 -05002237
Hauke Mehrtens8850dce2010-02-20 10:55:25 +00002238 /* do a phy reset to test if there is an active phy */
2239 if (b44_phy_reset(bp) < 0)
2240 bp->phy_addr = B44_PHY_ADDR_NO_PHY;
2241
Joe Perches7329f0d2011-07-05 07:43:46 +00002242 netdev_info(dev, "%s %pM\n", DRV_DESCRIPTION, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 return 0;
2245
Michael Buesch753f4922007-09-19 14:20:30 -07002246err_out_powerdown:
2247 ssb_bus_may_powerdown(sdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249err_out_free_dev:
2250 free_netdev(dev);
2251
Michael Buesch753f4922007-09-19 14:20:30 -07002252out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return err;
2254}
2255
Bill Pemberton23971882012-12-03 09:22:57 -05002256static void b44_remove_one(struct ssb_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Michael Buesch753f4922007-09-19 14:20:30 -07002258 struct net_device *dev = ssb_get_drvdata(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Francois Romieu874a6212005-11-07 01:50:46 +01002260 unregister_netdev(dev);
Michael Buesche92aa632009-02-26 22:35:02 -08002261 ssb_device_disable(sdev, 0);
Michael Buesch753f4922007-09-19 14:20:30 -07002262 ssb_bus_may_powerdown(sdev->bus);
Francois Romieu874a6212005-11-07 01:50:46 +01002263 free_netdev(dev);
Miguel Botónfedb0ee2008-01-01 01:17:54 +01002264 ssb_pcihost_set_power_state(sdev, PCI_D3hot);
Michael Buesch753f4922007-09-19 14:20:30 -07002265 ssb_set_drvdata(sdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Michael Buesch753f4922007-09-19 14:20:30 -07002268static int b44_suspend(struct ssb_device *sdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
Michael Buesch753f4922007-09-19 14:20:30 -07002270 struct net_device *dev = ssb_get_drvdata(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 struct b44 *bp = netdev_priv(dev);
2272
Michael Buesch753f4922007-09-19 14:20:30 -07002273 if (!netif_running(dev))
2274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
2276 del_timer_sync(&bp->timer);
2277
Jeff Garzik10badc22006-04-12 18:04:32 -04002278 spin_lock_irq(&bp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 b44_halt(bp);
Jeff Garzik10badc22006-04-12 18:04:32 -04002281 netif_carrier_off(bp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 netif_device_detach(bp->dev);
2283 b44_free_rings(bp);
2284
2285 spin_unlock_irq(&bp->lock);
Pavel Machek46e17852005-10-28 15:14:47 -07002286
2287 free_irq(dev->irq, dev);
Gary Zambrano52cafd92006-06-20 15:34:23 -07002288 if (bp->flags & B44_FLAG_WOL_ENABLE) {
Michael Chan5fc7d612007-01-26 23:59:57 -08002289 b44_init_hw(bp, B44_PARTIAL_RESET);
Gary Zambrano52cafd92006-06-20 15:34:23 -07002290 b44_setup_wol(bp);
2291 }
Michael Buesch753f4922007-09-19 14:20:30 -07002292
Miguel Botónfedb0ee2008-01-01 01:17:54 +01002293 ssb_pcihost_set_power_state(sdev, PCI_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return 0;
2295}
2296
Michael Buesch753f4922007-09-19 14:20:30 -07002297static int b44_resume(struct ssb_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Michael Buesch753f4922007-09-19 14:20:30 -07002299 struct net_device *dev = ssb_get_drvdata(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 struct b44 *bp = netdev_priv(dev);
Dmitriy Monakhov90afd0e2007-01-27 00:00:03 -08002301 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Michael Buesch753f4922007-09-19 14:20:30 -07002303 rc = ssb_bus_powerup(sdev->bus, 0);
Dmitriy Monakhov90afd0e2007-01-27 00:00:03 -08002304 if (rc) {
Michael Buesch753f4922007-09-19 14:20:30 -07002305 dev_err(sdev->dev,
2306 "Failed to powerup the bus\n");
Dmitriy Monakhov90afd0e2007-01-27 00:00:03 -08002307 return rc;
2308 }
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (!netif_running(dev))
2311 return 0;
2312
James Hoganafed4cc2010-10-17 01:48:59 +00002313 spin_lock_irq(&bp->lock);
2314 b44_init_rings(bp);
2315 b44_init_hw(bp, B44_FULL_RESET);
2316 spin_unlock_irq(&bp->lock);
2317
2318 /*
2319 * As a shared interrupt, the handler can be called immediately. To be
2320 * able to check the interrupt status the hardware must already be
2321 * powered back on (b44_init_hw).
2322 */
Dmitriy Monakhov90afd0e2007-01-27 00:00:03 -08002323 rc = request_irq(dev->irq, b44_interrupt, IRQF_SHARED, dev->name, dev);
2324 if (rc) {
Joe Perches2fc96ff2010-02-17 15:01:50 +00002325 netdev_err(dev, "request_irq failed\n");
James Hoganafed4cc2010-10-17 01:48:59 +00002326 spin_lock_irq(&bp->lock);
2327 b44_halt(bp);
2328 b44_free_rings(bp);
2329 spin_unlock_irq(&bp->lock);
Dmitriy Monakhov90afd0e2007-01-27 00:00:03 -08002330 return rc;
2331 }
Pavel Machek46e17852005-10-28 15:14:47 -07002332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 netif_device_attach(bp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 b44_enable_ints(bp);
Mark Lordd9e2d182005-11-30 22:30:23 +01002336 netif_wake_queue(dev);
Stephen Hemmingera72a8172007-06-04 13:25:37 -07002337
2338 mod_timer(&bp->timer, jiffies + 1);
2339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return 0;
2341}
2342
Michael Buesch753f4922007-09-19 14:20:30 -07002343static struct ssb_driver b44_ssb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 .name = DRV_MODULE_NAME,
Michael Buesch753f4922007-09-19 14:20:30 -07002345 .id_table = b44_ssb_tbl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 .probe = b44_init_one,
Bill Pemberton23971882012-12-03 09:22:57 -05002347 .remove = b44_remove_one,
Michael Buesch753f4922007-09-19 14:20:30 -07002348 .suspend = b44_suspend,
2349 .resume = b44_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350};
2351
Hauke Mehrtenscd155982011-06-21 20:57:16 +02002352static inline int __init b44_pci_init(void)
Michael Buesch753f4922007-09-19 14:20:30 -07002353{
2354 int err = 0;
2355#ifdef CONFIG_B44_PCI
2356 err = ssb_pcihost_register(&b44_pci_driver);
2357#endif
2358 return err;
2359}
2360
Nikola Pajkovsky64f0a832012-02-19 10:47:43 +00002361static inline void b44_pci_exit(void)
Michael Buesch753f4922007-09-19 14:20:30 -07002362{
2363#ifdef CONFIG_B44_PCI
2364 ssb_pcihost_unregister(&b44_pci_driver);
2365#endif
2366}
2367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368static int __init b44_init(void)
2369{
John W. Linville9f38c632005-10-18 21:30:59 -04002370 unsigned int dma_desc_align_size = dma_get_cache_alignment();
Michael Buesch753f4922007-09-19 14:20:30 -07002371 int err;
John W. Linville9f38c632005-10-18 21:30:59 -04002372
2373 /* Setup paramaters for syncing RX/TX DMA descriptors */
Alan Cox22d4d772006-01-17 17:53:56 +00002374 dma_desc_sync_size = max_t(unsigned int, dma_desc_align_size, sizeof(struct dma_desc));
John W. Linville9f38c632005-10-18 21:30:59 -04002375
Michael Buesch753f4922007-09-19 14:20:30 -07002376 err = b44_pci_init();
2377 if (err)
2378 return err;
2379 err = ssb_driver_register(&b44_ssb_driver);
2380 if (err)
2381 b44_pci_exit();
2382 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
2385static void __exit b44_cleanup(void)
2386{
Michael Buesch753f4922007-09-19 14:20:30 -07002387 ssb_driver_unregister(&b44_ssb_driver);
2388 b44_pci_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
2391module_init(b44_init);
2392module_exit(b44_cleanup);
2393