blob: 59dea3f829fc50fc9036ada7bcbcafb58ca4f48b [file] [log] [blame]
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001/*
2 * Copyright (C) 2006-2007 PA Semi, Inc
3 *
4 * Driver for the PA Semi PWRficient onchip 1G/10G Ethernet MACs
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/pci.h>
23#include <linux/interrupt.h>
24#include <linux/dmaengine.h>
25#include <linux/delay.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <asm/dma-mapping.h>
29#include <linux/in.h>
30#include <linux/skbuff.h>
31
32#include <linux/ip.h>
33#include <linux/tcp.h>
34#include <net/checksum.h>
Olof Johansson28ae79f2007-11-28 20:57:27 -060035#include <linux/inet_lro.h>
Olof Johanssonf5cd7872007-01-31 21:43:54 -060036
Olof Johansson771f7402007-05-08 00:47:21 -050037#include <asm/irq.h>
Olof Johanssonaf289e82007-10-03 13:03:54 -050038#include <asm/firmware.h>
Olof Johansson40afa532007-11-28 20:56:04 -060039#include <asm/pasemi_dma.h>
Olof Johansson771f7402007-05-08 00:47:21 -050040
Olof Johanssonf5cd7872007-01-31 21:43:54 -060041#include "pasemi_mac.h"
42
Olof Johansson8dc121a2007-10-02 16:26:53 -050043/* We have our own align, since ppc64 in general has it at 0 because
44 * of design flaws in some of the server bridge chips. However, for
45 * PWRficient doing the unaligned copies is more expensive than doing
46 * unaligned DMA, so make sure the data is aligned instead.
47 */
48#define LOCAL_SKB_ALIGN 2
Olof Johanssonf5cd7872007-01-31 21:43:54 -060049
50/* TODO list
51 *
Olof Johanssonf5cd7872007-01-31 21:43:54 -060052 * - Multicast support
53 * - Large MTU support
Olof Johansson7ddeae22007-10-02 16:27:28 -050054 * - SW LRO
55 * - Multiqueue RX/TX
Olof Johanssonf5cd7872007-01-31 21:43:54 -060056 */
57
58
59/* Must be a power of two */
Olof Johansson28ae79f2007-11-28 20:57:27 -060060#define RX_RING_SIZE 2048
Olof Johanssonad5da102007-10-02 16:27:15 -050061#define TX_RING_SIZE 4096
Olof Johanssonf5cd7872007-01-31 21:43:54 -060062
Olof Johansson28ae79f2007-11-28 20:57:27 -060063#define LRO_MAX_AGGR 64
64
Olof Johanssonceb51362007-05-08 00:47:49 -050065#define DEFAULT_MSG_ENABLE \
66 (NETIF_MSG_DRV | \
67 NETIF_MSG_PROBE | \
68 NETIF_MSG_LINK | \
69 NETIF_MSG_TIMER | \
70 NETIF_MSG_IFDOWN | \
71 NETIF_MSG_IFUP | \
72 NETIF_MSG_RX_ERR | \
73 NETIF_MSG_TX_ERR)
74
Olof Johansson34c20622007-11-28 20:56:32 -060075#define TX_DESC(tx, num) ((tx)->chan.ring_virt[(num) & (TX_RING_SIZE-1)])
Olof Johansson72b05b92007-11-28 20:54:28 -060076#define TX_DESC_INFO(tx, num) ((tx)->ring_info[(num) & (TX_RING_SIZE-1)])
Olof Johansson34c20622007-11-28 20:56:32 -060077#define RX_DESC(rx, num) ((rx)->chan.ring_virt[(num) & (RX_RING_SIZE-1)])
Olof Johansson72b05b92007-11-28 20:54:28 -060078#define RX_DESC_INFO(rx, num) ((rx)->ring_info[(num) & (RX_RING_SIZE-1)])
79#define RX_BUFF(rx, num) ((rx)->buffers[(num) & (RX_RING_SIZE-1)])
Olof Johanssonf5cd7872007-01-31 21:43:54 -060080
Olof Johansson021fa222007-08-22 09:13:11 -050081#define RING_USED(ring) (((ring)->next_to_fill - (ring)->next_to_clean) \
82 & ((ring)->size - 1))
83#define RING_AVAIL(ring) ((ring->size) - RING_USED(ring))
84
Olof Johanssonf5cd7872007-01-31 21:43:54 -060085#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
86
Olof Johanssonceb51362007-05-08 00:47:49 -050087MODULE_LICENSE("GPL");
88MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
89MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
90
91static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
92module_param(debug, int, 0);
93MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060094
Olof Johanssonaf289e82007-10-03 13:03:54 -050095static int translation_enabled(void)
96{
97#if defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
98 return 1;
99#else
100 return firmware_has_feature(FW_FEATURE_LPAR);
101#endif
102}
103
Olof Johansson34c20622007-11-28 20:56:32 -0600104static void write_iob_reg(unsigned int reg, unsigned int val)
Olof Johanssona85b9422007-09-15 13:40:59 -0700105{
Olof Johansson34c20622007-11-28 20:56:32 -0600106 pasemi_write_iob_reg(reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700107}
108
Olof Johansson5c153322007-11-28 20:56:41 -0600109static unsigned int read_mac_reg(const struct pasemi_mac *mac, unsigned int reg)
Olof Johanssona85b9422007-09-15 13:40:59 -0700110{
Olof Johansson34c20622007-11-28 20:56:32 -0600111 return pasemi_read_mac_reg(mac->dma_if, reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700112}
113
Olof Johansson5c153322007-11-28 20:56:41 -0600114static void write_mac_reg(const struct pasemi_mac *mac, unsigned int reg,
Olof Johanssona85b9422007-09-15 13:40:59 -0700115 unsigned int val)
116{
Olof Johansson34c20622007-11-28 20:56:32 -0600117 pasemi_write_mac_reg(mac->dma_if, reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700118}
119
Olof Johansson34c20622007-11-28 20:56:32 -0600120static unsigned int read_dma_reg(unsigned int reg)
Olof Johanssona85b9422007-09-15 13:40:59 -0700121{
Olof Johansson34c20622007-11-28 20:56:32 -0600122 return pasemi_read_dma_reg(reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700123}
124
Olof Johansson34c20622007-11-28 20:56:32 -0600125static void write_dma_reg(unsigned int reg, unsigned int val)
Olof Johanssona85b9422007-09-15 13:40:59 -0700126{
Olof Johansson34c20622007-11-28 20:56:32 -0600127 pasemi_write_dma_reg(reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700128}
129
Olof Johansson5c153322007-11-28 20:56:41 -0600130static struct pasemi_mac_rxring *rx_ring(const struct pasemi_mac *mac)
Olof Johansson72b05b92007-11-28 20:54:28 -0600131{
132 return mac->rx;
133}
134
Olof Johansson5c153322007-11-28 20:56:41 -0600135static struct pasemi_mac_txring *tx_ring(const struct pasemi_mac *mac)
Olof Johansson72b05b92007-11-28 20:54:28 -0600136{
137 return mac->tx;
138}
139
Olof Johansson5c153322007-11-28 20:56:41 -0600140static inline void prefetch_skb(const struct sk_buff *skb)
141{
142 const void *d = skb;
143
144 prefetch(d);
145 prefetch(d+64);
146 prefetch(d+128);
147 prefetch(d+192);
148}
149
Olof Johansson34c20622007-11-28 20:56:32 -0600150static int mac_to_intf(struct pasemi_mac *mac)
151{
152 struct pci_dev *pdev = mac->pdev;
153 u32 tmp;
154 int nintf, off, i, j;
155 int devfn = pdev->devfn;
156
157 tmp = read_dma_reg(PAS_DMA_CAP_IFI);
158 nintf = (tmp & PAS_DMA_CAP_IFI_NIN_M) >> PAS_DMA_CAP_IFI_NIN_S;
159 off = (tmp & PAS_DMA_CAP_IFI_IOFF_M) >> PAS_DMA_CAP_IFI_IOFF_S;
160
161 /* IOFF contains the offset to the registers containing the
162 * DMA interface-to-MAC-pci-id mappings, and NIN contains number
163 * of total interfaces. Each register contains 4 devfns.
164 * Just do a linear search until we find the devfn of the MAC
165 * we're trying to look up.
166 */
167
168 for (i = 0; i < (nintf+3)/4; i++) {
169 tmp = read_dma_reg(off+4*i);
170 for (j = 0; j < 4; j++) {
171 if (((tmp >> (8*j)) & 0xff) == devfn)
172 return i*4 + j;
173 }
174 }
175 return -1;
176}
177
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600178static int pasemi_get_mac_addr(struct pasemi_mac *mac)
179{
180 struct pci_dev *pdev = mac->pdev;
181 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500182 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600183 const u8 *maddr;
184 u8 addr[6];
185
186 if (!dn) {
187 dev_dbg(&pdev->dev,
188 "No device node for mac, not configuring\n");
189 return -ENOENT;
190 }
191
olof@lixom.net1af7f052007-05-12 14:57:46 -0500192 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500193
olof@lixom.net1af7f052007-05-12 14:57:46 -0500194 if (maddr && len == 6) {
195 memcpy(mac->mac_addr, maddr, 6);
196 return 0;
197 }
198
199 /* Some old versions of firmware mistakenly uses mac-address
200 * (and as a string) instead of a byte array in local-mac-address.
201 */
202
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500203 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700204 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500205
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600206 if (maddr == NULL) {
207 dev_warn(&pdev->dev,
208 "no mac address in device tree, not configuring\n");
209 return -ENOENT;
210 }
211
212 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
213 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
214 dev_warn(&pdev->dev,
215 "can't parse mac address, not configuring\n");
216 return -EINVAL;
217 }
218
olof@lixom.net1af7f052007-05-12 14:57:46 -0500219 memcpy(mac->mac_addr, addr, 6);
220
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600221 return 0;
222}
223
Olof Johansson5cea73b2008-01-23 13:56:19 -0600224static int pasemi_mac_set_mac_addr(struct net_device *dev, void *p)
225{
226 struct pasemi_mac *mac = netdev_priv(dev);
227 struct sockaddr *addr = p;
228 unsigned int adr0, adr1;
229
230 if (!is_valid_ether_addr(addr->sa_data))
231 return -EINVAL;
232
233 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
234
235 adr0 = dev->dev_addr[2] << 24 |
236 dev->dev_addr[3] << 16 |
237 dev->dev_addr[4] << 8 |
238 dev->dev_addr[5];
239 adr1 = read_mac_reg(mac, PAS_MAC_CFG_ADR1);
240 adr1 &= ~0xffff;
241 adr1 |= dev->dev_addr[0] << 8 | dev->dev_addr[1];
242
243 pasemi_mac_intf_disable(mac);
244 write_mac_reg(mac, PAS_MAC_CFG_ADR0, adr0);
245 write_mac_reg(mac, PAS_MAC_CFG_ADR1, adr1);
246 pasemi_mac_intf_enable(mac);
247
248 return 0;
249}
250
Olof Johansson28ae79f2007-11-28 20:57:27 -0600251static int get_skb_hdr(struct sk_buff *skb, void **iphdr,
252 void **tcph, u64 *hdr_flags, void *data)
253{
254 u64 macrx = (u64) data;
255 unsigned int ip_len;
256 struct iphdr *iph;
257
258 /* IPv4 header checksum failed */
259 if ((macrx & XCT_MACRX_HTY_M) != XCT_MACRX_HTY_IPV4_OK)
260 return -1;
261
262 /* non tcp packet */
263 skb_reset_network_header(skb);
264 iph = ip_hdr(skb);
265 if (iph->protocol != IPPROTO_TCP)
266 return -1;
267
268 ip_len = ip_hdrlen(skb);
269 skb_set_transport_header(skb, ip_len);
270 *tcph = tcp_hdr(skb);
271
272 /* check if ip header and tcp header are complete */
273 if (iph->tot_len < ip_len + tcp_hdrlen(skb))
274 return -1;
275
276 *hdr_flags = LRO_IPV4 | LRO_TCP;
277 *iphdr = iph;
278
279 return 0;
280}
281
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500282static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
Olof Johansson7e9916e2007-11-28 20:57:45 -0600283 const int nfrags,
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500284 struct sk_buff *skb,
Olof Johansson5c153322007-11-28 20:56:41 -0600285 const dma_addr_t *dmas)
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500286{
287 int f;
Olof Johansson5c153322007-11-28 20:56:41 -0600288 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500289
Olof Johansson5c153322007-11-28 20:56:41 -0600290 pci_unmap_single(pdev, dmas[0], skb_headlen(skb), PCI_DMA_TODEVICE);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500291
292 for (f = 0; f < nfrags; f++) {
293 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
294
Olof Johansson5c153322007-11-28 20:56:41 -0600295 pci_unmap_page(pdev, dmas[f+1], frag->size, PCI_DMA_TODEVICE);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500296 }
297 dev_kfree_skb_irq(skb);
298
299 /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
300 * aligned up to a power of 2
301 */
302 return (nfrags + 3) & ~1;
303}
304
Olof Johansson5c153322007-11-28 20:56:41 -0600305static int pasemi_mac_setup_rx_resources(const struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600306{
307 struct pasemi_mac_rxring *ring;
308 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson34c20622007-11-28 20:56:32 -0600309 int chno;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500310 unsigned int cfg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600311
Olof Johansson34c20622007-11-28 20:56:32 -0600312 ring = pasemi_dma_alloc_chan(RXCHAN, sizeof(struct pasemi_mac_rxring),
313 offsetof(struct pasemi_mac_rxring, chan));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600314
Olof Johansson34c20622007-11-28 20:56:32 -0600315 if (!ring) {
316 dev_err(&mac->pdev->dev, "Can't allocate RX channel\n");
317 goto out_chan;
318 }
319 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600320
321 spin_lock_init(&ring->lock);
322
Olof Johansson021fa222007-08-22 09:13:11 -0500323 ring->size = RX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500324 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600325 RX_RING_SIZE, GFP_KERNEL);
326
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500327 if (!ring->ring_info)
328 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600329
330 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600331 if (pasemi_dma_alloc_ring(&ring->chan, RX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500332 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600333
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600334 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
335 RX_RING_SIZE * sizeof(u64),
336 &ring->buf_dma, GFP_KERNEL);
337 if (!ring->buffers)
Olof Johansson34c20622007-11-28 20:56:32 -0600338 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600339
340 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
341
Olof Johansson34c20622007-11-28 20:56:32 -0600342 write_dma_reg(PAS_DMA_RXCHAN_BASEL(chno),
343 PAS_DMA_RXCHAN_BASEL_BRBL(ring->chan.ring_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600344
Olof Johansson34c20622007-11-28 20:56:32 -0600345 write_dma_reg(PAS_DMA_RXCHAN_BASEU(chno),
346 PAS_DMA_RXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32) |
347 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600348
Olof Johansson5c153322007-11-28 20:56:41 -0600349 cfg = PAS_DMA_RXCHAN_CFG_HBU(2);
Olof Johanssonaf289e82007-10-03 13:03:54 -0500350
351 if (translation_enabled())
352 cfg |= PAS_DMA_RXCHAN_CFG_CTR;
353
Olof Johansson34c20622007-11-28 20:56:32 -0600354 write_dma_reg(PAS_DMA_RXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600355
Olof Johansson34c20622007-11-28 20:56:32 -0600356 write_dma_reg(PAS_DMA_RXINT_BASEL(mac->dma_if),
357 PAS_DMA_RXINT_BASEL_BRBL(ring->buf_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600358
Olof Johansson34c20622007-11-28 20:56:32 -0600359 write_dma_reg(PAS_DMA_RXINT_BASEU(mac->dma_if),
360 PAS_DMA_RXINT_BASEU_BRBH(ring->buf_dma >> 32) |
361 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600362
Olof Johansson5c153322007-11-28 20:56:41 -0600363 cfg = PAS_DMA_RXINT_CFG_DHL(2) | PAS_DMA_RXINT_CFG_L2 |
Olof Johanssonaf289e82007-10-03 13:03:54 -0500364 PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP |
365 PAS_DMA_RXINT_CFG_HEN;
366
367 if (translation_enabled())
368 cfg |= PAS_DMA_RXINT_CFG_ITRR | PAS_DMA_RXINT_CFG_ITR;
369
Olof Johansson34c20622007-11-28 20:56:32 -0600370 write_dma_reg(PAS_DMA_RXINT_CFG(mac->dma_if), cfg);
Olof Johanssonc0efd522007-08-22 09:12:52 -0500371
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600372 ring->next_to_fill = 0;
373 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600374 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600375 mac->rx = ring;
376
377 return 0;
378
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500379out_ring_desc:
380 kfree(ring->ring_info);
381out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600382 pasemi_dma_free_chan(&ring->chan);
383out_chan:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600384 return -ENOMEM;
385}
386
Olof Johansson72b05b92007-11-28 20:54:28 -0600387static struct pasemi_mac_txring *
Olof Johansson5c153322007-11-28 20:56:41 -0600388pasemi_mac_setup_tx_resources(const struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600389{
390 struct pasemi_mac *mac = netdev_priv(dev);
391 u32 val;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600392 struct pasemi_mac_txring *ring;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500393 unsigned int cfg;
Olof Johansson34c20622007-11-28 20:56:32 -0600394 int chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600395
Olof Johansson34c20622007-11-28 20:56:32 -0600396 ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_txring),
397 offsetof(struct pasemi_mac_txring, chan));
398
399 if (!ring) {
400 dev_err(&mac->pdev->dev, "Can't allocate TX channel\n");
401 goto out_chan;
402 }
403
404 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600405
406 spin_lock_init(&ring->lock);
407
Olof Johansson021fa222007-08-22 09:13:11 -0500408 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500409 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600410 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500411 if (!ring->ring_info)
412 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600413
414 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600415 if (pasemi_dma_alloc_ring(&ring->chan, TX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500416 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600417
Olof Johansson34c20622007-11-28 20:56:32 -0600418 write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno),
419 PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma));
420 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500421 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600422
Olof Johansson34c20622007-11-28 20:56:32 -0600423 write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600424
Olof Johanssonaf289e82007-10-03 13:03:54 -0500425 cfg = PAS_DMA_TXCHAN_CFG_TY_IFACE |
426 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
427 PAS_DMA_TXCHAN_CFG_UP |
428 PAS_DMA_TXCHAN_CFG_WT(2);
429
430 if (translation_enabled())
431 cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
432
Olof Johansson34c20622007-11-28 20:56:32 -0600433 write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600434
Olof Johansson021fa222007-08-22 09:13:11 -0500435 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600436 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600437 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600438
Olof Johansson72b05b92007-11-28 20:54:28 -0600439 return ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600440
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500441out_ring_desc:
442 kfree(ring->ring_info);
443out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600444 pasemi_dma_free_chan(&ring->chan);
445out_chan:
Olof Johansson72b05b92007-11-28 20:54:28 -0600446 return NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600447}
448
Olof Johansson72b05b92007-11-28 20:54:28 -0600449static void pasemi_mac_free_tx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600450{
Olof Johansson72b05b92007-11-28 20:54:28 -0600451 struct pasemi_mac_txring *txring = tx_ring(mac);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500452 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600453 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500454 dma_addr_t dmas[MAX_SKB_FRAGS+1];
Olof Johansson7e9916e2007-11-28 20:57:45 -0600455 int freed, nfrags;
Olof Johanssonad5da102007-10-02 16:27:15 -0500456 int start, limit;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600457
Olof Johansson72b05b92007-11-28 20:54:28 -0600458 start = txring->next_to_clean;
459 limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500460
461 /* Compensate for when fill has wrapped and clean has not */
462 if (start > limit)
463 limit += TX_RING_SIZE;
464
465 for (i = start; i < limit; i += freed) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600466 info = &txring->ring_info[(i+1) & (TX_RING_SIZE-1)];
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500467 if (info->dma && info->skb) {
Olof Johansson7e9916e2007-11-28 20:57:45 -0600468 nfrags = skb_shinfo(info->skb)->nr_frags;
469 for (j = 0; j <= nfrags; j++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600470 dmas[j] = txring->ring_info[(i+1+j) &
471 (TX_RING_SIZE-1)].dma;
Olof Johansson7e9916e2007-11-28 20:57:45 -0600472 freed = pasemi_mac_unmap_tx_skb(mac, nfrags,
473 info->skb, dmas);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500474 } else
475 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600476 }
477
Olof Johansson72b05b92007-11-28 20:54:28 -0600478 kfree(txring->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600479 pasemi_dma_free_chan(&txring->chan);
480
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600481}
482
Olof Johansson72b05b92007-11-28 20:54:28 -0600483static void pasemi_mac_free_rx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600484{
Olof Johansson72b05b92007-11-28 20:54:28 -0600485 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600486 unsigned int i;
487 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600488
489 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600490 info = &RX_DESC_INFO(rx, i);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500491 if (info->skb && info->dma) {
492 pci_unmap_single(mac->dma_pdev,
493 info->dma,
494 info->skb->len,
495 PCI_DMA_FROMDEVICE);
496 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600497 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500498 info->dma = 0;
499 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600500 }
501
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500502 for (i = 0; i < RX_RING_SIZE; i++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600503 RX_DESC(rx, i) = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500504
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600505 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
Olof Johansson72b05b92007-11-28 20:54:28 -0600506 rx_ring(mac)->buffers, rx_ring(mac)->buf_dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600507
Olof Johansson72b05b92007-11-28 20:54:28 -0600508 kfree(rx_ring(mac)->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600509 pasemi_dma_free_chan(&rx_ring(mac)->chan);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600510 mac->rx = NULL;
511}
512
Olof Johansson5c153322007-11-28 20:56:41 -0600513static void pasemi_mac_replenish_rx_ring(const struct net_device *dev,
514 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600515{
Olof Johansson5c153322007-11-28 20:56:41 -0600516 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -0600517 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500518 int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600519
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500520 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600521 return;
522
Olof Johansson72b05b92007-11-28 20:54:28 -0600523 fill = rx_ring(mac)->next_to_fill;
Olof Johansson928773c2007-09-26 16:25:06 -0500524 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600525 struct pasemi_mac_buffer *info = &RX_DESC_INFO(rx, fill);
526 u64 *buff = &RX_BUFF(rx, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600527 struct sk_buff *skb;
528 dma_addr_t dma;
529
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500530 /* Entry in use? */
531 WARN_ON(*buff);
532
Olof Johansson5d894942007-11-28 20:57:56 -0600533 skb = dev_alloc_skb(BUF_SIZE);
534 skb_reserve(skb, LOCAL_SKB_ALIGN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600535
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500536 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600537 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600538
Olof Johansson8dc121a2007-10-02 16:26:53 -0500539 dma = pci_map_single(mac->dma_pdev, skb->data,
540 BUF_SIZE - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600541 PCI_DMA_FROMDEVICE);
542
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500543 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600544 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600545 break;
546 }
547
548 info->skb = skb;
549 info->dma = dma;
550 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500551 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600552 }
553
554 wmb();
555
Olof Johansson34c20622007-11-28 20:56:32 -0600556 write_dma_reg(PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600557
Olof Johansson72b05b92007-11-28 20:54:28 -0600558 rx_ring(mac)->next_to_fill = (rx_ring(mac)->next_to_fill + count) &
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500559 (RX_RING_SIZE - 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600560}
561
Olof Johansson5c153322007-11-28 20:56:41 -0600562static void pasemi_mac_restart_rx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500563{
Olof Johansson906674a2007-11-28 20:57:09 -0600564 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johansson52a94352007-05-12 18:01:09 -0500565 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500566 /* Re-enable packet count interrupts: finally
567 * ack the packet count interrupt we got in rx_intr.
568 */
569
Olof Johansson906674a2007-11-28 20:57:09 -0600570 pcnt = *rx->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500571
Olof Johansson52a94352007-05-12 18:01:09 -0500572 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500573
Olof Johansson906674a2007-11-28 20:57:09 -0600574 if (*rx->chan.status & PAS_STATUS_TIMER)
575 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
576
Olof Johansson34c20622007-11-28 20:56:32 -0600577 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(mac->rx->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500578}
579
Olof Johansson5c153322007-11-28 20:56:41 -0600580static void pasemi_mac_restart_tx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500581{
Olof Johansson52a94352007-05-12 18:01:09 -0500582 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500583
584 /* Re-enable packet count interrupts */
Olof Johansson34c20622007-11-28 20:56:32 -0600585 pcnt = *tx_ring(mac)->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500586
Olof Johansson52a94352007-05-12 18:01:09 -0500587 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500588
Olof Johansson34c20622007-11-28 20:56:32 -0600589 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(tx_ring(mac)->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500590}
591
592
Olof Johansson5c153322007-11-28 20:56:41 -0600593static inline void pasemi_mac_rx_error(const struct pasemi_mac *mac,
594 const u64 macrx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500595{
596 unsigned int rcmdsta, ccmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600597 struct pasemi_dmachan *chan = &rx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500598
599 if (!netif_msg_rx_err(mac))
600 return;
601
Olof Johansson34c20622007-11-28 20:56:32 -0600602 rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
603 ccmdsta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500604
605 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
Olof Johansson34c20622007-11-28 20:56:32 -0600606 macrx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500607
608 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
609 rcmdsta, ccmdsta);
610}
611
Olof Johansson5c153322007-11-28 20:56:41 -0600612static inline void pasemi_mac_tx_error(const struct pasemi_mac *mac,
613 const u64 mactx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500614{
615 unsigned int cmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600616 struct pasemi_dmachan *chan = &tx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500617
618 if (!netif_msg_tx_err(mac))
619 return;
620
Olof Johansson34c20622007-11-28 20:56:32 -0600621 cmdsta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500622
623 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
Olof Johansson34c20622007-11-28 20:56:32 -0600624 "tx status 0x%016lx\n", mactx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500625
626 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
627}
628
Olof Johansson5c153322007-11-28 20:56:41 -0600629static int pasemi_mac_clean_rx(struct pasemi_mac_rxring *rx,
630 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600631{
Olof Johansson5c153322007-11-28 20:56:41 -0600632 const struct pasemi_dmachan *chan = &rx->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600633 struct pasemi_mac *mac = rx->mac;
Olof Johansson5c153322007-11-28 20:56:41 -0600634 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500635 unsigned int n;
Olof Johansson5c153322007-11-28 20:56:41 -0600636 int count, buf_index, tot_bytes, packets;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500637 struct pasemi_mac_buffer *info;
638 struct sk_buff *skb;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500639 unsigned int len;
Olof Johansson5c153322007-11-28 20:56:41 -0600640 u64 macrx, eval;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500641 dma_addr_t dma;
Olof Johansson5c153322007-11-28 20:56:41 -0600642
643 tot_bytes = 0;
644 packets = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600645
Olof Johansson72b05b92007-11-28 20:54:28 -0600646 spin_lock(&rx->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600647
Olof Johansson72b05b92007-11-28 20:54:28 -0600648 n = rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600649
Olof Johansson72b05b92007-11-28 20:54:28 -0600650 prefetch(&RX_DESC(rx, n));
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500651
652 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600653 macrx = RX_DESC(rx, n);
Olof Johansson5c153322007-11-28 20:56:41 -0600654 prefetch(&RX_DESC(rx, n+4));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600655
Olof Johansson69c29d82007-10-02 16:24:51 -0500656 if ((macrx & XCT_MACRX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600657 (*chan->status & PAS_STATUS_ERROR))
Olof Johansson69c29d82007-10-02 16:24:51 -0500658 pasemi_mac_rx_error(mac, macrx);
659
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500660 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600661 break;
662
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600663 info = NULL;
664
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500665 BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600666
Olof Johansson72b05b92007-11-28 20:54:28 -0600667 eval = (RX_DESC(rx, n+1) & XCT_RXRES_8B_EVAL_M) >>
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500668 XCT_RXRES_8B_EVAL_S;
669 buf_index = eval-1;
670
Olof Johansson72b05b92007-11-28 20:54:28 -0600671 dma = (RX_DESC(rx, n+2) & XCT_PTR_ADDR_M);
672 info = &RX_DESC_INFO(rx, buf_index);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500673
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500674 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600675
Olof Johansson5c153322007-11-28 20:56:41 -0600676 prefetch_skb(skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600677
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500678 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600679
Olof Johansson5c153322007-11-28 20:56:41 -0600680 pci_unmap_single(pdev, dma, BUF_SIZE-LOCAL_SKB_ALIGN,
681 PCI_DMA_FROMDEVICE);
Olof Johansson32bee772007-11-06 22:21:38 -0600682
683 if (macrx & XCT_MACRX_CRC) {
684 /* CRC error flagged */
685 mac->netdev->stats.rx_errors++;
686 mac->netdev->stats.rx_crc_errors++;
Olof Johansson4352d822007-12-03 21:34:14 -0600687 /* No need to free skb, it'll be reused */
Olof Johansson32bee772007-11-06 22:21:38 -0600688 goto next;
689 }
690
Olof Johansson5d894942007-11-28 20:57:56 -0600691 info->skb = NULL;
Olof Johanssonad5da102007-10-02 16:27:15 -0500692 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500693
Olof Johansson26fcfa92007-08-22 09:12:59 -0500694 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500695 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500696 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600697 XCT_MACRX_CSUM_S;
698 } else
699 skb->ip_summed = CHECKSUM_NONE;
700
Olof Johansson5c153322007-11-28 20:56:41 -0600701 packets++;
702 tot_bytes += len;
703
704 /* Don't include CRC */
705 skb_put(skb, len-4);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600706
Olof Johansson26fcfa92007-08-22 09:12:59 -0500707 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johansson28ae79f2007-11-28 20:57:27 -0600708 lro_receive_skb(&mac->lro_mgr, skb, (void *)macrx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600709
Olof Johansson32bee772007-11-06 22:21:38 -0600710next:
Olof Johansson72b05b92007-11-28 20:54:28 -0600711 RX_DESC(rx, n) = 0;
712 RX_DESC(rx, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500713
Olof Johanssonad5da102007-10-02 16:27:15 -0500714 /* Need to zero it out since hardware doesn't, since the
715 * replenish loop uses it to tell when it's done.
716 */
Olof Johansson72b05b92007-11-28 20:54:28 -0600717 RX_BUFF(rx, buf_index) = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500718
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500719 n += 4;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600720 }
721
Olof Johansson9a50beb2007-10-02 16:26:30 -0500722 if (n > RX_RING_SIZE) {
723 /* Errata 5971 workaround: L2 target of headers */
Olof Johansson34c20622007-11-28 20:56:32 -0600724 write_iob_reg(PAS_IOB_COM_PKTHDRCNT, 0);
Olof Johansson9a50beb2007-10-02 16:26:30 -0500725 n &= (RX_RING_SIZE-1);
726 }
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500727
Olof Johansson72b05b92007-11-28 20:54:28 -0600728 rx_ring(mac)->next_to_clean = n;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500729
Olof Johansson28ae79f2007-11-28 20:57:27 -0600730 lro_flush_all(&mac->lro_mgr);
731
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500732 /* Increase is in number of 16-byte entries, and since each descriptor
733 * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
734 * count*2.
735 */
Olof Johansson34c20622007-11-28 20:56:32 -0600736 write_dma_reg(PAS_DMA_RXCHAN_INCR(mac->rx->chan.chno), count << 1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500737
738 pasemi_mac_replenish_rx_ring(mac->netdev, count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600739
Olof Johansson5c153322007-11-28 20:56:41 -0600740 mac->netdev->stats.rx_bytes += tot_bytes;
741 mac->netdev->stats.rx_packets += packets;
742
Olof Johansson72b05b92007-11-28 20:54:28 -0600743 spin_unlock(&rx_ring(mac)->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600744
745 return count;
746}
747
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500748/* Can't make this too large or we blow the kernel stack limits */
749#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
750
Olof Johansson72b05b92007-11-28 20:54:28 -0600751static int pasemi_mac_clean_tx(struct pasemi_mac_txring *txring)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600752{
Olof Johansson34c20622007-11-28 20:56:32 -0600753 struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600754 struct pasemi_mac *mac = txring->mac;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500755 int i, j;
Olof Johanssonad5da102007-10-02 16:27:15 -0500756 unsigned int start, descr_count, buf_count, batch_limit;
757 unsigned int ring_limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500758 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500759 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500760 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
761 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johansson7e9916e2007-11-28 20:57:45 -0600762 int nf[TX_CLEAN_BATCHSIZE];
763 int nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600764
Olof Johansson02df6cf2007-08-22 09:13:03 -0500765 total_count = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500766 batch_limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500767restart:
Olof Johansson72b05b92007-11-28 20:54:28 -0600768 spin_lock_irqsave(&txring->lock, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600769
Olof Johansson72b05b92007-11-28 20:54:28 -0600770 start = txring->next_to_clean;
771 ring_limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500772
Olof Johansson7e9916e2007-11-28 20:57:45 -0600773 prefetch(&TX_DESC_INFO(txring, start+1).skb);
774
Olof Johanssonad5da102007-10-02 16:27:15 -0500775 /* Compensate for when fill has wrapped but clean has not */
776 if (start > ring_limit)
777 ring_limit += TX_RING_SIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500778
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500779 buf_count = 0;
780 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600781
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500782 for (i = start;
Olof Johanssonad5da102007-10-02 16:27:15 -0500783 descr_count < batch_limit && i < ring_limit;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500784 i += buf_count) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600785 u64 mactx = TX_DESC(txring, i);
Olof Johanssonad5da102007-10-02 16:27:15 -0500786 struct sk_buff *skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500787
Olof Johansson7e9916e2007-11-28 20:57:45 -0600788 skb = TX_DESC_INFO(txring, i+1).skb;
789 nr_frags = TX_DESC_INFO(txring, i).dma;
790
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500791 if ((mactx & XCT_MACTX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600792 (*chan->status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500793 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500794
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500795 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500796 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600797 break;
798
Olof Johansson7e9916e2007-11-28 20:57:45 -0600799 buf_count = 2 + nr_frags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500800 /* Since we always fill with an even number of entries, make
801 * sure we skip any unused one at the end as well.
802 */
803 if (buf_count & 1)
804 buf_count++;
Olof Johansson7e9916e2007-11-28 20:57:45 -0600805
806 for (j = 0; j <= nr_frags; j++)
807 dmas[descr_count][j] = TX_DESC_INFO(txring, i+1+j).dma;
808
809 skbs[descr_count] = skb;
810 nf[descr_count] = nr_frags;
811
812 TX_DESC(txring, i) = 0;
813 TX_DESC(txring, i+1) = 0;
814
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500815 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600816 }
Olof Johansson72b05b92007-11-28 20:54:28 -0600817 txring->next_to_clean = i & (TX_RING_SIZE-1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500818
Olof Johansson72b05b92007-11-28 20:54:28 -0600819 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500820 netif_wake_queue(mac->netdev);
821
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500822 for (i = 0; i < descr_count; i++)
Olof Johansson7e9916e2007-11-28 20:57:45 -0600823 pasemi_mac_unmap_tx_skb(mac, nf[i], skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500824
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500825 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500826
827 /* If the batch was full, try to clean more */
Olof Johanssonad5da102007-10-02 16:27:15 -0500828 if (descr_count == batch_limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500829 goto restart;
830
831 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600832}
833
834
835static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
836{
Olof Johansson5c153322007-11-28 20:56:41 -0600837 const struct pasemi_mac_rxring *rxring = data;
Olof Johansson34c20622007-11-28 20:56:32 -0600838 struct pasemi_mac *mac = rxring->mac;
839 struct net_device *dev = mac->netdev;
Olof Johansson5c153322007-11-28 20:56:41 -0600840 const struct pasemi_dmachan *chan = &rxring->chan;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600841 unsigned int reg;
842
Olof Johansson34c20622007-11-28 20:56:32 -0600843 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600844 return IRQ_NONE;
845
Olof Johansson6dfa7522007-05-08 00:47:32 -0500846 /* Don't reset packet count so it won't fire again but clear
847 * all others.
848 */
849
Olof Johansson6dfa7522007-05-08 00:47:32 -0500850 reg = 0;
Olof Johansson34c20622007-11-28 20:56:32 -0600851 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500852 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600853 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500854 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600855
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700856 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500857
Olof Johansson34c20622007-11-28 20:56:32 -0600858 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600859
860 return IRQ_HANDLED;
861}
862
Olof Johansson61cec3b2007-11-28 20:56:54 -0600863#define TX_CLEAN_INTERVAL HZ
864
865static void pasemi_mac_tx_timer(unsigned long data)
866{
867 struct pasemi_mac_txring *txring = (struct pasemi_mac_txring *)data;
868 struct pasemi_mac *mac = txring->mac;
869
870 pasemi_mac_clean_tx(txring);
871
872 mod_timer(&txring->clean_timer, jiffies + TX_CLEAN_INTERVAL);
873
874 pasemi_mac_restart_tx_intr(mac);
875}
876
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600877static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
878{
Olof Johansson72b05b92007-11-28 20:54:28 -0600879 struct pasemi_mac_txring *txring = data;
Olof Johansson5c153322007-11-28 20:56:41 -0600880 const struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson61cec3b2007-11-28 20:56:54 -0600881 struct pasemi_mac *mac = txring->mac;
882 unsigned int reg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600883
Olof Johansson34c20622007-11-28 20:56:32 -0600884 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600885 return IRQ_NONE;
886
Olof Johansson61cec3b2007-11-28 20:56:54 -0600887 reg = 0;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500888
Olof Johansson34c20622007-11-28 20:56:32 -0600889 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500890 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600891 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500892 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600893
Olof Johansson61cec3b2007-11-28 20:56:54 -0600894 mod_timer(&txring->clean_timer, jiffies + (TX_CLEAN_INTERVAL)*2);
895
896 netif_rx_schedule(mac->netdev, &mac->napi);
897
898 if (reg)
899 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600900
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600901 return IRQ_HANDLED;
902}
903
Olof Johanssonb0cd2f92007-11-28 20:58:25 -0600904static void pasemi_mac_intf_disable(struct pasemi_mac *mac)
905{
906 unsigned int flags;
907
908 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
909 flags &= ~PAS_MAC_CFG_PCFG_PE;
910 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
911}
912
913static void pasemi_mac_intf_enable(struct pasemi_mac *mac)
914{
915 unsigned int flags;
916
917 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
918 flags |= PAS_MAC_CFG_PCFG_PE;
919 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
920}
921
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500922static void pasemi_adjust_link(struct net_device *dev)
923{
924 struct pasemi_mac *mac = netdev_priv(dev);
925 int msg;
926 unsigned int flags;
927 unsigned int new_flags;
928
929 if (!mac->phydev->link) {
930 /* If no link, MAC speed settings don't matter. Just report
931 * link down and return.
932 */
933 if (mac->link && netif_msg_link(mac))
934 printk(KERN_INFO "%s: Link is down.\n", dev->name);
935
936 netif_carrier_off(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -0600937 pasemi_mac_intf_disable(mac);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500938 mac->link = 0;
939
940 return;
Olof Johanssonb0cd2f92007-11-28 20:58:25 -0600941 } else {
942 pasemi_mac_intf_enable(mac);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500943 netif_carrier_on(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -0600944 }
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500945
Olof Johanssona85b9422007-09-15 13:40:59 -0700946 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500947 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
948 PAS_MAC_CFG_PCFG_TSR_M);
949
950 if (!mac->phydev->duplex)
951 new_flags |= PAS_MAC_CFG_PCFG_HD;
952
953 switch (mac->phydev->speed) {
954 case 1000:
955 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
956 PAS_MAC_CFG_PCFG_TSR_1G;
957 break;
958 case 100:
959 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
960 PAS_MAC_CFG_PCFG_TSR_100M;
961 break;
962 case 10:
963 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
964 PAS_MAC_CFG_PCFG_TSR_10M;
965 break;
966 default:
967 printk("Unsupported speed %d\n", mac->phydev->speed);
968 }
969
970 /* Print on link or speed/duplex change */
971 msg = mac->link != mac->phydev->link || flags != new_flags;
972
973 mac->duplex = mac->phydev->duplex;
974 mac->speed = mac->phydev->speed;
975 mac->link = mac->phydev->link;
976
977 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700978 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500979
980 if (msg && netif_msg_link(mac))
981 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
982 dev->name, mac->speed, mac->duplex ? "full" : "half");
983}
984
985static int pasemi_mac_phy_init(struct net_device *dev)
986{
987 struct pasemi_mac *mac = netdev_priv(dev);
988 struct device_node *dn, *phy_dn;
989 struct phy_device *phydev;
990 unsigned int phy_id;
991 const phandle *ph;
992 const unsigned int *prop;
993 struct resource r;
994 int ret;
995
996 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700997 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500998 if (!ph)
999 return -ENODEV;
1000 phy_dn = of_find_node_by_phandle(*ph);
1001
Linus Torvalds90287802007-05-08 11:57:17 -07001002 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001003 ret = of_address_to_resource(phy_dn->parent, 0, &r);
1004 if (ret)
1005 goto err;
1006
1007 phy_id = *prop;
1008 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
1009
1010 of_node_put(phy_dn);
1011
1012 mac->link = 0;
1013 mac->speed = 0;
1014 mac->duplex = -1;
1015
1016 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
1017
1018 if (IS_ERR(phydev)) {
1019 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
1020 return PTR_ERR(phydev);
1021 }
1022
1023 mac->phydev = phydev;
1024
1025 return 0;
1026
1027err:
1028 of_node_put(phy_dn);
1029 return -ENODEV;
1030}
1031
1032
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001033static int pasemi_mac_open(struct net_device *dev)
1034{
1035 struct pasemi_mac *mac = netdev_priv(dev);
1036 unsigned int flags;
1037 int ret;
1038
1039 /* enable rx section */
Olof Johansson34c20622007-11-28 20:56:32 -06001040 write_dma_reg(PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001041
1042 /* enable tx section */
Olof Johansson34c20622007-11-28 20:56:32 -06001043 write_dma_reg(PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001044
1045 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
1046 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
1047 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
1048
Olof Johanssona85b9422007-09-15 13:40:59 -07001049 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001050
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001051 ret = pasemi_mac_setup_rx_resources(dev);
1052 if (ret)
1053 goto out_rx_resources;
1054
Olof Johansson34c20622007-11-28 20:56:32 -06001055 mac->tx = pasemi_mac_setup_tx_resources(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001056
1057 if (!mac->tx)
1058 goto out_tx_ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001059
Olof Johansson906674a2007-11-28 20:57:09 -06001060 /* 0x3ff with 33MHz clock is about 31us */
1061 write_iob_reg(PAS_IOB_DMA_COM_TIMEOUTCFG,
1062 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0x3ff));
1063
Olof Johansson34c20622007-11-28 20:56:32 -06001064 write_iob_reg(PAS_IOB_DMA_RXCH_CFG(mac->rx->chan.chno),
Olof Johansson28ae79f2007-11-28 20:57:27 -06001065 PAS_IOB_DMA_RXCH_CFG_CNTTH(256));
Olof Johansson34c20622007-11-28 20:56:32 -06001066
1067 write_iob_reg(PAS_IOB_DMA_TXCH_CFG(mac->tx->chan.chno),
Olof Johansson61cec3b2007-11-28 20:56:54 -06001068 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johansson34c20622007-11-28 20:56:32 -06001069
Olof Johanssona85b9422007-09-15 13:40:59 -07001070 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
Olof Johansson34c20622007-11-28 20:56:32 -06001071 PAS_MAC_IPC_CHNL_DCHNO(mac->rx->chan.chno) |
1072 PAS_MAC_IPC_CHNL_BCH(mac->rx->chan.chno));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001073
1074 /* enable rx if */
Olof Johansson34c20622007-11-28 20:56:32 -06001075 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1076 PAS_DMA_RXINT_RCMDSTA_EN |
1077 PAS_DMA_RXINT_RCMDSTA_DROPS_M |
1078 PAS_DMA_RXINT_RCMDSTA_BP |
1079 PAS_DMA_RXINT_RCMDSTA_OO |
1080 PAS_DMA_RXINT_RCMDSTA_BT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001081
1082 /* enable rx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001083 pasemi_dma_start_chan(&rx_ring(mac)->chan, PAS_DMA_RXCHAN_CCMDSTA_DU |
1084 PAS_DMA_RXCHAN_CCMDSTA_OD |
1085 PAS_DMA_RXCHAN_CCMDSTA_FD |
1086 PAS_DMA_RXCHAN_CCMDSTA_DT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001087
1088 /* enable tx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001089 pasemi_dma_start_chan(&tx_ring(mac)->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
1090 PAS_DMA_TXCHAN_TCMDSTA_DB |
1091 PAS_DMA_TXCHAN_TCMDSTA_DE |
1092 PAS_DMA_TXCHAN_TCMDSTA_DA);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001093
Olof Johansson928773c2007-09-26 16:25:06 -05001094 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001095
Olof Johansson34c20622007-11-28 20:56:32 -06001096 write_dma_reg(PAS_DMA_RXCHAN_INCR(rx_ring(mac)->chan.chno),
1097 RX_RING_SIZE>>1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -05001098
Olof Johansson72b05b92007-11-28 20:54:28 -06001099 /* Clear out any residual packet count state from firmware */
1100 pasemi_mac_restart_rx_intr(mac);
1101 pasemi_mac_restart_tx_intr(mac);
1102
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001103 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
Olof Johansson36033762007-09-26 16:24:42 -05001104
1105 if (mac->type == MAC_TYPE_GMAC)
1106 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
1107 else
1108 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
1109
1110 /* Enable interface in MAC */
1111 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
1112
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001113 ret = pasemi_mac_phy_init(dev);
Olof Johanssonb0cd2f92007-11-28 20:58:25 -06001114 if (ret) {
1115 /* Since we won't get link notification, just enable RX */
1116 pasemi_mac_intf_enable(mac);
1117 if (mac->type == MAC_TYPE_GMAC) {
1118 /* Warn for missing PHY on SGMII (1Gig) ports */
1119 dev_warn(&mac->pdev->dev,
1120 "PHY init failed: %d.\n", ret);
1121 dev_warn(&mac->pdev->dev,
1122 "Defaulting to 1Gbit full duplex\n");
1123 }
Olof Johansson8304b632007-11-28 20:58:06 -06001124 }
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001125
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001126 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001127 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001128
Olof Johansson72b05b92007-11-28 20:54:28 -06001129 snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx",
1130 dev->name);
Olof Johansson771f7402007-05-08 00:47:21 -05001131
Olof Johansson34c20622007-11-28 20:56:32 -06001132 ret = request_irq(mac->tx->chan.irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johansson72b05b92007-11-28 20:54:28 -06001133 mac->tx_irq_name, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001134 if (ret) {
1135 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001136 mac->tx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001137 goto out_tx_int;
1138 }
1139
Olof Johansson72b05b92007-11-28 20:54:28 -06001140 snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx",
1141 dev->name);
1142
Olof Johansson34c20622007-11-28 20:56:32 -06001143 ret = request_irq(mac->rx->chan.irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
1144 mac->rx_irq_name, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001145 if (ret) {
1146 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001147 mac->rx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001148 goto out_rx_int;
1149 }
1150
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001151 if (mac->phydev)
1152 phy_start(mac->phydev);
1153
Olof Johansson61cec3b2007-11-28 20:56:54 -06001154 init_timer(&mac->tx->clean_timer);
1155 mac->tx->clean_timer.function = pasemi_mac_tx_timer;
1156 mac->tx->clean_timer.data = (unsigned long)mac->tx;
1157 mac->tx->clean_timer.expires = jiffies+HZ;
1158 add_timer(&mac->tx->clean_timer);
1159
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001160 return 0;
1161
1162out_rx_int:
Olof Johansson34c20622007-11-28 20:56:32 -06001163 free_irq(mac->tx->chan.irq, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001164out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001165 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001166 netif_stop_queue(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001167out_tx_ring:
1168 if (mac->tx)
1169 pasemi_mac_free_tx_resources(mac);
1170 pasemi_mac_free_rx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001171out_rx_resources:
1172
1173 return ret;
1174}
1175
1176#define MAX_RETRIES 5000
1177
1178static int pasemi_mac_close(struct net_device *dev)
1179{
1180 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson9e81d332007-10-02 16:27:39 -05001181 unsigned int sta;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001182 int retries;
Olof Johansson34c20622007-11-28 20:56:32 -06001183 int rxch, txch;
1184
1185 rxch = rx_ring(mac)->chan.chno;
1186 txch = tx_ring(mac)->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001187
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001188 if (mac->phydev) {
1189 phy_stop(mac->phydev);
1190 phy_disconnect(mac->phydev);
1191 }
1192
Olof Johansson61cec3b2007-11-28 20:56:54 -06001193 del_timer_sync(&mac->tx->clean_timer);
1194
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001195 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001196 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001197
Olof Johansson34c20622007-11-28 20:56:32 -06001198 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson9e81d332007-10-02 16:27:39 -05001199 if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
1200 PAS_DMA_RXINT_RCMDSTA_OO |
1201 PAS_DMA_RXINT_RCMDSTA_BT))
1202 printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);
1203
Olof Johansson34c20622007-11-28 20:56:32 -06001204 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001205 if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
1206 PAS_DMA_RXCHAN_CCMDSTA_OD |
1207 PAS_DMA_RXCHAN_CCMDSTA_FD |
1208 PAS_DMA_RXCHAN_CCMDSTA_DT))
1209 printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);
1210
Olof Johansson34c20622007-11-28 20:56:32 -06001211 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
Olof Johansson72b05b92007-11-28 20:54:28 -06001212 if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB |
1213 PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA))
Olof Johansson9e81d332007-10-02 16:27:39 -05001214 printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);
1215
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001216 /* Clean out any pending buffers */
Olof Johansson72b05b92007-11-28 20:54:28 -06001217 pasemi_mac_clean_tx(tx_ring(mac));
1218 pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001219
1220 /* Disable interface */
Olof Johansson34c20622007-11-28 20:56:32 -06001221 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch),
Olof Johansson72b05b92007-11-28 20:54:28 -06001222 PAS_DMA_TXCHAN_TCMDSTA_ST);
Olof Johansson34c20622007-11-28 20:56:32 -06001223 write_dma_reg( PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
Olof Johansson72b05b92007-11-28 20:54:28 -06001224 PAS_DMA_RXINT_RCMDSTA_ST);
Olof Johansson34c20622007-11-28 20:56:32 -06001225 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch),
Olof Johansson72b05b92007-11-28 20:54:28 -06001226 PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001227
1228 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001229 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001230 if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001231 break;
1232 cond_resched();
1233 }
1234
Olof Johansson9e81d332007-10-02 16:27:39 -05001235 if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johansson34c20622007-11-28 20:56:32 -06001236 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001237
1238 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001239 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001240 if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001241 break;
1242 cond_resched();
1243 }
1244
Olof Johansson9e81d332007-10-02 16:27:39 -05001245 if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001246 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001247
1248 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001249 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson9e81d332007-10-02 16:27:39 -05001250 if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001251 break;
1252 cond_resched();
1253 }
1254
Olof Johansson9e81d332007-10-02 16:27:39 -05001255 if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001256 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001257
1258 /* Then, disable the channel. This must be done separately from
1259 * stopping, since you can't disable when active.
1260 */
1261
Olof Johansson34c20622007-11-28 20:56:32 -06001262 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch), 0);
1263 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch), 0);
1264 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001265
Olof Johansson34c20622007-11-28 20:56:32 -06001266 free_irq(mac->tx->chan.irq, mac->tx);
1267 free_irq(mac->rx->chan.irq, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001268
1269 /* Free resources */
Olof Johansson72b05b92007-11-28 20:54:28 -06001270 pasemi_mac_free_rx_resources(mac);
1271 pasemi_mac_free_tx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001272
1273 return 0;
1274}
1275
1276static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1277{
1278 struct pasemi_mac *mac = netdev_priv(dev);
1279 struct pasemi_mac_txring *txring;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001280 u64 dflags, mactx;
1281 dma_addr_t map[MAX_SKB_FRAGS+1];
1282 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001283 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001284 int i, nfrags;
Olof Johansson5c153322007-11-28 20:56:41 -06001285 int fill;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001286
Olof Johanssondbd62af2007-11-06 22:20:39 -06001287 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001288
1289 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001290 const unsigned char *nh = skb_network_header(skb);
1291
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001292 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001293 case IPPROTO_TCP:
1294 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001295 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001296 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001297 break;
1298 case IPPROTO_UDP:
1299 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001300 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001301 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001302 break;
1303 }
1304 }
1305
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001306 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001307
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001308 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1309 PCI_DMA_TODEVICE);
1310 map_size[0] = skb_headlen(skb);
1311 if (dma_mapping_error(map[0]))
1312 goto out_err_nolock;
1313
1314 for (i = 0; i < nfrags; i++) {
1315 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1316
1317 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1318 frag->page_offset, frag->size,
1319 PCI_DMA_TODEVICE);
1320 map_size[i+1] = frag->size;
1321 if (dma_mapping_error(map[i+1])) {
1322 nfrags = i;
1323 goto out_err_nolock;
1324 }
1325 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001326
Olof Johansson26fcfa92007-08-22 09:12:59 -05001327 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001328
Olof Johansson72b05b92007-11-28 20:54:28 -06001329 txring = tx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001330
1331 spin_lock_irqsave(&txring->lock, flags);
1332
Olof Johansson5c153322007-11-28 20:56:41 -06001333 fill = txring->next_to_fill;
1334
Olof Johanssonad5da102007-10-02 16:27:15 -05001335 /* Avoid stepping on the same cache line that the DMA controller
1336 * is currently about to send, so leave at least 8 words available.
1337 * Total free space needed is mactx + fragments + 8
1338 */
1339 if (RING_AVAIL(txring) < nfrags + 10) {
1340 /* no room -- stop the queue and wait for tx intr */
1341 netif_stop_queue(dev);
1342 goto out_err;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001343 }
1344
Olof Johansson5c153322007-11-28 20:56:41 -06001345 TX_DESC(txring, fill) = mactx;
Olof Johansson7e9916e2007-11-28 20:57:45 -06001346 TX_DESC_INFO(txring, fill).dma = nfrags;
Olof Johansson5c153322007-11-28 20:56:41 -06001347 fill++;
1348 TX_DESC_INFO(txring, fill).skb = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001349 for (i = 0; i <= nfrags; i++) {
Olof Johansson5c153322007-11-28 20:56:41 -06001350 TX_DESC(txring, fill+i) =
Olof Johansson72b05b92007-11-28 20:54:28 -06001351 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
Olof Johansson5c153322007-11-28 20:56:41 -06001352 TX_DESC_INFO(txring, fill+i).dma = map[i];
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001353 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001354
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001355 /* We have to add an even number of 8-byte entries to the ring
1356 * even if the last one is unused. That means always an odd number
1357 * of pointers + one mactx descriptor.
1358 */
1359 if (nfrags & 1)
1360 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001361
Olof Johansson5c153322007-11-28 20:56:41 -06001362 txring->next_to_fill = (fill + nfrags + 1) & (TX_RING_SIZE-1);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001363
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001364 dev->stats.tx_packets++;
1365 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001366
1367 spin_unlock_irqrestore(&txring->lock, flags);
1368
Olof Johansson34c20622007-11-28 20:56:32 -06001369 write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001370
1371 return NETDEV_TX_OK;
1372
1373out_err:
1374 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001375out_err_nolock:
1376 while (nfrags--)
1377 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1378 PCI_DMA_TODEVICE);
1379
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001380 return NETDEV_TX_BUSY;
1381}
1382
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001383static void pasemi_mac_set_rx_mode(struct net_device *dev)
1384{
Olof Johansson5c153322007-11-28 20:56:41 -06001385 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001386 unsigned int flags;
1387
Olof Johanssona85b9422007-09-15 13:40:59 -07001388 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001389
1390 /* Set promiscuous */
1391 if (dev->flags & IFF_PROMISC)
1392 flags |= PAS_MAC_CFG_PCFG_PR;
1393 else
1394 flags &= ~PAS_MAC_CFG_PCFG_PR;
1395
Olof Johanssona85b9422007-09-15 13:40:59 -07001396 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001397}
1398
1399
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001400static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001401{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001402 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1403 struct net_device *dev = mac->netdev;
1404 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001405
Olof Johansson72b05b92007-11-28 20:54:28 -06001406 pasemi_mac_clean_tx(tx_ring(mac));
1407 pkts = pasemi_mac_clean_rx(rx_ring(mac), budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001408 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001409 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001410 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001411
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001412 pasemi_mac_restart_rx_intr(mac);
Olof Johansson61cec3b2007-11-28 20:56:54 -06001413 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001414 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001415 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001416}
1417
1418static int __devinit
1419pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1420{
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001421 struct net_device *dev;
1422 struct pasemi_mac *mac;
1423 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001424 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001425
1426 err = pci_enable_device(pdev);
1427 if (err)
1428 return err;
1429
1430 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1431 if (dev == NULL) {
1432 dev_err(&pdev->dev,
1433 "pasemi_mac: Could not allocate ethernet device.\n");
1434 err = -ENOMEM;
1435 goto out_disable_device;
1436 }
1437
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001438 pci_set_drvdata(pdev, dev);
1439 SET_NETDEV_DEV(dev, &pdev->dev);
1440
1441 mac = netdev_priv(dev);
1442
1443 mac->pdev = pdev;
1444 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001445
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001446 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1447
Olof Johansson5c153322007-11-28 20:56:41 -06001448 dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG |
1449 NETIF_F_HIGHDMA;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001450
Olof Johansson28ae79f2007-11-28 20:57:27 -06001451 mac->lro_mgr.max_aggr = LRO_MAX_AGGR;
1452 mac->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
1453 mac->lro_mgr.lro_arr = mac->lro_desc;
1454 mac->lro_mgr.get_skb_header = get_skb_hdr;
1455 mac->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1456 mac->lro_mgr.dev = mac->netdev;
1457 mac->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1458 mac->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1459
1460
Olof Johansson34c20622007-11-28 20:56:32 -06001461 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1462 if (!mac->dma_pdev) {
1463 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1464 err = -ENODEV;
1465 goto out;
1466 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001467
Olof Johansson34c20622007-11-28 20:56:32 -06001468 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1469 if (!mac->iob_pdev) {
1470 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1471 err = -ENODEV;
1472 goto out;
1473 }
1474
1475 /* get mac addr from device tree */
1476 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1477 err = -ENODEV;
1478 goto out;
1479 }
1480 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1481
1482 mac->dma_if = mac_to_intf(mac);
1483 if (mac->dma_if < 0) {
1484 dev_err(&mac->pdev->dev, "Can't map DMA interface\n");
1485 err = -ENODEV;
1486 goto out;
1487 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001488
1489 switch (pdev->device) {
1490 case 0xa005:
1491 mac->type = MAC_TYPE_GMAC;
1492 break;
1493 case 0xa006:
1494 mac->type = MAC_TYPE_XAUI;
1495 break;
1496 default:
1497 err = -ENODEV;
1498 goto out;
1499 }
1500
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001501 dev->open = pasemi_mac_open;
1502 dev->stop = pasemi_mac_close;
1503 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001504 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johansson5cea73b2008-01-23 13:56:19 -06001505 dev->set_mac_address = pasemi_mac_set_mac_addr;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001506
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001507 if (err)
1508 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001509
Olof Johanssonceb51362007-05-08 00:47:49 -05001510 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1511
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001512 /* Enable most messages by default */
1513 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1514
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001515 err = register_netdev(dev);
1516
1517 if (err) {
1518 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1519 err);
1520 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001521 } else if netif_msg_probe(mac)
Olof Johansson72b05b92007-11-28 20:54:28 -06001522 printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001523 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
Olof Johansson72b05b92007-11-28 20:54:28 -06001524 mac->dma_if, print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001525
1526 return err;
1527
1528out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001529 if (mac->iob_pdev)
1530 pci_dev_put(mac->iob_pdev);
1531 if (mac->dma_pdev)
1532 pci_dev_put(mac->dma_pdev);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001533
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001534 free_netdev(dev);
1535out_disable_device:
1536 pci_disable_device(pdev);
1537 return err;
1538
1539}
1540
1541static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1542{
1543 struct net_device *netdev = pci_get_drvdata(pdev);
1544 struct pasemi_mac *mac;
1545
1546 if (!netdev)
1547 return;
1548
1549 mac = netdev_priv(netdev);
1550
1551 unregister_netdev(netdev);
1552
1553 pci_disable_device(pdev);
1554 pci_dev_put(mac->dma_pdev);
1555 pci_dev_put(mac->iob_pdev);
1556
Olof Johansson34c20622007-11-28 20:56:32 -06001557 pasemi_dma_free_chan(&mac->tx->chan);
1558 pasemi_dma_free_chan(&mac->rx->chan);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001559
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001560 pci_set_drvdata(pdev, NULL);
1561 free_netdev(netdev);
1562}
1563
1564static struct pci_device_id pasemi_mac_pci_tbl[] = {
1565 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1566 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001567 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001568};
1569
1570MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1571
1572static struct pci_driver pasemi_mac_driver = {
1573 .name = "pasemi_mac",
1574 .id_table = pasemi_mac_pci_tbl,
1575 .probe = pasemi_mac_probe,
1576 .remove = __devexit_p(pasemi_mac_remove),
1577};
1578
1579static void __exit pasemi_mac_cleanup_module(void)
1580{
1581 pci_unregister_driver(&pasemi_mac_driver);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001582}
1583
1584int pasemi_mac_init_module(void)
1585{
Olof Johansson34c20622007-11-28 20:56:32 -06001586 int err;
1587
1588 err = pasemi_dma_init();
1589 if (err)
1590 return err;
1591
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001592 return pci_register_driver(&pasemi_mac_driver);
1593}
1594
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001595module_init(pasemi_mac_init_module);
1596module_exit(pasemi_mac_cleanup_module);