blob: 0d80b9d48c266ac3d83e77f98cb98f46045a8378 [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
40
41/* TODO list
42 *
43 * - Get rid of pci_{read,write}_config(), map registers with ioremap
44 * for performance
45 * - PHY support
46 * - Multicast support
47 * - Large MTU support
48 * - Other performance improvements
49 */
50
51
52/* Must be a power of two */
53#define RX_RING_SIZE 512
54#define TX_RING_SIZE 512
55
Olof Johanssonceb51362007-05-08 00:47:49 -050056#define DEFAULT_MSG_ENABLE \
57 (NETIF_MSG_DRV | \
58 NETIF_MSG_PROBE | \
59 NETIF_MSG_LINK | \
60 NETIF_MSG_TIMER | \
61 NETIF_MSG_IFDOWN | \
62 NETIF_MSG_IFUP | \
63 NETIF_MSG_RX_ERR | \
64 NETIF_MSG_TX_ERR)
65
Olof Johanssonf5cd7872007-01-31 21:43:54 -060066#define TX_DESC(mac, num) ((mac)->tx->desc[(num) & (TX_RING_SIZE-1)])
67#define TX_DESC_INFO(mac, num) ((mac)->tx->desc_info[(num) & (TX_RING_SIZE-1)])
68#define RX_DESC(mac, num) ((mac)->rx->desc[(num) & (RX_RING_SIZE-1)])
69#define RX_DESC_INFO(mac, num) ((mac)->rx->desc_info[(num) & (RX_RING_SIZE-1)])
70#define RX_BUFF(mac, num) ((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)])
71
72#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
73
Olof Johanssonceb51362007-05-08 00:47:49 -050074MODULE_LICENSE("GPL");
75MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
76MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
77
78static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
79module_param(debug, int, 0);
80MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060081
82static struct pasdma_status *dma_status;
83
Olof Johanssona85b9422007-09-15 13:40:59 -070084static unsigned int read_iob_reg(struct pasemi_mac *mac, unsigned int reg)
85{
Olof Johanssonb6e05a12007-09-15 13:44:07 -070086 return in_le32(mac->iob_regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -070087}
88
89static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg,
90 unsigned int val)
91{
Olof Johanssonb6e05a12007-09-15 13:44:07 -070092 out_le32(mac->iob_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -070093}
94
95static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
96{
Olof Johanssonb6e05a12007-09-15 13:44:07 -070097 return in_le32(mac->regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -070098}
99
100static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
101 unsigned int val)
102{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700103 out_le32(mac->regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700104}
105
106static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg)
107{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700108 return in_le32(mac->dma_regs+reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700109}
110
111static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg,
112 unsigned int val)
113{
Olof Johanssonb6e05a12007-09-15 13:44:07 -0700114 out_le32(mac->dma_regs+reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700115}
116
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600117static int pasemi_get_mac_addr(struct pasemi_mac *mac)
118{
119 struct pci_dev *pdev = mac->pdev;
120 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500121 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600122 const u8 *maddr;
123 u8 addr[6];
124
125 if (!dn) {
126 dev_dbg(&pdev->dev,
127 "No device node for mac, not configuring\n");
128 return -ENOENT;
129 }
130
olof@lixom.net1af7f052007-05-12 14:57:46 -0500131 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500132
olof@lixom.net1af7f052007-05-12 14:57:46 -0500133 if (maddr && len == 6) {
134 memcpy(mac->mac_addr, maddr, 6);
135 return 0;
136 }
137
138 /* Some old versions of firmware mistakenly uses mac-address
139 * (and as a string) instead of a byte array in local-mac-address.
140 */
141
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500142 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700143 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500144
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600145 if (maddr == NULL) {
146 dev_warn(&pdev->dev,
147 "no mac address in device tree, not configuring\n");
148 return -ENOENT;
149 }
150
olof@lixom.net1af7f052007-05-12 14:57:46 -0500151
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600152 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
153 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
154 dev_warn(&pdev->dev,
155 "can't parse mac address, not configuring\n");
156 return -EINVAL;
157 }
158
olof@lixom.net1af7f052007-05-12 14:57:46 -0500159 memcpy(mac->mac_addr, addr, 6);
160
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600161 return 0;
162}
163
164static int pasemi_mac_setup_rx_resources(struct net_device *dev)
165{
166 struct pasemi_mac_rxring *ring;
167 struct pasemi_mac *mac = netdev_priv(dev);
168 int chan_id = mac->dma_rxch;
169
170 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
171
172 if (!ring)
173 goto out_ring;
174
175 spin_lock_init(&ring->lock);
176
177 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
178 RX_RING_SIZE, GFP_KERNEL);
179
180 if (!ring->desc_info)
181 goto out_desc_info;
182
183 /* Allocate descriptors */
184 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
185 RX_RING_SIZE *
186 sizeof(struct pas_dma_xct_descr),
187 &ring->dma, GFP_KERNEL);
188
189 if (!ring->desc)
190 goto out_desc;
191
192 memset(ring->desc, 0, RX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
193
194 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
195 RX_RING_SIZE * sizeof(u64),
196 &ring->buf_dma, GFP_KERNEL);
197 if (!ring->buffers)
198 goto out_buffers;
199
200 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
201
Olof Johanssona85b9422007-09-15 13:40:59 -0700202 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEL(chan_id), PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600203
Olof Johanssona85b9422007-09-15 13:40:59 -0700204 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEU(chan_id),
205 PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
206 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600207
Olof Johanssona85b9422007-09-15 13:40:59 -0700208 write_dma_reg(mac, PAS_DMA_RXCHAN_CFG(chan_id),
Olof Johanssonc0efd522007-08-22 09:12:52 -0500209 PAS_DMA_RXCHAN_CFG_HBU(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600210
Olof Johanssona85b9422007-09-15 13:40:59 -0700211 write_dma_reg(mac, PAS_DMA_RXINT_BASEL(mac->dma_if),
212 PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600213
Olof Johanssona85b9422007-09-15 13:40:59 -0700214 write_dma_reg(mac, PAS_DMA_RXINT_BASEU(mac->dma_if),
215 PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) |
216 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600217
Olof Johanssonc0efd522007-08-22 09:12:52 -0500218 write_dma_reg(mac, PAS_DMA_RXINT_CFG(mac->dma_if),
219 PAS_DMA_RXINT_CFG_DHL(2));
220
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600221 ring->next_to_fill = 0;
222 ring->next_to_clean = 0;
223
224 snprintf(ring->irq_name, sizeof(ring->irq_name),
225 "%s rx", dev->name);
226 mac->rx = ring;
227
228 return 0;
229
230out_buffers:
231 dma_free_coherent(&mac->dma_pdev->dev,
232 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
233 mac->rx->desc, mac->rx->dma);
234out_desc:
235 kfree(ring->desc_info);
236out_desc_info:
237 kfree(ring);
238out_ring:
239 return -ENOMEM;
240}
241
242
243static int pasemi_mac_setup_tx_resources(struct net_device *dev)
244{
245 struct pasemi_mac *mac = netdev_priv(dev);
246 u32 val;
247 int chan_id = mac->dma_txch;
248 struct pasemi_mac_txring *ring;
249
250 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
251 if (!ring)
252 goto out_ring;
253
254 spin_lock_init(&ring->lock);
255
256 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
257 TX_RING_SIZE, GFP_KERNEL);
258 if (!ring->desc_info)
259 goto out_desc_info;
260
261 /* Allocate descriptors */
262 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
263 TX_RING_SIZE *
264 sizeof(struct pas_dma_xct_descr),
265 &ring->dma, GFP_KERNEL);
266 if (!ring->desc)
267 goto out_desc;
268
269 memset(ring->desc, 0, TX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
270
Olof Johanssona85b9422007-09-15 13:40:59 -0700271 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id),
272 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600273 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
274 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 2);
275
Olof Johanssona85b9422007-09-15 13:40:59 -0700276 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600277
Olof Johanssona85b9422007-09-15 13:40:59 -0700278 write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(chan_id),
279 PAS_DMA_TXCHAN_CFG_TY_IFACE |
280 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
281 PAS_DMA_TXCHAN_CFG_UP |
282 PAS_DMA_TXCHAN_CFG_WT(2));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600283
284 ring->next_to_use = 0;
285 ring->next_to_clean = 0;
286
287 snprintf(ring->irq_name, sizeof(ring->irq_name),
288 "%s tx", dev->name);
289 mac->tx = ring;
290
291 return 0;
292
293out_desc:
294 kfree(ring->desc_info);
295out_desc_info:
296 kfree(ring);
297out_ring:
298 return -ENOMEM;
299}
300
301static void pasemi_mac_free_tx_resources(struct net_device *dev)
302{
303 struct pasemi_mac *mac = netdev_priv(dev);
304 unsigned int i;
305 struct pasemi_mac_buffer *info;
306 struct pas_dma_xct_descr *dp;
307
308 for (i = 0; i < TX_RING_SIZE; i++) {
309 info = &TX_DESC_INFO(mac, i);
310 dp = &TX_DESC(mac, i);
311 if (info->dma) {
312 if (info->skb) {
313 pci_unmap_single(mac->dma_pdev,
314 info->dma,
315 info->skb->len,
316 PCI_DMA_TODEVICE);
317 dev_kfree_skb_any(info->skb);
318 }
319 info->dma = 0;
320 info->skb = NULL;
321 dp->mactx = 0;
322 dp->ptr = 0;
323 }
324 }
325
326 dma_free_coherent(&mac->dma_pdev->dev,
327 TX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
328 mac->tx->desc, mac->tx->dma);
329
330 kfree(mac->tx->desc_info);
331 kfree(mac->tx);
332 mac->tx = NULL;
333}
334
335static void pasemi_mac_free_rx_resources(struct net_device *dev)
336{
337 struct pasemi_mac *mac = netdev_priv(dev);
338 unsigned int i;
339 struct pasemi_mac_buffer *info;
340 struct pas_dma_xct_descr *dp;
341
342 for (i = 0; i < RX_RING_SIZE; i++) {
343 info = &RX_DESC_INFO(mac, i);
344 dp = &RX_DESC(mac, i);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500345 if (info->skb) {
346 if (info->dma) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600347 pci_unmap_single(mac->dma_pdev,
348 info->dma,
349 info->skb->len,
350 PCI_DMA_FROMDEVICE);
351 dev_kfree_skb_any(info->skb);
352 }
353 info->dma = 0;
354 info->skb = NULL;
355 dp->macrx = 0;
356 dp->ptr = 0;
357 }
358 }
359
360 dma_free_coherent(&mac->dma_pdev->dev,
361 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
362 mac->rx->desc, mac->rx->dma);
363
364 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
365 mac->rx->buffers, mac->rx->buf_dma);
366
367 kfree(mac->rx->desc_info);
368 kfree(mac->rx);
369 mac->rx = NULL;
370}
371
372static void pasemi_mac_replenish_rx_ring(struct net_device *dev)
373{
374 struct pasemi_mac *mac = netdev_priv(dev);
375 unsigned int i;
376 int start = mac->rx->next_to_fill;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500377 unsigned int limit, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600378
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500379 limit = (mac->rx->next_to_clean + RX_RING_SIZE -
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600380 mac->rx->next_to_fill) & (RX_RING_SIZE - 1);
381
382 /* Check to see if we're doing first-time setup */
383 if (unlikely(mac->rx->next_to_clean == 0 && mac->rx->next_to_fill == 0))
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500384 limit = RX_RING_SIZE;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600385
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500386 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600387 return;
388
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500389 i = start;
390 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600391 struct pasemi_mac_buffer *info = &RX_DESC_INFO(mac, i);
392 u64 *buff = &RX_BUFF(mac, i);
393 struct sk_buff *skb;
394 dma_addr_t dma;
395
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500396 /* skb might still be in there for recycle on short receives */
397 if (info->skb)
398 skb = info->skb;
399 else
400 skb = dev_alloc_skb(BUF_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600401
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500402 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600403 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600404
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600405 dma = pci_map_single(mac->dma_pdev, skb->data, skb->len,
406 PCI_DMA_FROMDEVICE);
407
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500408 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600409 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600410 break;
411 }
412
413 info->skb = skb;
414 info->dma = dma;
415 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500416 i++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600417 }
418
419 wmb();
420
Olof Johanssona85b9422007-09-15 13:40:59 -0700421 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), limit - count);
422 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), limit - count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600423
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500424 mac->rx->next_to_fill += limit - count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600425}
426
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500427static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
428{
Olof Johansson52a94352007-05-12 18:01:09 -0500429 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500430 /* Re-enable packet count interrupts: finally
431 * ack the packet count interrupt we got in rx_intr.
432 */
433
Olof Johansson52a94352007-05-12 18:01:09 -0500434 pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500435
Olof Johansson52a94352007-05-12 18:01:09 -0500436 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500437
Olof Johanssona85b9422007-09-15 13:40:59 -0700438 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500439}
440
441static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
442{
Olof Johansson52a94352007-05-12 18:01:09 -0500443 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500444
445 /* Re-enable packet count interrupts */
Olof Johansson52a94352007-05-12 18:01:09 -0500446 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500447
Olof Johansson52a94352007-05-12 18:01:09 -0500448 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500449
Olof Johanssona85b9422007-09-15 13:40:59 -0700450 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500451}
452
453
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600454static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
455{
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500456 unsigned int n;
457 int count;
458 struct pas_dma_xct_descr *dp;
459 struct pasemi_mac_buffer *info;
460 struct sk_buff *skb;
461 unsigned int i, len;
462 u64 macrx;
463 dma_addr_t dma;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600464
465 spin_lock(&mac->rx->lock);
466
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500467 n = mac->rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600468
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500469 for (count = limit; count; count--) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600470
471 rmb();
472
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500473 dp = &RX_DESC(mac, n);
474 macrx = dp->macrx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600475
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500476 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600477 break;
478
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600479
480 info = NULL;
481
482 /* We have to scan for our skb since there's no way
483 * to back-map them from the descriptor, and if we
484 * have several receive channels then they might not
485 * show up in the same order as they were put on the
486 * interface ring.
487 */
488
489 dma = (dp->ptr & XCT_PTR_ADDR_M);
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500490 for (i = n; i < (n + RX_RING_SIZE); i++) {
491 info = &RX_DESC_INFO(mac, i);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600492 if (info->dma == dma)
493 break;
494 }
495
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500496 skb = info->skb;
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500497 info->dma = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600498
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500499 pci_unmap_single(mac->dma_pdev, dma, skb->len,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600500 PCI_DMA_FROMDEVICE);
501
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500502 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600503
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500504 if (len < 256) {
505 struct sk_buff *new_skb =
506 netdev_alloc_skb(mac->netdev, len + NET_IP_ALIGN);
507 if (new_skb) {
508 skb_reserve(new_skb, NET_IP_ALIGN);
Olof Johansson73344862007-08-22 09:12:55 -0500509 memcpy(new_skb->data, skb->data, len);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500510 /* save the skb in buffer_info as good */
511 skb = new_skb;
512 }
513 /* else just continue with the old one */
514 } else
515 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600516
517 skb_put(skb, len);
518
519 skb->protocol = eth_type_trans(skb, mac->netdev);
520
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500521 if ((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600522 skb->ip_summed = CHECKSUM_COMPLETE;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500523 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600524 XCT_MACRX_CSUM_S;
525 } else
526 skb->ip_summed = CHECKSUM_NONE;
527
528 mac->stats.rx_bytes += len;
529 mac->stats.rx_packets++;
530
531 netif_receive_skb(skb);
532
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600533 dp->ptr = 0;
534 dp->macrx = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500535
536 n++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600537 }
538
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500539 mac->rx->next_to_clean += limit - count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600540 pasemi_mac_replenish_rx_ring(mac->netdev);
541
542 spin_unlock(&mac->rx->lock);
543
544 return count;
545}
546
547static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
548{
549 int i;
550 struct pasemi_mac_buffer *info;
551 struct pas_dma_xct_descr *dp;
552 int start, count;
553 int flags;
554
555 spin_lock_irqsave(&mac->tx->lock, flags);
556
557 start = mac->tx->next_to_clean;
558 count = 0;
559
560 for (i = start; i < mac->tx->next_to_use; i++) {
561 dp = &TX_DESC(mac, i);
562 if (!dp || (dp->mactx & XCT_MACTX_O))
563 break;
564
565 count++;
566
567 info = &TX_DESC_INFO(mac, i);
568
569 pci_unmap_single(mac->dma_pdev, info->dma,
570 info->skb->len, PCI_DMA_TODEVICE);
571 dev_kfree_skb_irq(info->skb);
572
573 info->skb = NULL;
574 info->dma = 0;
575 dp->mactx = 0;
576 dp->ptr = 0;
577 }
578 mac->tx->next_to_clean += count;
579 spin_unlock_irqrestore(&mac->tx->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500580 netif_wake_queue(mac->netdev);
581
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600582 return count;
583}
584
585
586static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
587{
588 struct net_device *dev = data;
589 struct pasemi_mac *mac = netdev_priv(dev);
590 unsigned int reg;
591
Olof Johansson6dfa7522007-05-08 00:47:32 -0500592 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600593 return IRQ_NONE;
594
Olof Johansson6dfa7522007-05-08 00:47:32 -0500595 if (*mac->rx_status & PAS_STATUS_ERROR)
596 printk("rx_status reported error\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600597
Olof Johansson6dfa7522007-05-08 00:47:32 -0500598 /* Don't reset packet count so it won't fire again but clear
599 * all others.
600 */
601
Olof Johansson6dfa7522007-05-08 00:47:32 -0500602 reg = 0;
603 if (*mac->rx_status & PAS_STATUS_SOFT)
604 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
605 if (*mac->rx_status & PAS_STATUS_ERROR)
606 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600607 if (*mac->rx_status & PAS_STATUS_TIMER)
608 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
609
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700610 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500611
Olof Johanssona85b9422007-09-15 13:40:59 -0700612 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600613
614 return IRQ_HANDLED;
615}
616
617static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
618{
619 struct net_device *dev = data;
620 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson52a94352007-05-12 18:01:09 -0500621 unsigned int reg, pcnt;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600622
Olof Johansson6dfa7522007-05-08 00:47:32 -0500623 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600624 return IRQ_NONE;
625
626 pasemi_mac_clean_tx(mac);
627
Olof Johansson52a94352007-05-12 18:01:09 -0500628 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
629
630 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500631
632 if (*mac->tx_status & PAS_STATUS_SOFT)
633 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
634 if (*mac->tx_status & PAS_STATUS_ERROR)
635 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600636
Olof Johanssona85b9422007-09-15 13:40:59 -0700637 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600638
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600639 return IRQ_HANDLED;
640}
641
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500642static void pasemi_adjust_link(struct net_device *dev)
643{
644 struct pasemi_mac *mac = netdev_priv(dev);
645 int msg;
646 unsigned int flags;
647 unsigned int new_flags;
648
649 if (!mac->phydev->link) {
650 /* If no link, MAC speed settings don't matter. Just report
651 * link down and return.
652 */
653 if (mac->link && netif_msg_link(mac))
654 printk(KERN_INFO "%s: Link is down.\n", dev->name);
655
656 netif_carrier_off(dev);
657 mac->link = 0;
658
659 return;
660 } else
661 netif_carrier_on(dev);
662
Olof Johanssona85b9422007-09-15 13:40:59 -0700663 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500664 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
665 PAS_MAC_CFG_PCFG_TSR_M);
666
667 if (!mac->phydev->duplex)
668 new_flags |= PAS_MAC_CFG_PCFG_HD;
669
670 switch (mac->phydev->speed) {
671 case 1000:
672 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
673 PAS_MAC_CFG_PCFG_TSR_1G;
674 break;
675 case 100:
676 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
677 PAS_MAC_CFG_PCFG_TSR_100M;
678 break;
679 case 10:
680 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
681 PAS_MAC_CFG_PCFG_TSR_10M;
682 break;
683 default:
684 printk("Unsupported speed %d\n", mac->phydev->speed);
685 }
686
687 /* Print on link or speed/duplex change */
688 msg = mac->link != mac->phydev->link || flags != new_flags;
689
690 mac->duplex = mac->phydev->duplex;
691 mac->speed = mac->phydev->speed;
692 mac->link = mac->phydev->link;
693
694 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700695 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500696
697 if (msg && netif_msg_link(mac))
698 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
699 dev->name, mac->speed, mac->duplex ? "full" : "half");
700}
701
702static int pasemi_mac_phy_init(struct net_device *dev)
703{
704 struct pasemi_mac *mac = netdev_priv(dev);
705 struct device_node *dn, *phy_dn;
706 struct phy_device *phydev;
707 unsigned int phy_id;
708 const phandle *ph;
709 const unsigned int *prop;
710 struct resource r;
711 int ret;
712
713 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700714 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500715 if (!ph)
716 return -ENODEV;
717 phy_dn = of_find_node_by_phandle(*ph);
718
Linus Torvalds90287802007-05-08 11:57:17 -0700719 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500720 ret = of_address_to_resource(phy_dn->parent, 0, &r);
721 if (ret)
722 goto err;
723
724 phy_id = *prop;
725 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
726
727 of_node_put(phy_dn);
728
729 mac->link = 0;
730 mac->speed = 0;
731 mac->duplex = -1;
732
733 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
734
735 if (IS_ERR(phydev)) {
736 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
737 return PTR_ERR(phydev);
738 }
739
740 mac->phydev = phydev;
741
742 return 0;
743
744err:
745 of_node_put(phy_dn);
746 return -ENODEV;
747}
748
749
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600750static int pasemi_mac_open(struct net_device *dev)
751{
752 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson771f7402007-05-08 00:47:21 -0500753 int base_irq;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600754 unsigned int flags;
755 int ret;
756
757 /* enable rx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700758 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600759
760 /* enable tx section */
Olof Johanssona85b9422007-09-15 13:40:59 -0700761 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600762
763 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
764 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
765 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
766
Olof Johanssona85b9422007-09-15 13:40:59 -0700767 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600768
769 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
770 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
771
772 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
773
Olof Johanssona85b9422007-09-15 13:40:59 -0700774 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
775 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600776
Olof Johanssona85b9422007-09-15 13:40:59 -0700777 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
778 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600779
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500780 /* Clear out any residual packet count state from firmware */
781 pasemi_mac_restart_rx_intr(mac);
782 pasemi_mac_restart_tx_intr(mac);
783
Olof Johansson6dfa7522007-05-08 00:47:32 -0500784 /* 0xffffff is max value, about 16ms */
Olof Johanssona85b9422007-09-15 13:40:59 -0700785 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
786 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600787
Olof Johanssona85b9422007-09-15 13:40:59 -0700788 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600789
790 ret = pasemi_mac_setup_rx_resources(dev);
791 if (ret)
792 goto out_rx_resources;
793
794 ret = pasemi_mac_setup_tx_resources(dev);
795 if (ret)
796 goto out_tx_resources;
797
Olof Johanssona85b9422007-09-15 13:40:59 -0700798 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
799 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
800 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600801
802 /* enable rx if */
Olof Johanssona85b9422007-09-15 13:40:59 -0700803 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
804 PAS_DMA_RXINT_RCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600805
806 /* enable rx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700807 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
808 PAS_DMA_RXCHAN_CCMDSTA_EN |
809 PAS_DMA_RXCHAN_CCMDSTA_DU);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600810
811 /* enable tx channel */
Olof Johanssona85b9422007-09-15 13:40:59 -0700812 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
813 PAS_DMA_TXCHAN_TCMDSTA_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600814
815 pasemi_mac_replenish_rx_ring(dev);
816
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500817 ret = pasemi_mac_phy_init(dev);
818 /* Some configs don't have PHYs (XAUI etc), so don't complain about
819 * failed init due to -ENODEV.
820 */
821 if (ret && ret != -ENODEV)
822 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
823
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600824 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700825 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600826
Olof Johansson771f7402007-05-08 00:47:21 -0500827 /* Interrupts are a bit different for our DMA controller: While
828 * it's got one a regular PCI device header, the interrupt there
829 * is really the base of the range it's using. Each tx and rx
830 * channel has it's own interrupt source.
831 */
832
833 base_irq = virq_to_hw(mac->dma_pdev->irq);
834
835 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
836 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
837
838 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600839 mac->tx->irq_name, dev);
840 if (ret) {
841 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500842 base_irq + mac->dma_txch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600843 goto out_tx_int;
844 }
845
Olof Johansson771f7402007-05-08 00:47:21 -0500846 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600847 mac->rx->irq_name, dev);
848 if (ret) {
849 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson771f7402007-05-08 00:47:21 -0500850 base_irq + 20 + mac->dma_rxch, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600851 goto out_rx_int;
852 }
853
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500854 if (mac->phydev)
855 phy_start(mac->phydev);
856
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600857 return 0;
858
859out_rx_int:
Olof Johansson771f7402007-05-08 00:47:21 -0500860 free_irq(mac->tx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600861out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700862 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600863 netif_stop_queue(dev);
864 pasemi_mac_free_tx_resources(dev);
865out_tx_resources:
866 pasemi_mac_free_rx_resources(dev);
867out_rx_resources:
868
869 return ret;
870}
871
872#define MAX_RETRIES 5000
873
874static int pasemi_mac_close(struct net_device *dev)
875{
876 struct pasemi_mac *mac = netdev_priv(dev);
877 unsigned int stat;
878 int retries;
879
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500880 if (mac->phydev) {
881 phy_stop(mac->phydev);
882 phy_disconnect(mac->phydev);
883 }
884
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600885 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700886 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600887
888 /* Clean out any pending buffers */
889 pasemi_mac_clean_tx(mac);
890 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
891
892 /* Disable interface */
Olof Johanssona85b9422007-09-15 13:40:59 -0700893 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST);
894 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST);
895 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600896
897 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700898 stat = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500899 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600900 break;
901 cond_resched();
902 }
903
Olof Johansson0ce68c72007-04-28 15:36:40 -0500904 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600905 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600906
907 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700908 stat = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500909 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600910 break;
911 cond_resched();
912 }
913
Olof Johansson0ce68c72007-04-28 15:36:40 -0500914 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600915 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600916
917 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johanssona85b9422007-09-15 13:40:59 -0700918 stat = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson0ce68c72007-04-28 15:36:40 -0500919 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600920 break;
921 cond_resched();
922 }
923
Olof Johansson0ce68c72007-04-28 15:36:40 -0500924 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600925 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600926
927 /* Then, disable the channel. This must be done separately from
928 * stopping, since you can't disable when active.
929 */
930
Olof Johanssona85b9422007-09-15 13:40:59 -0700931 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
932 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
933 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600934
Olof Johansson771f7402007-05-08 00:47:21 -0500935 free_irq(mac->tx_irq, dev);
936 free_irq(mac->rx_irq, dev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600937
938 /* Free resources */
939 pasemi_mac_free_rx_resources(dev);
940 pasemi_mac_free_tx_resources(dev);
941
942 return 0;
943}
944
945static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
946{
947 struct pasemi_mac *mac = netdev_priv(dev);
948 struct pasemi_mac_txring *txring;
949 struct pasemi_mac_buffer *info;
950 struct pas_dma_xct_descr *dp;
951 u64 dflags;
952 dma_addr_t map;
953 int flags;
954
955 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
956
957 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700958 const unsigned char *nh = skb_network_header(skb);
959
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700960 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600961 case IPPROTO_TCP:
962 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300963 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700964 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600965 break;
966 case IPPROTO_UDP:
967 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300968 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700969 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600970 break;
971 }
972 }
973
974 map = pci_map_single(mac->dma_pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
975
976 if (dma_mapping_error(map))
977 return NETDEV_TX_BUSY;
978
979 txring = mac->tx;
980
981 spin_lock_irqsave(&txring->lock, flags);
982
983 if (txring->next_to_clean - txring->next_to_use == TX_RING_SIZE) {
984 spin_unlock_irqrestore(&txring->lock, flags);
985 pasemi_mac_clean_tx(mac);
Olof Johansson52a94352007-05-12 18:01:09 -0500986 pasemi_mac_restart_tx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600987 spin_lock_irqsave(&txring->lock, flags);
988
989 if (txring->next_to_clean - txring->next_to_use ==
990 TX_RING_SIZE) {
991 /* Still no room -- stop the queue and wait for tx
992 * intr when there's room.
993 */
994 netif_stop_queue(dev);
995 goto out_err;
996 }
997 }
998
999
1000 dp = &TX_DESC(mac, txring->next_to_use);
1001 info = &TX_DESC_INFO(mac, txring->next_to_use);
1002
1003 dp->mactx = dflags | XCT_MACTX_LLEN(skb->len);
1004 dp->ptr = XCT_PTR_LEN(skb->len) | XCT_PTR_ADDR(map);
1005 info->dma = map;
1006 info->skb = skb;
1007
1008 txring->next_to_use++;
1009 mac->stats.tx_packets++;
1010 mac->stats.tx_bytes += skb->len;
1011
1012 spin_unlock_irqrestore(&txring->lock, flags);
1013
Olof Johanssona85b9422007-09-15 13:40:59 -07001014 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001015
1016 return NETDEV_TX_OK;
1017
1018out_err:
1019 spin_unlock_irqrestore(&txring->lock, flags);
1020 pci_unmap_single(mac->dma_pdev, map, skb->len, PCI_DMA_TODEVICE);
1021 return NETDEV_TX_BUSY;
1022}
1023
1024static struct net_device_stats *pasemi_mac_get_stats(struct net_device *dev)
1025{
1026 struct pasemi_mac *mac = netdev_priv(dev);
1027
1028 return &mac->stats;
1029}
1030
Olof Johanssonceb51362007-05-08 00:47:49 -05001031
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001032static void pasemi_mac_set_rx_mode(struct net_device *dev)
1033{
1034 struct pasemi_mac *mac = netdev_priv(dev);
1035 unsigned int flags;
1036
Olof Johanssona85b9422007-09-15 13:40:59 -07001037 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001038
1039 /* Set promiscuous */
1040 if (dev->flags & IFF_PROMISC)
1041 flags |= PAS_MAC_CFG_PCFG_PR;
1042 else
1043 flags &= ~PAS_MAC_CFG_PCFG_PR;
1044
Olof Johanssona85b9422007-09-15 13:40:59 -07001045 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001046}
1047
1048
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001049static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001050{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001051 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1052 struct net_device *dev = mac->netdev;
1053 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001054
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001055 pkts = pasemi_mac_clean_rx(mac, budget);
1056 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001057 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001058 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001059
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001060 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001061 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001062 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001063}
1064
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001065static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
1066{
1067 struct device_node *dn;
1068 void __iomem *ret;
1069
1070 dn = pci_device_to_OF_node(p);
1071 if (!dn)
1072 goto fallback;
1073
1074 ret = of_iomap(dn, index);
1075 if (!ret)
1076 goto fallback;
1077
1078 return ret;
1079fallback:
1080 /* This is hardcoded and ugly, but we have some firmware versions
1081 * that don't provide the register space in the device tree. Luckily
1082 * they are at well-known locations so we can just do the math here.
1083 */
1084 return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
1085}
1086
1087static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
1088{
1089 struct resource res;
1090 struct device_node *dn;
1091 int err;
1092
1093 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1094 if (!mac->dma_pdev) {
1095 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1096 return -ENODEV;
1097 }
1098
1099 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1100 if (!mac->iob_pdev) {
1101 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1102 return -ENODEV;
1103 }
1104
1105 mac->regs = map_onedev(mac->pdev, 0);
1106 mac->dma_regs = map_onedev(mac->dma_pdev, 0);
1107 mac->iob_regs = map_onedev(mac->iob_pdev, 0);
1108
1109 if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
1110 dev_err(&mac->pdev->dev, "Can't map registers\n");
1111 return -ENODEV;
1112 }
1113
1114 /* The dma status structure is located in the I/O bridge, and
1115 * is cache coherent.
1116 */
1117 if (!dma_status) {
1118 dn = pci_device_to_OF_node(mac->iob_pdev);
1119 if (dn)
1120 err = of_address_to_resource(dn, 1, &res);
1121 if (!dn || err) {
1122 /* Fallback for old firmware */
1123 res.start = 0xfd800000;
1124 res.end = res.start + 0x1000;
1125 }
1126 dma_status = __ioremap(res.start, res.end-res.start, 0);
1127 }
1128
1129 return 0;
1130}
1131
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001132static int __devinit
1133pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1134{
1135 static int index = 0;
1136 struct net_device *dev;
1137 struct pasemi_mac *mac;
1138 int err;
1139
1140 err = pci_enable_device(pdev);
1141 if (err)
1142 return err;
1143
1144 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1145 if (dev == NULL) {
1146 dev_err(&pdev->dev,
1147 "pasemi_mac: Could not allocate ethernet device.\n");
1148 err = -ENOMEM;
1149 goto out_disable_device;
1150 }
1151
1152 SET_MODULE_OWNER(dev);
1153 pci_set_drvdata(pdev, dev);
1154 SET_NETDEV_DEV(dev, &pdev->dev);
1155
1156 mac = netdev_priv(dev);
1157
1158 mac->pdev = pdev;
1159 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001160
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001161 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1162
1163 dev->features = NETIF_F_HW_CSUM;
1164
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001165 /* These should come out of the device tree eventually */
1166 mac->dma_txch = index;
1167 mac->dma_rxch = index;
1168
1169 /* We probe GMAC before XAUI, but the DMA interfaces are
1170 * in XAUI, GMAC order.
1171 */
1172 if (index < 4)
1173 mac->dma_if = index + 2;
1174 else
1175 mac->dma_if = index - 4;
1176 index++;
1177
1178 switch (pdev->device) {
1179 case 0xa005:
1180 mac->type = MAC_TYPE_GMAC;
1181 break;
1182 case 0xa006:
1183 mac->type = MAC_TYPE_XAUI;
1184 break;
1185 default:
1186 err = -ENODEV;
1187 goto out;
1188 }
1189
1190 /* get mac addr from device tree */
1191 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1192 err = -ENODEV;
1193 goto out;
1194 }
1195 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1196
1197 dev->open = pasemi_mac_open;
1198 dev->stop = pasemi_mac_close;
1199 dev->hard_start_xmit = pasemi_mac_start_tx;
1200 dev->get_stats = pasemi_mac_get_stats;
1201 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001202
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001203 err = pasemi_mac_map_regs(mac);
1204 if (err)
1205 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001206
1207 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1208 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1209
Olof Johanssonceb51362007-05-08 00:47:49 -05001210 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1211
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001212 /* Enable most messages by default */
1213 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1214
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001215 err = register_netdev(dev);
1216
1217 if (err) {
1218 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1219 err);
1220 goto out;
1221 } else
1222 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
1223 "hw addr %02x:%02x:%02x:%02x:%02x:%02x\n",
1224 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1225 mac->dma_if, mac->dma_txch, mac->dma_rxch,
1226 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1227 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1228
1229 return err;
1230
1231out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001232 if (mac->iob_pdev)
1233 pci_dev_put(mac->iob_pdev);
1234 if (mac->dma_pdev)
1235 pci_dev_put(mac->dma_pdev);
1236 if (mac->dma_regs)
1237 iounmap(mac->dma_regs);
1238 if (mac->iob_regs)
1239 iounmap(mac->iob_regs);
1240 if (mac->regs)
1241 iounmap(mac->regs);
1242
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001243 free_netdev(dev);
1244out_disable_device:
1245 pci_disable_device(pdev);
1246 return err;
1247
1248}
1249
1250static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1251{
1252 struct net_device *netdev = pci_get_drvdata(pdev);
1253 struct pasemi_mac *mac;
1254
1255 if (!netdev)
1256 return;
1257
1258 mac = netdev_priv(netdev);
1259
1260 unregister_netdev(netdev);
1261
1262 pci_disable_device(pdev);
1263 pci_dev_put(mac->dma_pdev);
1264 pci_dev_put(mac->iob_pdev);
1265
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001266 iounmap(mac->regs);
1267 iounmap(mac->dma_regs);
1268 iounmap(mac->iob_regs);
1269
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001270 pci_set_drvdata(pdev, NULL);
1271 free_netdev(netdev);
1272}
1273
1274static struct pci_device_id pasemi_mac_pci_tbl[] = {
1275 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1276 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001277 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001278};
1279
1280MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1281
1282static struct pci_driver pasemi_mac_driver = {
1283 .name = "pasemi_mac",
1284 .id_table = pasemi_mac_pci_tbl,
1285 .probe = pasemi_mac_probe,
1286 .remove = __devexit_p(pasemi_mac_remove),
1287};
1288
1289static void __exit pasemi_mac_cleanup_module(void)
1290{
1291 pci_unregister_driver(&pasemi_mac_driver);
1292 __iounmap(dma_status);
1293 dma_status = NULL;
1294}
1295
1296int pasemi_mac_init_module(void)
1297{
1298 return pci_register_driver(&pasemi_mac_driver);
1299}
1300
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001301module_init(pasemi_mac_init_module);
1302module_exit(pasemi_mac_cleanup_module);