blob: 2e39e0285d8f9d2a6a9712ce33249749ede49377 [file] [log] [blame]
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001/*
2 * Copyright (C) 2006-2007 PA Semi, Inc
3 *
4 * Driver for the PA Semi PWRficient onchip 1G/10G Ethernet MACs
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/pci.h>
23#include <linux/interrupt.h>
24#include <linux/dmaengine.h>
25#include <linux/delay.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <asm/dma-mapping.h>
29#include <linux/in.h>
30#include <linux/skbuff.h>
31
32#include <linux/ip.h>
33#include <linux/tcp.h>
34#include <net/checksum.h>
Olof Johansson28ae79f2007-11-28 20:57:27 -060035#include <linux/inet_lro.h>
Olof Johanssonf5cd7872007-01-31 21:43:54 -060036
Olof Johansson771f7402007-05-08 00:47:21 -050037#include <asm/irq.h>
Olof Johanssonaf289e82007-10-03 13:03:54 -050038#include <asm/firmware.h>
Olof Johansson40afa532007-11-28 20:56:04 -060039#include <asm/pasemi_dma.h>
Olof Johansson771f7402007-05-08 00:47:21 -050040
Olof Johanssonf5cd7872007-01-31 21:43:54 -060041#include "pasemi_mac.h"
42
Olof Johansson8dc121a2007-10-02 16:26:53 -050043/* We have our own align, since ppc64 in general has it at 0 because
44 * of design flaws in some of the server bridge chips. However, for
45 * PWRficient doing the unaligned copies is more expensive than doing
46 * unaligned DMA, so make sure the data is aligned instead.
47 */
48#define LOCAL_SKB_ALIGN 2
Olof Johanssonf5cd7872007-01-31 21:43:54 -060049
50/* TODO list
51 *
Olof Johanssonf5cd7872007-01-31 21:43:54 -060052 * - Multicast support
53 * - Large MTU support
Olof Johansson7ddeae22007-10-02 16:27:28 -050054 * - SW LRO
55 * - Multiqueue RX/TX
Olof Johanssonf5cd7872007-01-31 21:43:54 -060056 */
57
58
59/* Must be a power of two */
Olof Johansson28ae79f2007-11-28 20:57:27 -060060#define RX_RING_SIZE 2048
Olof Johanssonad5da102007-10-02 16:27:15 -050061#define TX_RING_SIZE 4096
Olof Johanssonf5cd7872007-01-31 21:43:54 -060062
Olof Johansson28ae79f2007-11-28 20:57:27 -060063#define LRO_MAX_AGGR 64
64
Olof Johanssonef1ea0b2008-01-23 13:56:47 -060065#define PE_MIN_MTU 64
66#define PE_MAX_MTU 1500
67#define PE_DEF_MTU ETH_DATA_LEN
68
Olof Johanssonceb51362007-05-08 00:47:49 -050069#define DEFAULT_MSG_ENABLE \
70 (NETIF_MSG_DRV | \
71 NETIF_MSG_PROBE | \
72 NETIF_MSG_LINK | \
73 NETIF_MSG_TIMER | \
74 NETIF_MSG_IFDOWN | \
75 NETIF_MSG_IFUP | \
76 NETIF_MSG_RX_ERR | \
77 NETIF_MSG_TX_ERR)
78
Olof Johansson34c20622007-11-28 20:56:32 -060079#define TX_DESC(tx, num) ((tx)->chan.ring_virt[(num) & (TX_RING_SIZE-1)])
Olof Johansson72b05b92007-11-28 20:54:28 -060080#define TX_DESC_INFO(tx, num) ((tx)->ring_info[(num) & (TX_RING_SIZE-1)])
Olof Johansson34c20622007-11-28 20:56:32 -060081#define RX_DESC(rx, num) ((rx)->chan.ring_virt[(num) & (RX_RING_SIZE-1)])
Olof Johansson72b05b92007-11-28 20:54:28 -060082#define RX_DESC_INFO(rx, num) ((rx)->ring_info[(num) & (RX_RING_SIZE-1)])
83#define RX_BUFF(rx, num) ((rx)->buffers[(num) & (RX_RING_SIZE-1)])
Olof Johanssonf5cd7872007-01-31 21:43:54 -060084
Olof Johansson021fa222007-08-22 09:13:11 -050085#define RING_USED(ring) (((ring)->next_to_fill - (ring)->next_to_clean) \
86 & ((ring)->size - 1))
87#define RING_AVAIL(ring) ((ring->size) - RING_USED(ring))
88
Olof Johanssonceb51362007-05-08 00:47:49 -050089MODULE_LICENSE("GPL");
90MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
91MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
92
93static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
94module_param(debug, int, 0);
95MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060096
Olof Johanssonaf289e82007-10-03 13:03:54 -050097static int translation_enabled(void)
98{
99#if defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
100 return 1;
101#else
102 return firmware_has_feature(FW_FEATURE_LPAR);
103#endif
104}
105
Olof Johansson34c20622007-11-28 20:56:32 -0600106static void write_iob_reg(unsigned int reg, unsigned int val)
Olof Johanssona85b9422007-09-15 13:40:59 -0700107{
Olof Johansson34c20622007-11-28 20:56:32 -0600108 pasemi_write_iob_reg(reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700109}
110
Olof Johansson5c153322007-11-28 20:56:41 -0600111static unsigned int read_mac_reg(const struct pasemi_mac *mac, unsigned int reg)
Olof Johanssona85b9422007-09-15 13:40:59 -0700112{
Olof Johansson34c20622007-11-28 20:56:32 -0600113 return pasemi_read_mac_reg(mac->dma_if, reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700114}
115
Olof Johansson5c153322007-11-28 20:56:41 -0600116static void write_mac_reg(const struct pasemi_mac *mac, unsigned int reg,
Olof Johanssona85b9422007-09-15 13:40:59 -0700117 unsigned int val)
118{
Olof Johansson34c20622007-11-28 20:56:32 -0600119 pasemi_write_mac_reg(mac->dma_if, reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700120}
121
Olof Johansson34c20622007-11-28 20:56:32 -0600122static unsigned int read_dma_reg(unsigned int reg)
Olof Johanssona85b9422007-09-15 13:40:59 -0700123{
Olof Johansson34c20622007-11-28 20:56:32 -0600124 return pasemi_read_dma_reg(reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700125}
126
Olof Johansson34c20622007-11-28 20:56:32 -0600127static void write_dma_reg(unsigned int reg, unsigned int val)
Olof Johanssona85b9422007-09-15 13:40:59 -0700128{
Olof Johansson34c20622007-11-28 20:56:32 -0600129 pasemi_write_dma_reg(reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700130}
131
Olof Johansson5c153322007-11-28 20:56:41 -0600132static struct pasemi_mac_rxring *rx_ring(const struct pasemi_mac *mac)
Olof Johansson72b05b92007-11-28 20:54:28 -0600133{
134 return mac->rx;
135}
136
Olof Johansson5c153322007-11-28 20:56:41 -0600137static struct pasemi_mac_txring *tx_ring(const struct pasemi_mac *mac)
Olof Johansson72b05b92007-11-28 20:54:28 -0600138{
139 return mac->tx;
140}
141
Olof Johansson5c153322007-11-28 20:56:41 -0600142static inline void prefetch_skb(const struct sk_buff *skb)
143{
144 const void *d = skb;
145
146 prefetch(d);
147 prefetch(d+64);
148 prefetch(d+128);
149 prefetch(d+192);
150}
151
Olof Johansson34c20622007-11-28 20:56:32 -0600152static int mac_to_intf(struct pasemi_mac *mac)
153{
154 struct pci_dev *pdev = mac->pdev;
155 u32 tmp;
156 int nintf, off, i, j;
157 int devfn = pdev->devfn;
158
159 tmp = read_dma_reg(PAS_DMA_CAP_IFI);
160 nintf = (tmp & PAS_DMA_CAP_IFI_NIN_M) >> PAS_DMA_CAP_IFI_NIN_S;
161 off = (tmp & PAS_DMA_CAP_IFI_IOFF_M) >> PAS_DMA_CAP_IFI_IOFF_S;
162
163 /* IOFF contains the offset to the registers containing the
164 * DMA interface-to-MAC-pci-id mappings, and NIN contains number
165 * of total interfaces. Each register contains 4 devfns.
166 * Just do a linear search until we find the devfn of the MAC
167 * we're trying to look up.
168 */
169
170 for (i = 0; i < (nintf+3)/4; i++) {
171 tmp = read_dma_reg(off+4*i);
172 for (j = 0; j < 4; j++) {
173 if (((tmp >> (8*j)) & 0xff) == devfn)
174 return i*4 + j;
175 }
176 }
177 return -1;
178}
179
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600180static void pasemi_mac_intf_disable(struct pasemi_mac *mac)
181{
182 unsigned int flags;
183
184 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
185 flags &= ~PAS_MAC_CFG_PCFG_PE;
186 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
187}
188
189static void pasemi_mac_intf_enable(struct pasemi_mac *mac)
190{
191 unsigned int flags;
192
193 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
194 flags |= PAS_MAC_CFG_PCFG_PE;
195 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
196}
197
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600198static int pasemi_get_mac_addr(struct pasemi_mac *mac)
199{
200 struct pci_dev *pdev = mac->pdev;
201 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500202 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600203 const u8 *maddr;
204 u8 addr[6];
205
206 if (!dn) {
207 dev_dbg(&pdev->dev,
208 "No device node for mac, not configuring\n");
209 return -ENOENT;
210 }
211
olof@lixom.net1af7f052007-05-12 14:57:46 -0500212 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500213
olof@lixom.net1af7f052007-05-12 14:57:46 -0500214 if (maddr && len == 6) {
215 memcpy(mac->mac_addr, maddr, 6);
216 return 0;
217 }
218
219 /* Some old versions of firmware mistakenly uses mac-address
220 * (and as a string) instead of a byte array in local-mac-address.
221 */
222
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500223 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700224 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500225
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600226 if (maddr == NULL) {
227 dev_warn(&pdev->dev,
228 "no mac address in device tree, not configuring\n");
229 return -ENOENT;
230 }
231
232 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
233 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
234 dev_warn(&pdev->dev,
235 "can't parse mac address, not configuring\n");
236 return -EINVAL;
237 }
238
olof@lixom.net1af7f052007-05-12 14:57:46 -0500239 memcpy(mac->mac_addr, addr, 6);
240
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600241 return 0;
242}
243
Olof Johansson5cea73b2008-01-23 13:56:19 -0600244static int pasemi_mac_set_mac_addr(struct net_device *dev, void *p)
245{
246 struct pasemi_mac *mac = netdev_priv(dev);
247 struct sockaddr *addr = p;
248 unsigned int adr0, adr1;
249
250 if (!is_valid_ether_addr(addr->sa_data))
251 return -EINVAL;
252
253 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
254
255 adr0 = dev->dev_addr[2] << 24 |
256 dev->dev_addr[3] << 16 |
257 dev->dev_addr[4] << 8 |
258 dev->dev_addr[5];
259 adr1 = read_mac_reg(mac, PAS_MAC_CFG_ADR1);
260 adr1 &= ~0xffff;
261 adr1 |= dev->dev_addr[0] << 8 | dev->dev_addr[1];
262
263 pasemi_mac_intf_disable(mac);
264 write_mac_reg(mac, PAS_MAC_CFG_ADR0, adr0);
265 write_mac_reg(mac, PAS_MAC_CFG_ADR1, adr1);
266 pasemi_mac_intf_enable(mac);
267
268 return 0;
269}
270
Olof Johansson28ae79f2007-11-28 20:57:27 -0600271static int get_skb_hdr(struct sk_buff *skb, void **iphdr,
272 void **tcph, u64 *hdr_flags, void *data)
273{
274 u64 macrx = (u64) data;
275 unsigned int ip_len;
276 struct iphdr *iph;
277
278 /* IPv4 header checksum failed */
279 if ((macrx & XCT_MACRX_HTY_M) != XCT_MACRX_HTY_IPV4_OK)
280 return -1;
281
282 /* non tcp packet */
283 skb_reset_network_header(skb);
284 iph = ip_hdr(skb);
285 if (iph->protocol != IPPROTO_TCP)
286 return -1;
287
288 ip_len = ip_hdrlen(skb);
289 skb_set_transport_header(skb, ip_len);
290 *tcph = tcp_hdr(skb);
291
292 /* check if ip header and tcp header are complete */
293 if (iph->tot_len < ip_len + tcp_hdrlen(skb))
294 return -1;
295
296 *hdr_flags = LRO_IPV4 | LRO_TCP;
297 *iphdr = iph;
298
299 return 0;
300}
301
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500302static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
Olof Johansson7e9916e2007-11-28 20:57:45 -0600303 const int nfrags,
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500304 struct sk_buff *skb,
Olof Johansson5c153322007-11-28 20:56:41 -0600305 const dma_addr_t *dmas)
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500306{
307 int f;
Olof Johansson5c153322007-11-28 20:56:41 -0600308 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500309
Olof Johansson5c153322007-11-28 20:56:41 -0600310 pci_unmap_single(pdev, dmas[0], skb_headlen(skb), PCI_DMA_TODEVICE);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500311
312 for (f = 0; f < nfrags; f++) {
313 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
314
Olof Johansson5c153322007-11-28 20:56:41 -0600315 pci_unmap_page(pdev, dmas[f+1], frag->size, PCI_DMA_TODEVICE);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500316 }
317 dev_kfree_skb_irq(skb);
318
319 /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
320 * aligned up to a power of 2
321 */
322 return (nfrags + 3) & ~1;
323}
324
Olof Johansson5c153322007-11-28 20:56:41 -0600325static int pasemi_mac_setup_rx_resources(const struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600326{
327 struct pasemi_mac_rxring *ring;
328 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson34c20622007-11-28 20:56:32 -0600329 int chno;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500330 unsigned int cfg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600331
Olof Johansson34c20622007-11-28 20:56:32 -0600332 ring = pasemi_dma_alloc_chan(RXCHAN, sizeof(struct pasemi_mac_rxring),
333 offsetof(struct pasemi_mac_rxring, chan));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600334
Olof Johansson34c20622007-11-28 20:56:32 -0600335 if (!ring) {
336 dev_err(&mac->pdev->dev, "Can't allocate RX channel\n");
337 goto out_chan;
338 }
339 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600340
341 spin_lock_init(&ring->lock);
342
Olof Johansson021fa222007-08-22 09:13:11 -0500343 ring->size = RX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500344 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600345 RX_RING_SIZE, GFP_KERNEL);
346
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500347 if (!ring->ring_info)
348 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600349
350 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600351 if (pasemi_dma_alloc_ring(&ring->chan, RX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500352 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600353
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600354 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
355 RX_RING_SIZE * sizeof(u64),
356 &ring->buf_dma, GFP_KERNEL);
357 if (!ring->buffers)
Olof Johansson34c20622007-11-28 20:56:32 -0600358 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600359
360 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
361
Olof Johansson34c20622007-11-28 20:56:32 -0600362 write_dma_reg(PAS_DMA_RXCHAN_BASEL(chno),
363 PAS_DMA_RXCHAN_BASEL_BRBL(ring->chan.ring_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600364
Olof Johansson34c20622007-11-28 20:56:32 -0600365 write_dma_reg(PAS_DMA_RXCHAN_BASEU(chno),
366 PAS_DMA_RXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32) |
367 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600368
Olof Johansson5c153322007-11-28 20:56:41 -0600369 cfg = PAS_DMA_RXCHAN_CFG_HBU(2);
Olof Johanssonaf289e82007-10-03 13:03:54 -0500370
371 if (translation_enabled())
372 cfg |= PAS_DMA_RXCHAN_CFG_CTR;
373
Olof Johansson34c20622007-11-28 20:56:32 -0600374 write_dma_reg(PAS_DMA_RXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600375
Olof Johansson34c20622007-11-28 20:56:32 -0600376 write_dma_reg(PAS_DMA_RXINT_BASEL(mac->dma_if),
377 PAS_DMA_RXINT_BASEL_BRBL(ring->buf_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600378
Olof Johansson34c20622007-11-28 20:56:32 -0600379 write_dma_reg(PAS_DMA_RXINT_BASEU(mac->dma_if),
380 PAS_DMA_RXINT_BASEU_BRBH(ring->buf_dma >> 32) |
381 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600382
Olof Johansson5c153322007-11-28 20:56:41 -0600383 cfg = PAS_DMA_RXINT_CFG_DHL(2) | PAS_DMA_RXINT_CFG_L2 |
Olof Johanssonaf289e82007-10-03 13:03:54 -0500384 PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP |
385 PAS_DMA_RXINT_CFG_HEN;
386
387 if (translation_enabled())
388 cfg |= PAS_DMA_RXINT_CFG_ITRR | PAS_DMA_RXINT_CFG_ITR;
389
Olof Johansson34c20622007-11-28 20:56:32 -0600390 write_dma_reg(PAS_DMA_RXINT_CFG(mac->dma_if), cfg);
Olof Johanssonc0efd522007-08-22 09:12:52 -0500391
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600392 ring->next_to_fill = 0;
393 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600394 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600395 mac->rx = ring;
396
397 return 0;
398
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500399out_ring_desc:
400 kfree(ring->ring_info);
401out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600402 pasemi_dma_free_chan(&ring->chan);
403out_chan:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600404 return -ENOMEM;
405}
406
Olof Johansson72b05b92007-11-28 20:54:28 -0600407static struct pasemi_mac_txring *
Olof Johansson5c153322007-11-28 20:56:41 -0600408pasemi_mac_setup_tx_resources(const struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600409{
410 struct pasemi_mac *mac = netdev_priv(dev);
411 u32 val;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600412 struct pasemi_mac_txring *ring;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500413 unsigned int cfg;
Olof Johansson34c20622007-11-28 20:56:32 -0600414 int chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600415
Olof Johansson34c20622007-11-28 20:56:32 -0600416 ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_txring),
417 offsetof(struct pasemi_mac_txring, chan));
418
419 if (!ring) {
420 dev_err(&mac->pdev->dev, "Can't allocate TX channel\n");
421 goto out_chan;
422 }
423
424 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600425
426 spin_lock_init(&ring->lock);
427
Olof Johansson021fa222007-08-22 09:13:11 -0500428 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500429 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600430 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500431 if (!ring->ring_info)
432 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600433
434 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600435 if (pasemi_dma_alloc_ring(&ring->chan, TX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500436 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600437
Olof Johansson34c20622007-11-28 20:56:32 -0600438 write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno),
439 PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma));
440 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500441 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600442
Olof Johansson34c20622007-11-28 20:56:32 -0600443 write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600444
Olof Johanssonaf289e82007-10-03 13:03:54 -0500445 cfg = PAS_DMA_TXCHAN_CFG_TY_IFACE |
446 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
447 PAS_DMA_TXCHAN_CFG_UP |
448 PAS_DMA_TXCHAN_CFG_WT(2);
449
450 if (translation_enabled())
451 cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
452
Olof Johansson34c20622007-11-28 20:56:32 -0600453 write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600454
Olof Johansson021fa222007-08-22 09:13:11 -0500455 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600456 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600457 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600458
Olof Johansson72b05b92007-11-28 20:54:28 -0600459 return ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600460
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500461out_ring_desc:
462 kfree(ring->ring_info);
463out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600464 pasemi_dma_free_chan(&ring->chan);
465out_chan:
Olof Johansson72b05b92007-11-28 20:54:28 -0600466 return NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600467}
468
Olof Johansson72b05b92007-11-28 20:54:28 -0600469static void pasemi_mac_free_tx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600470{
Olof Johansson72b05b92007-11-28 20:54:28 -0600471 struct pasemi_mac_txring *txring = tx_ring(mac);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500472 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600473 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500474 dma_addr_t dmas[MAX_SKB_FRAGS+1];
Olof Johansson7e9916e2007-11-28 20:57:45 -0600475 int freed, nfrags;
Olof Johanssonad5da102007-10-02 16:27:15 -0500476 int start, limit;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600477
Olof Johansson72b05b92007-11-28 20:54:28 -0600478 start = txring->next_to_clean;
479 limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500480
481 /* Compensate for when fill has wrapped and clean has not */
482 if (start > limit)
483 limit += TX_RING_SIZE;
484
485 for (i = start; i < limit; i += freed) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600486 info = &txring->ring_info[(i+1) & (TX_RING_SIZE-1)];
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500487 if (info->dma && info->skb) {
Olof Johansson7e9916e2007-11-28 20:57:45 -0600488 nfrags = skb_shinfo(info->skb)->nr_frags;
489 for (j = 0; j <= nfrags; j++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600490 dmas[j] = txring->ring_info[(i+1+j) &
491 (TX_RING_SIZE-1)].dma;
Olof Johansson7e9916e2007-11-28 20:57:45 -0600492 freed = pasemi_mac_unmap_tx_skb(mac, nfrags,
493 info->skb, dmas);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500494 } else
495 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600496 }
497
Olof Johansson72b05b92007-11-28 20:54:28 -0600498 kfree(txring->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600499 pasemi_dma_free_chan(&txring->chan);
500
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600501}
502
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600503static void pasemi_mac_free_rx_buffers(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600504{
Olof Johansson72b05b92007-11-28 20:54:28 -0600505 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600506 unsigned int i;
507 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600508
509 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600510 info = &RX_DESC_INFO(rx, i);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500511 if (info->skb && info->dma) {
512 pci_unmap_single(mac->dma_pdev,
513 info->dma,
514 info->skb->len,
515 PCI_DMA_FROMDEVICE);
516 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600517 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500518 info->dma = 0;
519 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600520 }
521
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500522 for (i = 0; i < RX_RING_SIZE; i++)
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600523 RX_BUFF(rx, i) = 0;
524}
525
526static void pasemi_mac_free_rx_resources(struct pasemi_mac *mac)
527{
528 pasemi_mac_free_rx_buffers(mac);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500529
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600530 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
Olof Johansson72b05b92007-11-28 20:54:28 -0600531 rx_ring(mac)->buffers, rx_ring(mac)->buf_dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600532
Olof Johansson72b05b92007-11-28 20:54:28 -0600533 kfree(rx_ring(mac)->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600534 pasemi_dma_free_chan(&rx_ring(mac)->chan);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600535 mac->rx = NULL;
536}
537
Olof Johansson5c153322007-11-28 20:56:41 -0600538static void pasemi_mac_replenish_rx_ring(const struct net_device *dev,
539 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600540{
Olof Johansson5c153322007-11-28 20:56:41 -0600541 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -0600542 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500543 int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600544
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500545 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600546 return;
547
Olof Johansson72b05b92007-11-28 20:54:28 -0600548 fill = rx_ring(mac)->next_to_fill;
Olof Johansson928773c2007-09-26 16:25:06 -0500549 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600550 struct pasemi_mac_buffer *info = &RX_DESC_INFO(rx, fill);
551 u64 *buff = &RX_BUFF(rx, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600552 struct sk_buff *skb;
553 dma_addr_t dma;
554
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500555 /* Entry in use? */
556 WARN_ON(*buff);
557
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600558 skb = dev_alloc_skb(mac->bufsz);
Olof Johansson5d894942007-11-28 20:57:56 -0600559 skb_reserve(skb, LOCAL_SKB_ALIGN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600560
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500561 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600562 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600563
Olof Johansson8dc121a2007-10-02 16:26:53 -0500564 dma = pci_map_single(mac->dma_pdev, skb->data,
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600565 mac->bufsz - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600566 PCI_DMA_FROMDEVICE);
567
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500568 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600569 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600570 break;
571 }
572
573 info->skb = skb;
574 info->dma = dma;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600575 *buff = XCT_RXB_LEN(mac->bufsz) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500576 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600577 }
578
579 wmb();
580
Olof Johansson34c20622007-11-28 20:56:32 -0600581 write_dma_reg(PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600582
Olof Johansson72b05b92007-11-28 20:54:28 -0600583 rx_ring(mac)->next_to_fill = (rx_ring(mac)->next_to_fill + count) &
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500584 (RX_RING_SIZE - 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600585}
586
Olof Johansson5c153322007-11-28 20:56:41 -0600587static void pasemi_mac_restart_rx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500588{
Olof Johansson906674a2007-11-28 20:57:09 -0600589 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johansson52a94352007-05-12 18:01:09 -0500590 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500591 /* Re-enable packet count interrupts: finally
592 * ack the packet count interrupt we got in rx_intr.
593 */
594
Olof Johansson906674a2007-11-28 20:57:09 -0600595 pcnt = *rx->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500596
Olof Johansson52a94352007-05-12 18:01:09 -0500597 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500598
Olof Johansson906674a2007-11-28 20:57:09 -0600599 if (*rx->chan.status & PAS_STATUS_TIMER)
600 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
601
Olof Johansson34c20622007-11-28 20:56:32 -0600602 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(mac->rx->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500603}
604
Olof Johansson5c153322007-11-28 20:56:41 -0600605static void pasemi_mac_restart_tx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500606{
Olof Johansson52a94352007-05-12 18:01:09 -0500607 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500608
609 /* Re-enable packet count interrupts */
Olof Johansson34c20622007-11-28 20:56:32 -0600610 pcnt = *tx_ring(mac)->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500611
Olof Johansson52a94352007-05-12 18:01:09 -0500612 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500613
Olof Johansson34c20622007-11-28 20:56:32 -0600614 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(tx_ring(mac)->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500615}
616
617
Olof Johansson5c153322007-11-28 20:56:41 -0600618static inline void pasemi_mac_rx_error(const struct pasemi_mac *mac,
619 const u64 macrx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500620{
621 unsigned int rcmdsta, ccmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600622 struct pasemi_dmachan *chan = &rx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500623
624 if (!netif_msg_rx_err(mac))
625 return;
626
Olof Johansson34c20622007-11-28 20:56:32 -0600627 rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
628 ccmdsta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500629
630 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
Olof Johansson34c20622007-11-28 20:56:32 -0600631 macrx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500632
633 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
634 rcmdsta, ccmdsta);
635}
636
Olof Johansson5c153322007-11-28 20:56:41 -0600637static inline void pasemi_mac_tx_error(const struct pasemi_mac *mac,
638 const u64 mactx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500639{
640 unsigned int cmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600641 struct pasemi_dmachan *chan = &tx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500642
643 if (!netif_msg_tx_err(mac))
644 return;
645
Olof Johansson34c20622007-11-28 20:56:32 -0600646 cmdsta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500647
648 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
Olof Johansson34c20622007-11-28 20:56:32 -0600649 "tx status 0x%016lx\n", mactx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500650
651 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
652}
653
Olof Johansson5c153322007-11-28 20:56:41 -0600654static int pasemi_mac_clean_rx(struct pasemi_mac_rxring *rx,
655 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600656{
Olof Johansson5c153322007-11-28 20:56:41 -0600657 const struct pasemi_dmachan *chan = &rx->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600658 struct pasemi_mac *mac = rx->mac;
Olof Johansson5c153322007-11-28 20:56:41 -0600659 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500660 unsigned int n;
Olof Johansson5c153322007-11-28 20:56:41 -0600661 int count, buf_index, tot_bytes, packets;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500662 struct pasemi_mac_buffer *info;
663 struct sk_buff *skb;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500664 unsigned int len;
Olof Johansson5c153322007-11-28 20:56:41 -0600665 u64 macrx, eval;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500666 dma_addr_t dma;
Olof Johansson5c153322007-11-28 20:56:41 -0600667
668 tot_bytes = 0;
669 packets = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600670
Olof Johansson72b05b92007-11-28 20:54:28 -0600671 spin_lock(&rx->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600672
Olof Johansson72b05b92007-11-28 20:54:28 -0600673 n = rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600674
Olof Johansson72b05b92007-11-28 20:54:28 -0600675 prefetch(&RX_DESC(rx, n));
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500676
677 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600678 macrx = RX_DESC(rx, n);
Olof Johansson5c153322007-11-28 20:56:41 -0600679 prefetch(&RX_DESC(rx, n+4));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600680
Olof Johansson69c29d82007-10-02 16:24:51 -0500681 if ((macrx & XCT_MACRX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600682 (*chan->status & PAS_STATUS_ERROR))
Olof Johansson69c29d82007-10-02 16:24:51 -0500683 pasemi_mac_rx_error(mac, macrx);
684
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500685 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600686 break;
687
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600688 info = NULL;
689
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500690 BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600691
Olof Johansson72b05b92007-11-28 20:54:28 -0600692 eval = (RX_DESC(rx, n+1) & XCT_RXRES_8B_EVAL_M) >>
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500693 XCT_RXRES_8B_EVAL_S;
694 buf_index = eval-1;
695
Olof Johansson72b05b92007-11-28 20:54:28 -0600696 dma = (RX_DESC(rx, n+2) & XCT_PTR_ADDR_M);
697 info = &RX_DESC_INFO(rx, buf_index);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500698
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500699 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600700
Olof Johansson5c153322007-11-28 20:56:41 -0600701 prefetch_skb(skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600702
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500703 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600704
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600705 pci_unmap_single(pdev, dma, mac->bufsz - LOCAL_SKB_ALIGN,
Olof Johansson5c153322007-11-28 20:56:41 -0600706 PCI_DMA_FROMDEVICE);
Olof Johansson32bee772007-11-06 22:21:38 -0600707
708 if (macrx & XCT_MACRX_CRC) {
709 /* CRC error flagged */
710 mac->netdev->stats.rx_errors++;
711 mac->netdev->stats.rx_crc_errors++;
Olof Johansson4352d822007-12-03 21:34:14 -0600712 /* No need to free skb, it'll be reused */
Olof Johansson32bee772007-11-06 22:21:38 -0600713 goto next;
714 }
715
Olof Johansson5d894942007-11-28 20:57:56 -0600716 info->skb = NULL;
Olof Johanssonad5da102007-10-02 16:27:15 -0500717 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500718
Olof Johansson26fcfa92007-08-22 09:12:59 -0500719 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500720 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500721 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600722 XCT_MACRX_CSUM_S;
723 } else
724 skb->ip_summed = CHECKSUM_NONE;
725
Olof Johansson5c153322007-11-28 20:56:41 -0600726 packets++;
727 tot_bytes += len;
728
729 /* Don't include CRC */
730 skb_put(skb, len-4);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600731
Olof Johansson26fcfa92007-08-22 09:12:59 -0500732 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johansson28ae79f2007-11-28 20:57:27 -0600733 lro_receive_skb(&mac->lro_mgr, skb, (void *)macrx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600734
Olof Johansson32bee772007-11-06 22:21:38 -0600735next:
Olof Johansson72b05b92007-11-28 20:54:28 -0600736 RX_DESC(rx, n) = 0;
737 RX_DESC(rx, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500738
Olof Johanssonad5da102007-10-02 16:27:15 -0500739 /* Need to zero it out since hardware doesn't, since the
740 * replenish loop uses it to tell when it's done.
741 */
Olof Johansson72b05b92007-11-28 20:54:28 -0600742 RX_BUFF(rx, buf_index) = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500743
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500744 n += 4;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600745 }
746
Olof Johansson9a50beb2007-10-02 16:26:30 -0500747 if (n > RX_RING_SIZE) {
748 /* Errata 5971 workaround: L2 target of headers */
Olof Johansson34c20622007-11-28 20:56:32 -0600749 write_iob_reg(PAS_IOB_COM_PKTHDRCNT, 0);
Olof Johansson9a50beb2007-10-02 16:26:30 -0500750 n &= (RX_RING_SIZE-1);
751 }
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500752
Olof Johansson72b05b92007-11-28 20:54:28 -0600753 rx_ring(mac)->next_to_clean = n;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500754
Olof Johansson28ae79f2007-11-28 20:57:27 -0600755 lro_flush_all(&mac->lro_mgr);
756
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500757 /* Increase is in number of 16-byte entries, and since each descriptor
758 * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
759 * count*2.
760 */
Olof Johansson34c20622007-11-28 20:56:32 -0600761 write_dma_reg(PAS_DMA_RXCHAN_INCR(mac->rx->chan.chno), count << 1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500762
763 pasemi_mac_replenish_rx_ring(mac->netdev, count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600764
Olof Johansson5c153322007-11-28 20:56:41 -0600765 mac->netdev->stats.rx_bytes += tot_bytes;
766 mac->netdev->stats.rx_packets += packets;
767
Olof Johansson72b05b92007-11-28 20:54:28 -0600768 spin_unlock(&rx_ring(mac)->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600769
770 return count;
771}
772
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500773/* Can't make this too large or we blow the kernel stack limits */
774#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
775
Olof Johansson72b05b92007-11-28 20:54:28 -0600776static int pasemi_mac_clean_tx(struct pasemi_mac_txring *txring)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600777{
Olof Johansson34c20622007-11-28 20:56:32 -0600778 struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600779 struct pasemi_mac *mac = txring->mac;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500780 int i, j;
Olof Johanssonad5da102007-10-02 16:27:15 -0500781 unsigned int start, descr_count, buf_count, batch_limit;
782 unsigned int ring_limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500783 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500784 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500785 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
786 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johansson7e9916e2007-11-28 20:57:45 -0600787 int nf[TX_CLEAN_BATCHSIZE];
788 int nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600789
Olof Johansson02df6cf2007-08-22 09:13:03 -0500790 total_count = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500791 batch_limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500792restart:
Olof Johansson72b05b92007-11-28 20:54:28 -0600793 spin_lock_irqsave(&txring->lock, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600794
Olof Johansson72b05b92007-11-28 20:54:28 -0600795 start = txring->next_to_clean;
796 ring_limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500797
Olof Johansson7e9916e2007-11-28 20:57:45 -0600798 prefetch(&TX_DESC_INFO(txring, start+1).skb);
799
Olof Johanssonad5da102007-10-02 16:27:15 -0500800 /* Compensate for when fill has wrapped but clean has not */
801 if (start > ring_limit)
802 ring_limit += TX_RING_SIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500803
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500804 buf_count = 0;
805 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600806
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500807 for (i = start;
Olof Johanssonad5da102007-10-02 16:27:15 -0500808 descr_count < batch_limit && i < ring_limit;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500809 i += buf_count) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600810 u64 mactx = TX_DESC(txring, i);
Olof Johanssonad5da102007-10-02 16:27:15 -0500811 struct sk_buff *skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500812
Olof Johansson7e9916e2007-11-28 20:57:45 -0600813 skb = TX_DESC_INFO(txring, i+1).skb;
814 nr_frags = TX_DESC_INFO(txring, i).dma;
815
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500816 if ((mactx & XCT_MACTX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600817 (*chan->status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500818 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500819
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500820 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500821 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600822 break;
823
Olof Johansson7e9916e2007-11-28 20:57:45 -0600824 buf_count = 2 + nr_frags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500825 /* Since we always fill with an even number of entries, make
826 * sure we skip any unused one at the end as well.
827 */
828 if (buf_count & 1)
829 buf_count++;
Olof Johansson7e9916e2007-11-28 20:57:45 -0600830
831 for (j = 0; j <= nr_frags; j++)
832 dmas[descr_count][j] = TX_DESC_INFO(txring, i+1+j).dma;
833
834 skbs[descr_count] = skb;
835 nf[descr_count] = nr_frags;
836
837 TX_DESC(txring, i) = 0;
838 TX_DESC(txring, i+1) = 0;
839
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500840 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600841 }
Olof Johansson72b05b92007-11-28 20:54:28 -0600842 txring->next_to_clean = i & (TX_RING_SIZE-1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500843
Olof Johansson72b05b92007-11-28 20:54:28 -0600844 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500845 netif_wake_queue(mac->netdev);
846
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500847 for (i = 0; i < descr_count; i++)
Olof Johansson7e9916e2007-11-28 20:57:45 -0600848 pasemi_mac_unmap_tx_skb(mac, nf[i], skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500849
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500850 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500851
852 /* If the batch was full, try to clean more */
Olof Johanssonad5da102007-10-02 16:27:15 -0500853 if (descr_count == batch_limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500854 goto restart;
855
856 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600857}
858
859
860static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
861{
Olof Johansson5c153322007-11-28 20:56:41 -0600862 const struct pasemi_mac_rxring *rxring = data;
Olof Johansson34c20622007-11-28 20:56:32 -0600863 struct pasemi_mac *mac = rxring->mac;
864 struct net_device *dev = mac->netdev;
Olof Johansson5c153322007-11-28 20:56:41 -0600865 const struct pasemi_dmachan *chan = &rxring->chan;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600866 unsigned int reg;
867
Olof Johansson34c20622007-11-28 20:56:32 -0600868 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600869 return IRQ_NONE;
870
Olof Johansson6dfa7522007-05-08 00:47:32 -0500871 /* Don't reset packet count so it won't fire again but clear
872 * all others.
873 */
874
Olof Johansson6dfa7522007-05-08 00:47:32 -0500875 reg = 0;
Olof Johansson34c20622007-11-28 20:56:32 -0600876 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500877 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600878 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500879 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600880
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700881 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500882
Olof Johansson34c20622007-11-28 20:56:32 -0600883 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600884
885 return IRQ_HANDLED;
886}
887
Olof Johansson61cec3b2007-11-28 20:56:54 -0600888#define TX_CLEAN_INTERVAL HZ
889
890static void pasemi_mac_tx_timer(unsigned long data)
891{
892 struct pasemi_mac_txring *txring = (struct pasemi_mac_txring *)data;
893 struct pasemi_mac *mac = txring->mac;
894
895 pasemi_mac_clean_tx(txring);
896
897 mod_timer(&txring->clean_timer, jiffies + TX_CLEAN_INTERVAL);
898
899 pasemi_mac_restart_tx_intr(mac);
900}
901
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600902static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
903{
Olof Johansson72b05b92007-11-28 20:54:28 -0600904 struct pasemi_mac_txring *txring = data;
Olof Johansson5c153322007-11-28 20:56:41 -0600905 const struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson61cec3b2007-11-28 20:56:54 -0600906 struct pasemi_mac *mac = txring->mac;
907 unsigned int reg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600908
Olof Johansson34c20622007-11-28 20:56:32 -0600909 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600910 return IRQ_NONE;
911
Olof Johansson61cec3b2007-11-28 20:56:54 -0600912 reg = 0;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500913
Olof Johansson34c20622007-11-28 20:56:32 -0600914 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500915 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600916 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500917 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600918
Olof Johansson61cec3b2007-11-28 20:56:54 -0600919 mod_timer(&txring->clean_timer, jiffies + (TX_CLEAN_INTERVAL)*2);
920
921 netif_rx_schedule(mac->netdev, &mac->napi);
922
923 if (reg)
924 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600925
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600926 return IRQ_HANDLED;
927}
928
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500929static void pasemi_adjust_link(struct net_device *dev)
930{
931 struct pasemi_mac *mac = netdev_priv(dev);
932 int msg;
933 unsigned int flags;
934 unsigned int new_flags;
935
936 if (!mac->phydev->link) {
937 /* If no link, MAC speed settings don't matter. Just report
938 * link down and return.
939 */
940 if (mac->link && netif_msg_link(mac))
941 printk(KERN_INFO "%s: Link is down.\n", dev->name);
942
943 netif_carrier_off(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -0600944 pasemi_mac_intf_disable(mac);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500945 mac->link = 0;
946
947 return;
Olof Johanssonb0cd2f92007-11-28 20:58:25 -0600948 } else {
949 pasemi_mac_intf_enable(mac);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500950 netif_carrier_on(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -0600951 }
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500952
Olof Johanssona85b9422007-09-15 13:40:59 -0700953 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500954 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
955 PAS_MAC_CFG_PCFG_TSR_M);
956
957 if (!mac->phydev->duplex)
958 new_flags |= PAS_MAC_CFG_PCFG_HD;
959
960 switch (mac->phydev->speed) {
961 case 1000:
962 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
963 PAS_MAC_CFG_PCFG_TSR_1G;
964 break;
965 case 100:
966 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
967 PAS_MAC_CFG_PCFG_TSR_100M;
968 break;
969 case 10:
970 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
971 PAS_MAC_CFG_PCFG_TSR_10M;
972 break;
973 default:
974 printk("Unsupported speed %d\n", mac->phydev->speed);
975 }
976
977 /* Print on link or speed/duplex change */
978 msg = mac->link != mac->phydev->link || flags != new_flags;
979
980 mac->duplex = mac->phydev->duplex;
981 mac->speed = mac->phydev->speed;
982 mac->link = mac->phydev->link;
983
984 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700985 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500986
987 if (msg && netif_msg_link(mac))
988 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
989 dev->name, mac->speed, mac->duplex ? "full" : "half");
990}
991
992static int pasemi_mac_phy_init(struct net_device *dev)
993{
994 struct pasemi_mac *mac = netdev_priv(dev);
995 struct device_node *dn, *phy_dn;
996 struct phy_device *phydev;
997 unsigned int phy_id;
998 const phandle *ph;
999 const unsigned int *prop;
1000 struct resource r;
1001 int ret;
1002
1003 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -07001004 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001005 if (!ph)
1006 return -ENODEV;
1007 phy_dn = of_find_node_by_phandle(*ph);
1008
Linus Torvalds90287802007-05-08 11:57:17 -07001009 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001010 ret = of_address_to_resource(phy_dn->parent, 0, &r);
1011 if (ret)
1012 goto err;
1013
1014 phy_id = *prop;
1015 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
1016
1017 of_node_put(phy_dn);
1018
1019 mac->link = 0;
1020 mac->speed = 0;
1021 mac->duplex = -1;
1022
1023 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
1024
1025 if (IS_ERR(phydev)) {
1026 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
1027 return PTR_ERR(phydev);
1028 }
1029
1030 mac->phydev = phydev;
1031
1032 return 0;
1033
1034err:
1035 of_node_put(phy_dn);
1036 return -ENODEV;
1037}
1038
1039
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001040static int pasemi_mac_open(struct net_device *dev)
1041{
1042 struct pasemi_mac *mac = netdev_priv(dev);
1043 unsigned int flags;
1044 int ret;
1045
1046 /* enable rx section */
Olof Johansson34c20622007-11-28 20:56:32 -06001047 write_dma_reg(PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001048
1049 /* enable tx section */
Olof Johansson34c20622007-11-28 20:56:32 -06001050 write_dma_reg(PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001051
1052 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
1053 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
1054 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
1055
Olof Johanssona85b9422007-09-15 13:40:59 -07001056 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001057
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001058 ret = pasemi_mac_setup_rx_resources(dev);
1059 if (ret)
1060 goto out_rx_resources;
1061
Olof Johansson34c20622007-11-28 20:56:32 -06001062 mac->tx = pasemi_mac_setup_tx_resources(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001063
1064 if (!mac->tx)
1065 goto out_tx_ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001066
Olof Johansson906674a2007-11-28 20:57:09 -06001067 /* 0x3ff with 33MHz clock is about 31us */
1068 write_iob_reg(PAS_IOB_DMA_COM_TIMEOUTCFG,
1069 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0x3ff));
1070
Olof Johansson34c20622007-11-28 20:56:32 -06001071 write_iob_reg(PAS_IOB_DMA_RXCH_CFG(mac->rx->chan.chno),
Olof Johansson28ae79f2007-11-28 20:57:27 -06001072 PAS_IOB_DMA_RXCH_CFG_CNTTH(256));
Olof Johansson34c20622007-11-28 20:56:32 -06001073
1074 write_iob_reg(PAS_IOB_DMA_TXCH_CFG(mac->tx->chan.chno),
Olof Johansson61cec3b2007-11-28 20:56:54 -06001075 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johansson34c20622007-11-28 20:56:32 -06001076
Olof Johanssona85b9422007-09-15 13:40:59 -07001077 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
Olof Johansson34c20622007-11-28 20:56:32 -06001078 PAS_MAC_IPC_CHNL_DCHNO(mac->rx->chan.chno) |
1079 PAS_MAC_IPC_CHNL_BCH(mac->rx->chan.chno));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001080
1081 /* enable rx if */
Olof Johansson34c20622007-11-28 20:56:32 -06001082 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1083 PAS_DMA_RXINT_RCMDSTA_EN |
1084 PAS_DMA_RXINT_RCMDSTA_DROPS_M |
1085 PAS_DMA_RXINT_RCMDSTA_BP |
1086 PAS_DMA_RXINT_RCMDSTA_OO |
1087 PAS_DMA_RXINT_RCMDSTA_BT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001088
1089 /* enable rx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001090 pasemi_dma_start_chan(&rx_ring(mac)->chan, PAS_DMA_RXCHAN_CCMDSTA_DU |
1091 PAS_DMA_RXCHAN_CCMDSTA_OD |
1092 PAS_DMA_RXCHAN_CCMDSTA_FD |
1093 PAS_DMA_RXCHAN_CCMDSTA_DT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001094
1095 /* enable tx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001096 pasemi_dma_start_chan(&tx_ring(mac)->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
1097 PAS_DMA_TXCHAN_TCMDSTA_DB |
1098 PAS_DMA_TXCHAN_TCMDSTA_DE |
1099 PAS_DMA_TXCHAN_TCMDSTA_DA);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001100
Olof Johansson928773c2007-09-26 16:25:06 -05001101 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001102
Olof Johansson34c20622007-11-28 20:56:32 -06001103 write_dma_reg(PAS_DMA_RXCHAN_INCR(rx_ring(mac)->chan.chno),
1104 RX_RING_SIZE>>1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -05001105
Olof Johansson72b05b92007-11-28 20:54:28 -06001106 /* Clear out any residual packet count state from firmware */
1107 pasemi_mac_restart_rx_intr(mac);
1108 pasemi_mac_restart_tx_intr(mac);
1109
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001110 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
Olof Johansson36033762007-09-26 16:24:42 -05001111
1112 if (mac->type == MAC_TYPE_GMAC)
1113 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
1114 else
1115 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
1116
1117 /* Enable interface in MAC */
1118 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
1119
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001120 ret = pasemi_mac_phy_init(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001121 if (ret) {
1122 /* Since we won't get link notification, just enable RX */
1123 pasemi_mac_intf_enable(mac);
1124 if (mac->type == MAC_TYPE_GMAC) {
1125 /* Warn for missing PHY on SGMII (1Gig) ports */
1126 dev_warn(&mac->pdev->dev,
1127 "PHY init failed: %d.\n", ret);
1128 dev_warn(&mac->pdev->dev,
1129 "Defaulting to 1Gbit full duplex\n");
1130 }
Olof Johansson8304b632007-11-28 20:58:06 -06001131 }
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001132
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001133 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001134 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001135
Olof Johansson72b05b92007-11-28 20:54:28 -06001136 snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx",
1137 dev->name);
Olof Johansson771f7402007-05-08 00:47:21 -05001138
Olof Johansson34c20622007-11-28 20:56:32 -06001139 ret = request_irq(mac->tx->chan.irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johansson72b05b92007-11-28 20:54:28 -06001140 mac->tx_irq_name, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001141 if (ret) {
1142 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001143 mac->tx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001144 goto out_tx_int;
1145 }
1146
Olof Johansson72b05b92007-11-28 20:54:28 -06001147 snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx",
1148 dev->name);
1149
Olof Johansson34c20622007-11-28 20:56:32 -06001150 ret = request_irq(mac->rx->chan.irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
1151 mac->rx_irq_name, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001152 if (ret) {
1153 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001154 mac->rx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001155 goto out_rx_int;
1156 }
1157
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001158 if (mac->phydev)
1159 phy_start(mac->phydev);
1160
Olof Johansson61cec3b2007-11-28 20:56:54 -06001161 init_timer(&mac->tx->clean_timer);
1162 mac->tx->clean_timer.function = pasemi_mac_tx_timer;
1163 mac->tx->clean_timer.data = (unsigned long)mac->tx;
1164 mac->tx->clean_timer.expires = jiffies+HZ;
1165 add_timer(&mac->tx->clean_timer);
1166
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001167 return 0;
1168
1169out_rx_int:
Olof Johansson34c20622007-11-28 20:56:32 -06001170 free_irq(mac->tx->chan.irq, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001171out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001172 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001173 netif_stop_queue(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001174out_tx_ring:
1175 if (mac->tx)
1176 pasemi_mac_free_tx_resources(mac);
1177 pasemi_mac_free_rx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001178out_rx_resources:
1179
1180 return ret;
1181}
1182
1183#define MAX_RETRIES 5000
1184
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001185static void pasemi_mac_pause_txchan(struct pasemi_mac *mac)
1186{
1187 unsigned int sta, retries;
1188 int txch = tx_ring(mac)->chan.chno;
1189
1190 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch),
1191 PAS_DMA_TXCHAN_TCMDSTA_ST);
1192
1193 for (retries = 0; retries < MAX_RETRIES; retries++) {
1194 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
1195 if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
1196 break;
1197 cond_resched();
1198 }
1199
1200 if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
1201 dev_err(&mac->dma_pdev->dev,
1202 "Failed to stop tx channel, tcmdsta %08x\n", sta);
1203
1204 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch), 0);
1205}
1206
1207static void pasemi_mac_pause_rxchan(struct pasemi_mac *mac)
1208{
1209 unsigned int sta, retries;
1210 int rxch = rx_ring(mac)->chan.chno;
1211
1212 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch),
1213 PAS_DMA_RXCHAN_CCMDSTA_ST);
1214 for (retries = 0; retries < MAX_RETRIES; retries++) {
1215 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
1216 if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
1217 break;
1218 cond_resched();
1219 }
1220
1221 if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
1222 dev_err(&mac->dma_pdev->dev,
1223 "Failed to stop rx channel, ccmdsta 08%x\n", sta);
1224 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch), 0);
1225}
1226
1227static void pasemi_mac_pause_rxint(struct pasemi_mac *mac)
1228{
1229 unsigned int sta, retries;
1230
1231 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1232 PAS_DMA_RXINT_RCMDSTA_ST);
1233 for (retries = 0; retries < MAX_RETRIES; retries++) {
1234 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1235 if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
1236 break;
1237 cond_resched();
1238 }
1239
1240 if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
1241 dev_err(&mac->dma_pdev->dev,
1242 "Failed to stop rx interface, rcmdsta %08x\n", sta);
1243 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
1244}
1245
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001246static int pasemi_mac_close(struct net_device *dev)
1247{
1248 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson9e81d332007-10-02 16:27:39 -05001249 unsigned int sta;
Olof Johansson34c20622007-11-28 20:56:32 -06001250 int rxch, txch;
1251
1252 rxch = rx_ring(mac)->chan.chno;
1253 txch = tx_ring(mac)->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001254
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001255 if (mac->phydev) {
1256 phy_stop(mac->phydev);
1257 phy_disconnect(mac->phydev);
1258 }
1259
Olof Johansson61cec3b2007-11-28 20:56:54 -06001260 del_timer_sync(&mac->tx->clean_timer);
1261
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001262 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001263 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001264
Olof Johansson34c20622007-11-28 20:56:32 -06001265 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson9e81d332007-10-02 16:27:39 -05001266 if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
1267 PAS_DMA_RXINT_RCMDSTA_OO |
1268 PAS_DMA_RXINT_RCMDSTA_BT))
1269 printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);
1270
Olof Johansson34c20622007-11-28 20:56:32 -06001271 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001272 if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
1273 PAS_DMA_RXCHAN_CCMDSTA_OD |
1274 PAS_DMA_RXCHAN_CCMDSTA_FD |
1275 PAS_DMA_RXCHAN_CCMDSTA_DT))
1276 printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);
1277
Olof Johansson34c20622007-11-28 20:56:32 -06001278 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
Olof Johansson72b05b92007-11-28 20:54:28 -06001279 if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB |
1280 PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA))
Olof Johansson9e81d332007-10-02 16:27:39 -05001281 printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);
1282
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001283 /* Clean out any pending buffers */
Olof Johansson72b05b92007-11-28 20:54:28 -06001284 pasemi_mac_clean_tx(tx_ring(mac));
1285 pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001286
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001287 pasemi_mac_pause_txchan(mac);
1288 pasemi_mac_pause_rxint(mac);
1289 pasemi_mac_pause_rxchan(mac);
Olof Johansson1145d952008-01-23 13:57:19 -06001290 pasemi_mac_intf_disable(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001291
Olof Johansson34c20622007-11-28 20:56:32 -06001292 free_irq(mac->tx->chan.irq, mac->tx);
1293 free_irq(mac->rx->chan.irq, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001294
1295 /* Free resources */
Olof Johansson72b05b92007-11-28 20:54:28 -06001296 pasemi_mac_free_rx_resources(mac);
1297 pasemi_mac_free_tx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001298
1299 return 0;
1300}
1301
1302static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1303{
1304 struct pasemi_mac *mac = netdev_priv(dev);
1305 struct pasemi_mac_txring *txring;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001306 u64 dflags, mactx;
1307 dma_addr_t map[MAX_SKB_FRAGS+1];
1308 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001309 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001310 int i, nfrags;
Olof Johansson5c153322007-11-28 20:56:41 -06001311 int fill;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001312
Olof Johanssondbd62af2007-11-06 22:20:39 -06001313 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001314
1315 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001316 const unsigned char *nh = skb_network_header(skb);
1317
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001318 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001319 case IPPROTO_TCP:
1320 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001321 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001322 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001323 break;
1324 case IPPROTO_UDP:
1325 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001326 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001327 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001328 break;
1329 }
1330 }
1331
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001332 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001333
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001334 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1335 PCI_DMA_TODEVICE);
1336 map_size[0] = skb_headlen(skb);
1337 if (dma_mapping_error(map[0]))
1338 goto out_err_nolock;
1339
1340 for (i = 0; i < nfrags; i++) {
1341 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1342
1343 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1344 frag->page_offset, frag->size,
1345 PCI_DMA_TODEVICE);
1346 map_size[i+1] = frag->size;
1347 if (dma_mapping_error(map[i+1])) {
1348 nfrags = i;
1349 goto out_err_nolock;
1350 }
1351 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001352
Olof Johansson26fcfa92007-08-22 09:12:59 -05001353 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001354
Olof Johansson72b05b92007-11-28 20:54:28 -06001355 txring = tx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001356
1357 spin_lock_irqsave(&txring->lock, flags);
1358
Olof Johansson5c153322007-11-28 20:56:41 -06001359 fill = txring->next_to_fill;
1360
Olof Johanssonad5da102007-10-02 16:27:15 -05001361 /* Avoid stepping on the same cache line that the DMA controller
1362 * is currently about to send, so leave at least 8 words available.
1363 * Total free space needed is mactx + fragments + 8
1364 */
1365 if (RING_AVAIL(txring) < nfrags + 10) {
1366 /* no room -- stop the queue and wait for tx intr */
1367 netif_stop_queue(dev);
1368 goto out_err;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001369 }
1370
Olof Johansson5c153322007-11-28 20:56:41 -06001371 TX_DESC(txring, fill) = mactx;
Olof Johansson7e9916e2007-11-28 20:57:45 -06001372 TX_DESC_INFO(txring, fill).dma = nfrags;
Olof Johansson5c153322007-11-28 20:56:41 -06001373 fill++;
1374 TX_DESC_INFO(txring, fill).skb = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001375 for (i = 0; i <= nfrags; i++) {
Olof Johansson5c153322007-11-28 20:56:41 -06001376 TX_DESC(txring, fill+i) =
Olof Johansson72b05b92007-11-28 20:54:28 -06001377 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
Olof Johansson5c153322007-11-28 20:56:41 -06001378 TX_DESC_INFO(txring, fill+i).dma = map[i];
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001379 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001380
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001381 /* We have to add an even number of 8-byte entries to the ring
1382 * even if the last one is unused. That means always an odd number
1383 * of pointers + one mactx descriptor.
1384 */
1385 if (nfrags & 1)
1386 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001387
Olof Johansson5c153322007-11-28 20:56:41 -06001388 txring->next_to_fill = (fill + nfrags + 1) & (TX_RING_SIZE-1);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001389
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001390 dev->stats.tx_packets++;
1391 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001392
1393 spin_unlock_irqrestore(&txring->lock, flags);
1394
Olof Johansson34c20622007-11-28 20:56:32 -06001395 write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001396
1397 return NETDEV_TX_OK;
1398
1399out_err:
1400 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001401out_err_nolock:
1402 while (nfrags--)
1403 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1404 PCI_DMA_TODEVICE);
1405
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001406 return NETDEV_TX_BUSY;
1407}
1408
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001409static void pasemi_mac_set_rx_mode(struct net_device *dev)
1410{
Olof Johansson5c153322007-11-28 20:56:41 -06001411 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001412 unsigned int flags;
1413
Olof Johanssona85b9422007-09-15 13:40:59 -07001414 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001415
1416 /* Set promiscuous */
1417 if (dev->flags & IFF_PROMISC)
1418 flags |= PAS_MAC_CFG_PCFG_PR;
1419 else
1420 flags &= ~PAS_MAC_CFG_PCFG_PR;
1421
Olof Johanssona85b9422007-09-15 13:40:59 -07001422 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001423}
1424
1425
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001426static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001427{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001428 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1429 struct net_device *dev = mac->netdev;
1430 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001431
Olof Johansson72b05b92007-11-28 20:54:28 -06001432 pasemi_mac_clean_tx(tx_ring(mac));
1433 pkts = pasemi_mac_clean_rx(rx_ring(mac), budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001434 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001435 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001436 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001437
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001438 pasemi_mac_restart_rx_intr(mac);
Olof Johansson61cec3b2007-11-28 20:56:54 -06001439 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001440 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001441 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001442}
1443
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001444static int pasemi_mac_change_mtu(struct net_device *dev, int new_mtu)
1445{
1446 struct pasemi_mac *mac = netdev_priv(dev);
1447 unsigned int reg;
1448 unsigned int rcmdsta;
1449 int running;
1450
1451 if (new_mtu < PE_MIN_MTU || new_mtu > PE_MAX_MTU)
1452 return -EINVAL;
1453
1454 running = netif_running(dev);
1455
1456 if (running) {
1457 /* Need to stop the interface, clean out all already
1458 * received buffers, free all unused buffers on the RX
1459 * interface ring, then finally re-fill the rx ring with
1460 * the new-size buffers and restart.
1461 */
1462
1463 napi_disable(&mac->napi);
1464 netif_tx_disable(dev);
1465 pasemi_mac_intf_disable(mac);
1466
1467 rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1468 pasemi_mac_pause_rxint(mac);
1469 pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
1470 pasemi_mac_free_rx_buffers(mac);
1471 }
1472
1473 /* Change maxf, i.e. what size frames are accepted.
1474 * Need room for ethernet header and CRC word
1475 */
1476 reg = read_mac_reg(mac, PAS_MAC_CFG_MACCFG);
1477 reg &= ~PAS_MAC_CFG_MACCFG_MAXF_M;
1478 reg |= PAS_MAC_CFG_MACCFG_MAXF(new_mtu + ETH_HLEN + 4);
1479 write_mac_reg(mac, PAS_MAC_CFG_MACCFG, reg);
1480
1481 dev->mtu = new_mtu;
1482 /* MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
1483 mac->bufsz = new_mtu + ETH_HLEN + ETH_FCS_LEN + LOCAL_SKB_ALIGN + 128;
1484
1485 if (running) {
1486 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1487 rcmdsta | PAS_DMA_RXINT_RCMDSTA_EN);
1488
1489 rx_ring(mac)->next_to_fill = 0;
1490 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE-1);
1491
1492 napi_enable(&mac->napi);
1493 netif_start_queue(dev);
1494 pasemi_mac_intf_enable(mac);
1495 }
1496
1497 return 0;
1498}
1499
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001500static int __devinit
1501pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1502{
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001503 struct net_device *dev;
1504 struct pasemi_mac *mac;
1505 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001506 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001507
1508 err = pci_enable_device(pdev);
1509 if (err)
1510 return err;
1511
1512 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1513 if (dev == NULL) {
1514 dev_err(&pdev->dev,
1515 "pasemi_mac: Could not allocate ethernet device.\n");
1516 err = -ENOMEM;
1517 goto out_disable_device;
1518 }
1519
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001520 pci_set_drvdata(pdev, dev);
1521 SET_NETDEV_DEV(dev, &pdev->dev);
1522
1523 mac = netdev_priv(dev);
1524
1525 mac->pdev = pdev;
1526 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001527
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001528 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1529
Olof Johansson5c153322007-11-28 20:56:41 -06001530 dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG |
1531 NETIF_F_HIGHDMA;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001532
Olof Johansson28ae79f2007-11-28 20:57:27 -06001533 mac->lro_mgr.max_aggr = LRO_MAX_AGGR;
1534 mac->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
1535 mac->lro_mgr.lro_arr = mac->lro_desc;
1536 mac->lro_mgr.get_skb_header = get_skb_hdr;
1537 mac->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1538 mac->lro_mgr.dev = mac->netdev;
1539 mac->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1540 mac->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1541
1542
Olof Johansson34c20622007-11-28 20:56:32 -06001543 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1544 if (!mac->dma_pdev) {
1545 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1546 err = -ENODEV;
1547 goto out;
1548 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001549
Olof Johansson34c20622007-11-28 20:56:32 -06001550 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1551 if (!mac->iob_pdev) {
1552 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1553 err = -ENODEV;
1554 goto out;
1555 }
1556
1557 /* get mac addr from device tree */
1558 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1559 err = -ENODEV;
1560 goto out;
1561 }
1562 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1563
1564 mac->dma_if = mac_to_intf(mac);
1565 if (mac->dma_if < 0) {
1566 dev_err(&mac->pdev->dev, "Can't map DMA interface\n");
1567 err = -ENODEV;
1568 goto out;
1569 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001570
1571 switch (pdev->device) {
1572 case 0xa005:
1573 mac->type = MAC_TYPE_GMAC;
1574 break;
1575 case 0xa006:
1576 mac->type = MAC_TYPE_XAUI;
1577 break;
1578 default:
1579 err = -ENODEV;
1580 goto out;
1581 }
1582
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001583 dev->open = pasemi_mac_open;
1584 dev->stop = pasemi_mac_close;
1585 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001586 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johansson5cea73b2008-01-23 13:56:19 -06001587 dev->set_mac_address = pasemi_mac_set_mac_addr;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001588 dev->mtu = PE_DEF_MTU;
1589 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
1590 mac->bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + LOCAL_SKB_ALIGN + 128;
1591
1592 dev->change_mtu = pasemi_mac_change_mtu;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001593
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001594 if (err)
1595 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001596
Olof Johanssonceb51362007-05-08 00:47:49 -05001597 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1598
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001599 /* Enable most messages by default */
1600 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1601
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001602 err = register_netdev(dev);
1603
1604 if (err) {
1605 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1606 err);
1607 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001608 } else if netif_msg_probe(mac)
Olof Johansson72b05b92007-11-28 20:54:28 -06001609 printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001610 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
Olof Johansson72b05b92007-11-28 20:54:28 -06001611 mac->dma_if, print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001612
1613 return err;
1614
1615out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001616 if (mac->iob_pdev)
1617 pci_dev_put(mac->iob_pdev);
1618 if (mac->dma_pdev)
1619 pci_dev_put(mac->dma_pdev);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001620
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001621 free_netdev(dev);
1622out_disable_device:
1623 pci_disable_device(pdev);
1624 return err;
1625
1626}
1627
1628static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1629{
1630 struct net_device *netdev = pci_get_drvdata(pdev);
1631 struct pasemi_mac *mac;
1632
1633 if (!netdev)
1634 return;
1635
1636 mac = netdev_priv(netdev);
1637
1638 unregister_netdev(netdev);
1639
1640 pci_disable_device(pdev);
1641 pci_dev_put(mac->dma_pdev);
1642 pci_dev_put(mac->iob_pdev);
1643
Olof Johansson34c20622007-11-28 20:56:32 -06001644 pasemi_dma_free_chan(&mac->tx->chan);
1645 pasemi_dma_free_chan(&mac->rx->chan);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001646
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001647 pci_set_drvdata(pdev, NULL);
1648 free_netdev(netdev);
1649}
1650
1651static struct pci_device_id pasemi_mac_pci_tbl[] = {
1652 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1653 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001654 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001655};
1656
1657MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1658
1659static struct pci_driver pasemi_mac_driver = {
1660 .name = "pasemi_mac",
1661 .id_table = pasemi_mac_pci_tbl,
1662 .probe = pasemi_mac_probe,
1663 .remove = __devexit_p(pasemi_mac_remove),
1664};
1665
1666static void __exit pasemi_mac_cleanup_module(void)
1667{
1668 pci_unregister_driver(&pasemi_mac_driver);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001669}
1670
1671int pasemi_mac_init_module(void)
1672{
Olof Johansson34c20622007-11-28 20:56:32 -06001673 int err;
1674
1675 err = pasemi_dma_init();
1676 if (err)
1677 return err;
1678
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001679 return pci_register_driver(&pasemi_mac_driver);
1680}
1681
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001682module_init(pasemi_mac_init_module);
1683module_exit(pasemi_mac_cleanup_module);