blob: 07475bb7d8600a6f0320454a5a22e3b8791c8180 [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 Johansson28ae79f2007-11-28 20:57:27 -0600224static int get_skb_hdr(struct sk_buff *skb, void **iphdr,
225 void **tcph, u64 *hdr_flags, void *data)
226{
227 u64 macrx = (u64) data;
228 unsigned int ip_len;
229 struct iphdr *iph;
230
231 /* IPv4 header checksum failed */
232 if ((macrx & XCT_MACRX_HTY_M) != XCT_MACRX_HTY_IPV4_OK)
233 return -1;
234
235 /* non tcp packet */
236 skb_reset_network_header(skb);
237 iph = ip_hdr(skb);
238 if (iph->protocol != IPPROTO_TCP)
239 return -1;
240
241 ip_len = ip_hdrlen(skb);
242 skb_set_transport_header(skb, ip_len);
243 *tcph = tcp_hdr(skb);
244
245 /* check if ip header and tcp header are complete */
246 if (iph->tot_len < ip_len + tcp_hdrlen(skb))
247 return -1;
248
249 *hdr_flags = LRO_IPV4 | LRO_TCP;
250 *iphdr = iph;
251
252 return 0;
253}
254
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500255static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
Olof Johansson7e9916e2007-11-28 20:57:45 -0600256 const int nfrags,
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500257 struct sk_buff *skb,
Olof Johansson5c153322007-11-28 20:56:41 -0600258 const dma_addr_t *dmas)
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500259{
260 int f;
Olof Johansson5c153322007-11-28 20:56:41 -0600261 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500262
Olof Johansson5c153322007-11-28 20:56:41 -0600263 pci_unmap_single(pdev, dmas[0], skb_headlen(skb), PCI_DMA_TODEVICE);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500264
265 for (f = 0; f < nfrags; f++) {
266 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
267
Olof Johansson5c153322007-11-28 20:56:41 -0600268 pci_unmap_page(pdev, dmas[f+1], frag->size, PCI_DMA_TODEVICE);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500269 }
270 dev_kfree_skb_irq(skb);
271
272 /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
273 * aligned up to a power of 2
274 */
275 return (nfrags + 3) & ~1;
276}
277
Olof Johansson5c153322007-11-28 20:56:41 -0600278static int pasemi_mac_setup_rx_resources(const struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600279{
280 struct pasemi_mac_rxring *ring;
281 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson34c20622007-11-28 20:56:32 -0600282 int chno;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500283 unsigned int cfg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600284
Olof Johansson34c20622007-11-28 20:56:32 -0600285 ring = pasemi_dma_alloc_chan(RXCHAN, sizeof(struct pasemi_mac_rxring),
286 offsetof(struct pasemi_mac_rxring, chan));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600287
Olof Johansson34c20622007-11-28 20:56:32 -0600288 if (!ring) {
289 dev_err(&mac->pdev->dev, "Can't allocate RX channel\n");
290 goto out_chan;
291 }
292 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600293
294 spin_lock_init(&ring->lock);
295
Olof Johansson021fa222007-08-22 09:13:11 -0500296 ring->size = RX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500297 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600298 RX_RING_SIZE, GFP_KERNEL);
299
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500300 if (!ring->ring_info)
301 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600302
303 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600304 if (pasemi_dma_alloc_ring(&ring->chan, RX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500305 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600306
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600307 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
308 RX_RING_SIZE * sizeof(u64),
309 &ring->buf_dma, GFP_KERNEL);
310 if (!ring->buffers)
Olof Johansson34c20622007-11-28 20:56:32 -0600311 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600312
313 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
314
Olof Johansson34c20622007-11-28 20:56:32 -0600315 write_dma_reg(PAS_DMA_RXCHAN_BASEL(chno),
316 PAS_DMA_RXCHAN_BASEL_BRBL(ring->chan.ring_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600317
Olof Johansson34c20622007-11-28 20:56:32 -0600318 write_dma_reg(PAS_DMA_RXCHAN_BASEU(chno),
319 PAS_DMA_RXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32) |
320 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600321
Olof Johansson5c153322007-11-28 20:56:41 -0600322 cfg = PAS_DMA_RXCHAN_CFG_HBU(2);
Olof Johanssonaf289e82007-10-03 13:03:54 -0500323
324 if (translation_enabled())
325 cfg |= PAS_DMA_RXCHAN_CFG_CTR;
326
Olof Johansson34c20622007-11-28 20:56:32 -0600327 write_dma_reg(PAS_DMA_RXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600328
Olof Johansson34c20622007-11-28 20:56:32 -0600329 write_dma_reg(PAS_DMA_RXINT_BASEL(mac->dma_if),
330 PAS_DMA_RXINT_BASEL_BRBL(ring->buf_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600331
Olof Johansson34c20622007-11-28 20:56:32 -0600332 write_dma_reg(PAS_DMA_RXINT_BASEU(mac->dma_if),
333 PAS_DMA_RXINT_BASEU_BRBH(ring->buf_dma >> 32) |
334 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600335
Olof Johansson5c153322007-11-28 20:56:41 -0600336 cfg = PAS_DMA_RXINT_CFG_DHL(2) | PAS_DMA_RXINT_CFG_L2 |
Olof Johanssonaf289e82007-10-03 13:03:54 -0500337 PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP |
338 PAS_DMA_RXINT_CFG_HEN;
339
340 if (translation_enabled())
341 cfg |= PAS_DMA_RXINT_CFG_ITRR | PAS_DMA_RXINT_CFG_ITR;
342
Olof Johansson34c20622007-11-28 20:56:32 -0600343 write_dma_reg(PAS_DMA_RXINT_CFG(mac->dma_if), cfg);
Olof Johanssonc0efd522007-08-22 09:12:52 -0500344
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600345 ring->next_to_fill = 0;
346 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600347 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600348 mac->rx = ring;
349
350 return 0;
351
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500352out_ring_desc:
353 kfree(ring->ring_info);
354out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600355 pasemi_dma_free_chan(&ring->chan);
356out_chan:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600357 return -ENOMEM;
358}
359
Olof Johansson72b05b92007-11-28 20:54:28 -0600360static struct pasemi_mac_txring *
Olof Johansson5c153322007-11-28 20:56:41 -0600361pasemi_mac_setup_tx_resources(const struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600362{
363 struct pasemi_mac *mac = netdev_priv(dev);
364 u32 val;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600365 struct pasemi_mac_txring *ring;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500366 unsigned int cfg;
Olof Johansson34c20622007-11-28 20:56:32 -0600367 int chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600368
Olof Johansson34c20622007-11-28 20:56:32 -0600369 ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_txring),
370 offsetof(struct pasemi_mac_txring, chan));
371
372 if (!ring) {
373 dev_err(&mac->pdev->dev, "Can't allocate TX channel\n");
374 goto out_chan;
375 }
376
377 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600378
379 spin_lock_init(&ring->lock);
380
Olof Johansson021fa222007-08-22 09:13:11 -0500381 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500382 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600383 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500384 if (!ring->ring_info)
385 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600386
387 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600388 if (pasemi_dma_alloc_ring(&ring->chan, TX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500389 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600390
Olof Johansson34c20622007-11-28 20:56:32 -0600391 write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno),
392 PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma));
393 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500394 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600395
Olof Johansson34c20622007-11-28 20:56:32 -0600396 write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600397
Olof Johanssonaf289e82007-10-03 13:03:54 -0500398 cfg = PAS_DMA_TXCHAN_CFG_TY_IFACE |
399 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
400 PAS_DMA_TXCHAN_CFG_UP |
401 PAS_DMA_TXCHAN_CFG_WT(2);
402
403 if (translation_enabled())
404 cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
405
Olof Johansson34c20622007-11-28 20:56:32 -0600406 write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600407
Olof Johansson021fa222007-08-22 09:13:11 -0500408 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600409 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600410 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600411
Olof Johansson72b05b92007-11-28 20:54:28 -0600412 return ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600413
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500414out_ring_desc:
415 kfree(ring->ring_info);
416out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600417 pasemi_dma_free_chan(&ring->chan);
418out_chan:
Olof Johansson72b05b92007-11-28 20:54:28 -0600419 return NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600420}
421
Olof Johansson72b05b92007-11-28 20:54:28 -0600422static void pasemi_mac_free_tx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600423{
Olof Johansson72b05b92007-11-28 20:54:28 -0600424 struct pasemi_mac_txring *txring = tx_ring(mac);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500425 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600426 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500427 dma_addr_t dmas[MAX_SKB_FRAGS+1];
Olof Johansson7e9916e2007-11-28 20:57:45 -0600428 int freed, nfrags;
Olof Johanssonad5da102007-10-02 16:27:15 -0500429 int start, limit;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600430
Olof Johansson72b05b92007-11-28 20:54:28 -0600431 start = txring->next_to_clean;
432 limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500433
434 /* Compensate for when fill has wrapped and clean has not */
435 if (start > limit)
436 limit += TX_RING_SIZE;
437
438 for (i = start; i < limit; i += freed) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600439 info = &txring->ring_info[(i+1) & (TX_RING_SIZE-1)];
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500440 if (info->dma && info->skb) {
Olof Johansson7e9916e2007-11-28 20:57:45 -0600441 nfrags = skb_shinfo(info->skb)->nr_frags;
442 for (j = 0; j <= nfrags; j++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600443 dmas[j] = txring->ring_info[(i+1+j) &
444 (TX_RING_SIZE-1)].dma;
Olof Johansson7e9916e2007-11-28 20:57:45 -0600445 freed = pasemi_mac_unmap_tx_skb(mac, nfrags,
446 info->skb, dmas);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500447 } else
448 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600449 }
450
Olof Johansson72b05b92007-11-28 20:54:28 -0600451 kfree(txring->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600452 pasemi_dma_free_chan(&txring->chan);
453
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600454}
455
Olof Johansson72b05b92007-11-28 20:54:28 -0600456static void pasemi_mac_free_rx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600457{
Olof Johansson72b05b92007-11-28 20:54:28 -0600458 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600459 unsigned int i;
460 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600461
462 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600463 info = &RX_DESC_INFO(rx, i);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500464 if (info->skb && info->dma) {
465 pci_unmap_single(mac->dma_pdev,
466 info->dma,
467 info->skb->len,
468 PCI_DMA_FROMDEVICE);
469 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600470 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500471 info->dma = 0;
472 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600473 }
474
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500475 for (i = 0; i < RX_RING_SIZE; i++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600476 RX_DESC(rx, i) = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500477
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600478 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
Olof Johansson72b05b92007-11-28 20:54:28 -0600479 rx_ring(mac)->buffers, rx_ring(mac)->buf_dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600480
Olof Johansson72b05b92007-11-28 20:54:28 -0600481 kfree(rx_ring(mac)->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600482 pasemi_dma_free_chan(&rx_ring(mac)->chan);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600483 mac->rx = NULL;
484}
485
Olof Johansson5c153322007-11-28 20:56:41 -0600486static void pasemi_mac_replenish_rx_ring(const struct net_device *dev,
487 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600488{
Olof Johansson5c153322007-11-28 20:56:41 -0600489 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -0600490 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500491 int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600492
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500493 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600494 return;
495
Olof Johansson72b05b92007-11-28 20:54:28 -0600496 fill = rx_ring(mac)->next_to_fill;
Olof Johansson928773c2007-09-26 16:25:06 -0500497 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600498 struct pasemi_mac_buffer *info = &RX_DESC_INFO(rx, fill);
499 u64 *buff = &RX_BUFF(rx, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600500 struct sk_buff *skb;
501 dma_addr_t dma;
502
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500503 /* Entry in use? */
504 WARN_ON(*buff);
505
Olof Johansson5d894942007-11-28 20:57:56 -0600506 skb = dev_alloc_skb(BUF_SIZE);
507 skb_reserve(skb, LOCAL_SKB_ALIGN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600508
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500509 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600510 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600511
Olof Johansson8dc121a2007-10-02 16:26:53 -0500512 dma = pci_map_single(mac->dma_pdev, skb->data,
513 BUF_SIZE - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600514 PCI_DMA_FROMDEVICE);
515
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500516 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600517 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600518 break;
519 }
520
521 info->skb = skb;
522 info->dma = dma;
523 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500524 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600525 }
526
527 wmb();
528
Olof Johansson34c20622007-11-28 20:56:32 -0600529 write_dma_reg(PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600530
Olof Johansson72b05b92007-11-28 20:54:28 -0600531 rx_ring(mac)->next_to_fill = (rx_ring(mac)->next_to_fill + count) &
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500532 (RX_RING_SIZE - 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600533}
534
Olof Johansson5c153322007-11-28 20:56:41 -0600535static void pasemi_mac_restart_rx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500536{
Olof Johansson906674a2007-11-28 20:57:09 -0600537 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johansson52a94352007-05-12 18:01:09 -0500538 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500539 /* Re-enable packet count interrupts: finally
540 * ack the packet count interrupt we got in rx_intr.
541 */
542
Olof Johansson906674a2007-11-28 20:57:09 -0600543 pcnt = *rx->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500544
Olof Johansson52a94352007-05-12 18:01:09 -0500545 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500546
Olof Johansson906674a2007-11-28 20:57:09 -0600547 if (*rx->chan.status & PAS_STATUS_TIMER)
548 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
549
Olof Johansson34c20622007-11-28 20:56:32 -0600550 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(mac->rx->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500551}
552
Olof Johansson5c153322007-11-28 20:56:41 -0600553static void pasemi_mac_restart_tx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500554{
Olof Johansson52a94352007-05-12 18:01:09 -0500555 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500556
557 /* Re-enable packet count interrupts */
Olof Johansson34c20622007-11-28 20:56:32 -0600558 pcnt = *tx_ring(mac)->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500559
Olof Johansson52a94352007-05-12 18:01:09 -0500560 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500561
Olof Johansson34c20622007-11-28 20:56:32 -0600562 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(tx_ring(mac)->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500563}
564
565
Olof Johansson5c153322007-11-28 20:56:41 -0600566static inline void pasemi_mac_rx_error(const struct pasemi_mac *mac,
567 const u64 macrx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500568{
569 unsigned int rcmdsta, ccmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600570 struct pasemi_dmachan *chan = &rx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500571
572 if (!netif_msg_rx_err(mac))
573 return;
574
Olof Johansson34c20622007-11-28 20:56:32 -0600575 rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
576 ccmdsta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500577
578 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
Olof Johansson34c20622007-11-28 20:56:32 -0600579 macrx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500580
581 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
582 rcmdsta, ccmdsta);
583}
584
Olof Johansson5c153322007-11-28 20:56:41 -0600585static inline void pasemi_mac_tx_error(const struct pasemi_mac *mac,
586 const u64 mactx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500587{
588 unsigned int cmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600589 struct pasemi_dmachan *chan = &tx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500590
591 if (!netif_msg_tx_err(mac))
592 return;
593
Olof Johansson34c20622007-11-28 20:56:32 -0600594 cmdsta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500595
596 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
Olof Johansson34c20622007-11-28 20:56:32 -0600597 "tx status 0x%016lx\n", mactx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500598
599 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
600}
601
Olof Johansson5c153322007-11-28 20:56:41 -0600602static int pasemi_mac_clean_rx(struct pasemi_mac_rxring *rx,
603 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600604{
Olof Johansson5c153322007-11-28 20:56:41 -0600605 const struct pasemi_dmachan *chan = &rx->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600606 struct pasemi_mac *mac = rx->mac;
Olof Johansson5c153322007-11-28 20:56:41 -0600607 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500608 unsigned int n;
Olof Johansson5c153322007-11-28 20:56:41 -0600609 int count, buf_index, tot_bytes, packets;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500610 struct pasemi_mac_buffer *info;
611 struct sk_buff *skb;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500612 unsigned int len;
Olof Johansson5c153322007-11-28 20:56:41 -0600613 u64 macrx, eval;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500614 dma_addr_t dma;
Olof Johansson5c153322007-11-28 20:56:41 -0600615
616 tot_bytes = 0;
617 packets = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600618
Olof Johansson72b05b92007-11-28 20:54:28 -0600619 spin_lock(&rx->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600620
Olof Johansson72b05b92007-11-28 20:54:28 -0600621 n = rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600622
Olof Johansson72b05b92007-11-28 20:54:28 -0600623 prefetch(&RX_DESC(rx, n));
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500624
625 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600626 macrx = RX_DESC(rx, n);
Olof Johansson5c153322007-11-28 20:56:41 -0600627 prefetch(&RX_DESC(rx, n+4));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600628
Olof Johansson69c29d82007-10-02 16:24:51 -0500629 if ((macrx & XCT_MACRX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600630 (*chan->status & PAS_STATUS_ERROR))
Olof Johansson69c29d82007-10-02 16:24:51 -0500631 pasemi_mac_rx_error(mac, macrx);
632
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500633 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600634 break;
635
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600636 info = NULL;
637
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500638 BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600639
Olof Johansson72b05b92007-11-28 20:54:28 -0600640 eval = (RX_DESC(rx, n+1) & XCT_RXRES_8B_EVAL_M) >>
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500641 XCT_RXRES_8B_EVAL_S;
642 buf_index = eval-1;
643
Olof Johansson72b05b92007-11-28 20:54:28 -0600644 dma = (RX_DESC(rx, n+2) & XCT_PTR_ADDR_M);
645 info = &RX_DESC_INFO(rx, buf_index);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500646
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500647 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600648
Olof Johansson5c153322007-11-28 20:56:41 -0600649 prefetch_skb(skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600650
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500651 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600652
Olof Johansson5c153322007-11-28 20:56:41 -0600653 pci_unmap_single(pdev, dma, BUF_SIZE-LOCAL_SKB_ALIGN,
654 PCI_DMA_FROMDEVICE);
Olof Johansson32bee772007-11-06 22:21:38 -0600655
656 if (macrx & XCT_MACRX_CRC) {
657 /* CRC error flagged */
658 mac->netdev->stats.rx_errors++;
659 mac->netdev->stats.rx_crc_errors++;
Olof Johansson4352d822007-12-03 21:34:14 -0600660 /* No need to free skb, it'll be reused */
Olof Johansson32bee772007-11-06 22:21:38 -0600661 goto next;
662 }
663
Olof Johansson5d894942007-11-28 20:57:56 -0600664 info->skb = NULL;
Olof Johanssonad5da102007-10-02 16:27:15 -0500665 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500666
Olof Johansson26fcfa92007-08-22 09:12:59 -0500667 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500668 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500669 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600670 XCT_MACRX_CSUM_S;
671 } else
672 skb->ip_summed = CHECKSUM_NONE;
673
Olof Johansson5c153322007-11-28 20:56:41 -0600674 packets++;
675 tot_bytes += len;
676
677 /* Don't include CRC */
678 skb_put(skb, len-4);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600679
Olof Johansson26fcfa92007-08-22 09:12:59 -0500680 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johansson28ae79f2007-11-28 20:57:27 -0600681 lro_receive_skb(&mac->lro_mgr, skb, (void *)macrx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600682
Olof Johansson32bee772007-11-06 22:21:38 -0600683next:
Olof Johansson72b05b92007-11-28 20:54:28 -0600684 RX_DESC(rx, n) = 0;
685 RX_DESC(rx, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500686
Olof Johanssonad5da102007-10-02 16:27:15 -0500687 /* Need to zero it out since hardware doesn't, since the
688 * replenish loop uses it to tell when it's done.
689 */
Olof Johansson72b05b92007-11-28 20:54:28 -0600690 RX_BUFF(rx, buf_index) = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500691
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500692 n += 4;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600693 }
694
Olof Johansson9a50beb2007-10-02 16:26:30 -0500695 if (n > RX_RING_SIZE) {
696 /* Errata 5971 workaround: L2 target of headers */
Olof Johansson34c20622007-11-28 20:56:32 -0600697 write_iob_reg(PAS_IOB_COM_PKTHDRCNT, 0);
Olof Johansson9a50beb2007-10-02 16:26:30 -0500698 n &= (RX_RING_SIZE-1);
699 }
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500700
Olof Johansson72b05b92007-11-28 20:54:28 -0600701 rx_ring(mac)->next_to_clean = n;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500702
Olof Johansson28ae79f2007-11-28 20:57:27 -0600703 lro_flush_all(&mac->lro_mgr);
704
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500705 /* Increase is in number of 16-byte entries, and since each descriptor
706 * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
707 * count*2.
708 */
Olof Johansson34c20622007-11-28 20:56:32 -0600709 write_dma_reg(PAS_DMA_RXCHAN_INCR(mac->rx->chan.chno), count << 1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500710
711 pasemi_mac_replenish_rx_ring(mac->netdev, count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600712
Olof Johansson5c153322007-11-28 20:56:41 -0600713 mac->netdev->stats.rx_bytes += tot_bytes;
714 mac->netdev->stats.rx_packets += packets;
715
Olof Johansson72b05b92007-11-28 20:54:28 -0600716 spin_unlock(&rx_ring(mac)->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600717
718 return count;
719}
720
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500721/* Can't make this too large or we blow the kernel stack limits */
722#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
723
Olof Johansson72b05b92007-11-28 20:54:28 -0600724static int pasemi_mac_clean_tx(struct pasemi_mac_txring *txring)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600725{
Olof Johansson34c20622007-11-28 20:56:32 -0600726 struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600727 struct pasemi_mac *mac = txring->mac;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500728 int i, j;
Olof Johanssonad5da102007-10-02 16:27:15 -0500729 unsigned int start, descr_count, buf_count, batch_limit;
730 unsigned int ring_limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500731 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500732 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500733 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
734 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johansson7e9916e2007-11-28 20:57:45 -0600735 int nf[TX_CLEAN_BATCHSIZE];
736 int nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600737
Olof Johansson02df6cf2007-08-22 09:13:03 -0500738 total_count = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500739 batch_limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500740restart:
Olof Johansson72b05b92007-11-28 20:54:28 -0600741 spin_lock_irqsave(&txring->lock, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600742
Olof Johansson72b05b92007-11-28 20:54:28 -0600743 start = txring->next_to_clean;
744 ring_limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500745
Olof Johansson7e9916e2007-11-28 20:57:45 -0600746 prefetch(&TX_DESC_INFO(txring, start+1).skb);
747
Olof Johanssonad5da102007-10-02 16:27:15 -0500748 /* Compensate for when fill has wrapped but clean has not */
749 if (start > ring_limit)
750 ring_limit += TX_RING_SIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500751
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500752 buf_count = 0;
753 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600754
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500755 for (i = start;
Olof Johanssonad5da102007-10-02 16:27:15 -0500756 descr_count < batch_limit && i < ring_limit;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500757 i += buf_count) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600758 u64 mactx = TX_DESC(txring, i);
Olof Johanssonad5da102007-10-02 16:27:15 -0500759 struct sk_buff *skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500760
Olof Johansson7e9916e2007-11-28 20:57:45 -0600761 skb = TX_DESC_INFO(txring, i+1).skb;
762 nr_frags = TX_DESC_INFO(txring, i).dma;
763
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500764 if ((mactx & XCT_MACTX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600765 (*chan->status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500766 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500767
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500768 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500769 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600770 break;
771
Olof Johansson7e9916e2007-11-28 20:57:45 -0600772 buf_count = 2 + nr_frags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500773 /* Since we always fill with an even number of entries, make
774 * sure we skip any unused one at the end as well.
775 */
776 if (buf_count & 1)
777 buf_count++;
Olof Johansson7e9916e2007-11-28 20:57:45 -0600778
779 for (j = 0; j <= nr_frags; j++)
780 dmas[descr_count][j] = TX_DESC_INFO(txring, i+1+j).dma;
781
782 skbs[descr_count] = skb;
783 nf[descr_count] = nr_frags;
784
785 TX_DESC(txring, i) = 0;
786 TX_DESC(txring, i+1) = 0;
787
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500788 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600789 }
Olof Johansson72b05b92007-11-28 20:54:28 -0600790 txring->next_to_clean = i & (TX_RING_SIZE-1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500791
Olof Johansson72b05b92007-11-28 20:54:28 -0600792 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500793 netif_wake_queue(mac->netdev);
794
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500795 for (i = 0; i < descr_count; i++)
Olof Johansson7e9916e2007-11-28 20:57:45 -0600796 pasemi_mac_unmap_tx_skb(mac, nf[i], skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500797
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500798 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500799
800 /* If the batch was full, try to clean more */
Olof Johanssonad5da102007-10-02 16:27:15 -0500801 if (descr_count == batch_limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500802 goto restart;
803
804 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600805}
806
807
808static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
809{
Olof Johansson5c153322007-11-28 20:56:41 -0600810 const struct pasemi_mac_rxring *rxring = data;
Olof Johansson34c20622007-11-28 20:56:32 -0600811 struct pasemi_mac *mac = rxring->mac;
812 struct net_device *dev = mac->netdev;
Olof Johansson5c153322007-11-28 20:56:41 -0600813 const struct pasemi_dmachan *chan = &rxring->chan;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600814 unsigned int reg;
815
Olof Johansson34c20622007-11-28 20:56:32 -0600816 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600817 return IRQ_NONE;
818
Olof Johansson6dfa7522007-05-08 00:47:32 -0500819 /* Don't reset packet count so it won't fire again but clear
820 * all others.
821 */
822
Olof Johansson6dfa7522007-05-08 00:47:32 -0500823 reg = 0;
Olof Johansson34c20622007-11-28 20:56:32 -0600824 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500825 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600826 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500827 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600828
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700829 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500830
Olof Johansson34c20622007-11-28 20:56:32 -0600831 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600832
833 return IRQ_HANDLED;
834}
835
Olof Johansson61cec3b2007-11-28 20:56:54 -0600836#define TX_CLEAN_INTERVAL HZ
837
838static void pasemi_mac_tx_timer(unsigned long data)
839{
840 struct pasemi_mac_txring *txring = (struct pasemi_mac_txring *)data;
841 struct pasemi_mac *mac = txring->mac;
842
843 pasemi_mac_clean_tx(txring);
844
845 mod_timer(&txring->clean_timer, jiffies + TX_CLEAN_INTERVAL);
846
847 pasemi_mac_restart_tx_intr(mac);
848}
849
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600850static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
851{
Olof Johansson72b05b92007-11-28 20:54:28 -0600852 struct pasemi_mac_txring *txring = data;
Olof Johansson5c153322007-11-28 20:56:41 -0600853 const struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson61cec3b2007-11-28 20:56:54 -0600854 struct pasemi_mac *mac = txring->mac;
855 unsigned int reg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600856
Olof Johansson34c20622007-11-28 20:56:32 -0600857 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600858 return IRQ_NONE;
859
Olof Johansson61cec3b2007-11-28 20:56:54 -0600860 reg = 0;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500861
Olof Johansson34c20622007-11-28 20:56:32 -0600862 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500863 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600864 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500865 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600866
Olof Johansson61cec3b2007-11-28 20:56:54 -0600867 mod_timer(&txring->clean_timer, jiffies + (TX_CLEAN_INTERVAL)*2);
868
869 netif_rx_schedule(mac->netdev, &mac->napi);
870
871 if (reg)
872 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600873
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600874 return IRQ_HANDLED;
875}
876
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500877static void pasemi_adjust_link(struct net_device *dev)
878{
879 struct pasemi_mac *mac = netdev_priv(dev);
880 int msg;
881 unsigned int flags;
882 unsigned int new_flags;
883
884 if (!mac->phydev->link) {
885 /* If no link, MAC speed settings don't matter. Just report
886 * link down and return.
887 */
888 if (mac->link && netif_msg_link(mac))
889 printk(KERN_INFO "%s: Link is down.\n", dev->name);
890
891 netif_carrier_off(dev);
892 mac->link = 0;
893
894 return;
895 } else
896 netif_carrier_on(dev);
897
Olof Johanssona85b9422007-09-15 13:40:59 -0700898 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500899 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
900 PAS_MAC_CFG_PCFG_TSR_M);
901
902 if (!mac->phydev->duplex)
903 new_flags |= PAS_MAC_CFG_PCFG_HD;
904
905 switch (mac->phydev->speed) {
906 case 1000:
907 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
908 PAS_MAC_CFG_PCFG_TSR_1G;
909 break;
910 case 100:
911 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
912 PAS_MAC_CFG_PCFG_TSR_100M;
913 break;
914 case 10:
915 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
916 PAS_MAC_CFG_PCFG_TSR_10M;
917 break;
918 default:
919 printk("Unsupported speed %d\n", mac->phydev->speed);
920 }
921
922 /* Print on link or speed/duplex change */
923 msg = mac->link != mac->phydev->link || flags != new_flags;
924
925 mac->duplex = mac->phydev->duplex;
926 mac->speed = mac->phydev->speed;
927 mac->link = mac->phydev->link;
928
929 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700930 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500931
932 if (msg && netif_msg_link(mac))
933 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
934 dev->name, mac->speed, mac->duplex ? "full" : "half");
935}
936
937static int pasemi_mac_phy_init(struct net_device *dev)
938{
939 struct pasemi_mac *mac = netdev_priv(dev);
940 struct device_node *dn, *phy_dn;
941 struct phy_device *phydev;
942 unsigned int phy_id;
943 const phandle *ph;
944 const unsigned int *prop;
945 struct resource r;
946 int ret;
947
948 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700949 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500950 if (!ph)
951 return -ENODEV;
952 phy_dn = of_find_node_by_phandle(*ph);
953
Linus Torvalds90287802007-05-08 11:57:17 -0700954 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500955 ret = of_address_to_resource(phy_dn->parent, 0, &r);
956 if (ret)
957 goto err;
958
959 phy_id = *prop;
960 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
961
962 of_node_put(phy_dn);
963
964 mac->link = 0;
965 mac->speed = 0;
966 mac->duplex = -1;
967
968 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
969
970 if (IS_ERR(phydev)) {
971 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
972 return PTR_ERR(phydev);
973 }
974
975 mac->phydev = phydev;
976
977 return 0;
978
979err:
980 of_node_put(phy_dn);
981 return -ENODEV;
982}
983
984
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600985static int pasemi_mac_open(struct net_device *dev)
986{
987 struct pasemi_mac *mac = netdev_priv(dev);
988 unsigned int flags;
989 int ret;
990
991 /* enable rx section */
Olof Johansson34c20622007-11-28 20:56:32 -0600992 write_dma_reg(PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600993
994 /* enable tx section */
Olof Johansson34c20622007-11-28 20:56:32 -0600995 write_dma_reg(PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600996
997 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
998 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
999 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
1000
Olof Johanssona85b9422007-09-15 13:40:59 -07001001 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001002
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001003 ret = pasemi_mac_setup_rx_resources(dev);
1004 if (ret)
1005 goto out_rx_resources;
1006
Olof Johansson34c20622007-11-28 20:56:32 -06001007 mac->tx = pasemi_mac_setup_tx_resources(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001008
1009 if (!mac->tx)
1010 goto out_tx_ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001011
Olof Johansson906674a2007-11-28 20:57:09 -06001012 /* 0x3ff with 33MHz clock is about 31us */
1013 write_iob_reg(PAS_IOB_DMA_COM_TIMEOUTCFG,
1014 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0x3ff));
1015
Olof Johansson34c20622007-11-28 20:56:32 -06001016 write_iob_reg(PAS_IOB_DMA_RXCH_CFG(mac->rx->chan.chno),
Olof Johansson28ae79f2007-11-28 20:57:27 -06001017 PAS_IOB_DMA_RXCH_CFG_CNTTH(256));
Olof Johansson34c20622007-11-28 20:56:32 -06001018
1019 write_iob_reg(PAS_IOB_DMA_TXCH_CFG(mac->tx->chan.chno),
Olof Johansson61cec3b2007-11-28 20:56:54 -06001020 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johansson34c20622007-11-28 20:56:32 -06001021
Olof Johanssona85b9422007-09-15 13:40:59 -07001022 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
Olof Johansson34c20622007-11-28 20:56:32 -06001023 PAS_MAC_IPC_CHNL_DCHNO(mac->rx->chan.chno) |
1024 PAS_MAC_IPC_CHNL_BCH(mac->rx->chan.chno));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001025
1026 /* enable rx if */
Olof Johansson34c20622007-11-28 20:56:32 -06001027 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1028 PAS_DMA_RXINT_RCMDSTA_EN |
1029 PAS_DMA_RXINT_RCMDSTA_DROPS_M |
1030 PAS_DMA_RXINT_RCMDSTA_BP |
1031 PAS_DMA_RXINT_RCMDSTA_OO |
1032 PAS_DMA_RXINT_RCMDSTA_BT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001033
1034 /* enable rx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001035 pasemi_dma_start_chan(&rx_ring(mac)->chan, PAS_DMA_RXCHAN_CCMDSTA_DU |
1036 PAS_DMA_RXCHAN_CCMDSTA_OD |
1037 PAS_DMA_RXCHAN_CCMDSTA_FD |
1038 PAS_DMA_RXCHAN_CCMDSTA_DT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001039
1040 /* enable tx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001041 pasemi_dma_start_chan(&tx_ring(mac)->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
1042 PAS_DMA_TXCHAN_TCMDSTA_DB |
1043 PAS_DMA_TXCHAN_TCMDSTA_DE |
1044 PAS_DMA_TXCHAN_TCMDSTA_DA);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001045
Olof Johansson928773c2007-09-26 16:25:06 -05001046 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001047
Olof Johansson34c20622007-11-28 20:56:32 -06001048 write_dma_reg(PAS_DMA_RXCHAN_INCR(rx_ring(mac)->chan.chno),
1049 RX_RING_SIZE>>1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -05001050
Olof Johansson72b05b92007-11-28 20:54:28 -06001051 /* Clear out any residual packet count state from firmware */
1052 pasemi_mac_restart_rx_intr(mac);
1053 pasemi_mac_restart_tx_intr(mac);
1054
Olof Johansson36033762007-09-26 16:24:42 -05001055 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
1056 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
1057
1058 if (mac->type == MAC_TYPE_GMAC)
1059 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
1060 else
1061 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
1062
1063 /* Enable interface in MAC */
1064 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
1065
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001066 ret = pasemi_mac_phy_init(dev);
1067 /* Some configs don't have PHYs (XAUI etc), so don't complain about
1068 * failed init due to -ENODEV.
1069 */
1070 if (ret && ret != -ENODEV)
1071 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
1072
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001073 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001074 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001075
Olof Johansson72b05b92007-11-28 20:54:28 -06001076 snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx",
1077 dev->name);
Olof Johansson771f7402007-05-08 00:47:21 -05001078
Olof Johansson34c20622007-11-28 20:56:32 -06001079 ret = request_irq(mac->tx->chan.irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johansson72b05b92007-11-28 20:54:28 -06001080 mac->tx_irq_name, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001081 if (ret) {
1082 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001083 mac->tx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001084 goto out_tx_int;
1085 }
1086
Olof Johansson72b05b92007-11-28 20:54:28 -06001087 snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx",
1088 dev->name);
1089
Olof Johansson34c20622007-11-28 20:56:32 -06001090 ret = request_irq(mac->rx->chan.irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
1091 mac->rx_irq_name, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001092 if (ret) {
1093 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001094 mac->rx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001095 goto out_rx_int;
1096 }
1097
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001098 if (mac->phydev)
1099 phy_start(mac->phydev);
1100
Olof Johansson61cec3b2007-11-28 20:56:54 -06001101 init_timer(&mac->tx->clean_timer);
1102 mac->tx->clean_timer.function = pasemi_mac_tx_timer;
1103 mac->tx->clean_timer.data = (unsigned long)mac->tx;
1104 mac->tx->clean_timer.expires = jiffies+HZ;
1105 add_timer(&mac->tx->clean_timer);
1106
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001107 return 0;
1108
1109out_rx_int:
Olof Johansson34c20622007-11-28 20:56:32 -06001110 free_irq(mac->tx->chan.irq, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001111out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001112 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001113 netif_stop_queue(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001114out_tx_ring:
1115 if (mac->tx)
1116 pasemi_mac_free_tx_resources(mac);
1117 pasemi_mac_free_rx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001118out_rx_resources:
1119
1120 return ret;
1121}
1122
1123#define MAX_RETRIES 5000
1124
1125static int pasemi_mac_close(struct net_device *dev)
1126{
1127 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson9e81d332007-10-02 16:27:39 -05001128 unsigned int sta;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001129 int retries;
Olof Johansson34c20622007-11-28 20:56:32 -06001130 int rxch, txch;
1131
1132 rxch = rx_ring(mac)->chan.chno;
1133 txch = tx_ring(mac)->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001134
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001135 if (mac->phydev) {
1136 phy_stop(mac->phydev);
1137 phy_disconnect(mac->phydev);
1138 }
1139
Olof Johansson61cec3b2007-11-28 20:56:54 -06001140 del_timer_sync(&mac->tx->clean_timer);
1141
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001142 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001143 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001144
Olof Johansson34c20622007-11-28 20:56:32 -06001145 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson9e81d332007-10-02 16:27:39 -05001146 if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
1147 PAS_DMA_RXINT_RCMDSTA_OO |
1148 PAS_DMA_RXINT_RCMDSTA_BT))
1149 printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);
1150
Olof Johansson34c20622007-11-28 20:56:32 -06001151 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001152 if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
1153 PAS_DMA_RXCHAN_CCMDSTA_OD |
1154 PAS_DMA_RXCHAN_CCMDSTA_FD |
1155 PAS_DMA_RXCHAN_CCMDSTA_DT))
1156 printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);
1157
Olof Johansson34c20622007-11-28 20:56:32 -06001158 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
Olof Johansson72b05b92007-11-28 20:54:28 -06001159 if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB |
1160 PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA))
Olof Johansson9e81d332007-10-02 16:27:39 -05001161 printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);
1162
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001163 /* Clean out any pending buffers */
Olof Johansson72b05b92007-11-28 20:54:28 -06001164 pasemi_mac_clean_tx(tx_ring(mac));
1165 pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001166
1167 /* Disable interface */
Olof Johansson34c20622007-11-28 20:56:32 -06001168 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch),
Olof Johansson72b05b92007-11-28 20:54:28 -06001169 PAS_DMA_TXCHAN_TCMDSTA_ST);
Olof Johansson34c20622007-11-28 20:56:32 -06001170 write_dma_reg( PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
Olof Johansson72b05b92007-11-28 20:54:28 -06001171 PAS_DMA_RXINT_RCMDSTA_ST);
Olof Johansson34c20622007-11-28 20:56:32 -06001172 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch),
Olof Johansson72b05b92007-11-28 20:54:28 -06001173 PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001174
1175 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001176 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001177 if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001178 break;
1179 cond_resched();
1180 }
1181
Olof Johansson9e81d332007-10-02 16:27:39 -05001182 if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johansson34c20622007-11-28 20:56:32 -06001183 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001184
1185 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001186 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001187 if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001188 break;
1189 cond_resched();
1190 }
1191
Olof Johansson9e81d332007-10-02 16:27:39 -05001192 if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001193 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001194
1195 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001196 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson9e81d332007-10-02 16:27:39 -05001197 if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001198 break;
1199 cond_resched();
1200 }
1201
Olof Johansson9e81d332007-10-02 16:27:39 -05001202 if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001203 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001204
1205 /* Then, disable the channel. This must be done separately from
1206 * stopping, since you can't disable when active.
1207 */
1208
Olof Johansson34c20622007-11-28 20:56:32 -06001209 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch), 0);
1210 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch), 0);
1211 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001212
Olof Johansson34c20622007-11-28 20:56:32 -06001213 free_irq(mac->tx->chan.irq, mac->tx);
1214 free_irq(mac->rx->chan.irq, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001215
1216 /* Free resources */
Olof Johansson72b05b92007-11-28 20:54:28 -06001217 pasemi_mac_free_rx_resources(mac);
1218 pasemi_mac_free_tx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001219
1220 return 0;
1221}
1222
1223static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1224{
1225 struct pasemi_mac *mac = netdev_priv(dev);
1226 struct pasemi_mac_txring *txring;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001227 u64 dflags, mactx;
1228 dma_addr_t map[MAX_SKB_FRAGS+1];
1229 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001230 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001231 int i, nfrags;
Olof Johansson5c153322007-11-28 20:56:41 -06001232 int fill;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001233
Olof Johanssondbd62af2007-11-06 22:20:39 -06001234 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001235
1236 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001237 const unsigned char *nh = skb_network_header(skb);
1238
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001239 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001240 case IPPROTO_TCP:
1241 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001242 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001243 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001244 break;
1245 case IPPROTO_UDP:
1246 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001247 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001248 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001249 break;
1250 }
1251 }
1252
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001253 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001254
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001255 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1256 PCI_DMA_TODEVICE);
1257 map_size[0] = skb_headlen(skb);
1258 if (dma_mapping_error(map[0]))
1259 goto out_err_nolock;
1260
1261 for (i = 0; i < nfrags; i++) {
1262 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1263
1264 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1265 frag->page_offset, frag->size,
1266 PCI_DMA_TODEVICE);
1267 map_size[i+1] = frag->size;
1268 if (dma_mapping_error(map[i+1])) {
1269 nfrags = i;
1270 goto out_err_nolock;
1271 }
1272 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001273
Olof Johansson26fcfa92007-08-22 09:12:59 -05001274 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001275
Olof Johansson72b05b92007-11-28 20:54:28 -06001276 txring = tx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001277
1278 spin_lock_irqsave(&txring->lock, flags);
1279
Olof Johansson5c153322007-11-28 20:56:41 -06001280 fill = txring->next_to_fill;
1281
Olof Johanssonad5da102007-10-02 16:27:15 -05001282 /* Avoid stepping on the same cache line that the DMA controller
1283 * is currently about to send, so leave at least 8 words available.
1284 * Total free space needed is mactx + fragments + 8
1285 */
1286 if (RING_AVAIL(txring) < nfrags + 10) {
1287 /* no room -- stop the queue and wait for tx intr */
1288 netif_stop_queue(dev);
1289 goto out_err;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001290 }
1291
Olof Johansson5c153322007-11-28 20:56:41 -06001292 TX_DESC(txring, fill) = mactx;
Olof Johansson7e9916e2007-11-28 20:57:45 -06001293 TX_DESC_INFO(txring, fill).dma = nfrags;
Olof Johansson5c153322007-11-28 20:56:41 -06001294 fill++;
1295 TX_DESC_INFO(txring, fill).skb = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001296 for (i = 0; i <= nfrags; i++) {
Olof Johansson5c153322007-11-28 20:56:41 -06001297 TX_DESC(txring, fill+i) =
Olof Johansson72b05b92007-11-28 20:54:28 -06001298 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
Olof Johansson5c153322007-11-28 20:56:41 -06001299 TX_DESC_INFO(txring, fill+i).dma = map[i];
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001300 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001301
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001302 /* We have to add an even number of 8-byte entries to the ring
1303 * even if the last one is unused. That means always an odd number
1304 * of pointers + one mactx descriptor.
1305 */
1306 if (nfrags & 1)
1307 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001308
Olof Johansson5c153322007-11-28 20:56:41 -06001309 txring->next_to_fill = (fill + nfrags + 1) & (TX_RING_SIZE-1);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001310
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001311 dev->stats.tx_packets++;
1312 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001313
1314 spin_unlock_irqrestore(&txring->lock, flags);
1315
Olof Johansson34c20622007-11-28 20:56:32 -06001316 write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001317
1318 return NETDEV_TX_OK;
1319
1320out_err:
1321 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001322out_err_nolock:
1323 while (nfrags--)
1324 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1325 PCI_DMA_TODEVICE);
1326
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001327 return NETDEV_TX_BUSY;
1328}
1329
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001330static void pasemi_mac_set_rx_mode(struct net_device *dev)
1331{
Olof Johansson5c153322007-11-28 20:56:41 -06001332 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001333 unsigned int flags;
1334
Olof Johanssona85b9422007-09-15 13:40:59 -07001335 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001336
1337 /* Set promiscuous */
1338 if (dev->flags & IFF_PROMISC)
1339 flags |= PAS_MAC_CFG_PCFG_PR;
1340 else
1341 flags &= ~PAS_MAC_CFG_PCFG_PR;
1342
Olof Johanssona85b9422007-09-15 13:40:59 -07001343 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001344}
1345
1346
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001347static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001348{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001349 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1350 struct net_device *dev = mac->netdev;
1351 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001352
Olof Johansson72b05b92007-11-28 20:54:28 -06001353 pasemi_mac_clean_tx(tx_ring(mac));
1354 pkts = pasemi_mac_clean_rx(rx_ring(mac), budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001355 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001356 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001357 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001358
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001359 pasemi_mac_restart_rx_intr(mac);
Olof Johansson61cec3b2007-11-28 20:56:54 -06001360 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001361 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001362 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001363}
1364
1365static int __devinit
1366pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1367{
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001368 struct net_device *dev;
1369 struct pasemi_mac *mac;
1370 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001371 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001372
1373 err = pci_enable_device(pdev);
1374 if (err)
1375 return err;
1376
1377 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1378 if (dev == NULL) {
1379 dev_err(&pdev->dev,
1380 "pasemi_mac: Could not allocate ethernet device.\n");
1381 err = -ENOMEM;
1382 goto out_disable_device;
1383 }
1384
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001385 pci_set_drvdata(pdev, dev);
1386 SET_NETDEV_DEV(dev, &pdev->dev);
1387
1388 mac = netdev_priv(dev);
1389
1390 mac->pdev = pdev;
1391 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001392
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001393 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1394
Olof Johansson5c153322007-11-28 20:56:41 -06001395 dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG |
1396 NETIF_F_HIGHDMA;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001397
Olof Johansson28ae79f2007-11-28 20:57:27 -06001398 mac->lro_mgr.max_aggr = LRO_MAX_AGGR;
1399 mac->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
1400 mac->lro_mgr.lro_arr = mac->lro_desc;
1401 mac->lro_mgr.get_skb_header = get_skb_hdr;
1402 mac->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1403 mac->lro_mgr.dev = mac->netdev;
1404 mac->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1405 mac->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1406
1407
Olof Johansson34c20622007-11-28 20:56:32 -06001408 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1409 if (!mac->dma_pdev) {
1410 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1411 err = -ENODEV;
1412 goto out;
1413 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001414
Olof Johansson34c20622007-11-28 20:56:32 -06001415 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1416 if (!mac->iob_pdev) {
1417 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1418 err = -ENODEV;
1419 goto out;
1420 }
1421
1422 /* get mac addr from device tree */
1423 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1424 err = -ENODEV;
1425 goto out;
1426 }
1427 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1428
1429 mac->dma_if = mac_to_intf(mac);
1430 if (mac->dma_if < 0) {
1431 dev_err(&mac->pdev->dev, "Can't map DMA interface\n");
1432 err = -ENODEV;
1433 goto out;
1434 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001435
1436 switch (pdev->device) {
1437 case 0xa005:
1438 mac->type = MAC_TYPE_GMAC;
1439 break;
1440 case 0xa006:
1441 mac->type = MAC_TYPE_XAUI;
1442 break;
1443 default:
1444 err = -ENODEV;
1445 goto out;
1446 }
1447
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001448 dev->open = pasemi_mac_open;
1449 dev->stop = pasemi_mac_close;
1450 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001451 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001452
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001453 if (err)
1454 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001455
Olof Johanssonceb51362007-05-08 00:47:49 -05001456 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1457
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001458 /* Enable most messages by default */
1459 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1460
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001461 err = register_netdev(dev);
1462
1463 if (err) {
1464 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1465 err);
1466 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001467 } else if netif_msg_probe(mac)
Olof Johansson72b05b92007-11-28 20:54:28 -06001468 printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001469 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
Olof Johansson72b05b92007-11-28 20:54:28 -06001470 mac->dma_if, print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001471
1472 return err;
1473
1474out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001475 if (mac->iob_pdev)
1476 pci_dev_put(mac->iob_pdev);
1477 if (mac->dma_pdev)
1478 pci_dev_put(mac->dma_pdev);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001479
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001480 free_netdev(dev);
1481out_disable_device:
1482 pci_disable_device(pdev);
1483 return err;
1484
1485}
1486
1487static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1488{
1489 struct net_device *netdev = pci_get_drvdata(pdev);
1490 struct pasemi_mac *mac;
1491
1492 if (!netdev)
1493 return;
1494
1495 mac = netdev_priv(netdev);
1496
1497 unregister_netdev(netdev);
1498
1499 pci_disable_device(pdev);
1500 pci_dev_put(mac->dma_pdev);
1501 pci_dev_put(mac->iob_pdev);
1502
Olof Johansson34c20622007-11-28 20:56:32 -06001503 pasemi_dma_free_chan(&mac->tx->chan);
1504 pasemi_dma_free_chan(&mac->rx->chan);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001505
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001506 pci_set_drvdata(pdev, NULL);
1507 free_netdev(netdev);
1508}
1509
1510static struct pci_device_id pasemi_mac_pci_tbl[] = {
1511 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1512 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001513 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001514};
1515
1516MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1517
1518static struct pci_driver pasemi_mac_driver = {
1519 .name = "pasemi_mac",
1520 .id_table = pasemi_mac_pci_tbl,
1521 .probe = pasemi_mac_probe,
1522 .remove = __devexit_p(pasemi_mac_remove),
1523};
1524
1525static void __exit pasemi_mac_cleanup_module(void)
1526{
1527 pci_unregister_driver(&pasemi_mac_driver);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001528}
1529
1530int pasemi_mac_init_module(void)
1531{
Olof Johansson34c20622007-11-28 20:56:32 -06001532 int err;
1533
1534 err = pasemi_dma_init();
1535 if (err)
1536 return err;
1537
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001538 return pci_register_driver(&pasemi_mac_driver);
1539}
1540
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001541module_init(pasemi_mac_init_module);
1542module_exit(pasemi_mac_cleanup_module);