blob: 54904ad29ea7336cf26457b56fe7de7ad4ef59e8 [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 Johansson8d636d82008-03-05 16:34:16 -060062#define CS_RING_SIZE (TX_RING_SIZE*2)
Olof Johanssonf5cd7872007-01-31 21:43:54 -060063
Olof Johansson28ae79f2007-11-28 20:57:27 -060064#define LRO_MAX_AGGR 64
65
Olof Johanssonef1ea0b2008-01-23 13:56:47 -060066#define PE_MIN_MTU 64
Olof Johansson8d636d82008-03-05 16:34:16 -060067#define PE_MAX_MTU 9000
Olof Johanssonef1ea0b2008-01-23 13:56:47 -060068#define PE_DEF_MTU ETH_DATA_LEN
69
Olof Johanssonceb51362007-05-08 00:47:49 -050070#define DEFAULT_MSG_ENABLE \
71 (NETIF_MSG_DRV | \
72 NETIF_MSG_PROBE | \
73 NETIF_MSG_LINK | \
74 NETIF_MSG_TIMER | \
75 NETIF_MSG_IFDOWN | \
76 NETIF_MSG_IFUP | \
77 NETIF_MSG_RX_ERR | \
78 NETIF_MSG_TX_ERR)
79
Olof Johansson34c20622007-11-28 20:56:32 -060080#define TX_DESC(tx, num) ((tx)->chan.ring_virt[(num) & (TX_RING_SIZE-1)])
Olof Johansson72b05b92007-11-28 20:54:28 -060081#define TX_DESC_INFO(tx, num) ((tx)->ring_info[(num) & (TX_RING_SIZE-1)])
Olof Johansson34c20622007-11-28 20:56:32 -060082#define RX_DESC(rx, num) ((rx)->chan.ring_virt[(num) & (RX_RING_SIZE-1)])
Olof Johansson72b05b92007-11-28 20:54:28 -060083#define RX_DESC_INFO(rx, num) ((rx)->ring_info[(num) & (RX_RING_SIZE-1)])
84#define RX_BUFF(rx, num) ((rx)->buffers[(num) & (RX_RING_SIZE-1)])
Olof Johansson8d636d82008-03-05 16:34:16 -060085#define CS_DESC(cs, num) ((cs)->chan.ring_virt[(num) & (CS_RING_SIZE-1)])
Olof Johanssonf5cd7872007-01-31 21:43:54 -060086
Olof Johansson021fa222007-08-22 09:13:11 -050087#define RING_USED(ring) (((ring)->next_to_fill - (ring)->next_to_clean) \
88 & ((ring)->size - 1))
89#define RING_AVAIL(ring) ((ring->size) - RING_USED(ring))
90
Olof Johanssonceb51362007-05-08 00:47:49 -050091MODULE_LICENSE("GPL");
92MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
93MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
94
95static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
96module_param(debug, int, 0);
97MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060098
Olof Johanssonaf289e82007-10-03 13:03:54 -050099static int translation_enabled(void)
100{
101#if defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
102 return 1;
103#else
104 return firmware_has_feature(FW_FEATURE_LPAR);
105#endif
106}
107
Olof Johansson34c20622007-11-28 20:56:32 -0600108static void write_iob_reg(unsigned int reg, unsigned int val)
Olof Johanssona85b9422007-09-15 13:40:59 -0700109{
Olof Johansson34c20622007-11-28 20:56:32 -0600110 pasemi_write_iob_reg(reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700111}
112
Olof Johansson5c153322007-11-28 20:56:41 -0600113static unsigned int read_mac_reg(const struct pasemi_mac *mac, unsigned int reg)
Olof Johanssona85b9422007-09-15 13:40:59 -0700114{
Olof Johansson34c20622007-11-28 20:56:32 -0600115 return pasemi_read_mac_reg(mac->dma_if, reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700116}
117
Olof Johansson5c153322007-11-28 20:56:41 -0600118static void write_mac_reg(const struct pasemi_mac *mac, unsigned int reg,
Olof Johanssona85b9422007-09-15 13:40:59 -0700119 unsigned int val)
120{
Olof Johansson34c20622007-11-28 20:56:32 -0600121 pasemi_write_mac_reg(mac->dma_if, reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700122}
123
Olof Johansson34c20622007-11-28 20:56:32 -0600124static unsigned int read_dma_reg(unsigned int reg)
Olof Johanssona85b9422007-09-15 13:40:59 -0700125{
Olof Johansson34c20622007-11-28 20:56:32 -0600126 return pasemi_read_dma_reg(reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700127}
128
Olof Johansson34c20622007-11-28 20:56:32 -0600129static void write_dma_reg(unsigned int reg, unsigned int val)
Olof Johanssona85b9422007-09-15 13:40:59 -0700130{
Olof Johansson34c20622007-11-28 20:56:32 -0600131 pasemi_write_dma_reg(reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700132}
133
Olof Johansson5c153322007-11-28 20:56:41 -0600134static struct pasemi_mac_rxring *rx_ring(const struct pasemi_mac *mac)
Olof Johansson72b05b92007-11-28 20:54:28 -0600135{
136 return mac->rx;
137}
138
Olof Johansson5c153322007-11-28 20:56:41 -0600139static struct pasemi_mac_txring *tx_ring(const struct pasemi_mac *mac)
Olof Johansson72b05b92007-11-28 20:54:28 -0600140{
141 return mac->tx;
142}
143
Olof Johansson5c153322007-11-28 20:56:41 -0600144static inline void prefetch_skb(const struct sk_buff *skb)
145{
146 const void *d = skb;
147
148 prefetch(d);
149 prefetch(d+64);
150 prefetch(d+128);
151 prefetch(d+192);
152}
153
Olof Johansson34c20622007-11-28 20:56:32 -0600154static int mac_to_intf(struct pasemi_mac *mac)
155{
156 struct pci_dev *pdev = mac->pdev;
157 u32 tmp;
158 int nintf, off, i, j;
159 int devfn = pdev->devfn;
160
161 tmp = read_dma_reg(PAS_DMA_CAP_IFI);
162 nintf = (tmp & PAS_DMA_CAP_IFI_NIN_M) >> PAS_DMA_CAP_IFI_NIN_S;
163 off = (tmp & PAS_DMA_CAP_IFI_IOFF_M) >> PAS_DMA_CAP_IFI_IOFF_S;
164
165 /* IOFF contains the offset to the registers containing the
166 * DMA interface-to-MAC-pci-id mappings, and NIN contains number
167 * of total interfaces. Each register contains 4 devfns.
168 * Just do a linear search until we find the devfn of the MAC
169 * we're trying to look up.
170 */
171
172 for (i = 0; i < (nintf+3)/4; i++) {
173 tmp = read_dma_reg(off+4*i);
174 for (j = 0; j < 4; j++) {
175 if (((tmp >> (8*j)) & 0xff) == devfn)
176 return i*4 + j;
177 }
178 }
179 return -1;
180}
181
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600182static void pasemi_mac_intf_disable(struct pasemi_mac *mac)
183{
184 unsigned int flags;
185
186 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
187 flags &= ~PAS_MAC_CFG_PCFG_PE;
188 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
189}
190
191static void pasemi_mac_intf_enable(struct pasemi_mac *mac)
192{
193 unsigned int flags;
194
195 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
196 flags |= PAS_MAC_CFG_PCFG_PE;
197 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
198}
199
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600200static int pasemi_get_mac_addr(struct pasemi_mac *mac)
201{
202 struct pci_dev *pdev = mac->pdev;
203 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500204 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600205 const u8 *maddr;
206 u8 addr[6];
207
208 if (!dn) {
209 dev_dbg(&pdev->dev,
210 "No device node for mac, not configuring\n");
211 return -ENOENT;
212 }
213
olof@lixom.net1af7f052007-05-12 14:57:46 -0500214 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500215
olof@lixom.net1af7f052007-05-12 14:57:46 -0500216 if (maddr && len == 6) {
217 memcpy(mac->mac_addr, maddr, 6);
218 return 0;
219 }
220
221 /* Some old versions of firmware mistakenly uses mac-address
222 * (and as a string) instead of a byte array in local-mac-address.
223 */
224
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500225 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700226 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500227
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600228 if (maddr == NULL) {
229 dev_warn(&pdev->dev,
230 "no mac address in device tree, not configuring\n");
231 return -ENOENT;
232 }
233
234 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
235 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
236 dev_warn(&pdev->dev,
237 "can't parse mac address, not configuring\n");
238 return -EINVAL;
239 }
240
olof@lixom.net1af7f052007-05-12 14:57:46 -0500241 memcpy(mac->mac_addr, addr, 6);
242
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600243 return 0;
244}
245
Olof Johansson5cea73b2008-01-23 13:56:19 -0600246static int pasemi_mac_set_mac_addr(struct net_device *dev, void *p)
247{
248 struct pasemi_mac *mac = netdev_priv(dev);
249 struct sockaddr *addr = p;
250 unsigned int adr0, adr1;
251
252 if (!is_valid_ether_addr(addr->sa_data))
253 return -EINVAL;
254
255 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
256
257 adr0 = dev->dev_addr[2] << 24 |
258 dev->dev_addr[3] << 16 |
259 dev->dev_addr[4] << 8 |
260 dev->dev_addr[5];
261 adr1 = read_mac_reg(mac, PAS_MAC_CFG_ADR1);
262 adr1 &= ~0xffff;
263 adr1 |= dev->dev_addr[0] << 8 | dev->dev_addr[1];
264
265 pasemi_mac_intf_disable(mac);
266 write_mac_reg(mac, PAS_MAC_CFG_ADR0, adr0);
267 write_mac_reg(mac, PAS_MAC_CFG_ADR1, adr1);
268 pasemi_mac_intf_enable(mac);
269
270 return 0;
271}
272
Olof Johansson28ae79f2007-11-28 20:57:27 -0600273static int get_skb_hdr(struct sk_buff *skb, void **iphdr,
274 void **tcph, u64 *hdr_flags, void *data)
275{
276 u64 macrx = (u64) data;
277 unsigned int ip_len;
278 struct iphdr *iph;
279
280 /* IPv4 header checksum failed */
281 if ((macrx & XCT_MACRX_HTY_M) != XCT_MACRX_HTY_IPV4_OK)
282 return -1;
283
284 /* non tcp packet */
285 skb_reset_network_header(skb);
286 iph = ip_hdr(skb);
287 if (iph->protocol != IPPROTO_TCP)
288 return -1;
289
290 ip_len = ip_hdrlen(skb);
291 skb_set_transport_header(skb, ip_len);
292 *tcph = tcp_hdr(skb);
293
294 /* check if ip header and tcp header are complete */
295 if (iph->tot_len < ip_len + tcp_hdrlen(skb))
296 return -1;
297
298 *hdr_flags = LRO_IPV4 | LRO_TCP;
299 *iphdr = iph;
300
301 return 0;
302}
303
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500304static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
Olof Johansson7e9916e2007-11-28 20:57:45 -0600305 const int nfrags,
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500306 struct sk_buff *skb,
Olof Johansson5c153322007-11-28 20:56:41 -0600307 const dma_addr_t *dmas)
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500308{
309 int f;
Olof Johansson5c153322007-11-28 20:56:41 -0600310 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500311
Olof Johansson5c153322007-11-28 20:56:41 -0600312 pci_unmap_single(pdev, dmas[0], skb_headlen(skb), PCI_DMA_TODEVICE);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500313
314 for (f = 0; f < nfrags; f++) {
315 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
316
Olof Johansson5c153322007-11-28 20:56:41 -0600317 pci_unmap_page(pdev, dmas[f+1], frag->size, PCI_DMA_TODEVICE);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500318 }
319 dev_kfree_skb_irq(skb);
320
321 /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
322 * aligned up to a power of 2
323 */
324 return (nfrags + 3) & ~1;
325}
326
Olof Johansson8d636d82008-03-05 16:34:16 -0600327static struct pasemi_mac_csring *pasemi_mac_setup_csring(struct pasemi_mac *mac)
328{
329 struct pasemi_mac_csring *ring;
330 u32 val;
331 unsigned int cfg;
332 int chno;
333
334 ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_csring),
335 offsetof(struct pasemi_mac_csring, chan));
336
337 if (!ring) {
338 dev_err(&mac->pdev->dev, "Can't allocate checksum channel\n");
339 goto out_chan;
340 }
341
342 chno = ring->chan.chno;
343
344 ring->size = CS_RING_SIZE;
345 ring->next_to_fill = 0;
346
347 /* Allocate descriptors */
348 if (pasemi_dma_alloc_ring(&ring->chan, CS_RING_SIZE))
349 goto out_ring_desc;
350
351 write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno),
352 PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma));
353 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32);
354 val |= PAS_DMA_TXCHAN_BASEU_SIZ(CS_RING_SIZE >> 3);
355
356 write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val);
357
358 ring->events[0] = pasemi_dma_alloc_flag();
359 ring->events[1] = pasemi_dma_alloc_flag();
360 if (ring->events[0] < 0 || ring->events[1] < 0)
361 goto out_flags;
362
363 pasemi_dma_clear_flag(ring->events[0]);
364 pasemi_dma_clear_flag(ring->events[1]);
365
366 ring->fun = pasemi_dma_alloc_fun();
367 if (ring->fun < 0)
368 goto out_fun;
369
370 cfg = PAS_DMA_TXCHAN_CFG_TY_FUNC | PAS_DMA_TXCHAN_CFG_UP |
371 PAS_DMA_TXCHAN_CFG_TATTR(ring->fun) |
372 PAS_DMA_TXCHAN_CFG_LPSQ | PAS_DMA_TXCHAN_CFG_LPDQ;
373
374 if (translation_enabled())
375 cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
376
377 write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg);
378
379 /* enable channel */
380 pasemi_dma_start_chan(&ring->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
381 PAS_DMA_TXCHAN_TCMDSTA_DB |
382 PAS_DMA_TXCHAN_TCMDSTA_DE |
383 PAS_DMA_TXCHAN_TCMDSTA_DA);
384
385 return ring;
386
387out_fun:
388out_flags:
389 if (ring->events[0] >= 0)
390 pasemi_dma_free_flag(ring->events[0]);
391 if (ring->events[1] >= 0)
392 pasemi_dma_free_flag(ring->events[1]);
393 pasemi_dma_free_ring(&ring->chan);
394out_ring_desc:
395 pasemi_dma_free_chan(&ring->chan);
396out_chan:
397
398 return NULL;
399}
400
401static void pasemi_mac_setup_csrings(struct pasemi_mac *mac)
402{
403 int i;
404 mac->cs[0] = pasemi_mac_setup_csring(mac);
405 if (mac->type == MAC_TYPE_XAUI)
406 mac->cs[1] = pasemi_mac_setup_csring(mac);
407 else
408 mac->cs[1] = 0;
409
410 for (i = 0; i < MAX_CS; i++)
411 if (mac->cs[i])
412 mac->num_cs++;
413}
414
415static void pasemi_mac_free_csring(struct pasemi_mac_csring *csring)
416{
417 pasemi_dma_stop_chan(&csring->chan);
418 pasemi_dma_free_flag(csring->events[0]);
419 pasemi_dma_free_flag(csring->events[1]);
420 pasemi_dma_free_ring(&csring->chan);
421 pasemi_dma_free_chan(&csring->chan);
422}
423
Olof Johansson5c153322007-11-28 20:56:41 -0600424static int pasemi_mac_setup_rx_resources(const struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600425{
426 struct pasemi_mac_rxring *ring;
427 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson34c20622007-11-28 20:56:32 -0600428 int chno;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500429 unsigned int cfg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600430
Olof Johansson34c20622007-11-28 20:56:32 -0600431 ring = pasemi_dma_alloc_chan(RXCHAN, sizeof(struct pasemi_mac_rxring),
432 offsetof(struct pasemi_mac_rxring, chan));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600433
Olof Johansson34c20622007-11-28 20:56:32 -0600434 if (!ring) {
435 dev_err(&mac->pdev->dev, "Can't allocate RX channel\n");
436 goto out_chan;
437 }
438 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600439
440 spin_lock_init(&ring->lock);
441
Olof Johansson021fa222007-08-22 09:13:11 -0500442 ring->size = RX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500443 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600444 RX_RING_SIZE, GFP_KERNEL);
445
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500446 if (!ring->ring_info)
447 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600448
449 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600450 if (pasemi_dma_alloc_ring(&ring->chan, RX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500451 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600452
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600453 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
454 RX_RING_SIZE * sizeof(u64),
455 &ring->buf_dma, GFP_KERNEL);
456 if (!ring->buffers)
Olof Johansson34c20622007-11-28 20:56:32 -0600457 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600458
459 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
460
Olof Johansson34c20622007-11-28 20:56:32 -0600461 write_dma_reg(PAS_DMA_RXCHAN_BASEL(chno),
462 PAS_DMA_RXCHAN_BASEL_BRBL(ring->chan.ring_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600463
Olof Johansson34c20622007-11-28 20:56:32 -0600464 write_dma_reg(PAS_DMA_RXCHAN_BASEU(chno),
465 PAS_DMA_RXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32) |
466 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600467
Olof Johansson5c153322007-11-28 20:56:41 -0600468 cfg = PAS_DMA_RXCHAN_CFG_HBU(2);
Olof Johanssonaf289e82007-10-03 13:03:54 -0500469
470 if (translation_enabled())
471 cfg |= PAS_DMA_RXCHAN_CFG_CTR;
472
Olof Johansson34c20622007-11-28 20:56:32 -0600473 write_dma_reg(PAS_DMA_RXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600474
Olof Johansson34c20622007-11-28 20:56:32 -0600475 write_dma_reg(PAS_DMA_RXINT_BASEL(mac->dma_if),
476 PAS_DMA_RXINT_BASEL_BRBL(ring->buf_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600477
Olof Johansson34c20622007-11-28 20:56:32 -0600478 write_dma_reg(PAS_DMA_RXINT_BASEU(mac->dma_if),
479 PAS_DMA_RXINT_BASEU_BRBH(ring->buf_dma >> 32) |
480 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600481
Olof Johansson5c153322007-11-28 20:56:41 -0600482 cfg = PAS_DMA_RXINT_CFG_DHL(2) | PAS_DMA_RXINT_CFG_L2 |
Olof Johanssonaf289e82007-10-03 13:03:54 -0500483 PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP |
484 PAS_DMA_RXINT_CFG_HEN;
485
486 if (translation_enabled())
487 cfg |= PAS_DMA_RXINT_CFG_ITRR | PAS_DMA_RXINT_CFG_ITR;
488
Olof Johansson34c20622007-11-28 20:56:32 -0600489 write_dma_reg(PAS_DMA_RXINT_CFG(mac->dma_if), cfg);
Olof Johanssonc0efd522007-08-22 09:12:52 -0500490
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600491 ring->next_to_fill = 0;
492 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600493 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600494 mac->rx = ring;
495
496 return 0;
497
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500498out_ring_desc:
499 kfree(ring->ring_info);
500out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600501 pasemi_dma_free_chan(&ring->chan);
502out_chan:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600503 return -ENOMEM;
504}
505
Olof Johansson72b05b92007-11-28 20:54:28 -0600506static struct pasemi_mac_txring *
Olof Johansson5c153322007-11-28 20:56:41 -0600507pasemi_mac_setup_tx_resources(const struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600508{
509 struct pasemi_mac *mac = netdev_priv(dev);
510 u32 val;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600511 struct pasemi_mac_txring *ring;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500512 unsigned int cfg;
Olof Johansson34c20622007-11-28 20:56:32 -0600513 int chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600514
Olof Johansson34c20622007-11-28 20:56:32 -0600515 ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_txring),
516 offsetof(struct pasemi_mac_txring, chan));
517
518 if (!ring) {
519 dev_err(&mac->pdev->dev, "Can't allocate TX channel\n");
520 goto out_chan;
521 }
522
523 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600524
525 spin_lock_init(&ring->lock);
526
Olof Johansson021fa222007-08-22 09:13:11 -0500527 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500528 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600529 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500530 if (!ring->ring_info)
531 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600532
533 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600534 if (pasemi_dma_alloc_ring(&ring->chan, TX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500535 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600536
Olof Johansson34c20622007-11-28 20:56:32 -0600537 write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno),
538 PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma));
539 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500540 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600541
Olof Johansson34c20622007-11-28 20:56:32 -0600542 write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600543
Olof Johanssonaf289e82007-10-03 13:03:54 -0500544 cfg = PAS_DMA_TXCHAN_CFG_TY_IFACE |
545 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
546 PAS_DMA_TXCHAN_CFG_UP |
Olof Johansson8d636d82008-03-05 16:34:16 -0600547 PAS_DMA_TXCHAN_CFG_WT(4);
Olof Johanssonaf289e82007-10-03 13:03:54 -0500548
549 if (translation_enabled())
550 cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
551
Olof Johansson34c20622007-11-28 20:56:32 -0600552 write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600553
Olof Johansson021fa222007-08-22 09:13:11 -0500554 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600555 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600556 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600557
Olof Johansson72b05b92007-11-28 20:54:28 -0600558 return ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600559
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500560out_ring_desc:
561 kfree(ring->ring_info);
562out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600563 pasemi_dma_free_chan(&ring->chan);
564out_chan:
Olof Johansson72b05b92007-11-28 20:54:28 -0600565 return NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600566}
567
Olof Johansson72b05b92007-11-28 20:54:28 -0600568static void pasemi_mac_free_tx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600569{
Olof Johansson72b05b92007-11-28 20:54:28 -0600570 struct pasemi_mac_txring *txring = tx_ring(mac);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500571 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600572 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500573 dma_addr_t dmas[MAX_SKB_FRAGS+1];
Olof Johansson7e9916e2007-11-28 20:57:45 -0600574 int freed, nfrags;
Olof Johanssonad5da102007-10-02 16:27:15 -0500575 int start, limit;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600576
Olof Johansson72b05b92007-11-28 20:54:28 -0600577 start = txring->next_to_clean;
578 limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500579
580 /* Compensate for when fill has wrapped and clean has not */
581 if (start > limit)
582 limit += TX_RING_SIZE;
583
584 for (i = start; i < limit; i += freed) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600585 info = &txring->ring_info[(i+1) & (TX_RING_SIZE-1)];
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500586 if (info->dma && info->skb) {
Olof Johansson7e9916e2007-11-28 20:57:45 -0600587 nfrags = skb_shinfo(info->skb)->nr_frags;
588 for (j = 0; j <= nfrags; j++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600589 dmas[j] = txring->ring_info[(i+1+j) &
590 (TX_RING_SIZE-1)].dma;
Olof Johansson7e9916e2007-11-28 20:57:45 -0600591 freed = pasemi_mac_unmap_tx_skb(mac, nfrags,
592 info->skb, dmas);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500593 } else
594 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600595 }
596
Olof Johansson72b05b92007-11-28 20:54:28 -0600597 kfree(txring->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600598 pasemi_dma_free_chan(&txring->chan);
599
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600600}
601
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600602static void pasemi_mac_free_rx_buffers(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600603{
Olof Johansson72b05b92007-11-28 20:54:28 -0600604 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600605 unsigned int i;
606 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600607
608 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600609 info = &RX_DESC_INFO(rx, i);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500610 if (info->skb && info->dma) {
611 pci_unmap_single(mac->dma_pdev,
612 info->dma,
613 info->skb->len,
614 PCI_DMA_FROMDEVICE);
615 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600616 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500617 info->dma = 0;
618 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600619 }
620
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500621 for (i = 0; i < RX_RING_SIZE; i++)
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600622 RX_BUFF(rx, i) = 0;
623}
624
625static void pasemi_mac_free_rx_resources(struct pasemi_mac *mac)
626{
627 pasemi_mac_free_rx_buffers(mac);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500628
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600629 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
Olof Johansson72b05b92007-11-28 20:54:28 -0600630 rx_ring(mac)->buffers, rx_ring(mac)->buf_dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600631
Olof Johansson72b05b92007-11-28 20:54:28 -0600632 kfree(rx_ring(mac)->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600633 pasemi_dma_free_chan(&rx_ring(mac)->chan);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600634 mac->rx = NULL;
635}
636
Olof Johansson5c153322007-11-28 20:56:41 -0600637static void pasemi_mac_replenish_rx_ring(const struct net_device *dev,
638 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600639{
Olof Johansson5c153322007-11-28 20:56:41 -0600640 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -0600641 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500642 int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600643
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500644 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600645 return;
646
Olof Johansson72b05b92007-11-28 20:54:28 -0600647 fill = rx_ring(mac)->next_to_fill;
Olof Johansson928773c2007-09-26 16:25:06 -0500648 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600649 struct pasemi_mac_buffer *info = &RX_DESC_INFO(rx, fill);
650 u64 *buff = &RX_BUFF(rx, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600651 struct sk_buff *skb;
652 dma_addr_t dma;
653
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500654 /* Entry in use? */
655 WARN_ON(*buff);
656
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600657 skb = dev_alloc_skb(mac->bufsz);
Olof Johansson5d894942007-11-28 20:57:56 -0600658 skb_reserve(skb, LOCAL_SKB_ALIGN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600659
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500660 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600661 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600662
Olof Johansson8dc121a2007-10-02 16:26:53 -0500663 dma = pci_map_single(mac->dma_pdev, skb->data,
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600664 mac->bufsz - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600665 PCI_DMA_FROMDEVICE);
666
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500667 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600668 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600669 break;
670 }
671
672 info->skb = skb;
673 info->dma = dma;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600674 *buff = XCT_RXB_LEN(mac->bufsz) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500675 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600676 }
677
678 wmb();
679
Olof Johansson34c20622007-11-28 20:56:32 -0600680 write_dma_reg(PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600681
Olof Johansson72b05b92007-11-28 20:54:28 -0600682 rx_ring(mac)->next_to_fill = (rx_ring(mac)->next_to_fill + count) &
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500683 (RX_RING_SIZE - 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600684}
685
Olof Johansson5c153322007-11-28 20:56:41 -0600686static void pasemi_mac_restart_rx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500687{
Olof Johansson906674a2007-11-28 20:57:09 -0600688 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johansson52a94352007-05-12 18:01:09 -0500689 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500690 /* Re-enable packet count interrupts: finally
691 * ack the packet count interrupt we got in rx_intr.
692 */
693
Olof Johansson906674a2007-11-28 20:57:09 -0600694 pcnt = *rx->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_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500697
Olof Johansson906674a2007-11-28 20:57:09 -0600698 if (*rx->chan.status & PAS_STATUS_TIMER)
699 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
700
Olof Johansson34c20622007-11-28 20:56:32 -0600701 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(mac->rx->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500702}
703
Olof Johansson5c153322007-11-28 20:56:41 -0600704static void pasemi_mac_restart_tx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500705{
Olof Johansson52a94352007-05-12 18:01:09 -0500706 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500707
708 /* Re-enable packet count interrupts */
Olof Johansson34c20622007-11-28 20:56:32 -0600709 pcnt = *tx_ring(mac)->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500710
Olof Johansson52a94352007-05-12 18:01:09 -0500711 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500712
Olof Johansson34c20622007-11-28 20:56:32 -0600713 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(tx_ring(mac)->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500714}
715
716
Olof Johansson5c153322007-11-28 20:56:41 -0600717static inline void pasemi_mac_rx_error(const struct pasemi_mac *mac,
718 const u64 macrx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500719{
720 unsigned int rcmdsta, ccmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600721 struct pasemi_dmachan *chan = &rx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500722
723 if (!netif_msg_rx_err(mac))
724 return;
725
Olof Johansson34c20622007-11-28 20:56:32 -0600726 rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
727 ccmdsta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500728
729 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
Olof Johansson34c20622007-11-28 20:56:32 -0600730 macrx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500731
732 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
733 rcmdsta, ccmdsta);
734}
735
Olof Johansson5c153322007-11-28 20:56:41 -0600736static inline void pasemi_mac_tx_error(const struct pasemi_mac *mac,
737 const u64 mactx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500738{
739 unsigned int cmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600740 struct pasemi_dmachan *chan = &tx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500741
742 if (!netif_msg_tx_err(mac))
743 return;
744
Olof Johansson34c20622007-11-28 20:56:32 -0600745 cmdsta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500746
747 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
Olof Johansson34c20622007-11-28 20:56:32 -0600748 "tx status 0x%016lx\n", mactx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500749
750 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
751}
752
Olof Johansson5c153322007-11-28 20:56:41 -0600753static int pasemi_mac_clean_rx(struct pasemi_mac_rxring *rx,
754 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600755{
Olof Johansson5c153322007-11-28 20:56:41 -0600756 const struct pasemi_dmachan *chan = &rx->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600757 struct pasemi_mac *mac = rx->mac;
Olof Johansson5c153322007-11-28 20:56:41 -0600758 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500759 unsigned int n;
Olof Johansson5c153322007-11-28 20:56:41 -0600760 int count, buf_index, tot_bytes, packets;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500761 struct pasemi_mac_buffer *info;
762 struct sk_buff *skb;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500763 unsigned int len;
Olof Johansson5c153322007-11-28 20:56:41 -0600764 u64 macrx, eval;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500765 dma_addr_t dma;
Olof Johansson5c153322007-11-28 20:56:41 -0600766
767 tot_bytes = 0;
768 packets = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600769
Olof Johansson72b05b92007-11-28 20:54:28 -0600770 spin_lock(&rx->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600771
Olof Johansson72b05b92007-11-28 20:54:28 -0600772 n = rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600773
Olof Johansson72b05b92007-11-28 20:54:28 -0600774 prefetch(&RX_DESC(rx, n));
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500775
776 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600777 macrx = RX_DESC(rx, n);
Olof Johansson5c153322007-11-28 20:56:41 -0600778 prefetch(&RX_DESC(rx, n+4));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600779
Olof Johansson69c29d82007-10-02 16:24:51 -0500780 if ((macrx & XCT_MACRX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600781 (*chan->status & PAS_STATUS_ERROR))
Olof Johansson69c29d82007-10-02 16:24:51 -0500782 pasemi_mac_rx_error(mac, macrx);
783
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500784 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600785 break;
786
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600787 info = NULL;
788
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500789 BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600790
Olof Johansson72b05b92007-11-28 20:54:28 -0600791 eval = (RX_DESC(rx, n+1) & XCT_RXRES_8B_EVAL_M) >>
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500792 XCT_RXRES_8B_EVAL_S;
793 buf_index = eval-1;
794
Olof Johansson72b05b92007-11-28 20:54:28 -0600795 dma = (RX_DESC(rx, n+2) & XCT_PTR_ADDR_M);
796 info = &RX_DESC_INFO(rx, buf_index);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500797
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500798 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600799
Olof Johansson5c153322007-11-28 20:56:41 -0600800 prefetch_skb(skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600801
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500802 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600803
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600804 pci_unmap_single(pdev, dma, mac->bufsz - LOCAL_SKB_ALIGN,
Olof Johansson5c153322007-11-28 20:56:41 -0600805 PCI_DMA_FROMDEVICE);
Olof Johansson32bee772007-11-06 22:21:38 -0600806
807 if (macrx & XCT_MACRX_CRC) {
808 /* CRC error flagged */
809 mac->netdev->stats.rx_errors++;
810 mac->netdev->stats.rx_crc_errors++;
Olof Johansson4352d822007-12-03 21:34:14 -0600811 /* No need to free skb, it'll be reused */
Olof Johansson32bee772007-11-06 22:21:38 -0600812 goto next;
813 }
814
Olof Johansson5d894942007-11-28 20:57:56 -0600815 info->skb = NULL;
Olof Johanssonad5da102007-10-02 16:27:15 -0500816 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500817
Olof Johansson26fcfa92007-08-22 09:12:59 -0500818 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500819 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500820 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600821 XCT_MACRX_CSUM_S;
822 } else
823 skb->ip_summed = CHECKSUM_NONE;
824
Olof Johansson5c153322007-11-28 20:56:41 -0600825 packets++;
826 tot_bytes += len;
827
828 /* Don't include CRC */
829 skb_put(skb, len-4);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600830
Olof Johansson26fcfa92007-08-22 09:12:59 -0500831 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johansson28ae79f2007-11-28 20:57:27 -0600832 lro_receive_skb(&mac->lro_mgr, skb, (void *)macrx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600833
Olof Johansson32bee772007-11-06 22:21:38 -0600834next:
Olof Johansson72b05b92007-11-28 20:54:28 -0600835 RX_DESC(rx, n) = 0;
836 RX_DESC(rx, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500837
Olof Johanssonad5da102007-10-02 16:27:15 -0500838 /* Need to zero it out since hardware doesn't, since the
839 * replenish loop uses it to tell when it's done.
840 */
Olof Johansson72b05b92007-11-28 20:54:28 -0600841 RX_BUFF(rx, buf_index) = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500842
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500843 n += 4;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600844 }
845
Olof Johansson9a50beb2007-10-02 16:26:30 -0500846 if (n > RX_RING_SIZE) {
847 /* Errata 5971 workaround: L2 target of headers */
Olof Johansson34c20622007-11-28 20:56:32 -0600848 write_iob_reg(PAS_IOB_COM_PKTHDRCNT, 0);
Olof Johansson9a50beb2007-10-02 16:26:30 -0500849 n &= (RX_RING_SIZE-1);
850 }
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500851
Olof Johansson72b05b92007-11-28 20:54:28 -0600852 rx_ring(mac)->next_to_clean = n;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500853
Olof Johansson28ae79f2007-11-28 20:57:27 -0600854 lro_flush_all(&mac->lro_mgr);
855
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500856 /* Increase is in number of 16-byte entries, and since each descriptor
857 * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
858 * count*2.
859 */
Olof Johansson34c20622007-11-28 20:56:32 -0600860 write_dma_reg(PAS_DMA_RXCHAN_INCR(mac->rx->chan.chno), count << 1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500861
862 pasemi_mac_replenish_rx_ring(mac->netdev, count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600863
Olof Johansson5c153322007-11-28 20:56:41 -0600864 mac->netdev->stats.rx_bytes += tot_bytes;
865 mac->netdev->stats.rx_packets += packets;
866
Olof Johansson72b05b92007-11-28 20:54:28 -0600867 spin_unlock(&rx_ring(mac)->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600868
869 return count;
870}
871
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500872/* Can't make this too large or we blow the kernel stack limits */
873#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
874
Olof Johansson72b05b92007-11-28 20:54:28 -0600875static int pasemi_mac_clean_tx(struct pasemi_mac_txring *txring)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600876{
Olof Johansson34c20622007-11-28 20:56:32 -0600877 struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600878 struct pasemi_mac *mac = txring->mac;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500879 int i, j;
Olof Johanssonad5da102007-10-02 16:27:15 -0500880 unsigned int start, descr_count, buf_count, batch_limit;
881 unsigned int ring_limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500882 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500883 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500884 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
885 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johansson7e9916e2007-11-28 20:57:45 -0600886 int nf[TX_CLEAN_BATCHSIZE];
887 int nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600888
Olof Johansson02df6cf2007-08-22 09:13:03 -0500889 total_count = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500890 batch_limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500891restart:
Olof Johansson72b05b92007-11-28 20:54:28 -0600892 spin_lock_irqsave(&txring->lock, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600893
Olof Johansson72b05b92007-11-28 20:54:28 -0600894 start = txring->next_to_clean;
895 ring_limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500896
Olof Johansson7e9916e2007-11-28 20:57:45 -0600897 prefetch(&TX_DESC_INFO(txring, start+1).skb);
898
Olof Johanssonad5da102007-10-02 16:27:15 -0500899 /* Compensate for when fill has wrapped but clean has not */
900 if (start > ring_limit)
901 ring_limit += TX_RING_SIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500902
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500903 buf_count = 0;
904 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600905
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500906 for (i = start;
Olof Johanssonad5da102007-10-02 16:27:15 -0500907 descr_count < batch_limit && i < ring_limit;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500908 i += buf_count) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600909 u64 mactx = TX_DESC(txring, i);
Olof Johanssonad5da102007-10-02 16:27:15 -0500910 struct sk_buff *skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500911
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500912 if ((mactx & XCT_MACTX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600913 (*chan->status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500914 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500915
Olof Johansson8d636d82008-03-05 16:34:16 -0600916 /* Skip over control descriptors */
917 if (!(mactx & XCT_MACTX_LLEN_M)) {
918 TX_DESC(txring, i) = 0;
919 TX_DESC(txring, i+1) = 0;
920 buf_count = 2;
921 continue;
922 }
923
924 skb = TX_DESC_INFO(txring, i+1).skb;
925 nr_frags = TX_DESC_INFO(txring, i).dma;
926
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500927 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500928 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600929 break;
930
Olof Johansson7e9916e2007-11-28 20:57:45 -0600931 buf_count = 2 + nr_frags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500932 /* Since we always fill with an even number of entries, make
933 * sure we skip any unused one at the end as well.
934 */
935 if (buf_count & 1)
936 buf_count++;
Olof Johansson7e9916e2007-11-28 20:57:45 -0600937
938 for (j = 0; j <= nr_frags; j++)
939 dmas[descr_count][j] = TX_DESC_INFO(txring, i+1+j).dma;
940
941 skbs[descr_count] = skb;
942 nf[descr_count] = nr_frags;
943
944 TX_DESC(txring, i) = 0;
945 TX_DESC(txring, i+1) = 0;
946
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500947 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600948 }
Olof Johansson72b05b92007-11-28 20:54:28 -0600949 txring->next_to_clean = i & (TX_RING_SIZE-1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500950
Olof Johansson72b05b92007-11-28 20:54:28 -0600951 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500952 netif_wake_queue(mac->netdev);
953
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500954 for (i = 0; i < descr_count; i++)
Olof Johansson7e9916e2007-11-28 20:57:45 -0600955 pasemi_mac_unmap_tx_skb(mac, nf[i], skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500956
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500957 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500958
959 /* If the batch was full, try to clean more */
Olof Johanssonad5da102007-10-02 16:27:15 -0500960 if (descr_count == batch_limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500961 goto restart;
962
963 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600964}
965
966
967static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
968{
Olof Johansson5c153322007-11-28 20:56:41 -0600969 const struct pasemi_mac_rxring *rxring = data;
Olof Johansson34c20622007-11-28 20:56:32 -0600970 struct pasemi_mac *mac = rxring->mac;
971 struct net_device *dev = mac->netdev;
Olof Johansson5c153322007-11-28 20:56:41 -0600972 const struct pasemi_dmachan *chan = &rxring->chan;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600973 unsigned int reg;
974
Olof Johansson34c20622007-11-28 20:56:32 -0600975 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600976 return IRQ_NONE;
977
Olof Johansson6dfa7522007-05-08 00:47:32 -0500978 /* Don't reset packet count so it won't fire again but clear
979 * all others.
980 */
981
Olof Johansson6dfa7522007-05-08 00:47:32 -0500982 reg = 0;
Olof Johansson34c20622007-11-28 20:56:32 -0600983 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500984 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600985 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500986 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600987
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700988 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500989
Olof Johansson34c20622007-11-28 20:56:32 -0600990 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600991
992 return IRQ_HANDLED;
993}
994
Olof Johansson61cec3b2007-11-28 20:56:54 -0600995#define TX_CLEAN_INTERVAL HZ
996
997static void pasemi_mac_tx_timer(unsigned long data)
998{
999 struct pasemi_mac_txring *txring = (struct pasemi_mac_txring *)data;
1000 struct pasemi_mac *mac = txring->mac;
1001
1002 pasemi_mac_clean_tx(txring);
1003
1004 mod_timer(&txring->clean_timer, jiffies + TX_CLEAN_INTERVAL);
1005
1006 pasemi_mac_restart_tx_intr(mac);
1007}
1008
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001009static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
1010{
Olof Johansson72b05b92007-11-28 20:54:28 -06001011 struct pasemi_mac_txring *txring = data;
Olof Johansson5c153322007-11-28 20:56:41 -06001012 const struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson61cec3b2007-11-28 20:56:54 -06001013 struct pasemi_mac *mac = txring->mac;
1014 unsigned int reg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001015
Olof Johansson34c20622007-11-28 20:56:32 -06001016 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001017 return IRQ_NONE;
1018
Olof Johansson61cec3b2007-11-28 20:56:54 -06001019 reg = 0;
Olof Johansson6dfa7522007-05-08 00:47:32 -05001020
Olof Johansson34c20622007-11-28 20:56:32 -06001021 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -05001022 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -06001023 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -05001024 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001025
Olof Johansson61cec3b2007-11-28 20:56:54 -06001026 mod_timer(&txring->clean_timer, jiffies + (TX_CLEAN_INTERVAL)*2);
1027
1028 netif_rx_schedule(mac->netdev, &mac->napi);
1029
1030 if (reg)
1031 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001032
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001033 return IRQ_HANDLED;
1034}
1035
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001036static void pasemi_adjust_link(struct net_device *dev)
1037{
1038 struct pasemi_mac *mac = netdev_priv(dev);
1039 int msg;
1040 unsigned int flags;
1041 unsigned int new_flags;
1042
1043 if (!mac->phydev->link) {
1044 /* If no link, MAC speed settings don't matter. Just report
1045 * link down and return.
1046 */
1047 if (mac->link && netif_msg_link(mac))
1048 printk(KERN_INFO "%s: Link is down.\n", dev->name);
1049
1050 netif_carrier_off(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001051 pasemi_mac_intf_disable(mac);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001052 mac->link = 0;
1053
1054 return;
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001055 } else {
1056 pasemi_mac_intf_enable(mac);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001057 netif_carrier_on(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001058 }
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001059
Olof Johanssona85b9422007-09-15 13:40:59 -07001060 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001061 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
1062 PAS_MAC_CFG_PCFG_TSR_M);
1063
1064 if (!mac->phydev->duplex)
1065 new_flags |= PAS_MAC_CFG_PCFG_HD;
1066
1067 switch (mac->phydev->speed) {
1068 case 1000:
1069 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
1070 PAS_MAC_CFG_PCFG_TSR_1G;
1071 break;
1072 case 100:
1073 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
1074 PAS_MAC_CFG_PCFG_TSR_100M;
1075 break;
1076 case 10:
1077 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
1078 PAS_MAC_CFG_PCFG_TSR_10M;
1079 break;
1080 default:
1081 printk("Unsupported speed %d\n", mac->phydev->speed);
1082 }
1083
1084 /* Print on link or speed/duplex change */
1085 msg = mac->link != mac->phydev->link || flags != new_flags;
1086
1087 mac->duplex = mac->phydev->duplex;
1088 mac->speed = mac->phydev->speed;
1089 mac->link = mac->phydev->link;
1090
1091 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -07001092 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001093
1094 if (msg && netif_msg_link(mac))
1095 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
1096 dev->name, mac->speed, mac->duplex ? "full" : "half");
1097}
1098
1099static int pasemi_mac_phy_init(struct net_device *dev)
1100{
1101 struct pasemi_mac *mac = netdev_priv(dev);
1102 struct device_node *dn, *phy_dn;
1103 struct phy_device *phydev;
1104 unsigned int phy_id;
1105 const phandle *ph;
1106 const unsigned int *prop;
1107 struct resource r;
1108 int ret;
1109
1110 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -07001111 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001112 if (!ph)
1113 return -ENODEV;
1114 phy_dn = of_find_node_by_phandle(*ph);
1115
Linus Torvalds90287802007-05-08 11:57:17 -07001116 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001117 ret = of_address_to_resource(phy_dn->parent, 0, &r);
1118 if (ret)
1119 goto err;
1120
1121 phy_id = *prop;
1122 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
1123
1124 of_node_put(phy_dn);
1125
1126 mac->link = 0;
1127 mac->speed = 0;
1128 mac->duplex = -1;
1129
1130 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
1131
1132 if (IS_ERR(phydev)) {
1133 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
1134 return PTR_ERR(phydev);
1135 }
1136
1137 mac->phydev = phydev;
1138
1139 return 0;
1140
1141err:
1142 of_node_put(phy_dn);
1143 return -ENODEV;
1144}
1145
1146
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001147static int pasemi_mac_open(struct net_device *dev)
1148{
1149 struct pasemi_mac *mac = netdev_priv(dev);
1150 unsigned int flags;
1151 int ret;
1152
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001153 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
1154 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
1155 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
1156
Olof Johanssona85b9422007-09-15 13:40:59 -07001157 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001158
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001159 ret = pasemi_mac_setup_rx_resources(dev);
1160 if (ret)
1161 goto out_rx_resources;
1162
Olof Johansson34c20622007-11-28 20:56:32 -06001163 mac->tx = pasemi_mac_setup_tx_resources(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001164
1165 if (!mac->tx)
1166 goto out_tx_ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001167
Olof Johansson8d636d82008-03-05 16:34:16 -06001168 if (dev->mtu > 1500) {
1169 pasemi_mac_setup_csrings(mac);
1170 if (!mac->num_cs)
1171 goto out_tx_ring;
1172 }
1173
Olof Johansson906674a2007-11-28 20:57:09 -06001174 /* 0x3ff with 33MHz clock is about 31us */
1175 write_iob_reg(PAS_IOB_DMA_COM_TIMEOUTCFG,
1176 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0x3ff));
1177
Olof Johansson34c20622007-11-28 20:56:32 -06001178 write_iob_reg(PAS_IOB_DMA_RXCH_CFG(mac->rx->chan.chno),
Olof Johansson28ae79f2007-11-28 20:57:27 -06001179 PAS_IOB_DMA_RXCH_CFG_CNTTH(256));
Olof Johansson34c20622007-11-28 20:56:32 -06001180
1181 write_iob_reg(PAS_IOB_DMA_TXCH_CFG(mac->tx->chan.chno),
Olof Johansson61cec3b2007-11-28 20:56:54 -06001182 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johansson34c20622007-11-28 20:56:32 -06001183
Olof Johanssona85b9422007-09-15 13:40:59 -07001184 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
Olof Johansson34c20622007-11-28 20:56:32 -06001185 PAS_MAC_IPC_CHNL_DCHNO(mac->rx->chan.chno) |
1186 PAS_MAC_IPC_CHNL_BCH(mac->rx->chan.chno));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001187
1188 /* enable rx if */
Olof Johansson34c20622007-11-28 20:56:32 -06001189 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1190 PAS_DMA_RXINT_RCMDSTA_EN |
1191 PAS_DMA_RXINT_RCMDSTA_DROPS_M |
1192 PAS_DMA_RXINT_RCMDSTA_BP |
1193 PAS_DMA_RXINT_RCMDSTA_OO |
1194 PAS_DMA_RXINT_RCMDSTA_BT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001195
1196 /* enable rx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001197 pasemi_dma_start_chan(&rx_ring(mac)->chan, PAS_DMA_RXCHAN_CCMDSTA_DU |
1198 PAS_DMA_RXCHAN_CCMDSTA_OD |
1199 PAS_DMA_RXCHAN_CCMDSTA_FD |
1200 PAS_DMA_RXCHAN_CCMDSTA_DT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001201
1202 /* enable tx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001203 pasemi_dma_start_chan(&tx_ring(mac)->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
1204 PAS_DMA_TXCHAN_TCMDSTA_DB |
1205 PAS_DMA_TXCHAN_TCMDSTA_DE |
1206 PAS_DMA_TXCHAN_TCMDSTA_DA);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001207
Olof Johansson928773c2007-09-26 16:25:06 -05001208 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001209
Olof Johansson34c20622007-11-28 20:56:32 -06001210 write_dma_reg(PAS_DMA_RXCHAN_INCR(rx_ring(mac)->chan.chno),
1211 RX_RING_SIZE>>1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -05001212
Olof Johansson72b05b92007-11-28 20:54:28 -06001213 /* Clear out any residual packet count state from firmware */
1214 pasemi_mac_restart_rx_intr(mac);
1215 pasemi_mac_restart_tx_intr(mac);
1216
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001217 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
Olof Johansson36033762007-09-26 16:24:42 -05001218
1219 if (mac->type == MAC_TYPE_GMAC)
1220 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
1221 else
1222 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
1223
1224 /* Enable interface in MAC */
1225 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
1226
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001227 ret = pasemi_mac_phy_init(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001228 if (ret) {
1229 /* Since we won't get link notification, just enable RX */
1230 pasemi_mac_intf_enable(mac);
1231 if (mac->type == MAC_TYPE_GMAC) {
1232 /* Warn for missing PHY on SGMII (1Gig) ports */
1233 dev_warn(&mac->pdev->dev,
1234 "PHY init failed: %d.\n", ret);
1235 dev_warn(&mac->pdev->dev,
1236 "Defaulting to 1Gbit full duplex\n");
1237 }
Olof Johansson8304b632007-11-28 20:58:06 -06001238 }
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001239
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001240 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001241 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001242
Olof Johansson72b05b92007-11-28 20:54:28 -06001243 snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx",
1244 dev->name);
Olof Johansson771f7402007-05-08 00:47:21 -05001245
Olof Johansson34c20622007-11-28 20:56:32 -06001246 ret = request_irq(mac->tx->chan.irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johansson72b05b92007-11-28 20:54:28 -06001247 mac->tx_irq_name, mac->tx);
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->tx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001251 goto out_tx_int;
1252 }
1253
Olof Johansson72b05b92007-11-28 20:54:28 -06001254 snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx",
1255 dev->name);
1256
Olof Johansson34c20622007-11-28 20:56:32 -06001257 ret = request_irq(mac->rx->chan.irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
1258 mac->rx_irq_name, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001259 if (ret) {
1260 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001261 mac->rx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001262 goto out_rx_int;
1263 }
1264
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001265 if (mac->phydev)
1266 phy_start(mac->phydev);
1267
Olof Johansson61cec3b2007-11-28 20:56:54 -06001268 init_timer(&mac->tx->clean_timer);
1269 mac->tx->clean_timer.function = pasemi_mac_tx_timer;
1270 mac->tx->clean_timer.data = (unsigned long)mac->tx;
1271 mac->tx->clean_timer.expires = jiffies+HZ;
1272 add_timer(&mac->tx->clean_timer);
1273
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001274 return 0;
1275
1276out_rx_int:
Olof Johansson34c20622007-11-28 20:56:32 -06001277 free_irq(mac->tx->chan.irq, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001278out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001279 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001280 netif_stop_queue(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001281out_tx_ring:
1282 if (mac->tx)
1283 pasemi_mac_free_tx_resources(mac);
1284 pasemi_mac_free_rx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001285out_rx_resources:
1286
1287 return ret;
1288}
1289
1290#define MAX_RETRIES 5000
1291
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001292static void pasemi_mac_pause_txchan(struct pasemi_mac *mac)
1293{
1294 unsigned int sta, retries;
1295 int txch = tx_ring(mac)->chan.chno;
1296
1297 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch),
1298 PAS_DMA_TXCHAN_TCMDSTA_ST);
1299
1300 for (retries = 0; retries < MAX_RETRIES; retries++) {
1301 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
1302 if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
1303 break;
1304 cond_resched();
1305 }
1306
1307 if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
1308 dev_err(&mac->dma_pdev->dev,
1309 "Failed to stop tx channel, tcmdsta %08x\n", sta);
1310
1311 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch), 0);
1312}
1313
1314static void pasemi_mac_pause_rxchan(struct pasemi_mac *mac)
1315{
1316 unsigned int sta, retries;
1317 int rxch = rx_ring(mac)->chan.chno;
1318
1319 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch),
1320 PAS_DMA_RXCHAN_CCMDSTA_ST);
1321 for (retries = 0; retries < MAX_RETRIES; retries++) {
1322 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
1323 if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
1324 break;
1325 cond_resched();
1326 }
1327
1328 if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
1329 dev_err(&mac->dma_pdev->dev,
1330 "Failed to stop rx channel, ccmdsta 08%x\n", sta);
1331 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch), 0);
1332}
1333
1334static void pasemi_mac_pause_rxint(struct pasemi_mac *mac)
1335{
1336 unsigned int sta, retries;
1337
1338 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1339 PAS_DMA_RXINT_RCMDSTA_ST);
1340 for (retries = 0; retries < MAX_RETRIES; retries++) {
1341 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1342 if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
1343 break;
1344 cond_resched();
1345 }
1346
1347 if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
1348 dev_err(&mac->dma_pdev->dev,
1349 "Failed to stop rx interface, rcmdsta %08x\n", sta);
1350 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
1351}
1352
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001353static int pasemi_mac_close(struct net_device *dev)
1354{
1355 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson9e81d332007-10-02 16:27:39 -05001356 unsigned int sta;
Olof Johansson8d636d82008-03-05 16:34:16 -06001357 int rxch, txch, i;
Olof Johansson34c20622007-11-28 20:56:32 -06001358
1359 rxch = rx_ring(mac)->chan.chno;
1360 txch = tx_ring(mac)->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001361
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001362 if (mac->phydev) {
1363 phy_stop(mac->phydev);
1364 phy_disconnect(mac->phydev);
1365 }
1366
Olof Johansson61cec3b2007-11-28 20:56:54 -06001367 del_timer_sync(&mac->tx->clean_timer);
1368
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001369 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001370 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001371
Olof Johansson34c20622007-11-28 20:56:32 -06001372 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson9e81d332007-10-02 16:27:39 -05001373 if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
1374 PAS_DMA_RXINT_RCMDSTA_OO |
1375 PAS_DMA_RXINT_RCMDSTA_BT))
1376 printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);
1377
Olof Johansson34c20622007-11-28 20:56:32 -06001378 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001379 if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
1380 PAS_DMA_RXCHAN_CCMDSTA_OD |
1381 PAS_DMA_RXCHAN_CCMDSTA_FD |
1382 PAS_DMA_RXCHAN_CCMDSTA_DT))
1383 printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);
1384
Olof Johansson34c20622007-11-28 20:56:32 -06001385 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
Olof Johansson72b05b92007-11-28 20:54:28 -06001386 if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB |
1387 PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA))
Olof Johansson9e81d332007-10-02 16:27:39 -05001388 printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);
1389
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001390 /* Clean out any pending buffers */
Olof Johansson72b05b92007-11-28 20:54:28 -06001391 pasemi_mac_clean_tx(tx_ring(mac));
1392 pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001393
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001394 pasemi_mac_pause_txchan(mac);
1395 pasemi_mac_pause_rxint(mac);
1396 pasemi_mac_pause_rxchan(mac);
Olof Johansson1145d952008-01-23 13:57:19 -06001397 pasemi_mac_intf_disable(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001398
Olof Johansson34c20622007-11-28 20:56:32 -06001399 free_irq(mac->tx->chan.irq, mac->tx);
1400 free_irq(mac->rx->chan.irq, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001401
Olof Johansson8d636d82008-03-05 16:34:16 -06001402 for (i = 0; i < mac->num_cs; i++)
1403 pasemi_mac_free_csring(mac->cs[i]);
1404
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001405 /* Free resources */
Olof Johansson72b05b92007-11-28 20:54:28 -06001406 pasemi_mac_free_rx_resources(mac);
1407 pasemi_mac_free_tx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001408
1409 return 0;
1410}
1411
Olof Johansson8d636d82008-03-05 16:34:16 -06001412static void pasemi_mac_queue_csdesc(const struct sk_buff *skb,
1413 const dma_addr_t *map,
1414 const unsigned int *map_size,
1415 struct pasemi_mac_txring *txring,
1416 struct pasemi_mac_csring *csring)
1417{
1418 u64 fund;
1419 dma_addr_t cs_dest;
1420 const int nh_off = skb_network_offset(skb);
1421 const int nh_len = skb_network_header_len(skb);
1422 const int nfrags = skb_shinfo(skb)->nr_frags;
1423 int cs_size, i, fill, hdr, cpyhdr, evt;
1424 dma_addr_t csdma;
1425
1426 fund = XCT_FUN_ST | XCT_FUN_RR_8BRES |
1427 XCT_FUN_O | XCT_FUN_FUN(csring->fun) |
1428 XCT_FUN_CRM_SIG | XCT_FUN_LLEN(skb->len - nh_off) |
1429 XCT_FUN_SHL(nh_len >> 2) | XCT_FUN_SE;
1430
1431 switch (ip_hdr(skb)->protocol) {
1432 case IPPROTO_TCP:
1433 fund |= XCT_FUN_SIG_TCP4;
1434 /* TCP checksum is 16 bytes into the header */
1435 cs_dest = map[0] + skb_transport_offset(skb) + 16;
1436 break;
1437 case IPPROTO_UDP:
1438 fund |= XCT_FUN_SIG_UDP4;
1439 /* UDP checksum is 6 bytes into the header */
1440 cs_dest = map[0] + skb_transport_offset(skb) + 6;
1441 break;
1442 default:
1443 BUG();
1444 }
1445
1446 /* Do the checksum offloaded */
1447 fill = csring->next_to_fill;
1448 hdr = fill;
1449
1450 CS_DESC(csring, fill++) = fund;
1451 /* Room for 8BRES. Checksum result is really 2 bytes into it */
1452 csdma = csring->chan.ring_dma + (fill & (CS_RING_SIZE-1)) * 8 + 2;
1453 CS_DESC(csring, fill++) = 0;
1454
1455 CS_DESC(csring, fill) = XCT_PTR_LEN(map_size[0]-nh_off) | XCT_PTR_ADDR(map[0]+nh_off);
1456 for (i = 1; i <= nfrags; i++)
1457 CS_DESC(csring, fill+i) = XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
1458
1459 fill += i;
1460 if (fill & 1)
1461 fill++;
1462
1463 /* Copy the result into the TCP packet */
1464 cpyhdr = fill;
1465 CS_DESC(csring, fill++) = XCT_FUN_O | XCT_FUN_FUN(csring->fun) |
1466 XCT_FUN_LLEN(2) | XCT_FUN_SE;
1467 CS_DESC(csring, fill++) = XCT_PTR_LEN(2) | XCT_PTR_ADDR(cs_dest) | XCT_PTR_T;
1468 CS_DESC(csring, fill++) = XCT_PTR_LEN(2) | XCT_PTR_ADDR(csdma);
1469 fill++;
1470
1471 evt = !csring->last_event;
1472 csring->last_event = evt;
1473
1474 /* Event handshaking with MAC TX */
1475 CS_DESC(csring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
1476 CTRL_CMD_ETYPE_SET | CTRL_CMD_REG(csring->events[evt]);
1477 CS_DESC(csring, fill++) = 0;
1478 CS_DESC(csring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
1479 CTRL_CMD_ETYPE_WCLR | CTRL_CMD_REG(csring->events[!evt]);
1480 CS_DESC(csring, fill++) = 0;
1481 csring->next_to_fill = fill & (CS_RING_SIZE-1);
1482
1483 cs_size = fill - hdr;
1484 write_dma_reg(PAS_DMA_TXCHAN_INCR(csring->chan.chno), (cs_size) >> 1);
1485
1486 /* TX-side event handshaking */
1487 fill = txring->next_to_fill;
1488 TX_DESC(txring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
1489 CTRL_CMD_ETYPE_WSET | CTRL_CMD_REG(csring->events[evt]);
1490 TX_DESC(txring, fill++) = 0;
1491 TX_DESC(txring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
1492 CTRL_CMD_ETYPE_CLR | CTRL_CMD_REG(csring->events[!evt]);
1493 TX_DESC(txring, fill++) = 0;
1494 txring->next_to_fill = fill;
1495
1496 write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), 2);
1497
1498 return;
1499}
1500
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001501static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1502{
Olof Johansson8d636d82008-03-05 16:34:16 -06001503 struct pasemi_mac * const mac = netdev_priv(dev);
1504 struct pasemi_mac_txring * const txring = tx_ring(mac);
1505 struct pasemi_mac_csring *csring;
1506 u64 dflags = 0;
1507 u64 mactx;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001508 dma_addr_t map[MAX_SKB_FRAGS+1];
1509 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001510 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001511 int i, nfrags;
Olof Johansson5c153322007-11-28 20:56:41 -06001512 int fill;
Olof Johansson8d636d82008-03-05 16:34:16 -06001513 const int nh_off = skb_network_offset(skb);
1514 const int nh_len = skb_network_header_len(skb);
1515
1516 prefetch(&txring->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001517
Olof Johanssondbd62af2007-11-06 22:20:39 -06001518 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001519
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001520 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001521
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001522 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1523 PCI_DMA_TODEVICE);
1524 map_size[0] = skb_headlen(skb);
1525 if (dma_mapping_error(map[0]))
1526 goto out_err_nolock;
1527
1528 for (i = 0; i < nfrags; i++) {
1529 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1530
1531 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1532 frag->page_offset, frag->size,
1533 PCI_DMA_TODEVICE);
1534 map_size[i+1] = frag->size;
1535 if (dma_mapping_error(map[i+1])) {
1536 nfrags = i;
1537 goto out_err_nolock;
1538 }
1539 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001540
Olof Johansson8d636d82008-03-05 16:34:16 -06001541 if (skb->ip_summed == CHECKSUM_PARTIAL && skb->len <= 1540) {
1542 switch (ip_hdr(skb)->protocol) {
1543 case IPPROTO_TCP:
1544 dflags |= XCT_MACTX_CSUM_TCP;
1545 dflags |= XCT_MACTX_IPH(nh_len >> 2);
1546 dflags |= XCT_MACTX_IPO(nh_off);
1547 break;
1548 case IPPROTO_UDP:
1549 dflags |= XCT_MACTX_CSUM_UDP;
1550 dflags |= XCT_MACTX_IPH(nh_len >> 2);
1551 dflags |= XCT_MACTX_IPO(nh_off);
1552 break;
1553 default:
1554 WARN_ON(1);
1555 }
1556 }
1557
Olof Johansson26fcfa92007-08-22 09:12:59 -05001558 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001559
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001560 spin_lock_irqsave(&txring->lock, flags);
1561
Olof Johanssonad5da102007-10-02 16:27:15 -05001562 /* Avoid stepping on the same cache line that the DMA controller
1563 * is currently about to send, so leave at least 8 words available.
1564 * Total free space needed is mactx + fragments + 8
1565 */
Olof Johansson8d636d82008-03-05 16:34:16 -06001566 if (RING_AVAIL(txring) < nfrags + 14) {
Olof Johanssonad5da102007-10-02 16:27:15 -05001567 /* no room -- stop the queue and wait for tx intr */
1568 netif_stop_queue(dev);
1569 goto out_err;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001570 }
1571
Olof Johansson8d636d82008-03-05 16:34:16 -06001572 /* Queue up checksum + event descriptors, if needed */
1573 if (mac->num_cs && skb->ip_summed == CHECKSUM_PARTIAL && skb->len > 1540) {
1574 csring = mac->cs[mac->last_cs];
1575 mac->last_cs = (mac->last_cs + 1) % mac->num_cs;
1576
1577 pasemi_mac_queue_csdesc(skb, map, map_size, txring, csring);
1578 }
1579
1580 fill = txring->next_to_fill;
Olof Johansson5c153322007-11-28 20:56:41 -06001581 TX_DESC(txring, fill) = mactx;
Olof Johansson7e9916e2007-11-28 20:57:45 -06001582 TX_DESC_INFO(txring, fill).dma = nfrags;
Olof Johansson5c153322007-11-28 20:56:41 -06001583 fill++;
1584 TX_DESC_INFO(txring, fill).skb = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001585 for (i = 0; i <= nfrags; i++) {
Olof Johansson5c153322007-11-28 20:56:41 -06001586 TX_DESC(txring, fill+i) =
Olof Johansson72b05b92007-11-28 20:54:28 -06001587 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
Olof Johansson5c153322007-11-28 20:56:41 -06001588 TX_DESC_INFO(txring, fill+i).dma = map[i];
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001589 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001590
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001591 /* We have to add an even number of 8-byte entries to the ring
1592 * even if the last one is unused. That means always an odd number
1593 * of pointers + one mactx descriptor.
1594 */
1595 if (nfrags & 1)
1596 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001597
Olof Johansson5c153322007-11-28 20:56:41 -06001598 txring->next_to_fill = (fill + nfrags + 1) & (TX_RING_SIZE-1);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001599
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001600 dev->stats.tx_packets++;
1601 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001602
1603 spin_unlock_irqrestore(&txring->lock, flags);
1604
Olof Johansson34c20622007-11-28 20:56:32 -06001605 write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001606
1607 return NETDEV_TX_OK;
1608
1609out_err:
1610 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001611out_err_nolock:
1612 while (nfrags--)
1613 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1614 PCI_DMA_TODEVICE);
1615
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001616 return NETDEV_TX_BUSY;
1617}
1618
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001619static void pasemi_mac_set_rx_mode(struct net_device *dev)
1620{
Olof Johansson5c153322007-11-28 20:56:41 -06001621 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001622 unsigned int flags;
1623
Olof Johanssona85b9422007-09-15 13:40:59 -07001624 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001625
1626 /* Set promiscuous */
1627 if (dev->flags & IFF_PROMISC)
1628 flags |= PAS_MAC_CFG_PCFG_PR;
1629 else
1630 flags &= ~PAS_MAC_CFG_PCFG_PR;
1631
Olof Johanssona85b9422007-09-15 13:40:59 -07001632 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001633}
1634
1635
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001636static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001637{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001638 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1639 struct net_device *dev = mac->netdev;
1640 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001641
Olof Johansson72b05b92007-11-28 20:54:28 -06001642 pasemi_mac_clean_tx(tx_ring(mac));
1643 pkts = pasemi_mac_clean_rx(rx_ring(mac), budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001644 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001645 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001646 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001647
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001648 pasemi_mac_restart_rx_intr(mac);
Olof Johansson61cec3b2007-11-28 20:56:54 -06001649 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001650 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001651 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001652}
1653
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001654static int pasemi_mac_change_mtu(struct net_device *dev, int new_mtu)
1655{
1656 struct pasemi_mac *mac = netdev_priv(dev);
1657 unsigned int reg;
Olof Johansson8d636d82008-03-05 16:34:16 -06001658 unsigned int rcmdsta = 0;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001659 int running;
Olof Johansson8d636d82008-03-05 16:34:16 -06001660 int ret = 0;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001661
1662 if (new_mtu < PE_MIN_MTU || new_mtu > PE_MAX_MTU)
1663 return -EINVAL;
1664
1665 running = netif_running(dev);
1666
1667 if (running) {
1668 /* Need to stop the interface, clean out all already
1669 * received buffers, free all unused buffers on the RX
1670 * interface ring, then finally re-fill the rx ring with
1671 * the new-size buffers and restart.
1672 */
1673
1674 napi_disable(&mac->napi);
1675 netif_tx_disable(dev);
1676 pasemi_mac_intf_disable(mac);
1677
1678 rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1679 pasemi_mac_pause_rxint(mac);
1680 pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
1681 pasemi_mac_free_rx_buffers(mac);
Olof Johansson8d636d82008-03-05 16:34:16 -06001682
1683 }
1684
1685 /* Setup checksum channels if large MTU and none already allocated */
1686 if (new_mtu > 1500 && !mac->num_cs) {
1687 pasemi_mac_setup_csrings(mac);
1688 if (!mac->num_cs) {
1689 ret = -ENOMEM;
1690 goto out;
1691 }
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001692 }
1693
1694 /* Change maxf, i.e. what size frames are accepted.
1695 * Need room for ethernet header and CRC word
1696 */
1697 reg = read_mac_reg(mac, PAS_MAC_CFG_MACCFG);
1698 reg &= ~PAS_MAC_CFG_MACCFG_MAXF_M;
1699 reg |= PAS_MAC_CFG_MACCFG_MAXF(new_mtu + ETH_HLEN + 4);
1700 write_mac_reg(mac, PAS_MAC_CFG_MACCFG, reg);
1701
1702 dev->mtu = new_mtu;
1703 /* MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
1704 mac->bufsz = new_mtu + ETH_HLEN + ETH_FCS_LEN + LOCAL_SKB_ALIGN + 128;
1705
Olof Johansson8d636d82008-03-05 16:34:16 -06001706out:
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001707 if (running) {
1708 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1709 rcmdsta | PAS_DMA_RXINT_RCMDSTA_EN);
1710
1711 rx_ring(mac)->next_to_fill = 0;
1712 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE-1);
1713
1714 napi_enable(&mac->napi);
1715 netif_start_queue(dev);
1716 pasemi_mac_intf_enable(mac);
1717 }
1718
Olof Johansson8d636d82008-03-05 16:34:16 -06001719 return ret;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001720}
1721
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001722static int __devinit
1723pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1724{
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001725 struct net_device *dev;
1726 struct pasemi_mac *mac;
1727 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001728 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001729
1730 err = pci_enable_device(pdev);
1731 if (err)
1732 return err;
1733
1734 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1735 if (dev == NULL) {
1736 dev_err(&pdev->dev,
1737 "pasemi_mac: Could not allocate ethernet device.\n");
1738 err = -ENOMEM;
1739 goto out_disable_device;
1740 }
1741
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001742 pci_set_drvdata(pdev, dev);
1743 SET_NETDEV_DEV(dev, &pdev->dev);
1744
1745 mac = netdev_priv(dev);
1746
1747 mac->pdev = pdev;
1748 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001749
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001750 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1751
Olof Johansson5c153322007-11-28 20:56:41 -06001752 dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG |
1753 NETIF_F_HIGHDMA;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001754
Olof Johansson28ae79f2007-11-28 20:57:27 -06001755 mac->lro_mgr.max_aggr = LRO_MAX_AGGR;
1756 mac->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
1757 mac->lro_mgr.lro_arr = mac->lro_desc;
1758 mac->lro_mgr.get_skb_header = get_skb_hdr;
1759 mac->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1760 mac->lro_mgr.dev = mac->netdev;
1761 mac->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1762 mac->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1763
1764
Olof Johansson34c20622007-11-28 20:56:32 -06001765 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1766 if (!mac->dma_pdev) {
1767 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1768 err = -ENODEV;
1769 goto out;
1770 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001771
Olof Johansson34c20622007-11-28 20:56:32 -06001772 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1773 if (!mac->iob_pdev) {
1774 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1775 err = -ENODEV;
1776 goto out;
1777 }
1778
1779 /* get mac addr from device tree */
1780 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1781 err = -ENODEV;
1782 goto out;
1783 }
1784 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1785
1786 mac->dma_if = mac_to_intf(mac);
1787 if (mac->dma_if < 0) {
1788 dev_err(&mac->pdev->dev, "Can't map DMA interface\n");
1789 err = -ENODEV;
1790 goto out;
1791 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001792
1793 switch (pdev->device) {
1794 case 0xa005:
1795 mac->type = MAC_TYPE_GMAC;
1796 break;
1797 case 0xa006:
1798 mac->type = MAC_TYPE_XAUI;
1799 break;
1800 default:
1801 err = -ENODEV;
1802 goto out;
1803 }
1804
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001805 dev->open = pasemi_mac_open;
1806 dev->stop = pasemi_mac_close;
1807 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001808 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johansson5cea73b2008-01-23 13:56:19 -06001809 dev->set_mac_address = pasemi_mac_set_mac_addr;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001810 dev->mtu = PE_DEF_MTU;
1811 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
1812 mac->bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + LOCAL_SKB_ALIGN + 128;
1813
1814 dev->change_mtu = pasemi_mac_change_mtu;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001815
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001816 if (err)
1817 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001818
Olof Johanssonceb51362007-05-08 00:47:49 -05001819 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1820
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001821 /* Enable most messages by default */
1822 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1823
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001824 err = register_netdev(dev);
1825
1826 if (err) {
1827 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1828 err);
1829 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001830 } else if netif_msg_probe(mac)
Olof Johansson72b05b92007-11-28 20:54:28 -06001831 printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001832 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
Olof Johansson72b05b92007-11-28 20:54:28 -06001833 mac->dma_if, print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001834
1835 return err;
1836
1837out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001838 if (mac->iob_pdev)
1839 pci_dev_put(mac->iob_pdev);
1840 if (mac->dma_pdev)
1841 pci_dev_put(mac->dma_pdev);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001842
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001843 free_netdev(dev);
1844out_disable_device:
1845 pci_disable_device(pdev);
1846 return err;
1847
1848}
1849
1850static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1851{
1852 struct net_device *netdev = pci_get_drvdata(pdev);
1853 struct pasemi_mac *mac;
1854
1855 if (!netdev)
1856 return;
1857
1858 mac = netdev_priv(netdev);
1859
1860 unregister_netdev(netdev);
1861
1862 pci_disable_device(pdev);
1863 pci_dev_put(mac->dma_pdev);
1864 pci_dev_put(mac->iob_pdev);
1865
Olof Johansson34c20622007-11-28 20:56:32 -06001866 pasemi_dma_free_chan(&mac->tx->chan);
1867 pasemi_dma_free_chan(&mac->rx->chan);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001868
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001869 pci_set_drvdata(pdev, NULL);
1870 free_netdev(netdev);
1871}
1872
1873static struct pci_device_id pasemi_mac_pci_tbl[] = {
1874 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1875 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001876 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001877};
1878
1879MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1880
1881static struct pci_driver pasemi_mac_driver = {
1882 .name = "pasemi_mac",
1883 .id_table = pasemi_mac_pci_tbl,
1884 .probe = pasemi_mac_probe,
1885 .remove = __devexit_p(pasemi_mac_remove),
1886};
1887
1888static void __exit pasemi_mac_cleanup_module(void)
1889{
1890 pci_unregister_driver(&pasemi_mac_driver);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001891}
1892
1893int pasemi_mac_init_module(void)
1894{
Olof Johansson34c20622007-11-28 20:56:32 -06001895 int err;
1896
1897 err = pasemi_dma_init();
1898 if (err)
1899 return err;
1900
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001901 return pci_register_driver(&pasemi_mac_driver);
1902}
1903
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001904module_init(pasemi_mac_init_module);
1905module_exit(pasemi_mac_cleanup_module);