]>
Commit | Line | Data |
---|---|---|
f89efd52 MP |
1 | /* |
2 | * rionet - Ethernet driver over RapidIO messaging services | |
3 | * | |
4 | * Copyright 2005 MontaVista Software, Inc. | |
5 | * Matt Porter <mporter@kernel.crashing.org> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2 of the License, or (at your | |
10 | * option) any later version. | |
11 | */ | |
12 | ||
13 | #include <linux/module.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/dma-mapping.h> | |
16 | #include <linux/delay.h> | |
17 | #include <linux/rio.h> | |
18 | #include <linux/rio_drv.h> | |
5a0e3ad6 | 19 | #include <linux/slab.h> |
f89efd52 MP |
20 | #include <linux/rio_ids.h> |
21 | ||
22 | #include <linux/netdevice.h> | |
23 | #include <linux/etherdevice.h> | |
24 | #include <linux/skbuff.h> | |
25 | #include <linux/crc32.h> | |
26 | #include <linux/ethtool.h> | |
27 | ||
28 | #define DRV_NAME "rionet" | |
2fb717ec | 29 | #define DRV_VERSION "0.3" |
f89efd52 MP |
30 | #define DRV_AUTHOR "Matt Porter <mporter@kernel.crashing.org>" |
31 | #define DRV_DESC "Ethernet over RapidIO" | |
32 | ||
33 | MODULE_AUTHOR(DRV_AUTHOR); | |
34 | MODULE_DESCRIPTION(DRV_DESC); | |
35 | MODULE_LICENSE("GPL"); | |
36 | ||
37 | #define RIONET_DEFAULT_MSGLEVEL \ | |
38 | (NETIF_MSG_DRV | \ | |
39 | NETIF_MSG_LINK | \ | |
40 | NETIF_MSG_RX_ERR | \ | |
41 | NETIF_MSG_TX_ERR) | |
42 | ||
43 | #define RIONET_DOORBELL_JOIN 0x1000 | |
44 | #define RIONET_DOORBELL_LEAVE 0x1001 | |
45 | ||
46 | #define RIONET_MAILBOX 0 | |
47 | ||
48 | #define RIONET_TX_RING_SIZE CONFIG_RIONET_TX_SIZE | |
49 | #define RIONET_RX_RING_SIZE CONFIG_RIONET_RX_SIZE | |
2fb717ec | 50 | #define RIONET_MAX_NETS 8 |
92444bb3 AJ |
51 | #define RIONET_MSG_SIZE RIO_MAX_MSG_SIZE |
52 | #define RIONET_MAX_MTU (RIONET_MSG_SIZE - ETH_HLEN) | |
f89efd52 MP |
53 | |
54 | struct rionet_private { | |
55 | struct rio_mport *mport; | |
56 | struct sk_buff *rx_skb[RIONET_RX_RING_SIZE]; | |
57 | struct sk_buff *tx_skb[RIONET_TX_RING_SIZE]; | |
f89efd52 MP |
58 | int rx_slot; |
59 | int tx_slot; | |
60 | int tx_cnt; | |
61 | int ack_slot; | |
62 | spinlock_t lock; | |
63 | spinlock_t tx_lock; | |
64 | u32 msg_enable; | |
65 | }; | |
66 | ||
67 | struct rionet_peer { | |
68 | struct list_head node; | |
69 | struct rio_dev *rdev; | |
70 | struct resource *res; | |
71 | }; | |
72 | ||
2fb717ec AB |
73 | struct rionet_net { |
74 | struct net_device *ndev; | |
75 | struct list_head peers; | |
76 | struct rio_dev **active; | |
77 | int nact; /* number of active peers */ | |
78 | }; | |
f89efd52 | 79 | |
2fb717ec | 80 | static struct rionet_net nets[RIONET_MAX_NETS]; |
f89efd52 | 81 | |
284fb68d AB |
82 | #define is_rionet_capable(src_ops, dst_ops) \ |
83 | ((src_ops & RIO_SRC_OPS_DATA_MSG) && \ | |
84 | (dst_ops & RIO_DST_OPS_DATA_MSG) && \ | |
f89efd52 MP |
85 | (src_ops & RIO_SRC_OPS_DOORBELL) && \ |
86 | (dst_ops & RIO_DST_OPS_DOORBELL)) | |
87 | #define dev_rionet_capable(dev) \ | |
284fb68d | 88 | is_rionet_capable(dev->src_ops, dev->dst_ops) |
f89efd52 | 89 | |
e0c87bd9 AB |
90 | #define RIONET_MAC_MATCH(x) (!memcmp((x), "\00\01\00\01", 4)) |
91 | #define RIONET_GET_DESTID(x) ((*((u8 *)x + 4) << 8) | *((u8 *)x + 5)) | |
f89efd52 | 92 | |
f89efd52 MP |
93 | static int rionet_rx_clean(struct net_device *ndev) |
94 | { | |
95 | int i; | |
96 | int error = 0; | |
4cf1653a | 97 | struct rionet_private *rnet = netdev_priv(ndev); |
f89efd52 MP |
98 | void *data; |
99 | ||
100 | i = rnet->rx_slot; | |
101 | ||
102 | do { | |
103 | if (!rnet->rx_skb[i]) | |
104 | continue; | |
105 | ||
106 | if (!(data = rio_get_inb_message(rnet->mport, RIONET_MAILBOX))) | |
107 | break; | |
108 | ||
109 | rnet->rx_skb[i]->data = data; | |
110 | skb_put(rnet->rx_skb[i], RIO_MAX_MSG_SIZE); | |
f89efd52 MP |
111 | rnet->rx_skb[i]->protocol = |
112 | eth_type_trans(rnet->rx_skb[i], ndev); | |
113 | error = netif_rx(rnet->rx_skb[i]); | |
114 | ||
115 | if (error == NET_RX_DROP) { | |
09f75cd7 | 116 | ndev->stats.rx_dropped++; |
f89efd52 | 117 | } else { |
09f75cd7 JG |
118 | ndev->stats.rx_packets++; |
119 | ndev->stats.rx_bytes += RIO_MAX_MSG_SIZE; | |
f89efd52 MP |
120 | } |
121 | ||
122 | } while ((i = (i + 1) % RIONET_RX_RING_SIZE) != rnet->rx_slot); | |
123 | ||
124 | return i; | |
125 | } | |
126 | ||
127 | static void rionet_rx_fill(struct net_device *ndev, int end) | |
128 | { | |
129 | int i; | |
4cf1653a | 130 | struct rionet_private *rnet = netdev_priv(ndev); |
f89efd52 MP |
131 | |
132 | i = rnet->rx_slot; | |
133 | do { | |
134 | rnet->rx_skb[i] = dev_alloc_skb(RIO_MAX_MSG_SIZE); | |
135 | ||
136 | if (!rnet->rx_skb[i]) | |
137 | break; | |
138 | ||
139 | rio_add_inb_buffer(rnet->mport, RIONET_MAILBOX, | |
140 | rnet->rx_skb[i]->data); | |
141 | } while ((i = (i + 1) % RIONET_RX_RING_SIZE) != end); | |
142 | ||
143 | rnet->rx_slot = i; | |
144 | } | |
145 | ||
146 | static int rionet_queue_tx_msg(struct sk_buff *skb, struct net_device *ndev, | |
147 | struct rio_dev *rdev) | |
148 | { | |
4cf1653a | 149 | struct rionet_private *rnet = netdev_priv(ndev); |
f89efd52 MP |
150 | |
151 | rio_add_outb_message(rnet->mport, rdev, 0, skb->data, skb->len); | |
152 | rnet->tx_skb[rnet->tx_slot] = skb; | |
153 | ||
09f75cd7 JG |
154 | ndev->stats.tx_packets++; |
155 | ndev->stats.tx_bytes += skb->len; | |
f89efd52 MP |
156 | |
157 | if (++rnet->tx_cnt == RIONET_TX_RING_SIZE) | |
158 | netif_stop_queue(ndev); | |
159 | ||
160 | ++rnet->tx_slot; | |
161 | rnet->tx_slot &= (RIONET_TX_RING_SIZE - 1); | |
162 | ||
163 | if (netif_msg_tx_queued(rnet)) | |
8df8a475 DM |
164 | printk(KERN_INFO "%s: queued skb len %8.8x\n", DRV_NAME, |
165 | skb->len); | |
f89efd52 MP |
166 | |
167 | return 0; | |
168 | } | |
169 | ||
170 | static int rionet_start_xmit(struct sk_buff *skb, struct net_device *ndev) | |
171 | { | |
172 | int i; | |
4cf1653a | 173 | struct rionet_private *rnet = netdev_priv(ndev); |
f89efd52 MP |
174 | struct ethhdr *eth = (struct ethhdr *)skb->data; |
175 | u16 destid; | |
176 | unsigned long flags; | |
7c4a6106 | 177 | int add_num = 1; |
f89efd52 MP |
178 | |
179 | local_irq_save(flags); | |
180 | if (!spin_trylock(&rnet->tx_lock)) { | |
181 | local_irq_restore(flags); | |
182 | return NETDEV_TX_LOCKED; | |
183 | } | |
184 | ||
7c4a6106 | 185 | if (is_multicast_ether_addr(eth->h_dest)) |
2fb717ec | 186 | add_num = nets[rnet->mport->id].nact; |
7c4a6106 AB |
187 | |
188 | if ((rnet->tx_cnt + add_num) > RIONET_TX_RING_SIZE) { | |
f89efd52 MP |
189 | netif_stop_queue(ndev); |
190 | spin_unlock_irqrestore(&rnet->tx_lock, flags); | |
191 | printk(KERN_ERR "%s: BUG! Tx Ring full when queue awake!\n", | |
192 | ndev->name); | |
193 | return NETDEV_TX_BUSY; | |
194 | } | |
195 | ||
abfc89c7 | 196 | if (is_multicast_ether_addr(eth->h_dest)) { |
7c4a6106 | 197 | int count = 0; |
2fb717ec | 198 | |
e0423236 ZW |
199 | for (i = 0; i < RIO_MAX_ROUTE_ENTRIES(rnet->mport->sys_size); |
200 | i++) | |
2fb717ec | 201 | if (nets[rnet->mport->id].active[i]) { |
f89efd52 | 202 | rionet_queue_tx_msg(skb, ndev, |
2fb717ec | 203 | nets[rnet->mport->id].active[i]); |
7c4a6106 AB |
204 | if (count) |
205 | atomic_inc(&skb->users); | |
206 | count++; | |
207 | } | |
f89efd52 MP |
208 | } else if (RIONET_MAC_MATCH(eth->h_dest)) { |
209 | destid = RIONET_GET_DESTID(eth->h_dest); | |
2fb717ec AB |
210 | if (nets[rnet->mport->id].active[destid]) |
211 | rionet_queue_tx_msg(skb, ndev, | |
212 | nets[rnet->mport->id].active[destid]); | |
e6161d64 AB |
213 | else { |
214 | /* | |
215 | * If the target device was removed from the list of | |
216 | * active peers but we still have TX packets targeting | |
217 | * it just report sending a packet to the target | |
218 | * (without actual packet transfer). | |
219 | */ | |
220 | dev_kfree_skb_any(skb); | |
221 | ndev->stats.tx_packets++; | |
222 | ndev->stats.tx_bytes += skb->len; | |
223 | } | |
f89efd52 MP |
224 | } |
225 | ||
226 | spin_unlock_irqrestore(&rnet->tx_lock, flags); | |
227 | ||
6ed10654 | 228 | return NETDEV_TX_OK; |
f89efd52 MP |
229 | } |
230 | ||
231 | static void rionet_dbell_event(struct rio_mport *mport, void *dev_id, u16 sid, u16 tid, | |
232 | u16 info) | |
233 | { | |
234 | struct net_device *ndev = dev_id; | |
4cf1653a | 235 | struct rionet_private *rnet = netdev_priv(ndev); |
f89efd52 MP |
236 | struct rionet_peer *peer; |
237 | ||
238 | if (netif_msg_intr(rnet)) | |
239 | printk(KERN_INFO "%s: doorbell sid %4.4x tid %4.4x info %4.4x", | |
240 | DRV_NAME, sid, tid, info); | |
241 | if (info == RIONET_DOORBELL_JOIN) { | |
2fb717ec AB |
242 | if (!nets[rnet->mport->id].active[sid]) { |
243 | list_for_each_entry(peer, | |
244 | &nets[rnet->mport->id].peers, node) { | |
7c4a6106 | 245 | if (peer->rdev->destid == sid) { |
2fb717ec AB |
246 | nets[rnet->mport->id].active[sid] = |
247 | peer->rdev; | |
248 | nets[rnet->mport->id].nact++; | |
7c4a6106 | 249 | } |
f89efd52 MP |
250 | } |
251 | rio_mport_send_doorbell(mport, sid, | |
252 | RIONET_DOORBELL_JOIN); | |
253 | } | |
254 | } else if (info == RIONET_DOORBELL_LEAVE) { | |
2fb717ec AB |
255 | nets[rnet->mport->id].active[sid] = NULL; |
256 | nets[rnet->mport->id].nact--; | |
f89efd52 MP |
257 | } else { |
258 | if (netif_msg_intr(rnet)) | |
259 | printk(KERN_WARNING "%s: unhandled doorbell\n", | |
260 | DRV_NAME); | |
261 | } | |
262 | } | |
263 | ||
264 | static void rionet_inb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot) | |
265 | { | |
266 | int n; | |
267 | struct net_device *ndev = dev_id; | |
4cf1653a | 268 | struct rionet_private *rnet = netdev_priv(ndev); |
f89efd52 MP |
269 | |
270 | if (netif_msg_intr(rnet)) | |
271 | printk(KERN_INFO "%s: inbound message event, mbox %d slot %d\n", | |
272 | DRV_NAME, mbox, slot); | |
273 | ||
274 | spin_lock(&rnet->lock); | |
275 | if ((n = rionet_rx_clean(ndev)) != rnet->rx_slot) | |
276 | rionet_rx_fill(ndev, n); | |
277 | spin_unlock(&rnet->lock); | |
278 | } | |
279 | ||
280 | static void rionet_outb_msg_event(struct rio_mport *mport, void *dev_id, int mbox, int slot) | |
281 | { | |
282 | struct net_device *ndev = dev_id; | |
4cf1653a | 283 | struct rionet_private *rnet = netdev_priv(ndev); |
f89efd52 | 284 | |
36915976 | 285 | spin_lock(&rnet->tx_lock); |
f89efd52 MP |
286 | |
287 | if (netif_msg_intr(rnet)) | |
288 | printk(KERN_INFO | |
289 | "%s: outbound message event, mbox %d slot %d\n", | |
290 | DRV_NAME, mbox, slot); | |
291 | ||
292 | while (rnet->tx_cnt && (rnet->ack_slot != slot)) { | |
293 | /* dma unmap single */ | |
294 | dev_kfree_skb_irq(rnet->tx_skb[rnet->ack_slot]); | |
295 | rnet->tx_skb[rnet->ack_slot] = NULL; | |
296 | ++rnet->ack_slot; | |
297 | rnet->ack_slot &= (RIONET_TX_RING_SIZE - 1); | |
298 | rnet->tx_cnt--; | |
299 | } | |
300 | ||
301 | if (rnet->tx_cnt < RIONET_TX_RING_SIZE) | |
302 | netif_wake_queue(ndev); | |
303 | ||
36915976 | 304 | spin_unlock(&rnet->tx_lock); |
f89efd52 MP |
305 | } |
306 | ||
307 | static int rionet_open(struct net_device *ndev) | |
308 | { | |
309 | int i, rc = 0; | |
310 | struct rionet_peer *peer, *tmp; | |
4cf1653a | 311 | struct rionet_private *rnet = netdev_priv(ndev); |
f89efd52 MP |
312 | |
313 | if (netif_msg_ifup(rnet)) | |
314 | printk(KERN_INFO "%s: open\n", DRV_NAME); | |
315 | ||
316 | if ((rc = rio_request_inb_dbell(rnet->mport, | |
317 | (void *)ndev, | |
318 | RIONET_DOORBELL_JOIN, | |
319 | RIONET_DOORBELL_LEAVE, | |
320 | rionet_dbell_event)) < 0) | |
321 | goto out; | |
322 | ||
323 | if ((rc = rio_request_inb_mbox(rnet->mport, | |
324 | (void *)ndev, | |
325 | RIONET_MAILBOX, | |
326 | RIONET_RX_RING_SIZE, | |
327 | rionet_inb_msg_event)) < 0) | |
328 | goto out; | |
329 | ||
330 | if ((rc = rio_request_outb_mbox(rnet->mport, | |
331 | (void *)ndev, | |
332 | RIONET_MAILBOX, | |
333 | RIONET_TX_RING_SIZE, | |
334 | rionet_outb_msg_event)) < 0) | |
335 | goto out; | |
336 | ||
337 | /* Initialize inbound message ring */ | |
338 | for (i = 0; i < RIONET_RX_RING_SIZE; i++) | |
339 | rnet->rx_skb[i] = NULL; | |
340 | rnet->rx_slot = 0; | |
341 | rionet_rx_fill(ndev, 0); | |
342 | ||
343 | rnet->tx_slot = 0; | |
344 | rnet->tx_cnt = 0; | |
345 | rnet->ack_slot = 0; | |
346 | ||
347 | netif_carrier_on(ndev); | |
348 | netif_start_queue(ndev); | |
349 | ||
2fb717ec AB |
350 | list_for_each_entry_safe(peer, tmp, |
351 | &nets[rnet->mport->id].peers, node) { | |
f89efd52 MP |
352 | if (!(peer->res = rio_request_outb_dbell(peer->rdev, |
353 | RIONET_DOORBELL_JOIN, | |
354 | RIONET_DOORBELL_LEAVE))) | |
355 | { | |
356 | printk(KERN_ERR "%s: error requesting doorbells\n", | |
357 | DRV_NAME); | |
358 | continue; | |
359 | } | |
360 | ||
284fb68d AB |
361 | /* Send a join message */ |
362 | rio_send_doorbell(peer->rdev, RIONET_DOORBELL_JOIN); | |
f89efd52 MP |
363 | } |
364 | ||
365 | out: | |
366 | return rc; | |
367 | } | |
368 | ||
369 | static int rionet_close(struct net_device *ndev) | |
370 | { | |
4cf1653a | 371 | struct rionet_private *rnet = netdev_priv(ndev); |
f89efd52 MP |
372 | struct rionet_peer *peer, *tmp; |
373 | int i; | |
374 | ||
375 | if (netif_msg_ifup(rnet)) | |
2fb717ec | 376 | printk(KERN_INFO "%s: close %s\n", DRV_NAME, ndev->name); |
f89efd52 MP |
377 | |
378 | netif_stop_queue(ndev); | |
379 | netif_carrier_off(ndev); | |
380 | ||
381 | for (i = 0; i < RIONET_RX_RING_SIZE; i++) | |
aaff1e19 | 382 | kfree_skb(rnet->rx_skb[i]); |
f89efd52 | 383 | |
2fb717ec AB |
384 | list_for_each_entry_safe(peer, tmp, |
385 | &nets[rnet->mport->id].peers, node) { | |
386 | if (nets[rnet->mport->id].active[peer->rdev->destid]) { | |
f89efd52 | 387 | rio_send_doorbell(peer->rdev, RIONET_DOORBELL_LEAVE); |
2fb717ec | 388 | nets[rnet->mport->id].active[peer->rdev->destid] = NULL; |
f89efd52 MP |
389 | } |
390 | rio_release_outb_dbell(peer->rdev, peer->res); | |
391 | } | |
392 | ||
393 | rio_release_inb_dbell(rnet->mport, RIONET_DOORBELL_JOIN, | |
394 | RIONET_DOORBELL_LEAVE); | |
395 | rio_release_inb_mbox(rnet->mport, RIONET_MAILBOX); | |
396 | rio_release_outb_mbox(rnet->mport, RIONET_MAILBOX); | |
397 | ||
398 | return 0; | |
399 | } | |
400 | ||
71db87ba | 401 | static void rionet_remove_dev(struct device *dev, struct subsys_interface *sif) |
f89efd52 | 402 | { |
e6161d64 | 403 | struct rio_dev *rdev = to_rio_dev(dev); |
2fb717ec | 404 | unsigned char netid = rdev->net->hport->id; |
f89efd52 MP |
405 | struct rionet_peer *peer, *tmp; |
406 | ||
e6161d64 AB |
407 | if (dev_rionet_capable(rdev)) { |
408 | list_for_each_entry_safe(peer, tmp, &nets[netid].peers, node) { | |
409 | if (peer->rdev == rdev) { | |
410 | if (nets[netid].active[rdev->destid]) { | |
411 | nets[netid].active[rdev->destid] = NULL; | |
412 | nets[netid].nact--; | |
413 | } | |
2fb717ec | 414 | |
e6161d64 AB |
415 | list_del(&peer->node); |
416 | kfree(peer); | |
417 | break; | |
418 | } | |
419 | } | |
f89efd52 MP |
420 | } |
421 | } | |
422 | ||
423 | static void rionet_get_drvinfo(struct net_device *ndev, | |
424 | struct ethtool_drvinfo *info) | |
425 | { | |
4cf1653a | 426 | struct rionet_private *rnet = netdev_priv(ndev); |
f89efd52 | 427 | |
7826d43f JP |
428 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
429 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); | |
430 | strlcpy(info->fw_version, "n/a", sizeof(info->fw_version)); | |
431 | strlcpy(info->bus_info, rnet->mport->name, sizeof(info->bus_info)); | |
f89efd52 MP |
432 | } |
433 | ||
434 | static u32 rionet_get_msglevel(struct net_device *ndev) | |
435 | { | |
4cf1653a | 436 | struct rionet_private *rnet = netdev_priv(ndev); |
f89efd52 MP |
437 | |
438 | return rnet->msg_enable; | |
439 | } | |
440 | ||
441 | static void rionet_set_msglevel(struct net_device *ndev, u32 value) | |
442 | { | |
4cf1653a | 443 | struct rionet_private *rnet = netdev_priv(ndev); |
f89efd52 MP |
444 | |
445 | rnet->msg_enable = value; | |
446 | } | |
447 | ||
92444bb3 AJ |
448 | static int rionet_change_mtu(struct net_device *ndev, int new_mtu) |
449 | { | |
450 | if ((new_mtu < 68) || (new_mtu > RIONET_MAX_MTU)) { | |
451 | printk(KERN_ERR "%s: Invalid MTU size %d\n", | |
452 | ndev->name, new_mtu); | |
453 | return -EINVAL; | |
454 | } | |
455 | ndev->mtu = new_mtu; | |
456 | return 0; | |
457 | } | |
458 | ||
7282d491 | 459 | static const struct ethtool_ops rionet_ethtool_ops = { |
f89efd52 MP |
460 | .get_drvinfo = rionet_get_drvinfo, |
461 | .get_msglevel = rionet_get_msglevel, | |
462 | .set_msglevel = rionet_set_msglevel, | |
463 | .get_link = ethtool_op_get_link, | |
464 | }; | |
465 | ||
a33a2bb3 AB |
466 | static const struct net_device_ops rionet_netdev_ops = { |
467 | .ndo_open = rionet_open, | |
468 | .ndo_stop = rionet_close, | |
469 | .ndo_start_xmit = rionet_start_xmit, | |
92444bb3 | 470 | .ndo_change_mtu = rionet_change_mtu, |
a33a2bb3 AB |
471 | .ndo_validate_addr = eth_validate_addr, |
472 | .ndo_set_mac_address = eth_mac_addr, | |
473 | }; | |
474 | ||
55caa924 | 475 | static int rionet_setup_netdev(struct rio_mport *mport, struct net_device *ndev) |
f89efd52 MP |
476 | { |
477 | int rc = 0; | |
f89efd52 MP |
478 | struct rionet_private *rnet; |
479 | u16 device_id; | |
acc65632 AM |
480 | const size_t rionet_active_bytes = sizeof(void *) * |
481 | RIO_MAX_ROUTE_ENTRIES(mport->sys_size); | |
f89efd52 | 482 | |
2fb717ec AB |
483 | nets[mport->id].active = (struct rio_dev **)__get_free_pages(GFP_KERNEL, |
484 | get_order(rionet_active_bytes)); | |
485 | if (!nets[mport->id].active) { | |
e0423236 ZW |
486 | rc = -ENOMEM; |
487 | goto out; | |
488 | } | |
2fb717ec | 489 | memset((void *)nets[mport->id].active, 0, rionet_active_bytes); |
e0423236 | 490 | |
f89efd52 | 491 | /* Set up private area */ |
4cf1653a | 492 | rnet = netdev_priv(ndev); |
f89efd52 MP |
493 | rnet->mport = mport; |
494 | ||
495 | /* Set the default MAC address */ | |
496 | device_id = rio_local_get_device_id(mport); | |
497 | ndev->dev_addr[0] = 0x00; | |
498 | ndev->dev_addr[1] = 0x01; | |
499 | ndev->dev_addr[2] = 0x00; | |
500 | ndev->dev_addr[3] = 0x01; | |
501 | ndev->dev_addr[4] = device_id >> 8; | |
502 | ndev->dev_addr[5] = device_id & 0xff; | |
503 | ||
a33a2bb3 | 504 | ndev->netdev_ops = &rionet_netdev_ops; |
92444bb3 | 505 | ndev->mtu = RIONET_MAX_MTU; |
f89efd52 | 506 | ndev->features = NETIF_F_LLTX; |
2aaf308b | 507 | SET_NETDEV_DEV(ndev, &mport->dev); |
7ad24ea4 | 508 | ndev->ethtool_ops = &rionet_ethtool_ops; |
f89efd52 | 509 | |
f89efd52 MP |
510 | spin_lock_init(&rnet->lock); |
511 | spin_lock_init(&rnet->tx_lock); | |
512 | ||
513 | rnet->msg_enable = RIONET_DEFAULT_MSGLEVEL; | |
514 | ||
515 | rc = register_netdev(ndev); | |
516 | if (rc != 0) | |
517 | goto out; | |
518 | ||
2fb717ec | 519 | printk(KERN_INFO "%s: %s %s Version %s, MAC %pM, %s\n", |
f89efd52 MP |
520 | ndev->name, |
521 | DRV_NAME, | |
522 | DRV_DESC, | |
523 | DRV_VERSION, | |
2fb717ec AB |
524 | ndev->dev_addr, |
525 | mport->name); | |
f89efd52 MP |
526 | |
527 | out: | |
528 | return rc; | |
529 | } | |
530 | ||
2fb717ec AB |
531 | static unsigned long net_table[RIONET_MAX_NETS/sizeof(unsigned long) + 1]; |
532 | ||
e6161d64 | 533 | static int rionet_add_dev(struct device *dev, struct subsys_interface *sif) |
f89efd52 MP |
534 | { |
535 | int rc = -ENODEV; | |
284fb68d | 536 | u32 lsrc_ops, ldst_ops; |
f89efd52 | 537 | struct rionet_peer *peer; |
55caa924 | 538 | struct net_device *ndev = NULL; |
e6161d64 | 539 | struct rio_dev *rdev = to_rio_dev(dev); |
2fb717ec AB |
540 | unsigned char netid = rdev->net->hport->id; |
541 | int oldnet; | |
f89efd52 | 542 | |
2fb717ec AB |
543 | if (netid >= RIONET_MAX_NETS) |
544 | return rc; | |
f89efd52 | 545 | |
2fb717ec | 546 | oldnet = test_and_set_bit(netid, net_table); |
55caa924 | 547 | |
f89efd52 | 548 | /* |
e6161d64 AB |
549 | * If first time through this net, make sure local device is rionet |
550 | * capable and setup netdev (this step will be skipped in later probes | |
551 | * on the same net). | |
f89efd52 | 552 | */ |
2fb717ec | 553 | if (!oldnet) { |
f89efd52 MP |
554 | rio_local_read_config_32(rdev->net->hport, RIO_SRC_OPS_CAR, |
555 | &lsrc_ops); | |
556 | rio_local_read_config_32(rdev->net->hport, RIO_DST_OPS_CAR, | |
557 | &ldst_ops); | |
284fb68d | 558 | if (!is_rionet_capable(lsrc_ops, ldst_ops)) { |
f89efd52 | 559 | printk(KERN_ERR |
2fb717ec AB |
560 | "%s: local device %s is not network capable\n", |
561 | DRV_NAME, rdev->net->hport->name); | |
f89efd52 MP |
562 | goto out; |
563 | } | |
564 | ||
2fb717ec AB |
565 | /* Allocate our net_device structure */ |
566 | ndev = alloc_etherdev(sizeof(struct rionet_private)); | |
567 | if (ndev == NULL) { | |
568 | rc = -ENOMEM; | |
569 | goto out; | |
570 | } | |
571 | nets[netid].ndev = ndev; | |
55caa924 | 572 | rc = rionet_setup_netdev(rdev->net->hport, ndev); |
e6161d64 AB |
573 | if (rc) { |
574 | printk(KERN_ERR "%s: failed to setup netdev (rc=%d)\n", | |
575 | DRV_NAME, rc); | |
576 | goto out; | |
577 | } | |
578 | ||
2fb717ec AB |
579 | INIT_LIST_HEAD(&nets[netid].peers); |
580 | nets[netid].nact = 0; | |
581 | } else if (nets[netid].ndev == NULL) | |
582 | goto out; | |
f89efd52 MP |
583 | |
584 | /* | |
585 | * If the remote device has mailbox/doorbell capabilities, | |
586 | * add it to the peer list. | |
587 | */ | |
588 | if (dev_rionet_capable(rdev)) { | |
589 | if (!(peer = kmalloc(sizeof(struct rionet_peer), GFP_KERNEL))) { | |
590 | rc = -ENOMEM; | |
591 | goto out; | |
592 | } | |
593 | peer->rdev = rdev; | |
2fb717ec | 594 | list_add_tail(&peer->node, &nets[netid].peers); |
f89efd52 MP |
595 | } |
596 | ||
e6161d64 AB |
597 | return 0; |
598 | out: | |
f89efd52 MP |
599 | return rc; |
600 | } | |
601 | ||
e6161d64 | 602 | #ifdef MODULE |
f89efd52 | 603 | static struct rio_device_id rionet_id_table[] = { |
e6161d64 AB |
604 | {RIO_DEVICE(RIO_ANY_ID, RIO_ANY_ID)}, |
605 | { 0, } /* terminate list */ | |
f89efd52 MP |
606 | }; |
607 | ||
e6161d64 AB |
608 | MODULE_DEVICE_TABLE(rapidio, rionet_id_table); |
609 | #endif | |
610 | ||
611 | static struct subsys_interface rionet_interface = { | |
612 | .name = "rionet", | |
613 | .subsys = &rio_bus_type, | |
614 | .add_dev = rionet_add_dev, | |
615 | .remove_dev = rionet_remove_dev, | |
f89efd52 MP |
616 | }; |
617 | ||
618 | static int __init rionet_init(void) | |
619 | { | |
e6161d64 | 620 | return subsys_interface_register(&rionet_interface); |
f89efd52 MP |
621 | } |
622 | ||
623 | static void __exit rionet_exit(void) | |
624 | { | |
e6161d64 AB |
625 | struct rionet_private *rnet; |
626 | struct net_device *ndev; | |
627 | struct rionet_peer *peer, *tmp; | |
628 | int i; | |
629 | ||
630 | for (i = 0; i < RIONET_MAX_NETS; i++) { | |
631 | if (nets[i].ndev != NULL) { | |
632 | ndev = nets[i].ndev; | |
633 | rnet = netdev_priv(ndev); | |
634 | unregister_netdev(ndev); | |
635 | ||
636 | list_for_each_entry_safe(peer, | |
637 | tmp, &nets[i].peers, node) { | |
638 | list_del(&peer->node); | |
639 | kfree(peer); | |
640 | } | |
641 | ||
642 | free_pages((unsigned long)nets[i].active, | |
643 | get_order(sizeof(void *) * | |
644 | RIO_MAX_ROUTE_ENTRIES(rnet->mport->sys_size))); | |
645 | nets[i].active = NULL; | |
646 | ||
647 | free_netdev(ndev); | |
648 | } | |
649 | } | |
650 | ||
651 | subsys_interface_unregister(&rionet_interface); | |
f89efd52 MP |
652 | } |
653 | ||
2f809985 | 654 | late_initcall(rionet_init); |
f89efd52 | 655 | module_exit(rionet_exit); |