blob: a50eb34ece5d8b12cfc31d41b64505eb4d44a17e [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 Johansson40afa532007-11-28 20:56:04 -060038#include <asm/pasemi_dma.h>
Olof Johansson771f7402007-05-08 00:47:21 -050039
Olof Johanssonf5cd7872007-01-31 21:43:54 -060040#include "pasemi_mac.h"
41
Olof Johansson8dc121a2007-10-02 16:26:53 -050042/* We have our own align, since ppc64 in general has it at 0 because
43 * of design flaws in some of the server bridge chips. However, for
44 * PWRficient doing the unaligned copies is more expensive than doing
45 * unaligned DMA, so make sure the data is aligned instead.
46 */
47#define LOCAL_SKB_ALIGN 2
Olof Johanssonf5cd7872007-01-31 21:43:54 -060048
49/* TODO list
50 *
Olof Johanssonf5cd7872007-01-31 21:43:54 -060051 * - Multicast support
52 * - Large MTU support
Olof Johansson7ddeae22007-10-02 16:27:28 -050053 * - SW LRO
54 * - Multiqueue RX/TX
Olof Johanssonf5cd7872007-01-31 21:43:54 -060055 */
56
57
58/* Must be a power of two */
Olof Johanssonad5da102007-10-02 16:27:15 -050059#define RX_RING_SIZE 4096
60#define TX_RING_SIZE 4096
Olof Johanssonf5cd7872007-01-31 21:43:54 -060061
Olof Johanssonceb51362007-05-08 00:47:49 -050062#define DEFAULT_MSG_ENABLE \
63 (NETIF_MSG_DRV | \
64 NETIF_MSG_PROBE | \
65 NETIF_MSG_LINK | \
66 NETIF_MSG_TIMER | \
67 NETIF_MSG_IFDOWN | \
68 NETIF_MSG_IFUP | \
69 NETIF_MSG_RX_ERR | \
70 NETIF_MSG_TX_ERR)
71
Olof Johansson34c20622007-11-28 20:56:32 -060072#define TX_DESC(tx, num) ((tx)->chan.ring_virt[(num) & (TX_RING_SIZE-1)])
Olof Johansson72b05b92007-11-28 20:54:28 -060073#define TX_DESC_INFO(tx, num) ((tx)->ring_info[(num) & (TX_RING_SIZE-1)])
Olof Johansson34c20622007-11-28 20:56:32 -060074#define RX_DESC(rx, num) ((rx)->chan.ring_virt[(num) & (RX_RING_SIZE-1)])
Olof Johansson72b05b92007-11-28 20:54:28 -060075#define RX_DESC_INFO(rx, num) ((rx)->ring_info[(num) & (RX_RING_SIZE-1)])
76#define RX_BUFF(rx, num) ((rx)->buffers[(num) & (RX_RING_SIZE-1)])
Olof Johanssonf5cd7872007-01-31 21:43:54 -060077
Olof Johansson021fa222007-08-22 09:13:11 -050078#define RING_USED(ring) (((ring)->next_to_fill - (ring)->next_to_clean) \
79 & ((ring)->size - 1))
80#define RING_AVAIL(ring) ((ring->size) - RING_USED(ring))
81
Olof Johanssonf5cd7872007-01-31 21:43:54 -060082#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
83
Olof Johanssonceb51362007-05-08 00:47:49 -050084MODULE_LICENSE("GPL");
85MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
86MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
87
88static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
89module_param(debug, int, 0);
90MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
Olof Johanssonf5cd7872007-01-31 21:43:54 -060091
Olof Johanssonaf289e82007-10-03 13:03:54 -050092static int translation_enabled(void)
93{
94#if defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
95 return 1;
96#else
97 return firmware_has_feature(FW_FEATURE_LPAR);
98#endif
99}
100
Olof Johansson34c20622007-11-28 20:56:32 -0600101static void write_iob_reg(unsigned int reg, unsigned int val)
Olof Johanssona85b9422007-09-15 13:40:59 -0700102{
Olof Johansson34c20622007-11-28 20:56:32 -0600103 pasemi_write_iob_reg(reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700104}
105
106static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
107{
Olof Johansson34c20622007-11-28 20:56:32 -0600108 return pasemi_read_mac_reg(mac->dma_if, reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700109}
110
111static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
112 unsigned int val)
113{
Olof Johansson34c20622007-11-28 20:56:32 -0600114 pasemi_write_mac_reg(mac->dma_if, reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700115}
116
Olof Johansson34c20622007-11-28 20:56:32 -0600117static unsigned int read_dma_reg(unsigned int reg)
Olof Johanssona85b9422007-09-15 13:40:59 -0700118{
Olof Johansson34c20622007-11-28 20:56:32 -0600119 return pasemi_read_dma_reg(reg);
Olof Johanssona85b9422007-09-15 13:40:59 -0700120}
121
Olof Johansson34c20622007-11-28 20:56:32 -0600122static void write_dma_reg(unsigned int reg, unsigned int val)
Olof Johanssona85b9422007-09-15 13:40:59 -0700123{
Olof Johansson34c20622007-11-28 20:56:32 -0600124 pasemi_write_dma_reg(reg, val);
Olof Johanssona85b9422007-09-15 13:40:59 -0700125}
126
Olof Johansson72b05b92007-11-28 20:54:28 -0600127static struct pasemi_mac_rxring *rx_ring(struct pasemi_mac *mac)
128{
129 return mac->rx;
130}
131
132static struct pasemi_mac_txring *tx_ring(struct pasemi_mac *mac)
133{
134 return mac->tx;
135}
136
Olof Johansson34c20622007-11-28 20:56:32 -0600137static int mac_to_intf(struct pasemi_mac *mac)
138{
139 struct pci_dev *pdev = mac->pdev;
140 u32 tmp;
141 int nintf, off, i, j;
142 int devfn = pdev->devfn;
143
144 tmp = read_dma_reg(PAS_DMA_CAP_IFI);
145 nintf = (tmp & PAS_DMA_CAP_IFI_NIN_M) >> PAS_DMA_CAP_IFI_NIN_S;
146 off = (tmp & PAS_DMA_CAP_IFI_IOFF_M) >> PAS_DMA_CAP_IFI_IOFF_S;
147
148 /* IOFF contains the offset to the registers containing the
149 * DMA interface-to-MAC-pci-id mappings, and NIN contains number
150 * of total interfaces. Each register contains 4 devfns.
151 * Just do a linear search until we find the devfn of the MAC
152 * we're trying to look up.
153 */
154
155 for (i = 0; i < (nintf+3)/4; i++) {
156 tmp = read_dma_reg(off+4*i);
157 for (j = 0; j < 4; j++) {
158 if (((tmp >> (8*j)) & 0xff) == devfn)
159 return i*4 + j;
160 }
161 }
162 return -1;
163}
164
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600165static int pasemi_get_mac_addr(struct pasemi_mac *mac)
166{
167 struct pci_dev *pdev = mac->pdev;
168 struct device_node *dn = pci_device_to_OF_node(pdev);
olof@lixom.net1af7f052007-05-12 14:57:46 -0500169 int len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600170 const u8 *maddr;
171 u8 addr[6];
172
173 if (!dn) {
174 dev_dbg(&pdev->dev,
175 "No device node for mac, not configuring\n");
176 return -ENOENT;
177 }
178
olof@lixom.net1af7f052007-05-12 14:57:46 -0500179 maddr = of_get_property(dn, "local-mac-address", &len);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500180
olof@lixom.net1af7f052007-05-12 14:57:46 -0500181 if (maddr && len == 6) {
182 memcpy(mac->mac_addr, maddr, 6);
183 return 0;
184 }
185
186 /* Some old versions of firmware mistakenly uses mac-address
187 * (and as a string) instead of a byte array in local-mac-address.
188 */
189
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500190 if (maddr == NULL)
Linus Torvalds90287802007-05-08 11:57:17 -0700191 maddr = of_get_property(dn, "mac-address", NULL);
Olof Johanssona5fd22e2007-05-08 00:48:02 -0500192
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600193 if (maddr == NULL) {
194 dev_warn(&pdev->dev,
195 "no mac address in device tree, not configuring\n");
196 return -ENOENT;
197 }
198
olof@lixom.net1af7f052007-05-12 14:57:46 -0500199
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600200 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
201 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
202 dev_warn(&pdev->dev,
203 "can't parse mac address, not configuring\n");
204 return -EINVAL;
205 }
206
olof@lixom.net1af7f052007-05-12 14:57:46 -0500207 memcpy(mac->mac_addr, addr, 6);
208
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600209 return 0;
210}
211
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500212static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
213 struct sk_buff *skb,
214 dma_addr_t *dmas)
215{
216 int f;
217 int nfrags = skb_shinfo(skb)->nr_frags;
218
219 pci_unmap_single(mac->dma_pdev, dmas[0], skb_headlen(skb),
220 PCI_DMA_TODEVICE);
221
222 for (f = 0; f < nfrags; f++) {
223 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
224
225 pci_unmap_page(mac->dma_pdev, dmas[f+1], frag->size,
226 PCI_DMA_TODEVICE);
227 }
228 dev_kfree_skb_irq(skb);
229
230 /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
231 * aligned up to a power of 2
232 */
233 return (nfrags + 3) & ~1;
234}
235
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600236static int pasemi_mac_setup_rx_resources(struct net_device *dev)
237{
238 struct pasemi_mac_rxring *ring;
239 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson34c20622007-11-28 20:56:32 -0600240 int chno;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500241 unsigned int cfg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600242
Olof Johansson34c20622007-11-28 20:56:32 -0600243 ring = pasemi_dma_alloc_chan(RXCHAN, sizeof(struct pasemi_mac_rxring),
244 offsetof(struct pasemi_mac_rxring, chan));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600245
Olof Johansson34c20622007-11-28 20:56:32 -0600246 if (!ring) {
247 dev_err(&mac->pdev->dev, "Can't allocate RX channel\n");
248 goto out_chan;
249 }
250 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600251
252 spin_lock_init(&ring->lock);
253
Olof Johansson021fa222007-08-22 09:13:11 -0500254 ring->size = RX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500255 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600256 RX_RING_SIZE, GFP_KERNEL);
257
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500258 if (!ring->ring_info)
259 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600260
261 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600262 if (pasemi_dma_alloc_ring(&ring->chan, RX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500263 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600264
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600265 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
266 RX_RING_SIZE * sizeof(u64),
267 &ring->buf_dma, GFP_KERNEL);
268 if (!ring->buffers)
Olof Johansson34c20622007-11-28 20:56:32 -0600269 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600270
271 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
272
Olof Johansson34c20622007-11-28 20:56:32 -0600273 write_dma_reg(PAS_DMA_RXCHAN_BASEL(chno),
274 PAS_DMA_RXCHAN_BASEL_BRBL(ring->chan.ring_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600275
Olof Johansson34c20622007-11-28 20:56:32 -0600276 write_dma_reg(PAS_DMA_RXCHAN_BASEU(chno),
277 PAS_DMA_RXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32) |
278 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600279
Olof Johansson34c20622007-11-28 20:56:32 -0600280 cfg = PAS_DMA_RXCHAN_CFG_HBU(1);
Olof Johanssonaf289e82007-10-03 13:03:54 -0500281
282 if (translation_enabled())
283 cfg |= PAS_DMA_RXCHAN_CFG_CTR;
284
Olof Johansson34c20622007-11-28 20:56:32 -0600285 write_dma_reg(PAS_DMA_RXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600286
Olof Johansson34c20622007-11-28 20:56:32 -0600287 write_dma_reg(PAS_DMA_RXINT_BASEL(mac->dma_if),
288 PAS_DMA_RXINT_BASEL_BRBL(ring->buf_dma));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600289
Olof Johansson34c20622007-11-28 20:56:32 -0600290 write_dma_reg(PAS_DMA_RXINT_BASEU(mac->dma_if),
291 PAS_DMA_RXINT_BASEU_BRBH(ring->buf_dma >> 32) |
292 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600293
Olof Johansson34c20622007-11-28 20:56:32 -0600294 cfg = PAS_DMA_RXINT_CFG_DHL(1) | PAS_DMA_RXINT_CFG_L2 |
Olof Johanssonaf289e82007-10-03 13:03:54 -0500295 PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP |
296 PAS_DMA_RXINT_CFG_HEN;
297
298 if (translation_enabled())
299 cfg |= PAS_DMA_RXINT_CFG_ITRR | PAS_DMA_RXINT_CFG_ITR;
300
Olof Johansson34c20622007-11-28 20:56:32 -0600301 write_dma_reg(PAS_DMA_RXINT_CFG(mac->dma_if), cfg);
Olof Johanssonc0efd522007-08-22 09:12:52 -0500302
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600303 ring->next_to_fill = 0;
304 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600305 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600306 mac->rx = ring;
307
308 return 0;
309
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500310out_ring_desc:
311 kfree(ring->ring_info);
312out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600313 pasemi_dma_free_chan(&ring->chan);
314out_chan:
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600315 return -ENOMEM;
316}
317
Olof Johansson72b05b92007-11-28 20:54:28 -0600318static struct pasemi_mac_txring *
Olof Johansson34c20622007-11-28 20:56:32 -0600319pasemi_mac_setup_tx_resources(struct net_device *dev)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600320{
321 struct pasemi_mac *mac = netdev_priv(dev);
322 u32 val;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600323 struct pasemi_mac_txring *ring;
Olof Johanssonaf289e82007-10-03 13:03:54 -0500324 unsigned int cfg;
Olof Johansson34c20622007-11-28 20:56:32 -0600325 int chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600326
Olof Johansson34c20622007-11-28 20:56:32 -0600327 ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_txring),
328 offsetof(struct pasemi_mac_txring, chan));
329
330 if (!ring) {
331 dev_err(&mac->pdev->dev, "Can't allocate TX channel\n");
332 goto out_chan;
333 }
334
335 chno = ring->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600336
337 spin_lock_init(&ring->lock);
338
Olof Johansson021fa222007-08-22 09:13:11 -0500339 ring->size = TX_RING_SIZE;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500340 ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600341 TX_RING_SIZE, GFP_KERNEL);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500342 if (!ring->ring_info)
343 goto out_ring_info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600344
345 /* Allocate descriptors */
Olof Johansson34c20622007-11-28 20:56:32 -0600346 if (pasemi_dma_alloc_ring(&ring->chan, TX_RING_SIZE))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500347 goto out_ring_desc;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600348
Olof Johansson34c20622007-11-28 20:56:32 -0600349 write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno),
350 PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma));
351 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500352 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600353
Olof Johansson34c20622007-11-28 20:56:32 -0600354 write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600355
Olof Johanssonaf289e82007-10-03 13:03:54 -0500356 cfg = PAS_DMA_TXCHAN_CFG_TY_IFACE |
357 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
358 PAS_DMA_TXCHAN_CFG_UP |
359 PAS_DMA_TXCHAN_CFG_WT(2);
360
361 if (translation_enabled())
362 cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
363
Olof Johansson34c20622007-11-28 20:56:32 -0600364 write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600365
Olof Johansson021fa222007-08-22 09:13:11 -0500366 ring->next_to_fill = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600367 ring->next_to_clean = 0;
Olof Johansson72b05b92007-11-28 20:54:28 -0600368 ring->mac = mac;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600369
Olof Johansson72b05b92007-11-28 20:54:28 -0600370 return ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600371
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500372out_ring_desc:
373 kfree(ring->ring_info);
374out_ring_info:
Olof Johansson34c20622007-11-28 20:56:32 -0600375 pasemi_dma_free_chan(&ring->chan);
376out_chan:
Olof Johansson72b05b92007-11-28 20:54:28 -0600377 return NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600378}
379
Olof Johansson72b05b92007-11-28 20:54:28 -0600380static void pasemi_mac_free_tx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600381{
Olof Johansson72b05b92007-11-28 20:54:28 -0600382 struct pasemi_mac_txring *txring = tx_ring(mac);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500383 unsigned int i, j;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600384 struct pasemi_mac_buffer *info;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500385 dma_addr_t dmas[MAX_SKB_FRAGS+1];
386 int freed;
Olof Johanssonad5da102007-10-02 16:27:15 -0500387 int start, limit;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600388
Olof Johansson72b05b92007-11-28 20:54:28 -0600389 start = txring->next_to_clean;
390 limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500391
392 /* Compensate for when fill has wrapped and clean has not */
393 if (start > limit)
394 limit += TX_RING_SIZE;
395
396 for (i = start; i < limit; i += freed) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600397 info = &txring->ring_info[(i+1) & (TX_RING_SIZE-1)];
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500398 if (info->dma && info->skb) {
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500399 for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600400 dmas[j] = txring->ring_info[(i+1+j) &
401 (TX_RING_SIZE-1)].dma;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500402 freed = pasemi_mac_unmap_tx_skb(mac, info->skb, dmas);
403 } else
404 freed = 2;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600405 }
406
Olof Johansson72b05b92007-11-28 20:54:28 -0600407 kfree(txring->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600408 pasemi_dma_free_chan(&txring->chan);
409
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600410}
411
Olof Johansson72b05b92007-11-28 20:54:28 -0600412static void pasemi_mac_free_rx_resources(struct pasemi_mac *mac)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600413{
Olof Johansson72b05b92007-11-28 20:54:28 -0600414 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600415 unsigned int i;
416 struct pasemi_mac_buffer *info;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600417
418 for (i = 0; i < RX_RING_SIZE; i++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600419 info = &RX_DESC_INFO(rx, i);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500420 if (info->skb && info->dma) {
421 pci_unmap_single(mac->dma_pdev,
422 info->dma,
423 info->skb->len,
424 PCI_DMA_FROMDEVICE);
425 dev_kfree_skb_any(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600426 }
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500427 info->dma = 0;
428 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600429 }
430
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500431 for (i = 0; i < RX_RING_SIZE; i++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600432 RX_DESC(rx, i) = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500433
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600434 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
Olof Johansson72b05b92007-11-28 20:54:28 -0600435 rx_ring(mac)->buffers, rx_ring(mac)->buf_dma);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600436
Olof Johansson72b05b92007-11-28 20:54:28 -0600437 kfree(rx_ring(mac)->ring_info);
Olof Johansson34c20622007-11-28 20:56:32 -0600438 pasemi_dma_free_chan(&rx_ring(mac)->chan);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600439 mac->rx = NULL;
440}
441
Olof Johansson928773c2007-09-26 16:25:06 -0500442static void pasemi_mac_replenish_rx_ring(struct net_device *dev, int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600443{
444 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -0600445 struct pasemi_mac_rxring *rx = rx_ring(mac);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500446 int fill, count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600447
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500448 if (limit <= 0)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600449 return;
450
Olof Johansson72b05b92007-11-28 20:54:28 -0600451 fill = rx_ring(mac)->next_to_fill;
Olof Johansson928773c2007-09-26 16:25:06 -0500452 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600453 struct pasemi_mac_buffer *info = &RX_DESC_INFO(rx, fill);
454 u64 *buff = &RX_BUFF(rx, fill);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600455 struct sk_buff *skb;
456 dma_addr_t dma;
457
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500458 /* Entry in use? */
459 WARN_ON(*buff);
460
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500461 /* skb might still be in there for recycle on short receives */
462 if (info->skb)
463 skb = info->skb;
Olof Johansson8dc121a2007-10-02 16:26:53 -0500464 else {
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500465 skb = dev_alloc_skb(BUF_SIZE);
Olof Johansson8dc121a2007-10-02 16:26:53 -0500466 skb_reserve(skb, LOCAL_SKB_ALIGN);
467 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600468
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500469 if (unlikely(!skb))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600470 break;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600471
Olof Johansson8dc121a2007-10-02 16:26:53 -0500472 dma = pci_map_single(mac->dma_pdev, skb->data,
473 BUF_SIZE - LOCAL_SKB_ALIGN,
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600474 PCI_DMA_FROMDEVICE);
475
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500476 if (unlikely(dma_mapping_error(dma))) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600477 dev_kfree_skb_irq(info->skb);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600478 break;
479 }
480
481 info->skb = skb;
482 info->dma = dma;
483 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500484 fill++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600485 }
486
487 wmb();
488
Olof Johansson34c20622007-11-28 20:56:32 -0600489 write_dma_reg(PAS_DMA_RXINT_INCR(mac->dma_if), count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600490
Olof Johansson72b05b92007-11-28 20:54:28 -0600491 rx_ring(mac)->next_to_fill = (rx_ring(mac)->next_to_fill + count) &
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500492 (RX_RING_SIZE - 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600493}
494
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500495static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
496{
Olof Johansson52a94352007-05-12 18:01:09 -0500497 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500498 /* Re-enable packet count interrupts: finally
499 * ack the packet count interrupt we got in rx_intr.
500 */
501
Olof Johansson34c20622007-11-28 20:56:32 -0600502 pcnt = *rx_ring(mac)->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500503
Olof Johansson52a94352007-05-12 18:01:09 -0500504 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500505
Olof Johansson34c20622007-11-28 20:56:32 -0600506 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(mac->rx->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500507}
508
509static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
510{
Olof Johansson52a94352007-05-12 18:01:09 -0500511 unsigned int reg, pcnt;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500512
513 /* Re-enable packet count interrupts */
Olof Johansson34c20622007-11-28 20:56:32 -0600514 pcnt = *tx_ring(mac)->chan.status & PAS_STATUS_PCNT_M;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500515
Olof Johansson52a94352007-05-12 18:01:09 -0500516 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500517
Olof Johansson34c20622007-11-28 20:56:32 -0600518 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(tx_ring(mac)->chan.chno), reg);
Olof Johansson1b0335ea2007-05-08 00:47:26 -0500519}
520
521
Olof Johansson69c29d82007-10-02 16:24:51 -0500522static inline void pasemi_mac_rx_error(struct pasemi_mac *mac, u64 macrx)
523{
524 unsigned int rcmdsta, ccmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600525 struct pasemi_dmachan *chan = &rx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500526
527 if (!netif_msg_rx_err(mac))
528 return;
529
Olof Johansson34c20622007-11-28 20:56:32 -0600530 rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
531 ccmdsta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500532
533 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
Olof Johansson34c20622007-11-28 20:56:32 -0600534 macrx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500535
536 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
537 rcmdsta, ccmdsta);
538}
539
540static inline void pasemi_mac_tx_error(struct pasemi_mac *mac, u64 mactx)
541{
542 unsigned int cmdsta;
Olof Johansson34c20622007-11-28 20:56:32 -0600543 struct pasemi_dmachan *chan = &tx_ring(mac)->chan;
Olof Johansson69c29d82007-10-02 16:24:51 -0500544
545 if (!netif_msg_tx_err(mac))
546 return;
547
Olof Johansson34c20622007-11-28 20:56:32 -0600548 cmdsta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan->chno));
Olof Johansson69c29d82007-10-02 16:24:51 -0500549
550 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
Olof Johansson34c20622007-11-28 20:56:32 -0600551 "tx status 0x%016lx\n", mactx, *chan->status);
Olof Johansson69c29d82007-10-02 16:24:51 -0500552
553 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
554}
555
Olof Johansson72b05b92007-11-28 20:54:28 -0600556static int pasemi_mac_clean_rx(struct pasemi_mac_rxring *rx, int limit)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600557{
Olof Johansson34c20622007-11-28 20:56:32 -0600558 struct pasemi_dmachan *chan = &rx->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600559 struct pasemi_mac *mac = rx->mac;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500560 unsigned int n;
561 int count;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500562 struct pasemi_mac_buffer *info;
563 struct sk_buff *skb;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500564 unsigned int len;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500565 u64 macrx;
566 dma_addr_t dma;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500567 int buf_index;
568 u64 eval;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600569
Olof Johansson72b05b92007-11-28 20:54:28 -0600570 spin_lock(&rx->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600571
Olof Johansson72b05b92007-11-28 20:54:28 -0600572 n = rx->next_to_clean;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600573
Olof Johansson72b05b92007-11-28 20:54:28 -0600574 prefetch(&RX_DESC(rx, n));
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500575
576 for (count = 0; count < limit; count++) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600577 macrx = RX_DESC(rx, n);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600578
Olof Johansson69c29d82007-10-02 16:24:51 -0500579 if ((macrx & XCT_MACRX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600580 (*chan->status & PAS_STATUS_ERROR))
Olof Johansson69c29d82007-10-02 16:24:51 -0500581 pasemi_mac_rx_error(mac, macrx);
582
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500583 if (!(macrx & XCT_MACRX_O))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600584 break;
585
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600586 info = NULL;
587
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500588 BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600589
Olof Johansson72b05b92007-11-28 20:54:28 -0600590 eval = (RX_DESC(rx, n+1) & XCT_RXRES_8B_EVAL_M) >>
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500591 XCT_RXRES_8B_EVAL_S;
592 buf_index = eval-1;
593
Olof Johansson72b05b92007-11-28 20:54:28 -0600594 dma = (RX_DESC(rx, n+2) & XCT_PTR_ADDR_M);
595 info = &RX_DESC_INFO(rx, buf_index);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500596
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500597 skb = info->skb;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600598
Olof Johanssonad5da102007-10-02 16:27:15 -0500599 prefetch(skb);
600 prefetch(&skb->data_len);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600601
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500602 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600603
Olof Johansson32bee772007-11-06 22:21:38 -0600604 pci_unmap_single(mac->dma_pdev, dma, len, PCI_DMA_FROMDEVICE);
605
606 if (macrx & XCT_MACRX_CRC) {
607 /* CRC error flagged */
608 mac->netdev->stats.rx_errors++;
609 mac->netdev->stats.rx_crc_errors++;
Olof Johansson4352d822007-12-03 21:34:14 -0600610 /* No need to free skb, it'll be reused */
Olof Johansson32bee772007-11-06 22:21:38 -0600611 goto next;
612 }
613
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500614 if (len < 256) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500615 struct sk_buff *new_skb;
616
617 new_skb = netdev_alloc_skb(mac->netdev,
618 len + LOCAL_SKB_ALIGN);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500619 if (new_skb) {
Olof Johansson8dc121a2007-10-02 16:26:53 -0500620 skb_reserve(new_skb, LOCAL_SKB_ALIGN);
Olof Johansson73344862007-08-22 09:12:55 -0500621 memcpy(new_skb->data, skb->data, len);
Olof Johansson9f05cfe2007-05-08 00:47:37 -0500622 /* save the skb in buffer_info as good */
623 skb = new_skb;
624 }
625 /* else just continue with the old one */
626 } else
627 info->skb = NULL;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600628
Olof Johanssonad5da102007-10-02 16:27:15 -0500629 info->dma = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500630
Olof Johansson32bee772007-11-06 22:21:38 -0600631 /* Don't include CRC */
632 skb_put(skb, len-4);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600633
Olof Johansson26fcfa92007-08-22 09:12:59 -0500634 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
Olof Johansson38bf3182007-08-22 09:13:24 -0500635 skb->ip_summed = CHECKSUM_UNNECESSARY;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500636 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600637 XCT_MACRX_CSUM_S;
638 } else
639 skb->ip_summed = CHECKSUM_NONE;
640
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700641 mac->netdev->stats.rx_bytes += len;
642 mac->netdev->stats.rx_packets++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600643
Olof Johansson26fcfa92007-08-22 09:12:59 -0500644 skb->protocol = eth_type_trans(skb, mac->netdev);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600645 netif_receive_skb(skb);
646
Olof Johansson32bee772007-11-06 22:21:38 -0600647next:
Olof Johansson72b05b92007-11-28 20:54:28 -0600648 RX_DESC(rx, n) = 0;
649 RX_DESC(rx, n+1) = 0;
Olof Johanssoncd4ceb22007-05-08 00:47:45 -0500650
Olof Johanssonad5da102007-10-02 16:27:15 -0500651 /* Need to zero it out since hardware doesn't, since the
652 * replenish loop uses it to tell when it's done.
653 */
Olof Johansson72b05b92007-11-28 20:54:28 -0600654 RX_BUFF(rx, buf_index) = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500655
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500656 n += 4;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600657 }
658
Olof Johansson9a50beb2007-10-02 16:26:30 -0500659 if (n > RX_RING_SIZE) {
660 /* Errata 5971 workaround: L2 target of headers */
Olof Johansson34c20622007-11-28 20:56:32 -0600661 write_iob_reg(PAS_IOB_COM_PKTHDRCNT, 0);
Olof Johansson9a50beb2007-10-02 16:26:30 -0500662 n &= (RX_RING_SIZE-1);
663 }
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500664
Olof Johansson72b05b92007-11-28 20:54:28 -0600665 rx_ring(mac)->next_to_clean = n;
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500666
667 /* Increase is in number of 16-byte entries, and since each descriptor
668 * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
669 * count*2.
670 */
Olof Johansson34c20622007-11-28 20:56:32 -0600671 write_dma_reg(PAS_DMA_RXCHAN_INCR(mac->rx->chan.chno), count << 1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500672
673 pasemi_mac_replenish_rx_ring(mac->netdev, count);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600674
Olof Johansson72b05b92007-11-28 20:54:28 -0600675 spin_unlock(&rx_ring(mac)->lock);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600676
677 return count;
678}
679
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500680/* Can't make this too large or we blow the kernel stack limits */
681#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
682
Olof Johansson72b05b92007-11-28 20:54:28 -0600683static int pasemi_mac_clean_tx(struct pasemi_mac_txring *txring)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600684{
Olof Johansson34c20622007-11-28 20:56:32 -0600685 struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson72b05b92007-11-28 20:54:28 -0600686 struct pasemi_mac *mac = txring->mac;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500687 int i, j;
Olof Johanssonad5da102007-10-02 16:27:15 -0500688 unsigned int start, descr_count, buf_count, batch_limit;
689 unsigned int ring_limit;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500690 unsigned int total_count;
Olof Johanssonca7e2352007-09-26 16:23:59 -0500691 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500692 struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
693 dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600694
Olof Johansson02df6cf2007-08-22 09:13:03 -0500695 total_count = 0;
Olof Johanssonad5da102007-10-02 16:27:15 -0500696 batch_limit = TX_CLEAN_BATCHSIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500697restart:
Olof Johansson72b05b92007-11-28 20:54:28 -0600698 spin_lock_irqsave(&txring->lock, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600699
Olof Johansson72b05b92007-11-28 20:54:28 -0600700 start = txring->next_to_clean;
701 ring_limit = txring->next_to_fill;
Olof Johanssonad5da102007-10-02 16:27:15 -0500702
703 /* Compensate for when fill has wrapped but clean has not */
704 if (start > ring_limit)
705 ring_limit += TX_RING_SIZE;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500706
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500707 buf_count = 0;
708 descr_count = 0;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600709
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500710 for (i = start;
Olof Johanssonad5da102007-10-02 16:27:15 -0500711 descr_count < batch_limit && i < ring_limit;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500712 i += buf_count) {
Olof Johansson72b05b92007-11-28 20:54:28 -0600713 u64 mactx = TX_DESC(txring, i);
Olof Johanssonad5da102007-10-02 16:27:15 -0500714 struct sk_buff *skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500715
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500716 if ((mactx & XCT_MACTX_E) ||
Olof Johansson34c20622007-11-28 20:56:32 -0600717 (*chan->status & PAS_STATUS_ERROR))
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500718 pasemi_mac_tx_error(mac, mactx);
Olof Johansson69c29d82007-10-02 16:24:51 -0500719
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500720 if (unlikely(mactx & XCT_MACTX_O))
Olof Johansson02df6cf2007-08-22 09:13:03 -0500721 /* Not yet transmitted */
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600722 break;
723
Olof Johansson72b05b92007-11-28 20:54:28 -0600724 skb = TX_DESC_INFO(txring, i+1).skb;
Olof Johanssonad5da102007-10-02 16:27:15 -0500725 skbs[descr_count] = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500726
Olof Johanssonad5da102007-10-02 16:27:15 -0500727 buf_count = 2 + skb_shinfo(skb)->nr_frags;
728 for (j = 0; j <= skb_shinfo(skb)->nr_frags; j++)
Olof Johansson72b05b92007-11-28 20:54:28 -0600729 dmas[descr_count][j] = TX_DESC_INFO(txring, i+1+j).dma;
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500730
Olof Johansson72b05b92007-11-28 20:54:28 -0600731 TX_DESC(txring, i) = 0;
732 TX_DESC(txring, i+1) = 0;
Olof Johanssonfc9e4d22007-10-02 16:25:53 -0500733
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500734 /* Since we always fill with an even number of entries, make
735 * sure we skip any unused one at the end as well.
736 */
737 if (buf_count & 1)
738 buf_count++;
739 descr_count++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600740 }
Olof Johansson72b05b92007-11-28 20:54:28 -0600741 txring->next_to_clean = i & (TX_RING_SIZE-1);
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500742
Olof Johansson72b05b92007-11-28 20:54:28 -0600743 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500744 netif_wake_queue(mac->netdev);
745
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500746 for (i = 0; i < descr_count; i++)
747 pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]);
Olof Johansson02df6cf2007-08-22 09:13:03 -0500748
Olof Johanssonad3c20d2007-10-02 16:26:13 -0500749 total_count += descr_count;
Olof Johansson02df6cf2007-08-22 09:13:03 -0500750
751 /* If the batch was full, try to clean more */
Olof Johanssonad5da102007-10-02 16:27:15 -0500752 if (descr_count == batch_limit)
Olof Johansson02df6cf2007-08-22 09:13:03 -0500753 goto restart;
754
755 return total_count;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600756}
757
758
759static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
760{
Olof Johansson34c20622007-11-28 20:56:32 -0600761 struct pasemi_mac_rxring *rxring = data;
762 struct pasemi_mac *mac = rxring->mac;
763 struct net_device *dev = mac->netdev;
764 struct pasemi_dmachan *chan = &rxring->chan;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600765 unsigned int reg;
766
Olof Johansson34c20622007-11-28 20:56:32 -0600767 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600768 return IRQ_NONE;
769
Olof Johansson6dfa7522007-05-08 00:47:32 -0500770 /* Don't reset packet count so it won't fire again but clear
771 * all others.
772 */
773
Olof Johansson6dfa7522007-05-08 00:47:32 -0500774 reg = 0;
Olof Johansson34c20622007-11-28 20:56:32 -0600775 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500776 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600777 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500778 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600779 if (*chan->status & PAS_STATUS_TIMER)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600780 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
781
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700782 netif_rx_schedule(dev, &mac->napi);
Olof Johansson6dfa7522007-05-08 00:47:32 -0500783
Olof Johansson34c20622007-11-28 20:56:32 -0600784 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600785
786 return IRQ_HANDLED;
787}
788
789static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
790{
Olof Johansson72b05b92007-11-28 20:54:28 -0600791 struct pasemi_mac_txring *txring = data;
Olof Johansson34c20622007-11-28 20:56:32 -0600792 struct pasemi_dmachan *chan = &txring->chan;
Olof Johansson52a94352007-05-12 18:01:09 -0500793 unsigned int reg, pcnt;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600794
Olof Johansson34c20622007-11-28 20:56:32 -0600795 if (!(*chan->status & PAS_STATUS_CAUSE_M))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600796 return IRQ_NONE;
797
Olof Johansson72b05b92007-11-28 20:54:28 -0600798 pasemi_mac_clean_tx(txring);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600799
Olof Johansson34c20622007-11-28 20:56:32 -0600800 pcnt = *chan->status & PAS_STATUS_PCNT_M;
Olof Johansson52a94352007-05-12 18:01:09 -0500801
802 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
Olof Johansson6dfa7522007-05-08 00:47:32 -0500803
Olof Johansson34c20622007-11-28 20:56:32 -0600804 if (*chan->status & PAS_STATUS_SOFT)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500805 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
Olof Johansson34c20622007-11-28 20:56:32 -0600806 if (*chan->status & PAS_STATUS_ERROR)
Olof Johansson6dfa7522007-05-08 00:47:32 -0500807 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600808
Olof Johansson34c20622007-11-28 20:56:32 -0600809 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(chan->chno), reg);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600810
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600811 return IRQ_HANDLED;
812}
813
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500814static void pasemi_adjust_link(struct net_device *dev)
815{
816 struct pasemi_mac *mac = netdev_priv(dev);
817 int msg;
818 unsigned int flags;
819 unsigned int new_flags;
820
821 if (!mac->phydev->link) {
822 /* If no link, MAC speed settings don't matter. Just report
823 * link down and return.
824 */
825 if (mac->link && netif_msg_link(mac))
826 printk(KERN_INFO "%s: Link is down.\n", dev->name);
827
828 netif_carrier_off(dev);
829 mac->link = 0;
830
831 return;
832 } else
833 netif_carrier_on(dev);
834
Olof Johanssona85b9422007-09-15 13:40:59 -0700835 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500836 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
837 PAS_MAC_CFG_PCFG_TSR_M);
838
839 if (!mac->phydev->duplex)
840 new_flags |= PAS_MAC_CFG_PCFG_HD;
841
842 switch (mac->phydev->speed) {
843 case 1000:
844 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
845 PAS_MAC_CFG_PCFG_TSR_1G;
846 break;
847 case 100:
848 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
849 PAS_MAC_CFG_PCFG_TSR_100M;
850 break;
851 case 10:
852 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
853 PAS_MAC_CFG_PCFG_TSR_10M;
854 break;
855 default:
856 printk("Unsupported speed %d\n", mac->phydev->speed);
857 }
858
859 /* Print on link or speed/duplex change */
860 msg = mac->link != mac->phydev->link || flags != new_flags;
861
862 mac->duplex = mac->phydev->duplex;
863 mac->speed = mac->phydev->speed;
864 mac->link = mac->phydev->link;
865
866 if (new_flags != flags)
Olof Johanssona85b9422007-09-15 13:40:59 -0700867 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500868
869 if (msg && netif_msg_link(mac))
870 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
871 dev->name, mac->speed, mac->duplex ? "full" : "half");
872}
873
874static int pasemi_mac_phy_init(struct net_device *dev)
875{
876 struct pasemi_mac *mac = netdev_priv(dev);
877 struct device_node *dn, *phy_dn;
878 struct phy_device *phydev;
879 unsigned int phy_id;
880 const phandle *ph;
881 const unsigned int *prop;
882 struct resource r;
883 int ret;
884
885 dn = pci_device_to_OF_node(mac->pdev);
Linus Torvalds90287802007-05-08 11:57:17 -0700886 ph = of_get_property(dn, "phy-handle", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500887 if (!ph)
888 return -ENODEV;
889 phy_dn = of_find_node_by_phandle(*ph);
890
Linus Torvalds90287802007-05-08 11:57:17 -0700891 prop = of_get_property(phy_dn, "reg", NULL);
Olof Johanssonbb6e9592007-05-08 00:47:54 -0500892 ret = of_address_to_resource(phy_dn->parent, 0, &r);
893 if (ret)
894 goto err;
895
896 phy_id = *prop;
897 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
898
899 of_node_put(phy_dn);
900
901 mac->link = 0;
902 mac->speed = 0;
903 mac->duplex = -1;
904
905 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
906
907 if (IS_ERR(phydev)) {
908 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
909 return PTR_ERR(phydev);
910 }
911
912 mac->phydev = phydev;
913
914 return 0;
915
916err:
917 of_node_put(phy_dn);
918 return -ENODEV;
919}
920
921
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600922static int pasemi_mac_open(struct net_device *dev)
923{
924 struct pasemi_mac *mac = netdev_priv(dev);
925 unsigned int flags;
926 int ret;
927
928 /* enable rx section */
Olof Johansson34c20622007-11-28 20:56:32 -0600929 write_dma_reg(PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600930
931 /* enable tx section */
Olof Johansson34c20622007-11-28 20:56:32 -0600932 write_dma_reg(PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600933
934 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
935 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
936 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
937
Olof Johanssona85b9422007-09-15 13:40:59 -0700938 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600939
Olof Johansson6dfa7522007-05-08 00:47:32 -0500940 /* 0xffffff is max value, about 16ms */
Olof Johansson34c20622007-11-28 20:56:32 -0600941 write_iob_reg(PAS_IOB_DMA_COM_TIMEOUTCFG,
942 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600943
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600944 ret = pasemi_mac_setup_rx_resources(dev);
945 if (ret)
946 goto out_rx_resources;
947
Olof Johansson34c20622007-11-28 20:56:32 -0600948 mac->tx = pasemi_mac_setup_tx_resources(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -0600949
950 if (!mac->tx)
951 goto out_tx_ring;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600952
Olof Johansson34c20622007-11-28 20:56:32 -0600953 write_iob_reg(PAS_IOB_DMA_RXCH_CFG(mac->rx->chan.chno),
954 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
955
956 write_iob_reg(PAS_IOB_DMA_TXCH_CFG(mac->tx->chan.chno),
957 PAS_IOB_DMA_TXCH_CFG_CNTTH(128));
958
Olof Johanssona85b9422007-09-15 13:40:59 -0700959 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
Olof Johansson34c20622007-11-28 20:56:32 -0600960 PAS_MAC_IPC_CHNL_DCHNO(mac->rx->chan.chno) |
961 PAS_MAC_IPC_CHNL_BCH(mac->rx->chan.chno));
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600962
963 /* enable rx if */
Olof Johansson34c20622007-11-28 20:56:32 -0600964 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
965 PAS_DMA_RXINT_RCMDSTA_EN |
966 PAS_DMA_RXINT_RCMDSTA_DROPS_M |
967 PAS_DMA_RXINT_RCMDSTA_BP |
968 PAS_DMA_RXINT_RCMDSTA_OO |
969 PAS_DMA_RXINT_RCMDSTA_BT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600970
971 /* enable rx channel */
Olof Johansson34c20622007-11-28 20:56:32 -0600972 pasemi_dma_start_chan(&rx_ring(mac)->chan, PAS_DMA_RXCHAN_CCMDSTA_DU |
973 PAS_DMA_RXCHAN_CCMDSTA_OD |
974 PAS_DMA_RXCHAN_CCMDSTA_FD |
975 PAS_DMA_RXCHAN_CCMDSTA_DT);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600976
977 /* enable tx channel */
Olof Johansson34c20622007-11-28 20:56:32 -0600978 pasemi_dma_start_chan(&tx_ring(mac)->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
979 PAS_DMA_TXCHAN_TCMDSTA_DB |
980 PAS_DMA_TXCHAN_TCMDSTA_DE |
981 PAS_DMA_TXCHAN_TCMDSTA_DA);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600982
Olof Johansson928773c2007-09-26 16:25:06 -0500983 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600984
Olof Johansson34c20622007-11-28 20:56:32 -0600985 write_dma_reg(PAS_DMA_RXCHAN_INCR(rx_ring(mac)->chan.chno),
986 RX_RING_SIZE>>1);
Olof Johanssonb5254ee2007-10-02 16:27:57 -0500987
Olof Johansson72b05b92007-11-28 20:54:28 -0600988 /* Clear out any residual packet count state from firmware */
989 pasemi_mac_restart_rx_intr(mac);
990 pasemi_mac_restart_tx_intr(mac);
991
Olof Johansson36033762007-09-26 16:24:42 -0500992 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
993 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
994
995 if (mac->type == MAC_TYPE_GMAC)
996 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
997 else
998 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
999
1000 /* Enable interface in MAC */
1001 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
1002
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001003 ret = pasemi_mac_phy_init(dev);
1004 /* Some configs don't have PHYs (XAUI etc), so don't complain about
1005 * failed init due to -ENODEV.
1006 */
1007 if (ret && ret != -ENODEV)
1008 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
1009
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001010 netif_start_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001011 napi_enable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001012
Olof Johansson72b05b92007-11-28 20:54:28 -06001013 snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx",
1014 dev->name);
Olof Johansson771f7402007-05-08 00:47:21 -05001015
Olof Johansson34c20622007-11-28 20:56:32 -06001016 ret = request_irq(mac->tx->chan.irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
Olof Johansson72b05b92007-11-28 20:54:28 -06001017 mac->tx_irq_name, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001018 if (ret) {
1019 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001020 mac->tx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001021 goto out_tx_int;
1022 }
1023
Olof Johansson72b05b92007-11-28 20:54:28 -06001024 snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx",
1025 dev->name);
1026
Olof Johansson34c20622007-11-28 20:56:32 -06001027 ret = request_irq(mac->rx->chan.irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
1028 mac->rx_irq_name, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001029 if (ret) {
1030 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
Olof Johansson34c20622007-11-28 20:56:32 -06001031 mac->rx->chan.irq, ret);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001032 goto out_rx_int;
1033 }
1034
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001035 if (mac->phydev)
1036 phy_start(mac->phydev);
1037
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001038 return 0;
1039
1040out_rx_int:
Olof Johansson34c20622007-11-28 20:56:32 -06001041 free_irq(mac->tx->chan.irq, mac->tx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001042out_tx_int:
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001043 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001044 netif_stop_queue(dev);
Olof Johansson72b05b92007-11-28 20:54:28 -06001045out_tx_ring:
1046 if (mac->tx)
1047 pasemi_mac_free_tx_resources(mac);
1048 pasemi_mac_free_rx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001049out_rx_resources:
1050
1051 return ret;
1052}
1053
1054#define MAX_RETRIES 5000
1055
1056static int pasemi_mac_close(struct net_device *dev)
1057{
1058 struct pasemi_mac *mac = netdev_priv(dev);
Olof Johansson9e81d332007-10-02 16:27:39 -05001059 unsigned int sta;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001060 int retries;
Olof Johansson34c20622007-11-28 20:56:32 -06001061 int rxch, txch;
1062
1063 rxch = rx_ring(mac)->chan.chno;
1064 txch = tx_ring(mac)->chan.chno;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001065
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001066 if (mac->phydev) {
1067 phy_stop(mac->phydev);
1068 phy_disconnect(mac->phydev);
1069 }
1070
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001071 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001072 napi_disable(&mac->napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001073
Olof Johansson34c20622007-11-28 20:56:32 -06001074 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson9e81d332007-10-02 16:27:39 -05001075 if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
1076 PAS_DMA_RXINT_RCMDSTA_OO |
1077 PAS_DMA_RXINT_RCMDSTA_BT))
1078 printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);
1079
Olof Johansson34c20622007-11-28 20:56:32 -06001080 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001081 if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
1082 PAS_DMA_RXCHAN_CCMDSTA_OD |
1083 PAS_DMA_RXCHAN_CCMDSTA_FD |
1084 PAS_DMA_RXCHAN_CCMDSTA_DT))
1085 printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);
1086
Olof Johansson34c20622007-11-28 20:56:32 -06001087 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
Olof Johansson72b05b92007-11-28 20:54:28 -06001088 if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB |
1089 PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA))
Olof Johansson9e81d332007-10-02 16:27:39 -05001090 printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);
1091
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001092 /* Clean out any pending buffers */
Olof Johansson72b05b92007-11-28 20:54:28 -06001093 pasemi_mac_clean_tx(tx_ring(mac));
1094 pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001095
1096 /* Disable interface */
Olof Johansson34c20622007-11-28 20:56:32 -06001097 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch),
Olof Johansson72b05b92007-11-28 20:54:28 -06001098 PAS_DMA_TXCHAN_TCMDSTA_ST);
Olof Johansson34c20622007-11-28 20:56:32 -06001099 write_dma_reg( PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
Olof Johansson72b05b92007-11-28 20:54:28 -06001100 PAS_DMA_RXINT_RCMDSTA_ST);
Olof Johansson34c20622007-11-28 20:56:32 -06001101 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch),
Olof Johansson72b05b92007-11-28 20:54:28 -06001102 PAS_DMA_RXCHAN_CCMDSTA_ST);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001103
1104 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001105 sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001106 if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001107 break;
1108 cond_resched();
1109 }
1110
Olof Johansson9e81d332007-10-02 16:27:39 -05001111 if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johansson34c20622007-11-28 20:56:32 -06001112 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001113
1114 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001115 sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
Olof Johansson9e81d332007-10-02 16:27:39 -05001116 if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001117 break;
1118 cond_resched();
1119 }
1120
Olof Johansson9e81d332007-10-02 16:27:39 -05001121 if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001122 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001123
1124 for (retries = 0; retries < MAX_RETRIES; retries++) {
Olof Johansson34c20622007-11-28 20:56:32 -06001125 sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
Olof Johansson9e81d332007-10-02 16:27:39 -05001126 if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001127 break;
1128 cond_resched();
1129 }
1130
Olof Johansson9e81d332007-10-02 16:27:39 -05001131 if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001132 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001133
1134 /* Then, disable the channel. This must be done separately from
1135 * stopping, since you can't disable when active.
1136 */
1137
Olof Johansson34c20622007-11-28 20:56:32 -06001138 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch), 0);
1139 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch), 0);
1140 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001141
Olof Johansson34c20622007-11-28 20:56:32 -06001142 free_irq(mac->tx->chan.irq, mac->tx);
1143 free_irq(mac->rx->chan.irq, mac->rx);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001144
1145 /* Free resources */
Olof Johansson72b05b92007-11-28 20:54:28 -06001146 pasemi_mac_free_rx_resources(mac);
1147 pasemi_mac_free_tx_resources(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001148
1149 return 0;
1150}
1151
1152static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1153{
1154 struct pasemi_mac *mac = netdev_priv(dev);
1155 struct pasemi_mac_txring *txring;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001156 u64 dflags, mactx;
1157 dma_addr_t map[MAX_SKB_FRAGS+1];
1158 unsigned int map_size[MAX_SKB_FRAGS+1];
Olof Johanssonca7e2352007-09-26 16:23:59 -05001159 unsigned long flags;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001160 int i, nfrags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001161
Olof Johanssondbd62af2007-11-06 22:20:39 -06001162 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001163
1164 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001165 const unsigned char *nh = skb_network_header(skb);
1166
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001167 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001168 case IPPROTO_TCP:
1169 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001170 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001171 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001172 break;
1173 case IPPROTO_UDP:
1174 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001175 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001176 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001177 break;
1178 }
1179 }
1180
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001181 nfrags = skb_shinfo(skb)->nr_frags;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001182
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001183 map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1184 PCI_DMA_TODEVICE);
1185 map_size[0] = skb_headlen(skb);
1186 if (dma_mapping_error(map[0]))
1187 goto out_err_nolock;
1188
1189 for (i = 0; i < nfrags; i++) {
1190 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1191
1192 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1193 frag->page_offset, frag->size,
1194 PCI_DMA_TODEVICE);
1195 map_size[i+1] = frag->size;
1196 if (dma_mapping_error(map[i+1])) {
1197 nfrags = i;
1198 goto out_err_nolock;
1199 }
1200 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001201
Olof Johansson26fcfa92007-08-22 09:12:59 -05001202 mactx = dflags | XCT_MACTX_LLEN(skb->len);
Olof Johansson26fcfa92007-08-22 09:12:59 -05001203
Olof Johansson72b05b92007-11-28 20:54:28 -06001204 txring = tx_ring(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001205
1206 spin_lock_irqsave(&txring->lock, flags);
1207
Olof Johanssonad5da102007-10-02 16:27:15 -05001208 /* Avoid stepping on the same cache line that the DMA controller
1209 * is currently about to send, so leave at least 8 words available.
1210 * Total free space needed is mactx + fragments + 8
1211 */
1212 if (RING_AVAIL(txring) < nfrags + 10) {
1213 /* no room -- stop the queue and wait for tx intr */
1214 netif_stop_queue(dev);
1215 goto out_err;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001216 }
1217
Olof Johansson72b05b92007-11-28 20:54:28 -06001218 TX_DESC(txring, txring->next_to_fill) = mactx;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001219 txring->next_to_fill++;
Olof Johansson72b05b92007-11-28 20:54:28 -06001220 TX_DESC_INFO(txring, txring->next_to_fill).skb = skb;
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001221 for (i = 0; i <= nfrags; i++) {
Olof Johansson72b05b92007-11-28 20:54:28 -06001222 TX_DESC(txring, txring->next_to_fill+i) =
1223 XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
1224 TX_DESC_INFO(txring, txring->next_to_fill+i).dma = map[i];
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001225 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001226
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001227 /* We have to add an even number of 8-byte entries to the ring
1228 * even if the last one is unused. That means always an odd number
1229 * of pointers + one mactx descriptor.
1230 */
1231 if (nfrags & 1)
1232 nfrags++;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001233
Olof Johanssonad5da102007-10-02 16:27:15 -05001234 txring->next_to_fill = (txring->next_to_fill + nfrags + 1) &
1235 (TX_RING_SIZE-1);
Olof Johanssonfc9e4d22007-10-02 16:25:53 -05001236
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001237 dev->stats.tx_packets++;
1238 dev->stats.tx_bytes += skb->len;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001239
1240 spin_unlock_irqrestore(&txring->lock, flags);
1241
Olof Johansson34c20622007-11-28 20:56:32 -06001242 write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), (nfrags+2) >> 1);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001243
1244 return NETDEV_TX_OK;
1245
1246out_err:
1247 spin_unlock_irqrestore(&txring->lock, flags);
Olof Johanssonad3c20d2007-10-02 16:26:13 -05001248out_err_nolock:
1249 while (nfrags--)
1250 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1251 PCI_DMA_TODEVICE);
1252
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001253 return NETDEV_TX_BUSY;
1254}
1255
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001256static void pasemi_mac_set_rx_mode(struct net_device *dev)
1257{
1258 struct pasemi_mac *mac = netdev_priv(dev);
1259 unsigned int flags;
1260
Olof Johanssona85b9422007-09-15 13:40:59 -07001261 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001262
1263 /* Set promiscuous */
1264 if (dev->flags & IFF_PROMISC)
1265 flags |= PAS_MAC_CFG_PCFG_PR;
1266 else
1267 flags &= ~PAS_MAC_CFG_PCFG_PR;
1268
Olof Johanssona85b9422007-09-15 13:40:59 -07001269 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001270}
1271
1272
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001273static int pasemi_mac_poll(struct napi_struct *napi, int budget)
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001274{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001275 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1276 struct net_device *dev = mac->netdev;
1277 int pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001278
Olof Johansson72b05b92007-11-28 20:54:28 -06001279 pasemi_mac_clean_tx(tx_ring(mac));
1280 pkts = pasemi_mac_clean_rx(rx_ring(mac), budget);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001281 if (pkts < budget) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001282 /* all done, no more packets present */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001283 netif_rx_complete(dev, napi);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001284
Olof Johansson1b0335ea2007-05-08 00:47:26 -05001285 pasemi_mac_restart_rx_intr(mac);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001286 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001287 return pkts;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001288}
1289
1290static int __devinit
1291pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1292{
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001293 struct net_device *dev;
1294 struct pasemi_mac *mac;
1295 int err;
Joe Perches0795af52007-10-03 17:59:30 -07001296 DECLARE_MAC_BUF(mac_buf);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001297
1298 err = pci_enable_device(pdev);
1299 if (err)
1300 return err;
1301
1302 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1303 if (dev == NULL) {
1304 dev_err(&pdev->dev,
1305 "pasemi_mac: Could not allocate ethernet device.\n");
1306 err = -ENOMEM;
1307 goto out_disable_device;
1308 }
1309
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001310 pci_set_drvdata(pdev, dev);
1311 SET_NETDEV_DEV(dev, &pdev->dev);
1312
1313 mac = netdev_priv(dev);
1314
1315 mac->pdev = pdev;
1316 mac->netdev = dev;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001317
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001318 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1319
David Woodhouse0581d3f2007-12-03 04:34:32 +00001320 dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001321
Olof Johansson34c20622007-11-28 20:56:32 -06001322 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1323 if (!mac->dma_pdev) {
1324 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1325 err = -ENODEV;
1326 goto out;
1327 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001328
Olof Johansson34c20622007-11-28 20:56:32 -06001329 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1330 if (!mac->iob_pdev) {
1331 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1332 err = -ENODEV;
1333 goto out;
1334 }
1335
1336 /* get mac addr from device tree */
1337 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1338 err = -ENODEV;
1339 goto out;
1340 }
1341 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1342
1343 mac->dma_if = mac_to_intf(mac);
1344 if (mac->dma_if < 0) {
1345 dev_err(&mac->pdev->dev, "Can't map DMA interface\n");
1346 err = -ENODEV;
1347 goto out;
1348 }
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001349
1350 switch (pdev->device) {
1351 case 0xa005:
1352 mac->type = MAC_TYPE_GMAC;
1353 break;
1354 case 0xa006:
1355 mac->type = MAC_TYPE_XAUI;
1356 break;
1357 default:
1358 err = -ENODEV;
1359 goto out;
1360 }
1361
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001362 dev->open = pasemi_mac_open;
1363 dev->stop = pasemi_mac_close;
1364 dev->hard_start_xmit = pasemi_mac_start_tx;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001365 dev->set_multicast_list = pasemi_mac_set_rx_mode;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001366
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001367 if (err)
1368 goto out;
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001369
Olof Johanssonceb51362007-05-08 00:47:49 -05001370 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1371
Olof Johanssonbb6e9592007-05-08 00:47:54 -05001372 /* Enable most messages by default */
1373 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1374
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001375 err = register_netdev(dev);
1376
1377 if (err) {
1378 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1379 err);
1380 goto out;
Olof Johansson69c29d82007-10-02 16:24:51 -05001381 } else if netif_msg_probe(mac)
Olof Johansson72b05b92007-11-28 20:54:28 -06001382 printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %s\n",
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001383 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
Olof Johansson72b05b92007-11-28 20:54:28 -06001384 mac->dma_if, print_mac(mac_buf, dev->dev_addr));
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001385
1386 return err;
1387
1388out:
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001389 if (mac->iob_pdev)
1390 pci_dev_put(mac->iob_pdev);
1391 if (mac->dma_pdev)
1392 pci_dev_put(mac->dma_pdev);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001393
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001394 free_netdev(dev);
1395out_disable_device:
1396 pci_disable_device(pdev);
1397 return err;
1398
1399}
1400
1401static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1402{
1403 struct net_device *netdev = pci_get_drvdata(pdev);
1404 struct pasemi_mac *mac;
1405
1406 if (!netdev)
1407 return;
1408
1409 mac = netdev_priv(netdev);
1410
1411 unregister_netdev(netdev);
1412
1413 pci_disable_device(pdev);
1414 pci_dev_put(mac->dma_pdev);
1415 pci_dev_put(mac->iob_pdev);
1416
Olof Johansson34c20622007-11-28 20:56:32 -06001417 pasemi_dma_free_chan(&mac->tx->chan);
1418 pasemi_dma_free_chan(&mac->rx->chan);
Olof Johanssonb6e05a12007-09-15 13:44:07 -07001419
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001420 pci_set_drvdata(pdev, NULL);
1421 free_netdev(netdev);
1422}
1423
1424static struct pci_device_id pasemi_mac_pci_tbl[] = {
1425 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1426 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
olof@lixom.netfd178252007-05-12 14:57:36 -05001427 { },
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001428};
1429
1430MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1431
1432static struct pci_driver pasemi_mac_driver = {
1433 .name = "pasemi_mac",
1434 .id_table = pasemi_mac_pci_tbl,
1435 .probe = pasemi_mac_probe,
1436 .remove = __devexit_p(pasemi_mac_remove),
1437};
1438
1439static void __exit pasemi_mac_cleanup_module(void)
1440{
1441 pci_unregister_driver(&pasemi_mac_driver);
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001442}
1443
1444int pasemi_mac_init_module(void)
1445{
Olof Johansson34c20622007-11-28 20:56:32 -06001446 int err;
1447
1448 err = pasemi_dma_init();
1449 if (err)
1450 return err;
1451
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001452 return pci_register_driver(&pasemi_mac_driver);
1453}
1454
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001455module_init(pasemi_mac_init_module);
1456module_exit(pasemi_mac_cleanup_module);