blob: 4a451f8c6f4d98203d41cc43a9e9f4816808b205 [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 */
Olof Johanssonad5da102007-10-02 16:27:15 -050059#define RX_RING_SIZE 4096
60#define TX_RING_SIZE 4096
Olof Johanssonf5cd7872007-01-31 21:43:54 -060061
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 Johanssonad5da102007-10-02 16:27:15 -0500339 int start, limit;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600340
Olof Johanssonad5da102007-10-02 16:27:15 -0500341 start = mac->tx->next_to_clean;
342 limit = mac->tx->next_to_fill;
343
344 /* Compensate for when fill has wrapped and clean has not */
345 if (start > limit)
346 limit += TX_RING_SIZE;
347
348 for (i = start; i < limit; i += freed) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500349 info = &TX_RING_INFO(mac, i+1);
350 if (info->dma && info->skb) {
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500351 for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
352 dmas[j] = TX_RING_INFO(mac, i+1+j).dma;
353 freed = pasemi_mac_unmap_tx_skb(mac, info->skb, dmas);
354 } else
355 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600356 }
357
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500358 for (i = 0; i < TX_RING_SIZE; i++)
359 TX_RING(mac, i) = 0;
360
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600361 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500362 TX_RING_SIZE * sizeof(u64),
363 mac->tx->ring, mac->tx->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600364
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500365 kfree(mac->tx->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600366 kfree(mac->tx);
367 mac->tx = NULL;
368}
369
370static void pasemi_mac_free_rx_resources(struct net_device *dev)
371{
372 struct pasemi_mac *mac = netdev_priv(dev);
373 unsigned int i;
374 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600375
376 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500377 info = &RX_RING_INFO(mac, i);
378 if (info->skb && info->dma) {
379 pci_unmap_single(mac->dma_pdev,
380 info->dma,
381 info->skb->len,
382 PCI_DMA_FROMDEVICE);
383 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600384 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500385 info->dma = 0;
386 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600387 }
388
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500389 for (i = 0; i < RX_RING_SIZE; i++)
390 RX_RING(mac, i) = 0;
391
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600392 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500393 RX_RING_SIZE * sizeof(u64),
394 mac->rx->ring, mac->rx->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600395
396 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
397 mac->rx->buffers, mac->rx->buf_dma);
398
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500399 kfree(mac->rx->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600400 kfree(mac->rx);
401 mac->rx = NULL;
402}
403
Olof Johansson928773c2007-09-26 16:25:06 -0500404static void pasemi_mac_replenish_rx_ring(struct net_device *dev, int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600405{
406 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600407 int start = mac->rx->next_to_fill;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500408 unsigned int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600409
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500410 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600411 return;
412
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500413 fill = start;
Olof Johansson928773c2007-09-26 16:25:06 -0500414 for (count = 0; count < limit; count++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500415 struct pasemi_mac_buffer *info = &RX_RING_INFO(mac, fill);
416 u64 *buff = &RX_BUFF(mac, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600417 struct sk_buff *skb;
418 dma_addr_t dma;
419
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500420 /* Entry in use? */
421 WARN_ON(*buff);
422
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500423 /* skb might still be in there for recycle on short receives */
424 if (info->skb)
425 skb = info->skb;
Olof Johansson8dc121a2007-10-02 16:26:53 -0500426 else {
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500427 skb = dev_alloc_skb(BUF_SIZE);
Olof Johansson8dc121a2007-10-02 16:26:53 -0500428 skb_reserve(skb, LOCAL_SKB_ALIGN);
429 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600430
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500431 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600432 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600433
Olof Johansson8dc121a2007-10-02 16:26:53 -0500434 dma = pci_map_single(mac->dma_pdev, skb->data,
435 BUF_SIZE - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600436 PCI_DMA_FROMDEVICE);
437
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500438 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600439 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600440 break;
441 }
442
443 info->skb = skb;
444 info->dma = dma;
445 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500446 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600447 }
448
449 wmb();
450
Olof Johansson928773c2007-09-26 16:25:06 -0500451 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), count);
452 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600453
Olof Johansson928773c2007-09-26 16:25:06 -0500454 mac->rx->next_to_fill += count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600455}
456
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500457static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
458{
Olof Johansson52a94352007-05-12 18:01:09 -0500459 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500460 /* Re-enable packet count interrupts: finally
461 * ack the packet count interrupt we got in rx_intr.
462 */
463
Olof Johansson52a94352007-05-12 18:01:09 -0500464 pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500465
Olof Johansson52a94352007-05-12 18:01:09 -0500466 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500467
Olof Johanssona85b9422007-09-15 13:40:59 -0700468 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500469}
470
471static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
472{
Olof Johansson52a94352007-05-12 18:01:09 -0500473 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500474
475 /* Re-enable packet count interrupts */
Olof Johansson52a94352007-05-12 18:01:09 -0500476 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500477
Olof Johansson52a94352007-05-12 18:01:09 -0500478 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500479
Olof Johanssona85b9422007-09-15 13:40:59 -0700480 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500481}
482
483
Olof Johansson69c29d82007-10-02 16:24:51 -0500484static inline void pasemi_mac_rx_error(struct pasemi_mac *mac, u64 macrx)
485{
486 unsigned int rcmdsta, ccmdsta;
487
488 if (!netif_msg_rx_err(mac))
489 return;
490
491 rcmdsta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
492 ccmdsta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
493
494 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
495 macrx, *mac->rx_status);
496
497 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
498 rcmdsta, ccmdsta);
499}
500
501static inline void pasemi_mac_tx_error(struct pasemi_mac *mac, u64 mactx)
502{
503 unsigned int cmdsta;
504
505 if (!netif_msg_tx_err(mac))
506 return;
507
508 cmdsta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
509
510 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
511 "tx status 0x%016lx\n", mactx, *mac->tx_status);
512
513 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
514}
515
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600516static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
517{
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500518 unsigned int n;
519 int count;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500520 struct pasemi_mac_buffer *info;
521 struct sk_buff *skb;
522 unsigned int i, len;
523 u64 macrx;
524 dma_addr_t dma;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600525
526 spin_lock(&mac->rx->lock);
527
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500528 n = mac->rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600529
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500530 for (count = limit; count; count--) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500531 macrx = RX_RING(mac, n);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600532
Olof Johansson69c29d82007-10-02 16:24:51 -0500533 if ((macrx & XCT_MACRX_E) ||
534 (*mac->rx_status & PAS_STATUS_ERROR))
535 pasemi_mac_rx_error(mac, macrx);
536
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500537 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600538 break;
539
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600540 info = NULL;
541
542 /* We have to scan for our skb since there's no way
543 * to back-map them from the descriptor, and if we
544 * have several receive channels then they might not
545 * show up in the same order as they were put on the
546 * interface ring.
547 */
548
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500549 dma = (RX_RING(mac, n+1) & XCT_PTR_ADDR_M);
550 for (i = mac->rx->next_to_fill;
551 i < (mac->rx->next_to_fill + RX_RING_SIZE);
552 i++) {
553 info = &RX_RING_INFO(mac, i);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600554 if (info->dma == dma)
555 break;
556 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500557
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500558 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600559
Olof Johanssonad5da102007-10-02 16:27:15 -0500560 prefetch(skb);
561 prefetch(&skb->data_len);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600562
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500563 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600564
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500565 if (len < 256) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500566 struct sk_buff *new_skb;
567
568 new_skb = netdev_alloc_skb(mac->netdev,
569 len + LOCAL_SKB_ALIGN);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500570 if (new_skb) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500571 skb_reserve(new_skb, LOCAL_SKB_ALIGN);
Olof Johansson73344862007-08-22 09:12:55 -0500572 memcpy(new_skb->data, skb->data, len);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500573 /* save the skb in buffer_info as good */
574 skb = new_skb;
575 }
576 /* else just continue with the old one */
577 } else
578 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600579
Olof Johanssonad5da102007-10-02 16:27:15 -0500580 pci_unmap_single(mac->dma_pdev, dma, len, PCI_DMA_FROMDEVICE);
581
582 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500583
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 Johanssonad5da102007-10-02 16:27:15 -0500602 /* Need to zero it out since hardware doesn't, since the
603 * replenish loop uses it to tell when it's done.
604 */
605 RX_BUFF(mac, i) = 0;
606
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500607 n += 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600608 }
609
Olof Johansson9a50beb2007-10-02 16:26:30 -0500610 if (n > RX_RING_SIZE) {
611 /* Errata 5971 workaround: L2 target of headers */
612 write_iob_reg(mac, PAS_IOB_COM_PKTHDRCNT, 0);
613 n &= (RX_RING_SIZE-1);
614 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500615 mac->rx->next_to_clean = n;
Olof Johansson928773c2007-09-26 16:25:06 -0500616 pasemi_mac_replenish_rx_ring(mac->netdev, limit-count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600617
618 spin_unlock(&mac->rx->lock);
619
620 return count;
621}
622
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500623/* Can't make this too large or we blow the kernel stack limits */
624#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
625
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600626static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
627{
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500628 int i, j;
Olof Johanssonad5da102007-10-02 16:27:15 -0500629 unsigned int start, descr_count, buf_count, batch_limit;
630 unsigned int ring_limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500631 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500632 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500633 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
634 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600635
Olof Johansson02df6cf2007-08-22 09:13:03 -0500636 total_count = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500637 batch_limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500638restart:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600639 spin_lock_irqsave(&mac->tx->lock, flags);
640
641 start = mac->tx->next_to_clean;
Olof Johanssonad5da102007-10-02 16:27:15 -0500642 ring_limit = mac->tx->next_to_fill;
643
644 /* Compensate for when fill has wrapped but clean has not */
645 if (start > ring_limit)
646 ring_limit += TX_RING_SIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500647
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500648 buf_count = 0;
649 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600650
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500651 for (i = start;
Olof Johanssonad5da102007-10-02 16:27:15 -0500652 descr_count < batch_limit && i < ring_limit;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500653 i += buf_count) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500654 u64 mactx = TX_RING(mac, i);
Olof Johanssonad5da102007-10-02 16:27:15 -0500655 struct sk_buff *skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500656
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500657 if ((mactx & XCT_MACTX_E) ||
Olof Johansson69c29d82007-10-02 16:24:51 -0500658 (*mac->tx_status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500659 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500660
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500661 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500662 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600663 break;
664
Olof Johanssonad5da102007-10-02 16:27:15 -0500665 skb = TX_RING_INFO(mac, i+1).skb;
666 skbs[descr_count] = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500667
Olof Johanssonad5da102007-10-02 16:27:15 -0500668 buf_count = 2 + skb_shinfo(skb)->nr_frags;
669 for (j = 0; j <= skb_shinfo(skb)->nr_frags; j++)
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500670 dmas[descr_count][j] = TX_RING_INFO(mac, i+1+j).dma;
671
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500672 TX_RING(mac, i) = 0;
673 TX_RING(mac, i+1) = 0;
674
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500675 /* Since we always fill with an even number of entries, make
676 * sure we skip any unused one at the end as well.
677 */
678 if (buf_count & 1)
679 buf_count++;
680 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600681 }
Olof Johanssonad5da102007-10-02 16:27:15 -0500682 mac->tx->next_to_clean = i & (TX_RING_SIZE-1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500683
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600684 spin_unlock_irqrestore(&mac->tx->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500685 netif_wake_queue(mac->netdev);
686
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500687 for (i = 0; i < descr_count; i++)
688 pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500689
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500690 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500691
692 /* If the batch was full, try to clean more */
Olof Johanssonad5da102007-10-02 16:27:15 -0500693 if (descr_count == batch_limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500694 goto restart;
695
696 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600697}
698
699
700static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
701{
702 struct net_device *dev = data;
703 struct pasemi_mac *mac = netdev_priv(dev);
704 unsigned int reg;
705
Olof Johansson6dfa7522007-05-08 00:47:32 -0500706 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600707 return IRQ_NONE;
708
Olof Johansson6dfa7522007-05-08 00:47:32 -0500709 /* Don't reset packet count so it won't fire again but clear
710 * all others.
711 */
712
Olof Johansson6dfa7522007-05-08 00:47:32 -0500713 reg = 0;
714 if (*mac->rx_status & PAS_STATUS_SOFT)
715 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
716 if (*mac->rx_status & PAS_STATUS_ERROR)
717 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600718 if (*mac->rx_status & PAS_STATUS_TIMER)
719 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
720
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700721 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500722
Olof Johanssona85b9422007-09-15 13:40:59 -0700723 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600724
725 return IRQ_HANDLED;
726}
727
728static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
729{
730 struct net_device *dev = data;
731 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson52a94352007-05-12 18:01:09 -0500732 unsigned int reg, pcnt;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600733
Olof Johansson6dfa7522007-05-08 00:47:32 -0500734 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600735 return IRQ_NONE;
736
737 pasemi_mac_clean_tx(mac);
738
Olof Johansson52a94352007-05-12 18:01:09 -0500739 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
740
741 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500742
743 if (*mac->tx_status & PAS_STATUS_SOFT)
744 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
745 if (*mac->tx_status & PAS_STATUS_ERROR)
746 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600747
Olof Johanssona85b9422007-09-15 13:40:59 -0700748 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600749
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600750 return IRQ_HANDLED;
751}
752
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500753static void pasemi_adjust_link(struct net_device *dev)
754{
755 struct pasemi_mac *mac = netdev_priv(dev);
756 int msg;
757 unsigned int flags;
758 unsigned int new_flags;
759
760 if (!mac->phydev->link) {
761 /* If no link, MAC speed settings don't matter. Just report
762 * link down and return.
763 */
764 if (mac->link && netif_msg_link(mac))
765 printk(KERN_INFO "%s: Link is down.\n", dev->name);
766
767 netif_carrier_off(dev);
768 mac->link = 0;
769
770 return;
771 } else
772 netif_carrier_on(dev);
773
Olof Johanssona85b9422007-09-15 13:40:59 -0700774 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500775 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
776 PAS_MAC_CFG_PCFG_TSR_M);
777
778 if (!mac->phydev->duplex)
779 new_flags |= PAS_MAC_CFG_PCFG_HD;
780
781 switch (mac->phydev->speed) {
782 case 1000:
783 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
784 PAS_MAC_CFG_PCFG_TSR_1G;
785 break;
786 case 100:
787 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
788 PAS_MAC_CFG_PCFG_TSR_100M;
789 break;
790 case 10:
791 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
792 PAS_MAC_CFG_PCFG_TSR_10M;
793 break;
794 default:
795 printk("Unsupported speed %d\n", mac->phydev->speed);
796 }
797
798 /* Print on link or speed/duplex change */
799 msg = mac->link != mac->phydev->link || flags != new_flags;
800
801 mac->duplex = mac->phydev->duplex;
802 mac->speed = mac->phydev->speed;
803 mac->link = mac->phydev->link;
804
805 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700806 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500807
808 if (msg && netif_msg_link(mac))
809 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
810 dev->name, mac->speed, mac->duplex ? "full" : "half");
811}
812
813static int pasemi_mac_phy_init(struct net_device *dev)
814{
815 struct pasemi_mac *mac = netdev_priv(dev);
816 struct device_node *dn, *phy_dn;
817 struct phy_device *phydev;
818 unsigned int phy_id;
819 const phandle *ph;
820 const unsigned int *prop;
821 struct resource r;
822 int ret;
823
824 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700825 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500826 if (!ph)
827 return -ENODEV;
828 phy_dn = of_find_node_by_phandle(*ph);
829
Linus Torvalds90287802007-05-08 11:57:17 -0700830 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500831 ret = of_address_to_resource(phy_dn->parent, 0, &r);
832 if (ret)
833 goto err;
834
835 phy_id = *prop;
836 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
837
838 of_node_put(phy_dn);
839
840 mac->link = 0;
841 mac->speed = 0;
842 mac->duplex = -1;
843
844 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
845
846 if (IS_ERR(phydev)) {
847 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
848 return PTR_ERR(phydev);
849 }
850
851 mac->phydev = phydev;
852
853 return 0;
854
855err:
856 of_node_put(phy_dn);
857 return -ENODEV;
858}
859
860
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600861static int pasemi_mac_open(struct net_device *dev)
862{
863 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500864 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600865 unsigned int flags;
866 int ret;
867
868 /* enable rx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700869 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600870
871 /* enable tx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700872 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600873
874 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
875 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
876 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
877
Olof Johanssona85b9422007-09-15 13:40:59 -0700878 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600879
Olof Johanssona85b9422007-09-15 13:40:59 -0700880 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
881 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600882
Olof Johanssona85b9422007-09-15 13:40:59 -0700883 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
Olof Johansson02df6cf2007-08-22 09:13:03 -0500884 PAS_IOB_DMA_TXCH_CFG_CNTTH(128));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600885
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500886 /* Clear out any residual packet count state from firmware */
887 pasemi_mac_restart_rx_intr(mac);
888 pasemi_mac_restart_tx_intr(mac);
889
Olof Johansson6dfa7522007-05-08 00:47:32 -0500890 /* 0xffffff is max value, about 16ms */
Olof Johanssona85b9422007-09-15 13:40:59 -0700891 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
892 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600893
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600894 ret = pasemi_mac_setup_rx_resources(dev);
895 if (ret)
896 goto out_rx_resources;
897
898 ret = pasemi_mac_setup_tx_resources(dev);
899 if (ret)
900 goto out_tx_resources;
901
Olof Johanssona85b9422007-09-15 13:40:59 -0700902 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
903 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
904 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600905
906 /* enable rx if */
Olof Johanssona85b9422007-09-15 13:40:59 -0700907 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
908 PAS_DMA_RXINT_RCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600909
910 /* enable rx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700911 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
912 PAS_DMA_RXCHAN_CCMDSTA_EN |
913 PAS_DMA_RXCHAN_CCMDSTA_DU);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600914
915 /* enable tx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700916 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
917 PAS_DMA_TXCHAN_TCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600918
Olof Johansson928773c2007-09-26 16:25:06 -0500919 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600920
Olof Johansson36033762007-09-26 16:24:42 -0500921 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
922 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
923
924 if (mac->type == MAC_TYPE_GMAC)
925 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
926 else
927 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
928
929 /* Enable interface in MAC */
930 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
931
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500932 ret = pasemi_mac_phy_init(dev);
933 /* Some configs don't have PHYs (XAUI etc), so don't complain about
934 * failed init due to -ENODEV.
935 */
936 if (ret && ret != -ENODEV)
937 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
938
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600939 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700940 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600941
Olof Johansson771f7402007-05-08 00:47:21 -0500942 /* Interrupts are a bit different for our DMA controller: While
943 * it's got one a regular PCI device header, the interrupt there
944 * is really the base of the range it's using. Each tx and rx
945 * channel has it's own interrupt source.
946 */
947
948 base_irq = virq_to_hw(mac->dma_pdev->irq);
949
950 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
951 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
952
953 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600954 mac->tx->irq_name, dev);
955 if (ret) {
956 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500957 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600958 goto out_tx_int;
959 }
960
Olof Johansson771f7402007-05-08 00:47:21 -0500961 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600962 mac->rx->irq_name, dev);
963 if (ret) {
964 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500965 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600966 goto out_rx_int;
967 }
968
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500969 if (mac->phydev)
970 phy_start(mac->phydev);
971
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600972 return 0;
973
974out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -0500975 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600976out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700977 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600978 netif_stop_queue(dev);
979 pasemi_mac_free_tx_resources(dev);
980out_tx_resources:
981 pasemi_mac_free_rx_resources(dev);
982out_rx_resources:
983
984 return ret;
985}
986
987#define MAX_RETRIES 5000
988
989static int pasemi_mac_close(struct net_device *dev)
990{
991 struct pasemi_mac *mac = netdev_priv(dev);
992 unsigned int stat;
993 int retries;
994
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500995 if (mac->phydev) {
996 phy_stop(mac->phydev);
997 phy_disconnect(mac->phydev);
998 }
999
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001000 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001001 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001002
1003 /* Clean out any pending buffers */
1004 pasemi_mac_clean_tx(mac);
1005 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
1006
1007 /* Disable interface */
Olof Johanssona85b9422007-09-15 13:40:59 -07001008 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST);
1009 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST);
1010 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001011
1012 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -07001013 stat = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
Olof Johansson0ce68c72007-04-28 15:36:40 -05001014 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001015 break;
1016 cond_resched();
1017 }
1018
Olof Johansson0ce68c72007-04-28 15:36:40 -05001019 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001020 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001021
1022 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -07001023 stat = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
Olof Johansson0ce68c72007-04-28 15:36:40 -05001024 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001025 break;
1026 cond_resched();
1027 }
1028
Olof Johansson0ce68c72007-04-28 15:36:40 -05001029 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001030 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001031
1032 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -07001033 stat = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson0ce68c72007-04-28 15:36:40 -05001034 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001035 break;
1036 cond_resched();
1037 }
1038
Olof Johansson0ce68c72007-04-28 15:36:40 -05001039 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001040 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001041
1042 /* Then, disable the channel. This must be done separately from
1043 * stopping, since you can't disable when active.
1044 */
1045
Olof Johanssona85b9422007-09-15 13:40:59 -07001046 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
1047 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
1048 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001049
Olof Johansson771f7402007-05-08 00:47:21 -05001050 free_irq(mac->tx_irq, dev);
1051 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001052
1053 /* Free resources */
1054 pasemi_mac_free_rx_resources(dev);
1055 pasemi_mac_free_tx_resources(dev);
1056
1057 return 0;
1058}
1059
1060static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1061{
1062 struct pasemi_mac *mac = netdev_priv(dev);
1063 struct pasemi_mac_txring *txring;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001064 u64 dflags, mactx;
1065 dma_addr_t map[MAX_SKB_FRAGS+1];
1066 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001067 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001068 int i, nfrags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001069
1070 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
1071
1072 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001073 const unsigned char *nh = skb_network_header(skb);
1074
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001075 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001076 case IPPROTO_TCP:
1077 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001078 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001079 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001080 break;
1081 case IPPROTO_UDP:
1082 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001083 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001084 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001085 break;
1086 }
1087 }
1088
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001089 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001090
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001091 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1092 PCI_DMA_TODEVICE);
1093 map_size[0] = skb_headlen(skb);
1094 if (dma_mapping_error(map[0]))
1095 goto out_err_nolock;
1096
1097 for (i = 0; i < nfrags; i++) {
1098 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1099
1100 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1101 frag->page_offset, frag->size,
1102 PCI_DMA_TODEVICE);
1103 map_size[i+1] = frag->size;
1104 if (dma_mapping_error(map[i+1])) {
1105 nfrags = i;
1106 goto out_err_nolock;
1107 }
1108 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001109
Olof Johansson26fcfa92007-08-22 09:12:59 -05001110 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001111
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001112 txring = mac->tx;
1113
1114 spin_lock_irqsave(&txring->lock, flags);
1115
Olof Johanssonad5da102007-10-02 16:27:15 -05001116 /* Avoid stepping on the same cache line that the DMA controller
1117 * is currently about to send, so leave at least 8 words available.
1118 * Total free space needed is mactx + fragments + 8
1119 */
1120 if (RING_AVAIL(txring) < nfrags + 10) {
1121 /* no room -- stop the queue and wait for tx intr */
1122 netif_stop_queue(dev);
1123 goto out_err;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001124 }
1125
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001126 TX_RING(mac, txring->next_to_fill) = mactx;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001127 txring->next_to_fill++;
1128 TX_RING_INFO(mac, txring->next_to_fill).skb = skb;
1129 for (i = 0; i <= nfrags; i++) {
1130 TX_RING(mac, txring->next_to_fill+i) =
1131 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
1132 TX_RING_INFO(mac, txring->next_to_fill+i).dma = map[i];
1133 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001134
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001135 /* We have to add an even number of 8-byte entries to the ring
1136 * even if the last one is unused. That means always an odd number
1137 * of pointers + one mactx descriptor.
1138 */
1139 if (nfrags & 1)
1140 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001141
Olof Johanssonad5da102007-10-02 16:27:15 -05001142 txring->next_to_fill = (txring->next_to_fill + nfrags + 1) &
1143 (TX_RING_SIZE-1);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001144
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001145 dev->stats.tx_packets++;
1146 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001147
1148 spin_unlock_irqrestore(&txring->lock, flags);
1149
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001150 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001151
1152 return NETDEV_TX_OK;
1153
1154out_err:
1155 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001156out_err_nolock:
1157 while (nfrags--)
1158 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1159 PCI_DMA_TODEVICE);
1160
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001161 return NETDEV_TX_BUSY;
1162}
1163
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001164static void pasemi_mac_set_rx_mode(struct net_device *dev)
1165{
1166 struct pasemi_mac *mac = netdev_priv(dev);
1167 unsigned int flags;
1168
Olof Johanssona85b9422007-09-15 13:40:59 -07001169 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001170
1171 /* Set promiscuous */
1172 if (dev->flags & IFF_PROMISC)
1173 flags |= PAS_MAC_CFG_PCFG_PR;
1174 else
1175 flags &= ~PAS_MAC_CFG_PCFG_PR;
1176
Olof Johanssona85b9422007-09-15 13:40:59 -07001177 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001178}
1179
1180
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001181static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001182{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001183 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1184 struct net_device *dev = mac->netdev;
1185 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001186
Olof Johansson829185e2007-09-15 13:53:19 -07001187 pasemi_mac_clean_tx(mac);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001188 pkts = pasemi_mac_clean_rx(mac, budget);
1189 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001190 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001191 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001192
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001193 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001194 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001195 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001196}
1197
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001198static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
1199{
1200 struct device_node *dn;
1201 void __iomem *ret;
1202
1203 dn = pci_device_to_OF_node(p);
1204 if (!dn)
1205 goto fallback;
1206
1207 ret = of_iomap(dn, index);
1208 if (!ret)
1209 goto fallback;
1210
1211 return ret;
1212fallback:
1213 /* This is hardcoded and ugly, but we have some firmware versions
1214 * that don't provide the register space in the device tree. Luckily
1215 * they are at well-known locations so we can just do the math here.
1216 */
1217 return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
1218}
1219
1220static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
1221{
1222 struct resource res;
1223 struct device_node *dn;
1224 int err;
1225
1226 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1227 if (!mac->dma_pdev) {
1228 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1229 return -ENODEV;
1230 }
1231
1232 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1233 if (!mac->iob_pdev) {
1234 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1235 return -ENODEV;
1236 }
1237
1238 mac->regs = map_onedev(mac->pdev, 0);
1239 mac->dma_regs = map_onedev(mac->dma_pdev, 0);
1240 mac->iob_regs = map_onedev(mac->iob_pdev, 0);
1241
1242 if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
1243 dev_err(&mac->pdev->dev, "Can't map registers\n");
1244 return -ENODEV;
1245 }
1246
1247 /* The dma status structure is located in the I/O bridge, and
1248 * is cache coherent.
1249 */
1250 if (!dma_status) {
1251 dn = pci_device_to_OF_node(mac->iob_pdev);
1252 if (dn)
1253 err = of_address_to_resource(dn, 1, &res);
1254 if (!dn || err) {
1255 /* Fallback for old firmware */
1256 res.start = 0xfd800000;
1257 res.end = res.start + 0x1000;
1258 }
1259 dma_status = __ioremap(res.start, res.end-res.start, 0);
1260 }
1261
1262 return 0;
1263}
1264
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001265static int __devinit
1266pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1267{
1268 static int index = 0;
1269 struct net_device *dev;
1270 struct pasemi_mac *mac;
1271 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001272 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001273
1274 err = pci_enable_device(pdev);
1275 if (err)
1276 return err;
1277
1278 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1279 if (dev == NULL) {
1280 dev_err(&pdev->dev,
1281 "pasemi_mac: Could not allocate ethernet device.\n");
1282 err = -ENOMEM;
1283 goto out_disable_device;
1284 }
1285
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001286 pci_set_drvdata(pdev, dev);
1287 SET_NETDEV_DEV(dev, &pdev->dev);
1288
1289 mac = netdev_priv(dev);
1290
1291 mac->pdev = pdev;
1292 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001293
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001294 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1295
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001296 dev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX | NETIF_F_SG;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001297
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001298 /* These should come out of the device tree eventually */
1299 mac->dma_txch = index;
1300 mac->dma_rxch = index;
1301
1302 /* We probe GMAC before XAUI, but the DMA interfaces are
1303 * in XAUI, GMAC order.
1304 */
1305 if (index < 4)
1306 mac->dma_if = index + 2;
1307 else
1308 mac->dma_if = index - 4;
1309 index++;
1310
1311 switch (pdev->device) {
1312 case 0xa005:
1313 mac->type = MAC_TYPE_GMAC;
1314 break;
1315 case 0xa006:
1316 mac->type = MAC_TYPE_XAUI;
1317 break;
1318 default:
1319 err = -ENODEV;
1320 goto out;
1321 }
1322
1323 /* get mac addr from device tree */
1324 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1325 err = -ENODEV;
1326 goto out;
1327 }
1328 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1329
1330 dev->open = pasemi_mac_open;
1331 dev->stop = pasemi_mac_close;
1332 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001333 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001334
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001335 err = pasemi_mac_map_regs(mac);
1336 if (err)
1337 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001338
1339 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1340 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1341
Olof Johanssonceb51362007-05-08 00:47:49 -05001342 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1343
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001344 /* Enable most messages by default */
1345 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1346
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001347 err = register_netdev(dev);
1348
1349 if (err) {
1350 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1351 err);
1352 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001353 } else if netif_msg_probe(mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001354 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
Joe Perches0795af52007-10-03 17:59:30 -07001355 "hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001356 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1357 mac->dma_if, mac->dma_txch, mac->dma_rxch,
Joe Perches0795af52007-10-03 17:59:30 -07001358 print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001359
1360 return err;
1361
1362out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001363 if (mac->iob_pdev)
1364 pci_dev_put(mac->iob_pdev);
1365 if (mac->dma_pdev)
1366 pci_dev_put(mac->dma_pdev);
1367 if (mac->dma_regs)
1368 iounmap(mac->dma_regs);
1369 if (mac->iob_regs)
1370 iounmap(mac->iob_regs);
1371 if (mac->regs)
1372 iounmap(mac->regs);
1373
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001374 free_netdev(dev);
1375out_disable_device:
1376 pci_disable_device(pdev);
1377 return err;
1378
1379}
1380
1381static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1382{
1383 struct net_device *netdev = pci_get_drvdata(pdev);
1384 struct pasemi_mac *mac;
1385
1386 if (!netdev)
1387 return;
1388
1389 mac = netdev_priv(netdev);
1390
1391 unregister_netdev(netdev);
1392
1393 pci_disable_device(pdev);
1394 pci_dev_put(mac->dma_pdev);
1395 pci_dev_put(mac->iob_pdev);
1396
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001397 iounmap(mac->regs);
1398 iounmap(mac->dma_regs);
1399 iounmap(mac->iob_regs);
1400
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001401 pci_set_drvdata(pdev, NULL);
1402 free_netdev(netdev);
1403}
1404
1405static struct pci_device_id pasemi_mac_pci_tbl[] = {
1406 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1407 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001408 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001409};
1410
1411MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1412
1413static struct pci_driver pasemi_mac_driver = {
1414 .name = "pasemi_mac",
1415 .id_table = pasemi_mac_pci_tbl,
1416 .probe = pasemi_mac_probe,
1417 .remove = __devexit_p(pasemi_mac_remove),
1418};
1419
1420static void __exit pasemi_mac_cleanup_module(void)
1421{
1422 pci_unregister_driver(&pasemi_mac_driver);
1423 __iounmap(dma_status);
1424 dma_status = NULL;
1425}
1426
1427int pasemi_mac_init_module(void)
1428{
1429 return pci_register_driver(&pasemi_mac_driver);
1430}
1431
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001432module_init(pasemi_mac_init_module);
1433module_exit(pasemi_mac_cleanup_module);