blob: 8ab6ae0a61079dd28f606ce3ca4d637dfdf50e52 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Olof Johanssonf5cd7872007-01-31 21:43:54 -060024#include <linux/interrupt.h>
25#include <linux/dmaengine.h>
26#include <linux/delay.h>
27#include <linux/netdevice.h>
Grant Likely1dd2d062009-04-25 12:53:17 +000028#include <linux/of_mdio.h>
Olof Johanssonf5cd7872007-01-31 21:43:54 -060029#include <linux/etherdevice.h>
30#include <asm/dma-mapping.h>
31#include <linux/in.h>
32#include <linux/skbuff.h>
33
34#include <linux/ip.h>
35#include <linux/tcp.h>
36#include <net/checksum.h>
Olof Johansson28ae79f2007-11-28 20:57:27 -060037#include <linux/inet_lro.h>
Olof Johanssonf5cd7872007-01-31 21:43:54 -060038
Olof Johansson771f7402007-05-08 00:47:21 -050039#include <asm/irq.h>
Olof Johanssonaf289e82007-10-03 13:03:54 -050040#include <asm/firmware.h>
Olof Johansson40afa532007-11-28 20:56:04 -060041#include <asm/pasemi_dma.h>
Olof Johansson771f7402007-05-08 00:47:21 -050042
Olof Johanssonf5cd7872007-01-31 21:43:54 -060043#include "pasemi_mac.h"
44
Olof Johansson8dc121a2007-10-02 16:26:53 -050045/* We have our own align, since ppc64 in general has it at 0 because
46 * of design flaws in some of the server bridge chips. However, for
47 * PWRficient doing the unaligned copies is more expensive than doing
48 * unaligned DMA, so make sure the data is aligned instead.
49 */
50#define LOCAL_SKB_ALIGN 2
Olof Johanssonf5cd7872007-01-31 21:43:54 -060051
52/* TODO list
53 *
Olof Johanssonf5cd7872007-01-31 21:43:54 -060054 * - Multicast support
55 * - Large MTU support
Olof Johansson7ddeae22007-10-02 16:27:28 -050056 * - SW LRO
57 * - Multiqueue RX/TX
Olof Johanssonf5cd7872007-01-31 21:43:54 -060058 */
59
Olof Johansson28ae79f2007-11-28 20:57:27 -060060#define LRO_MAX_AGGR 64
61
Olof Johanssonef1ea0b2008-01-23 13:56:47 -060062#define PE_MIN_MTU 64
Olof Johansson8d636d82008-03-05 16:34:16 -060063#define PE_MAX_MTU 9000
Olof Johanssonef1ea0b2008-01-23 13:56:47 -060064#define PE_DEF_MTU ETH_DATA_LEN
65
Olof Johanssonceb51362007-05-08 00:47:49 -050066#define DEFAULT_MSG_ENABLE \
67 (NETIF_MSG_DRV | \
68 NETIF_MSG_PROBE | \
69 NETIF_MSG_LINK | \
70 NETIF_MSG_TIMER | \
71 NETIF_MSG_IFDOWN | \
72 NETIF_MSG_IFUP | \
73 NETIF_MSG_RX_ERR | \
74 NETIF_MSG_TX_ERR)
75
Olof Johanssonceb51362007-05-08 00:47:49 -050076MODULE_LICENSE("GPL");
77MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
78MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
79
80static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
81module_param(debug, int, 0);
82MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060083
Olof Johanssone37c7722008-02-20 20:57:59 -060084extern const struct ethtool_ops pasemi_mac_ethtool_ops;
85
Olof Johanssonaf289e82007-10-03 13:03:54 -050086static int translation_enabled(void)
87{
88#if defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
89 return 1;
90#else
91 return firmware_has_feature(FW_FEATURE_LPAR);
92#endif
93}
94
Olof Johansson34c20622007-11-28 20:56:32 -060095static void write_iob_reg(unsigned int reg, unsigned int val)
Olof Johanssona85b9422007-09-15 13:40:59 -070096{
Olof Johansson34c20622007-11-28 20:56:32 -060097 pasemi_write_iob_reg(reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -070098}
99
Olof Johansson5c153322007-11-28 20:56:41 -0600100static unsigned int read_mac_reg(const struct pasemi_mac *mac, unsigned int reg)
Olof Johanssona85b9422007-09-15 13:40:59 -0700101{
Olof Johansson34c20622007-11-28 20:56:32 -0600102 return pasemi_read_mac_reg(mac->dma_if, reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700103}
104
Olof Johansson5c153322007-11-28 20:56:41 -0600105static void write_mac_reg(const struct pasemi_mac *mac, unsigned int reg,
Olof Johanssona85b9422007-09-15 13:40:59 -0700106 unsigned int val)
107{
Olof Johansson34c20622007-11-28 20:56:32 -0600108 pasemi_write_mac_reg(mac->dma_if, reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700109}
110
Olof Johansson34c20622007-11-28 20:56:32 -0600111static unsigned int read_dma_reg(unsigned int reg)
Olof Johanssona85b9422007-09-15 13:40:59 -0700112{
Olof Johansson34c20622007-11-28 20:56:32 -0600113 return pasemi_read_dma_reg(reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700114}
115
Olof Johansson34c20622007-11-28 20:56:32 -0600116static void write_dma_reg(unsigned int reg, unsigned int val)
Olof Johanssona85b9422007-09-15 13:40:59 -0700117{
Olof Johansson34c20622007-11-28 20:56:32 -0600118 pasemi_write_dma_reg(reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700119}
120
Olof Johansson5c153322007-11-28 20:56:41 -0600121static struct pasemi_mac_rxring *rx_ring(const struct pasemi_mac *mac)
Olof Johansson72b05b92007-11-28 20:54:28 -0600122{
123 return mac->rx;
124}
125
Olof Johansson5c153322007-11-28 20:56:41 -0600126static struct pasemi_mac_txring *tx_ring(const struct pasemi_mac *mac)
Olof Johansson72b05b92007-11-28 20:54:28 -0600127{
128 return mac->tx;
129}
130
Olof Johansson5c153322007-11-28 20:56:41 -0600131static inline void prefetch_skb(const struct sk_buff *skb)
132{
133 const void *d = skb;
134
135 prefetch(d);
136 prefetch(d+64);
137 prefetch(d+128);
138 prefetch(d+192);
139}
140
Olof Johansson34c20622007-11-28 20:56:32 -0600141static int mac_to_intf(struct pasemi_mac *mac)
142{
143 struct pci_dev *pdev = mac->pdev;
144 u32 tmp;
145 int nintf, off, i, j;
146 int devfn = pdev->devfn;
147
148 tmp = read_dma_reg(PAS_DMA_CAP_IFI);
149 nintf = (tmp & PAS_DMA_CAP_IFI_NIN_M) >> PAS_DMA_CAP_IFI_NIN_S;
150 off = (tmp & PAS_DMA_CAP_IFI_IOFF_M) >> PAS_DMA_CAP_IFI_IOFF_S;
151
152 /* IOFF contains the offset to the registers containing the
153 * DMA interface-to-MAC-pci-id mappings, and NIN contains number
154 * of total interfaces. Each register contains 4 devfns.
155 * Just do a linear search until we find the devfn of the MAC
156 * we're trying to look up.
157 */
158
159 for (i = 0; i < (nintf+3)/4; i++) {
160 tmp = read_dma_reg(off+4*i);
161 for (j = 0; j < 4; j++) {
162 if (((tmp >> (8*j)) & 0xff) == devfn)
163 return i*4 + j;
164 }
165 }
166 return -1;
167}
168
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600169static void pasemi_mac_intf_disable(struct pasemi_mac *mac)
170{
171 unsigned int flags;
172
173 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
174 flags &= ~PAS_MAC_CFG_PCFG_PE;
175 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
176}
177
178static void pasemi_mac_intf_enable(struct pasemi_mac *mac)
179{
180 unsigned int flags;
181
182 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
183 flags |= PAS_MAC_CFG_PCFG_PE;
184 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
185}
186
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600187static int pasemi_get_mac_addr(struct pasemi_mac *mac)
188{
189 struct pci_dev *pdev = mac->pdev;
190 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500191 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600192 const u8 *maddr;
193 u8 addr[6];
194
195 if (!dn) {
196 dev_dbg(&pdev->dev,
197 "No device node for mac, not configuring\n");
198 return -ENOENT;
199 }
200
olof@lixom.net1af7f052007-05-12 14:57:46 -0500201 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500202
olof@lixom.net1af7f052007-05-12 14:57:46 -0500203 if (maddr && len == 6) {
204 memcpy(mac->mac_addr, maddr, 6);
205 return 0;
206 }
207
208 /* Some old versions of firmware mistakenly uses mac-address
209 * (and as a string) instead of a byte array in local-mac-address.
210 */
211
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500212 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700213 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500214
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600215 if (maddr == NULL) {
216 dev_warn(&pdev->dev,
217 "no mac address in device tree, not configuring\n");
218 return -ENOENT;
219 }
220
221 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
222 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
223 dev_warn(&pdev->dev,
224 "can't parse mac address, not configuring\n");
225 return -EINVAL;
226 }
227
olof@lixom.net1af7f052007-05-12 14:57:46 -0500228 memcpy(mac->mac_addr, addr, 6);
229
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600230 return 0;
231}
232
Olof Johansson5cea73b2008-01-23 13:56:19 -0600233static int pasemi_mac_set_mac_addr(struct net_device *dev, void *p)
234{
235 struct pasemi_mac *mac = netdev_priv(dev);
236 struct sockaddr *addr = p;
237 unsigned int adr0, adr1;
238
239 if (!is_valid_ether_addr(addr->sa_data))
240 return -EINVAL;
241
242 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
243
244 adr0 = dev->dev_addr[2] << 24 |
245 dev->dev_addr[3] << 16 |
246 dev->dev_addr[4] << 8 |
247 dev->dev_addr[5];
248 adr1 = read_mac_reg(mac, PAS_MAC_CFG_ADR1);
249 adr1 &= ~0xffff;
250 adr1 |= dev->dev_addr[0] << 8 | dev->dev_addr[1];
251
252 pasemi_mac_intf_disable(mac);
253 write_mac_reg(mac, PAS_MAC_CFG_ADR0, adr0);
254 write_mac_reg(mac, PAS_MAC_CFG_ADR1, adr1);
255 pasemi_mac_intf_enable(mac);
256
257 return 0;
258}
259
Olof Johansson28ae79f2007-11-28 20:57:27 -0600260static int get_skb_hdr(struct sk_buff *skb, void **iphdr,
261 void **tcph, u64 *hdr_flags, void *data)
262{
263 u64 macrx = (u64) data;
264 unsigned int ip_len;
265 struct iphdr *iph;
266
267 /* IPv4 header checksum failed */
268 if ((macrx & XCT_MACRX_HTY_M) != XCT_MACRX_HTY_IPV4_OK)
269 return -1;
270
271 /* non tcp packet */
272 skb_reset_network_header(skb);
273 iph = ip_hdr(skb);
274 if (iph->protocol != IPPROTO_TCP)
275 return -1;
276
277 ip_len = ip_hdrlen(skb);
278 skb_set_transport_header(skb, ip_len);
279 *tcph = tcp_hdr(skb);
280
281 /* check if ip header and tcp header are complete */
Roland Dreier77321232008-07-01 10:22:45 -0700282 if (ntohs(iph->tot_len) < ip_len + tcp_hdrlen(skb))
Olof Johansson28ae79f2007-11-28 20:57:27 -0600283 return -1;
284
285 *hdr_flags = LRO_IPV4 | LRO_TCP;
286 *iphdr = iph;
287
288 return 0;
289}
290
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500291static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
Olof Johansson7e9916e2007-11-28 20:57:45 -0600292 const int nfrags,
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500293 struct sk_buff *skb,
Olof Johansson5c153322007-11-28 20:56:41 -0600294 const dma_addr_t *dmas)
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500295{
296 int f;
Olof Johansson5c153322007-11-28 20:56:41 -0600297 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500298
Olof Johansson5c153322007-11-28 20:56:41 -0600299 pci_unmap_single(pdev, dmas[0], skb_headlen(skb), PCI_DMA_TODEVICE);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500300
301 for (f = 0; f < nfrags; f++) {
302 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
303
Olof Johansson5c153322007-11-28 20:56:41 -0600304 pci_unmap_page(pdev, dmas[f+1], frag->size, PCI_DMA_TODEVICE);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500305 }
306 dev_kfree_skb_irq(skb);
307
308 /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
309 * aligned up to a power of 2
310 */
311 return (nfrags + 3) & ~1;
312}
313
Olof Johansson8d636d82008-03-05 16:34:16 -0600314static struct pasemi_mac_csring *pasemi_mac_setup_csring(struct pasemi_mac *mac)
315{
316 struct pasemi_mac_csring *ring;
317 u32 val;
318 unsigned int cfg;
319 int chno;
320
321 ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_csring),
322 offsetof(struct pasemi_mac_csring, chan));
323
324 if (!ring) {
325 dev_err(&mac->pdev->dev, "Can't allocate checksum channel\n");
326 goto out_chan;
327 }
328
329 chno = ring->chan.chno;
330
331 ring->size = CS_RING_SIZE;
332 ring->next_to_fill = 0;
333
334 /* Allocate descriptors */
335 if (pasemi_dma_alloc_ring(&ring->chan, CS_RING_SIZE))
336 goto out_ring_desc;
337
338 write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno),
339 PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma));
340 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32);
341 val |= PAS_DMA_TXCHAN_BASEU_SIZ(CS_RING_SIZE >> 3);
342
343 write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val);
344
345 ring->events[0] = pasemi_dma_alloc_flag();
346 ring->events[1] = pasemi_dma_alloc_flag();
347 if (ring->events[0] < 0 || ring->events[1] < 0)
348 goto out_flags;
349
350 pasemi_dma_clear_flag(ring->events[0]);
351 pasemi_dma_clear_flag(ring->events[1]);
352
353 ring->fun = pasemi_dma_alloc_fun();
354 if (ring->fun < 0)
355 goto out_fun;
356
357 cfg = PAS_DMA_TXCHAN_CFG_TY_FUNC | PAS_DMA_TXCHAN_CFG_UP |
358 PAS_DMA_TXCHAN_CFG_TATTR(ring->fun) |
359 PAS_DMA_TXCHAN_CFG_LPSQ | PAS_DMA_TXCHAN_CFG_LPDQ;
360
361 if (translation_enabled())
362 cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
363
364 write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg);
365
366 /* enable channel */
367 pasemi_dma_start_chan(&ring->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
368 PAS_DMA_TXCHAN_TCMDSTA_DB |
369 PAS_DMA_TXCHAN_TCMDSTA_DE |
370 PAS_DMA_TXCHAN_TCMDSTA_DA);
371
372 return ring;
373
374out_fun:
375out_flags:
376 if (ring->events[0] >= 0)
377 pasemi_dma_free_flag(ring->events[0]);
378 if (ring->events[1] >= 0)
379 pasemi_dma_free_flag(ring->events[1]);
380 pasemi_dma_free_ring(&ring->chan);
381out_ring_desc:
382 pasemi_dma_free_chan(&ring->chan);
383out_chan:
384
385 return NULL;
386}
387
388static void pasemi_mac_setup_csrings(struct pasemi_mac *mac)
389{
390 int i;
391 mac->cs[0] = pasemi_mac_setup_csring(mac);
392 if (mac->type == MAC_TYPE_XAUI)
393 mac->cs[1] = pasemi_mac_setup_csring(mac);
394 else
395 mac->cs[1] = 0;
396
397 for (i = 0; i < MAX_CS; i++)
398 if (mac->cs[i])
399 mac->num_cs++;
400}
401
402static void pasemi_mac_free_csring(struct pasemi_mac_csring *csring)
403{
404 pasemi_dma_stop_chan(&csring->chan);
405 pasemi_dma_free_flag(csring->events[0]);
406 pasemi_dma_free_flag(csring->events[1]);
407 pasemi_dma_free_ring(&csring->chan);
408 pasemi_dma_free_chan(&csring->chan);
Olof Johansson1724ac22008-03-25 09:58:40 -0500409 pasemi_dma_free_fun(csring->fun);
Olof Johansson8d636d82008-03-05 16:34:16 -0600410}
411
Olof Johansson5c153322007-11-28 20:56:41 -0600412static int pasemi_mac_setup_rx_resources(const struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600413{
414 struct pasemi_mac_rxring *ring;
415 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson34c20622007-11-28 20:56:32 -0600416 int chno;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500417 unsigned int cfg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600418
Olof Johansson34c20622007-11-28 20:56:32 -0600419 ring = pasemi_dma_alloc_chan(RXCHAN, sizeof(struct pasemi_mac_rxring),
420 offsetof(struct pasemi_mac_rxring, chan));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600421
Olof Johansson34c20622007-11-28 20:56:32 -0600422 if (!ring) {
423 dev_err(&mac->pdev->dev, "Can't allocate RX channel\n");
424 goto out_chan;
425 }
426 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600427
428 spin_lock_init(&ring->lock);
429
Olof Johansson021fa222007-08-22 09:13:11 -0500430 ring->size = RX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500431 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600432 RX_RING_SIZE, GFP_KERNEL);
433
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500434 if (!ring->ring_info)
435 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600436
437 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600438 if (pasemi_dma_alloc_ring(&ring->chan, RX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500439 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600440
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600441 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
442 RX_RING_SIZE * sizeof(u64),
443 &ring->buf_dma, GFP_KERNEL);
444 if (!ring->buffers)
Olof Johansson34c20622007-11-28 20:56:32 -0600445 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600446
447 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
448
Olof Johansson34c20622007-11-28 20:56:32 -0600449 write_dma_reg(PAS_DMA_RXCHAN_BASEL(chno),
450 PAS_DMA_RXCHAN_BASEL_BRBL(ring->chan.ring_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600451
Olof Johansson34c20622007-11-28 20:56:32 -0600452 write_dma_reg(PAS_DMA_RXCHAN_BASEU(chno),
453 PAS_DMA_RXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32) |
454 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600455
Olof Johansson5c153322007-11-28 20:56:41 -0600456 cfg = PAS_DMA_RXCHAN_CFG_HBU(2);
Olof Johanssonaf289e82007-10-03 13:03:54 -0500457
458 if (translation_enabled())
459 cfg |= PAS_DMA_RXCHAN_CFG_CTR;
460
Olof Johansson34c20622007-11-28 20:56:32 -0600461 write_dma_reg(PAS_DMA_RXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600462
Olof Johansson34c20622007-11-28 20:56:32 -0600463 write_dma_reg(PAS_DMA_RXINT_BASEL(mac->dma_if),
464 PAS_DMA_RXINT_BASEL_BRBL(ring->buf_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600465
Olof Johansson34c20622007-11-28 20:56:32 -0600466 write_dma_reg(PAS_DMA_RXINT_BASEU(mac->dma_if),
467 PAS_DMA_RXINT_BASEU_BRBH(ring->buf_dma >> 32) |
468 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600469
Olof Johansson5c153322007-11-28 20:56:41 -0600470 cfg = PAS_DMA_RXINT_CFG_DHL(2) | PAS_DMA_RXINT_CFG_L2 |
Olof Johanssonaf289e82007-10-03 13:03:54 -0500471 PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP |
472 PAS_DMA_RXINT_CFG_HEN;
473
474 if (translation_enabled())
475 cfg |= PAS_DMA_RXINT_CFG_ITRR | PAS_DMA_RXINT_CFG_ITR;
476
Olof Johansson34c20622007-11-28 20:56:32 -0600477 write_dma_reg(PAS_DMA_RXINT_CFG(mac->dma_if), cfg);
Olof Johanssonc0efd522007-08-22 09:12:52 -0500478
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600479 ring->next_to_fill = 0;
480 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600481 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600482 mac->rx = ring;
483
484 return 0;
485
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500486out_ring_desc:
487 kfree(ring->ring_info);
488out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600489 pasemi_dma_free_chan(&ring->chan);
490out_chan:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600491 return -ENOMEM;
492}
493
Olof Johansson72b05b92007-11-28 20:54:28 -0600494static struct pasemi_mac_txring *
Olof Johansson5c153322007-11-28 20:56:41 -0600495pasemi_mac_setup_tx_resources(const struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600496{
497 struct pasemi_mac *mac = netdev_priv(dev);
498 u32 val;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600499 struct pasemi_mac_txring *ring;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500500 unsigned int cfg;
Olof Johansson34c20622007-11-28 20:56:32 -0600501 int chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600502
Olof Johansson34c20622007-11-28 20:56:32 -0600503 ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_txring),
504 offsetof(struct pasemi_mac_txring, chan));
505
506 if (!ring) {
507 dev_err(&mac->pdev->dev, "Can't allocate TX channel\n");
508 goto out_chan;
509 }
510
511 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600512
513 spin_lock_init(&ring->lock);
514
Olof Johansson021fa222007-08-22 09:13:11 -0500515 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500516 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600517 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500518 if (!ring->ring_info)
519 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600520
521 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600522 if (pasemi_dma_alloc_ring(&ring->chan, TX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500523 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600524
Olof Johansson34c20622007-11-28 20:56:32 -0600525 write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno),
526 PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma));
527 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500528 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600529
Olof Johansson34c20622007-11-28 20:56:32 -0600530 write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600531
Olof Johanssonaf289e82007-10-03 13:03:54 -0500532 cfg = PAS_DMA_TXCHAN_CFG_TY_IFACE |
533 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
534 PAS_DMA_TXCHAN_CFG_UP |
Olof Johansson8d636d82008-03-05 16:34:16 -0600535 PAS_DMA_TXCHAN_CFG_WT(4);
Olof Johanssonaf289e82007-10-03 13:03:54 -0500536
537 if (translation_enabled())
538 cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
539
Olof Johansson34c20622007-11-28 20:56:32 -0600540 write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600541
Olof Johansson021fa222007-08-22 09:13:11 -0500542 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600543 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600544 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600545
Olof Johansson72b05b92007-11-28 20:54:28 -0600546 return ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600547
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500548out_ring_desc:
549 kfree(ring->ring_info);
550out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600551 pasemi_dma_free_chan(&ring->chan);
552out_chan:
Olof Johansson72b05b92007-11-28 20:54:28 -0600553 return NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600554}
555
Olof Johansson72b05b92007-11-28 20:54:28 -0600556static void pasemi_mac_free_tx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600557{
Olof Johansson72b05b92007-11-28 20:54:28 -0600558 struct pasemi_mac_txring *txring = tx_ring(mac);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500559 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600560 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500561 dma_addr_t dmas[MAX_SKB_FRAGS+1];
Olof Johansson7e9916e2007-11-28 20:57:45 -0600562 int freed, nfrags;
Olof Johanssonad5da102007-10-02 16:27:15 -0500563 int start, limit;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600564
Olof Johansson72b05b92007-11-28 20:54:28 -0600565 start = txring->next_to_clean;
566 limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500567
568 /* Compensate for when fill has wrapped and clean has not */
569 if (start > limit)
570 limit += TX_RING_SIZE;
571
572 for (i = start; i < limit; i += freed) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600573 info = &txring->ring_info[(i+1) & (TX_RING_SIZE-1)];
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500574 if (info->dma && info->skb) {
Olof Johansson7e9916e2007-11-28 20:57:45 -0600575 nfrags = skb_shinfo(info->skb)->nr_frags;
576 for (j = 0; j <= nfrags; j++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600577 dmas[j] = txring->ring_info[(i+1+j) &
578 (TX_RING_SIZE-1)].dma;
Olof Johansson7e9916e2007-11-28 20:57:45 -0600579 freed = pasemi_mac_unmap_tx_skb(mac, nfrags,
580 info->skb, dmas);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500581 } else
582 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600583 }
584
Olof Johansson72b05b92007-11-28 20:54:28 -0600585 kfree(txring->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600586 pasemi_dma_free_chan(&txring->chan);
587
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600588}
589
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600590static void pasemi_mac_free_rx_buffers(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600591{
Olof Johansson72b05b92007-11-28 20:54:28 -0600592 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600593 unsigned int i;
594 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600595
596 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600597 info = &RX_DESC_INFO(rx, i);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500598 if (info->skb && info->dma) {
599 pci_unmap_single(mac->dma_pdev,
600 info->dma,
601 info->skb->len,
602 PCI_DMA_FROMDEVICE);
603 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600604 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500605 info->dma = 0;
606 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600607 }
608
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500609 for (i = 0; i < RX_RING_SIZE; i++)
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600610 RX_BUFF(rx, i) = 0;
611}
612
613static void pasemi_mac_free_rx_resources(struct pasemi_mac *mac)
614{
615 pasemi_mac_free_rx_buffers(mac);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500616
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600617 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
Olof Johansson72b05b92007-11-28 20:54:28 -0600618 rx_ring(mac)->buffers, rx_ring(mac)->buf_dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600619
Olof Johansson72b05b92007-11-28 20:54:28 -0600620 kfree(rx_ring(mac)->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600621 pasemi_dma_free_chan(&rx_ring(mac)->chan);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600622 mac->rx = NULL;
623}
624
Olof Johansson5c153322007-11-28 20:56:41 -0600625static void pasemi_mac_replenish_rx_ring(const struct net_device *dev,
626 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600627{
Olof Johansson5c153322007-11-28 20:56:41 -0600628 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -0600629 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500630 int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600631
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500632 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600633 return;
634
Olof Johansson72b05b92007-11-28 20:54:28 -0600635 fill = rx_ring(mac)->next_to_fill;
Olof Johansson928773c2007-09-26 16:25:06 -0500636 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600637 struct pasemi_mac_buffer *info = &RX_DESC_INFO(rx, fill);
638 u64 *buff = &RX_BUFF(rx, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600639 struct sk_buff *skb;
640 dma_addr_t dma;
641
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500642 /* Entry in use? */
643 WARN_ON(*buff);
644
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600645 skb = dev_alloc_skb(mac->bufsz);
Olof Johansson5d894942007-11-28 20:57:56 -0600646 skb_reserve(skb, LOCAL_SKB_ALIGN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600647
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500648 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600649 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600650
Olof Johansson8dc121a2007-10-02 16:26:53 -0500651 dma = pci_map_single(mac->dma_pdev, skb->data,
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600652 mac->bufsz - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600653 PCI_DMA_FROMDEVICE);
654
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700655 if (unlikely(pci_dma_mapping_error(mac->dma_pdev, dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600656 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600657 break;
658 }
659
660 info->skb = skb;
661 info->dma = dma;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600662 *buff = XCT_RXB_LEN(mac->bufsz) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500663 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600664 }
665
666 wmb();
667
Olof Johansson34c20622007-11-28 20:56:32 -0600668 write_dma_reg(PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600669
Olof Johansson72b05b92007-11-28 20:54:28 -0600670 rx_ring(mac)->next_to_fill = (rx_ring(mac)->next_to_fill + count) &
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500671 (RX_RING_SIZE - 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600672}
673
Olof Johansson5c153322007-11-28 20:56:41 -0600674static void pasemi_mac_restart_rx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500675{
Olof Johansson906674a2007-11-28 20:57:09 -0600676 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johansson52a94352007-05-12 18:01:09 -0500677 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500678 /* Re-enable packet count interrupts: finally
679 * ack the packet count interrupt we got in rx_intr.
680 */
681
Olof Johansson906674a2007-11-28 20:57:09 -0600682 pcnt = *rx->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500683
Olof Johansson52a94352007-05-12 18:01:09 -0500684 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500685
Olof Johansson906674a2007-11-28 20:57:09 -0600686 if (*rx->chan.status & PAS_STATUS_TIMER)
687 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
688
Olof Johansson34c20622007-11-28 20:56:32 -0600689 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(mac->rx->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500690}
691
Olof Johansson5c153322007-11-28 20:56:41 -0600692static void pasemi_mac_restart_tx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500693{
Olof Johansson52a94352007-05-12 18:01:09 -0500694 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500695
696 /* Re-enable packet count interrupts */
Olof Johansson34c20622007-11-28 20:56:32 -0600697 pcnt = *tx_ring(mac)->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500698
Olof Johansson52a94352007-05-12 18:01:09 -0500699 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500700
Olof Johansson34c20622007-11-28 20:56:32 -0600701 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(tx_ring(mac)->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500702}
703
704
Olof Johansson5c153322007-11-28 20:56:41 -0600705static inline void pasemi_mac_rx_error(const struct pasemi_mac *mac,
706 const u64 macrx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500707{
708 unsigned int rcmdsta, ccmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600709 struct pasemi_dmachan *chan = &rx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500710
711 if (!netif_msg_rx_err(mac))
712 return;
713
Olof Johansson34c20622007-11-28 20:56:32 -0600714 rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
715 ccmdsta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500716
Ingo Molnarfe333322009-01-06 14:26:03 +0000717 printk(KERN_ERR "pasemi_mac: rx error. macrx %016llx, rx status %llx\n",
Olof Johansson34c20622007-11-28 20:56:32 -0600718 macrx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500719
720 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
721 rcmdsta, ccmdsta);
722}
723
Olof Johansson5c153322007-11-28 20:56:41 -0600724static inline void pasemi_mac_tx_error(const struct pasemi_mac *mac,
725 const u64 mactx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500726{
727 unsigned int cmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600728 struct pasemi_dmachan *chan = &tx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500729
730 if (!netif_msg_tx_err(mac))
731 return;
732
Olof Johansson34c20622007-11-28 20:56:32 -0600733 cmdsta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500734
Ingo Molnarfe333322009-01-06 14:26:03 +0000735 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016llx, "\
736 "tx status 0x%016llx\n", mactx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500737
738 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
739}
740
Olof Johansson5c153322007-11-28 20:56:41 -0600741static int pasemi_mac_clean_rx(struct pasemi_mac_rxring *rx,
742 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600743{
Olof Johansson5c153322007-11-28 20:56:41 -0600744 const struct pasemi_dmachan *chan = &rx->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600745 struct pasemi_mac *mac = rx->mac;
Olof Johansson5c153322007-11-28 20:56:41 -0600746 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500747 unsigned int n;
Olof Johansson5c153322007-11-28 20:56:41 -0600748 int count, buf_index, tot_bytes, packets;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500749 struct pasemi_mac_buffer *info;
750 struct sk_buff *skb;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500751 unsigned int len;
Olof Johansson5c153322007-11-28 20:56:41 -0600752 u64 macrx, eval;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500753 dma_addr_t dma;
Olof Johansson5c153322007-11-28 20:56:41 -0600754
755 tot_bytes = 0;
756 packets = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600757
Olof Johansson72b05b92007-11-28 20:54:28 -0600758 spin_lock(&rx->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600759
Olof Johansson72b05b92007-11-28 20:54:28 -0600760 n = rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600761
Olof Johansson72b05b92007-11-28 20:54:28 -0600762 prefetch(&RX_DESC(rx, n));
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500763
764 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600765 macrx = RX_DESC(rx, n);
Olof Johansson5c153322007-11-28 20:56:41 -0600766 prefetch(&RX_DESC(rx, n+4));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600767
Olof Johansson69c29d82007-10-02 16:24:51 -0500768 if ((macrx & XCT_MACRX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600769 (*chan->status & PAS_STATUS_ERROR))
Olof Johansson69c29d82007-10-02 16:24:51 -0500770 pasemi_mac_rx_error(mac, macrx);
771
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500772 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600773 break;
774
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600775 info = NULL;
776
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500777 BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600778
Olof Johansson72b05b92007-11-28 20:54:28 -0600779 eval = (RX_DESC(rx, n+1) & XCT_RXRES_8B_EVAL_M) >>
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500780 XCT_RXRES_8B_EVAL_S;
781 buf_index = eval-1;
782
Olof Johansson72b05b92007-11-28 20:54:28 -0600783 dma = (RX_DESC(rx, n+2) & XCT_PTR_ADDR_M);
784 info = &RX_DESC_INFO(rx, buf_index);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500785
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500786 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600787
Olof Johansson5c153322007-11-28 20:56:41 -0600788 prefetch_skb(skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600789
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500790 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600791
Olof Johanssonef1ea0b2008-01-23 13:56:47 -0600792 pci_unmap_single(pdev, dma, mac->bufsz - LOCAL_SKB_ALIGN,
Olof Johansson5c153322007-11-28 20:56:41 -0600793 PCI_DMA_FROMDEVICE);
Olof Johansson32bee772007-11-06 22:21:38 -0600794
795 if (macrx & XCT_MACRX_CRC) {
796 /* CRC error flagged */
797 mac->netdev->stats.rx_errors++;
798 mac->netdev->stats.rx_crc_errors++;
Olof Johansson4352d822007-12-03 21:34:14 -0600799 /* No need to free skb, it'll be reused */
Olof Johansson32bee772007-11-06 22:21:38 -0600800 goto next;
801 }
802
Olof Johansson5d894942007-11-28 20:57:56 -0600803 info->skb = NULL;
Olof Johanssonad5da102007-10-02 16:27:15 -0500804 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500805
Olof Johansson26fcfa92007-08-22 09:12:59 -0500806 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500807 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500808 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600809 XCT_MACRX_CSUM_S;
810 } else
811 skb->ip_summed = CHECKSUM_NONE;
812
Olof Johansson5c153322007-11-28 20:56:41 -0600813 packets++;
814 tot_bytes += len;
815
816 /* Don't include CRC */
817 skb_put(skb, len-4);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600818
Olof Johansson26fcfa92007-08-22 09:12:59 -0500819 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johansson28ae79f2007-11-28 20:57:27 -0600820 lro_receive_skb(&mac->lro_mgr, skb, (void *)macrx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600821
Olof Johansson32bee772007-11-06 22:21:38 -0600822next:
Olof Johansson72b05b92007-11-28 20:54:28 -0600823 RX_DESC(rx, n) = 0;
824 RX_DESC(rx, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500825
Olof Johanssonad5da102007-10-02 16:27:15 -0500826 /* Need to zero it out since hardware doesn't, since the
827 * replenish loop uses it to tell when it's done.
828 */
Olof Johansson72b05b92007-11-28 20:54:28 -0600829 RX_BUFF(rx, buf_index) = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500830
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500831 n += 4;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600832 }
833
Olof Johansson9a50beb2007-10-02 16:26:30 -0500834 if (n > RX_RING_SIZE) {
835 /* Errata 5971 workaround: L2 target of headers */
Olof Johansson34c20622007-11-28 20:56:32 -0600836 write_iob_reg(PAS_IOB_COM_PKTHDRCNT, 0);
Olof Johansson9a50beb2007-10-02 16:26:30 -0500837 n &= (RX_RING_SIZE-1);
838 }
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500839
Olof Johansson72b05b92007-11-28 20:54:28 -0600840 rx_ring(mac)->next_to_clean = n;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500841
Olof Johansson28ae79f2007-11-28 20:57:27 -0600842 lro_flush_all(&mac->lro_mgr);
843
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500844 /* Increase is in number of 16-byte entries, and since each descriptor
845 * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
846 * count*2.
847 */
Olof Johansson34c20622007-11-28 20:56:32 -0600848 write_dma_reg(PAS_DMA_RXCHAN_INCR(mac->rx->chan.chno), count << 1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500849
850 pasemi_mac_replenish_rx_ring(mac->netdev, count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600851
Olof Johansson5c153322007-11-28 20:56:41 -0600852 mac->netdev->stats.rx_bytes += tot_bytes;
853 mac->netdev->stats.rx_packets += packets;
854
Olof Johansson72b05b92007-11-28 20:54:28 -0600855 spin_unlock(&rx_ring(mac)->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600856
857 return count;
858}
859
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500860/* Can't make this too large or we blow the kernel stack limits */
861#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
862
Olof Johansson72b05b92007-11-28 20:54:28 -0600863static int pasemi_mac_clean_tx(struct pasemi_mac_txring *txring)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600864{
Olof Johansson34c20622007-11-28 20:56:32 -0600865 struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600866 struct pasemi_mac *mac = txring->mac;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500867 int i, j;
Olof Johanssonad5da102007-10-02 16:27:15 -0500868 unsigned int start, descr_count, buf_count, batch_limit;
869 unsigned int ring_limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500870 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500871 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500872 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
873 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johansson7e9916e2007-11-28 20:57:45 -0600874 int nf[TX_CLEAN_BATCHSIZE];
875 int nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600876
Olof Johansson02df6cf2007-08-22 09:13:03 -0500877 total_count = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500878 batch_limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500879restart:
Olof Johansson72b05b92007-11-28 20:54:28 -0600880 spin_lock_irqsave(&txring->lock, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600881
Olof Johansson72b05b92007-11-28 20:54:28 -0600882 start = txring->next_to_clean;
883 ring_limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500884
Olof Johansson7e9916e2007-11-28 20:57:45 -0600885 prefetch(&TX_DESC_INFO(txring, start+1).skb);
886
Olof Johanssonad5da102007-10-02 16:27:15 -0500887 /* Compensate for when fill has wrapped but clean has not */
888 if (start > ring_limit)
889 ring_limit += TX_RING_SIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500890
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500891 buf_count = 0;
892 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600893
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500894 for (i = start;
Olof Johanssonad5da102007-10-02 16:27:15 -0500895 descr_count < batch_limit && i < ring_limit;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500896 i += buf_count) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600897 u64 mactx = TX_DESC(txring, i);
Olof Johanssonad5da102007-10-02 16:27:15 -0500898 struct sk_buff *skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500899
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500900 if ((mactx & XCT_MACTX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600901 (*chan->status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500902 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500903
Olof Johansson8d636d82008-03-05 16:34:16 -0600904 /* Skip over control descriptors */
905 if (!(mactx & XCT_MACTX_LLEN_M)) {
906 TX_DESC(txring, i) = 0;
907 TX_DESC(txring, i+1) = 0;
908 buf_count = 2;
909 continue;
910 }
911
912 skb = TX_DESC_INFO(txring, i+1).skb;
913 nr_frags = TX_DESC_INFO(txring, i).dma;
914
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500915 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500916 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600917 break;
918
Olof Johansson7e9916e2007-11-28 20:57:45 -0600919 buf_count = 2 + nr_frags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500920 /* Since we always fill with an even number of entries, make
921 * sure we skip any unused one at the end as well.
922 */
923 if (buf_count & 1)
924 buf_count++;
Olof Johansson7e9916e2007-11-28 20:57:45 -0600925
926 for (j = 0; j <= nr_frags; j++)
927 dmas[descr_count][j] = TX_DESC_INFO(txring, i+1+j).dma;
928
929 skbs[descr_count] = skb;
930 nf[descr_count] = nr_frags;
931
932 TX_DESC(txring, i) = 0;
933 TX_DESC(txring, i+1) = 0;
934
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500935 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600936 }
Olof Johansson72b05b92007-11-28 20:54:28 -0600937 txring->next_to_clean = i & (TX_RING_SIZE-1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500938
Olof Johansson72b05b92007-11-28 20:54:28 -0600939 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500940 netif_wake_queue(mac->netdev);
941
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500942 for (i = 0; i < descr_count; i++)
Olof Johansson7e9916e2007-11-28 20:57:45 -0600943 pasemi_mac_unmap_tx_skb(mac, nf[i], skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500944
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500945 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500946
947 /* If the batch was full, try to clean more */
Olof Johanssonad5da102007-10-02 16:27:15 -0500948 if (descr_count == batch_limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500949 goto restart;
950
951 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600952}
953
954
955static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
956{
Olof Johansson5c153322007-11-28 20:56:41 -0600957 const struct pasemi_mac_rxring *rxring = data;
Olof Johansson34c20622007-11-28 20:56:32 -0600958 struct pasemi_mac *mac = rxring->mac;
Olof Johansson5c153322007-11-28 20:56:41 -0600959 const struct pasemi_dmachan *chan = &rxring->chan;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600960 unsigned int reg;
961
Olof Johansson34c20622007-11-28 20:56:32 -0600962 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600963 return IRQ_NONE;
964
Olof Johansson6dfa7522007-05-08 00:47:32 -0500965 /* Don't reset packet count so it won't fire again but clear
966 * all others.
967 */
968
Olof Johansson6dfa7522007-05-08 00:47:32 -0500969 reg = 0;
Olof Johansson34c20622007-11-28 20:56:32 -0600970 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500971 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600972 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500973 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600974
Ben Hutchings288379f2009-01-19 16:43:59 -0800975 napi_schedule(&mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500976
Olof Johansson34c20622007-11-28 20:56:32 -0600977 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600978
979 return IRQ_HANDLED;
980}
981
Olof Johansson61cec3b2007-11-28 20:56:54 -0600982#define TX_CLEAN_INTERVAL HZ
983
984static void pasemi_mac_tx_timer(unsigned long data)
985{
986 struct pasemi_mac_txring *txring = (struct pasemi_mac_txring *)data;
987 struct pasemi_mac *mac = txring->mac;
988
989 pasemi_mac_clean_tx(txring);
990
991 mod_timer(&txring->clean_timer, jiffies + TX_CLEAN_INTERVAL);
992
993 pasemi_mac_restart_tx_intr(mac);
994}
995
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600996static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
997{
Olof Johansson72b05b92007-11-28 20:54:28 -0600998 struct pasemi_mac_txring *txring = data;
Olof Johansson5c153322007-11-28 20:56:41 -0600999 const struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson61cec3b2007-11-28 20:56:54 -06001000 struct pasemi_mac *mac = txring->mac;
1001 unsigned int reg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001002
Olof Johansson34c20622007-11-28 20:56:32 -06001003 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001004 return IRQ_NONE;
1005
Olof Johansson61cec3b2007-11-28 20:56:54 -06001006 reg = 0;
Olof Johansson6dfa7522007-05-08 00:47:32 -05001007
Olof Johansson34c20622007-11-28 20:56:32 -06001008 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -05001009 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -06001010 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -05001011 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001012
Olof Johansson61cec3b2007-11-28 20:56:54 -06001013 mod_timer(&txring->clean_timer, jiffies + (TX_CLEAN_INTERVAL)*2);
1014
Ben Hutchings288379f2009-01-19 16:43:59 -08001015 napi_schedule(&mac->napi);
Olof Johansson61cec3b2007-11-28 20:56:54 -06001016
1017 if (reg)
1018 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001019
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001020 return IRQ_HANDLED;
1021}
1022
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001023static void pasemi_adjust_link(struct net_device *dev)
1024{
1025 struct pasemi_mac *mac = netdev_priv(dev);
1026 int msg;
1027 unsigned int flags;
1028 unsigned int new_flags;
1029
1030 if (!mac->phydev->link) {
1031 /* If no link, MAC speed settings don't matter. Just report
1032 * link down and return.
1033 */
1034 if (mac->link && netif_msg_link(mac))
1035 printk(KERN_INFO "%s: Link is down.\n", dev->name);
1036
1037 netif_carrier_off(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001038 pasemi_mac_intf_disable(mac);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001039 mac->link = 0;
1040
1041 return;
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001042 } else {
1043 pasemi_mac_intf_enable(mac);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001044 netif_carrier_on(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001045 }
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001046
Olof Johanssona85b9422007-09-15 13:40:59 -07001047 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001048 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
1049 PAS_MAC_CFG_PCFG_TSR_M);
1050
1051 if (!mac->phydev->duplex)
1052 new_flags |= PAS_MAC_CFG_PCFG_HD;
1053
1054 switch (mac->phydev->speed) {
1055 case 1000:
1056 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
1057 PAS_MAC_CFG_PCFG_TSR_1G;
1058 break;
1059 case 100:
1060 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
1061 PAS_MAC_CFG_PCFG_TSR_100M;
1062 break;
1063 case 10:
1064 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
1065 PAS_MAC_CFG_PCFG_TSR_10M;
1066 break;
1067 default:
1068 printk("Unsupported speed %d\n", mac->phydev->speed);
1069 }
1070
1071 /* Print on link or speed/duplex change */
1072 msg = mac->link != mac->phydev->link || flags != new_flags;
1073
1074 mac->duplex = mac->phydev->duplex;
1075 mac->speed = mac->phydev->speed;
1076 mac->link = mac->phydev->link;
1077
1078 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -07001079 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001080
1081 if (msg && netif_msg_link(mac))
1082 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
1083 dev->name, mac->speed, mac->duplex ? "full" : "half");
1084}
1085
1086static int pasemi_mac_phy_init(struct net_device *dev)
1087{
1088 struct pasemi_mac *mac = netdev_priv(dev);
1089 struct device_node *dn, *phy_dn;
1090 struct phy_device *phydev;
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001091
1092 dn = pci_device_to_OF_node(mac->pdev);
Grant Likely1dd2d062009-04-25 12:53:17 +00001093 phy_dn = of_parse_phandle(dn, "phy-handle", 0);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001094 of_node_put(phy_dn);
1095
1096 mac->link = 0;
1097 mac->speed = 0;
1098 mac->duplex = -1;
1099
Grant Likely1dd2d062009-04-25 12:53:17 +00001100 phydev = of_phy_connect(dev, phy_dn, &pasemi_adjust_link, 0,
1101 PHY_INTERFACE_MODE_SGMII);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001102
1103 if (IS_ERR(phydev)) {
1104 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
1105 return PTR_ERR(phydev);
1106 }
1107
1108 mac->phydev = phydev;
1109
1110 return 0;
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001111}
1112
1113
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001114static int pasemi_mac_open(struct net_device *dev)
1115{
1116 struct pasemi_mac *mac = netdev_priv(dev);
1117 unsigned int flags;
Olof Johanssone37c7722008-02-20 20:57:59 -06001118 int i, ret;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001119
1120 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
1121 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
1122 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
1123
Olof Johanssona85b9422007-09-15 13:40:59 -07001124 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001125
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001126 ret = pasemi_mac_setup_rx_resources(dev);
1127 if (ret)
1128 goto out_rx_resources;
1129
Olof Johansson34c20622007-11-28 20:56:32 -06001130 mac->tx = pasemi_mac_setup_tx_resources(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001131
1132 if (!mac->tx)
1133 goto out_tx_ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001134
Olof Johansson1724ac22008-03-25 09:58:40 -05001135 /* We might already have allocated rings in case mtu was changed
1136 * before interface was brought up.
1137 */
1138 if (dev->mtu > 1500 && !mac->num_cs) {
Olof Johansson8d636d82008-03-05 16:34:16 -06001139 pasemi_mac_setup_csrings(mac);
1140 if (!mac->num_cs)
1141 goto out_tx_ring;
1142 }
1143
Olof Johanssone37c7722008-02-20 20:57:59 -06001144 /* Zero out rmon counters */
1145 for (i = 0; i < 32; i++)
1146 write_mac_reg(mac, PAS_MAC_RMON(i), 0);
1147
Olof Johansson906674a2007-11-28 20:57:09 -06001148 /* 0x3ff with 33MHz clock is about 31us */
1149 write_iob_reg(PAS_IOB_DMA_COM_TIMEOUTCFG,
1150 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0x3ff));
1151
Olof Johansson34c20622007-11-28 20:56:32 -06001152 write_iob_reg(PAS_IOB_DMA_RXCH_CFG(mac->rx->chan.chno),
Olof Johansson28ae79f2007-11-28 20:57:27 -06001153 PAS_IOB_DMA_RXCH_CFG_CNTTH(256));
Olof Johansson34c20622007-11-28 20:56:32 -06001154
1155 write_iob_reg(PAS_IOB_DMA_TXCH_CFG(mac->tx->chan.chno),
Olof Johansson61cec3b2007-11-28 20:56:54 -06001156 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johansson34c20622007-11-28 20:56:32 -06001157
Olof Johanssona85b9422007-09-15 13:40:59 -07001158 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
Olof Johansson34c20622007-11-28 20:56:32 -06001159 PAS_MAC_IPC_CHNL_DCHNO(mac->rx->chan.chno) |
1160 PAS_MAC_IPC_CHNL_BCH(mac->rx->chan.chno));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001161
1162 /* enable rx if */
Olof Johansson34c20622007-11-28 20:56:32 -06001163 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1164 PAS_DMA_RXINT_RCMDSTA_EN |
1165 PAS_DMA_RXINT_RCMDSTA_DROPS_M |
1166 PAS_DMA_RXINT_RCMDSTA_BP |
1167 PAS_DMA_RXINT_RCMDSTA_OO |
1168 PAS_DMA_RXINT_RCMDSTA_BT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001169
1170 /* enable rx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001171 pasemi_dma_start_chan(&rx_ring(mac)->chan, PAS_DMA_RXCHAN_CCMDSTA_DU |
1172 PAS_DMA_RXCHAN_CCMDSTA_OD |
1173 PAS_DMA_RXCHAN_CCMDSTA_FD |
1174 PAS_DMA_RXCHAN_CCMDSTA_DT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001175
1176 /* enable tx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001177 pasemi_dma_start_chan(&tx_ring(mac)->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
1178 PAS_DMA_TXCHAN_TCMDSTA_DB |
1179 PAS_DMA_TXCHAN_TCMDSTA_DE |
1180 PAS_DMA_TXCHAN_TCMDSTA_DA);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001181
Olof Johansson928773c2007-09-26 16:25:06 -05001182 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001183
Olof Johansson34c20622007-11-28 20:56:32 -06001184 write_dma_reg(PAS_DMA_RXCHAN_INCR(rx_ring(mac)->chan.chno),
1185 RX_RING_SIZE>>1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -05001186
Olof Johansson72b05b92007-11-28 20:54:28 -06001187 /* Clear out any residual packet count state from firmware */
1188 pasemi_mac_restart_rx_intr(mac);
1189 pasemi_mac_restart_tx_intr(mac);
1190
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001191 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
Olof Johansson36033762007-09-26 16:24:42 -05001192
1193 if (mac->type == MAC_TYPE_GMAC)
1194 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
1195 else
1196 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
1197
1198 /* Enable interface in MAC */
1199 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
1200
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001201 ret = pasemi_mac_phy_init(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001202 if (ret) {
1203 /* Since we won't get link notification, just enable RX */
1204 pasemi_mac_intf_enable(mac);
1205 if (mac->type == MAC_TYPE_GMAC) {
1206 /* Warn for missing PHY on SGMII (1Gig) ports */
1207 dev_warn(&mac->pdev->dev,
1208 "PHY init failed: %d.\n", ret);
1209 dev_warn(&mac->pdev->dev,
1210 "Defaulting to 1Gbit full duplex\n");
1211 }
Olof Johansson8304b632007-11-28 20:58:06 -06001212 }
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001213
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001214 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001215 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001216
Olof Johansson72b05b92007-11-28 20:54:28 -06001217 snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx",
1218 dev->name);
Olof Johansson771f7402007-05-08 00:47:21 -05001219
Joe Perchesa0607fd2009-11-18 23:29:17 -08001220 ret = request_irq(mac->tx->chan.irq, pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johansson72b05b92007-11-28 20:54:28 -06001221 mac->tx_irq_name, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001222 if (ret) {
1223 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001224 mac->tx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001225 goto out_tx_int;
1226 }
1227
Olof Johansson72b05b92007-11-28 20:54:28 -06001228 snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx",
1229 dev->name);
1230
Joe Perchesa0607fd2009-11-18 23:29:17 -08001231 ret = request_irq(mac->rx->chan.irq, pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johansson34c20622007-11-28 20:56:32 -06001232 mac->rx_irq_name, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001233 if (ret) {
1234 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001235 mac->rx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001236 goto out_rx_int;
1237 }
1238
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001239 if (mac->phydev)
1240 phy_start(mac->phydev);
1241
Olof Johansson61cec3b2007-11-28 20:56:54 -06001242 init_timer(&mac->tx->clean_timer);
1243 mac->tx->clean_timer.function = pasemi_mac_tx_timer;
1244 mac->tx->clean_timer.data = (unsigned long)mac->tx;
1245 mac->tx->clean_timer.expires = jiffies+HZ;
1246 add_timer(&mac->tx->clean_timer);
1247
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001248 return 0;
1249
1250out_rx_int:
Olof Johansson34c20622007-11-28 20:56:32 -06001251 free_irq(mac->tx->chan.irq, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001252out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001253 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001254 netif_stop_queue(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001255out_tx_ring:
1256 if (mac->tx)
1257 pasemi_mac_free_tx_resources(mac);
1258 pasemi_mac_free_rx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001259out_rx_resources:
1260
1261 return ret;
1262}
1263
1264#define MAX_RETRIES 5000
1265
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001266static void pasemi_mac_pause_txchan(struct pasemi_mac *mac)
1267{
1268 unsigned int sta, retries;
1269 int txch = tx_ring(mac)->chan.chno;
1270
1271 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch),
1272 PAS_DMA_TXCHAN_TCMDSTA_ST);
1273
1274 for (retries = 0; retries < MAX_RETRIES; retries++) {
1275 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
1276 if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
1277 break;
1278 cond_resched();
1279 }
1280
1281 if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
1282 dev_err(&mac->dma_pdev->dev,
1283 "Failed to stop tx channel, tcmdsta %08x\n", sta);
1284
1285 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch), 0);
1286}
1287
1288static void pasemi_mac_pause_rxchan(struct pasemi_mac *mac)
1289{
1290 unsigned int sta, retries;
1291 int rxch = rx_ring(mac)->chan.chno;
1292
1293 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch),
1294 PAS_DMA_RXCHAN_CCMDSTA_ST);
1295 for (retries = 0; retries < MAX_RETRIES; retries++) {
1296 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
1297 if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
1298 break;
1299 cond_resched();
1300 }
1301
1302 if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
1303 dev_err(&mac->dma_pdev->dev,
1304 "Failed to stop rx channel, ccmdsta 08%x\n", sta);
1305 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch), 0);
1306}
1307
1308static void pasemi_mac_pause_rxint(struct pasemi_mac *mac)
1309{
1310 unsigned int sta, retries;
1311
1312 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1313 PAS_DMA_RXINT_RCMDSTA_ST);
1314 for (retries = 0; retries < MAX_RETRIES; retries++) {
1315 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1316 if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
1317 break;
1318 cond_resched();
1319 }
1320
1321 if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
1322 dev_err(&mac->dma_pdev->dev,
1323 "Failed to stop rx interface, rcmdsta %08x\n", sta);
1324 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
1325}
1326
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001327static int pasemi_mac_close(struct net_device *dev)
1328{
1329 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson9e81d332007-10-02 16:27:39 -05001330 unsigned int sta;
Olof Johansson8d636d82008-03-05 16:34:16 -06001331 int rxch, txch, i;
Olof Johansson34c20622007-11-28 20:56:32 -06001332
1333 rxch = rx_ring(mac)->chan.chno;
1334 txch = tx_ring(mac)->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001335
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001336 if (mac->phydev) {
1337 phy_stop(mac->phydev);
1338 phy_disconnect(mac->phydev);
1339 }
1340
Olof Johansson61cec3b2007-11-28 20:56:54 -06001341 del_timer_sync(&mac->tx->clean_timer);
1342
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001343 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001344 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001345
Olof Johansson34c20622007-11-28 20:56:32 -06001346 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson9e81d332007-10-02 16:27:39 -05001347 if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
1348 PAS_DMA_RXINT_RCMDSTA_OO |
1349 PAS_DMA_RXINT_RCMDSTA_BT))
1350 printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);
1351
Olof Johansson34c20622007-11-28 20:56:32 -06001352 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001353 if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
1354 PAS_DMA_RXCHAN_CCMDSTA_OD |
1355 PAS_DMA_RXCHAN_CCMDSTA_FD |
1356 PAS_DMA_RXCHAN_CCMDSTA_DT))
1357 printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);
1358
Olof Johansson34c20622007-11-28 20:56:32 -06001359 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
Olof Johansson72b05b92007-11-28 20:54:28 -06001360 if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB |
1361 PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA))
Olof Johansson9e81d332007-10-02 16:27:39 -05001362 printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);
1363
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001364 /* Clean out any pending buffers */
Olof Johansson72b05b92007-11-28 20:54:28 -06001365 pasemi_mac_clean_tx(tx_ring(mac));
1366 pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001367
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001368 pasemi_mac_pause_txchan(mac);
1369 pasemi_mac_pause_rxint(mac);
1370 pasemi_mac_pause_rxchan(mac);
Olof Johansson1145d952008-01-23 13:57:19 -06001371 pasemi_mac_intf_disable(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001372
Olof Johansson34c20622007-11-28 20:56:32 -06001373 free_irq(mac->tx->chan.irq, mac->tx);
1374 free_irq(mac->rx->chan.irq, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001375
Olof Johansson1724ac22008-03-25 09:58:40 -05001376 for (i = 0; i < mac->num_cs; i++) {
Olof Johansson8d636d82008-03-05 16:34:16 -06001377 pasemi_mac_free_csring(mac->cs[i]);
Olof Johansson1724ac22008-03-25 09:58:40 -05001378 mac->cs[i] = NULL;
1379 }
1380
1381 mac->num_cs = 0;
Olof Johansson8d636d82008-03-05 16:34:16 -06001382
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001383 /* Free resources */
Olof Johansson72b05b92007-11-28 20:54:28 -06001384 pasemi_mac_free_rx_resources(mac);
1385 pasemi_mac_free_tx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001386
1387 return 0;
1388}
1389
Olof Johansson8d636d82008-03-05 16:34:16 -06001390static void pasemi_mac_queue_csdesc(const struct sk_buff *skb,
1391 const dma_addr_t *map,
1392 const unsigned int *map_size,
1393 struct pasemi_mac_txring *txring,
1394 struct pasemi_mac_csring *csring)
1395{
1396 u64 fund;
1397 dma_addr_t cs_dest;
1398 const int nh_off = skb_network_offset(skb);
1399 const int nh_len = skb_network_header_len(skb);
1400 const int nfrags = skb_shinfo(skb)->nr_frags;
1401 int cs_size, i, fill, hdr, cpyhdr, evt;
1402 dma_addr_t csdma;
1403
1404 fund = XCT_FUN_ST | XCT_FUN_RR_8BRES |
1405 XCT_FUN_O | XCT_FUN_FUN(csring->fun) |
1406 XCT_FUN_CRM_SIG | XCT_FUN_LLEN(skb->len - nh_off) |
1407 XCT_FUN_SHL(nh_len >> 2) | XCT_FUN_SE;
1408
1409 switch (ip_hdr(skb)->protocol) {
1410 case IPPROTO_TCP:
1411 fund |= XCT_FUN_SIG_TCP4;
1412 /* TCP checksum is 16 bytes into the header */
1413 cs_dest = map[0] + skb_transport_offset(skb) + 16;
1414 break;
1415 case IPPROTO_UDP:
1416 fund |= XCT_FUN_SIG_UDP4;
1417 /* UDP checksum is 6 bytes into the header */
1418 cs_dest = map[0] + skb_transport_offset(skb) + 6;
1419 break;
1420 default:
1421 BUG();
1422 }
1423
1424 /* Do the checksum offloaded */
1425 fill = csring->next_to_fill;
1426 hdr = fill;
1427
1428 CS_DESC(csring, fill++) = fund;
1429 /* Room for 8BRES. Checksum result is really 2 bytes into it */
1430 csdma = csring->chan.ring_dma + (fill & (CS_RING_SIZE-1)) * 8 + 2;
1431 CS_DESC(csring, fill++) = 0;
1432
1433 CS_DESC(csring, fill) = XCT_PTR_LEN(map_size[0]-nh_off) | XCT_PTR_ADDR(map[0]+nh_off);
1434 for (i = 1; i <= nfrags; i++)
1435 CS_DESC(csring, fill+i) = XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
1436
1437 fill += i;
1438 if (fill & 1)
1439 fill++;
1440
1441 /* Copy the result into the TCP packet */
1442 cpyhdr = fill;
1443 CS_DESC(csring, fill++) = XCT_FUN_O | XCT_FUN_FUN(csring->fun) |
1444 XCT_FUN_LLEN(2) | XCT_FUN_SE;
1445 CS_DESC(csring, fill++) = XCT_PTR_LEN(2) | XCT_PTR_ADDR(cs_dest) | XCT_PTR_T;
1446 CS_DESC(csring, fill++) = XCT_PTR_LEN(2) | XCT_PTR_ADDR(csdma);
1447 fill++;
1448
1449 evt = !csring->last_event;
1450 csring->last_event = evt;
1451
1452 /* Event handshaking with MAC TX */
1453 CS_DESC(csring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
1454 CTRL_CMD_ETYPE_SET | CTRL_CMD_REG(csring->events[evt]);
1455 CS_DESC(csring, fill++) = 0;
1456 CS_DESC(csring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
1457 CTRL_CMD_ETYPE_WCLR | CTRL_CMD_REG(csring->events[!evt]);
1458 CS_DESC(csring, fill++) = 0;
1459 csring->next_to_fill = fill & (CS_RING_SIZE-1);
1460
1461 cs_size = fill - hdr;
1462 write_dma_reg(PAS_DMA_TXCHAN_INCR(csring->chan.chno), (cs_size) >> 1);
1463
1464 /* TX-side event handshaking */
1465 fill = txring->next_to_fill;
1466 TX_DESC(txring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
1467 CTRL_CMD_ETYPE_WSET | CTRL_CMD_REG(csring->events[evt]);
1468 TX_DESC(txring, fill++) = 0;
1469 TX_DESC(txring, fill++) = CTRL_CMD_T | CTRL_CMD_META_EVT | CTRL_CMD_O |
1470 CTRL_CMD_ETYPE_CLR | CTRL_CMD_REG(csring->events[!evt]);
1471 TX_DESC(txring, fill++) = 0;
1472 txring->next_to_fill = fill;
1473
1474 write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), 2);
Olof Johansson8d636d82008-03-05 16:34:16 -06001475}
1476
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001477static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1478{
Olof Johansson8d636d82008-03-05 16:34:16 -06001479 struct pasemi_mac * const mac = netdev_priv(dev);
1480 struct pasemi_mac_txring * const txring = tx_ring(mac);
1481 struct pasemi_mac_csring *csring;
1482 u64 dflags = 0;
1483 u64 mactx;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001484 dma_addr_t map[MAX_SKB_FRAGS+1];
1485 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001486 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001487 int i, nfrags;
Olof Johansson5c153322007-11-28 20:56:41 -06001488 int fill;
Olof Johansson8d636d82008-03-05 16:34:16 -06001489 const int nh_off = skb_network_offset(skb);
1490 const int nh_len = skb_network_header_len(skb);
1491
1492 prefetch(&txring->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001493
Olof Johanssondbd62af2007-11-06 22:20:39 -06001494 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001495
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001496 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001497
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001498 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1499 PCI_DMA_TODEVICE);
1500 map_size[0] = skb_headlen(skb);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001501 if (pci_dma_mapping_error(mac->dma_pdev, map[0]))
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001502 goto out_err_nolock;
1503
1504 for (i = 0; i < nfrags; i++) {
1505 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1506
1507 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1508 frag->page_offset, frag->size,
1509 PCI_DMA_TODEVICE);
1510 map_size[i+1] = frag->size;
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001511 if (pci_dma_mapping_error(mac->dma_pdev, map[i+1])) {
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001512 nfrags = i;
1513 goto out_err_nolock;
1514 }
1515 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001516
Olof Johansson8d636d82008-03-05 16:34:16 -06001517 if (skb->ip_summed == CHECKSUM_PARTIAL && skb->len <= 1540) {
1518 switch (ip_hdr(skb)->protocol) {
1519 case IPPROTO_TCP:
1520 dflags |= XCT_MACTX_CSUM_TCP;
1521 dflags |= XCT_MACTX_IPH(nh_len >> 2);
1522 dflags |= XCT_MACTX_IPO(nh_off);
1523 break;
1524 case IPPROTO_UDP:
1525 dflags |= XCT_MACTX_CSUM_UDP;
1526 dflags |= XCT_MACTX_IPH(nh_len >> 2);
1527 dflags |= XCT_MACTX_IPO(nh_off);
1528 break;
1529 default:
1530 WARN_ON(1);
1531 }
1532 }
1533
Olof Johansson26fcfa92007-08-22 09:12:59 -05001534 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001535
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001536 spin_lock_irqsave(&txring->lock, flags);
1537
Olof Johanssonad5da102007-10-02 16:27:15 -05001538 /* Avoid stepping on the same cache line that the DMA controller
1539 * is currently about to send, so leave at least 8 words available.
1540 * Total free space needed is mactx + fragments + 8
1541 */
Olof Johansson8d636d82008-03-05 16:34:16 -06001542 if (RING_AVAIL(txring) < nfrags + 14) {
Olof Johanssonad5da102007-10-02 16:27:15 -05001543 /* no room -- stop the queue and wait for tx intr */
1544 netif_stop_queue(dev);
1545 goto out_err;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001546 }
1547
Olof Johansson8d636d82008-03-05 16:34:16 -06001548 /* Queue up checksum + event descriptors, if needed */
1549 if (mac->num_cs && skb->ip_summed == CHECKSUM_PARTIAL && skb->len > 1540) {
1550 csring = mac->cs[mac->last_cs];
1551 mac->last_cs = (mac->last_cs + 1) % mac->num_cs;
1552
1553 pasemi_mac_queue_csdesc(skb, map, map_size, txring, csring);
1554 }
1555
1556 fill = txring->next_to_fill;
Olof Johansson5c153322007-11-28 20:56:41 -06001557 TX_DESC(txring, fill) = mactx;
Olof Johansson7e9916e2007-11-28 20:57:45 -06001558 TX_DESC_INFO(txring, fill).dma = nfrags;
Olof Johansson5c153322007-11-28 20:56:41 -06001559 fill++;
1560 TX_DESC_INFO(txring, fill).skb = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001561 for (i = 0; i <= nfrags; i++) {
Olof Johansson5c153322007-11-28 20:56:41 -06001562 TX_DESC(txring, fill+i) =
Olof Johansson72b05b92007-11-28 20:54:28 -06001563 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
Olof Johansson5c153322007-11-28 20:56:41 -06001564 TX_DESC_INFO(txring, fill+i).dma = map[i];
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001565 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001566
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001567 /* We have to add an even number of 8-byte entries to the ring
1568 * even if the last one is unused. That means always an odd number
1569 * of pointers + one mactx descriptor.
1570 */
1571 if (nfrags & 1)
1572 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001573
Olof Johansson5c153322007-11-28 20:56:41 -06001574 txring->next_to_fill = (fill + nfrags + 1) & (TX_RING_SIZE-1);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001575
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001576 dev->stats.tx_packets++;
1577 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001578
1579 spin_unlock_irqrestore(&txring->lock, flags);
1580
Olof Johansson34c20622007-11-28 20:56:32 -06001581 write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001582
1583 return NETDEV_TX_OK;
1584
1585out_err:
1586 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001587out_err_nolock:
1588 while (nfrags--)
1589 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1590 PCI_DMA_TODEVICE);
1591
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001592 return NETDEV_TX_BUSY;
1593}
1594
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001595static void pasemi_mac_set_rx_mode(struct net_device *dev)
1596{
Olof Johansson5c153322007-11-28 20:56:41 -06001597 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001598 unsigned int flags;
1599
Olof Johanssona85b9422007-09-15 13:40:59 -07001600 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001601
1602 /* Set promiscuous */
1603 if (dev->flags & IFF_PROMISC)
1604 flags |= PAS_MAC_CFG_PCFG_PR;
1605 else
1606 flags &= ~PAS_MAC_CFG_PCFG_PR;
1607
Olof Johanssona85b9422007-09-15 13:40:59 -07001608 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001609}
1610
1611
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001612static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001613{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001614 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001615 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001616
Olof Johansson72b05b92007-11-28 20:54:28 -06001617 pasemi_mac_clean_tx(tx_ring(mac));
1618 pkts = pasemi_mac_clean_rx(rx_ring(mac), budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001619 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001620 /* all done, no more packets present */
Ben Hutchings288379f2009-01-19 16:43:59 -08001621 napi_complete(napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001622
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001623 pasemi_mac_restart_rx_intr(mac);
Olof Johansson61cec3b2007-11-28 20:56:54 -06001624 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001625 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001626 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001627}
1628
Nate Case6e620402008-03-21 17:02:42 -05001629#ifdef CONFIG_NET_POLL_CONTROLLER
1630/*
1631 * Polling 'interrupt' - used by things like netconsole to send skbs
1632 * without having to re-enable interrupts. It's not called while
1633 * the interrupt routine is executing.
1634 */
1635static void pasemi_mac_netpoll(struct net_device *dev)
1636{
1637 const struct pasemi_mac *mac = netdev_priv(dev);
1638
1639 disable_irq(mac->tx->chan.irq);
1640 pasemi_mac_tx_intr(mac->tx->chan.irq, mac->tx);
1641 enable_irq(mac->tx->chan.irq);
1642
1643 disable_irq(mac->rx->chan.irq);
1644 pasemi_mac_rx_intr(mac->rx->chan.irq, mac->rx);
1645 enable_irq(mac->rx->chan.irq);
1646}
1647#endif
1648
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001649static int pasemi_mac_change_mtu(struct net_device *dev, int new_mtu)
1650{
1651 struct pasemi_mac *mac = netdev_priv(dev);
1652 unsigned int reg;
Olof Johansson8d636d82008-03-05 16:34:16 -06001653 unsigned int rcmdsta = 0;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001654 int running;
Olof Johansson8d636d82008-03-05 16:34:16 -06001655 int ret = 0;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001656
1657 if (new_mtu < PE_MIN_MTU || new_mtu > PE_MAX_MTU)
1658 return -EINVAL;
1659
1660 running = netif_running(dev);
1661
1662 if (running) {
1663 /* Need to stop the interface, clean out all already
1664 * received buffers, free all unused buffers on the RX
1665 * interface ring, then finally re-fill the rx ring with
1666 * the new-size buffers and restart.
1667 */
1668
1669 napi_disable(&mac->napi);
1670 netif_tx_disable(dev);
1671 pasemi_mac_intf_disable(mac);
1672
1673 rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1674 pasemi_mac_pause_rxint(mac);
1675 pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
1676 pasemi_mac_free_rx_buffers(mac);
Olof Johansson8d636d82008-03-05 16:34:16 -06001677
1678 }
1679
1680 /* Setup checksum channels if large MTU and none already allocated */
1681 if (new_mtu > 1500 && !mac->num_cs) {
1682 pasemi_mac_setup_csrings(mac);
1683 if (!mac->num_cs) {
1684 ret = -ENOMEM;
1685 goto out;
1686 }
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001687 }
1688
1689 /* Change maxf, i.e. what size frames are accepted.
1690 * Need room for ethernet header and CRC word
1691 */
1692 reg = read_mac_reg(mac, PAS_MAC_CFG_MACCFG);
1693 reg &= ~PAS_MAC_CFG_MACCFG_MAXF_M;
1694 reg |= PAS_MAC_CFG_MACCFG_MAXF(new_mtu + ETH_HLEN + 4);
1695 write_mac_reg(mac, PAS_MAC_CFG_MACCFG, reg);
1696
1697 dev->mtu = new_mtu;
1698 /* MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
1699 mac->bufsz = new_mtu + ETH_HLEN + ETH_FCS_LEN + LOCAL_SKB_ALIGN + 128;
1700
Olof Johansson8d636d82008-03-05 16:34:16 -06001701out:
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001702 if (running) {
1703 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1704 rcmdsta | PAS_DMA_RXINT_RCMDSTA_EN);
1705
1706 rx_ring(mac)->next_to_fill = 0;
1707 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE-1);
1708
1709 napi_enable(&mac->napi);
1710 netif_start_queue(dev);
1711 pasemi_mac_intf_enable(mac);
1712 }
1713
Olof Johansson8d636d82008-03-05 16:34:16 -06001714 return ret;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001715}
1716
Alexander Beregalov9e0ac842009-04-15 12:52:54 +00001717static const struct net_device_ops pasemi_netdev_ops = {
1718 .ndo_open = pasemi_mac_open,
1719 .ndo_stop = pasemi_mac_close,
1720 .ndo_start_xmit = pasemi_mac_start_tx,
1721 .ndo_set_multicast_list = pasemi_mac_set_rx_mode,
1722 .ndo_set_mac_address = pasemi_mac_set_mac_addr,
1723 .ndo_change_mtu = pasemi_mac_change_mtu,
1724 .ndo_validate_addr = eth_validate_addr,
1725#ifdef CONFIG_NET_POLL_CONTROLLER
1726 .ndo_poll_controller = pasemi_mac_netpoll,
1727#endif
1728};
1729
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001730static int __devinit
1731pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1732{
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001733 struct net_device *dev;
1734 struct pasemi_mac *mac;
roel kluin15b8e192009-04-23 08:53:20 +00001735 int err, ret;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001736
1737 err = pci_enable_device(pdev);
1738 if (err)
1739 return err;
1740
1741 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1742 if (dev == NULL) {
1743 dev_err(&pdev->dev,
1744 "pasemi_mac: Could not allocate ethernet device.\n");
1745 err = -ENOMEM;
1746 goto out_disable_device;
1747 }
1748
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001749 pci_set_drvdata(pdev, dev);
1750 SET_NETDEV_DEV(dev, &pdev->dev);
1751
1752 mac = netdev_priv(dev);
1753
1754 mac->pdev = pdev;
1755 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001756
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001757 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1758
Olof Johansson5c153322007-11-28 20:56:41 -06001759 dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG |
Olof Johansson25156782008-02-20 20:57:58 -06001760 NETIF_F_HIGHDMA | NETIF_F_GSO;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001761
Olof Johansson28ae79f2007-11-28 20:57:27 -06001762 mac->lro_mgr.max_aggr = LRO_MAX_AGGR;
1763 mac->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
1764 mac->lro_mgr.lro_arr = mac->lro_desc;
1765 mac->lro_mgr.get_skb_header = get_skb_hdr;
1766 mac->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1767 mac->lro_mgr.dev = mac->netdev;
1768 mac->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1769 mac->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1770
1771
Olof Johansson34c20622007-11-28 20:56:32 -06001772 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1773 if (!mac->dma_pdev) {
1774 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1775 err = -ENODEV;
1776 goto out;
1777 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001778
Olof Johansson34c20622007-11-28 20:56:32 -06001779 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1780 if (!mac->iob_pdev) {
1781 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1782 err = -ENODEV;
1783 goto out;
1784 }
1785
1786 /* get mac addr from device tree */
1787 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1788 err = -ENODEV;
1789 goto out;
1790 }
1791 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1792
roel kluin15b8e192009-04-23 08:53:20 +00001793 ret = mac_to_intf(mac);
1794 if (ret < 0) {
Olof Johansson34c20622007-11-28 20:56:32 -06001795 dev_err(&mac->pdev->dev, "Can't map DMA interface\n");
1796 err = -ENODEV;
1797 goto out;
1798 }
roel kluin15b8e192009-04-23 08:53:20 +00001799 mac->dma_if = ret;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001800
1801 switch (pdev->device) {
1802 case 0xa005:
1803 mac->type = MAC_TYPE_GMAC;
1804 break;
1805 case 0xa006:
1806 mac->type = MAC_TYPE_XAUI;
1807 break;
1808 default:
1809 err = -ENODEV;
1810 goto out;
1811 }
1812
Alexander Beregalov9e0ac842009-04-15 12:52:54 +00001813 dev->netdev_ops = &pasemi_netdev_ops;
Olof Johanssonef1ea0b2008-01-23 13:56:47 -06001814 dev->mtu = PE_DEF_MTU;
1815 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
1816 mac->bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + LOCAL_SKB_ALIGN + 128;
1817
Olof Johanssone37c7722008-02-20 20:57:59 -06001818 dev->ethtool_ops = &pasemi_mac_ethtool_ops;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001819
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001820 if (err)
1821 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001822
Olof Johanssonceb51362007-05-08 00:47:49 -05001823 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1824
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001825 /* Enable most messages by default */
1826 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1827
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001828 err = register_netdev(dev);
1829
1830 if (err) {
1831 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1832 err);
1833 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001834 } else if netif_msg_probe(mac)
Johannes Berge1749612008-10-27 15:59:26 -07001835 printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %pM\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001836 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
Johannes Berge1749612008-10-27 15:59:26 -07001837 mac->dma_if, dev->dev_addr);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001838
1839 return err;
1840
1841out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001842 if (mac->iob_pdev)
1843 pci_dev_put(mac->iob_pdev);
1844 if (mac->dma_pdev)
1845 pci_dev_put(mac->dma_pdev);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001846
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001847 free_netdev(dev);
1848out_disable_device:
1849 pci_disable_device(pdev);
1850 return err;
1851
1852}
1853
1854static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1855{
1856 struct net_device *netdev = pci_get_drvdata(pdev);
1857 struct pasemi_mac *mac;
1858
1859 if (!netdev)
1860 return;
1861
1862 mac = netdev_priv(netdev);
1863
1864 unregister_netdev(netdev);
1865
1866 pci_disable_device(pdev);
1867 pci_dev_put(mac->dma_pdev);
1868 pci_dev_put(mac->iob_pdev);
1869
Olof Johansson34c20622007-11-28 20:56:32 -06001870 pasemi_dma_free_chan(&mac->tx->chan);
1871 pasemi_dma_free_chan(&mac->rx->chan);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001872
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001873 pci_set_drvdata(pdev, NULL);
1874 free_netdev(netdev);
1875}
1876
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00001877static DEFINE_PCI_DEVICE_TABLE(pasemi_mac_pci_tbl) = {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001878 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1879 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001880 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001881};
1882
1883MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1884
1885static struct pci_driver pasemi_mac_driver = {
1886 .name = "pasemi_mac",
1887 .id_table = pasemi_mac_pci_tbl,
1888 .probe = pasemi_mac_probe,
1889 .remove = __devexit_p(pasemi_mac_remove),
1890};
1891
1892static void __exit pasemi_mac_cleanup_module(void)
1893{
1894 pci_unregister_driver(&pasemi_mac_driver);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001895}
1896
1897int pasemi_mac_init_module(void)
1898{
Olof Johansson34c20622007-11-28 20:56:32 -06001899 int err;
1900
1901 err = pasemi_dma_init();
1902 if (err)
1903 return err;
1904
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001905 return pci_register_driver(&pasemi_mac_driver);
1906}
1907
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001908module_init(pasemi_mac_init_module);
1909module_exit(pasemi_mac_cleanup_module);