Commit | Line | Data |
---|---|---|
15d014d1 LB |
1 | /* |
2 | * IXP2000 MSF network device driver | |
3 | * Copyright (C) 2004, 2005 Lennert Buytenhek <buytenh@wantstofly.org> | |
4 | * Dedicated to Marija Kulikova. | |
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 as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | */ | |
11 | ||
15d014d1 LB |
12 | #include <linux/module.h> |
13 | #include <linux/kernel.h> | |
14 | #include <linux/netdevice.h> | |
15 | #include <linux/etherdevice.h> | |
16 | #include <linux/init.h> | |
17 | #include <linux/moduleparam.h> | |
18ec5c73 | 18 | #include <asm/hardware/uengine.h> |
15d014d1 LB |
19 | #include <asm/io.h> |
20 | #include "ixp2400_rx.ucode" | |
21 | #include "ixp2400_tx.ucode" | |
22 | #include "ixpdev_priv.h" | |
23 | #include "ixpdev.h" | |
b9696ea3 | 24 | #include "pm3386.h" |
15d014d1 | 25 | |
12747784 LB |
26 | #define DRV_MODULE_VERSION "0.2" |
27 | ||
15d014d1 LB |
28 | static int nds_count; |
29 | static struct net_device **nds; | |
30 | static int nds_open; | |
31 | static void (*set_port_admin_status)(int port, int up); | |
32 | ||
33 | static struct ixpdev_rx_desc * const rx_desc = | |
34 | (struct ixpdev_rx_desc *)(IXP2000_SRAM0_VIRT_BASE + RX_BUF_DESC_BASE); | |
35 | static struct ixpdev_tx_desc * const tx_desc = | |
36 | (struct ixpdev_tx_desc *)(IXP2000_SRAM0_VIRT_BASE + TX_BUF_DESC_BASE); | |
37 | static int tx_pointer; | |
38 | ||
39 | ||
40 | static int ixpdev_xmit(struct sk_buff *skb, struct net_device *dev) | |
41 | { | |
42 | struct ixpdev_priv *ip = netdev_priv(dev); | |
43 | struct ixpdev_tx_desc *desc; | |
44 | int entry; | |
4871953c | 45 | unsigned long flags; |
15d014d1 LB |
46 | |
47 | if (unlikely(skb->len > PAGE_SIZE)) { | |
48 | /* @@@ Count drops. */ | |
49 | dev_kfree_skb(skb); | |
6ed10654 | 50 | return NETDEV_TX_OK; |
15d014d1 LB |
51 | } |
52 | ||
53 | entry = tx_pointer; | |
54 | tx_pointer = (tx_pointer + 1) % TX_BUF_COUNT; | |
55 | ||
56 | desc = tx_desc + entry; | |
57 | desc->pkt_length = skb->len; | |
58 | desc->channel = ip->channel; | |
59 | ||
60 | skb_copy_and_csum_dev(skb, phys_to_virt(desc->buf_addr)); | |
61 | dev_kfree_skb(skb); | |
62 | ||
63 | ixp2000_reg_write(RING_TX_PENDING, | |
64 | TX_BUF_DESC_BASE + (entry * sizeof(struct ixpdev_tx_desc))); | |
65 | ||
66 | dev->trans_start = jiffies; | |
67 | ||
4871953c | 68 | local_irq_save(flags); |
15d014d1 LB |
69 | ip->tx_queue_entries++; |
70 | if (ip->tx_queue_entries == TX_BUF_COUNT_PER_CHAN) | |
71 | netif_stop_queue(dev); | |
4871953c | 72 | local_irq_restore(flags); |
15d014d1 | 73 | |
6ed10654 | 74 | return NETDEV_TX_OK; |
15d014d1 LB |
75 | } |
76 | ||
77 | ||
bea3348e | 78 | static int ixpdev_rx(struct net_device *dev, int processed, int budget) |
15d014d1 | 79 | { |
bea3348e | 80 | while (processed < budget) { |
15d014d1 LB |
81 | struct ixpdev_rx_desc *desc; |
82 | struct sk_buff *skb; | |
83 | void *buf; | |
84 | u32 _desc; | |
85 | ||
86 | _desc = ixp2000_reg_read(RING_RX_DONE); | |
87 | if (_desc == 0) | |
88 | return 0; | |
89 | ||
90 | desc = rx_desc + | |
91 | ((_desc - RX_BUF_DESC_BASE) / sizeof(struct ixpdev_rx_desc)); | |
92 | buf = phys_to_virt(desc->buf_addr); | |
93 | ||
94 | if (desc->pkt_length < 4 || desc->pkt_length > PAGE_SIZE) { | |
95 | printk(KERN_ERR "ixp2000: rx err, length %d\n", | |
96 | desc->pkt_length); | |
97 | goto err; | |
98 | } | |
99 | ||
100 | if (desc->channel < 0 || desc->channel >= nds_count) { | |
101 | printk(KERN_ERR "ixp2000: rx err, channel %d\n", | |
102 | desc->channel); | |
103 | goto err; | |
104 | } | |
105 | ||
106 | /* @@@ Make FCS stripping configurable. */ | |
107 | desc->pkt_length -= 4; | |
108 | ||
109 | if (unlikely(!netif_running(nds[desc->channel]))) | |
110 | goto err; | |
111 | ||
89d71a66 | 112 | skb = netdev_alloc_skb_ip_align(dev, desc->pkt_length); |
15d014d1 | 113 | if (likely(skb != NULL)) { |
8c7b7faa | 114 | skb_copy_to_linear_data(skb, buf, desc->pkt_length); |
15d014d1 | 115 | skb_put(skb, desc->pkt_length); |
4c13eb66 | 116 | skb->protocol = eth_type_trans(skb, nds[desc->channel]); |
15d014d1 | 117 | |
15d014d1 LB |
118 | netif_receive_skb(skb); |
119 | } | |
120 | ||
121 | err: | |
122 | ixp2000_reg_write(RING_RX_PENDING, _desc); | |
bea3348e | 123 | processed++; |
15d014d1 LB |
124 | } |
125 | ||
bea3348e | 126 | return processed; |
15d014d1 LB |
127 | } |
128 | ||
129 | /* dev always points to nds[0]. */ | |
bea3348e | 130 | static int ixpdev_poll(struct napi_struct *napi, int budget) |
15d014d1 | 131 | { |
bea3348e SH |
132 | struct ixpdev_priv *ip = container_of(napi, struct ixpdev_priv, napi); |
133 | struct net_device *dev = ip->dev; | |
134 | int rx; | |
135 | ||
bea3348e | 136 | rx = 0; |
15d014d1 LB |
137 | do { |
138 | ixp2000_reg_write(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0x00ff); | |
139 | ||
bea3348e SH |
140 | rx = ixpdev_rx(dev, rx, budget); |
141 | if (rx >= budget) | |
142 | break; | |
15d014d1 LB |
143 | } while (ixp2000_reg_read(IXP2000_IRQ_THD_RAW_STATUS_A_0) & 0x00ff); |
144 | ||
288379f0 | 145 | napi_complete(napi); |
15d014d1 LB |
146 | ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0x00ff); |
147 | ||
bea3348e | 148 | return rx; |
15d014d1 LB |
149 | } |
150 | ||
15d014d1 LB |
151 | static void ixpdev_tx_complete(void) |
152 | { | |
153 | int channel; | |
154 | u32 wake; | |
155 | ||
156 | wake = 0; | |
157 | while (1) { | |
158 | struct ixpdev_priv *ip; | |
159 | u32 desc; | |
160 | int entry; | |
161 | ||
162 | desc = ixp2000_reg_read(RING_TX_DONE); | |
163 | if (desc == 0) | |
164 | break; | |
165 | ||
166 | /* @@@ Check whether entries come back in order. */ | |
167 | entry = (desc - TX_BUF_DESC_BASE) / sizeof(struct ixpdev_tx_desc); | |
168 | channel = tx_desc[entry].channel; | |
169 | ||
170 | if (channel < 0 || channel >= nds_count) { | |
171 | printk(KERN_ERR "ixp2000: txcomp channel index " | |
172 | "out of bounds (%d, %.8i, %d)\n", | |
173 | channel, (unsigned int)desc, entry); | |
174 | continue; | |
175 | } | |
176 | ||
177 | ip = netdev_priv(nds[channel]); | |
178 | if (ip->tx_queue_entries == TX_BUF_COUNT_PER_CHAN) | |
179 | wake |= 1 << channel; | |
180 | ip->tx_queue_entries--; | |
181 | } | |
182 | ||
183 | for (channel = 0; wake != 0; channel++) { | |
184 | if (wake & (1 << channel)) { | |
185 | netif_wake_queue(nds[channel]); | |
186 | wake &= ~(1 << channel); | |
187 | } | |
188 | } | |
189 | } | |
190 | ||
7d12e780 | 191 | static irqreturn_t ixpdev_interrupt(int irq, void *dev_id) |
15d014d1 LB |
192 | { |
193 | u32 status; | |
194 | ||
195 | status = ixp2000_reg_read(IXP2000_IRQ_THD_STATUS_A_0); | |
196 | if (status == 0) | |
197 | return IRQ_NONE; | |
198 | ||
199 | /* | |
200 | * Any of the eight receive units signaled RX? | |
201 | */ | |
202 | if (status & 0x00ff) { | |
bea3348e SH |
203 | struct net_device *dev = nds[0]; |
204 | struct ixpdev_priv *ip = netdev_priv(dev); | |
205 | ||
15d014d1 | 206 | ixp2000_reg_wrb(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0x00ff); |
bea3348e | 207 | if (likely(napi_schedule_prep(&ip->napi))) { |
288379f0 | 208 | __napi_schedule(&ip->napi); |
15d014d1 LB |
209 | } else { |
210 | printk(KERN_CRIT "ixp2000: irq while polling!!\n"); | |
211 | } | |
212 | } | |
213 | ||
214 | /* | |
215 | * Any of the eight transmit units signaled TXdone? | |
216 | */ | |
217 | if (status & 0xff00) { | |
218 | ixp2000_reg_wrb(IXP2000_IRQ_THD_RAW_STATUS_A_0, 0xff00); | |
219 | ixpdev_tx_complete(); | |
220 | } | |
221 | ||
222 | return IRQ_HANDLED; | |
223 | } | |
224 | ||
8ce51d69 LB |
225 | #ifdef CONFIG_NET_POLL_CONTROLLER |
226 | static void ixpdev_poll_controller(struct net_device *dev) | |
227 | { | |
228 | disable_irq(IRQ_IXP2000_THDA0); | |
0da2f0f1 | 229 | ixpdev_interrupt(IRQ_IXP2000_THDA0, dev); |
8ce51d69 LB |
230 | enable_irq(IRQ_IXP2000_THDA0); |
231 | } | |
232 | #endif | |
233 | ||
15d014d1 LB |
234 | static int ixpdev_open(struct net_device *dev) |
235 | { | |
236 | struct ixpdev_priv *ip = netdev_priv(dev); | |
237 | int err; | |
238 | ||
bea3348e | 239 | napi_enable(&ip->napi); |
15d014d1 LB |
240 | if (!nds_open++) { |
241 | err = request_irq(IRQ_IXP2000_THDA0, ixpdev_interrupt, | |
1fb9df5d | 242 | IRQF_SHARED, "ixp2000_eth", nds); |
15d014d1 LB |
243 | if (err) { |
244 | nds_open--; | |
bea3348e | 245 | napi_disable(&ip->napi); |
15d014d1 LB |
246 | return err; |
247 | } | |
248 | ||
249 | ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_SET_A_0, 0xffff); | |
250 | } | |
251 | ||
252 | set_port_admin_status(ip->channel, 1); | |
253 | netif_start_queue(dev); | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
258 | static int ixpdev_close(struct net_device *dev) | |
259 | { | |
260 | struct ixpdev_priv *ip = netdev_priv(dev); | |
261 | ||
262 | netif_stop_queue(dev); | |
bea3348e | 263 | napi_disable(&ip->napi); |
15d014d1 LB |
264 | set_port_admin_status(ip->channel, 0); |
265 | ||
266 | if (!--nds_open) { | |
267 | ixp2000_reg_write(IXP2000_IRQ_THD_ENABLE_CLEAR_A_0, 0xffff); | |
268 | free_irq(IRQ_IXP2000_THDA0, nds); | |
269 | } | |
270 | ||
271 | return 0; | |
272 | } | |
273 | ||
b9696ea3 VS |
274 | static struct net_device_stats *ixpdev_get_stats(struct net_device *dev) |
275 | { | |
276 | struct ixpdev_priv *ip = netdev_priv(dev); | |
277 | ||
278 | pm3386_get_stats(ip->channel, &(dev->stats)); | |
279 | ||
280 | return &(dev->stats); | |
281 | } | |
282 | ||
e7090e34 AB |
283 | static const struct net_device_ops ixpdev_netdev_ops = { |
284 | .ndo_open = ixpdev_open, | |
285 | .ndo_stop = ixpdev_close, | |
286 | .ndo_start_xmit = ixpdev_xmit, | |
287 | .ndo_change_mtu = eth_change_mtu, | |
288 | .ndo_validate_addr = eth_validate_addr, | |
289 | .ndo_set_mac_address = eth_mac_addr, | |
b9696ea3 | 290 | .ndo_get_stats = ixpdev_get_stats, |
e7090e34 AB |
291 | #ifdef CONFIG_NET_POLL_CONTROLLER |
292 | .ndo_poll_controller = ixpdev_poll_controller, | |
293 | #endif | |
294 | }; | |
295 | ||
15d014d1 LB |
296 | struct net_device *ixpdev_alloc(int channel, int sizeof_priv) |
297 | { | |
298 | struct net_device *dev; | |
299 | struct ixpdev_priv *ip; | |
300 | ||
301 | dev = alloc_etherdev(sizeof_priv); | |
302 | if (dev == NULL) | |
303 | return NULL; | |
304 | ||
e7090e34 | 305 | dev->netdev_ops = &ixpdev_netdev_ops; |
15d014d1 LB |
306 | |
307 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; | |
15d014d1 LB |
308 | |
309 | ip = netdev_priv(dev); | |
bea3348e SH |
310 | ip->dev = dev; |
311 | netif_napi_add(dev, &ip->napi, ixpdev_poll, 64); | |
15d014d1 LB |
312 | ip->channel = channel; |
313 | ip->tx_queue_entries = 0; | |
314 | ||
315 | return dev; | |
316 | } | |
317 | ||
318 | int ixpdev_init(int __nds_count, struct net_device **__nds, | |
319 | void (*__set_port_admin_status)(int port, int up)) | |
320 | { | |
321 | int i; | |
322 | int err; | |
323 | ||
5d4fe2c1 | 324 | BUILD_BUG_ON(RX_BUF_COUNT > 192 || TX_BUF_COUNT > 192); |
15d014d1 | 325 | |
12747784 LB |
326 | printk(KERN_INFO "IXP2000 MSF ethernet driver %s\n", DRV_MODULE_VERSION); |
327 | ||
15d014d1 LB |
328 | nds_count = __nds_count; |
329 | nds = __nds; | |
330 | set_port_admin_status = __set_port_admin_status; | |
331 | ||
15d014d1 LB |
332 | for (i = 0; i < RX_BUF_COUNT; i++) { |
333 | void *buf; | |
334 | ||
335 | buf = (void *)get_zeroed_page(GFP_KERNEL); | |
336 | if (buf == NULL) { | |
337 | err = -ENOMEM; | |
338 | while (--i >= 0) | |
339 | free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr)); | |
0c49919a | 340 | goto err_out; |
15d014d1 LB |
341 | } |
342 | rx_desc[i].buf_addr = virt_to_phys(buf); | |
343 | rx_desc[i].buf_length = PAGE_SIZE; | |
344 | } | |
345 | ||
346 | /* @@@ Maybe we shouldn't be preallocating TX buffers. */ | |
347 | for (i = 0; i < TX_BUF_COUNT; i++) { | |
348 | void *buf; | |
349 | ||
350 | buf = (void *)get_zeroed_page(GFP_KERNEL); | |
351 | if (buf == NULL) { | |
352 | err = -ENOMEM; | |
353 | while (--i >= 0) | |
354 | free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr)); | |
355 | goto err_free_rx; | |
356 | } | |
357 | tx_desc[i].buf_addr = virt_to_phys(buf); | |
358 | } | |
359 | ||
360 | /* 256 entries, ring status set means 'empty', base address 0x0000. */ | |
361 | ixp2000_reg_write(RING_RX_PENDING_BASE, 0x44000000); | |
362 | ixp2000_reg_write(RING_RX_PENDING_HEAD, 0x00000000); | |
363 | ixp2000_reg_write(RING_RX_PENDING_TAIL, 0x00000000); | |
364 | ||
365 | /* 256 entries, ring status set means 'full', base address 0x0400. */ | |
366 | ixp2000_reg_write(RING_RX_DONE_BASE, 0x40000400); | |
367 | ixp2000_reg_write(RING_RX_DONE_HEAD, 0x00000000); | |
368 | ixp2000_reg_write(RING_RX_DONE_TAIL, 0x00000000); | |
369 | ||
370 | for (i = 0; i < RX_BUF_COUNT; i++) { | |
371 | ixp2000_reg_write(RING_RX_PENDING, | |
372 | RX_BUF_DESC_BASE + (i * sizeof(struct ixpdev_rx_desc))); | |
373 | } | |
374 | ||
375 | ixp2000_uengine_load(0, &ixp2400_rx); | |
376 | ixp2000_uengine_start_contexts(0, 0xff); | |
377 | ||
15d014d1 LB |
378 | /* 256 entries, ring status set means 'empty', base address 0x0800. */ |
379 | ixp2000_reg_write(RING_TX_PENDING_BASE, 0x44000800); | |
380 | ixp2000_reg_write(RING_TX_PENDING_HEAD, 0x00000000); | |
381 | ixp2000_reg_write(RING_TX_PENDING_TAIL, 0x00000000); | |
382 | ||
383 | /* 256 entries, ring status set means 'full', base address 0x0c00. */ | |
384 | ixp2000_reg_write(RING_TX_DONE_BASE, 0x40000c00); | |
385 | ixp2000_reg_write(RING_TX_DONE_HEAD, 0x00000000); | |
386 | ixp2000_reg_write(RING_TX_DONE_TAIL, 0x00000000); | |
387 | ||
388 | ixp2000_uengine_load(1, &ixp2400_tx); | |
389 | ixp2000_uengine_start_contexts(1, 0xff); | |
390 | ||
0c49919a LB |
391 | for (i = 0; i < nds_count; i++) { |
392 | err = register_netdev(nds[i]); | |
393 | if (err) { | |
394 | while (--i >= 0) | |
395 | unregister_netdev(nds[i]); | |
396 | goto err_free_tx; | |
397 | } | |
398 | } | |
399 | ||
7ed98bfd LB |
400 | for (i = 0; i < nds_count; i++) { |
401 | printk(KERN_INFO "%s: IXP2000 MSF ethernet (port %d), " | |
402 | "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x.\n", nds[i]->name, i, | |
403 | nds[i]->dev_addr[0], nds[i]->dev_addr[1], | |
404 | nds[i]->dev_addr[2], nds[i]->dev_addr[3], | |
405 | nds[i]->dev_addr[4], nds[i]->dev_addr[5]); | |
406 | } | |
407 | ||
15d014d1 LB |
408 | return 0; |
409 | ||
0c49919a LB |
410 | err_free_tx: |
411 | for (i = 0; i < TX_BUF_COUNT; i++) | |
412 | free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr)); | |
413 | ||
15d014d1 LB |
414 | err_free_rx: |
415 | for (i = 0; i < RX_BUF_COUNT; i++) | |
416 | free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr)); | |
417 | ||
15d014d1 LB |
418 | err_out: |
419 | return err; | |
420 | } | |
421 | ||
422 | void ixpdev_deinit(void) | |
423 | { | |
424 | int i; | |
425 | ||
426 | /* @@@ Flush out pending packets. */ | |
427 | ||
0c49919a LB |
428 | for (i = 0; i < nds_count; i++) |
429 | unregister_netdev(nds[i]); | |
430 | ||
15d014d1 LB |
431 | ixp2000_uengine_stop_contexts(1, 0xff); |
432 | ixp2000_uengine_stop_contexts(0, 0xff); | |
433 | ixp2000_uengine_reset(0x3); | |
434 | ||
435 | for (i = 0; i < TX_BUF_COUNT; i++) | |
436 | free_page((unsigned long)phys_to_virt(tx_desc[i].buf_addr)); | |
437 | ||
438 | for (i = 0; i < RX_BUF_COUNT; i++) | |
439 | free_page((unsigned long)phys_to_virt(rx_desc[i].buf_addr)); | |
15d014d1 | 440 | } |