blob: b14f171b1b670bea2aeae94f7cb65b0a46804653 [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>
Olof Johanssonaf289e82007-10-03 13:03:54 -050037#include <asm/firmware.h>
Olof Johansson771f7402007-05-08 00:47:21 -050038
Olof Johanssonf5cd7872007-01-31 21:43:54 -060039#include "pasemi_mac.h"
40
Olof Johansson8dc121a2007-10-02 16:26:53 -050041/* We have our own align, since ppc64 in general has it at 0 because
42 * of design flaws in some of the server bridge chips. However, for
43 * PWRficient doing the unaligned copies is more expensive than doing
44 * unaligned DMA, so make sure the data is aligned instead.
45 */
46#define LOCAL_SKB_ALIGN 2
Olof Johanssonf5cd7872007-01-31 21:43:54 -060047
48/* TODO list
49 *
Olof Johanssonf5cd7872007-01-31 21:43:54 -060050 * - Multicast support
51 * - Large MTU support
Olof Johansson7ddeae22007-10-02 16:27:28 -050052 * - SW LRO
53 * - Multiqueue RX/TX
Olof Johanssonf5cd7872007-01-31 21:43:54 -060054 */
55
56
57/* Must be a power of two */
Olof Johanssonad5da102007-10-02 16:27:15 -050058#define RX_RING_SIZE 4096
59#define TX_RING_SIZE 4096
Olof Johanssonf5cd7872007-01-31 21:43:54 -060060
Olof Johanssonceb51362007-05-08 00:47:49 -050061#define DEFAULT_MSG_ENABLE \
62 (NETIF_MSG_DRV | \
63 NETIF_MSG_PROBE | \
64 NETIF_MSG_LINK | \
65 NETIF_MSG_TIMER | \
66 NETIF_MSG_IFDOWN | \
67 NETIF_MSG_IFUP | \
68 NETIF_MSG_RX_ERR | \
69 NETIF_MSG_TX_ERR)
70
Olof Johanssonfc9e4d22007-10-02 16:25:53 -050071#define TX_RING(mac, num) ((mac)->tx->ring[(num) & (TX_RING_SIZE-1)])
72#define TX_RING_INFO(mac, num) ((mac)->tx->ring_info[(num) & (TX_RING_SIZE-1)])
73#define RX_RING(mac, num) ((mac)->rx->ring[(num) & (RX_RING_SIZE-1)])
74#define RX_RING_INFO(mac, num) ((mac)->rx->ring_info[(num) & (RX_RING_SIZE-1)])
Olof Johanssonf5cd7872007-01-31 21:43:54 -060075#define RX_BUFF(mac, num) ((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)])
76
Olof Johansson021fa222007-08-22 09:13:11 -050077#define RING_USED(ring) (((ring)->next_to_fill - (ring)->next_to_clean) \
78 & ((ring)->size - 1))
79#define RING_AVAIL(ring) ((ring->size) - RING_USED(ring))
80
Olof Johanssonf5cd7872007-01-31 21:43:54 -060081#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
82
Olof Johanssonceb51362007-05-08 00:47:49 -050083MODULE_LICENSE("GPL");
84MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
85MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
86
87static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
88module_param(debug, int, 0);
89MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060090
91static struct pasdma_status *dma_status;
92
Olof Johanssonaf289e82007-10-03 13:03:54 -050093static int translation_enabled(void)
94{
95#if defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
96 return 1;
97#else
98 return firmware_has_feature(FW_FEATURE_LPAR);
99#endif
100}
101
Olof Johanssona85b9422007-09-15 13:40:59 -0700102static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg,
103 unsigned int val)
104{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700105 out_le32(mac->iob_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700106}
107
108static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
109{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700110 return in_le32(mac->regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700111}
112
113static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
114 unsigned int val)
115{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700116 out_le32(mac->regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700117}
118
119static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg)
120{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700121 return in_le32(mac->dma_regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700122}
123
124static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg,
125 unsigned int val)
126{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700127 out_le32(mac->dma_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700128}
129
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600130static int pasemi_get_mac_addr(struct pasemi_mac *mac)
131{
132 struct pci_dev *pdev = mac->pdev;
133 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500134 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600135 const u8 *maddr;
136 u8 addr[6];
137
138 if (!dn) {
139 dev_dbg(&pdev->dev,
140 "No device node for mac, not configuring\n");
141 return -ENOENT;
142 }
143
olof@lixom.net1af7f052007-05-12 14:57:46 -0500144 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500145
olof@lixom.net1af7f052007-05-12 14:57:46 -0500146 if (maddr && len == 6) {
147 memcpy(mac->mac_addr, maddr, 6);
148 return 0;
149 }
150
151 /* Some old versions of firmware mistakenly uses mac-address
152 * (and as a string) instead of a byte array in local-mac-address.
153 */
154
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500155 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700156 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500157
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600158 if (maddr == NULL) {
159 dev_warn(&pdev->dev,
160 "no mac address in device tree, not configuring\n");
161 return -ENOENT;
162 }
163
olof@lixom.net1af7f052007-05-12 14:57:46 -0500164
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600165 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
166 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
167 dev_warn(&pdev->dev,
168 "can't parse mac address, not configuring\n");
169 return -EINVAL;
170 }
171
olof@lixom.net1af7f052007-05-12 14:57:46 -0500172 memcpy(mac->mac_addr, addr, 6);
173
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600174 return 0;
175}
176
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500177static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
178 struct sk_buff *skb,
179 dma_addr_t *dmas)
180{
181 int f;
182 int nfrags = skb_shinfo(skb)->nr_frags;
183
184 pci_unmap_single(mac->dma_pdev, dmas[0], skb_headlen(skb),
185 PCI_DMA_TODEVICE);
186
187 for (f = 0; f < nfrags; f++) {
188 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
189
190 pci_unmap_page(mac->dma_pdev, dmas[f+1], frag->size,
191 PCI_DMA_TODEVICE);
192 }
193 dev_kfree_skb_irq(skb);
194
195 /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
196 * aligned up to a power of 2
197 */
198 return (nfrags + 3) & ~1;
199}
200
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600201static int pasemi_mac_setup_rx_resources(struct net_device *dev)
202{
203 struct pasemi_mac_rxring *ring;
204 struct pasemi_mac *mac = netdev_priv(dev);
205 int chan_id = mac->dma_rxch;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500206 unsigned int cfg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600207
208 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
209
210 if (!ring)
211 goto out_ring;
212
213 spin_lock_init(&ring->lock);
214
Olof Johansson021fa222007-08-22 09:13:11 -0500215 ring->size = RX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500216 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600217 RX_RING_SIZE, GFP_KERNEL);
218
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500219 if (!ring->ring_info)
220 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600221
222 /* Allocate descriptors */
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500223 ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
224 RX_RING_SIZE * sizeof(u64),
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600225 &ring->dma, GFP_KERNEL);
226
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500227 if (!ring->ring)
228 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600229
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500230 memset(ring->ring, 0, RX_RING_SIZE * sizeof(u64));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600231
232 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
233 RX_RING_SIZE * sizeof(u64),
234 &ring->buf_dma, GFP_KERNEL);
235 if (!ring->buffers)
236 goto out_buffers;
237
238 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
239
Olof Johanssona85b9422007-09-15 13:40:59 -0700240 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEL(chan_id), PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600241
Olof Johanssona85b9422007-09-15 13:40:59 -0700242 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEU(chan_id),
243 PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500244 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600245
Olof Johanssonaf289e82007-10-03 13:03:54 -0500246 cfg = PAS_DMA_RXCHAN_CFG_HBU(2);
247
248 if (translation_enabled())
249 cfg |= PAS_DMA_RXCHAN_CFG_CTR;
250
251 write_dma_reg(mac, PAS_DMA_RXCHAN_CFG(chan_id), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600252
Olof Johanssona85b9422007-09-15 13:40:59 -0700253 write_dma_reg(mac, PAS_DMA_RXINT_BASEL(mac->dma_if),
Olof Johanssonaf289e82007-10-03 13:03:54 -0500254 PAS_DMA_RXINT_BASEL_BRBL(ring->buf_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600255
Olof Johanssona85b9422007-09-15 13:40:59 -0700256 write_dma_reg(mac, PAS_DMA_RXINT_BASEU(mac->dma_if),
Olof Johanssonaf289e82007-10-03 13:03:54 -0500257 PAS_DMA_RXINT_BASEU_BRBH(ring->buf_dma >> 32) |
Olof Johanssona85b9422007-09-15 13:40:59 -0700258 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600259
Olof Johanssonaf289e82007-10-03 13:03:54 -0500260 cfg = PAS_DMA_RXINT_CFG_DHL(3) | PAS_DMA_RXINT_CFG_L2 |
261 PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP |
262 PAS_DMA_RXINT_CFG_HEN;
263
264 if (translation_enabled())
265 cfg |= PAS_DMA_RXINT_CFG_ITRR | PAS_DMA_RXINT_CFG_ITR;
266
267 write_dma_reg(mac, PAS_DMA_RXINT_CFG(mac->dma_if), cfg);
Olof Johanssonc0efd522007-08-22 09:12:52 -0500268
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600269 ring->next_to_fill = 0;
270 ring->next_to_clean = 0;
271
272 snprintf(ring->irq_name, sizeof(ring->irq_name),
273 "%s rx", dev->name);
274 mac->rx = ring;
275
276 return 0;
277
278out_buffers:
279 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500280 RX_RING_SIZE * sizeof(u64),
281 mac->rx->ring, mac->rx->dma);
282out_ring_desc:
283 kfree(ring->ring_info);
284out_ring_info:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600285 kfree(ring);
286out_ring:
287 return -ENOMEM;
288}
289
290
291static int pasemi_mac_setup_tx_resources(struct net_device *dev)
292{
293 struct pasemi_mac *mac = netdev_priv(dev);
294 u32 val;
295 int chan_id = mac->dma_txch;
296 struct pasemi_mac_txring *ring;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500297 unsigned int cfg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600298
299 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
300 if (!ring)
301 goto out_ring;
302
303 spin_lock_init(&ring->lock);
304
Olof Johansson021fa222007-08-22 09:13:11 -0500305 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500306 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600307 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500308 if (!ring->ring_info)
309 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600310
311 /* Allocate descriptors */
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500312 ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
313 TX_RING_SIZE * sizeof(u64),
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600314 &ring->dma, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500315 if (!ring->ring)
316 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600317
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500318 memset(ring->ring, 0, TX_RING_SIZE * sizeof(u64));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600319
Olof Johanssona85b9422007-09-15 13:40:59 -0700320 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id),
321 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600322 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500323 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600324
Olof Johanssona85b9422007-09-15 13:40:59 -0700325 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600326
Olof Johanssonaf289e82007-10-03 13:03:54 -0500327 cfg = PAS_DMA_TXCHAN_CFG_TY_IFACE |
328 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
329 PAS_DMA_TXCHAN_CFG_UP |
330 PAS_DMA_TXCHAN_CFG_WT(2);
331
332 if (translation_enabled())
333 cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
334
335 write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(chan_id), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600336
Olof Johansson021fa222007-08-22 09:13:11 -0500337 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600338 ring->next_to_clean = 0;
339
340 snprintf(ring->irq_name, sizeof(ring->irq_name),
341 "%s tx", dev->name);
342 mac->tx = ring;
343
344 return 0;
345
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500346out_ring_desc:
347 kfree(ring->ring_info);
348out_ring_info:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600349 kfree(ring);
350out_ring:
351 return -ENOMEM;
352}
353
354static void pasemi_mac_free_tx_resources(struct net_device *dev)
355{
356 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500357 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600358 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500359 dma_addr_t dmas[MAX_SKB_FRAGS+1];
360 int freed;
Olof Johanssonad5da102007-10-02 16:27:15 -0500361 int start, limit;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600362
Olof Johanssonad5da102007-10-02 16:27:15 -0500363 start = mac->tx->next_to_clean;
364 limit = mac->tx->next_to_fill;
365
366 /* Compensate for when fill has wrapped and clean has not */
367 if (start > limit)
368 limit += TX_RING_SIZE;
369
370 for (i = start; i < limit; i += freed) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500371 info = &TX_RING_INFO(mac, i+1);
372 if (info->dma && info->skb) {
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500373 for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
374 dmas[j] = TX_RING_INFO(mac, i+1+j).dma;
375 freed = pasemi_mac_unmap_tx_skb(mac, info->skb, dmas);
376 } else
377 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600378 }
379
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500380 for (i = 0; i < TX_RING_SIZE; i++)
381 TX_RING(mac, i) = 0;
382
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600383 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500384 TX_RING_SIZE * sizeof(u64),
385 mac->tx->ring, mac->tx->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600386
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500387 kfree(mac->tx->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600388 kfree(mac->tx);
389 mac->tx = NULL;
390}
391
392static void pasemi_mac_free_rx_resources(struct net_device *dev)
393{
394 struct pasemi_mac *mac = netdev_priv(dev);
395 unsigned int i;
396 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600397
398 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500399 info = &RX_RING_INFO(mac, i);
400 if (info->skb && info->dma) {
401 pci_unmap_single(mac->dma_pdev,
402 info->dma,
403 info->skb->len,
404 PCI_DMA_FROMDEVICE);
405 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600406 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500407 info->dma = 0;
408 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600409 }
410
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500411 for (i = 0; i < RX_RING_SIZE; i++)
412 RX_RING(mac, i) = 0;
413
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600414 dma_free_coherent(&mac->dma_pdev->dev,
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500415 RX_RING_SIZE * sizeof(u64),
416 mac->rx->ring, mac->rx->dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600417
418 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
419 mac->rx->buffers, mac->rx->buf_dma);
420
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500421 kfree(mac->rx->ring_info);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600422 kfree(mac->rx);
423 mac->rx = NULL;
424}
425
Olof Johansson928773c2007-09-26 16:25:06 -0500426static void pasemi_mac_replenish_rx_ring(struct net_device *dev, int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600427{
428 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500429 int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600430
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500431 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600432 return;
433
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500434 fill = mac->rx->next_to_fill;
Olof Johansson928773c2007-09-26 16:25:06 -0500435 for (count = 0; count < limit; count++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500436 struct pasemi_mac_buffer *info = &RX_RING_INFO(mac, fill);
437 u64 *buff = &RX_BUFF(mac, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600438 struct sk_buff *skb;
439 dma_addr_t dma;
440
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500441 /* Entry in use? */
442 WARN_ON(*buff);
443
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500444 /* skb might still be in there for recycle on short receives */
445 if (info->skb)
446 skb = info->skb;
Olof Johansson8dc121a2007-10-02 16:26:53 -0500447 else {
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500448 skb = dev_alloc_skb(BUF_SIZE);
Olof Johansson8dc121a2007-10-02 16:26:53 -0500449 skb_reserve(skb, LOCAL_SKB_ALIGN);
450 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600451
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500452 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600453 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600454
Olof Johansson8dc121a2007-10-02 16:26:53 -0500455 dma = pci_map_single(mac->dma_pdev, skb->data,
456 BUF_SIZE - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600457 PCI_DMA_FROMDEVICE);
458
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500459 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600460 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600461 break;
462 }
463
464 info->skb = skb;
465 info->dma = dma;
466 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500467 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600468 }
469
470 wmb();
471
Olof Johansson928773c2007-09-26 16:25:06 -0500472 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600473
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500474 mac->rx->next_to_fill = (mac->rx->next_to_fill + count) &
475 (RX_RING_SIZE - 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600476}
477
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500478static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
479{
Olof Johansson52a94352007-05-12 18:01:09 -0500480 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500481 /* Re-enable packet count interrupts: finally
482 * ack the packet count interrupt we got in rx_intr.
483 */
484
Olof Johansson52a94352007-05-12 18:01:09 -0500485 pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500486
Olof Johansson52a94352007-05-12 18:01:09 -0500487 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500488
Olof Johanssona85b9422007-09-15 13:40:59 -0700489 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500490}
491
492static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
493{
Olof Johansson52a94352007-05-12 18:01:09 -0500494 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500495
496 /* Re-enable packet count interrupts */
Olof Johansson52a94352007-05-12 18:01:09 -0500497 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500498
Olof Johansson52a94352007-05-12 18:01:09 -0500499 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500500
Olof Johanssona85b9422007-09-15 13:40:59 -0700501 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500502}
503
504
Olof Johansson69c29d82007-10-02 16:24:51 -0500505static inline void pasemi_mac_rx_error(struct pasemi_mac *mac, u64 macrx)
506{
507 unsigned int rcmdsta, ccmdsta;
508
509 if (!netif_msg_rx_err(mac))
510 return;
511
512 rcmdsta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
513 ccmdsta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
514
515 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
516 macrx, *mac->rx_status);
517
518 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
519 rcmdsta, ccmdsta);
520}
521
522static inline void pasemi_mac_tx_error(struct pasemi_mac *mac, u64 mactx)
523{
524 unsigned int cmdsta;
525
526 if (!netif_msg_tx_err(mac))
527 return;
528
529 cmdsta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
530
531 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
532 "tx status 0x%016lx\n", mactx, *mac->tx_status);
533
534 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
535}
536
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600537static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
538{
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500539 unsigned int n;
540 int count;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500541 struct pasemi_mac_buffer *info;
542 struct sk_buff *skb;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500543 unsigned int len;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500544 u64 macrx;
545 dma_addr_t dma;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500546 int buf_index;
547 u64 eval;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600548
549 spin_lock(&mac->rx->lock);
550
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500551 n = mac->rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600552
Olof Johanssonde052382007-10-20 14:10:03 -0500553 prefetch(&RX_RING(mac, n));
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500554
555 for (count = 0; count < limit; count++) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500556 macrx = RX_RING(mac, n);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600557
Olof Johansson69c29d82007-10-02 16:24:51 -0500558 if ((macrx & XCT_MACRX_E) ||
559 (*mac->rx_status & PAS_STATUS_ERROR))
560 pasemi_mac_rx_error(mac, macrx);
561
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500562 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600563 break;
564
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600565 info = NULL;
566
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500567 BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600568
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500569 eval = (RX_RING(mac, n+1) & XCT_RXRES_8B_EVAL_M) >>
570 XCT_RXRES_8B_EVAL_S;
571 buf_index = eval-1;
572
573 dma = (RX_RING(mac, n+2) & XCT_PTR_ADDR_M);
574 info = &RX_RING_INFO(mac, buf_index);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500575
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500576 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600577
Olof Johanssonad5da102007-10-02 16:27:15 -0500578 prefetch(skb);
579 prefetch(&skb->data_len);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600580
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500581 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600582
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500583 if (len < 256) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500584 struct sk_buff *new_skb;
585
586 new_skb = netdev_alloc_skb(mac->netdev,
587 len + LOCAL_SKB_ALIGN);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500588 if (new_skb) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500589 skb_reserve(new_skb, LOCAL_SKB_ALIGN);
Olof Johansson73344862007-08-22 09:12:55 -0500590 memcpy(new_skb->data, skb->data, len);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500591 /* save the skb in buffer_info as good */
592 skb = new_skb;
593 }
594 /* else just continue with the old one */
595 } else
596 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600597
Olof Johanssonad5da102007-10-02 16:27:15 -0500598 pci_unmap_single(mac->dma_pdev, dma, len, PCI_DMA_FROMDEVICE);
599
600 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500601
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600602 skb_put(skb, len);
603
Olof Johansson26fcfa92007-08-22 09:12:59 -0500604 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500605 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500606 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600607 XCT_MACRX_CSUM_S;
608 } else
609 skb->ip_summed = CHECKSUM_NONE;
610
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700611 mac->netdev->stats.rx_bytes += len;
612 mac->netdev->stats.rx_packets++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600613
Olof Johansson26fcfa92007-08-22 09:12:59 -0500614 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600615 netif_receive_skb(skb);
616
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500617 RX_RING(mac, n) = 0;
618 RX_RING(mac, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500619
Olof Johanssonad5da102007-10-02 16:27:15 -0500620 /* Need to zero it out since hardware doesn't, since the
621 * replenish loop uses it to tell when it's done.
622 */
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500623 RX_BUFF(mac, buf_index) = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500624
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500625 n += 4;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600626 }
627
Olof Johansson9a50beb2007-10-02 16:26:30 -0500628 if (n > RX_RING_SIZE) {
629 /* Errata 5971 workaround: L2 target of headers */
630 write_iob_reg(mac, PAS_IOB_COM_PKTHDRCNT, 0);
631 n &= (RX_RING_SIZE-1);
632 }
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500633
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500634 mac->rx->next_to_clean = n;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500635
636 /* Increase is in number of 16-byte entries, and since each descriptor
637 * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
638 * count*2.
639 */
640 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), count << 1);
641
642 pasemi_mac_replenish_rx_ring(mac->netdev, count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600643
644 spin_unlock(&mac->rx->lock);
645
646 return count;
647}
648
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500649/* Can't make this too large or we blow the kernel stack limits */
650#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
651
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600652static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
653{
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500654 int i, j;
Olof Johanssonad5da102007-10-02 16:27:15 -0500655 unsigned int start, descr_count, buf_count, batch_limit;
656 unsigned int ring_limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500657 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500658 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500659 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
660 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600661
Olof Johansson02df6cf2007-08-22 09:13:03 -0500662 total_count = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500663 batch_limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500664restart:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600665 spin_lock_irqsave(&mac->tx->lock, flags);
666
667 start = mac->tx->next_to_clean;
Olof Johanssonad5da102007-10-02 16:27:15 -0500668 ring_limit = mac->tx->next_to_fill;
669
670 /* Compensate for when fill has wrapped but clean has not */
671 if (start > ring_limit)
672 ring_limit += TX_RING_SIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500673
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500674 buf_count = 0;
675 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600676
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500677 for (i = start;
Olof Johanssonad5da102007-10-02 16:27:15 -0500678 descr_count < batch_limit && i < ring_limit;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500679 i += buf_count) {
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500680 u64 mactx = TX_RING(mac, i);
Olof Johanssonad5da102007-10-02 16:27:15 -0500681 struct sk_buff *skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500682
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500683 if ((mactx & XCT_MACTX_E) ||
Olof Johansson69c29d82007-10-02 16:24:51 -0500684 (*mac->tx_status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500685 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500686
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500687 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500688 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600689 break;
690
Olof Johanssonad5da102007-10-02 16:27:15 -0500691 skb = TX_RING_INFO(mac, i+1).skb;
692 skbs[descr_count] = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500693
Olof Johanssonad5da102007-10-02 16:27:15 -0500694 buf_count = 2 + skb_shinfo(skb)->nr_frags;
695 for (j = 0; j <= skb_shinfo(skb)->nr_frags; j++)
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500696 dmas[descr_count][j] = TX_RING_INFO(mac, i+1+j).dma;
697
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500698 TX_RING(mac, i) = 0;
699 TX_RING(mac, i+1) = 0;
700
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500701 /* Since we always fill with an even number of entries, make
702 * sure we skip any unused one at the end as well.
703 */
704 if (buf_count & 1)
705 buf_count++;
706 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600707 }
Olof Johanssonad5da102007-10-02 16:27:15 -0500708 mac->tx->next_to_clean = i & (TX_RING_SIZE-1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500709
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600710 spin_unlock_irqrestore(&mac->tx->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500711 netif_wake_queue(mac->netdev);
712
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500713 for (i = 0; i < descr_count; i++)
714 pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500715
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500716 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500717
718 /* If the batch was full, try to clean more */
Olof Johanssonad5da102007-10-02 16:27:15 -0500719 if (descr_count == batch_limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500720 goto restart;
721
722 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600723}
724
725
726static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
727{
728 struct net_device *dev = data;
729 struct pasemi_mac *mac = netdev_priv(dev);
730 unsigned int reg;
731
Olof Johansson6dfa7522007-05-08 00:47:32 -0500732 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600733 return IRQ_NONE;
734
Olof Johansson6dfa7522007-05-08 00:47:32 -0500735 /* Don't reset packet count so it won't fire again but clear
736 * all others.
737 */
738
Olof Johansson6dfa7522007-05-08 00:47:32 -0500739 reg = 0;
740 if (*mac->rx_status & PAS_STATUS_SOFT)
741 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
742 if (*mac->rx_status & PAS_STATUS_ERROR)
743 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600744 if (*mac->rx_status & PAS_STATUS_TIMER)
745 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
746
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700747 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500748
Olof Johanssona85b9422007-09-15 13:40:59 -0700749 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600750
751 return IRQ_HANDLED;
752}
753
754static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
755{
756 struct net_device *dev = data;
757 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson52a94352007-05-12 18:01:09 -0500758 unsigned int reg, pcnt;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600759
Olof Johansson6dfa7522007-05-08 00:47:32 -0500760 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600761 return IRQ_NONE;
762
763 pasemi_mac_clean_tx(mac);
764
Olof Johansson52a94352007-05-12 18:01:09 -0500765 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
766
767 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500768
769 if (*mac->tx_status & PAS_STATUS_SOFT)
770 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
771 if (*mac->tx_status & PAS_STATUS_ERROR)
772 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600773
Olof Johanssona85b9422007-09-15 13:40:59 -0700774 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600775
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600776 return IRQ_HANDLED;
777}
778
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500779static void pasemi_adjust_link(struct net_device *dev)
780{
781 struct pasemi_mac *mac = netdev_priv(dev);
782 int msg;
783 unsigned int flags;
784 unsigned int new_flags;
785
786 if (!mac->phydev->link) {
787 /* If no link, MAC speed settings don't matter. Just report
788 * link down and return.
789 */
790 if (mac->link && netif_msg_link(mac))
791 printk(KERN_INFO "%s: Link is down.\n", dev->name);
792
793 netif_carrier_off(dev);
794 mac->link = 0;
795
796 return;
797 } else
798 netif_carrier_on(dev);
799
Olof Johanssona85b9422007-09-15 13:40:59 -0700800 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500801 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
802 PAS_MAC_CFG_PCFG_TSR_M);
803
804 if (!mac->phydev->duplex)
805 new_flags |= PAS_MAC_CFG_PCFG_HD;
806
807 switch (mac->phydev->speed) {
808 case 1000:
809 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
810 PAS_MAC_CFG_PCFG_TSR_1G;
811 break;
812 case 100:
813 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
814 PAS_MAC_CFG_PCFG_TSR_100M;
815 break;
816 case 10:
817 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
818 PAS_MAC_CFG_PCFG_TSR_10M;
819 break;
820 default:
821 printk("Unsupported speed %d\n", mac->phydev->speed);
822 }
823
824 /* Print on link or speed/duplex change */
825 msg = mac->link != mac->phydev->link || flags != new_flags;
826
827 mac->duplex = mac->phydev->duplex;
828 mac->speed = mac->phydev->speed;
829 mac->link = mac->phydev->link;
830
831 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700832 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500833
834 if (msg && netif_msg_link(mac))
835 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
836 dev->name, mac->speed, mac->duplex ? "full" : "half");
837}
838
839static int pasemi_mac_phy_init(struct net_device *dev)
840{
841 struct pasemi_mac *mac = netdev_priv(dev);
842 struct device_node *dn, *phy_dn;
843 struct phy_device *phydev;
844 unsigned int phy_id;
845 const phandle *ph;
846 const unsigned int *prop;
847 struct resource r;
848 int ret;
849
850 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700851 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500852 if (!ph)
853 return -ENODEV;
854 phy_dn = of_find_node_by_phandle(*ph);
855
Linus Torvalds90287802007-05-08 11:57:17 -0700856 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500857 ret = of_address_to_resource(phy_dn->parent, 0, &r);
858 if (ret)
859 goto err;
860
861 phy_id = *prop;
862 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
863
864 of_node_put(phy_dn);
865
866 mac->link = 0;
867 mac->speed = 0;
868 mac->duplex = -1;
869
870 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
871
872 if (IS_ERR(phydev)) {
873 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
874 return PTR_ERR(phydev);
875 }
876
877 mac->phydev = phydev;
878
879 return 0;
880
881err:
882 of_node_put(phy_dn);
883 return -ENODEV;
884}
885
886
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600887static int pasemi_mac_open(struct net_device *dev)
888{
889 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500890 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600891 unsigned int flags;
892 int ret;
893
894 /* enable rx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700895 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600896
897 /* enable tx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700898 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600899
900 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
901 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
902 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
903
Olof Johanssona85b9422007-09-15 13:40:59 -0700904 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600905
Olof Johanssona85b9422007-09-15 13:40:59 -0700906 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
907 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600908
Olof Johanssona85b9422007-09-15 13:40:59 -0700909 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
Olof Johansson02df6cf2007-08-22 09:13:03 -0500910 PAS_IOB_DMA_TXCH_CFG_CNTTH(128));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600911
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500912 /* Clear out any residual packet count state from firmware */
913 pasemi_mac_restart_rx_intr(mac);
914 pasemi_mac_restart_tx_intr(mac);
915
Olof Johansson6dfa7522007-05-08 00:47:32 -0500916 /* 0xffffff is max value, about 16ms */
Olof Johanssona85b9422007-09-15 13:40:59 -0700917 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
918 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600919
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600920 ret = pasemi_mac_setup_rx_resources(dev);
921 if (ret)
922 goto out_rx_resources;
923
924 ret = pasemi_mac_setup_tx_resources(dev);
925 if (ret)
926 goto out_tx_resources;
927
Olof Johanssona85b9422007-09-15 13:40:59 -0700928 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
929 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
930 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600931
932 /* enable rx if */
Olof Johanssona85b9422007-09-15 13:40:59 -0700933 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
Olof Johansson9e81d332007-10-02 16:27:39 -0500934 PAS_DMA_RXINT_RCMDSTA_EN |
935 PAS_DMA_RXINT_RCMDSTA_DROPS_M |
936 PAS_DMA_RXINT_RCMDSTA_BP |
937 PAS_DMA_RXINT_RCMDSTA_OO |
938 PAS_DMA_RXINT_RCMDSTA_BT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600939
940 /* enable rx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700941 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
942 PAS_DMA_RXCHAN_CCMDSTA_EN |
Olof Johansson9e81d332007-10-02 16:27:39 -0500943 PAS_DMA_RXCHAN_CCMDSTA_DU |
944 PAS_DMA_RXCHAN_CCMDSTA_OD |
945 PAS_DMA_RXCHAN_CCMDSTA_FD |
946 PAS_DMA_RXCHAN_CCMDSTA_DT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600947
948 /* enable tx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700949 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
Olof Johansson9e81d332007-10-02 16:27:39 -0500950 PAS_DMA_TXCHAN_TCMDSTA_EN |
951 PAS_DMA_TXCHAN_TCMDSTA_SZ |
952 PAS_DMA_TXCHAN_TCMDSTA_DB |
953 PAS_DMA_TXCHAN_TCMDSTA_DE |
954 PAS_DMA_TXCHAN_TCMDSTA_DA);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600955
Olof Johansson928773c2007-09-26 16:25:06 -0500956 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600957
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500958 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), RX_RING_SIZE>>1);
959
Olof Johansson36033762007-09-26 16:24:42 -0500960 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
961 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
962
963 if (mac->type == MAC_TYPE_GMAC)
964 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
965 else
966 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
967
968 /* Enable interface in MAC */
969 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
970
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500971 ret = pasemi_mac_phy_init(dev);
972 /* Some configs don't have PHYs (XAUI etc), so don't complain about
973 * failed init due to -ENODEV.
974 */
975 if (ret && ret != -ENODEV)
976 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
977
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600978 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700979 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600980
Olof Johansson771f7402007-05-08 00:47:21 -0500981 /* Interrupts are a bit different for our DMA controller: While
982 * it's got one a regular PCI device header, the interrupt there
983 * is really the base of the range it's using. Each tx and rx
984 * channel has it's own interrupt source.
985 */
986
987 base_irq = virq_to_hw(mac->dma_pdev->irq);
988
989 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
990 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
991
992 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600993 mac->tx->irq_name, dev);
994 if (ret) {
995 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500996 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600997 goto out_tx_int;
998 }
999
Olof Johansson771f7402007-05-08 00:47:21 -05001000 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001001 mac->rx->irq_name, dev);
1002 if (ret) {
1003 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -05001004 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001005 goto out_rx_int;
1006 }
1007
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001008 if (mac->phydev)
1009 phy_start(mac->phydev);
1010
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001011 return 0;
1012
1013out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -05001014 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001015out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001016 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001017 netif_stop_queue(dev);
1018 pasemi_mac_free_tx_resources(dev);
1019out_tx_resources:
1020 pasemi_mac_free_rx_resources(dev);
1021out_rx_resources:
1022
1023 return ret;
1024}
1025
1026#define MAX_RETRIES 5000
1027
1028static int pasemi_mac_close(struct net_device *dev)
1029{
1030 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson9e81d332007-10-02 16:27:39 -05001031 unsigned int sta;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001032 int retries;
1033
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001034 if (mac->phydev) {
1035 phy_stop(mac->phydev);
1036 phy_disconnect(mac->phydev);
1037 }
1038
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001039 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001040 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001041
Olof Johansson9e81d332007-10-02 16:27:39 -05001042 sta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1043 if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
1044 PAS_DMA_RXINT_RCMDSTA_OO |
1045 PAS_DMA_RXINT_RCMDSTA_BT))
1046 printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);
1047
1048 sta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
1049 if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
1050 PAS_DMA_RXCHAN_CCMDSTA_OD |
1051 PAS_DMA_RXCHAN_CCMDSTA_FD |
1052 PAS_DMA_RXCHAN_CCMDSTA_DT))
1053 printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);
1054
1055 sta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
1056 if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ |
1057 PAS_DMA_TXCHAN_TCMDSTA_DB |
1058 PAS_DMA_TXCHAN_TCMDSTA_DE |
1059 PAS_DMA_TXCHAN_TCMDSTA_DA))
1060 printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);
1061
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001062 /* Clean out any pending buffers */
1063 pasemi_mac_clean_tx(mac);
1064 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
1065
1066 /* Disable interface */
Olof Johanssona85b9422007-09-15 13:40:59 -07001067 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST);
1068 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST);
1069 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001070
1071 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson9e81d332007-10-02 16:27:39 -05001072 sta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
1073 if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001074 break;
1075 cond_resched();
1076 }
1077
Olof Johansson9e81d332007-10-02 16:27:39 -05001078 if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001079 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001080
1081 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson9e81d332007-10-02 16:27:39 -05001082 sta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
1083 if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001084 break;
1085 cond_resched();
1086 }
1087
Olof Johansson9e81d332007-10-02 16:27:39 -05001088 if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001089 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001090
1091 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson9e81d332007-10-02 16:27:39 -05001092 sta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1093 if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001094 break;
1095 cond_resched();
1096 }
1097
Olof Johansson9e81d332007-10-02 16:27:39 -05001098 if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001099 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001100
1101 /* Then, disable the channel. This must be done separately from
1102 * stopping, since you can't disable when active.
1103 */
1104
Olof Johanssona85b9422007-09-15 13:40:59 -07001105 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
1106 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
1107 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001108
Olof Johansson771f7402007-05-08 00:47:21 -05001109 free_irq(mac->tx_irq, dev);
1110 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001111
1112 /* Free resources */
1113 pasemi_mac_free_rx_resources(dev);
1114 pasemi_mac_free_tx_resources(dev);
1115
1116 return 0;
1117}
1118
1119static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1120{
1121 struct pasemi_mac *mac = netdev_priv(dev);
1122 struct pasemi_mac_txring *txring;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001123 u64 dflags, mactx;
1124 dma_addr_t map[MAX_SKB_FRAGS+1];
1125 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001126 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001127 int i, nfrags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001128
Olof Johanssondbd62af2007-11-06 22:20:39 -06001129 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001130
1131 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001132 const unsigned char *nh = skb_network_header(skb);
1133
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001134 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001135 case IPPROTO_TCP:
1136 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001137 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001138 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001139 break;
1140 case IPPROTO_UDP:
1141 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001142 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001143 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001144 break;
1145 }
1146 }
1147
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001148 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001149
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001150 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1151 PCI_DMA_TODEVICE);
1152 map_size[0] = skb_headlen(skb);
1153 if (dma_mapping_error(map[0]))
1154 goto out_err_nolock;
1155
1156 for (i = 0; i < nfrags; i++) {
1157 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1158
1159 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1160 frag->page_offset, frag->size,
1161 PCI_DMA_TODEVICE);
1162 map_size[i+1] = frag->size;
1163 if (dma_mapping_error(map[i+1])) {
1164 nfrags = i;
1165 goto out_err_nolock;
1166 }
1167 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001168
Olof Johansson26fcfa92007-08-22 09:12:59 -05001169 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001170
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001171 txring = mac->tx;
1172
1173 spin_lock_irqsave(&txring->lock, flags);
1174
Olof Johanssonad5da102007-10-02 16:27:15 -05001175 /* Avoid stepping on the same cache line that the DMA controller
1176 * is currently about to send, so leave at least 8 words available.
1177 * Total free space needed is mactx + fragments + 8
1178 */
1179 if (RING_AVAIL(txring) < nfrags + 10) {
1180 /* no room -- stop the queue and wait for tx intr */
1181 netif_stop_queue(dev);
1182 goto out_err;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001183 }
1184
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001185 TX_RING(mac, txring->next_to_fill) = mactx;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001186 txring->next_to_fill++;
1187 TX_RING_INFO(mac, txring->next_to_fill).skb = skb;
1188 for (i = 0; i <= nfrags; i++) {
1189 TX_RING(mac, txring->next_to_fill+i) =
1190 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
1191 TX_RING_INFO(mac, txring->next_to_fill+i).dma = map[i];
1192 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001193
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001194 /* We have to add an even number of 8-byte entries to the ring
1195 * even if the last one is unused. That means always an odd number
1196 * of pointers + one mactx descriptor.
1197 */
1198 if (nfrags & 1)
1199 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001200
Olof Johanssonad5da102007-10-02 16:27:15 -05001201 txring->next_to_fill = (txring->next_to_fill + nfrags + 1) &
1202 (TX_RING_SIZE-1);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001203
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001204 dev->stats.tx_packets++;
1205 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001206
1207 spin_unlock_irqrestore(&txring->lock, flags);
1208
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001209 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001210
1211 return NETDEV_TX_OK;
1212
1213out_err:
1214 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001215out_err_nolock:
1216 while (nfrags--)
1217 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1218 PCI_DMA_TODEVICE);
1219
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001220 return NETDEV_TX_BUSY;
1221}
1222
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001223static void pasemi_mac_set_rx_mode(struct net_device *dev)
1224{
1225 struct pasemi_mac *mac = netdev_priv(dev);
1226 unsigned int flags;
1227
Olof Johanssona85b9422007-09-15 13:40:59 -07001228 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001229
1230 /* Set promiscuous */
1231 if (dev->flags & IFF_PROMISC)
1232 flags |= PAS_MAC_CFG_PCFG_PR;
1233 else
1234 flags &= ~PAS_MAC_CFG_PCFG_PR;
1235
Olof Johanssona85b9422007-09-15 13:40:59 -07001236 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001237}
1238
1239
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001240static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001241{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001242 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1243 struct net_device *dev = mac->netdev;
1244 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001245
Olof Johansson829185e2007-09-15 13:53:19 -07001246 pasemi_mac_clean_tx(mac);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001247 pkts = pasemi_mac_clean_rx(mac, budget);
1248 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001249 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001250 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001251
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001252 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001253 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001254 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001255}
1256
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001257static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
1258{
1259 struct device_node *dn;
1260 void __iomem *ret;
1261
1262 dn = pci_device_to_OF_node(p);
1263 if (!dn)
1264 goto fallback;
1265
1266 ret = of_iomap(dn, index);
1267 if (!ret)
1268 goto fallback;
1269
1270 return ret;
1271fallback:
1272 /* This is hardcoded and ugly, but we have some firmware versions
1273 * that don't provide the register space in the device tree. Luckily
1274 * they are at well-known locations so we can just do the math here.
1275 */
1276 return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
1277}
1278
1279static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
1280{
1281 struct resource res;
1282 struct device_node *dn;
1283 int err;
1284
1285 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1286 if (!mac->dma_pdev) {
1287 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1288 return -ENODEV;
1289 }
1290
1291 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1292 if (!mac->iob_pdev) {
1293 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1294 return -ENODEV;
1295 }
1296
1297 mac->regs = map_onedev(mac->pdev, 0);
1298 mac->dma_regs = map_onedev(mac->dma_pdev, 0);
1299 mac->iob_regs = map_onedev(mac->iob_pdev, 0);
1300
1301 if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
1302 dev_err(&mac->pdev->dev, "Can't map registers\n");
1303 return -ENODEV;
1304 }
1305
1306 /* The dma status structure is located in the I/O bridge, and
1307 * is cache coherent.
1308 */
1309 if (!dma_status) {
1310 dn = pci_device_to_OF_node(mac->iob_pdev);
1311 if (dn)
1312 err = of_address_to_resource(dn, 1, &res);
1313 if (!dn || err) {
1314 /* Fallback for old firmware */
1315 res.start = 0xfd800000;
1316 res.end = res.start + 0x1000;
1317 }
1318 dma_status = __ioremap(res.start, res.end-res.start, 0);
1319 }
1320
1321 return 0;
1322}
1323
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001324static int __devinit
1325pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1326{
1327 static int index = 0;
1328 struct net_device *dev;
1329 struct pasemi_mac *mac;
1330 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001331 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001332
1333 err = pci_enable_device(pdev);
1334 if (err)
1335 return err;
1336
1337 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1338 if (dev == NULL) {
1339 dev_err(&pdev->dev,
1340 "pasemi_mac: Could not allocate ethernet device.\n");
1341 err = -ENOMEM;
1342 goto out_disable_device;
1343 }
1344
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001345 pci_set_drvdata(pdev, dev);
1346 SET_NETDEV_DEV(dev, &pdev->dev);
1347
1348 mac = netdev_priv(dev);
1349
1350 mac->pdev = pdev;
1351 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001352
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001353 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1354
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001355 dev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX | NETIF_F_SG;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001356
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001357 /* These should come out of the device tree eventually */
1358 mac->dma_txch = index;
1359 mac->dma_rxch = index;
1360
1361 /* We probe GMAC before XAUI, but the DMA interfaces are
1362 * in XAUI, GMAC order.
1363 */
1364 if (index < 4)
1365 mac->dma_if = index + 2;
1366 else
1367 mac->dma_if = index - 4;
1368 index++;
1369
1370 switch (pdev->device) {
1371 case 0xa005:
1372 mac->type = MAC_TYPE_GMAC;
1373 break;
1374 case 0xa006:
1375 mac->type = MAC_TYPE_XAUI;
1376 break;
1377 default:
1378 err = -ENODEV;
1379 goto out;
1380 }
1381
1382 /* get mac addr from device tree */
1383 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1384 err = -ENODEV;
1385 goto out;
1386 }
1387 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1388
1389 dev->open = pasemi_mac_open;
1390 dev->stop = pasemi_mac_close;
1391 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001392 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001393
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001394 err = pasemi_mac_map_regs(mac);
1395 if (err)
1396 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001397
1398 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1399 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1400
Olof Johanssonceb51362007-05-08 00:47:49 -05001401 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1402
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001403 /* Enable most messages by default */
1404 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1405
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001406 err = register_netdev(dev);
1407
1408 if (err) {
1409 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1410 err);
1411 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001412 } else if netif_msg_probe(mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001413 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
Joe Perches0795af52007-10-03 17:59:30 -07001414 "hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001415 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1416 mac->dma_if, mac->dma_txch, mac->dma_rxch,
Joe Perches0795af52007-10-03 17:59:30 -07001417 print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001418
1419 return err;
1420
1421out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001422 if (mac->iob_pdev)
1423 pci_dev_put(mac->iob_pdev);
1424 if (mac->dma_pdev)
1425 pci_dev_put(mac->dma_pdev);
1426 if (mac->dma_regs)
1427 iounmap(mac->dma_regs);
1428 if (mac->iob_regs)
1429 iounmap(mac->iob_regs);
1430 if (mac->regs)
1431 iounmap(mac->regs);
1432
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001433 free_netdev(dev);
1434out_disable_device:
1435 pci_disable_device(pdev);
1436 return err;
1437
1438}
1439
1440static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1441{
1442 struct net_device *netdev = pci_get_drvdata(pdev);
1443 struct pasemi_mac *mac;
1444
1445 if (!netdev)
1446 return;
1447
1448 mac = netdev_priv(netdev);
1449
1450 unregister_netdev(netdev);
1451
1452 pci_disable_device(pdev);
1453 pci_dev_put(mac->dma_pdev);
1454 pci_dev_put(mac->iob_pdev);
1455
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001456 iounmap(mac->regs);
1457 iounmap(mac->dma_regs);
1458 iounmap(mac->iob_regs);
1459
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001460 pci_set_drvdata(pdev, NULL);
1461 free_netdev(netdev);
1462}
1463
1464static struct pci_device_id pasemi_mac_pci_tbl[] = {
1465 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1466 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001467 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001468};
1469
1470MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1471
1472static struct pci_driver pasemi_mac_driver = {
1473 .name = "pasemi_mac",
1474 .id_table = pasemi_mac_pci_tbl,
1475 .probe = pasemi_mac_probe,
1476 .remove = __devexit_p(pasemi_mac_remove),
1477};
1478
1479static void __exit pasemi_mac_cleanup_module(void)
1480{
1481 pci_unregister_driver(&pasemi_mac_driver);
1482 __iounmap(dma_status);
1483 dma_status = NULL;
1484}
1485
1486int pasemi_mac_init_module(void)
1487{
1488 return pci_register_driver(&pasemi_mac_driver);
1489}
1490
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001491module_init(pasemi_mac_init_module);
1492module_exit(pasemi_mac_cleanup_module);