blob: c50f0f4de6d843319c401d6e3104cbd843755a0a [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
Olof Johansson28ae79f2007-11-28 20:57:27 -060058#define LRO_MAX_AGGR 64
59
Olof Johanssonef1ea0b2008-01-23 13:56:47 -060060#define PE_MIN_MTU 64
Olof Johansson8d636d82008-03-05 16:34:16 -060061#define PE_MAX_MTU 9000
Olof Johanssonef1ea0b2008-01-23 13:56:47 -060062#define PE_DEF_MTU ETH_DATA_LEN
63
Olof Johanssonceb51362007-05-08 00:47:49 -050064#define DEFAULT_MSG_ENABLE \
65 (NETIF_MSG_DRV | \
66 NETIF_MSG_PROBE | \
67 NETIF_MSG_LINK | \
68 NETIF_MSG_TIMER | \
69 NETIF_MSG_IFDOWN | \
70 NETIF_MSG_IFUP | \
71 NETIF_MSG_RX_ERR | \
72 NETIF_MSG_TX_ERR)
73
Olof Johanssonceb51362007-05-08 00:47:49 -050074MODULE_LICENSE("GPL");
75MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
76MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
77
78static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
79module_param(debug, int, 0);
80MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060081
Olof Johanssone37c7722008-02-20 20:57:59 -060082extern const struct ethtool_ops pasemi_mac_ethtool_ops;
83
Olof Johanssonaf289e82007-10-03 13:03:54 -050084static int translation_enabled(void)
85{
86#if defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
87 return 1;
88#else
89 return firmware_has_feature(FW_FEATURE_LPAR);
90#endif
91}
92
Olof Johansson34c20622007-11-28 20:56:32 -060093static void write_iob_reg(unsigned int reg, unsigned int val)
Olof Johanssona85b9422007-09-15 13:40:59 -070094{
Olof Johansson34c20622007-11-28 20:56:32 -060095 pasemi_write_iob_reg(reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -070096}
97
Olof Johansson5c153322007-11-28 20:56:41 -060098static unsigned int read_mac_reg(const struct pasemi_mac *mac, unsigned int reg)
Olof Johanssona85b9422007-09-15 13:40:59 -070099{
Olof Johansson34c20622007-11-28 20:56:32 -0600100 return pasemi_read_mac_reg(mac->dma_if, reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700101}
102
Olof Johansson5c153322007-11-28 20:56:41 -0600103static void write_mac_reg(const struct pasemi_mac *mac, unsigned int reg,
Olof Johanssona85b9422007-09-15 13:40:59 -0700104 unsigned int val)
105{
Olof Johansson34c20622007-11-28 20:56:32 -0600106 pasemi_write_mac_reg(mac->dma_if, reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700107}
108
Olof Johansson34c20622007-11-28 20:56:32 -0600109static unsigned int read_dma_reg(unsigned int reg)
Olof Johanssona85b9422007-09-15 13:40:59 -0700110{
Olof Johansson34c20622007-11-28 20:56:32 -0600111 return pasemi_read_dma_reg(reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700112}
113
Olof Johansson34c20622007-11-28 20:56:32 -0600114static void write_dma_reg(unsigned int reg, unsigned int val)
Olof Johanssona85b9422007-09-15 13:40:59 -0700115{
Olof Johansson34c20622007-11-28 20:56:32 -0600116 pasemi_write_dma_reg(reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700117}
118
Olof Johansson5c153322007-11-28 20:56:41 -0600119static struct pasemi_mac_rxring *rx_ring(const struct pasemi_mac *mac)
Olof Johansson72b05b92007-11-28 20:54:28 -0600120{
121 return mac->rx;
122}
123
Olof Johansson5c153322007-11-28 20:56:41 -0600124static struct pasemi_mac_txring *tx_ring(const struct pasemi_mac *mac)
Olof Johansson72b05b92007-11-28 20:54:28 -0600125{
126 return mac->tx;
127}
128
Olof Johansson5c153322007-11-28 20:56:41 -0600129static inline void prefetch_skb(const struct sk_buff *skb)
130{
131 const void *d = skb;
132
133 prefetch(d);
134 prefetch(d+64);
135 prefetch(d+128);
136 prefetch(d+192);
137}
138
Olof Johansson34c20622007-11-28 20:56:32 -0600139static int mac_to_intf(struct pasemi_mac *mac)
140{
141 struct pci_dev *pdev = mac->pdev;
142 u32 tmp;
143 int nintf, off, i, j;
144 int devfn = pdev->devfn;
145
146 tmp = read_dma_reg(PAS_DMA_CAP_IFI);
147 nintf = (tmp & PAS_DMA_CAP_IFI_NIN_M) >> PAS_DMA_CAP_IFI_NIN_S;
148 off = (tmp & PAS_DMA_CAP_IFI_IOFF_M) >> PAS_DMA_CAP_IFI_IOFF_S;
149
150 /* IOFF contains the offset to the registers containing the
151 * DMA interface-to-MAC-pci-id mappings, and NIN contains number
152 * of total interfaces. Each register contains 4 devfns.
153 * Just do a linear search until we find the devfn of the MAC
154 * we're trying to look up.
155 */
156
157 for (i = 0; i < (nintf+3)/4; i++) {
158 tmp = read_dma_reg(off+4*i);
159 for (j = 0; j < 4; j++) {
160 if (((tmp >> (8*j)) & 0xff) == devfn)
161 return i*4 + j;
162 }
163 }
164 return -1;
165}
166
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600167static void pasemi_mac_intf_disable(struct pasemi_mac *mac)
168{
169 unsigned int flags;
170
171 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
172 flags &= ~PAS_MAC_CFG_PCFG_PE;
173 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
174}
175
176static void pasemi_mac_intf_enable(struct pasemi_mac *mac)
177{
178 unsigned int flags;
179
180 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
181 flags |= PAS_MAC_CFG_PCFG_PE;
182 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
183}
184
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600185static int pasemi_get_mac_addr(struct pasemi_mac *mac)
186{
187 struct pci_dev *pdev = mac->pdev;
188 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500189 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600190 const u8 *maddr;
191 u8 addr[6];
192
193 if (!dn) {
194 dev_dbg(&pdev->dev,
195 "No device node for mac, not configuring\n");
196 return -ENOENT;
197 }
198
olof@lixom.net1af7f052007-05-12 14:57:46 -0500199 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500200
olof@lixom.net1af7f052007-05-12 14:57:46 -0500201 if (maddr && len == 6) {
202 memcpy(mac->mac_addr, maddr, 6);
203 return 0;
204 }
205
206 /* Some old versions of firmware mistakenly uses mac-address
207 * (and as a string) instead of a byte array in local-mac-address.
208 */
209
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500210 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700211 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500212
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600213 if (maddr == NULL) {
214 dev_warn(&pdev->dev,
215 "no mac address in device tree, not configuring\n");
216 return -ENOENT;
217 }
218
219 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
220 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
221 dev_warn(&pdev->dev,
222 "can't parse mac address, not configuring\n");
223 return -EINVAL;
224 }
225
olof@lixom.net1af7f052007-05-12 14:57:46 -0500226 memcpy(mac->mac_addr, addr, 6);
227
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600228 return 0;
229}
230
Olof Johansson5cea73b2008-01-23 13:56:19 -0600231static int pasemi_mac_set_mac_addr(struct net_device *dev, void *p)
232{
233 struct pasemi_mac *mac = netdev_priv(dev);
234 struct sockaddr *addr = p;
235 unsigned int adr0, adr1;
236
237 if (!is_valid_ether_addr(addr->sa_data))
238 return -EINVAL;
239
240 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
241
242 adr0 = dev->dev_addr[2] << 24 |
243 dev->dev_addr[3] << 16 |
244 dev->dev_addr[4] << 8 |
245 dev->dev_addr[5];
246 adr1 = read_mac_reg(mac, PAS_MAC_CFG_ADR1);
247 adr1 &= ~0xffff;
248 adr1 |= dev->dev_addr[0] << 8 | dev->dev_addr[1];
249
250 pasemi_mac_intf_disable(mac);
251 write_mac_reg(mac, PAS_MAC_CFG_ADR0, adr0);
252 write_mac_reg(mac, PAS_MAC_CFG_ADR1, adr1);
253 pasemi_mac_intf_enable(mac);
254
255 return 0;
256}
257
Olof Johansson28ae79f2007-11-28 20:57:27 -0600258static int get_skb_hdr(struct sk_buff *skb, void **iphdr,
259 void **tcph, u64 *hdr_flags, void *data)
260{
261 u64 macrx = (u64) data;
262 unsigned int ip_len;
263 struct iphdr *iph;
264
265 /* IPv4 header checksum failed */
266 if ((macrx & XCT_MACRX_HTY_M) != XCT_MACRX_HTY_IPV4_OK)
267 return -1;
268
269 /* non tcp packet */
270 skb_reset_network_header(skb);
271 iph = ip_hdr(skb);
272 if (iph->protocol != IPPROTO_TCP)
273 return -1;
274
275 ip_len = ip_hdrlen(skb);
276 skb_set_transport_header(skb, ip_len);
277 *tcph = tcp_hdr(skb);
278
279 /* check if ip header and tcp header are complete */
280 if (iph->tot_len < ip_len + tcp_hdrlen(skb))
281 return -1;
282
283 *hdr_flags = LRO_IPV4 | LRO_TCP;
284 *iphdr = iph;
285
286 return 0;
287}
288
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500289static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
Olof Johansson7e9916e2007-11-28 20:57:45 -0600290 const int nfrags,
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500291 struct sk_buff *skb,
Olof Johansson5c153322007-11-28 20:56:41 -0600292 const dma_addr_t *dmas)
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500293{
294 int f;
Olof Johansson5c153322007-11-28 20:56:41 -0600295 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500296
Olof Johansson5c153322007-11-28 20:56:41 -0600297 pci_unmap_single(pdev, dmas[0], skb_headlen(skb), PCI_DMA_TODEVICE);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500298
299 for (f = 0; f < nfrags; f++) {
300 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
301
Olof Johansson5c153322007-11-28 20:56:41 -0600302 pci_unmap_page(pdev, dmas[f+1], frag->size, PCI_DMA_TODEVICE);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500303 }
304 dev_kfree_skb_irq(skb);
305
306 /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
307 * aligned up to a power of 2
308 */
309 return (nfrags + 3) & ~1;
310}
311
Olof Johansson8d636d82008-03-05 16:34:16 -0600312static struct pasemi_mac_csring *pasemi_mac_setup_csring(struct pasemi_mac *mac)
313{
314 struct pasemi_mac_csring *ring;
315 u32 val;
316 unsigned int cfg;
317 int chno;
318
319 ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_csring),
320 offsetof(struct pasemi_mac_csring, chan));
321
322 if (!ring) {
323 dev_err(&mac->pdev->dev, "Can't allocate checksum channel\n");
324 goto out_chan;
325 }
326
327 chno = ring->chan.chno;
328
329 ring->size = CS_RING_SIZE;
330 ring->next_to_fill = 0;
331
332 /* Allocate descriptors */
333 if (pasemi_dma_alloc_ring(&ring->chan, CS_RING_SIZE))
334 goto out_ring_desc;
335
336 write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno),
337 PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma));
338 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32);
339 val |= PAS_DMA_TXCHAN_BASEU_SIZ(CS_RING_SIZE >> 3);
340
341 write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val);
342
343 ring->events[0] = pasemi_dma_alloc_flag();
344 ring->events[1] = pasemi_dma_alloc_flag();
345 if (ring->events[0] < 0 || ring->events[1] < 0)
346 goto out_flags;
347
348 pasemi_dma_clear_flag(ring->events[0]);
349 pasemi_dma_clear_flag(ring->events[1]);
350
351 ring->fun = pasemi_dma_alloc_fun();
352 if (ring->fun < 0)
353 goto out_fun;
354
355 cfg = PAS_DMA_TXCHAN_CFG_TY_FUNC | PAS_DMA_TXCHAN_CFG_UP |
356 PAS_DMA_TXCHAN_CFG_TATTR(ring->fun) |
357 PAS_DMA_TXCHAN_CFG_LPSQ | PAS_DMA_TXCHAN_CFG_LPDQ;
358
359 if (translation_enabled())
360 cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
361
362 write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg);
363
364 /* enable channel */
365 pasemi_dma_start_chan(&ring->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
366 PAS_DMA_TXCHAN_TCMDSTA_DB |
367 PAS_DMA_TXCHAN_TCMDSTA_DE |
368 PAS_DMA_TXCHAN_TCMDSTA_DA);
369
370 return ring;
371
372out_fun:
373out_flags:
374 if (ring->events[0] >= 0)
375 pasemi_dma_free_flag(ring->events[0]);
376 if (ring->events[1] >= 0)
377 pasemi_dma_free_flag(ring->events[1]);
378 pasemi_dma_free_ring(&ring->chan);
379out_ring_desc:
380 pasemi_dma_free_chan(&ring->chan);
381out_chan:
382
383 return NULL;
384}
385
386static void pasemi_mac_setup_csrings(struct pasemi_mac *mac)
387{
388 int i;
389 mac->cs[0] = pasemi_mac_setup_csring(mac);
390 if (mac->type == MAC_TYPE_XAUI)
391 mac->cs[1] = pasemi_mac_setup_csring(mac);
392 else
393 mac->cs[1] = 0;
394
395 for (i = 0; i < MAX_CS; i++)
396 if (mac->cs[i])
397 mac->num_cs++;
398}
399
400static void pasemi_mac_free_csring(struct pasemi_mac_csring *csring)
401{
402 pasemi_dma_stop_chan(&csring->chan);
403 pasemi_dma_free_flag(csring->events[0]);
404 pasemi_dma_free_flag(csring->events[1]);
405 pasemi_dma_free_ring(&csring->chan);
406 pasemi_dma_free_chan(&csring->chan);
407}
408
Olof Johansson5c153322007-11-28 20:56:41 -0600409static int pasemi_mac_setup_rx_resources(const struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600410{
411 struct pasemi_mac_rxring *ring;
412 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson34c20622007-11-28 20:56:32 -0600413 int chno;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500414 unsigned int cfg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600415
Olof Johansson34c20622007-11-28 20:56:32 -0600416 ring = pasemi_dma_alloc_chan(RXCHAN, sizeof(struct pasemi_mac_rxring),
417 offsetof(struct pasemi_mac_rxring, chan));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600418
Olof Johansson34c20622007-11-28 20:56:32 -0600419 if (!ring) {
420 dev_err(&mac->pdev->dev, "Can't allocate RX channel\n");
421 goto out_chan;
422 }
423 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600424
425 spin_lock_init(&ring->lock);
426
Olof Johansson021fa222007-08-22 09:13:11 -0500427 ring->size = RX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500428 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600429 RX_RING_SIZE, GFP_KERNEL);
430
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, RX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500436 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600437
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600438 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
439 RX_RING_SIZE * sizeof(u64),
440 &ring->buf_dma, GFP_KERNEL);
441 if (!ring->buffers)
Olof Johansson34c20622007-11-28 20:56:32 -0600442 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600443
444 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
445
Olof Johansson34c20622007-11-28 20:56:32 -0600446 write_dma_reg(PAS_DMA_RXCHAN_BASEL(chno),
447 PAS_DMA_RXCHAN_BASEL_BRBL(ring->chan.ring_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600448
Olof Johansson34c20622007-11-28 20:56:32 -0600449 write_dma_reg(PAS_DMA_RXCHAN_BASEU(chno),
450 PAS_DMA_RXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32) |
451 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600452
Olof Johansson5c153322007-11-28 20:56:41 -0600453 cfg = PAS_DMA_RXCHAN_CFG_HBU(2);
Olof Johanssonaf289e82007-10-03 13:03:54 -0500454
455 if (translation_enabled())
456 cfg |= PAS_DMA_RXCHAN_CFG_CTR;
457
Olof Johansson34c20622007-11-28 20:56:32 -0600458 write_dma_reg(PAS_DMA_RXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600459
Olof Johansson34c20622007-11-28 20:56:32 -0600460 write_dma_reg(PAS_DMA_RXINT_BASEL(mac->dma_if),
461 PAS_DMA_RXINT_BASEL_BRBL(ring->buf_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600462
Olof Johansson34c20622007-11-28 20:56:32 -0600463 write_dma_reg(PAS_DMA_RXINT_BASEU(mac->dma_if),
464 PAS_DMA_RXINT_BASEU_BRBH(ring->buf_dma >> 32) |
465 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600466
Olof Johansson5c153322007-11-28 20:56:41 -0600467 cfg = PAS_DMA_RXINT_CFG_DHL(2) | PAS_DMA_RXINT_CFG_L2 |
Olof Johanssonaf289e82007-10-03 13:03:54 -0500468 PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP |
469 PAS_DMA_RXINT_CFG_HEN;
470
471 if (translation_enabled())
472 cfg |= PAS_DMA_RXINT_CFG_ITRR | PAS_DMA_RXINT_CFG_ITR;
473
Olof Johansson34c20622007-11-28 20:56:32 -0600474 write_dma_reg(PAS_DMA_RXINT_CFG(mac->dma_if), cfg);
Olof Johanssonc0efd522007-08-22 09:12:52 -0500475
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600476 ring->next_to_fill = 0;
477 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600478 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600479 mac->rx = ring;
480
481 return 0;
482
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500483out_ring_desc:
484 kfree(ring->ring_info);
485out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600486 pasemi_dma_free_chan(&ring->chan);
487out_chan:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600488 return -ENOMEM;
489}
490
Olof Johansson72b05b92007-11-28 20:54:28 -0600491static struct pasemi_mac_txring *
Olof Johansson5c153322007-11-28 20:56:41 -0600492pasemi_mac_setup_tx_resources(const struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600493{
494 struct pasemi_mac *mac = netdev_priv(dev);
495 u32 val;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600496 struct pasemi_mac_txring *ring;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500497 unsigned int cfg;
Olof Johansson34c20622007-11-28 20:56:32 -0600498 int chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600499
Olof Johansson34c20622007-11-28 20:56:32 -0600500 ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_txring),
501 offsetof(struct pasemi_mac_txring, chan));
502
503 if (!ring) {
504 dev_err(&mac->pdev->dev, "Can't allocate TX channel\n");
505 goto out_chan;
506 }
507
508 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600509
510 spin_lock_init(&ring->lock);
511
Olof Johansson021fa222007-08-22 09:13:11 -0500512 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500513 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600514 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500515 if (!ring->ring_info)
516 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600517
518 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600519 if (pasemi_dma_alloc_ring(&ring->chan, TX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500520 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600521
Olof Johansson34c20622007-11-28 20:56:32 -0600522 write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno),
523 PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma));
524 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500525 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600526
Olof Johansson34c20622007-11-28 20:56:32 -0600527 write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600528
Olof Johanssonaf289e82007-10-03 13:03:54 -0500529 cfg = PAS_DMA_TXCHAN_CFG_TY_IFACE |
530 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
531 PAS_DMA_TXCHAN_CFG_UP |
Olof Johansson8d636d82008-03-05 16:34:16 -0600532 PAS_DMA_TXCHAN_CFG_WT(4);
Olof Johanssonaf289e82007-10-03 13:03:54 -0500533
534 if (translation_enabled())
535 cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
536
Olof Johansson34c20622007-11-28 20:56:32 -0600537 write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600538
Olof Johansson021fa222007-08-22 09:13:11 -0500539 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600540 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600541 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600542
Olof Johansson72b05b92007-11-28 20:54:28 -0600543 return ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600544
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500545out_ring_desc:
546 kfree(ring->ring_info);
547out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600548 pasemi_dma_free_chan(&ring->chan);
549out_chan:
Olof Johansson72b05b92007-11-28 20:54:28 -0600550 return NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600551}
552
Olof Johansson72b05b92007-11-28 20:54:28 -0600553static void pasemi_mac_free_tx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600554{
Olof Johansson72b05b92007-11-28 20:54:28 -0600555 struct pasemi_mac_txring *txring = tx_ring(mac);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500556 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600557 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500558 dma_addr_t dmas[MAX_SKB_FRAGS+1];
Olof Johansson7e9916e2007-11-28 20:57:45 -0600559 int freed, nfrags;
Olof Johanssonad5da102007-10-02 16:27:15 -0500560 int start, limit;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600561
Olof Johansson72b05b92007-11-28 20:54:28 -0600562 start = txring->next_to_clean;
563 limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500564
565 /* Compensate for when fill has wrapped and clean has not */
566 if (start > limit)
567 limit += TX_RING_SIZE;
568
569 for (i = start; i < limit; i += freed) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600570 info = &txring->ring_info[(i+1) & (TX_RING_SIZE-1)];
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500571 if (info->dma && info->skb) {
Olof Johansson7e9916e2007-11-28 20:57:45 -0600572 nfrags = skb_shinfo(info->skb)->nr_frags;
573 for (j = 0; j <= nfrags; j++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600574 dmas[j] = txring->ring_info[(i+1+j) &
575 (TX_RING_SIZE-1)].dma;
Olof Johansson7e9916e2007-11-28 20:57:45 -0600576 freed = pasemi_mac_unmap_tx_skb(mac, nfrags,
577 info->skb, dmas);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500578 } else
579 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600580 }
581
Olof Johansson72b05b92007-11-28 20:54:28 -0600582 kfree(txring->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600583 pasemi_dma_free_chan(&txring->chan);
584
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600585}
586
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600587static void pasemi_mac_free_rx_buffers(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600588{
Olof Johansson72b05b92007-11-28 20:54:28 -0600589 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600590 unsigned int i;
591 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600592
593 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600594 info = &RX_DESC_INFO(rx, i);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500595 if (info->skb && info->dma) {
596 pci_unmap_single(mac->dma_pdev,
597 info->dma,
598 info->skb->len,
599 PCI_DMA_FROMDEVICE);
600 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600601 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500602 info->dma = 0;
603 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600604 }
605
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500606 for (i = 0; i < RX_RING_SIZE; i++)
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600607 RX_BUFF(rx, i) = 0;
608}
609
610static void pasemi_mac_free_rx_resources(struct pasemi_mac *mac)
611{
612 pasemi_mac_free_rx_buffers(mac);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500613
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600614 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
Olof Johansson72b05b92007-11-28 20:54:28 -0600615 rx_ring(mac)->buffers, rx_ring(mac)->buf_dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600616
Olof Johansson72b05b92007-11-28 20:54:28 -0600617 kfree(rx_ring(mac)->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600618 pasemi_dma_free_chan(&rx_ring(mac)->chan);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600619 mac->rx = NULL;
620}
621
Olof Johansson5c153322007-11-28 20:56:41 -0600622static void pasemi_mac_replenish_rx_ring(const struct net_device *dev,
623 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600624{
Olof Johansson5c153322007-11-28 20:56:41 -0600625 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -0600626 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500627 int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600628
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500629 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600630 return;
631
Olof Johansson72b05b92007-11-28 20:54:28 -0600632 fill = rx_ring(mac)->next_to_fill;
Olof Johansson928773c2007-09-26 16:25:06 -0500633 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600634 struct pasemi_mac_buffer *info = &RX_DESC_INFO(rx, fill);
635 u64 *buff = &RX_BUFF(rx, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600636 struct sk_buff *skb;
637 dma_addr_t dma;
638
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500639 /* Entry in use? */
640 WARN_ON(*buff);
641
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600642 skb = dev_alloc_skb(mac->bufsz);
Olof Johansson5d894942007-11-28 20:57:56 -0600643 skb_reserve(skb, LOCAL_SKB_ALIGN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600644
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500645 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600646 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600647
Olof Johansson8dc121a2007-10-02 16:26:53 -0500648 dma = pci_map_single(mac->dma_pdev, skb->data,
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600649 mac->bufsz - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600650 PCI_DMA_FROMDEVICE);
651
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500652 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600653 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600654 break;
655 }
656
657 info->skb = skb;
658 info->dma = dma;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600659 *buff = XCT_RXB_LEN(mac->bufsz) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500660 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600661 }
662
663 wmb();
664
Olof Johansson34c20622007-11-28 20:56:32 -0600665 write_dma_reg(PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600666
Olof Johansson72b05b92007-11-28 20:54:28 -0600667 rx_ring(mac)->next_to_fill = (rx_ring(mac)->next_to_fill + count) &
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500668 (RX_RING_SIZE - 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600669}
670
Olof Johansson5c153322007-11-28 20:56:41 -0600671static void pasemi_mac_restart_rx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500672{
Olof Johansson906674a2007-11-28 20:57:09 -0600673 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johansson52a94352007-05-12 18:01:09 -0500674 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500675 /* Re-enable packet count interrupts: finally
676 * ack the packet count interrupt we got in rx_intr.
677 */
678
Olof Johansson906674a2007-11-28 20:57:09 -0600679 pcnt = *rx->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500680
Olof Johansson52a94352007-05-12 18:01:09 -0500681 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500682
Olof Johansson906674a2007-11-28 20:57:09 -0600683 if (*rx->chan.status & PAS_STATUS_TIMER)
684 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
685
Olof Johansson34c20622007-11-28 20:56:32 -0600686 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(mac->rx->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500687}
688
Olof Johansson5c153322007-11-28 20:56:41 -0600689static void pasemi_mac_restart_tx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500690{
Olof Johansson52a94352007-05-12 18:01:09 -0500691 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500692
693 /* Re-enable packet count interrupts */
Olof Johansson34c20622007-11-28 20:56:32 -0600694 pcnt = *tx_ring(mac)->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500695
Olof Johansson52a94352007-05-12 18:01:09 -0500696 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500697
Olof Johansson34c20622007-11-28 20:56:32 -0600698 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(tx_ring(mac)->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500699}
700
701
Olof Johansson5c153322007-11-28 20:56:41 -0600702static inline void pasemi_mac_rx_error(const struct pasemi_mac *mac,
703 const u64 macrx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500704{
705 unsigned int rcmdsta, ccmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600706 struct pasemi_dmachan *chan = &rx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500707
708 if (!netif_msg_rx_err(mac))
709 return;
710
Olof Johansson34c20622007-11-28 20:56:32 -0600711 rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
712 ccmdsta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500713
714 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
Olof Johansson34c20622007-11-28 20:56:32 -0600715 macrx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500716
717 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
718 rcmdsta, ccmdsta);
719}
720
Olof Johansson5c153322007-11-28 20:56:41 -0600721static inline void pasemi_mac_tx_error(const struct pasemi_mac *mac,
722 const u64 mactx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500723{
724 unsigned int cmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600725 struct pasemi_dmachan *chan = &tx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500726
727 if (!netif_msg_tx_err(mac))
728 return;
729
Olof Johansson34c20622007-11-28 20:56:32 -0600730 cmdsta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500731
732 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
Olof Johansson34c20622007-11-28 20:56:32 -0600733 "tx status 0x%016lx\n", mactx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500734
735 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
736}
737
Olof Johansson5c153322007-11-28 20:56:41 -0600738static int pasemi_mac_clean_rx(struct pasemi_mac_rxring *rx,
739 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600740{
Olof Johansson5c153322007-11-28 20:56:41 -0600741 const struct pasemi_dmachan *chan = &rx->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600742 struct pasemi_mac *mac = rx->mac;
Olof Johansson5c153322007-11-28 20:56:41 -0600743 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500744 unsigned int n;
Olof Johansson5c153322007-11-28 20:56:41 -0600745 int count, buf_index, tot_bytes, packets;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500746 struct pasemi_mac_buffer *info;
747 struct sk_buff *skb;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500748 unsigned int len;
Olof Johansson5c153322007-11-28 20:56:41 -0600749 u64 macrx, eval;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500750 dma_addr_t dma;
Olof Johansson5c153322007-11-28 20:56:41 -0600751
752 tot_bytes = 0;
753 packets = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600754
Olof Johansson72b05b92007-11-28 20:54:28 -0600755 spin_lock(&rx->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600756
Olof Johansson72b05b92007-11-28 20:54:28 -0600757 n = rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600758
Olof Johansson72b05b92007-11-28 20:54:28 -0600759 prefetch(&RX_DESC(rx, n));
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500760
761 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600762 macrx = RX_DESC(rx, n);
Olof Johansson5c153322007-11-28 20:56:41 -0600763 prefetch(&RX_DESC(rx, n+4));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600764
Olof Johansson69c29d82007-10-02 16:24:51 -0500765 if ((macrx & XCT_MACRX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600766 (*chan->status & PAS_STATUS_ERROR))
Olof Johansson69c29d82007-10-02 16:24:51 -0500767 pasemi_mac_rx_error(mac, macrx);
768
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500769 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600770 break;
771
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600772 info = NULL;
773
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500774 BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600775
Olof Johansson72b05b92007-11-28 20:54:28 -0600776 eval = (RX_DESC(rx, n+1) & XCT_RXRES_8B_EVAL_M) >>
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500777 XCT_RXRES_8B_EVAL_S;
778 buf_index = eval-1;
779
Olof Johansson72b05b92007-11-28 20:54:28 -0600780 dma = (RX_DESC(rx, n+2) & XCT_PTR_ADDR_M);
781 info = &RX_DESC_INFO(rx, buf_index);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500782
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500783 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600784
Olof Johansson5c153322007-11-28 20:56:41 -0600785 prefetch_skb(skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600786
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500787 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600788
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600789 pci_unmap_single(pdev, dma, mac->bufsz - LOCAL_SKB_ALIGN,
Olof Johansson5c153322007-11-28 20:56:41 -0600790 PCI_DMA_FROMDEVICE);
Olof Johansson32bee772007-11-06 22:21:38 -0600791
792 if (macrx & XCT_MACRX_CRC) {
793 /* CRC error flagged */
794 mac->netdev->stats.rx_errors++;
795 mac->netdev->stats.rx_crc_errors++;
Olof Johansson4352d822007-12-03 21:34:14 -0600796 /* No need to free skb, it'll be reused */
Olof Johansson32bee772007-11-06 22:21:38 -0600797 goto next;
798 }
799
Olof Johansson5d894942007-11-28 20:57:56 -0600800 info->skb = NULL;
Olof Johanssonad5da102007-10-02 16:27:15 -0500801 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500802
Olof Johansson26fcfa92007-08-22 09:12:59 -0500803 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500804 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500805 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600806 XCT_MACRX_CSUM_S;
807 } else
808 skb->ip_summed = CHECKSUM_NONE;
809
Olof Johansson5c153322007-11-28 20:56:41 -0600810 packets++;
811 tot_bytes += len;
812
813 /* Don't include CRC */
814 skb_put(skb, len-4);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600815
Olof Johansson26fcfa92007-08-22 09:12:59 -0500816 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johansson28ae79f2007-11-28 20:57:27 -0600817 lro_receive_skb(&mac->lro_mgr, skb, (void *)macrx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600818
Olof Johansson32bee772007-11-06 22:21:38 -0600819next:
Olof Johansson72b05b92007-11-28 20:54:28 -0600820 RX_DESC(rx, n) = 0;
821 RX_DESC(rx, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500822
Olof Johanssonad5da102007-10-02 16:27:15 -0500823 /* Need to zero it out since hardware doesn't, since the
824 * replenish loop uses it to tell when it's done.
825 */
Olof Johansson72b05b92007-11-28 20:54:28 -0600826 RX_BUFF(rx, buf_index) = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500827
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500828 n += 4;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600829 }
830
Olof Johansson9a50beb2007-10-02 16:26:30 -0500831 if (n > RX_RING_SIZE) {
832 /* Errata 5971 workaround: L2 target of headers */
Olof Johansson34c20622007-11-28 20:56:32 -0600833 write_iob_reg(PAS_IOB_COM_PKTHDRCNT, 0);
Olof Johansson9a50beb2007-10-02 16:26:30 -0500834 n &= (RX_RING_SIZE-1);
835 }
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500836
Olof Johansson72b05b92007-11-28 20:54:28 -0600837 rx_ring(mac)->next_to_clean = n;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500838
Olof Johansson28ae79f2007-11-28 20:57:27 -0600839 lro_flush_all(&mac->lro_mgr);
840
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500841 /* Increase is in number of 16-byte entries, and since each descriptor
842 * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
843 * count*2.
844 */
Olof Johansson34c20622007-11-28 20:56:32 -0600845 write_dma_reg(PAS_DMA_RXCHAN_INCR(mac->rx->chan.chno), count << 1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500846
847 pasemi_mac_replenish_rx_ring(mac->netdev, count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600848
Olof Johansson5c153322007-11-28 20:56:41 -0600849 mac->netdev->stats.rx_bytes += tot_bytes;
850 mac->netdev->stats.rx_packets += packets;
851
Olof Johansson72b05b92007-11-28 20:54:28 -0600852 spin_unlock(&rx_ring(mac)->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600853
854 return count;
855}
856
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500857/* Can't make this too large or we blow the kernel stack limits */
858#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
859
Olof Johansson72b05b92007-11-28 20:54:28 -0600860static int pasemi_mac_clean_tx(struct pasemi_mac_txring *txring)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600861{
Olof Johansson34c20622007-11-28 20:56:32 -0600862 struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600863 struct pasemi_mac *mac = txring->mac;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500864 int i, j;
Olof Johanssonad5da102007-10-02 16:27:15 -0500865 unsigned int start, descr_count, buf_count, batch_limit;
866 unsigned int ring_limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500867 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500868 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500869 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
870 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johansson7e9916e2007-11-28 20:57:45 -0600871 int nf[TX_CLEAN_BATCHSIZE];
872 int nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600873
Olof Johansson02df6cf2007-08-22 09:13:03 -0500874 total_count = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500875 batch_limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500876restart:
Olof Johansson72b05b92007-11-28 20:54:28 -0600877 spin_lock_irqsave(&txring->lock, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600878
Olof Johansson72b05b92007-11-28 20:54:28 -0600879 start = txring->next_to_clean;
880 ring_limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500881
Olof Johansson7e9916e2007-11-28 20:57:45 -0600882 prefetch(&TX_DESC_INFO(txring, start+1).skb);
883
Olof Johanssonad5da102007-10-02 16:27:15 -0500884 /* Compensate for when fill has wrapped but clean has not */
885 if (start > ring_limit)
886 ring_limit += TX_RING_SIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500887
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500888 buf_count = 0;
889 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600890
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500891 for (i = start;
Olof Johanssonad5da102007-10-02 16:27:15 -0500892 descr_count < batch_limit && i < ring_limit;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500893 i += buf_count) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600894 u64 mactx = TX_DESC(txring, i);
Olof Johanssonad5da102007-10-02 16:27:15 -0500895 struct sk_buff *skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500896
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500897 if ((mactx & XCT_MACTX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600898 (*chan->status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500899 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500900
Olof Johansson8d636d82008-03-05 16:34:16 -0600901 /* Skip over control descriptors */
902 if (!(mactx & XCT_MACTX_LLEN_M)) {
903 TX_DESC(txring, i) = 0;
904 TX_DESC(txring, i+1) = 0;
905 buf_count = 2;
906 continue;
907 }
908
909 skb = TX_DESC_INFO(txring, i+1).skb;
910 nr_frags = TX_DESC_INFO(txring, i).dma;
911
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500912 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500913 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600914 break;
915
Olof Johansson7e9916e2007-11-28 20:57:45 -0600916 buf_count = 2 + nr_frags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500917 /* Since we always fill with an even number of entries, make
918 * sure we skip any unused one at the end as well.
919 */
920 if (buf_count & 1)
921 buf_count++;
Olof Johansson7e9916e2007-11-28 20:57:45 -0600922
923 for (j = 0; j <= nr_frags; j++)
924 dmas[descr_count][j] = TX_DESC_INFO(txring, i+1+j).dma;
925
926 skbs[descr_count] = skb;
927 nf[descr_count] = nr_frags;
928
929 TX_DESC(txring, i) = 0;
930 TX_DESC(txring, i+1) = 0;
931
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500932 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600933 }
Olof Johansson72b05b92007-11-28 20:54:28 -0600934 txring->next_to_clean = i & (TX_RING_SIZE-1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500935
Olof Johansson72b05b92007-11-28 20:54:28 -0600936 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500937 netif_wake_queue(mac->netdev);
938
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500939 for (i = 0; i < descr_count; i++)
Olof Johansson7e9916e2007-11-28 20:57:45 -0600940 pasemi_mac_unmap_tx_skb(mac, nf[i], skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500941
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500942 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500943
944 /* If the batch was full, try to clean more */
Olof Johanssonad5da102007-10-02 16:27:15 -0500945 if (descr_count == batch_limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500946 goto restart;
947
948 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600949}
950
951
952static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
953{
Olof Johansson5c153322007-11-28 20:56:41 -0600954 const struct pasemi_mac_rxring *rxring = data;
Olof Johansson34c20622007-11-28 20:56:32 -0600955 struct pasemi_mac *mac = rxring->mac;
956 struct net_device *dev = mac->netdev;
Olof Johansson5c153322007-11-28 20:56:41 -0600957 const struct pasemi_dmachan *chan = &rxring->chan;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600958 unsigned int reg;
959
Olof Johansson34c20622007-11-28 20:56:32 -0600960 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600961 return IRQ_NONE;
962
Olof Johansson6dfa7522007-05-08 00:47:32 -0500963 /* Don't reset packet count so it won't fire again but clear
964 * all others.
965 */
966
Olof Johansson6dfa7522007-05-08 00:47:32 -0500967 reg = 0;
Olof Johansson34c20622007-11-28 20:56:32 -0600968 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500969 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600970 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500971 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600972
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700973 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500974
Olof Johansson34c20622007-11-28 20:56:32 -0600975 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600976
977 return IRQ_HANDLED;
978}
979
Olof Johansson61cec3b2007-11-28 20:56:54 -0600980#define TX_CLEAN_INTERVAL HZ
981
982static void pasemi_mac_tx_timer(unsigned long data)
983{
984 struct pasemi_mac_txring *txring = (struct pasemi_mac_txring *)data;
985 struct pasemi_mac *mac = txring->mac;
986
987 pasemi_mac_clean_tx(txring);
988
989 mod_timer(&txring->clean_timer, jiffies + TX_CLEAN_INTERVAL);
990
991 pasemi_mac_restart_tx_intr(mac);
992}
993
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600994static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
995{
Olof Johansson72b05b92007-11-28 20:54:28 -0600996 struct pasemi_mac_txring *txring = data;
Olof Johansson5c153322007-11-28 20:56:41 -0600997 const struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson61cec3b2007-11-28 20:56:54 -0600998 struct pasemi_mac *mac = txring->mac;
999 unsigned int reg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001000
Olof Johansson34c20622007-11-28 20:56:32 -06001001 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001002 return IRQ_NONE;
1003
Olof Johansson61cec3b2007-11-28 20:56:54 -06001004 reg = 0;
Olof Johansson6dfa7522007-05-08 00:47:32 -05001005
Olof Johansson34c20622007-11-28 20:56:32 -06001006 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -05001007 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -06001008 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -05001009 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001010
Olof Johansson61cec3b2007-11-28 20:56:54 -06001011 mod_timer(&txring->clean_timer, jiffies + (TX_CLEAN_INTERVAL)*2);
1012
1013 netif_rx_schedule(mac->netdev, &mac->napi);
1014
1015 if (reg)
1016 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001017
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001018 return IRQ_HANDLED;
1019}
1020
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001021static void pasemi_adjust_link(struct net_device *dev)
1022{
1023 struct pasemi_mac *mac = netdev_priv(dev);
1024 int msg;
1025 unsigned int flags;
1026 unsigned int new_flags;
1027
1028 if (!mac->phydev->link) {
1029 /* If no link, MAC speed settings don't matter. Just report
1030 * link down and return.
1031 */
1032 if (mac->link && netif_msg_link(mac))
1033 printk(KERN_INFO "%s: Link is down.\n", dev->name);
1034
1035 netif_carrier_off(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001036 pasemi_mac_intf_disable(mac);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001037 mac->link = 0;
1038
1039 return;
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001040 } else {
1041 pasemi_mac_intf_enable(mac);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001042 netif_carrier_on(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001043 }
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001044
Olof Johanssona85b9422007-09-15 13:40:59 -07001045 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001046 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
1047 PAS_MAC_CFG_PCFG_TSR_M);
1048
1049 if (!mac->phydev->duplex)
1050 new_flags |= PAS_MAC_CFG_PCFG_HD;
1051
1052 switch (mac->phydev->speed) {
1053 case 1000:
1054 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
1055 PAS_MAC_CFG_PCFG_TSR_1G;
1056 break;
1057 case 100:
1058 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
1059 PAS_MAC_CFG_PCFG_TSR_100M;
1060 break;
1061 case 10:
1062 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
1063 PAS_MAC_CFG_PCFG_TSR_10M;
1064 break;
1065 default:
1066 printk("Unsupported speed %d\n", mac->phydev->speed);
1067 }
1068
1069 /* Print on link or speed/duplex change */
1070 msg = mac->link != mac->phydev->link || flags != new_flags;
1071
1072 mac->duplex = mac->phydev->duplex;
1073 mac->speed = mac->phydev->speed;
1074 mac->link = mac->phydev->link;
1075
1076 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -07001077 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001078
1079 if (msg && netif_msg_link(mac))
1080 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
1081 dev->name, mac->speed, mac->duplex ? "full" : "half");
1082}
1083
1084static int pasemi_mac_phy_init(struct net_device *dev)
1085{
1086 struct pasemi_mac *mac = netdev_priv(dev);
1087 struct device_node *dn, *phy_dn;
1088 struct phy_device *phydev;
1089 unsigned int phy_id;
1090 const phandle *ph;
1091 const unsigned int *prop;
1092 struct resource r;
1093 int ret;
1094
1095 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -07001096 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001097 if (!ph)
1098 return -ENODEV;
1099 phy_dn = of_find_node_by_phandle(*ph);
1100
Linus Torvalds90287802007-05-08 11:57:17 -07001101 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001102 ret = of_address_to_resource(phy_dn->parent, 0, &r);
1103 if (ret)
1104 goto err;
1105
1106 phy_id = *prop;
1107 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
1108
1109 of_node_put(phy_dn);
1110
1111 mac->link = 0;
1112 mac->speed = 0;
1113 mac->duplex = -1;
1114
1115 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
1116
1117 if (IS_ERR(phydev)) {
1118 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
1119 return PTR_ERR(phydev);
1120 }
1121
1122 mac->phydev = phydev;
1123
1124 return 0;
1125
1126err:
1127 of_node_put(phy_dn);
1128 return -ENODEV;
1129}
1130
1131
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001132static int pasemi_mac_open(struct net_device *dev)
1133{
1134 struct pasemi_mac *mac = netdev_priv(dev);
1135 unsigned int flags;
Olof Johanssone37c7722008-02-20 20:57:59 -06001136 int i, ret;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001137
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001138 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
1139 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
1140 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
1141
Olof Johanssona85b9422007-09-15 13:40:59 -07001142 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001143
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001144 ret = pasemi_mac_setup_rx_resources(dev);
1145 if (ret)
1146 goto out_rx_resources;
1147
Olof Johansson34c20622007-11-28 20:56:32 -06001148 mac->tx = pasemi_mac_setup_tx_resources(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001149
1150 if (!mac->tx)
1151 goto out_tx_ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001152
Olof Johansson8d636d82008-03-05 16:34:16 -06001153 if (dev->mtu > 1500) {
1154 pasemi_mac_setup_csrings(mac);
1155 if (!mac->num_cs)
1156 goto out_tx_ring;
1157 }
1158
Olof Johanssone37c7722008-02-20 20:57:59 -06001159 /* Zero out rmon counters */
1160 for (i = 0; i < 32; i++)
1161 write_mac_reg(mac, PAS_MAC_RMON(i), 0);
1162
Olof Johansson906674a2007-11-28 20:57:09 -06001163 /* 0x3ff with 33MHz clock is about 31us */
1164 write_iob_reg(PAS_IOB_DMA_COM_TIMEOUTCFG,
1165 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0x3ff));
1166
Olof Johansson34c20622007-11-28 20:56:32 -06001167 write_iob_reg(PAS_IOB_DMA_RXCH_CFG(mac->rx->chan.chno),
Olof Johansson28ae79f2007-11-28 20:57:27 -06001168 PAS_IOB_DMA_RXCH_CFG_CNTTH(256));
Olof Johansson34c20622007-11-28 20:56:32 -06001169
1170 write_iob_reg(PAS_IOB_DMA_TXCH_CFG(mac->tx->chan.chno),
Olof Johansson61cec3b2007-11-28 20:56:54 -06001171 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johansson34c20622007-11-28 20:56:32 -06001172
Olof Johanssona85b9422007-09-15 13:40:59 -07001173 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
Olof Johansson34c20622007-11-28 20:56:32 -06001174 PAS_MAC_IPC_CHNL_DCHNO(mac->rx->chan.chno) |
1175 PAS_MAC_IPC_CHNL_BCH(mac->rx->chan.chno));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001176
1177 /* enable rx if */
Olof Johansson34c20622007-11-28 20:56:32 -06001178 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1179 PAS_DMA_RXINT_RCMDSTA_EN |
1180 PAS_DMA_RXINT_RCMDSTA_DROPS_M |
1181 PAS_DMA_RXINT_RCMDSTA_BP |
1182 PAS_DMA_RXINT_RCMDSTA_OO |
1183 PAS_DMA_RXINT_RCMDSTA_BT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001184
1185 /* enable rx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001186 pasemi_dma_start_chan(&rx_ring(mac)->chan, PAS_DMA_RXCHAN_CCMDSTA_DU |
1187 PAS_DMA_RXCHAN_CCMDSTA_OD |
1188 PAS_DMA_RXCHAN_CCMDSTA_FD |
1189 PAS_DMA_RXCHAN_CCMDSTA_DT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001190
1191 /* enable tx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001192 pasemi_dma_start_chan(&tx_ring(mac)->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
1193 PAS_DMA_TXCHAN_TCMDSTA_DB |
1194 PAS_DMA_TXCHAN_TCMDSTA_DE |
1195 PAS_DMA_TXCHAN_TCMDSTA_DA);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001196
Olof Johansson928773c2007-09-26 16:25:06 -05001197 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001198
Olof Johansson34c20622007-11-28 20:56:32 -06001199 write_dma_reg(PAS_DMA_RXCHAN_INCR(rx_ring(mac)->chan.chno),
1200 RX_RING_SIZE>>1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -05001201
Olof Johansson72b05b92007-11-28 20:54:28 -06001202 /* Clear out any residual packet count state from firmware */
1203 pasemi_mac_restart_rx_intr(mac);
1204 pasemi_mac_restart_tx_intr(mac);
1205
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001206 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
Olof Johansson36033762007-09-26 16:24:42 -05001207
1208 if (mac->type == MAC_TYPE_GMAC)
1209 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
1210 else
1211 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
1212
1213 /* Enable interface in MAC */
1214 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
1215
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001216 ret = pasemi_mac_phy_init(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001217 if (ret) {
1218 /* Since we won't get link notification, just enable RX */
1219 pasemi_mac_intf_enable(mac);
1220 if (mac->type == MAC_TYPE_GMAC) {
1221 /* Warn for missing PHY on SGMII (1Gig) ports */
1222 dev_warn(&mac->pdev->dev,
1223 "PHY init failed: %d.\n", ret);
1224 dev_warn(&mac->pdev->dev,
1225 "Defaulting to 1Gbit full duplex\n");
1226 }
Olof Johansson8304b632007-11-28 20:58:06 -06001227 }
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001228
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001229 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001230 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001231
Olof Johansson72b05b92007-11-28 20:54:28 -06001232 snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx",
1233 dev->name);
Olof Johansson771f7402007-05-08 00:47:21 -05001234
Olof Johansson34c20622007-11-28 20:56:32 -06001235 ret = request_irq(mac->tx->chan.irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johansson72b05b92007-11-28 20:54:28 -06001236 mac->tx_irq_name, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001237 if (ret) {
1238 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001239 mac->tx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001240 goto out_tx_int;
1241 }
1242
Olof Johansson72b05b92007-11-28 20:54:28 -06001243 snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx",
1244 dev->name);
1245
Olof Johansson34c20622007-11-28 20:56:32 -06001246 ret = request_irq(mac->rx->chan.irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
1247 mac->rx_irq_name, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001248 if (ret) {
1249 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001250 mac->rx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001251 goto out_rx_int;
1252 }
1253
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001254 if (mac->phydev)
1255 phy_start(mac->phydev);
1256
Olof Johansson61cec3b2007-11-28 20:56:54 -06001257 init_timer(&mac->tx->clean_timer);
1258 mac->tx->clean_timer.function = pasemi_mac_tx_timer;
1259 mac->tx->clean_timer.data = (unsigned long)mac->tx;
1260 mac->tx->clean_timer.expires = jiffies+HZ;
1261 add_timer(&mac->tx->clean_timer);
1262
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001263 return 0;
1264
1265out_rx_int:
Olof Johansson34c20622007-11-28 20:56:32 -06001266 free_irq(mac->tx->chan.irq, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001267out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001268 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001269 netif_stop_queue(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001270out_tx_ring:
1271 if (mac->tx)
1272 pasemi_mac_free_tx_resources(mac);
1273 pasemi_mac_free_rx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001274out_rx_resources:
1275
1276 return ret;
1277}
1278
1279#define MAX_RETRIES 5000
1280
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001281static void pasemi_mac_pause_txchan(struct pasemi_mac *mac)
1282{
1283 unsigned int sta, retries;
1284 int txch = tx_ring(mac)->chan.chno;
1285
1286 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch),
1287 PAS_DMA_TXCHAN_TCMDSTA_ST);
1288
1289 for (retries = 0; retries < MAX_RETRIES; retries++) {
1290 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
1291 if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
1292 break;
1293 cond_resched();
1294 }
1295
1296 if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
1297 dev_err(&mac->dma_pdev->dev,
1298 "Failed to stop tx channel, tcmdsta %08x\n", sta);
1299
1300 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch), 0);
1301}
1302
1303static void pasemi_mac_pause_rxchan(struct pasemi_mac *mac)
1304{
1305 unsigned int sta, retries;
1306 int rxch = rx_ring(mac)->chan.chno;
1307
1308 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch),
1309 PAS_DMA_RXCHAN_CCMDSTA_ST);
1310 for (retries = 0; retries < MAX_RETRIES; retries++) {
1311 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
1312 if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
1313 break;
1314 cond_resched();
1315 }
1316
1317 if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
1318 dev_err(&mac->dma_pdev->dev,
1319 "Failed to stop rx channel, ccmdsta 08%x\n", sta);
1320 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch), 0);
1321}
1322
1323static void pasemi_mac_pause_rxint(struct pasemi_mac *mac)
1324{
1325 unsigned int sta, retries;
1326
1327 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1328 PAS_DMA_RXINT_RCMDSTA_ST);
1329 for (retries = 0; retries < MAX_RETRIES; retries++) {
1330 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1331 if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
1332 break;
1333 cond_resched();
1334 }
1335
1336 if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
1337 dev_err(&mac->dma_pdev->dev,
1338 "Failed to stop rx interface, rcmdsta %08x\n", sta);
1339 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
1340}
1341
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001342static int pasemi_mac_close(struct net_device *dev)
1343{
1344 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson9e81d332007-10-02 16:27:39 -05001345 unsigned int sta;
Olof Johansson8d636d82008-03-05 16:34:16 -06001346 int rxch, txch, i;
Olof Johansson34c20622007-11-28 20:56:32 -06001347
1348 rxch = rx_ring(mac)->chan.chno;
1349 txch = tx_ring(mac)->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001350
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001351 if (mac->phydev) {
1352 phy_stop(mac->phydev);
1353 phy_disconnect(mac->phydev);
1354 }
1355
Olof Johansson61cec3b2007-11-28 20:56:54 -06001356 del_timer_sync(&mac->tx->clean_timer);
1357
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001358 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001359 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001360
Olof Johansson34c20622007-11-28 20:56:32 -06001361 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson9e81d332007-10-02 16:27:39 -05001362 if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
1363 PAS_DMA_RXINT_RCMDSTA_OO |
1364 PAS_DMA_RXINT_RCMDSTA_BT))
1365 printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);
1366
Olof Johansson34c20622007-11-28 20:56:32 -06001367 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001368 if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
1369 PAS_DMA_RXCHAN_CCMDSTA_OD |
1370 PAS_DMA_RXCHAN_CCMDSTA_FD |
1371 PAS_DMA_RXCHAN_CCMDSTA_DT))
1372 printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);
1373
Olof Johansson34c20622007-11-28 20:56:32 -06001374 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
Olof Johansson72b05b92007-11-28 20:54:28 -06001375 if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB |
1376 PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA))
Olof Johansson9e81d332007-10-02 16:27:39 -05001377 printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);
1378
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001379 /* Clean out any pending buffers */
Olof Johansson72b05b92007-11-28 20:54:28 -06001380 pasemi_mac_clean_tx(tx_ring(mac));
1381 pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001382
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001383 pasemi_mac_pause_txchan(mac);
1384 pasemi_mac_pause_rxint(mac);
1385 pasemi_mac_pause_rxchan(mac);
Olof Johansson1145d952008-01-23 13:57:19 -06001386 pasemi_mac_intf_disable(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001387
Olof Johansson34c20622007-11-28 20:56:32 -06001388 free_irq(mac->tx->chan.irq, mac->tx);
1389 free_irq(mac->rx->chan.irq, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001390
Olof Johansson8d636d82008-03-05 16:34:16 -06001391 for (i = 0; i < mac->num_cs; i++)
1392 pasemi_mac_free_csring(mac->cs[i]);
1393
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001394 /* Free resources */
Olof Johansson72b05b92007-11-28 20:54:28 -06001395 pasemi_mac_free_rx_resources(mac);
1396 pasemi_mac_free_tx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001397
1398 return 0;
1399}
1400
Olof Johansson8d636d82008-03-05 16:34:16 -06001401static void pasemi_mac_queue_csdesc(const struct sk_buff *skb,
1402 const dma_addr_t *map,
1403 const unsigned int *map_size,
1404 struct pasemi_mac_txring *txring,
1405 struct pasemi_mac_csring *csring)
1406{
1407 u64 fund;
1408 dma_addr_t cs_dest;
1409 const int nh_off = skb_network_offset(skb);
1410 const int nh_len = skb_network_header_len(skb);
1411 const int nfrags = skb_shinfo(skb)->nr_frags;
1412 int cs_size, i, fill, hdr, cpyhdr, evt;
1413 dma_addr_t csdma;
1414
1415 fund = XCT_FUN_ST | XCT_FUN_RR_8BRES |
1416 XCT_FUN_O | XCT_FUN_FUN(csring->fun) |
1417 XCT_FUN_CRM_SIG | XCT_FUN_LLEN(skb->len - nh_off) |
1418 XCT_FUN_SHL(nh_len >> 2) | XCT_FUN_SE;
1419
1420 switch (ip_hdr(skb)->protocol) {
1421 case IPPROTO_TCP:
1422 fund |= XCT_FUN_SIG_TCP4;
1423 /* TCP checksum is 16 bytes into the header */
1424 cs_dest = map[0] + skb_transport_offset(skb) + 16;
1425 break;
1426 case IPPROTO_UDP:
1427 fund |= XCT_FUN_SIG_UDP4;
1428 /* UDP checksum is 6 bytes into the header */
1429 cs_dest = map[0] + skb_transport_offset(skb) + 6;
1430 break;
1431 default:
1432 BUG();
1433 }
1434
1435 /* Do the checksum offloaded */
1436 fill = csring->next_to_fill;
1437 hdr = fill;
1438
1439 CS_DESC(csring, fill++) = fund;
1440 /* Room for 8BRES. Checksum result is really 2 bytes into it */
1441 csdma = csring->chan.ring_dma + (fill & (CS_RING_SIZE-1)) * 8 + 2;
1442 CS_DESC(csring, fill++) = 0;
1443
1444 CS_DESC(csring, fill) = XCT_PTR_LEN(map_size[0]-nh_off) | XCT_PTR_ADDR(map[0]+nh_off);
1445 for (i = 1; i <= nfrags; i++)
1446 CS_DESC(csring, fill+i) = XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
1447
1448 fill += i;
1449 if (fill & 1)
1450 fill++;
1451
1452 /* Copy the result into the TCP packet */
1453 cpyhdr = fill;
1454 CS_DESC(csring, fill++) = XCT_FUN_O | XCT_FUN_FUN(csring->fun) |
1455 XCT_FUN_LLEN(2) | XCT_FUN_SE;
1456 CS_DESC(csring, fill++) = XCT_PTR_LEN(2) | XCT_PTR_ADDR(cs_dest) | XCT_PTR_T;
1457 CS_DESC(csring, fill++) = XCT_PTR_LEN(2) | XCT_PTR_ADDR(csdma);
1458 fill++;
1459
1460 evt = !csring->last_event;
1461 csring->last_event = evt;
1462
1463 /* Event handshaking with MAC TX */
1464 CS_DESC(csring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
1465 CTRL_CMD_ETYPE_SET | CTRL_CMD_REG(csring->events[evt]);
1466 CS_DESC(csring, fill++) = 0;
1467 CS_DESC(csring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
1468 CTRL_CMD_ETYPE_WCLR | CTRL_CMD_REG(csring->events[!evt]);
1469 CS_DESC(csring, fill++) = 0;
1470 csring->next_to_fill = fill & (CS_RING_SIZE-1);
1471
1472 cs_size = fill - hdr;
1473 write_dma_reg(PAS_DMA_TXCHAN_INCR(csring->chan.chno), (cs_size) >> 1);
1474
1475 /* TX-side event handshaking */
1476 fill = txring->next_to_fill;
1477 TX_DESC(txring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
1478 CTRL_CMD_ETYPE_WSET | CTRL_CMD_REG(csring->events[evt]);
1479 TX_DESC(txring, fill++) = 0;
1480 TX_DESC(txring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
1481 CTRL_CMD_ETYPE_CLR | CTRL_CMD_REG(csring->events[!evt]);
1482 TX_DESC(txring, fill++) = 0;
1483 txring->next_to_fill = fill;
1484
1485 write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), 2);
1486
1487 return;
1488}
1489
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001490static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1491{
Olof Johansson8d636d82008-03-05 16:34:16 -06001492 struct pasemi_mac * const mac = netdev_priv(dev);
1493 struct pasemi_mac_txring * const txring = tx_ring(mac);
1494 struct pasemi_mac_csring *csring;
1495 u64 dflags = 0;
1496 u64 mactx;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001497 dma_addr_t map[MAX_SKB_FRAGS+1];
1498 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001499 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001500 int i, nfrags;
Olof Johansson5c153322007-11-28 20:56:41 -06001501 int fill;
Olof Johansson8d636d82008-03-05 16:34:16 -06001502 const int nh_off = skb_network_offset(skb);
1503 const int nh_len = skb_network_header_len(skb);
1504
1505 prefetch(&txring->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001506
Olof Johanssondbd62af2007-11-06 22:20:39 -06001507 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001508
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001509 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001510
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001511 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1512 PCI_DMA_TODEVICE);
1513 map_size[0] = skb_headlen(skb);
1514 if (dma_mapping_error(map[0]))
1515 goto out_err_nolock;
1516
1517 for (i = 0; i < nfrags; i++) {
1518 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1519
1520 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1521 frag->page_offset, frag->size,
1522 PCI_DMA_TODEVICE);
1523 map_size[i+1] = frag->size;
1524 if (dma_mapping_error(map[i+1])) {
1525 nfrags = i;
1526 goto out_err_nolock;
1527 }
1528 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001529
Olof Johansson8d636d82008-03-05 16:34:16 -06001530 if (skb->ip_summed == CHECKSUM_PARTIAL && skb->len <= 1540) {
1531 switch (ip_hdr(skb)->protocol) {
1532 case IPPROTO_TCP:
1533 dflags |= XCT_MACTX_CSUM_TCP;
1534 dflags |= XCT_MACTX_IPH(nh_len >> 2);
1535 dflags |= XCT_MACTX_IPO(nh_off);
1536 break;
1537 case IPPROTO_UDP:
1538 dflags |= XCT_MACTX_CSUM_UDP;
1539 dflags |= XCT_MACTX_IPH(nh_len >> 2);
1540 dflags |= XCT_MACTX_IPO(nh_off);
1541 break;
1542 default:
1543 WARN_ON(1);
1544 }
1545 }
1546
Olof Johansson26fcfa92007-08-22 09:12:59 -05001547 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001548
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001549 spin_lock_irqsave(&txring->lock, flags);
1550
Olof Johanssonad5da102007-10-02 16:27:15 -05001551 /* Avoid stepping on the same cache line that the DMA controller
1552 * is currently about to send, so leave at least 8 words available.
1553 * Total free space needed is mactx + fragments + 8
1554 */
Olof Johansson8d636d82008-03-05 16:34:16 -06001555 if (RING_AVAIL(txring) < nfrags + 14) {
Olof Johanssonad5da102007-10-02 16:27:15 -05001556 /* no room -- stop the queue and wait for tx intr */
1557 netif_stop_queue(dev);
1558 goto out_err;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001559 }
1560
Olof Johansson8d636d82008-03-05 16:34:16 -06001561 /* Queue up checksum + event descriptors, if needed */
1562 if (mac->num_cs && skb->ip_summed == CHECKSUM_PARTIAL && skb->len > 1540) {
1563 csring = mac->cs[mac->last_cs];
1564 mac->last_cs = (mac->last_cs + 1) % mac->num_cs;
1565
1566 pasemi_mac_queue_csdesc(skb, map, map_size, txring, csring);
1567 }
1568
1569 fill = txring->next_to_fill;
Olof Johansson5c153322007-11-28 20:56:41 -06001570 TX_DESC(txring, fill) = mactx;
Olof Johansson7e9916e2007-11-28 20:57:45 -06001571 TX_DESC_INFO(txring, fill).dma = nfrags;
Olof Johansson5c153322007-11-28 20:56:41 -06001572 fill++;
1573 TX_DESC_INFO(txring, fill).skb = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001574 for (i = 0; i <= nfrags; i++) {
Olof Johansson5c153322007-11-28 20:56:41 -06001575 TX_DESC(txring, fill+i) =
Olof Johansson72b05b92007-11-28 20:54:28 -06001576 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
Olof Johansson5c153322007-11-28 20:56:41 -06001577 TX_DESC_INFO(txring, fill+i).dma = map[i];
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001578 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001579
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001580 /* We have to add an even number of 8-byte entries to the ring
1581 * even if the last one is unused. That means always an odd number
1582 * of pointers + one mactx descriptor.
1583 */
1584 if (nfrags & 1)
1585 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001586
Olof Johansson5c153322007-11-28 20:56:41 -06001587 txring->next_to_fill = (fill + nfrags + 1) & (TX_RING_SIZE-1);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001588
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001589 dev->stats.tx_packets++;
1590 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001591
1592 spin_unlock_irqrestore(&txring->lock, flags);
1593
Olof Johansson34c20622007-11-28 20:56:32 -06001594 write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001595
1596 return NETDEV_TX_OK;
1597
1598out_err:
1599 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001600out_err_nolock:
1601 while (nfrags--)
1602 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1603 PCI_DMA_TODEVICE);
1604
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001605 return NETDEV_TX_BUSY;
1606}
1607
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001608static void pasemi_mac_set_rx_mode(struct net_device *dev)
1609{
Olof Johansson5c153322007-11-28 20:56:41 -06001610 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001611 unsigned int flags;
1612
Olof Johanssona85b9422007-09-15 13:40:59 -07001613 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001614
1615 /* Set promiscuous */
1616 if (dev->flags & IFF_PROMISC)
1617 flags |= PAS_MAC_CFG_PCFG_PR;
1618 else
1619 flags &= ~PAS_MAC_CFG_PCFG_PR;
1620
Olof Johanssona85b9422007-09-15 13:40:59 -07001621 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001622}
1623
1624
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001625static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001626{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001627 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1628 struct net_device *dev = mac->netdev;
1629 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001630
Olof Johansson72b05b92007-11-28 20:54:28 -06001631 pasemi_mac_clean_tx(tx_ring(mac));
1632 pkts = pasemi_mac_clean_rx(rx_ring(mac), budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001633 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001634 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001635 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001636
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001637 pasemi_mac_restart_rx_intr(mac);
Olof Johansson61cec3b2007-11-28 20:56:54 -06001638 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001639 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001640 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001641}
1642
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001643static int pasemi_mac_change_mtu(struct net_device *dev, int new_mtu)
1644{
1645 struct pasemi_mac *mac = netdev_priv(dev);
1646 unsigned int reg;
Olof Johansson8d636d82008-03-05 16:34:16 -06001647 unsigned int rcmdsta = 0;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001648 int running;
Olof Johansson8d636d82008-03-05 16:34:16 -06001649 int ret = 0;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001650
1651 if (new_mtu < PE_MIN_MTU || new_mtu > PE_MAX_MTU)
1652 return -EINVAL;
1653
1654 running = netif_running(dev);
1655
1656 if (running) {
1657 /* Need to stop the interface, clean out all already
1658 * received buffers, free all unused buffers on the RX
1659 * interface ring, then finally re-fill the rx ring with
1660 * the new-size buffers and restart.
1661 */
1662
1663 napi_disable(&mac->napi);
1664 netif_tx_disable(dev);
1665 pasemi_mac_intf_disable(mac);
1666
1667 rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1668 pasemi_mac_pause_rxint(mac);
1669 pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
1670 pasemi_mac_free_rx_buffers(mac);
Olof Johansson8d636d82008-03-05 16:34:16 -06001671
1672 }
1673
1674 /* Setup checksum channels if large MTU and none already allocated */
1675 if (new_mtu > 1500 && !mac->num_cs) {
1676 pasemi_mac_setup_csrings(mac);
1677 if (!mac->num_cs) {
1678 ret = -ENOMEM;
1679 goto out;
1680 }
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001681 }
1682
1683 /* Change maxf, i.e. what size frames are accepted.
1684 * Need room for ethernet header and CRC word
1685 */
1686 reg = read_mac_reg(mac, PAS_MAC_CFG_MACCFG);
1687 reg &= ~PAS_MAC_CFG_MACCFG_MAXF_M;
1688 reg |= PAS_MAC_CFG_MACCFG_MAXF(new_mtu + ETH_HLEN + 4);
1689 write_mac_reg(mac, PAS_MAC_CFG_MACCFG, reg);
1690
1691 dev->mtu = new_mtu;
1692 /* MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
1693 mac->bufsz = new_mtu + ETH_HLEN + ETH_FCS_LEN + LOCAL_SKB_ALIGN + 128;
1694
Olof Johansson8d636d82008-03-05 16:34:16 -06001695out:
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001696 if (running) {
1697 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1698 rcmdsta | PAS_DMA_RXINT_RCMDSTA_EN);
1699
1700 rx_ring(mac)->next_to_fill = 0;
1701 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE-1);
1702
1703 napi_enable(&mac->napi);
1704 netif_start_queue(dev);
1705 pasemi_mac_intf_enable(mac);
1706 }
1707
Olof Johansson8d636d82008-03-05 16:34:16 -06001708 return ret;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001709}
1710
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001711static int __devinit
1712pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1713{
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001714 struct net_device *dev;
1715 struct pasemi_mac *mac;
1716 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001717 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001718
1719 err = pci_enable_device(pdev);
1720 if (err)
1721 return err;
1722
1723 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1724 if (dev == NULL) {
1725 dev_err(&pdev->dev,
1726 "pasemi_mac: Could not allocate ethernet device.\n");
1727 err = -ENOMEM;
1728 goto out_disable_device;
1729 }
1730
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001731 pci_set_drvdata(pdev, dev);
1732 SET_NETDEV_DEV(dev, &pdev->dev);
1733
1734 mac = netdev_priv(dev);
1735
1736 mac->pdev = pdev;
1737 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001738
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001739 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1740
Olof Johansson5c153322007-11-28 20:56:41 -06001741 dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG |
Olof Johansson25156782008-02-20 20:57:58 -06001742 NETIF_F_HIGHDMA | NETIF_F_GSO;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001743
Olof Johansson28ae79f2007-11-28 20:57:27 -06001744 mac->lro_mgr.max_aggr = LRO_MAX_AGGR;
1745 mac->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
1746 mac->lro_mgr.lro_arr = mac->lro_desc;
1747 mac->lro_mgr.get_skb_header = get_skb_hdr;
1748 mac->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1749 mac->lro_mgr.dev = mac->netdev;
1750 mac->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1751 mac->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1752
1753
Olof Johansson34c20622007-11-28 20:56:32 -06001754 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1755 if (!mac->dma_pdev) {
1756 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1757 err = -ENODEV;
1758 goto out;
1759 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001760
Olof Johansson34c20622007-11-28 20:56:32 -06001761 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1762 if (!mac->iob_pdev) {
1763 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1764 err = -ENODEV;
1765 goto out;
1766 }
1767
1768 /* get mac addr from device tree */
1769 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1770 err = -ENODEV;
1771 goto out;
1772 }
1773 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1774
1775 mac->dma_if = mac_to_intf(mac);
1776 if (mac->dma_if < 0) {
1777 dev_err(&mac->pdev->dev, "Can't map DMA interface\n");
1778 err = -ENODEV;
1779 goto out;
1780 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001781
1782 switch (pdev->device) {
1783 case 0xa005:
1784 mac->type = MAC_TYPE_GMAC;
1785 break;
1786 case 0xa006:
1787 mac->type = MAC_TYPE_XAUI;
1788 break;
1789 default:
1790 err = -ENODEV;
1791 goto out;
1792 }
1793
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001794 dev->open = pasemi_mac_open;
1795 dev->stop = pasemi_mac_close;
1796 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001797 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johansson5cea73b2008-01-23 13:56:19 -06001798 dev->set_mac_address = pasemi_mac_set_mac_addr;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001799 dev->mtu = PE_DEF_MTU;
1800 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
1801 mac->bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + LOCAL_SKB_ALIGN + 128;
1802
1803 dev->change_mtu = pasemi_mac_change_mtu;
Olof Johanssone37c7722008-02-20 20:57:59 -06001804 dev->ethtool_ops = &pasemi_mac_ethtool_ops;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001805
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001806 if (err)
1807 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001808
Olof Johanssonceb51362007-05-08 00:47:49 -05001809 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1810
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001811 /* Enable most messages by default */
1812 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1813
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001814 err = register_netdev(dev);
1815
1816 if (err) {
1817 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1818 err);
1819 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001820 } else if netif_msg_probe(mac)
Olof Johansson72b05b92007-11-28 20:54:28 -06001821 printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001822 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
Olof Johansson72b05b92007-11-28 20:54:28 -06001823 mac->dma_if, print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001824
1825 return err;
1826
1827out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001828 if (mac->iob_pdev)
1829 pci_dev_put(mac->iob_pdev);
1830 if (mac->dma_pdev)
1831 pci_dev_put(mac->dma_pdev);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001832
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001833 free_netdev(dev);
1834out_disable_device:
1835 pci_disable_device(pdev);
1836 return err;
1837
1838}
1839
1840static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1841{
1842 struct net_device *netdev = pci_get_drvdata(pdev);
1843 struct pasemi_mac *mac;
1844
1845 if (!netdev)
1846 return;
1847
1848 mac = netdev_priv(netdev);
1849
1850 unregister_netdev(netdev);
1851
1852 pci_disable_device(pdev);
1853 pci_dev_put(mac->dma_pdev);
1854 pci_dev_put(mac->iob_pdev);
1855
Olof Johansson34c20622007-11-28 20:56:32 -06001856 pasemi_dma_free_chan(&mac->tx->chan);
1857 pasemi_dma_free_chan(&mac->rx->chan);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001858
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001859 pci_set_drvdata(pdev, NULL);
1860 free_netdev(netdev);
1861}
1862
1863static struct pci_device_id pasemi_mac_pci_tbl[] = {
1864 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1865 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001866 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001867};
1868
1869MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1870
1871static struct pci_driver pasemi_mac_driver = {
1872 .name = "pasemi_mac",
1873 .id_table = pasemi_mac_pci_tbl,
1874 .probe = pasemi_mac_probe,
1875 .remove = __devexit_p(pasemi_mac_remove),
1876};
1877
1878static void __exit pasemi_mac_cleanup_module(void)
1879{
1880 pci_unregister_driver(&pasemi_mac_driver);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001881}
1882
1883int pasemi_mac_init_module(void)
1884{
Olof Johansson34c20622007-11-28 20:56:32 -06001885 int err;
1886
1887 err = pasemi_dma_init();
1888 if (err)
1889 return err;
1890
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001891 return pci_register_driver(&pasemi_mac_driver);
1892}
1893
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001894module_init(pasemi_mac_init_module);
1895module_exit(pasemi_mac_cleanup_module);