blob: b3994f5d2d2beae18084512c96427d3c3001b935 [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>
35
Olof Johansson771f7402007-05-08 00:47:21 -050036#include <asm/irq.h>
37
Olof Johanssonf5cd7872007-01-31 21:43:54 -060038#include "pasemi_mac.h"
39
Olof Johansson8dc121a2007-10-02 16:26:53 -050040/* We have our own align, since ppc64 in general has it at 0 because
41 * of design flaws in some of the server bridge chips. However, for
42 * PWRficient doing the unaligned copies is more expensive than doing
43 * unaligned DMA, so make sure the data is aligned instead.
44 */
45#define LOCAL_SKB_ALIGN 2
Olof Johanssonf5cd7872007-01-31 21:43:54 -060046
47/* TODO list
48 *
49 * - Get rid of pci_{read,write}_config(), map registers with ioremap
50 * for performance
51 * - PHY support
52 * - Multicast support
53 * - Large MTU support
54 * - Other performance improvements
55 */
56
57
58/* Must be a power of two */
59#define RX_RING_SIZE 512
60#define TX_RING_SIZE 512
61
Olof Johanssonceb51362007-05-08 00:47:49 -050062#define DEFAULT_MSG_ENABLE \
63 (NETIF_MSG_DRV | \
64 NETIF_MSG_PROBE | \
65 NETIF_MSG_LINK | \
66 NETIF_MSG_TIMER | \
67 NETIF_MSG_IFDOWN | \
68 NETIF_MSG_IFUP | \
69 NETIF_MSG_RX_ERR | \
70 NETIF_MSG_TX_ERR)
71
Olof Johanssonfc9e4d22007-10-02 16:25:53 -050072#define TX_RING(mac, num) ((mac)->tx->ring[(num) & (TX_RING_SIZE-1)])
73#define TX_RING_INFO(mac, num) ((mac)->tx->ring_info[(num) & (TX_RING_SIZE-1)])
74#define RX_RING(mac, num) ((mac)->rx->ring[(num) & (RX_RING_SIZE-1)])
75#define RX_RING_INFO(mac, num) ((mac)->rx->ring_info[(num) & (RX_RING_SIZE-1)])
Olof Johanssonf5cd7872007-01-31 21:43:54 -060076#define RX_BUFF(mac, num) ((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)])
77
Olof Johansson021fa222007-08-22 09:13:11 -050078#define RING_USED(ring) (((ring)->next_to_fill - (ring)->next_to_clean) \
79 & ((ring)->size - 1))
80#define RING_AVAIL(ring) ((ring->size) - RING_USED(ring))
81
Olof Johanssonf5cd7872007-01-31 21:43:54 -060082#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
83
Olof Johanssonceb51362007-05-08 00:47:49 -050084MODULE_LICENSE("GPL");
85MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
86MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
87
88static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
89module_param(debug, int, 0);
90MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060091
92static struct pasdma_status *dma_status;
93
Olof Johanssona85b9422007-09-15 13:40:59 -070094static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg,
95 unsigned int val)
96{
Olof Johanssonb6e05a12007-09-15 13:44:07 -070097 out_le32(mac->iob_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -070098}
99
100static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
101{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700102 return in_le32(mac->regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700103}
104
105static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
106 unsigned int val)
107{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700108 out_le32(mac->regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700109}
110
111static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg)
112{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700113 return in_le32(mac->dma_regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700114}
115
116static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg,
117 unsigned int val)
118{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700119 out_le32(mac->dma_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700120}
121
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600122static int pasemi_get_mac_addr(struct pasemi_mac *mac)
123{
124 struct pci_dev *pdev = mac->pdev;
125 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500126 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600127 const u8 *maddr;
128 u8 addr[6];
129
130 if (!dn) {
131 dev_dbg(&pdev->dev,
132 "No device node for mac, not configuring\n");
133 return -ENOENT;
134 }
135
olof@lixom.net1af7f052007-05-12 14:57:46 -0500136 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500137
olof@lixom.net1af7f052007-05-12 14:57:46 -0500138 if (maddr && len == 6) {
139 memcpy(mac->mac_addr, maddr, 6);
140 return 0;
141 }
142
143 /* Some old versions of firmware mistakenly uses mac-address
144 * (and as a string) instead of a byte array in local-mac-address.
145 */
146
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500147 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700148 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500149
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600150 if (maddr == NULL) {
151 dev_warn(&pdev->dev,
152 "no mac address in device tree, not configuring\n");
153 return -ENOENT;
154 }
155
olof@lixom.net1af7f052007-05-12 14:57:46 -0500156
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600157 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
158 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
159 dev_warn(&pdev->dev,
160 "can't parse mac address, not configuring\n");
161 return -EINVAL;
162 }
163
olof@lixom.net1af7f052007-05-12 14:57:46 -0500164 memcpy(mac->mac_addr, addr, 6);
165
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600166 return 0;
167}
168
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500169static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
170 struct sk_buff *skb,
171 dma_addr_t *dmas)
172{
173 int f;
174 int nfrags = skb_shinfo(skb)->nr_frags;
175
176 pci_unmap_single(mac->dma_pdev, dmas[0], skb_headlen(skb),
177 PCI_DMA_TODEVICE);
178
179 for (f = 0; f < nfrags; f++) {
180 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
181
182 pci_unmap_page(mac->dma_pdev, dmas[f+1], frag->size,
183 PCI_DMA_TODEVICE);
184 }
185 dev_kfree_skb_irq(skb);
186
187 /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
188 * aligned up to a power of 2
189 */
190 return (nfrags + 3) & ~1;
191}
192
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600193static int pasemi_mac_setup_rx_resources(struct net_device *dev)
194{
195 struct pasemi_mac_rxring *ring;
196 struct pasemi_mac *mac = netdev_priv(dev);
197 int chan_id = mac->dma_rxch;
198
199 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
200
201 if (!ring)
202 goto out_ring;
203
204 spin_lock_init(&ring->lock);
205
Olof Johansson021fa222007-08-22 09:13:11 -0500206 ring->size = RX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500207 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600208 RX_RING_SIZE, GFP_KERNEL);
209
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500210 if (!ring->ring_info)
211 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600212
213 /* Allocate descriptors */
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500214 ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
215 RX_RING_SIZE * sizeof(u64),
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600216 &ring->dma, GFP_KERNEL);
217
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500218 if (!ring->ring)
219 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600220
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500221 memset(ring->ring, 0, RX_RING_SIZE * sizeof(u64));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600222
223 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
224 RX_RING_SIZE * sizeof(u64),
225 &ring->buf_dma, GFP_KERNEL);
226 if (!ring->buffers)
227 goto out_buffers;
228
229 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
230
Olof Johanssona85b9422007-09-15 13:40:59 -0700231 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEL(chan_id), PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600232
Olof Johanssona85b9422007-09-15 13:40:59 -0700233 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEU(chan_id),
234 PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500235 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600236
Olof Johanssona85b9422007-09-15 13:40:59 -0700237 write_dma_reg(mac, PAS_DMA_RXCHAN_CFG(chan_id),
Olof Johanssonc0efd522007-08-22 09:12:52 -0500238 PAS_DMA_RXCHAN_CFG_HBU(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600239
Olof Johanssona85b9422007-09-15 13:40:59 -0700240 write_dma_reg(mac, PAS_DMA_RXINT_BASEL(mac->dma_if),
241 PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600242
Olof Johanssona85b9422007-09-15 13:40:59 -0700243 write_dma_reg(mac, PAS_DMA_RXINT_BASEU(mac->dma_if),
244 PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) |
245 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600246
Olof Johanssonc0efd522007-08-22 09:12:52 -0500247 write_dma_reg(mac, PAS_DMA_RXINT_CFG(mac->dma_if),
Olof Johansson9a50beb2007-10-02 16:26:30 -0500248 PAS_DMA_RXINT_CFG_DHL(3) |
249 PAS_DMA_RXINT_CFG_L2 |
250 PAS_DMA_RXINT_CFG_LW);
Olof Johanssonc0efd522007-08-22 09:12:52 -0500251
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600252 ring->next_to_fill = 0;
253 ring->next_to_clean = 0;
254
255 snprintf(ring->irq_name, sizeof(ring->irq_name),
256 "%s rx", dev->name);
257 mac->rx = ring;
258
259 return 0;
260
261out_buffers:
262 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500263 RX_RING_SIZE * sizeof(u64),
264 mac->rx->ring, mac->rx->dma);
265out_ring_desc:
266 kfree(ring->ring_info);
267out_ring_info:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600268 kfree(ring);
269out_ring:
270 return -ENOMEM;
271}
272
273
274static int pasemi_mac_setup_tx_resources(struct net_device *dev)
275{
276 struct pasemi_mac *mac = netdev_priv(dev);
277 u32 val;
278 int chan_id = mac->dma_txch;
279 struct pasemi_mac_txring *ring;
280
281 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
282 if (!ring)
283 goto out_ring;
284
285 spin_lock_init(&ring->lock);
286
Olof Johansson021fa222007-08-22 09:13:11 -0500287 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500288 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600289 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500290 if (!ring->ring_info)
291 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600292
293 /* Allocate descriptors */
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500294 ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
295 TX_RING_SIZE * sizeof(u64),
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600296 &ring->dma, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500297 if (!ring->ring)
298 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600299
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500300 memset(ring->ring, 0, TX_RING_SIZE * sizeof(u64));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600301
Olof Johanssona85b9422007-09-15 13:40:59 -0700302 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id),
303 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600304 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500305 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600306
Olof Johanssona85b9422007-09-15 13:40:59 -0700307 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600308
Olof Johanssona85b9422007-09-15 13:40:59 -0700309 write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(chan_id),
310 PAS_DMA_TXCHAN_CFG_TY_IFACE |
311 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
312 PAS_DMA_TXCHAN_CFG_UP |
313 PAS_DMA_TXCHAN_CFG_WT(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600314
Olof Johansson021fa222007-08-22 09:13:11 -0500315 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600316 ring->next_to_clean = 0;
317
318 snprintf(ring->irq_name, sizeof(ring->irq_name),
319 "%s tx", dev->name);
320 mac->tx = ring;
321
322 return 0;
323
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500324out_ring_desc:
325 kfree(ring->ring_info);
326out_ring_info:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600327 kfree(ring);
328out_ring:
329 return -ENOMEM;
330}
331
332static void pasemi_mac_free_tx_resources(struct net_device *dev)
333{
334 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500335 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600336 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500337 dma_addr_t dmas[MAX_SKB_FRAGS+1];
338 int freed;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600339
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500340 for (i = 0; i < TX_RING_SIZE; i += freed) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500341 info = &TX_RING_INFO(mac, i+1);
342 if (info->dma && info->skb) {
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500343 for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
344 dmas[j] = TX_RING_INFO(mac, i+1+j).dma;
345 freed = pasemi_mac_unmap_tx_skb(mac, info->skb, dmas);
346 } else
347 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600348 }
349
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500350 for (i = 0; i < TX_RING_SIZE; i++)
351 TX_RING(mac, i) = 0;
352
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600353 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500354 TX_RING_SIZE * sizeof(u64),
355 mac->tx->ring, mac->tx->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600356
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500357 kfree(mac->tx->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600358 kfree(mac->tx);
359 mac->tx = NULL;
360}
361
362static void pasemi_mac_free_rx_resources(struct net_device *dev)
363{
364 struct pasemi_mac *mac = netdev_priv(dev);
365 unsigned int i;
366 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600367
368 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500369 info = &RX_RING_INFO(mac, i);
370 if (info->skb && info->dma) {
371 pci_unmap_single(mac->dma_pdev,
372 info->dma,
373 info->skb->len,
374 PCI_DMA_FROMDEVICE);
375 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600376 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500377 info->dma = 0;
378 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600379 }
380
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500381 for (i = 0; i < RX_RING_SIZE; i++)
382 RX_RING(mac, i) = 0;
383
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600384 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500385 RX_RING_SIZE * sizeof(u64),
386 mac->rx->ring, mac->rx->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600387
388 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
389 mac->rx->buffers, mac->rx->buf_dma);
390
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500391 kfree(mac->rx->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600392 kfree(mac->rx);
393 mac->rx = NULL;
394}
395
Olof Johansson928773c2007-09-26 16:25:06 -0500396static void pasemi_mac_replenish_rx_ring(struct net_device *dev, int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600397{
398 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600399 int start = mac->rx->next_to_fill;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500400 unsigned int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600401
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500402 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600403 return;
404
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500405 fill = start;
Olof Johansson928773c2007-09-26 16:25:06 -0500406 for (count = 0; count < limit; count++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500407 struct pasemi_mac_buffer *info = &RX_RING_INFO(mac, fill);
408 u64 *buff = &RX_BUFF(mac, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600409 struct sk_buff *skb;
410 dma_addr_t dma;
411
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500412 /* Entry in use? */
413 WARN_ON(*buff);
414
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500415 /* skb might still be in there for recycle on short receives */
416 if (info->skb)
417 skb = info->skb;
Olof Johansson8dc121a2007-10-02 16:26:53 -0500418 else {
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500419 skb = dev_alloc_skb(BUF_SIZE);
Olof Johansson8dc121a2007-10-02 16:26:53 -0500420 skb_reserve(skb, LOCAL_SKB_ALIGN);
421 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600422
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500423 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600424 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600425
Olof Johansson8dc121a2007-10-02 16:26:53 -0500426 dma = pci_map_single(mac->dma_pdev, skb->data,
427 BUF_SIZE - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600428 PCI_DMA_FROMDEVICE);
429
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500430 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600431 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600432 break;
433 }
434
435 info->skb = skb;
436 info->dma = dma;
437 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500438 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600439 }
440
441 wmb();
442
Olof Johansson928773c2007-09-26 16:25:06 -0500443 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), count);
444 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600445
Olof Johansson928773c2007-09-26 16:25:06 -0500446 mac->rx->next_to_fill += count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600447}
448
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500449static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
450{
Olof Johansson52a94352007-05-12 18:01:09 -0500451 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500452 /* Re-enable packet count interrupts: finally
453 * ack the packet count interrupt we got in rx_intr.
454 */
455
Olof Johansson52a94352007-05-12 18:01:09 -0500456 pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500457
Olof Johansson52a94352007-05-12 18:01:09 -0500458 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500459
Olof Johanssona85b9422007-09-15 13:40:59 -0700460 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500461}
462
463static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
464{
Olof Johansson52a94352007-05-12 18:01:09 -0500465 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500466
467 /* Re-enable packet count interrupts */
Olof Johansson52a94352007-05-12 18:01:09 -0500468 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500469
Olof Johansson52a94352007-05-12 18:01:09 -0500470 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500471
Olof Johanssona85b9422007-09-15 13:40:59 -0700472 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500473}
474
475
Olof Johansson69c29d82007-10-02 16:24:51 -0500476static inline void pasemi_mac_rx_error(struct pasemi_mac *mac, u64 macrx)
477{
478 unsigned int rcmdsta, ccmdsta;
479
480 if (!netif_msg_rx_err(mac))
481 return;
482
483 rcmdsta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
484 ccmdsta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
485
486 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
487 macrx, *mac->rx_status);
488
489 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
490 rcmdsta, ccmdsta);
491}
492
493static inline void pasemi_mac_tx_error(struct pasemi_mac *mac, u64 mactx)
494{
495 unsigned int cmdsta;
496
497 if (!netif_msg_tx_err(mac))
498 return;
499
500 cmdsta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
501
502 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
503 "tx status 0x%016lx\n", mactx, *mac->tx_status);
504
505 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
506}
507
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600508static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
509{
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500510 unsigned int n;
511 int count;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500512 struct pasemi_mac_buffer *info;
513 struct sk_buff *skb;
514 unsigned int i, len;
515 u64 macrx;
516 dma_addr_t dma;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600517
518 spin_lock(&mac->rx->lock);
519
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500520 n = mac->rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600521
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500522 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600523
524 rmb();
525
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500526 macrx = RX_RING(mac, n);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600527
Olof Johansson69c29d82007-10-02 16:24:51 -0500528 if ((macrx & XCT_MACRX_E) ||
529 (*mac->rx_status & PAS_STATUS_ERROR))
530 pasemi_mac_rx_error(mac, macrx);
531
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500532 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600533 break;
534
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600535 info = NULL;
536
537 /* We have to scan for our skb since there's no way
538 * to back-map them from the descriptor, and if we
539 * have several receive channels then they might not
540 * show up in the same order as they were put on the
541 * interface ring.
542 */
543
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500544 dma = (RX_RING(mac, n+1) & XCT_PTR_ADDR_M);
545 for (i = mac->rx->next_to_fill;
546 i < (mac->rx->next_to_fill + RX_RING_SIZE);
547 i++) {
548 info = &RX_RING_INFO(mac, i);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600549 if (info->dma == dma)
550 break;
551 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500552
Olof Johansson26fcfa92007-08-22 09:12:59 -0500553 prefetchw(info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600554
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500555 skb = info->skb;
Olof Johansson26fcfa92007-08-22 09:12:59 -0500556 prefetchw(skb);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500557 info->dma = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600558
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500559 pci_unmap_single(mac->dma_pdev, dma, skb->len,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600560 PCI_DMA_FROMDEVICE);
561
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500562 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600563
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500564 if (len < 256) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500565 struct sk_buff *new_skb;
566
567 new_skb = netdev_alloc_skb(mac->netdev,
568 len + LOCAL_SKB_ALIGN);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500569 if (new_skb) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500570 skb_reserve(new_skb, LOCAL_SKB_ALIGN);
Olof Johansson73344862007-08-22 09:12:55 -0500571 memcpy(new_skb->data, skb->data, len);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500572 /* save the skb in buffer_info as good */
573 skb = new_skb;
574 }
575 /* else just continue with the old one */
576 } else
577 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600578
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500579 /* Need to zero it out since hardware doesn't, since the
580 * replenish loop uses it to tell when it's done.
581 */
582 RX_BUFF(mac, i) = 0;
583
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600584 skb_put(skb, len);
585
Olof Johansson26fcfa92007-08-22 09:12:59 -0500586 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500587 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500588 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600589 XCT_MACRX_CSUM_S;
590 } else
591 skb->ip_summed = CHECKSUM_NONE;
592
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700593 mac->netdev->stats.rx_bytes += len;
594 mac->netdev->stats.rx_packets++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600595
Olof Johansson26fcfa92007-08-22 09:12:59 -0500596 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600597 netif_receive_skb(skb);
598
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500599 RX_RING(mac, n) = 0;
600 RX_RING(mac, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500601
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500602 n += 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600603 }
604
Olof Johansson9a50beb2007-10-02 16:26:30 -0500605 if (n > RX_RING_SIZE) {
606 /* Errata 5971 workaround: L2 target of headers */
607 write_iob_reg(mac, PAS_IOB_COM_PKTHDRCNT, 0);
608 n &= (RX_RING_SIZE-1);
609 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500610 mac->rx->next_to_clean = n;
Olof Johansson928773c2007-09-26 16:25:06 -0500611 pasemi_mac_replenish_rx_ring(mac->netdev, limit-count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600612
613 spin_unlock(&mac->rx->lock);
614
615 return count;
616}
617
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500618/* Can't make this too large or we blow the kernel stack limits */
619#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
620
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600621static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
622{
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500623 int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600624 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500625 unsigned int start, descr_count, buf_count, limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500626 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500627 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500628 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
629 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600630
Olof Johansson02df6cf2007-08-22 09:13:03 -0500631 total_count = 0;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500632 limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500633restart:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600634 spin_lock_irqsave(&mac->tx->lock, flags);
635
636 start = mac->tx->next_to_clean;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500637
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500638 buf_count = 0;
639 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600640
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500641 for (i = start;
642 descr_count < limit && i < mac->tx->next_to_fill;
643 i += buf_count) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500644 u64 mactx = TX_RING(mac, i);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500645
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500646 if ((mactx & XCT_MACTX_E) ||
Olof Johansson69c29d82007-10-02 16:24:51 -0500647 (*mac->tx_status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500648 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500649
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500650 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500651 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600652 break;
653
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500654 info = &TX_RING_INFO(mac, i+1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500655 skbs[descr_count] = info->skb;
656
657 buf_count = 2 + skb_shinfo(info->skb)->nr_frags;
658 for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
659 dmas[descr_count][j] = TX_RING_INFO(mac, i+1+j).dma;
660
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600661
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600662 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500663 TX_RING(mac, i) = 0;
664 TX_RING(mac, i+1) = 0;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500665 TX_RING_INFO(mac, i+1).skb = 0;
666 TX_RING_INFO(mac, i+1).dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500667
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500668 /* Since we always fill with an even number of entries, make
669 * sure we skip any unused one at the end as well.
670 */
671 if (buf_count & 1)
672 buf_count++;
673 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600674 }
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500675 mac->tx->next_to_clean = i;
676
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600677 spin_unlock_irqrestore(&mac->tx->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500678 netif_wake_queue(mac->netdev);
679
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500680 for (i = 0; i < descr_count; i++)
681 pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500682
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500683 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500684
685 /* If the batch was full, try to clean more */
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500686 if (descr_count == limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500687 goto restart;
688
689 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600690}
691
692
693static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
694{
695 struct net_device *dev = data;
696 struct pasemi_mac *mac = netdev_priv(dev);
697 unsigned int reg;
698
Olof Johansson6dfa7522007-05-08 00:47:32 -0500699 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600700 return IRQ_NONE;
701
Olof Johansson6dfa7522007-05-08 00:47:32 -0500702 /* Don't reset packet count so it won't fire again but clear
703 * all others.
704 */
705
Olof Johansson6dfa7522007-05-08 00:47:32 -0500706 reg = 0;
707 if (*mac->rx_status & PAS_STATUS_SOFT)
708 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
709 if (*mac->rx_status & PAS_STATUS_ERROR)
710 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600711 if (*mac->rx_status & PAS_STATUS_TIMER)
712 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
713
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700714 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500715
Olof Johanssona85b9422007-09-15 13:40:59 -0700716 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600717
718 return IRQ_HANDLED;
719}
720
721static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
722{
723 struct net_device *dev = data;
724 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson52a94352007-05-12 18:01:09 -0500725 unsigned int reg, pcnt;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600726
Olof Johansson6dfa7522007-05-08 00:47:32 -0500727 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600728 return IRQ_NONE;
729
730 pasemi_mac_clean_tx(mac);
731
Olof Johansson52a94352007-05-12 18:01:09 -0500732 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
733
734 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500735
736 if (*mac->tx_status & PAS_STATUS_SOFT)
737 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
738 if (*mac->tx_status & PAS_STATUS_ERROR)
739 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600740
Olof Johanssona85b9422007-09-15 13:40:59 -0700741 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600742
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600743 return IRQ_HANDLED;
744}
745
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500746static void pasemi_adjust_link(struct net_device *dev)
747{
748 struct pasemi_mac *mac = netdev_priv(dev);
749 int msg;
750 unsigned int flags;
751 unsigned int new_flags;
752
753 if (!mac->phydev->link) {
754 /* If no link, MAC speed settings don't matter. Just report
755 * link down and return.
756 */
757 if (mac->link && netif_msg_link(mac))
758 printk(KERN_INFO "%s: Link is down.\n", dev->name);
759
760 netif_carrier_off(dev);
761 mac->link = 0;
762
763 return;
764 } else
765 netif_carrier_on(dev);
766
Olof Johanssona85b9422007-09-15 13:40:59 -0700767 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500768 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
769 PAS_MAC_CFG_PCFG_TSR_M);
770
771 if (!mac->phydev->duplex)
772 new_flags |= PAS_MAC_CFG_PCFG_HD;
773
774 switch (mac->phydev->speed) {
775 case 1000:
776 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
777 PAS_MAC_CFG_PCFG_TSR_1G;
778 break;
779 case 100:
780 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
781 PAS_MAC_CFG_PCFG_TSR_100M;
782 break;
783 case 10:
784 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
785 PAS_MAC_CFG_PCFG_TSR_10M;
786 break;
787 default:
788 printk("Unsupported speed %d\n", mac->phydev->speed);
789 }
790
791 /* Print on link or speed/duplex change */
792 msg = mac->link != mac->phydev->link || flags != new_flags;
793
794 mac->duplex = mac->phydev->duplex;
795 mac->speed = mac->phydev->speed;
796 mac->link = mac->phydev->link;
797
798 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700799 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500800
801 if (msg && netif_msg_link(mac))
802 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
803 dev->name, mac->speed, mac->duplex ? "full" : "half");
804}
805
806static int pasemi_mac_phy_init(struct net_device *dev)
807{
808 struct pasemi_mac *mac = netdev_priv(dev);
809 struct device_node *dn, *phy_dn;
810 struct phy_device *phydev;
811 unsigned int phy_id;
812 const phandle *ph;
813 const unsigned int *prop;
814 struct resource r;
815 int ret;
816
817 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700818 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500819 if (!ph)
820 return -ENODEV;
821 phy_dn = of_find_node_by_phandle(*ph);
822
Linus Torvalds90287802007-05-08 11:57:17 -0700823 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500824 ret = of_address_to_resource(phy_dn->parent, 0, &r);
825 if (ret)
826 goto err;
827
828 phy_id = *prop;
829 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
830
831 of_node_put(phy_dn);
832
833 mac->link = 0;
834 mac->speed = 0;
835 mac->duplex = -1;
836
837 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
838
839 if (IS_ERR(phydev)) {
840 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
841 return PTR_ERR(phydev);
842 }
843
844 mac->phydev = phydev;
845
846 return 0;
847
848err:
849 of_node_put(phy_dn);
850 return -ENODEV;
851}
852
853
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600854static int pasemi_mac_open(struct net_device *dev)
855{
856 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500857 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600858 unsigned int flags;
859 int ret;
860
861 /* enable rx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700862 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600863
864 /* enable tx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700865 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600866
867 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
868 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
869 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
870
Olof Johanssona85b9422007-09-15 13:40:59 -0700871 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600872
Olof Johanssona85b9422007-09-15 13:40:59 -0700873 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
874 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600875
Olof Johanssona85b9422007-09-15 13:40:59 -0700876 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
Olof Johansson02df6cf2007-08-22 09:13:03 -0500877 PAS_IOB_DMA_TXCH_CFG_CNTTH(128));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600878
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500879 /* Clear out any residual packet count state from firmware */
880 pasemi_mac_restart_rx_intr(mac);
881 pasemi_mac_restart_tx_intr(mac);
882
Olof Johansson6dfa7522007-05-08 00:47:32 -0500883 /* 0xffffff is max value, about 16ms */
Olof Johanssona85b9422007-09-15 13:40:59 -0700884 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
885 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600886
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600887 ret = pasemi_mac_setup_rx_resources(dev);
888 if (ret)
889 goto out_rx_resources;
890
891 ret = pasemi_mac_setup_tx_resources(dev);
892 if (ret)
893 goto out_tx_resources;
894
Olof Johanssona85b9422007-09-15 13:40:59 -0700895 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
896 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
897 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600898
899 /* enable rx if */
Olof Johanssona85b9422007-09-15 13:40:59 -0700900 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
901 PAS_DMA_RXINT_RCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600902
903 /* enable rx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700904 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
905 PAS_DMA_RXCHAN_CCMDSTA_EN |
906 PAS_DMA_RXCHAN_CCMDSTA_DU);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600907
908 /* enable tx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700909 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
910 PAS_DMA_TXCHAN_TCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600911
Olof Johansson928773c2007-09-26 16:25:06 -0500912 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600913
Olof Johansson36033762007-09-26 16:24:42 -0500914 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
915 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
916
917 if (mac->type == MAC_TYPE_GMAC)
918 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
919 else
920 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
921
922 /* Enable interface in MAC */
923 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
924
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500925 ret = pasemi_mac_phy_init(dev);
926 /* Some configs don't have PHYs (XAUI etc), so don't complain about
927 * failed init due to -ENODEV.
928 */
929 if (ret && ret != -ENODEV)
930 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
931
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600932 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700933 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600934
Olof Johansson771f7402007-05-08 00:47:21 -0500935 /* Interrupts are a bit different for our DMA controller: While
936 * it's got one a regular PCI device header, the interrupt there
937 * is really the base of the range it's using. Each tx and rx
938 * channel has it's own interrupt source.
939 */
940
941 base_irq = virq_to_hw(mac->dma_pdev->irq);
942
943 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
944 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
945
946 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600947 mac->tx->irq_name, dev);
948 if (ret) {
949 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500950 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600951 goto out_tx_int;
952 }
953
Olof Johansson771f7402007-05-08 00:47:21 -0500954 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600955 mac->rx->irq_name, dev);
956 if (ret) {
957 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500958 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600959 goto out_rx_int;
960 }
961
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500962 if (mac->phydev)
963 phy_start(mac->phydev);
964
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600965 return 0;
966
967out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -0500968 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600969out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700970 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600971 netif_stop_queue(dev);
972 pasemi_mac_free_tx_resources(dev);
973out_tx_resources:
974 pasemi_mac_free_rx_resources(dev);
975out_rx_resources:
976
977 return ret;
978}
979
980#define MAX_RETRIES 5000
981
982static int pasemi_mac_close(struct net_device *dev)
983{
984 struct pasemi_mac *mac = netdev_priv(dev);
985 unsigned int stat;
986 int retries;
987
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500988 if (mac->phydev) {
989 phy_stop(mac->phydev);
990 phy_disconnect(mac->phydev);
991 }
992
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600993 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700994 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600995
996 /* Clean out any pending buffers */
997 pasemi_mac_clean_tx(mac);
998 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
999
1000 /* Disable interface */
Olof Johanssona85b9422007-09-15 13:40:59 -07001001 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST);
1002 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST);
1003 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001004
1005 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -07001006 stat = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
Olof Johansson0ce68c72007-04-28 15:36:40 -05001007 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001008 break;
1009 cond_resched();
1010 }
1011
Olof Johansson0ce68c72007-04-28 15:36:40 -05001012 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001013 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001014
1015 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -07001016 stat = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
Olof Johansson0ce68c72007-04-28 15:36:40 -05001017 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001018 break;
1019 cond_resched();
1020 }
1021
Olof Johansson0ce68c72007-04-28 15:36:40 -05001022 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001023 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001024
1025 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -07001026 stat = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson0ce68c72007-04-28 15:36:40 -05001027 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001028 break;
1029 cond_resched();
1030 }
1031
Olof Johansson0ce68c72007-04-28 15:36:40 -05001032 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001033 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001034
1035 /* Then, disable the channel. This must be done separately from
1036 * stopping, since you can't disable when active.
1037 */
1038
Olof Johanssona85b9422007-09-15 13:40:59 -07001039 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
1040 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
1041 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001042
Olof Johansson771f7402007-05-08 00:47:21 -05001043 free_irq(mac->tx_irq, dev);
1044 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001045
1046 /* Free resources */
1047 pasemi_mac_free_rx_resources(dev);
1048 pasemi_mac_free_tx_resources(dev);
1049
1050 return 0;
1051}
1052
1053static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1054{
1055 struct pasemi_mac *mac = netdev_priv(dev);
1056 struct pasemi_mac_txring *txring;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001057 u64 dflags, mactx;
1058 dma_addr_t map[MAX_SKB_FRAGS+1];
1059 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001060 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001061 int i, nfrags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001062
1063 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
1064
1065 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001066 const unsigned char *nh = skb_network_header(skb);
1067
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001068 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001069 case IPPROTO_TCP:
1070 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001071 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001072 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001073 break;
1074 case IPPROTO_UDP:
1075 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001076 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001077 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001078 break;
1079 }
1080 }
1081
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001082 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001083
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001084 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1085 PCI_DMA_TODEVICE);
1086 map_size[0] = skb_headlen(skb);
1087 if (dma_mapping_error(map[0]))
1088 goto out_err_nolock;
1089
1090 for (i = 0; i < nfrags; i++) {
1091 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1092
1093 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1094 frag->page_offset, frag->size,
1095 PCI_DMA_TODEVICE);
1096 map_size[i+1] = frag->size;
1097 if (dma_mapping_error(map[i+1])) {
1098 nfrags = i;
1099 goto out_err_nolock;
1100 }
1101 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001102
Olof Johansson26fcfa92007-08-22 09:12:59 -05001103 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001104
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001105 txring = mac->tx;
1106
1107 spin_lock_irqsave(&txring->lock, flags);
1108
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001109 if (RING_AVAIL(txring) <= nfrags+3) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001110 spin_unlock_irqrestore(&txring->lock, flags);
1111 pasemi_mac_clean_tx(mac);
Olof Johansson52a94352007-05-12 18:01:09 -05001112 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001113 spin_lock_irqsave(&txring->lock, flags);
1114
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001115 if (RING_AVAIL(txring) <= nfrags+3) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001116 /* Still no room -- stop the queue and wait for tx
1117 * intr when there's room.
1118 */
1119 netif_stop_queue(dev);
1120 goto out_err;
1121 }
1122 }
1123
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001124 TX_RING(mac, txring->next_to_fill) = mactx;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001125 txring->next_to_fill++;
1126 TX_RING_INFO(mac, txring->next_to_fill).skb = skb;
1127 for (i = 0; i <= nfrags; i++) {
1128 TX_RING(mac, txring->next_to_fill+i) =
1129 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
1130 TX_RING_INFO(mac, txring->next_to_fill+i).dma = map[i];
1131 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001132
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001133 /* We have to add an even number of 8-byte entries to the ring
1134 * even if the last one is unused. That means always an odd number
1135 * of pointers + one mactx descriptor.
1136 */
1137 if (nfrags & 1)
1138 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001139
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001140 txring->next_to_fill += nfrags + 1;
1141
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001142
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001143 dev->stats.tx_packets++;
1144 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001145
1146 spin_unlock_irqrestore(&txring->lock, flags);
1147
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001148 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001149
1150 return NETDEV_TX_OK;
1151
1152out_err:
1153 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001154out_err_nolock:
1155 while (nfrags--)
1156 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1157 PCI_DMA_TODEVICE);
1158
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001159 return NETDEV_TX_BUSY;
1160}
1161
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001162static void pasemi_mac_set_rx_mode(struct net_device *dev)
1163{
1164 struct pasemi_mac *mac = netdev_priv(dev);
1165 unsigned int flags;
1166
Olof Johanssona85b9422007-09-15 13:40:59 -07001167 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001168
1169 /* Set promiscuous */
1170 if (dev->flags & IFF_PROMISC)
1171 flags |= PAS_MAC_CFG_PCFG_PR;
1172 else
1173 flags &= ~PAS_MAC_CFG_PCFG_PR;
1174
Olof Johanssona85b9422007-09-15 13:40:59 -07001175 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001176}
1177
1178
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001179static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001180{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001181 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1182 struct net_device *dev = mac->netdev;
1183 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001184
Olof Johansson829185e2007-09-15 13:53:19 -07001185 pasemi_mac_clean_tx(mac);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001186 pkts = pasemi_mac_clean_rx(mac, budget);
1187 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001188 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001189 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001190
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001191 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001192 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001193 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001194}
1195
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001196static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
1197{
1198 struct device_node *dn;
1199 void __iomem *ret;
1200
1201 dn = pci_device_to_OF_node(p);
1202 if (!dn)
1203 goto fallback;
1204
1205 ret = of_iomap(dn, index);
1206 if (!ret)
1207 goto fallback;
1208
1209 return ret;
1210fallback:
1211 /* This is hardcoded and ugly, but we have some firmware versions
1212 * that don't provide the register space in the device tree. Luckily
1213 * they are at well-known locations so we can just do the math here.
1214 */
1215 return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
1216}
1217
1218static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
1219{
1220 struct resource res;
1221 struct device_node *dn;
1222 int err;
1223
1224 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1225 if (!mac->dma_pdev) {
1226 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1227 return -ENODEV;
1228 }
1229
1230 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1231 if (!mac->iob_pdev) {
1232 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1233 return -ENODEV;
1234 }
1235
1236 mac->regs = map_onedev(mac->pdev, 0);
1237 mac->dma_regs = map_onedev(mac->dma_pdev, 0);
1238 mac->iob_regs = map_onedev(mac->iob_pdev, 0);
1239
1240 if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
1241 dev_err(&mac->pdev->dev, "Can't map registers\n");
1242 return -ENODEV;
1243 }
1244
1245 /* The dma status structure is located in the I/O bridge, and
1246 * is cache coherent.
1247 */
1248 if (!dma_status) {
1249 dn = pci_device_to_OF_node(mac->iob_pdev);
1250 if (dn)
1251 err = of_address_to_resource(dn, 1, &res);
1252 if (!dn || err) {
1253 /* Fallback for old firmware */
1254 res.start = 0xfd800000;
1255 res.end = res.start + 0x1000;
1256 }
1257 dma_status = __ioremap(res.start, res.end-res.start, 0);
1258 }
1259
1260 return 0;
1261}
1262
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001263static int __devinit
1264pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1265{
1266 static int index = 0;
1267 struct net_device *dev;
1268 struct pasemi_mac *mac;
1269 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001270 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001271
1272 err = pci_enable_device(pdev);
1273 if (err)
1274 return err;
1275
1276 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1277 if (dev == NULL) {
1278 dev_err(&pdev->dev,
1279 "pasemi_mac: Could not allocate ethernet device.\n");
1280 err = -ENOMEM;
1281 goto out_disable_device;
1282 }
1283
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001284 pci_set_drvdata(pdev, dev);
1285 SET_NETDEV_DEV(dev, &pdev->dev);
1286
1287 mac = netdev_priv(dev);
1288
1289 mac->pdev = pdev;
1290 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001291
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001292 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1293
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001294 dev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX | NETIF_F_SG;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001295
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001296 /* These should come out of the device tree eventually */
1297 mac->dma_txch = index;
1298 mac->dma_rxch = index;
1299
1300 /* We probe GMAC before XAUI, but the DMA interfaces are
1301 * in XAUI, GMAC order.
1302 */
1303 if (index < 4)
1304 mac->dma_if = index + 2;
1305 else
1306 mac->dma_if = index - 4;
1307 index++;
1308
1309 switch (pdev->device) {
1310 case 0xa005:
1311 mac->type = MAC_TYPE_GMAC;
1312 break;
1313 case 0xa006:
1314 mac->type = MAC_TYPE_XAUI;
1315 break;
1316 default:
1317 err = -ENODEV;
1318 goto out;
1319 }
1320
1321 /* get mac addr from device tree */
1322 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1323 err = -ENODEV;
1324 goto out;
1325 }
1326 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1327
1328 dev->open = pasemi_mac_open;
1329 dev->stop = pasemi_mac_close;
1330 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001331 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001332
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001333 err = pasemi_mac_map_regs(mac);
1334 if (err)
1335 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001336
1337 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1338 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1339
Olof Johanssonceb51362007-05-08 00:47:49 -05001340 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1341
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001342 /* Enable most messages by default */
1343 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1344
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001345 err = register_netdev(dev);
1346
1347 if (err) {
1348 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1349 err);
1350 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001351 } else if netif_msg_probe(mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001352 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
Joe Perches0795af52007-10-03 17:59:30 -07001353 "hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001354 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1355 mac->dma_if, mac->dma_txch, mac->dma_rxch,
Joe Perches0795af52007-10-03 17:59:30 -07001356 print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001357
1358 return err;
1359
1360out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001361 if (mac->iob_pdev)
1362 pci_dev_put(mac->iob_pdev);
1363 if (mac->dma_pdev)
1364 pci_dev_put(mac->dma_pdev);
1365 if (mac->dma_regs)
1366 iounmap(mac->dma_regs);
1367 if (mac->iob_regs)
1368 iounmap(mac->iob_regs);
1369 if (mac->regs)
1370 iounmap(mac->regs);
1371
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001372 free_netdev(dev);
1373out_disable_device:
1374 pci_disable_device(pdev);
1375 return err;
1376
1377}
1378
1379static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1380{
1381 struct net_device *netdev = pci_get_drvdata(pdev);
1382 struct pasemi_mac *mac;
1383
1384 if (!netdev)
1385 return;
1386
1387 mac = netdev_priv(netdev);
1388
1389 unregister_netdev(netdev);
1390
1391 pci_disable_device(pdev);
1392 pci_dev_put(mac->dma_pdev);
1393 pci_dev_put(mac->iob_pdev);
1394
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001395 iounmap(mac->regs);
1396 iounmap(mac->dma_regs);
1397 iounmap(mac->iob_regs);
1398
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001399 pci_set_drvdata(pdev, NULL);
1400 free_netdev(netdev);
1401}
1402
1403static struct pci_device_id pasemi_mac_pci_tbl[] = {
1404 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1405 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001406 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001407};
1408
1409MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1410
1411static struct pci_driver pasemi_mac_driver = {
1412 .name = "pasemi_mac",
1413 .id_table = pasemi_mac_pci_tbl,
1414 .probe = pasemi_mac_probe,
1415 .remove = __devexit_p(pasemi_mac_remove),
1416};
1417
1418static void __exit pasemi_mac_cleanup_module(void)
1419{
1420 pci_unregister_driver(&pasemi_mac_driver);
1421 __iounmap(dma_status);
1422 dma_status = NULL;
1423}
1424
1425int pasemi_mac_init_module(void)
1426{
1427 return pci_register_driver(&pasemi_mac_driver);
1428}
1429
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001430module_init(pasemi_mac_init_module);
1431module_exit(pasemi_mac_cleanup_module);