blob: 98b639742680cd90d3dcd988035be5e0b5f573ed [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,
256 struct sk_buff *skb,
Olof Johansson5c153322007-11-28 20:56:41 -0600257 const dma_addr_t *dmas)
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500258{
259 int f;
260 int nfrags = skb_shinfo(skb)->nr_frags;
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];
428 int freed;
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 Johanssonad3c20d2007-10-02 16:26:13 -0500441 for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600442 dmas[j] = txring->ring_info[(i+1+j) &
443 (TX_RING_SIZE-1)].dma;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500444 freed = pasemi_mac_unmap_tx_skb(mac, info->skb, dmas);
445 } else
446 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600447 }
448
Olof Johansson72b05b92007-11-28 20:54:28 -0600449 kfree(txring->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600450 pasemi_dma_free_chan(&txring->chan);
451
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600452}
453
Olof Johansson72b05b92007-11-28 20:54:28 -0600454static void pasemi_mac_free_rx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600455{
Olof Johansson72b05b92007-11-28 20:54:28 -0600456 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600457 unsigned int i;
458 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600459
460 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600461 info = &RX_DESC_INFO(rx, i);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500462 if (info->skb && info->dma) {
463 pci_unmap_single(mac->dma_pdev,
464 info->dma,
465 info->skb->len,
466 PCI_DMA_FROMDEVICE);
467 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600468 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500469 info->dma = 0;
470 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600471 }
472
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500473 for (i = 0; i < RX_RING_SIZE; i++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600474 RX_DESC(rx, i) = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500475
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600476 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
Olof Johansson72b05b92007-11-28 20:54:28 -0600477 rx_ring(mac)->buffers, rx_ring(mac)->buf_dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600478
Olof Johansson72b05b92007-11-28 20:54:28 -0600479 kfree(rx_ring(mac)->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600480 pasemi_dma_free_chan(&rx_ring(mac)->chan);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600481 mac->rx = NULL;
482}
483
Olof Johansson5c153322007-11-28 20:56:41 -0600484static void pasemi_mac_replenish_rx_ring(const struct net_device *dev,
485 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600486{
Olof Johansson5c153322007-11-28 20:56:41 -0600487 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -0600488 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500489 int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600490
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500491 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600492 return;
493
Olof Johansson72b05b92007-11-28 20:54:28 -0600494 fill = rx_ring(mac)->next_to_fill;
Olof Johansson928773c2007-09-26 16:25:06 -0500495 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600496 struct pasemi_mac_buffer *info = &RX_DESC_INFO(rx, fill);
497 u64 *buff = &RX_BUFF(rx, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600498 struct sk_buff *skb;
499 dma_addr_t dma;
500
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500501 /* Entry in use? */
502 WARN_ON(*buff);
503
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500504 /* skb might still be in there for recycle on short receives */
505 if (info->skb)
506 skb = info->skb;
Olof Johansson8dc121a2007-10-02 16:26:53 -0500507 else {
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500508 skb = dev_alloc_skb(BUF_SIZE);
Olof Johansson8dc121a2007-10-02 16:26:53 -0500509 skb_reserve(skb, LOCAL_SKB_ALIGN);
510 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600511
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500512 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600513 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600514
Olof Johansson8dc121a2007-10-02 16:26:53 -0500515 dma = pci_map_single(mac->dma_pdev, skb->data,
516 BUF_SIZE - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600517 PCI_DMA_FROMDEVICE);
518
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500519 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600520 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600521 break;
522 }
523
524 info->skb = skb;
525 info->dma = dma;
526 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500527 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600528 }
529
530 wmb();
531
Olof Johansson34c20622007-11-28 20:56:32 -0600532 write_dma_reg(PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600533
Olof Johansson72b05b92007-11-28 20:54:28 -0600534 rx_ring(mac)->next_to_fill = (rx_ring(mac)->next_to_fill + count) &
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500535 (RX_RING_SIZE - 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600536}
537
Olof Johansson5c153322007-11-28 20:56:41 -0600538static void pasemi_mac_restart_rx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500539{
Olof Johansson906674a2007-11-28 20:57:09 -0600540 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johansson52a94352007-05-12 18:01:09 -0500541 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500542 /* Re-enable packet count interrupts: finally
543 * ack the packet count interrupt we got in rx_intr.
544 */
545
Olof Johansson906674a2007-11-28 20:57:09 -0600546 pcnt = *rx->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500547
Olof Johansson52a94352007-05-12 18:01:09 -0500548 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500549
Olof Johansson906674a2007-11-28 20:57:09 -0600550 if (*rx->chan.status & PAS_STATUS_TIMER)
551 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
552
Olof Johansson34c20622007-11-28 20:56:32 -0600553 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(mac->rx->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500554}
555
Olof Johansson5c153322007-11-28 20:56:41 -0600556static void pasemi_mac_restart_tx_intr(const struct pasemi_mac *mac)
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500557{
Olof Johansson52a94352007-05-12 18:01:09 -0500558 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500559
560 /* Re-enable packet count interrupts */
Olof Johansson34c20622007-11-28 20:56:32 -0600561 pcnt = *tx_ring(mac)->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500562
Olof Johansson52a94352007-05-12 18:01:09 -0500563 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500564
Olof Johansson34c20622007-11-28 20:56:32 -0600565 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(tx_ring(mac)->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500566}
567
568
Olof Johansson5c153322007-11-28 20:56:41 -0600569static inline void pasemi_mac_rx_error(const struct pasemi_mac *mac,
570 const u64 macrx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500571{
572 unsigned int rcmdsta, ccmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600573 struct pasemi_dmachan *chan = &rx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500574
575 if (!netif_msg_rx_err(mac))
576 return;
577
Olof Johansson34c20622007-11-28 20:56:32 -0600578 rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
579 ccmdsta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500580
581 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
Olof Johansson34c20622007-11-28 20:56:32 -0600582 macrx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500583
584 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
585 rcmdsta, ccmdsta);
586}
587
Olof Johansson5c153322007-11-28 20:56:41 -0600588static inline void pasemi_mac_tx_error(const struct pasemi_mac *mac,
589 const u64 mactx)
Olof Johansson69c29d82007-10-02 16:24:51 -0500590{
591 unsigned int cmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600592 struct pasemi_dmachan *chan = &tx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500593
594 if (!netif_msg_tx_err(mac))
595 return;
596
Olof Johansson34c20622007-11-28 20:56:32 -0600597 cmdsta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500598
599 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
Olof Johansson34c20622007-11-28 20:56:32 -0600600 "tx status 0x%016lx\n", mactx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500601
602 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
603}
604
Olof Johansson5c153322007-11-28 20:56:41 -0600605static int pasemi_mac_clean_rx(struct pasemi_mac_rxring *rx,
606 const int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600607{
Olof Johansson5c153322007-11-28 20:56:41 -0600608 const struct pasemi_dmachan *chan = &rx->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600609 struct pasemi_mac *mac = rx->mac;
Olof Johansson5c153322007-11-28 20:56:41 -0600610 struct pci_dev *pdev = mac->dma_pdev;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500611 unsigned int n;
Olof Johansson5c153322007-11-28 20:56:41 -0600612 int count, buf_index, tot_bytes, packets;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500613 struct pasemi_mac_buffer *info;
614 struct sk_buff *skb;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500615 unsigned int len;
Olof Johansson5c153322007-11-28 20:56:41 -0600616 u64 macrx, eval;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500617 dma_addr_t dma;
Olof Johansson5c153322007-11-28 20:56:41 -0600618
619 tot_bytes = 0;
620 packets = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600621
Olof Johansson72b05b92007-11-28 20:54:28 -0600622 spin_lock(&rx->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600623
Olof Johansson72b05b92007-11-28 20:54:28 -0600624 n = rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600625
Olof Johansson72b05b92007-11-28 20:54:28 -0600626 prefetch(&RX_DESC(rx, n));
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500627
628 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600629 macrx = RX_DESC(rx, n);
Olof Johansson5c153322007-11-28 20:56:41 -0600630 prefetch(&RX_DESC(rx, n+4));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600631
Olof Johansson69c29d82007-10-02 16:24:51 -0500632 if ((macrx & XCT_MACRX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600633 (*chan->status & PAS_STATUS_ERROR))
Olof Johansson69c29d82007-10-02 16:24:51 -0500634 pasemi_mac_rx_error(mac, macrx);
635
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500636 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600637 break;
638
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600639 info = NULL;
640
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500641 BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600642
Olof Johansson72b05b92007-11-28 20:54:28 -0600643 eval = (RX_DESC(rx, n+1) & XCT_RXRES_8B_EVAL_M) >>
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500644 XCT_RXRES_8B_EVAL_S;
645 buf_index = eval-1;
646
Olof Johansson72b05b92007-11-28 20:54:28 -0600647 dma = (RX_DESC(rx, n+2) & XCT_PTR_ADDR_M);
648 info = &RX_DESC_INFO(rx, buf_index);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500649
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500650 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600651
Olof Johansson5c153322007-11-28 20:56:41 -0600652 prefetch_skb(skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600653
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500654 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600655
Olof Johansson5c153322007-11-28 20:56:41 -0600656 pci_unmap_single(pdev, dma, BUF_SIZE-LOCAL_SKB_ALIGN,
657 PCI_DMA_FROMDEVICE);
Olof Johansson32bee772007-11-06 22:21:38 -0600658
659 if (macrx & XCT_MACRX_CRC) {
660 /* CRC error flagged */
661 mac->netdev->stats.rx_errors++;
662 mac->netdev->stats.rx_crc_errors++;
Olof Johansson4352d822007-12-03 21:34:14 -0600663 /* No need to free skb, it'll be reused */
Olof Johansson32bee772007-11-06 22:21:38 -0600664 goto next;
665 }
666
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500667 if (len < 256) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500668 struct sk_buff *new_skb;
669
670 new_skb = netdev_alloc_skb(mac->netdev,
671 len + LOCAL_SKB_ALIGN);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500672 if (new_skb) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500673 skb_reserve(new_skb, LOCAL_SKB_ALIGN);
Olof Johansson73344862007-08-22 09:12:55 -0500674 memcpy(new_skb->data, skb->data, len);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500675 /* save the skb in buffer_info as good */
676 skb = new_skb;
677 }
678 /* else just continue with the old one */
679 } else
680 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600681
Olof Johanssonad5da102007-10-02 16:27:15 -0500682 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500683
Olof Johansson26fcfa92007-08-22 09:12:59 -0500684 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500685 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500686 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600687 XCT_MACRX_CSUM_S;
688 } else
689 skb->ip_summed = CHECKSUM_NONE;
690
Olof Johansson5c153322007-11-28 20:56:41 -0600691 packets++;
692 tot_bytes += len;
693
694 /* Don't include CRC */
695 skb_put(skb, len-4);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600696
Olof Johansson26fcfa92007-08-22 09:12:59 -0500697 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johansson28ae79f2007-11-28 20:57:27 -0600698 lro_receive_skb(&mac->lro_mgr, skb, (void *)macrx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600699
Olof Johansson32bee772007-11-06 22:21:38 -0600700next:
Olof Johansson72b05b92007-11-28 20:54:28 -0600701 RX_DESC(rx, n) = 0;
702 RX_DESC(rx, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500703
Olof Johanssonad5da102007-10-02 16:27:15 -0500704 /* Need to zero it out since hardware doesn't, since the
705 * replenish loop uses it to tell when it's done.
706 */
Olof Johansson72b05b92007-11-28 20:54:28 -0600707 RX_BUFF(rx, buf_index) = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500708
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500709 n += 4;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600710 }
711
Olof Johansson9a50beb2007-10-02 16:26:30 -0500712 if (n > RX_RING_SIZE) {
713 /* Errata 5971 workaround: L2 target of headers */
Olof Johansson34c20622007-11-28 20:56:32 -0600714 write_iob_reg(PAS_IOB_COM_PKTHDRCNT, 0);
Olof Johansson9a50beb2007-10-02 16:26:30 -0500715 n &= (RX_RING_SIZE-1);
716 }
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500717
Olof Johansson72b05b92007-11-28 20:54:28 -0600718 rx_ring(mac)->next_to_clean = n;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500719
Olof Johansson28ae79f2007-11-28 20:57:27 -0600720 lro_flush_all(&mac->lro_mgr);
721
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500722 /* Increase is in number of 16-byte entries, and since each descriptor
723 * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
724 * count*2.
725 */
Olof Johansson34c20622007-11-28 20:56:32 -0600726 write_dma_reg(PAS_DMA_RXCHAN_INCR(mac->rx->chan.chno), count << 1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500727
728 pasemi_mac_replenish_rx_ring(mac->netdev, count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600729
Olof Johansson5c153322007-11-28 20:56:41 -0600730 mac->netdev->stats.rx_bytes += tot_bytes;
731 mac->netdev->stats.rx_packets += packets;
732
Olof Johansson72b05b92007-11-28 20:54:28 -0600733 spin_unlock(&rx_ring(mac)->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600734
735 return count;
736}
737
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500738/* Can't make this too large or we blow the kernel stack limits */
739#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
740
Olof Johansson72b05b92007-11-28 20:54:28 -0600741static int pasemi_mac_clean_tx(struct pasemi_mac_txring *txring)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600742{
Olof Johansson34c20622007-11-28 20:56:32 -0600743 struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600744 struct pasemi_mac *mac = txring->mac;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500745 int i, j;
Olof Johanssonad5da102007-10-02 16:27:15 -0500746 unsigned int start, descr_count, buf_count, batch_limit;
747 unsigned int ring_limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500748 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500749 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500750 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
751 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600752
Olof Johansson02df6cf2007-08-22 09:13:03 -0500753 total_count = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500754 batch_limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500755restart:
Olof Johansson72b05b92007-11-28 20:54:28 -0600756 spin_lock_irqsave(&txring->lock, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600757
Olof Johansson72b05b92007-11-28 20:54:28 -0600758 start = txring->next_to_clean;
759 ring_limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500760
761 /* Compensate for when fill has wrapped but clean has not */
762 if (start > ring_limit)
763 ring_limit += TX_RING_SIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500764
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500765 buf_count = 0;
766 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600767
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500768 for (i = start;
Olof Johanssonad5da102007-10-02 16:27:15 -0500769 descr_count < batch_limit && i < ring_limit;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500770 i += buf_count) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600771 u64 mactx = TX_DESC(txring, i);
Olof Johanssonad5da102007-10-02 16:27:15 -0500772 struct sk_buff *skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500773
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500774 if ((mactx & XCT_MACTX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600775 (*chan->status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500776 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500777
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500778 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500779 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600780 break;
781
Olof Johansson72b05b92007-11-28 20:54:28 -0600782 skb = TX_DESC_INFO(txring, i+1).skb;
Olof Johanssonad5da102007-10-02 16:27:15 -0500783 skbs[descr_count] = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500784
Olof Johanssonad5da102007-10-02 16:27:15 -0500785 buf_count = 2 + skb_shinfo(skb)->nr_frags;
786 for (j = 0; j <= skb_shinfo(skb)->nr_frags; j++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600787 dmas[descr_count][j] = TX_DESC_INFO(txring, i+1+j).dma;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500788
Olof Johansson72b05b92007-11-28 20:54:28 -0600789 TX_DESC(txring, i) = 0;
790 TX_DESC(txring, i+1) = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500791
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500792 /* Since we always fill with an even number of entries, make
793 * sure we skip any unused one at the end as well.
794 */
795 if (buf_count & 1)
796 buf_count++;
797 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600798 }
Olof Johansson72b05b92007-11-28 20:54:28 -0600799 txring->next_to_clean = i & (TX_RING_SIZE-1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500800
Olof Johansson72b05b92007-11-28 20:54:28 -0600801 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500802 netif_wake_queue(mac->netdev);
803
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500804 for (i = 0; i < descr_count; i++)
805 pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500806
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500807 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500808
809 /* If the batch was full, try to clean more */
Olof Johanssonad5da102007-10-02 16:27:15 -0500810 if (descr_count == batch_limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500811 goto restart;
812
813 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600814}
815
816
817static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
818{
Olof Johansson5c153322007-11-28 20:56:41 -0600819 const struct pasemi_mac_rxring *rxring = data;
Olof Johansson34c20622007-11-28 20:56:32 -0600820 struct pasemi_mac *mac = rxring->mac;
821 struct net_device *dev = mac->netdev;
Olof Johansson5c153322007-11-28 20:56:41 -0600822 const struct pasemi_dmachan *chan = &rxring->chan;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600823 unsigned int reg;
824
Olof Johansson34c20622007-11-28 20:56:32 -0600825 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600826 return IRQ_NONE;
827
Olof Johansson6dfa7522007-05-08 00:47:32 -0500828 /* Don't reset packet count so it won't fire again but clear
829 * all others.
830 */
831
Olof Johansson6dfa7522007-05-08 00:47:32 -0500832 reg = 0;
Olof Johansson34c20622007-11-28 20:56:32 -0600833 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500834 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600835 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500836 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600837
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700838 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500839
Olof Johansson34c20622007-11-28 20:56:32 -0600840 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600841
842 return IRQ_HANDLED;
843}
844
Olof Johansson61cec3b2007-11-28 20:56:54 -0600845#define TX_CLEAN_INTERVAL HZ
846
847static void pasemi_mac_tx_timer(unsigned long data)
848{
849 struct pasemi_mac_txring *txring = (struct pasemi_mac_txring *)data;
850 struct pasemi_mac *mac = txring->mac;
851
852 pasemi_mac_clean_tx(txring);
853
854 mod_timer(&txring->clean_timer, jiffies + TX_CLEAN_INTERVAL);
855
856 pasemi_mac_restart_tx_intr(mac);
857}
858
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600859static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
860{
Olof Johansson72b05b92007-11-28 20:54:28 -0600861 struct pasemi_mac_txring *txring = data;
Olof Johansson5c153322007-11-28 20:56:41 -0600862 const struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson61cec3b2007-11-28 20:56:54 -0600863 struct pasemi_mac *mac = txring->mac;
864 unsigned int reg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600865
Olof Johansson34c20622007-11-28 20:56:32 -0600866 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600867 return IRQ_NONE;
868
Olof Johansson61cec3b2007-11-28 20:56:54 -0600869 reg = 0;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500870
Olof Johansson34c20622007-11-28 20:56:32 -0600871 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500872 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600873 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500874 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600875
Olof Johansson61cec3b2007-11-28 20:56:54 -0600876 mod_timer(&txring->clean_timer, jiffies + (TX_CLEAN_INTERVAL)*2);
877
878 netif_rx_schedule(mac->netdev, &mac->napi);
879
880 if (reg)
881 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600882
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600883 return IRQ_HANDLED;
884}
885
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500886static void pasemi_adjust_link(struct net_device *dev)
887{
888 struct pasemi_mac *mac = netdev_priv(dev);
889 int msg;
890 unsigned int flags;
891 unsigned int new_flags;
892
893 if (!mac->phydev->link) {
894 /* If no link, MAC speed settings don't matter. Just report
895 * link down and return.
896 */
897 if (mac->link && netif_msg_link(mac))
898 printk(KERN_INFO "%s: Link is down.\n", dev->name);
899
900 netif_carrier_off(dev);
901 mac->link = 0;
902
903 return;
904 } else
905 netif_carrier_on(dev);
906
Olof Johanssona85b9422007-09-15 13:40:59 -0700907 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500908 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
909 PAS_MAC_CFG_PCFG_TSR_M);
910
911 if (!mac->phydev->duplex)
912 new_flags |= PAS_MAC_CFG_PCFG_HD;
913
914 switch (mac->phydev->speed) {
915 case 1000:
916 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
917 PAS_MAC_CFG_PCFG_TSR_1G;
918 break;
919 case 100:
920 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
921 PAS_MAC_CFG_PCFG_TSR_100M;
922 break;
923 case 10:
924 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
925 PAS_MAC_CFG_PCFG_TSR_10M;
926 break;
927 default:
928 printk("Unsupported speed %d\n", mac->phydev->speed);
929 }
930
931 /* Print on link or speed/duplex change */
932 msg = mac->link != mac->phydev->link || flags != new_flags;
933
934 mac->duplex = mac->phydev->duplex;
935 mac->speed = mac->phydev->speed;
936 mac->link = mac->phydev->link;
937
938 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700939 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500940
941 if (msg && netif_msg_link(mac))
942 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
943 dev->name, mac->speed, mac->duplex ? "full" : "half");
944}
945
946static int pasemi_mac_phy_init(struct net_device *dev)
947{
948 struct pasemi_mac *mac = netdev_priv(dev);
949 struct device_node *dn, *phy_dn;
950 struct phy_device *phydev;
951 unsigned int phy_id;
952 const phandle *ph;
953 const unsigned int *prop;
954 struct resource r;
955 int ret;
956
957 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700958 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500959 if (!ph)
960 return -ENODEV;
961 phy_dn = of_find_node_by_phandle(*ph);
962
Linus Torvalds90287802007-05-08 11:57:17 -0700963 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500964 ret = of_address_to_resource(phy_dn->parent, 0, &r);
965 if (ret)
966 goto err;
967
968 phy_id = *prop;
969 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
970
971 of_node_put(phy_dn);
972
973 mac->link = 0;
974 mac->speed = 0;
975 mac->duplex = -1;
976
977 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
978
979 if (IS_ERR(phydev)) {
980 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
981 return PTR_ERR(phydev);
982 }
983
984 mac->phydev = phydev;
985
986 return 0;
987
988err:
989 of_node_put(phy_dn);
990 return -ENODEV;
991}
992
993
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600994static int pasemi_mac_open(struct net_device *dev)
995{
996 struct pasemi_mac *mac = netdev_priv(dev);
997 unsigned int flags;
998 int ret;
999
1000 /* enable rx section */
Olof Johansson34c20622007-11-28 20:56:32 -06001001 write_dma_reg(PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001002
1003 /* enable tx section */
Olof Johansson34c20622007-11-28 20:56:32 -06001004 write_dma_reg(PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001005
1006 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
1007 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
1008 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
1009
Olof Johanssona85b9422007-09-15 13:40:59 -07001010 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001011
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001012 ret = pasemi_mac_setup_rx_resources(dev);
1013 if (ret)
1014 goto out_rx_resources;
1015
Olof Johansson34c20622007-11-28 20:56:32 -06001016 mac->tx = pasemi_mac_setup_tx_resources(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001017
1018 if (!mac->tx)
1019 goto out_tx_ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001020
Olof Johansson906674a2007-11-28 20:57:09 -06001021 /* 0x3ff with 33MHz clock is about 31us */
1022 write_iob_reg(PAS_IOB_DMA_COM_TIMEOUTCFG,
1023 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0x3ff));
1024
Olof Johansson34c20622007-11-28 20:56:32 -06001025 write_iob_reg(PAS_IOB_DMA_RXCH_CFG(mac->rx->chan.chno),
Olof Johansson28ae79f2007-11-28 20:57:27 -06001026 PAS_IOB_DMA_RXCH_CFG_CNTTH(256));
Olof Johansson34c20622007-11-28 20:56:32 -06001027
1028 write_iob_reg(PAS_IOB_DMA_TXCH_CFG(mac->tx->chan.chno),
Olof Johansson61cec3b2007-11-28 20:56:54 -06001029 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johansson34c20622007-11-28 20:56:32 -06001030
Olof Johanssona85b9422007-09-15 13:40:59 -07001031 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
Olof Johansson34c20622007-11-28 20:56:32 -06001032 PAS_MAC_IPC_CHNL_DCHNO(mac->rx->chan.chno) |
1033 PAS_MAC_IPC_CHNL_BCH(mac->rx->chan.chno));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001034
1035 /* enable rx if */
Olof Johansson34c20622007-11-28 20:56:32 -06001036 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1037 PAS_DMA_RXINT_RCMDSTA_EN |
1038 PAS_DMA_RXINT_RCMDSTA_DROPS_M |
1039 PAS_DMA_RXINT_RCMDSTA_BP |
1040 PAS_DMA_RXINT_RCMDSTA_OO |
1041 PAS_DMA_RXINT_RCMDSTA_BT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001042
1043 /* enable rx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001044 pasemi_dma_start_chan(&rx_ring(mac)->chan, PAS_DMA_RXCHAN_CCMDSTA_DU |
1045 PAS_DMA_RXCHAN_CCMDSTA_OD |
1046 PAS_DMA_RXCHAN_CCMDSTA_FD |
1047 PAS_DMA_RXCHAN_CCMDSTA_DT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001048
1049 /* enable tx channel */
Olof Johansson34c20622007-11-28 20:56:32 -06001050 pasemi_dma_start_chan(&tx_ring(mac)->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
1051 PAS_DMA_TXCHAN_TCMDSTA_DB |
1052 PAS_DMA_TXCHAN_TCMDSTA_DE |
1053 PAS_DMA_TXCHAN_TCMDSTA_DA);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001054
Olof Johansson928773c2007-09-26 16:25:06 -05001055 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001056
Olof Johansson34c20622007-11-28 20:56:32 -06001057 write_dma_reg(PAS_DMA_RXCHAN_INCR(rx_ring(mac)->chan.chno),
1058 RX_RING_SIZE>>1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -05001059
Olof Johansson72b05b92007-11-28 20:54:28 -06001060 /* Clear out any residual packet count state from firmware */
1061 pasemi_mac_restart_rx_intr(mac);
1062 pasemi_mac_restart_tx_intr(mac);
1063
Olof Johansson36033762007-09-26 16:24:42 -05001064 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
1065 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
1066
1067 if (mac->type == MAC_TYPE_GMAC)
1068 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
1069 else
1070 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
1071
1072 /* Enable interface in MAC */
1073 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
1074
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001075 ret = pasemi_mac_phy_init(dev);
1076 /* Some configs don't have PHYs (XAUI etc), so don't complain about
1077 * failed init due to -ENODEV.
1078 */
1079 if (ret && ret != -ENODEV)
1080 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
1081
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001082 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001083 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001084
Olof Johansson72b05b92007-11-28 20:54:28 -06001085 snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx",
1086 dev->name);
Olof Johansson771f7402007-05-08 00:47:21 -05001087
Olof Johansson34c20622007-11-28 20:56:32 -06001088 ret = request_irq(mac->tx->chan.irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johansson72b05b92007-11-28 20:54:28 -06001089 mac->tx_irq_name, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001090 if (ret) {
1091 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001092 mac->tx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001093 goto out_tx_int;
1094 }
1095
Olof Johansson72b05b92007-11-28 20:54:28 -06001096 snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx",
1097 dev->name);
1098
Olof Johansson34c20622007-11-28 20:56:32 -06001099 ret = request_irq(mac->rx->chan.irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
1100 mac->rx_irq_name, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001101 if (ret) {
1102 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001103 mac->rx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001104 goto out_rx_int;
1105 }
1106
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001107 if (mac->phydev)
1108 phy_start(mac->phydev);
1109
Olof Johansson61cec3b2007-11-28 20:56:54 -06001110 init_timer(&mac->tx->clean_timer);
1111 mac->tx->clean_timer.function = pasemi_mac_tx_timer;
1112 mac->tx->clean_timer.data = (unsigned long)mac->tx;
1113 mac->tx->clean_timer.expires = jiffies+HZ;
1114 add_timer(&mac->tx->clean_timer);
1115
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001116 return 0;
1117
1118out_rx_int:
Olof Johansson34c20622007-11-28 20:56:32 -06001119 free_irq(mac->tx->chan.irq, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001120out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001121 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001122 netif_stop_queue(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001123out_tx_ring:
1124 if (mac->tx)
1125 pasemi_mac_free_tx_resources(mac);
1126 pasemi_mac_free_rx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001127out_rx_resources:
1128
1129 return ret;
1130}
1131
1132#define MAX_RETRIES 5000
1133
1134static int pasemi_mac_close(struct net_device *dev)
1135{
1136 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson9e81d332007-10-02 16:27:39 -05001137 unsigned int sta;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001138 int retries;
Olof Johansson34c20622007-11-28 20:56:32 -06001139 int rxch, txch;
1140
1141 rxch = rx_ring(mac)->chan.chno;
1142 txch = tx_ring(mac)->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001143
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001144 if (mac->phydev) {
1145 phy_stop(mac->phydev);
1146 phy_disconnect(mac->phydev);
1147 }
1148
Olof Johansson61cec3b2007-11-28 20:56:54 -06001149 del_timer_sync(&mac->tx->clean_timer);
1150
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001151 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001152 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001153
Olof Johansson34c20622007-11-28 20:56:32 -06001154 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson9e81d332007-10-02 16:27:39 -05001155 if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
1156 PAS_DMA_RXINT_RCMDSTA_OO |
1157 PAS_DMA_RXINT_RCMDSTA_BT))
1158 printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);
1159
Olof Johansson34c20622007-11-28 20:56:32 -06001160 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001161 if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
1162 PAS_DMA_RXCHAN_CCMDSTA_OD |
1163 PAS_DMA_RXCHAN_CCMDSTA_FD |
1164 PAS_DMA_RXCHAN_CCMDSTA_DT))
1165 printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);
1166
Olof Johansson34c20622007-11-28 20:56:32 -06001167 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
Olof Johansson72b05b92007-11-28 20:54:28 -06001168 if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB |
1169 PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA))
Olof Johansson9e81d332007-10-02 16:27:39 -05001170 printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);
1171
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001172 /* Clean out any pending buffers */
Olof Johansson72b05b92007-11-28 20:54:28 -06001173 pasemi_mac_clean_tx(tx_ring(mac));
1174 pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001175
1176 /* Disable interface */
Olof Johansson34c20622007-11-28 20:56:32 -06001177 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch),
Olof Johansson72b05b92007-11-28 20:54:28 -06001178 PAS_DMA_TXCHAN_TCMDSTA_ST);
Olof Johansson34c20622007-11-28 20:56:32 -06001179 write_dma_reg( PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
Olof Johansson72b05b92007-11-28 20:54:28 -06001180 PAS_DMA_RXINT_RCMDSTA_ST);
Olof Johansson34c20622007-11-28 20:56:32 -06001181 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch),
Olof Johansson72b05b92007-11-28 20:54:28 -06001182 PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001183
1184 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001185 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001186 if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001187 break;
1188 cond_resched();
1189 }
1190
Olof Johansson9e81d332007-10-02 16:27:39 -05001191 if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johansson34c20622007-11-28 20:56:32 -06001192 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001193
1194 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001195 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001196 if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001197 break;
1198 cond_resched();
1199 }
1200
Olof Johansson9e81d332007-10-02 16:27:39 -05001201 if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001202 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001203
1204 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001205 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson9e81d332007-10-02 16:27:39 -05001206 if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001207 break;
1208 cond_resched();
1209 }
1210
Olof Johansson9e81d332007-10-02 16:27:39 -05001211 if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001212 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001213
1214 /* Then, disable the channel. This must be done separately from
1215 * stopping, since you can't disable when active.
1216 */
1217
Olof Johansson34c20622007-11-28 20:56:32 -06001218 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch), 0);
1219 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch), 0);
1220 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001221
Olof Johansson34c20622007-11-28 20:56:32 -06001222 free_irq(mac->tx->chan.irq, mac->tx);
1223 free_irq(mac->rx->chan.irq, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001224
1225 /* Free resources */
Olof Johansson72b05b92007-11-28 20:54:28 -06001226 pasemi_mac_free_rx_resources(mac);
1227 pasemi_mac_free_tx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001228
1229 return 0;
1230}
1231
1232static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1233{
1234 struct pasemi_mac *mac = netdev_priv(dev);
1235 struct pasemi_mac_txring *txring;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001236 u64 dflags, mactx;
1237 dma_addr_t map[MAX_SKB_FRAGS+1];
1238 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001239 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001240 int i, nfrags;
Olof Johansson5c153322007-11-28 20:56:41 -06001241 int fill;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001242
Olof Johanssondbd62af2007-11-06 22:20:39 -06001243 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001244
1245 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001246 const unsigned char *nh = skb_network_header(skb);
1247
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001248 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001249 case IPPROTO_TCP:
1250 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001251 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001252 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001253 break;
1254 case IPPROTO_UDP:
1255 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001256 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001257 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001258 break;
1259 }
1260 }
1261
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001262 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001263
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001264 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1265 PCI_DMA_TODEVICE);
1266 map_size[0] = skb_headlen(skb);
1267 if (dma_mapping_error(map[0]))
1268 goto out_err_nolock;
1269
1270 for (i = 0; i < nfrags; i++) {
1271 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1272
1273 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1274 frag->page_offset, frag->size,
1275 PCI_DMA_TODEVICE);
1276 map_size[i+1] = frag->size;
1277 if (dma_mapping_error(map[i+1])) {
1278 nfrags = i;
1279 goto out_err_nolock;
1280 }
1281 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001282
Olof Johansson26fcfa92007-08-22 09:12:59 -05001283 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001284
Olof Johansson72b05b92007-11-28 20:54:28 -06001285 txring = tx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001286
1287 spin_lock_irqsave(&txring->lock, flags);
1288
Olof Johansson5c153322007-11-28 20:56:41 -06001289 fill = txring->next_to_fill;
1290
Olof Johanssonad5da102007-10-02 16:27:15 -05001291 /* Avoid stepping on the same cache line that the DMA controller
1292 * is currently about to send, so leave at least 8 words available.
1293 * Total free space needed is mactx + fragments + 8
1294 */
1295 if (RING_AVAIL(txring) < nfrags + 10) {
1296 /* no room -- stop the queue and wait for tx intr */
1297 netif_stop_queue(dev);
1298 goto out_err;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001299 }
1300
Olof Johansson5c153322007-11-28 20:56:41 -06001301 TX_DESC(txring, fill) = mactx;
1302 fill++;
1303 TX_DESC_INFO(txring, fill).skb = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001304 for (i = 0; i <= nfrags; i++) {
Olof Johansson5c153322007-11-28 20:56:41 -06001305 TX_DESC(txring, fill+i) =
Olof Johansson72b05b92007-11-28 20:54:28 -06001306 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
Olof Johansson5c153322007-11-28 20:56:41 -06001307 TX_DESC_INFO(txring, fill+i).dma = map[i];
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001308 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001309
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001310 /* We have to add an even number of 8-byte entries to the ring
1311 * even if the last one is unused. That means always an odd number
1312 * of pointers + one mactx descriptor.
1313 */
1314 if (nfrags & 1)
1315 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001316
Olof Johansson5c153322007-11-28 20:56:41 -06001317 txring->next_to_fill = (fill + nfrags + 1) & (TX_RING_SIZE-1);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001318
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001319 dev->stats.tx_packets++;
1320 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001321
1322 spin_unlock_irqrestore(&txring->lock, flags);
1323
Olof Johansson34c20622007-11-28 20:56:32 -06001324 write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001325
1326 return NETDEV_TX_OK;
1327
1328out_err:
1329 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001330out_err_nolock:
1331 while (nfrags--)
1332 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1333 PCI_DMA_TODEVICE);
1334
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001335 return NETDEV_TX_BUSY;
1336}
1337
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001338static void pasemi_mac_set_rx_mode(struct net_device *dev)
1339{
Olof Johansson5c153322007-11-28 20:56:41 -06001340 const struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001341 unsigned int flags;
1342
Olof Johanssona85b9422007-09-15 13:40:59 -07001343 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001344
1345 /* Set promiscuous */
1346 if (dev->flags & IFF_PROMISC)
1347 flags |= PAS_MAC_CFG_PCFG_PR;
1348 else
1349 flags &= ~PAS_MAC_CFG_PCFG_PR;
1350
Olof Johanssona85b9422007-09-15 13:40:59 -07001351 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001352}
1353
1354
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001355static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001356{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001357 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1358 struct net_device *dev = mac->netdev;
1359 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001360
Olof Johansson72b05b92007-11-28 20:54:28 -06001361 pasemi_mac_clean_tx(tx_ring(mac));
1362 pkts = pasemi_mac_clean_rx(rx_ring(mac), budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001363 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001364 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001365 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001366
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001367 pasemi_mac_restart_rx_intr(mac);
Olof Johansson61cec3b2007-11-28 20:56:54 -06001368 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001369 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001370 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001371}
1372
1373static int __devinit
1374pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1375{
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001376 struct net_device *dev;
1377 struct pasemi_mac *mac;
1378 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001379 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001380
1381 err = pci_enable_device(pdev);
1382 if (err)
1383 return err;
1384
1385 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1386 if (dev == NULL) {
1387 dev_err(&pdev->dev,
1388 "pasemi_mac: Could not allocate ethernet device.\n");
1389 err = -ENOMEM;
1390 goto out_disable_device;
1391 }
1392
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001393 pci_set_drvdata(pdev, dev);
1394 SET_NETDEV_DEV(dev, &pdev->dev);
1395
1396 mac = netdev_priv(dev);
1397
1398 mac->pdev = pdev;
1399 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001400
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001401 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1402
Olof Johansson5c153322007-11-28 20:56:41 -06001403 dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG |
1404 NETIF_F_HIGHDMA;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001405
Olof Johansson28ae79f2007-11-28 20:57:27 -06001406 mac->lro_mgr.max_aggr = LRO_MAX_AGGR;
1407 mac->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
1408 mac->lro_mgr.lro_arr = mac->lro_desc;
1409 mac->lro_mgr.get_skb_header = get_skb_hdr;
1410 mac->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
1411 mac->lro_mgr.dev = mac->netdev;
1412 mac->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1413 mac->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1414
1415
Olof Johansson34c20622007-11-28 20:56:32 -06001416 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1417 if (!mac->dma_pdev) {
1418 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1419 err = -ENODEV;
1420 goto out;
1421 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001422
Olof Johansson34c20622007-11-28 20:56:32 -06001423 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1424 if (!mac->iob_pdev) {
1425 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1426 err = -ENODEV;
1427 goto out;
1428 }
1429
1430 /* get mac addr from device tree */
1431 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1432 err = -ENODEV;
1433 goto out;
1434 }
1435 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1436
1437 mac->dma_if = mac_to_intf(mac);
1438 if (mac->dma_if < 0) {
1439 dev_err(&mac->pdev->dev, "Can't map DMA interface\n");
1440 err = -ENODEV;
1441 goto out;
1442 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001443
1444 switch (pdev->device) {
1445 case 0xa005:
1446 mac->type = MAC_TYPE_GMAC;
1447 break;
1448 case 0xa006:
1449 mac->type = MAC_TYPE_XAUI;
1450 break;
1451 default:
1452 err = -ENODEV;
1453 goto out;
1454 }
1455
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001456 dev->open = pasemi_mac_open;
1457 dev->stop = pasemi_mac_close;
1458 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001459 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001460
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001461 if (err)
1462 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001463
Olof Johanssonceb51362007-05-08 00:47:49 -05001464 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1465
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001466 /* Enable most messages by default */
1467 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1468
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001469 err = register_netdev(dev);
1470
1471 if (err) {
1472 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1473 err);
1474 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001475 } else if netif_msg_probe(mac)
Olof Johansson72b05b92007-11-28 20:54:28 -06001476 printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001477 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
Olof Johansson72b05b92007-11-28 20:54:28 -06001478 mac->dma_if, print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001479
1480 return err;
1481
1482out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001483 if (mac->iob_pdev)
1484 pci_dev_put(mac->iob_pdev);
1485 if (mac->dma_pdev)
1486 pci_dev_put(mac->dma_pdev);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001487
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001488 free_netdev(dev);
1489out_disable_device:
1490 pci_disable_device(pdev);
1491 return err;
1492
1493}
1494
1495static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1496{
1497 struct net_device *netdev = pci_get_drvdata(pdev);
1498 struct pasemi_mac *mac;
1499
1500 if (!netdev)
1501 return;
1502
1503 mac = netdev_priv(netdev);
1504
1505 unregister_netdev(netdev);
1506
1507 pci_disable_device(pdev);
1508 pci_dev_put(mac->dma_pdev);
1509 pci_dev_put(mac->iob_pdev);
1510
Olof Johansson34c20622007-11-28 20:56:32 -06001511 pasemi_dma_free_chan(&mac->tx->chan);
1512 pasemi_dma_free_chan(&mac->rx->chan);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001513
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001514 pci_set_drvdata(pdev, NULL);
1515 free_netdev(netdev);
1516}
1517
1518static struct pci_device_id pasemi_mac_pci_tbl[] = {
1519 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1520 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001521 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001522};
1523
1524MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1525
1526static struct pci_driver pasemi_mac_driver = {
1527 .name = "pasemi_mac",
1528 .id_table = pasemi_mac_pci_tbl,
1529 .probe = pasemi_mac_probe,
1530 .remove = __devexit_p(pasemi_mac_remove),
1531};
1532
1533static void __exit pasemi_mac_cleanup_module(void)
1534{
1535 pci_unregister_driver(&pasemi_mac_driver);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001536}
1537
1538int pasemi_mac_init_module(void)
1539{
Olof Johansson34c20622007-11-28 20:56:32 -06001540 int err;
1541
1542 err = pasemi_dma_init();
1543 if (err)
1544 return err;
1545
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001546 return pci_register_driver(&pasemi_mac_driver);
1547}
1548
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001549module_init(pasemi_mac_init_module);
1550module_exit(pasemi_mac_cleanup_module);