Commit | Line | Data |
---|---|---|
02c18891 MM |
1 | /* |
2 | * PS3 gelic network driver. | |
3 | * | |
4 | * Copyright (C) 2007 Sony Computer Entertainment Inc. | |
5 | * Copyright 2006, 2007 Sony Corporation | |
6 | * | |
7 | * This file is based on: spider_net.c | |
8 | * | |
9 | * (C) Copyright IBM Corp. 2005 | |
10 | * | |
11 | * Authors : Utz Bacher <utz.bacher@de.ibm.com> | |
12 | * Jens Osterkamp <Jens.Osterkamp@de.ibm.com> | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or modify | |
15 | * it under the terms of the GNU General Public License as published by | |
16 | * the Free Software Foundation; either version 2, or (at your option) | |
17 | * any later version. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with this program; if not, write to the Free Software | |
26 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
27 | */ | |
28 | ||
29 | #undef DEBUG | |
30 | ||
31 | #include <linux/kernel.h> | |
32 | #include <linux/module.h> | |
33 | ||
34 | #include <linux/etherdevice.h> | |
35 | #include <linux/ethtool.h> | |
36 | #include <linux/if_vlan.h> | |
37 | ||
38 | #include <linux/in.h> | |
39 | #include <linux/ip.h> | |
40 | #include <linux/tcp.h> | |
41 | ||
42 | #include <linux/dma-mapping.h> | |
43 | #include <net/checksum.h> | |
44 | #include <asm/firmware.h> | |
45 | #include <asm/ps3.h> | |
46 | #include <asm/lv1call.h> | |
47 | ||
48 | #include "ps3_gelic_net.h" | |
09dde54c | 49 | #include "ps3_gelic_wireless.h" |
02c18891 MM |
50 | |
51 | #define DRV_NAME "Gelic Network Driver" | |
09dde54c | 52 | #define DRV_VERSION "2.0" |
02c18891 MM |
53 | |
54 | MODULE_AUTHOR("SCE Inc."); | |
55 | MODULE_DESCRIPTION("Gelic Network driver"); | |
56 | MODULE_LICENSE("GPL"); | |
57 | ||
589866f9 MM |
58 | |
59 | static inline void gelic_card_enable_rxdmac(struct gelic_card *card); | |
60 | static inline void gelic_card_disable_rxdmac(struct gelic_card *card); | |
61 | static inline void gelic_card_disable_txdmac(struct gelic_card *card); | |
62 | static inline void gelic_card_reset_chain(struct gelic_card *card, | |
63 | struct gelic_descr_chain *chain, | |
64 | struct gelic_descr *start_descr); | |
02c18891 MM |
65 | |
66 | /* set irq_mask */ | |
589866f9 | 67 | int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask) |
02c18891 MM |
68 | { |
69 | int status; | |
70 | ||
71 | status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card), | |
72 | mask, 0); | |
73 | if (status) | |
74 | dev_info(ctodev(card), | |
589866f9 | 75 | "%s failed %d\n", __func__, status); |
02c18891 MM |
76 | return status; |
77 | } | |
589866f9 | 78 | |
59e97327 | 79 | static inline void gelic_card_rx_irq_on(struct gelic_card *card) |
02c18891 | 80 | { |
589866f9 MM |
81 | card->irq_mask |= GELIC_CARD_RXINT; |
82 | gelic_card_set_irq_mask(card, card->irq_mask); | |
02c18891 | 83 | } |
59e97327 | 84 | static inline void gelic_card_rx_irq_off(struct gelic_card *card) |
02c18891 | 85 | { |
589866f9 MM |
86 | card->irq_mask &= ~GELIC_CARD_RXINT; |
87 | gelic_card_set_irq_mask(card, card->irq_mask); | |
02c18891 | 88 | } |
01fed4c2 | 89 | |
589866f9 MM |
90 | static void gelic_card_get_ether_port_status(struct gelic_card *card, |
91 | int inform) | |
01fed4c2 MM |
92 | { |
93 | u64 v2; | |
94 | struct net_device *ether_netdev; | |
95 | ||
96 | lv1_net_control(bus_id(card), dev_id(card), | |
97 | GELIC_LV1_GET_ETH_PORT_STATUS, | |
d4d7f1f9 | 98 | GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0, |
01fed4c2 MM |
99 | &card->ether_port_status, &v2); |
100 | ||
101 | if (inform) { | |
d4d7f1f9 | 102 | ether_netdev = card->netdev[GELIC_PORT_ETHERNET_0]; |
01fed4c2 MM |
103 | if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP) |
104 | netif_carrier_on(ether_netdev); | |
105 | else | |
106 | netif_carrier_off(ether_netdev); | |
107 | } | |
108 | } | |
109 | ||
55873ed8 HS |
110 | static int gelic_card_set_link_mode(struct gelic_card *card, int mode) |
111 | { | |
112 | int status; | |
113 | u64 v1, v2; | |
114 | ||
115 | status = lv1_net_control(bus_id(card), dev_id(card), | |
116 | GELIC_LV1_SET_NEGOTIATION_MODE, | |
117 | GELIC_LV1_PHY_ETHERNET_0, mode, 0, &v1, &v2); | |
118 | if (status) { | |
119 | pr_info("%s: failed setting negotiation mode %d\n", __func__, | |
120 | status); | |
121 | return -EBUSY; | |
122 | } | |
123 | ||
124 | card->link_mode = mode; | |
125 | return 0; | |
126 | } | |
127 | ||
589866f9 MM |
128 | void gelic_card_up(struct gelic_card *card) |
129 | { | |
130 | pr_debug("%s: called\n", __func__); | |
2914f3ef | 131 | mutex_lock(&card->updown_lock); |
589866f9 MM |
132 | if (atomic_inc_return(&card->users) == 1) { |
133 | pr_debug("%s: real do\n", __func__); | |
134 | /* enable irq */ | |
135 | gelic_card_set_irq_mask(card, card->irq_mask); | |
136 | /* start rx */ | |
137 | gelic_card_enable_rxdmac(card); | |
138 | ||
139 | napi_enable(&card->napi); | |
140 | } | |
2914f3ef | 141 | mutex_unlock(&card->updown_lock); |
589866f9 MM |
142 | pr_debug("%s: done\n", __func__); |
143 | } | |
144 | ||
145 | void gelic_card_down(struct gelic_card *card) | |
146 | { | |
147 | u64 mask; | |
148 | pr_debug("%s: called\n", __func__); | |
2914f3ef | 149 | mutex_lock(&card->updown_lock); |
589866f9 MM |
150 | if (atomic_dec_if_positive(&card->users) == 0) { |
151 | pr_debug("%s: real do\n", __func__); | |
152 | napi_disable(&card->napi); | |
153 | /* | |
154 | * Disable irq. Wireless interrupts will | |
155 | * be disabled later if any | |
156 | */ | |
157 | mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED | | |
158 | GELIC_CARD_WLAN_COMMAND_COMPLETED); | |
159 | gelic_card_set_irq_mask(card, mask); | |
160 | /* stop rx */ | |
161 | gelic_card_disable_rxdmac(card); | |
162 | gelic_card_reset_chain(card, &card->rx_chain, | |
163 | card->descr + GELIC_NET_TX_DESCRIPTORS); | |
164 | /* stop tx */ | |
165 | gelic_card_disable_txdmac(card); | |
166 | } | |
2914f3ef | 167 | mutex_unlock(&card->updown_lock); |
589866f9 MM |
168 | pr_debug("%s: done\n", __func__); |
169 | } | |
01fed4c2 | 170 | |
02c18891 | 171 | /** |
59e97327 | 172 | * gelic_descr_get_status -- returns the status of a descriptor |
02c18891 MM |
173 | * @descr: descriptor to look at |
174 | * | |
175 | * returns the status as in the dmac_cmd_status field of the descriptor | |
176 | */ | |
59e97327 MM |
177 | static enum gelic_descr_dma_status |
178 | gelic_descr_get_status(struct gelic_descr *descr) | |
02c18891 | 179 | { |
59e97327 | 180 | return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK; |
02c18891 MM |
181 | } |
182 | ||
183 | /** | |
59e97327 | 184 | * gelic_descr_set_status -- sets the status of a descriptor |
02c18891 MM |
185 | * @descr: descriptor to change |
186 | * @status: status to set in the descriptor | |
187 | * | |
188 | * changes the status to the specified value. Doesn't change other bits | |
189 | * in the status | |
190 | */ | |
59e97327 MM |
191 | static void gelic_descr_set_status(struct gelic_descr *descr, |
192 | enum gelic_descr_dma_status status) | |
02c18891 | 193 | { |
59e97327 | 194 | descr->dmac_cmd_status = cpu_to_be32(status | |
589866f9 MM |
195 | (be32_to_cpu(descr->dmac_cmd_status) & |
196 | ~GELIC_DESCR_DMA_STAT_MASK)); | |
02c18891 MM |
197 | /* |
198 | * dma_cmd_status field is used to indicate whether the descriptor | |
199 | * is valid or not. | |
200 | * Usually caller of this function wants to inform that to the | |
201 | * hardware, so we assure here the hardware sees the change. | |
202 | */ | |
203 | wmb(); | |
204 | } | |
205 | ||
206 | /** | |
59e97327 | 207 | * gelic_card_free_chain - free descriptor chain |
02c18891 MM |
208 | * @card: card structure |
209 | * @descr_in: address of desc | |
210 | */ | |
59e97327 MM |
211 | static void gelic_card_free_chain(struct gelic_card *card, |
212 | struct gelic_descr *descr_in) | |
02c18891 | 213 | { |
59e97327 | 214 | struct gelic_descr *descr; |
02c18891 MM |
215 | |
216 | for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) { | |
217 | dma_unmap_single(ctodev(card), descr->bus_addr, | |
59e97327 | 218 | GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL); |
02c18891 MM |
219 | descr->bus_addr = 0; |
220 | } | |
221 | } | |
222 | ||
223 | /** | |
59e97327 | 224 | * gelic_card_init_chain - links descriptor chain |
02c18891 MM |
225 | * @card: card structure |
226 | * @chain: address of chain | |
227 | * @start_descr: address of descriptor array | |
228 | * @no: number of descriptors | |
229 | * | |
230 | * we manage a circular list that mirrors the hardware structure, | |
231 | * except that the hardware uses bus addresses. | |
232 | * | |
233 | * returns 0 on success, <0 on failure | |
234 | */ | |
48dce82c GU |
235 | static int __devinit gelic_card_init_chain(struct gelic_card *card, |
236 | struct gelic_descr_chain *chain, | |
237 | struct gelic_descr *start_descr, | |
238 | int no) | |
02c18891 MM |
239 | { |
240 | int i; | |
59e97327 | 241 | struct gelic_descr *descr; |
02c18891 MM |
242 | |
243 | descr = start_descr; | |
244 | memset(descr, 0, sizeof(*descr) * no); | |
245 | ||
246 | /* set up the hardware pointers in each descriptor */ | |
247 | for (i = 0; i < no; i++, descr++) { | |
59e97327 | 248 | gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); |
02c18891 MM |
249 | descr->bus_addr = |
250 | dma_map_single(ctodev(card), descr, | |
59e97327 | 251 | GELIC_DESCR_SIZE, |
02c18891 MM |
252 | DMA_BIDIRECTIONAL); |
253 | ||
254 | if (!descr->bus_addr) | |
255 | goto iommu_error; | |
256 | ||
257 | descr->next = descr + 1; | |
258 | descr->prev = descr - 1; | |
259 | } | |
260 | /* make them as ring */ | |
261 | (descr - 1)->next = start_descr; | |
262 | start_descr->prev = (descr - 1); | |
263 | ||
264 | /* chain bus addr of hw descriptor */ | |
265 | descr = start_descr; | |
266 | for (i = 0; i < no; i++, descr++) { | |
100e1d89 | 267 | descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr); |
02c18891 MM |
268 | } |
269 | ||
270 | chain->head = start_descr; | |
271 | chain->tail = start_descr; | |
272 | ||
273 | /* do not chain last hw descriptor */ | |
274 | (descr - 1)->next_descr_addr = 0; | |
275 | ||
276 | return 0; | |
277 | ||
278 | iommu_error: | |
279 | for (i--, descr--; 0 <= i; i--, descr--) | |
280 | if (descr->bus_addr) | |
281 | dma_unmap_single(ctodev(card), descr->bus_addr, | |
59e97327 | 282 | GELIC_DESCR_SIZE, |
02c18891 MM |
283 | DMA_BIDIRECTIONAL); |
284 | return -ENOMEM; | |
285 | } | |
286 | ||
589866f9 MM |
287 | /** |
288 | * gelic_card_reset_chain - reset status of a descriptor chain | |
289 | * @card: card structure | |
290 | * @chain: address of chain | |
291 | * @start_descr: address of descriptor array | |
292 | * | |
293 | * Reset the status of dma descriptors to ready state | |
294 | * and re-initialize the hardware chain for later use | |
295 | */ | |
296 | static void gelic_card_reset_chain(struct gelic_card *card, | |
297 | struct gelic_descr_chain *chain, | |
298 | struct gelic_descr *start_descr) | |
299 | { | |
300 | struct gelic_descr *descr; | |
301 | ||
302 | for (descr = start_descr; start_descr != descr->next; descr++) { | |
303 | gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED); | |
304 | descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr); | |
305 | } | |
306 | ||
307 | chain->head = start_descr; | |
308 | chain->tail = (descr - 1); | |
309 | ||
310 | (descr - 1)->next_descr_addr = 0; | |
311 | } | |
02c18891 | 312 | /** |
59e97327 | 313 | * gelic_descr_prepare_rx - reinitializes a rx descriptor |
02c18891 MM |
314 | * @card: card structure |
315 | * @descr: descriptor to re-init | |
316 | * | |
af901ca1 | 317 | * return 0 on success, <0 on failure |
02c18891 MM |
318 | * |
319 | * allocates a new rx skb, iommu-maps it and attaches it to the descriptor. | |
320 | * Activate the descriptor state-wise | |
321 | */ | |
59e97327 | 322 | static int gelic_descr_prepare_rx(struct gelic_card *card, |
589866f9 | 323 | struct gelic_descr *descr) |
02c18891 MM |
324 | { |
325 | int offset; | |
326 | unsigned int bufsize; | |
327 | ||
59e97327 | 328 | if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE) |
02c18891 | 329 | dev_info(ctodev(card), "%s: ERROR status \n", __func__); |
02c18891 MM |
330 | /* we need to round up the buffer size to a multiple of 128 */ |
331 | bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN); | |
332 | ||
333 | /* and we need to have it 128 byte aligned, therefore we allocate a | |
334 | * bit more */ | |
589866f9 | 335 | descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1); |
02c18891 MM |
336 | if (!descr->skb) { |
337 | descr->buf_addr = 0; /* tell DMAC don't touch memory */ | |
338 | dev_info(ctodev(card), | |
339 | "%s:allocate skb failed !!\n", __func__); | |
340 | return -ENOMEM; | |
341 | } | |
100e1d89 | 342 | descr->buf_size = cpu_to_be32(bufsize); |
02c18891 MM |
343 | descr->dmac_cmd_status = 0; |
344 | descr->result_size = 0; | |
345 | descr->valid_size = 0; | |
346 | descr->data_error = 0; | |
347 | ||
348 | offset = ((unsigned long)descr->skb->data) & | |
349 | (GELIC_NET_RXBUF_ALIGN - 1); | |
350 | if (offset) | |
351 | skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset); | |
352 | /* io-mmu-map the skb */ | |
100e1d89 MM |
353 | descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card), |
354 | descr->skb->data, | |
355 | GELIC_NET_MAX_MTU, | |
356 | DMA_FROM_DEVICE)); | |
02c18891 MM |
357 | if (!descr->buf_addr) { |
358 | dev_kfree_skb_any(descr->skb); | |
359 | descr->skb = NULL; | |
360 | dev_info(ctodev(card), | |
361 | "%s:Could not iommu-map rx buffer\n", __func__); | |
59e97327 | 362 | gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); |
02c18891 MM |
363 | return -ENOMEM; |
364 | } else { | |
59e97327 | 365 | gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED); |
02c18891 MM |
366 | return 0; |
367 | } | |
368 | } | |
369 | ||
370 | /** | |
59e97327 | 371 | * gelic_card_release_rx_chain - free all skb of rx descr |
02c18891 MM |
372 | * @card: card structure |
373 | * | |
374 | */ | |
59e97327 | 375 | static void gelic_card_release_rx_chain(struct gelic_card *card) |
02c18891 | 376 | { |
59e97327 | 377 | struct gelic_descr *descr = card->rx_chain.head; |
02c18891 MM |
378 | |
379 | do { | |
380 | if (descr->skb) { | |
381 | dma_unmap_single(ctodev(card), | |
100e1d89 | 382 | be32_to_cpu(descr->buf_addr), |
02c18891 MM |
383 | descr->skb->len, |
384 | DMA_FROM_DEVICE); | |
385 | descr->buf_addr = 0; | |
386 | dev_kfree_skb_any(descr->skb); | |
387 | descr->skb = NULL; | |
59e97327 MM |
388 | gelic_descr_set_status(descr, |
389 | GELIC_DESCR_DMA_NOT_IN_USE); | |
02c18891 MM |
390 | } |
391 | descr = descr->next; | |
392 | } while (descr != card->rx_chain.head); | |
393 | } | |
394 | ||
395 | /** | |
59e97327 | 396 | * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains |
02c18891 MM |
397 | * @card: card structure |
398 | * | |
399 | * fills all descriptors in the rx chain: allocates skbs | |
400 | * and iommu-maps them. | |
59e97327 | 401 | * returns 0 on success, < 0 on failure |
02c18891 | 402 | */ |
59e97327 | 403 | static int gelic_card_fill_rx_chain(struct gelic_card *card) |
02c18891 | 404 | { |
59e97327 | 405 | struct gelic_descr *descr = card->rx_chain.head; |
02c18891 MM |
406 | int ret; |
407 | ||
408 | do { | |
409 | if (!descr->skb) { | |
59e97327 | 410 | ret = gelic_descr_prepare_rx(card, descr); |
02c18891 MM |
411 | if (ret) |
412 | goto rewind; | |
413 | } | |
414 | descr = descr->next; | |
415 | } while (descr != card->rx_chain.head); | |
416 | ||
417 | return 0; | |
418 | rewind: | |
59e97327 | 419 | gelic_card_release_rx_chain(card); |
02c18891 MM |
420 | return ret; |
421 | } | |
422 | ||
423 | /** | |
59e97327 | 424 | * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains |
02c18891 MM |
425 | * @card: card structure |
426 | * | |
59e97327 | 427 | * returns 0 on success, < 0 on failure |
02c18891 | 428 | */ |
48dce82c | 429 | static int __devinit gelic_card_alloc_rx_skbs(struct gelic_card *card) |
02c18891 | 430 | { |
59e97327 | 431 | struct gelic_descr_chain *chain; |
02c18891 MM |
432 | int ret; |
433 | chain = &card->rx_chain; | |
59e97327 | 434 | ret = gelic_card_fill_rx_chain(card); |
589866f9 | 435 | chain->tail = card->rx_top->prev; /* point to the last */ |
02c18891 MM |
436 | return ret; |
437 | } | |
438 | ||
439 | /** | |
59e97327 | 440 | * gelic_descr_release_tx - processes a used tx descriptor |
02c18891 MM |
441 | * @card: card structure |
442 | * @descr: descriptor to release | |
443 | * | |
444 | * releases a used tx descriptor (unmapping, freeing of skb) | |
445 | */ | |
59e97327 | 446 | static void gelic_descr_release_tx(struct gelic_card *card, |
589866f9 | 447 | struct gelic_descr *descr) |
02c18891 | 448 | { |
173261ed | 449 | struct sk_buff *skb = descr->skb; |
02c18891 | 450 | |
589866f9 MM |
451 | BUG_ON(!(be32_to_cpu(descr->data_status) & GELIC_DESCR_TX_TAIL)); |
452 | ||
453 | dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), skb->len, | |
454 | DMA_TO_DEVICE); | |
173261ed | 455 | dev_kfree_skb_any(skb); |
02c18891 MM |
456 | |
457 | descr->buf_addr = 0; | |
458 | descr->buf_size = 0; | |
459 | descr->next_descr_addr = 0; | |
460 | descr->result_size = 0; | |
461 | descr->valid_size = 0; | |
462 | descr->data_status = 0; | |
463 | descr->data_error = 0; | |
464 | descr->skb = NULL; | |
465 | ||
466 | /* set descr status */ | |
59e97327 | 467 | gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); |
02c18891 MM |
468 | } |
469 | ||
589866f9 MM |
470 | static void gelic_card_stop_queues(struct gelic_card *card) |
471 | { | |
d4d7f1f9 | 472 | netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]); |
589866f9 MM |
473 | |
474 | if (card->netdev[GELIC_PORT_WIRELESS]) | |
475 | netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]); | |
476 | } | |
477 | static void gelic_card_wake_queues(struct gelic_card *card) | |
478 | { | |
d4d7f1f9 | 479 | netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]); |
589866f9 MM |
480 | |
481 | if (card->netdev[GELIC_PORT_WIRELESS]) | |
482 | netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]); | |
483 | } | |
02c18891 | 484 | /** |
59e97327 | 485 | * gelic_card_release_tx_chain - processes sent tx descriptors |
02c18891 MM |
486 | * @card: adapter structure |
487 | * @stop: net_stop sequence | |
488 | * | |
489 | * releases the tx descriptors that gelic has finished with | |
490 | */ | |
59e97327 | 491 | static void gelic_card_release_tx_chain(struct gelic_card *card, int stop) |
02c18891 | 492 | { |
59e97327 MM |
493 | struct gelic_descr_chain *tx_chain; |
494 | enum gelic_descr_dma_status status; | |
589866f9 | 495 | struct net_device *netdev; |
02c18891 MM |
496 | int release = 0; |
497 | ||
498 | for (tx_chain = &card->tx_chain; | |
499 | tx_chain->head != tx_chain->tail && tx_chain->tail; | |
500 | tx_chain->tail = tx_chain->tail->next) { | |
59e97327 | 501 | status = gelic_descr_get_status(tx_chain->tail); |
589866f9 | 502 | netdev = tx_chain->tail->skb->dev; |
02c18891 | 503 | switch (status) { |
59e97327 MM |
504 | case GELIC_DESCR_DMA_RESPONSE_ERROR: |
505 | case GELIC_DESCR_DMA_PROTECTION_ERROR: | |
506 | case GELIC_DESCR_DMA_FORCE_END: | |
02c18891 MM |
507 | if (printk_ratelimit()) |
508 | dev_info(ctodev(card), | |
509 | "%s: forcing end of tx descriptor " \ | |
510 | "with status %x\n", | |
511 | __func__, status); | |
589866f9 | 512 | netdev->stats.tx_dropped++; |
02c18891 MM |
513 | break; |
514 | ||
59e97327 | 515 | case GELIC_DESCR_DMA_COMPLETE: |
48544cc2 | 516 | if (tx_chain->tail->skb) { |
589866f9 MM |
517 | netdev->stats.tx_packets++; |
518 | netdev->stats.tx_bytes += | |
48544cc2 MM |
519 | tx_chain->tail->skb->len; |
520 | } | |
02c18891 MM |
521 | break; |
522 | ||
59e97327 | 523 | case GELIC_DESCR_DMA_CARDOWNED: |
02c18891 MM |
524 | /* pending tx request */ |
525 | default: | |
59e97327 | 526 | /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */ |
48544cc2 MM |
527 | if (!stop) |
528 | goto out; | |
02c18891 | 529 | } |
59e97327 | 530 | gelic_descr_release_tx(card, tx_chain->tail); |
48544cc2 | 531 | release ++; |
02c18891 MM |
532 | } |
533 | out: | |
173261ed | 534 | if (!stop && release) |
589866f9 | 535 | gelic_card_wake_queues(card); |
02c18891 MM |
536 | } |
537 | ||
538 | /** | |
539 | * gelic_net_set_multi - sets multicast addresses and promisc flags | |
540 | * @netdev: interface device structure | |
541 | * | |
542 | * gelic_net_set_multi configures multicast addresses as needed for the | |
543 | * netdev interface. It also sets up multicast, allmulti and promisc | |
544 | * flags appropriately | |
545 | */ | |
589866f9 | 546 | void gelic_net_set_multi(struct net_device *netdev) |
02c18891 | 547 | { |
589866f9 | 548 | struct gelic_card *card = netdev_card(netdev); |
02c18891 MM |
549 | struct dev_mc_list *mc; |
550 | unsigned int i; | |
551 | uint8_t *p; | |
552 | u64 addr; | |
553 | int status; | |
554 | ||
555 | /* clear all multicast address */ | |
556 | status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card), | |
557 | 0, 1); | |
558 | if (status) | |
559 | dev_err(ctodev(card), | |
560 | "lv1_net_remove_multicast_address failed %d\n", | |
561 | status); | |
562 | /* set broadcast address */ | |
563 | status = lv1_net_add_multicast_address(bus_id(card), dev_id(card), | |
564 | GELIC_NET_BROADCAST_ADDR, 0); | |
565 | if (status) | |
566 | dev_err(ctodev(card), | |
567 | "lv1_net_add_multicast_address failed, %d\n", | |
568 | status); | |
569 | ||
589866f9 MM |
570 | if ((netdev->flags & IFF_ALLMULTI) || |
571 | (netdev->mc_count > GELIC_NET_MC_COUNT_MAX)) { | |
02c18891 MM |
572 | status = lv1_net_add_multicast_address(bus_id(card), |
573 | dev_id(card), | |
574 | 0, 1); | |
575 | if (status) | |
576 | dev_err(ctodev(card), | |
577 | "lv1_net_add_multicast_address failed, %d\n", | |
578 | status); | |
579 | return; | |
580 | } | |
581 | ||
589866f9 | 582 | /* set multicast addresses */ |
02c18891 MM |
583 | for (mc = netdev->mc_list; mc; mc = mc->next) { |
584 | addr = 0; | |
585 | p = mc->dmi_addr; | |
586 | for (i = 0; i < ETH_ALEN; i++) { | |
587 | addr <<= 8; | |
588 | addr |= *p++; | |
589 | } | |
590 | status = lv1_net_add_multicast_address(bus_id(card), | |
591 | dev_id(card), | |
592 | addr, 0); | |
593 | if (status) | |
594 | dev_err(ctodev(card), | |
595 | "lv1_net_add_multicast_address failed, %d\n", | |
596 | status); | |
597 | } | |
598 | } | |
599 | ||
600 | /** | |
59e97327 | 601 | * gelic_card_enable_rxdmac - enables the receive DMA controller |
02c18891 MM |
602 | * @card: card structure |
603 | * | |
59e97327 | 604 | * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN |
02c18891 MM |
605 | * in the GDADMACCNTR register |
606 | */ | |
59e97327 | 607 | static inline void gelic_card_enable_rxdmac(struct gelic_card *card) |
02c18891 MM |
608 | { |
609 | int status; | |
610 | ||
589866f9 MM |
611 | #ifdef DEBUG |
612 | if (gelic_descr_get_status(card->rx_chain.head) != | |
613 | GELIC_DESCR_DMA_CARDOWNED) { | |
614 | printk(KERN_ERR "%s: status=%x\n", __func__, | |
615 | be32_to_cpu(card->rx_chain.head->dmac_cmd_status)); | |
616 | printk(KERN_ERR "%s: nextphy=%x\n", __func__, | |
617 | be32_to_cpu(card->rx_chain.head->next_descr_addr)); | |
618 | printk(KERN_ERR "%s: head=%p\n", __func__, | |
619 | card->rx_chain.head); | |
620 | } | |
621 | #endif | |
02c18891 | 622 | status = lv1_net_start_rx_dma(bus_id(card), dev_id(card), |
589866f9 | 623 | card->rx_chain.head->bus_addr, 0); |
02c18891 MM |
624 | if (status) |
625 | dev_info(ctodev(card), | |
626 | "lv1_net_start_rx_dma failed, status=%d\n", status); | |
627 | } | |
628 | ||
629 | /** | |
59e97327 | 630 | * gelic_card_disable_rxdmac - disables the receive DMA controller |
02c18891 MM |
631 | * @card: card structure |
632 | * | |
59e97327 | 633 | * gelic_card_disable_rxdmac terminates processing on the DMA controller by |
02c18891 MM |
634 | * turing off DMA and issueing a force end |
635 | */ | |
59e97327 | 636 | static inline void gelic_card_disable_rxdmac(struct gelic_card *card) |
02c18891 MM |
637 | { |
638 | int status; | |
639 | ||
640 | /* this hvc blocks until the DMA in progress really stopped */ | |
641 | status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0); | |
642 | if (status) | |
643 | dev_err(ctodev(card), | |
644 | "lv1_net_stop_rx_dma faild, %d\n", status); | |
645 | } | |
646 | ||
647 | /** | |
59e97327 | 648 | * gelic_card_disable_txdmac - disables the transmit DMA controller |
02c18891 MM |
649 | * @card: card structure |
650 | * | |
59e97327 | 651 | * gelic_card_disable_txdmac terminates processing on the DMA controller by |
02c18891 MM |
652 | * turing off DMA and issueing a force end |
653 | */ | |
59e97327 | 654 | static inline void gelic_card_disable_txdmac(struct gelic_card *card) |
02c18891 MM |
655 | { |
656 | int status; | |
657 | ||
658 | /* this hvc blocks until the DMA in progress really stopped */ | |
659 | status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0); | |
660 | if (status) | |
661 | dev_err(ctodev(card), | |
662 | "lv1_net_stop_tx_dma faild, status=%d\n", status); | |
663 | } | |
664 | ||
665 | /** | |
666 | * gelic_net_stop - called upon ifconfig down | |
667 | * @netdev: interface device structure | |
668 | * | |
669 | * always returns 0 | |
670 | */ | |
589866f9 | 671 | int gelic_net_stop(struct net_device *netdev) |
02c18891 | 672 | { |
589866f9 | 673 | struct gelic_card *card; |
02c18891 | 674 | |
589866f9 | 675 | pr_debug("%s: start\n", __func__); |
02c18891 | 676 | |
589866f9 | 677 | netif_stop_queue(netdev); |
02c18891 MM |
678 | netif_carrier_off(netdev); |
679 | ||
589866f9 MM |
680 | card = netdev_card(netdev); |
681 | gelic_card_down(card); | |
02c18891 | 682 | |
589866f9 | 683 | pr_debug("%s: done\n", __func__); |
02c18891 MM |
684 | return 0; |
685 | } | |
686 | ||
687 | /** | |
59e97327 | 688 | * gelic_card_get_next_tx_descr - returns the next available tx descriptor |
02c18891 MM |
689 | * @card: device structure to get descriptor from |
690 | * | |
691 | * returns the address of the next descriptor, or NULL if not available. | |
692 | */ | |
59e97327 MM |
693 | static struct gelic_descr * |
694 | gelic_card_get_next_tx_descr(struct gelic_card *card) | |
02c18891 MM |
695 | { |
696 | if (!card->tx_chain.head) | |
697 | return NULL; | |
173261ed | 698 | /* see if the next descriptor is free */ |
02c18891 | 699 | if (card->tx_chain.tail != card->tx_chain.head->next && |
59e97327 MM |
700 | gelic_descr_get_status(card->tx_chain.head) == |
701 | GELIC_DESCR_DMA_NOT_IN_USE) | |
02c18891 MM |
702 | return card->tx_chain.head; |
703 | else | |
704 | return NULL; | |
705 | ||
706 | } | |
707 | ||
708 | /** | |
589866f9 | 709 | * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field |
02c18891 MM |
710 | * @descr: descriptor structure to fill out |
711 | * @skb: packet to consider | |
02c18891 MM |
712 | * |
713 | * fills out the command and status field of the descriptor structure, | |
714 | * depending on hardware checksum settings. This function assumes a wmb() | |
715 | * has executed before. | |
716 | */ | |
59e97327 MM |
717 | static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr, |
718 | struct sk_buff *skb) | |
02c18891 | 719 | { |
02c18891 | 720 | if (skb->ip_summed != CHECKSUM_PARTIAL) |
100e1d89 | 721 | descr->dmac_cmd_status = |
59e97327 MM |
722 | cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM | |
723 | GELIC_DESCR_TX_DMA_FRAME_TAIL); | |
02c18891 MM |
724 | else { |
725 | /* is packet ip? | |
726 | * if yes: tcp? udp? */ | |
727 | if (skb->protocol == htons(ETH_P_IP)) { | |
728 | if (ip_hdr(skb)->protocol == IPPROTO_TCP) | |
729 | descr->dmac_cmd_status = | |
59e97327 MM |
730 | cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM | |
731 | GELIC_DESCR_TX_DMA_FRAME_TAIL); | |
173261ed | 732 | |
02c18891 MM |
733 | else if (ip_hdr(skb)->protocol == IPPROTO_UDP) |
734 | descr->dmac_cmd_status = | |
59e97327 MM |
735 | cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM | |
736 | GELIC_DESCR_TX_DMA_FRAME_TAIL); | |
02c18891 MM |
737 | else /* |
738 | * the stack should checksum non-tcp and non-udp | |
739 | * packets on his own: NETIF_F_IP_CSUM | |
740 | */ | |
741 | descr->dmac_cmd_status = | |
59e97327 MM |
742 | cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM | |
743 | GELIC_DESCR_TX_DMA_FRAME_TAIL); | |
02c18891 MM |
744 | } |
745 | } | |
746 | } | |
747 | ||
173261ed MM |
748 | static inline struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb, |
749 | unsigned short tag) | |
750 | { | |
751 | struct vlan_ethhdr *veth; | |
752 | static unsigned int c; | |
753 | ||
754 | if (skb_headroom(skb) < VLAN_HLEN) { | |
755 | struct sk_buff *sk_tmp = skb; | |
756 | pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c); | |
757 | skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN); | |
758 | if (!skb) | |
759 | return NULL; | |
760 | dev_kfree_skb_any(sk_tmp); | |
761 | } | |
762 | veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN); | |
763 | ||
764 | /* Move the mac addresses to the top of buffer */ | |
765 | memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN); | |
766 | ||
09640e63 | 767 | veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q); |
173261ed MM |
768 | veth->h_vlan_TCI = htons(tag); |
769 | ||
770 | return skb; | |
771 | } | |
772 | ||
02c18891 | 773 | /** |
589866f9 | 774 | * gelic_descr_prepare_tx - setup a descriptor for sending packets |
02c18891 MM |
775 | * @card: card structure |
776 | * @descr: descriptor structure | |
777 | * @skb: packet to use | |
778 | * | |
779 | * returns 0 on success, <0 on failure. | |
780 | * | |
781 | */ | |
59e97327 MM |
782 | static int gelic_descr_prepare_tx(struct gelic_card *card, |
783 | struct gelic_descr *descr, | |
784 | struct sk_buff *skb) | |
02c18891 | 785 | { |
173261ed | 786 | dma_addr_t buf; |
02c18891 | 787 | |
589866f9 | 788 | if (card->vlan_required) { |
173261ed | 789 | struct sk_buff *skb_tmp; |
589866f9 MM |
790 | enum gelic_port_type type; |
791 | ||
792 | type = netdev_port(skb->dev)->type; | |
173261ed | 793 | skb_tmp = gelic_put_vlan_tag(skb, |
589866f9 | 794 | card->vlan[type].tx); |
173261ed MM |
795 | if (!skb_tmp) |
796 | return -ENOMEM; | |
797 | skb = skb_tmp; | |
02c18891 MM |
798 | } |
799 | ||
173261ed | 800 | buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE); |
02c18891 | 801 | |
173261ed | 802 | if (!buf) { |
02c18891 MM |
803 | dev_err(ctodev(card), |
804 | "dma map 2 failed (%p, %i). Dropping packet\n", | |
173261ed | 805 | skb->data, skb->len); |
02c18891 MM |
806 | return -ENOMEM; |
807 | } | |
808 | ||
100e1d89 MM |
809 | descr->buf_addr = cpu_to_be32(buf); |
810 | descr->buf_size = cpu_to_be32(skb->len); | |
173261ed | 811 | descr->skb = skb; |
02c18891 | 812 | descr->data_status = 0; |
173261ed | 813 | descr->next_descr_addr = 0; /* terminate hw descr */ |
59e97327 | 814 | gelic_descr_set_tx_cmdstat(descr, skb); |
48544cc2 MM |
815 | |
816 | /* bump free descriptor pointer */ | |
173261ed | 817 | card->tx_chain.head = descr->next; |
02c18891 MM |
818 | return 0; |
819 | } | |
820 | ||
821 | /** | |
59e97327 | 822 | * gelic_card_kick_txdma - enables TX DMA processing |
02c18891 MM |
823 | * @card: card structure |
824 | * @descr: descriptor address to enable TX processing at | |
825 | * | |
826 | */ | |
59e97327 MM |
827 | static int gelic_card_kick_txdma(struct gelic_card *card, |
828 | struct gelic_descr *descr) | |
02c18891 | 829 | { |
48544cc2 | 830 | int status = 0; |
02c18891 MM |
831 | |
832 | if (card->tx_dma_progress) | |
833 | return 0; | |
834 | ||
59e97327 | 835 | if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) { |
02c18891 | 836 | card->tx_dma_progress = 1; |
dc029ad9 MM |
837 | status = lv1_net_start_tx_dma(bus_id(card), dev_id(card), |
838 | descr->bus_addr, 0); | |
839 | if (status) | |
02c18891 | 840 | dev_info(ctodev(card), "lv1_net_start_txdma failed," \ |
dc029ad9 | 841 | "status=%d\n", status); |
02c18891 MM |
842 | } |
843 | return status; | |
844 | } | |
845 | ||
846 | /** | |
847 | * gelic_net_xmit - transmits a frame over the device | |
848 | * @skb: packet to send out | |
849 | * @netdev: interface device structure | |
850 | * | |
851 | * returns 0 on success, <0 on failure | |
852 | */ | |
589866f9 | 853 | int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev) |
02c18891 | 854 | { |
589866f9 | 855 | struct gelic_card *card = netdev_card(netdev); |
59e97327 | 856 | struct gelic_descr *descr; |
02c18891 MM |
857 | int result; |
858 | unsigned long flags; | |
859 | ||
589866f9 | 860 | spin_lock_irqsave(&card->tx_lock, flags); |
02c18891 | 861 | |
59e97327 | 862 | gelic_card_release_tx_chain(card, 0); |
48544cc2 | 863 | |
59e97327 | 864 | descr = gelic_card_get_next_tx_descr(card); |
02c18891 | 865 | if (!descr) { |
48544cc2 MM |
866 | /* |
867 | * no more descriptors free | |
868 | */ | |
589866f9 MM |
869 | gelic_card_stop_queues(card); |
870 | spin_unlock_irqrestore(&card->tx_lock, flags); | |
02c18891 MM |
871 | return NETDEV_TX_BUSY; |
872 | } | |
02c18891 | 873 | |
59e97327 | 874 | result = gelic_descr_prepare_tx(card, descr, skb); |
48544cc2 MM |
875 | if (result) { |
876 | /* | |
877 | * DMA map failed. As chanses are that failure | |
878 | * would continue, just release skb and return | |
879 | */ | |
589866f9 | 880 | netdev->stats.tx_dropped++; |
48544cc2 | 881 | dev_kfree_skb_any(skb); |
589866f9 | 882 | spin_unlock_irqrestore(&card->tx_lock, flags); |
48544cc2 MM |
883 | return NETDEV_TX_OK; |
884 | } | |
885 | /* | |
886 | * link this prepared descriptor to previous one | |
887 | * to achieve high performance | |
888 | */ | |
100e1d89 | 889 | descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr); |
02c18891 MM |
890 | /* |
891 | * as hardware descriptor is modified in the above lines, | |
892 | * ensure that the hardware sees it | |
893 | */ | |
894 | wmb(); | |
59e97327 | 895 | if (gelic_card_kick_txdma(card, descr)) { |
48544cc2 MM |
896 | /* |
897 | * kick failed. | |
898 | * release descriptors which were just prepared | |
899 | */ | |
589866f9 | 900 | netdev->stats.tx_dropped++; |
59e97327 MM |
901 | gelic_descr_release_tx(card, descr); |
902 | gelic_descr_release_tx(card, descr->next); | |
48544cc2 MM |
903 | card->tx_chain.tail = descr->next->next; |
904 | dev_info(ctodev(card), "%s: kick failure\n", __func__); | |
905 | } else { | |
906 | /* OK, DMA started/reserved */ | |
907 | netdev->trans_start = jiffies; | |
908 | } | |
02c18891 | 909 | |
589866f9 | 910 | spin_unlock_irqrestore(&card->tx_lock, flags); |
02c18891 | 911 | return NETDEV_TX_OK; |
02c18891 MM |
912 | } |
913 | ||
914 | /** | |
915 | * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on | |
916 | * @descr: descriptor to process | |
917 | * @card: card structure | |
589866f9 | 918 | * @netdev: net_device structure to be passed packet |
02c18891 MM |
919 | * |
920 | * iommu-unmaps the skb, fills out skb structure and passes the data to the | |
921 | * stack. The descriptor state is not changed. | |
922 | */ | |
59e97327 | 923 | static void gelic_net_pass_skb_up(struct gelic_descr *descr, |
589866f9 MM |
924 | struct gelic_card *card, |
925 | struct net_device *netdev) | |
926 | ||
02c18891 | 927 | { |
589866f9 | 928 | struct sk_buff *skb = descr->skb; |
02c18891 MM |
929 | u32 data_status, data_error; |
930 | ||
100e1d89 MM |
931 | data_status = be32_to_cpu(descr->data_status); |
932 | data_error = be32_to_cpu(descr->data_error); | |
02c18891 | 933 | /* unmap skb buffer */ |
589866f9 MM |
934 | dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), |
935 | GELIC_NET_MAX_MTU, | |
02c18891 MM |
936 | DMA_FROM_DEVICE); |
937 | ||
589866f9 | 938 | skb_put(skb, be32_to_cpu(descr->valid_size)? |
100e1d89 MM |
939 | be32_to_cpu(descr->valid_size) : |
940 | be32_to_cpu(descr->result_size)); | |
02c18891 MM |
941 | if (!descr->valid_size) |
942 | dev_info(ctodev(card), "buffer full %x %x %x\n", | |
100e1d89 MM |
943 | be32_to_cpu(descr->result_size), |
944 | be32_to_cpu(descr->buf_size), | |
945 | be32_to_cpu(descr->dmac_cmd_status)); | |
02c18891 MM |
946 | |
947 | descr->skb = NULL; | |
948 | /* | |
949 | * the card put 2 bytes vlan tag in front | |
950 | * of the ethernet frame | |
951 | */ | |
952 | skb_pull(skb, 2); | |
953 | skb->protocol = eth_type_trans(skb, netdev); | |
954 | ||
955 | /* checksum offload */ | |
956 | if (card->rx_csum) { | |
59e97327 MM |
957 | if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) && |
958 | (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK))) | |
02c18891 MM |
959 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
960 | else | |
961 | skb->ip_summed = CHECKSUM_NONE; | |
962 | } else | |
963 | skb->ip_summed = CHECKSUM_NONE; | |
964 | ||
965 | /* update netdevice statistics */ | |
589866f9 MM |
966 | netdev->stats.rx_packets++; |
967 | netdev->stats.rx_bytes += skb->len; | |
02c18891 MM |
968 | |
969 | /* pass skb up to stack */ | |
970 | netif_receive_skb(skb); | |
971 | } | |
972 | ||
973 | /** | |
59e97327 | 974 | * gelic_card_decode_one_descr - processes an rx descriptor |
02c18891 MM |
975 | * @card: card structure |
976 | * | |
977 | * returns 1 if a packet has been sent to the stack, otherwise 0 | |
978 | * | |
979 | * processes an rx descriptor by iommu-unmapping the data buffer and passing | |
980 | * the packet up to the stack | |
981 | */ | |
59e97327 | 982 | static int gelic_card_decode_one_descr(struct gelic_card *card) |
02c18891 | 983 | { |
59e97327 MM |
984 | enum gelic_descr_dma_status status; |
985 | struct gelic_descr_chain *chain = &card->rx_chain; | |
589866f9 MM |
986 | struct gelic_descr *descr = chain->head; |
987 | struct net_device *netdev = NULL; | |
02c18891 MM |
988 | int dmac_chain_ended; |
989 | ||
59e97327 | 990 | status = gelic_descr_get_status(descr); |
02c18891 MM |
991 | /* is this descriptor terminated with next_descr == NULL? */ |
992 | dmac_chain_ended = | |
100e1d89 | 993 | be32_to_cpu(descr->dmac_cmd_status) & |
59e97327 | 994 | GELIC_DESCR_RX_DMA_CHAIN_END; |
02c18891 | 995 | |
59e97327 | 996 | if (status == GELIC_DESCR_DMA_CARDOWNED) |
02c18891 MM |
997 | return 0; |
998 | ||
59e97327 | 999 | if (status == GELIC_DESCR_DMA_NOT_IN_USE) { |
02c18891 MM |
1000 | dev_dbg(ctodev(card), "dormant descr? %p\n", descr); |
1001 | return 0; | |
1002 | } | |
1003 | ||
589866f9 MM |
1004 | /* netdevice select */ |
1005 | if (card->vlan_required) { | |
1006 | unsigned int i; | |
1007 | u16 vid; | |
1008 | vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK; | |
1009 | for (i = 0; i < GELIC_PORT_MAX; i++) { | |
1010 | if (card->vlan[i].rx == vid) { | |
1011 | netdev = card->netdev[i]; | |
1012 | break; | |
1013 | } | |
1014 | }; | |
1015 | if (GELIC_PORT_MAX <= i) { | |
1016 | pr_info("%s: unknown packet vid=%x\n", __func__, vid); | |
1017 | goto refill; | |
1018 | } | |
1019 | } else | |
d4d7f1f9 | 1020 | netdev = card->netdev[GELIC_PORT_ETHERNET_0]; |
589866f9 | 1021 | |
59e97327 MM |
1022 | if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) || |
1023 | (status == GELIC_DESCR_DMA_PROTECTION_ERROR) || | |
1024 | (status == GELIC_DESCR_DMA_FORCE_END)) { | |
02c18891 MM |
1025 | dev_info(ctodev(card), "dropping RX descriptor with state %x\n", |
1026 | status); | |
589866f9 | 1027 | netdev->stats.rx_dropped++; |
02c18891 MM |
1028 | goto refill; |
1029 | } | |
1030 | ||
59e97327 | 1031 | if (status == GELIC_DESCR_DMA_BUFFER_FULL) { |
fe6d3a40 MM |
1032 | /* |
1033 | * Buffer full would occur if and only if | |
1034 | * the frame length was longer than the size of this | |
1035 | * descriptor's buffer. If the frame length was equal | |
1036 | * to or shorter than buffer'size, FRAME_END condition | |
1037 | * would occur. | |
1038 | * Anyway this frame was longer than the MTU, | |
1039 | * just drop it. | |
1040 | */ | |
1041 | dev_info(ctodev(card), "overlength frame\n"); | |
1042 | goto refill; | |
1043 | } | |
1044 | /* | |
1045 | * descriptoers any other than FRAME_END here should | |
1046 | * be treated as error. | |
1047 | */ | |
59e97327 | 1048 | if (status != GELIC_DESCR_DMA_FRAME_END) { |
02c18891 MM |
1049 | dev_dbg(ctodev(card), "RX descriptor with state %x\n", |
1050 | status); | |
1051 | goto refill; | |
1052 | } | |
1053 | ||
1054 | /* ok, we've got a packet in descr */ | |
589866f9 | 1055 | gelic_net_pass_skb_up(descr, card, netdev); |
02c18891 | 1056 | refill: |
fe6d3a40 MM |
1057 | /* |
1058 | * So that always DMAC can see the end | |
1059 | * of the descriptor chain to avoid | |
1060 | * from unwanted DMAC overrun. | |
1061 | */ | |
1062 | descr->next_descr_addr = 0; | |
02c18891 MM |
1063 | |
1064 | /* change the descriptor state: */ | |
59e97327 | 1065 | gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); |
02c18891 | 1066 | |
fe6d3a40 MM |
1067 | /* |
1068 | * this call can fail, but for now, just leave this | |
1069 | * decriptor without skb | |
02c18891 | 1070 | */ |
59e97327 | 1071 | gelic_descr_prepare_rx(card, descr); |
fe6d3a40 | 1072 | |
589866f9 MM |
1073 | chain->tail = descr; |
1074 | chain->head = descr->next; | |
fe6d3a40 MM |
1075 | |
1076 | /* | |
1077 | * Set this descriptor the end of the chain. | |
1078 | */ | |
100e1d89 | 1079 | descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr); |
02c18891 | 1080 | |
fe6d3a40 MM |
1081 | /* |
1082 | * If dmac chain was met, DMAC stopped. | |
1083 | * thus re-enable it | |
1084 | */ | |
02c18891 | 1085 | if (dmac_chain_ended) { |
583aae10 MM |
1086 | card->rx_dma_restart_required = 1; |
1087 | dev_dbg(ctodev(card), "reenable rx dma scheduled\n"); | |
02c18891 MM |
1088 | } |
1089 | ||
1090 | return 1; | |
1091 | } | |
1092 | ||
1093 | /** | |
1094 | * gelic_net_poll - NAPI poll function called by the stack to return packets | |
589866f9 | 1095 | * @napi: napi structure |
02c18891 MM |
1096 | * @budget: number of packets we can pass to the stack at most |
1097 | * | |
589866f9 | 1098 | * returns the number of the processed packets |
02c18891 MM |
1099 | * |
1100 | */ | |
bea3348e | 1101 | static int gelic_net_poll(struct napi_struct *napi, int budget) |
02c18891 | 1102 | { |
59e97327 | 1103 | struct gelic_card *card = container_of(napi, struct gelic_card, napi); |
bea3348e | 1104 | int packets_done = 0; |
02c18891 | 1105 | |
bea3348e | 1106 | while (packets_done < budget) { |
59e97327 | 1107 | if (!gelic_card_decode_one_descr(card)) |
02c18891 | 1108 | break; |
bea3348e SH |
1109 | |
1110 | packets_done++; | |
02c18891 | 1111 | } |
bea3348e SH |
1112 | |
1113 | if (packets_done < budget) { | |
589866f9 | 1114 | napi_complete(napi); |
59e97327 | 1115 | gelic_card_rx_irq_on(card); |
bea3348e SH |
1116 | } |
1117 | return packets_done; | |
02c18891 | 1118 | } |
02c18891 MM |
1119 | /** |
1120 | * gelic_net_change_mtu - changes the MTU of an interface | |
1121 | * @netdev: interface device structure | |
1122 | * @new_mtu: new MTU value | |
1123 | * | |
1124 | * returns 0 on success, <0 on failure | |
1125 | */ | |
589866f9 | 1126 | int gelic_net_change_mtu(struct net_device *netdev, int new_mtu) |
02c18891 MM |
1127 | { |
1128 | /* no need to re-alloc skbs or so -- the max mtu is about 2.3k | |
1129 | * and mtu is outbound only anyway */ | |
1130 | if ((new_mtu < GELIC_NET_MIN_MTU) || | |
1131 | (new_mtu > GELIC_NET_MAX_MTU)) { | |
1132 | return -EINVAL; | |
1133 | } | |
1134 | netdev->mtu = new_mtu; | |
1135 | return 0; | |
1136 | } | |
1137 | ||
1138 | /** | |
59e97327 | 1139 | * gelic_card_interrupt - event handler for gelic_net |
02c18891 | 1140 | */ |
59e97327 | 1141 | static irqreturn_t gelic_card_interrupt(int irq, void *ptr) |
02c18891 MM |
1142 | { |
1143 | unsigned long flags; | |
589866f9 | 1144 | struct gelic_card *card = ptr; |
02c18891 MM |
1145 | u64 status; |
1146 | ||
1147 | status = card->irq_status; | |
1148 | ||
1149 | if (!status) | |
1150 | return IRQ_NONE; | |
1151 | ||
589866f9 MM |
1152 | status &= card->irq_mask; |
1153 | ||
583aae10 MM |
1154 | if (card->rx_dma_restart_required) { |
1155 | card->rx_dma_restart_required = 0; | |
59e97327 | 1156 | gelic_card_enable_rxdmac(card); |
583aae10 MM |
1157 | } |
1158 | ||
59e97327 MM |
1159 | if (status & GELIC_CARD_RXINT) { |
1160 | gelic_card_rx_irq_off(card); | |
589866f9 | 1161 | napi_schedule(&card->napi); |
02c18891 MM |
1162 | } |
1163 | ||
59e97327 | 1164 | if (status & GELIC_CARD_TXINT) { |
589866f9 | 1165 | spin_lock_irqsave(&card->tx_lock, flags); |
02c18891 | 1166 | card->tx_dma_progress = 0; |
59e97327 | 1167 | gelic_card_release_tx_chain(card, 0); |
48544cc2 | 1168 | /* kick outstanding tx descriptor if any */ |
59e97327 | 1169 | gelic_card_kick_txdma(card, card->tx_chain.tail); |
589866f9 | 1170 | spin_unlock_irqrestore(&card->tx_lock, flags); |
02c18891 | 1171 | } |
01fed4c2 MM |
1172 | |
1173 | /* ether port status changed */ | |
1174 | if (status & GELIC_CARD_PORT_STATUS_CHANGED) | |
1175 | gelic_card_get_ether_port_status(card, 1); | |
589866f9 | 1176 | |
09dde54c MM |
1177 | #ifdef CONFIG_GELIC_WIRELESS |
1178 | if (status & (GELIC_CARD_WLAN_EVENT_RECEIVED | | |
1179 | GELIC_CARD_WLAN_COMMAND_COMPLETED)) | |
1180 | gelic_wl_interrupt(card->netdev[GELIC_PORT_WIRELESS], status); | |
1181 | #endif | |
1182 | ||
02c18891 MM |
1183 | return IRQ_HANDLED; |
1184 | } | |
1185 | ||
1186 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
1187 | /** | |
1188 | * gelic_net_poll_controller - artificial interrupt for netconsole etc. | |
1189 | * @netdev: interface device structure | |
1190 | * | |
1191 | * see Documentation/networking/netconsole.txt | |
1192 | */ | |
589866f9 | 1193 | void gelic_net_poll_controller(struct net_device *netdev) |
02c18891 | 1194 | { |
589866f9 | 1195 | struct gelic_card *card = netdev_card(netdev); |
02c18891 | 1196 | |
59e97327 MM |
1197 | gelic_card_set_irq_mask(card, 0); |
1198 | gelic_card_interrupt(netdev->irq, netdev); | |
589866f9 | 1199 | gelic_card_set_irq_mask(card, card->irq_mask); |
02c18891 MM |
1200 | } |
1201 | #endif /* CONFIG_NET_POLL_CONTROLLER */ | |
1202 | ||
02c18891 MM |
1203 | /** |
1204 | * gelic_net_open - called upon ifonfig up | |
1205 | * @netdev: interface device structure | |
1206 | * | |
1207 | * returns 0 on success, <0 on failure | |
1208 | * | |
1209 | * gelic_net_open allocates all the descriptors and memory needed for | |
1210 | * operation, sets up multicast list and enables interrupts | |
1211 | */ | |
589866f9 | 1212 | int gelic_net_open(struct net_device *netdev) |
02c18891 | 1213 | { |
589866f9 | 1214 | struct gelic_card *card = netdev_card(netdev); |
02c18891 | 1215 | |
589866f9 | 1216 | dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev); |
02c18891 | 1217 | |
589866f9 | 1218 | gelic_card_up(card); |
02c18891 MM |
1219 | |
1220 | netif_start_queue(netdev); | |
01fed4c2 | 1221 | gelic_card_get_ether_port_status(card, 1); |
02c18891 | 1222 | |
589866f9 | 1223 | dev_dbg(ctodev(card), " <- %s\n", __func__); |
02c18891 | 1224 | return 0; |
02c18891 MM |
1225 | } |
1226 | ||
589866f9 MM |
1227 | void gelic_net_get_drvinfo(struct net_device *netdev, |
1228 | struct ethtool_drvinfo *info) | |
02c18891 MM |
1229 | { |
1230 | strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1); | |
1231 | strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1); | |
1232 | } | |
1233 | ||
59e97327 MM |
1234 | static int gelic_ether_get_settings(struct net_device *netdev, |
1235 | struct ethtool_cmd *cmd) | |
02c18891 | 1236 | { |
589866f9 | 1237 | struct gelic_card *card = netdev_card(netdev); |
02c18891 | 1238 | |
01fed4c2 MM |
1239 | gelic_card_get_ether_port_status(card, 0); |
1240 | ||
1241 | if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX) | |
1242 | cmd->duplex = DUPLEX_FULL; | |
1243 | else | |
1244 | cmd->duplex = DUPLEX_HALF; | |
1245 | ||
1246 | switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) { | |
1247 | case GELIC_LV1_ETHER_SPEED_10: | |
1248 | cmd->speed = SPEED_10; | |
1249 | break; | |
1250 | case GELIC_LV1_ETHER_SPEED_100: | |
1251 | cmd->speed = SPEED_100; | |
1252 | break; | |
1253 | case GELIC_LV1_ETHER_SPEED_1000: | |
1254 | cmd->speed = SPEED_1000; | |
1255 | break; | |
1256 | default: | |
1257 | pr_info("%s: speed unknown\n", __func__); | |
1258 | cmd->speed = SPEED_10; | |
1259 | break; | |
02c18891 | 1260 | } |
01fed4c2 | 1261 | |
02c18891 MM |
1262 | cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg | |
1263 | SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | | |
1264 | SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full | | |
55873ed8 | 1265 | SUPPORTED_1000baseT_Full; |
02c18891 | 1266 | cmd->advertising = cmd->supported; |
55873ed8 HS |
1267 | if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) { |
1268 | cmd->autoneg = AUTONEG_ENABLE; | |
1269 | } else { | |
1270 | cmd->autoneg = AUTONEG_DISABLE; | |
1271 | cmd->advertising &= ~ADVERTISED_Autoneg; | |
1272 | } | |
02c18891 MM |
1273 | cmd->port = PORT_TP; |
1274 | ||
1275 | return 0; | |
1276 | } | |
1277 | ||
55873ed8 HS |
1278 | static int gelic_ether_set_settings(struct net_device *netdev, |
1279 | struct ethtool_cmd *cmd) | |
1280 | { | |
1281 | struct gelic_card *card = netdev_card(netdev); | |
1282 | u64 mode; | |
1283 | int ret; | |
1284 | ||
1285 | if (cmd->autoneg == AUTONEG_ENABLE) { | |
1286 | mode = GELIC_LV1_ETHER_AUTO_NEG; | |
1287 | } else { | |
1288 | switch (cmd->speed) { | |
1289 | case SPEED_10: | |
1290 | mode = GELIC_LV1_ETHER_SPEED_10; | |
1291 | break; | |
1292 | case SPEED_100: | |
1293 | mode = GELIC_LV1_ETHER_SPEED_100; | |
1294 | break; | |
1295 | case SPEED_1000: | |
1296 | mode = GELIC_LV1_ETHER_SPEED_1000; | |
1297 | break; | |
1298 | default: | |
1299 | return -EINVAL; | |
1300 | } | |
1301 | if (cmd->duplex == DUPLEX_FULL) | |
1302 | mode |= GELIC_LV1_ETHER_FULL_DUPLEX; | |
1303 | else if (cmd->speed == SPEED_1000) { | |
1304 | pr_info("1000 half duplex is not supported.\n"); | |
1305 | return -EINVAL; | |
1306 | } | |
1307 | } | |
1308 | ||
1309 | ret = gelic_card_set_link_mode(card, mode); | |
1310 | ||
1311 | if (ret) | |
1312 | return ret; | |
1313 | ||
1314 | return 0; | |
1315 | } | |
1316 | ||
589866f9 | 1317 | u32 gelic_net_get_rx_csum(struct net_device *netdev) |
02c18891 | 1318 | { |
589866f9 | 1319 | struct gelic_card *card = netdev_card(netdev); |
02c18891 MM |
1320 | |
1321 | return card->rx_csum; | |
1322 | } | |
1323 | ||
589866f9 | 1324 | int gelic_net_set_rx_csum(struct net_device *netdev, u32 data) |
02c18891 | 1325 | { |
589866f9 | 1326 | struct gelic_card *card = netdev_card(netdev); |
02c18891 MM |
1327 | |
1328 | card->rx_csum = data; | |
1329 | return 0; | |
1330 | } | |
1331 | ||
3faac215 MM |
1332 | static void gelic_net_get_wol(struct net_device *netdev, |
1333 | struct ethtool_wolinfo *wol) | |
1334 | { | |
1335 | if (0 <= ps3_compare_firmware_version(2, 2, 0)) | |
1336 | wol->supported = WAKE_MAGIC; | |
1337 | else | |
1338 | wol->supported = 0; | |
1339 | ||
1340 | wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0; | |
1341 | memset(&wol->sopass, 0, sizeof(wol->sopass)); | |
1342 | } | |
1343 | static int gelic_net_set_wol(struct net_device *netdev, | |
1344 | struct ethtool_wolinfo *wol) | |
1345 | { | |
1346 | int status; | |
1347 | struct gelic_card *card; | |
1348 | u64 v1, v2; | |
1349 | ||
1350 | if (ps3_compare_firmware_version(2, 2, 0) < 0 || | |
1351 | !capable(CAP_NET_ADMIN)) | |
1352 | return -EPERM; | |
1353 | ||
1354 | if (wol->wolopts & ~WAKE_MAGIC) | |
1355 | return -EINVAL; | |
1356 | ||
1357 | card = netdev_card(netdev); | |
1358 | if (wol->wolopts & WAKE_MAGIC) { | |
1359 | status = lv1_net_control(bus_id(card), dev_id(card), | |
1360 | GELIC_LV1_SET_WOL, | |
1361 | GELIC_LV1_WOL_MAGIC_PACKET, | |
1362 | 0, GELIC_LV1_WOL_MP_ENABLE, | |
1363 | &v1, &v2); | |
1364 | if (status) { | |
1365 | pr_info("%s: enabling WOL failed %d\n", __func__, | |
1366 | status); | |
1367 | status = -EIO; | |
1368 | goto done; | |
1369 | } | |
1370 | status = lv1_net_control(bus_id(card), dev_id(card), | |
1371 | GELIC_LV1_SET_WOL, | |
1372 | GELIC_LV1_WOL_ADD_MATCH_ADDR, | |
1373 | 0, GELIC_LV1_WOL_MATCH_ALL, | |
1374 | &v1, &v2); | |
1375 | if (!status) | |
1376 | ps3_sys_manager_set_wol(1); | |
1377 | else { | |
1378 | pr_info("%s: enabling WOL filter failed %d\n", | |
1379 | __func__, status); | |
1380 | status = -EIO; | |
1381 | } | |
1382 | } else { | |
1383 | status = lv1_net_control(bus_id(card), dev_id(card), | |
1384 | GELIC_LV1_SET_WOL, | |
1385 | GELIC_LV1_WOL_MAGIC_PACKET, | |
1386 | 0, GELIC_LV1_WOL_MP_DISABLE, | |
1387 | &v1, &v2); | |
1388 | if (status) { | |
1389 | pr_info("%s: disabling WOL failed %d\n", __func__, | |
1390 | status); | |
1391 | status = -EIO; | |
1392 | goto done; | |
1393 | } | |
1394 | status = lv1_net_control(bus_id(card), dev_id(card), | |
1395 | GELIC_LV1_SET_WOL, | |
1396 | GELIC_LV1_WOL_DELETE_MATCH_ADDR, | |
1397 | 0, GELIC_LV1_WOL_MATCH_ALL, | |
1398 | &v1, &v2); | |
1399 | if (!status) | |
1400 | ps3_sys_manager_set_wol(0); | |
1401 | else { | |
1402 | pr_info("%s: removing WOL filter failed %d\n", | |
1403 | __func__, status); | |
1404 | status = -EIO; | |
1405 | } | |
1406 | } | |
1407 | done: | |
1408 | return status; | |
1409 | } | |
1410 | ||
0fc0b732 | 1411 | static const struct ethtool_ops gelic_ether_ethtool_ops = { |
02c18891 | 1412 | .get_drvinfo = gelic_net_get_drvinfo, |
59e97327 | 1413 | .get_settings = gelic_ether_get_settings, |
55873ed8 | 1414 | .set_settings = gelic_ether_set_settings, |
7bc56b92 | 1415 | .get_link = ethtool_op_get_link, |
7bc56b92 MM |
1416 | .get_tx_csum = ethtool_op_get_tx_csum, |
1417 | .set_tx_csum = ethtool_op_set_tx_csum, | |
02c18891 MM |
1418 | .get_rx_csum = gelic_net_get_rx_csum, |
1419 | .set_rx_csum = gelic_net_set_rx_csum, | |
3faac215 MM |
1420 | .get_wol = gelic_net_get_wol, |
1421 | .set_wol = gelic_net_set_wol, | |
02c18891 | 1422 | }; |
02c18891 MM |
1423 | |
1424 | /** | |
1425 | * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout | |
1426 | * function (to be called not under interrupt status) | |
1427 | * @work: work is context of tx timout task | |
1428 | * | |
1429 | * called as task when tx hangs, resets interface (if interface is up) | |
1430 | */ | |
1431 | static void gelic_net_tx_timeout_task(struct work_struct *work) | |
1432 | { | |
59e97327 MM |
1433 | struct gelic_card *card = |
1434 | container_of(work, struct gelic_card, tx_timeout_task); | |
d4d7f1f9 | 1435 | struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0]; |
02c18891 MM |
1436 | |
1437 | dev_info(ctodev(card), "%s:Timed out. Restarting... \n", __func__); | |
1438 | ||
1439 | if (!(netdev->flags & IFF_UP)) | |
1440 | goto out; | |
1441 | ||
1442 | netif_device_detach(netdev); | |
1443 | gelic_net_stop(netdev); | |
1444 | ||
1445 | gelic_net_open(netdev); | |
1446 | netif_device_attach(netdev); | |
1447 | ||
1448 | out: | |
1449 | atomic_dec(&card->tx_timeout_task_counter); | |
1450 | } | |
1451 | ||
1452 | /** | |
1453 | * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in. | |
1454 | * @netdev: interface device structure | |
1455 | * | |
1456 | * called, if tx hangs. Schedules a task that resets the interface | |
1457 | */ | |
589866f9 | 1458 | void gelic_net_tx_timeout(struct net_device *netdev) |
02c18891 | 1459 | { |
59e97327 | 1460 | struct gelic_card *card; |
02c18891 | 1461 | |
589866f9 | 1462 | card = netdev_card(netdev); |
02c18891 MM |
1463 | atomic_inc(&card->tx_timeout_task_counter); |
1464 | if (netdev->flags & IFF_UP) | |
1465 | schedule_work(&card->tx_timeout_task); | |
1466 | else | |
1467 | atomic_dec(&card->tx_timeout_task_counter); | |
1468 | } | |
1469 | ||
5384e836 MM |
1470 | static const struct net_device_ops gelic_netdevice_ops = { |
1471 | .ndo_open = gelic_net_open, | |
1472 | .ndo_stop = gelic_net_stop, | |
1473 | .ndo_start_xmit = gelic_net_xmit, | |
1474 | .ndo_set_multicast_list = gelic_net_set_multi, | |
1475 | .ndo_change_mtu = gelic_net_change_mtu, | |
1476 | .ndo_tx_timeout = gelic_net_tx_timeout, | |
240c102d | 1477 | .ndo_set_mac_address = eth_mac_addr, |
5384e836 MM |
1478 | .ndo_validate_addr = eth_validate_addr, |
1479 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
1480 | .ndo_poll_controller = gelic_net_poll_controller, | |
1481 | #endif | |
1482 | }; | |
1483 | ||
02c18891 | 1484 | /** |
59e97327 | 1485 | * gelic_ether_setup_netdev_ops - initialization of net_device operations |
02c18891 MM |
1486 | * @netdev: net_device structure |
1487 | * | |
1488 | * fills out function pointers in the net_device structure | |
1489 | */ | |
48dce82c GU |
1490 | static void __devinit gelic_ether_setup_netdev_ops(struct net_device *netdev, |
1491 | struct napi_struct *napi) | |
02c18891 | 1492 | { |
02c18891 | 1493 | netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT; |
589866f9 MM |
1494 | /* NAPI */ |
1495 | netif_napi_add(netdev, napi, | |
1496 | gelic_net_poll, GELIC_NET_NAPI_WEIGHT); | |
1497 | netdev->ethtool_ops = &gelic_ether_ethtool_ops; | |
5384e836 | 1498 | netdev->netdev_ops = &gelic_netdevice_ops; |
02c18891 MM |
1499 | } |
1500 | ||
1501 | /** | |
589866f9 MM |
1502 | * gelic_ether_setup_netdev - initialization of net_device |
1503 | * @netdev: net_device structure | |
02c18891 MM |
1504 | * @card: card structure |
1505 | * | |
1506 | * Returns 0 on success or <0 on failure | |
1507 | * | |
589866f9 MM |
1508 | * gelic_ether_setup_netdev initializes the net_device structure |
1509 | * and register it. | |
02c18891 | 1510 | **/ |
48dce82c GU |
1511 | int __devinit gelic_net_setup_netdev(struct net_device *netdev, |
1512 | struct gelic_card *card) | |
02c18891 | 1513 | { |
02c18891 MM |
1514 | int status; |
1515 | u64 v1, v2; | |
1516 | ||
02c18891 MM |
1517 | netdev->features = NETIF_F_IP_CSUM; |
1518 | ||
1519 | status = lv1_net_control(bus_id(card), dev_id(card), | |
59e97327 | 1520 | GELIC_LV1_GET_MAC_ADDRESS, |
02c18891 | 1521 | 0, 0, 0, &v1, &v2); |
589866f9 | 1522 | v1 <<= 16; |
02c18891 MM |
1523 | if (status || !is_valid_ether_addr((u8 *)&v1)) { |
1524 | dev_info(ctodev(card), | |
1525 | "%s:lv1_net_control GET_MAC_ADDR failed %d\n", | |
1526 | __func__, status); | |
1527 | return -EINVAL; | |
1528 | } | |
589866f9 | 1529 | memcpy(netdev->dev_addr, &v1, ETH_ALEN); |
173261ed | 1530 | |
589866f9 | 1531 | if (card->vlan_required) { |
173261ed | 1532 | netdev->hard_header_len += VLAN_HLEN; |
589866f9 MM |
1533 | /* |
1534 | * As vlan is internally used, | |
1535 | * we can not receive vlan packets | |
1536 | */ | |
1537 | netdev->features |= NETIF_F_VLAN_CHALLENGED; | |
173261ed | 1538 | } |
02c18891 MM |
1539 | |
1540 | status = register_netdev(netdev); | |
1541 | if (status) { | |
589866f9 MM |
1542 | dev_err(ctodev(card), "%s:Couldn't register %s %d\n", |
1543 | __func__, netdev->name, status); | |
02c18891 MM |
1544 | return status; |
1545 | } | |
e174961c JB |
1546 | dev_info(ctodev(card), "%s: MAC addr %pM\n", |
1547 | netdev->name, netdev->dev_addr); | |
02c18891 MM |
1548 | |
1549 | return 0; | |
1550 | } | |
1551 | ||
1552 | /** | |
59e97327 | 1553 | * gelic_alloc_card_net - allocates net_device and card structure |
02c18891 MM |
1554 | * |
1555 | * returns the card structure or NULL in case of errors | |
1556 | * | |
1557 | * the card and net_device structures are linked to each other | |
1558 | */ | |
589866f9 | 1559 | #define GELIC_ALIGN (32) |
48dce82c | 1560 | static struct gelic_card * __devinit gelic_alloc_card_net(struct net_device **netdev) |
02c18891 | 1561 | { |
59e97327 | 1562 | struct gelic_card *card; |
589866f9 MM |
1563 | struct gelic_port *port; |
1564 | void *p; | |
02c18891 | 1565 | size_t alloc_size; |
02c18891 | 1566 | /* |
589866f9 MM |
1567 | * gelic requires dma descriptor is 32 bytes aligned and |
1568 | * the hypervisor requires irq_status is 8 bytes aligned. | |
02c18891 | 1569 | */ |
59e97327 MM |
1570 | BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8); |
1571 | BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32); | |
589866f9 MM |
1572 | alloc_size = |
1573 | sizeof(struct gelic_card) + | |
1574 | sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS + | |
1575 | sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS + | |
1576 | GELIC_ALIGN - 1; | |
02c18891 | 1577 | |
589866f9 MM |
1578 | p = kzalloc(alloc_size, GFP_KERNEL); |
1579 | if (!p) | |
02c18891 | 1580 | return NULL; |
589866f9 MM |
1581 | card = PTR_ALIGN(p, GELIC_ALIGN); |
1582 | card->unalign = p; | |
1583 | ||
1584 | /* | |
1585 | * alloc netdev | |
1586 | */ | |
1587 | *netdev = alloc_etherdev(sizeof(struct gelic_port)); | |
1588 | if (!netdev) { | |
1589 | kfree(card->unalign); | |
1590 | return NULL; | |
1591 | } | |
1592 | port = netdev_priv(*netdev); | |
1593 | ||
1594 | /* gelic_port */ | |
1595 | port->netdev = *netdev; | |
1596 | port->card = card; | |
d4d7f1f9 | 1597 | port->type = GELIC_PORT_ETHERNET_0; |
589866f9 MM |
1598 | |
1599 | /* gelic_card */ | |
d4d7f1f9 | 1600 | card->netdev[GELIC_PORT_ETHERNET_0] = *netdev; |
02c18891 | 1601 | |
02c18891 MM |
1602 | INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task); |
1603 | init_waitqueue_head(&card->waitq); | |
1604 | atomic_set(&card->tx_timeout_task_counter, 0); | |
2914f3ef | 1605 | mutex_init(&card->updown_lock); |
589866f9 | 1606 | atomic_set(&card->users, 0); |
02c18891 MM |
1607 | |
1608 | return card; | |
1609 | } | |
1610 | ||
48dce82c | 1611 | static void __devinit gelic_card_get_vlan_info(struct gelic_card *card) |
589866f9 MM |
1612 | { |
1613 | u64 v1, v2; | |
1614 | int status; | |
1615 | unsigned int i; | |
1616 | struct { | |
1617 | int tx; | |
1618 | int rx; | |
1619 | } vlan_id_ix[2] = { | |
d4d7f1f9 GL |
1620 | [GELIC_PORT_ETHERNET_0] = { |
1621 | .tx = GELIC_LV1_VLAN_TX_ETHERNET_0, | |
1622 | .rx = GELIC_LV1_VLAN_RX_ETHERNET_0 | |
589866f9 MM |
1623 | }, |
1624 | [GELIC_PORT_WIRELESS] = { | |
1625 | .tx = GELIC_LV1_VLAN_TX_WIRELESS, | |
1626 | .rx = GELIC_LV1_VLAN_RX_WIRELESS | |
1627 | } | |
1628 | }; | |
1629 | ||
1630 | for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) { | |
1631 | /* tx tag */ | |
1632 | status = lv1_net_control(bus_id(card), dev_id(card), | |
1633 | GELIC_LV1_GET_VLAN_ID, | |
1634 | vlan_id_ix[i].tx, | |
1635 | 0, 0, &v1, &v2); | |
1636 | if (status || !v1) { | |
1637 | if (status != LV1_NO_ENTRY) | |
1638 | dev_dbg(ctodev(card), | |
1639 | "get vlan id for tx(%d) failed(%d)\n", | |
1640 | vlan_id_ix[i].tx, status); | |
1641 | card->vlan[i].tx = 0; | |
1642 | card->vlan[i].rx = 0; | |
1643 | continue; | |
1644 | } | |
1645 | card->vlan[i].tx = (u16)v1; | |
1646 | ||
1647 | /* rx tag */ | |
1648 | status = lv1_net_control(bus_id(card), dev_id(card), | |
1649 | GELIC_LV1_GET_VLAN_ID, | |
1650 | vlan_id_ix[i].rx, | |
1651 | 0, 0, &v1, &v2); | |
1652 | if (status || !v1) { | |
1653 | if (status != LV1_NO_ENTRY) | |
1654 | dev_info(ctodev(card), | |
1655 | "get vlan id for rx(%d) failed(%d)\n", | |
1656 | vlan_id_ix[i].rx, status); | |
1657 | card->vlan[i].tx = 0; | |
1658 | card->vlan[i].rx = 0; | |
1659 | continue; | |
1660 | } | |
1661 | card->vlan[i].rx = (u16)v1; | |
1662 | ||
1663 | dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n", | |
1664 | i, card->vlan[i].tx, card->vlan[i].rx); | |
1665 | } | |
1666 | ||
d4d7f1f9 | 1667 | if (card->vlan[GELIC_PORT_ETHERNET_0].tx) { |
589866f9 MM |
1668 | BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx); |
1669 | card->vlan_required = 1; | |
1670 | } else | |
1671 | card->vlan_required = 0; | |
1672 | ||
1673 | /* check wirelss capable firmware */ | |
1674 | if (ps3_compare_firmware_version(1, 6, 0) < 0) { | |
1675 | card->vlan[GELIC_PORT_WIRELESS].tx = 0; | |
1676 | card->vlan[GELIC_PORT_WIRELESS].rx = 0; | |
1677 | } | |
1678 | ||
1679 | dev_info(ctodev(card), "internal vlan %s\n", | |
1680 | card->vlan_required? "enabled" : "disabled"); | |
1681 | } | |
02c18891 MM |
1682 | /** |
1683 | * ps3_gelic_driver_probe - add a device to the control of this driver | |
1684 | */ | |
48dce82c | 1685 | static int __devinit ps3_gelic_driver_probe(struct ps3_system_bus_device *dev) |
02c18891 | 1686 | { |
589866f9 MM |
1687 | struct gelic_card *card; |
1688 | struct net_device *netdev; | |
02c18891 MM |
1689 | int result; |
1690 | ||
589866f9 | 1691 | pr_debug("%s: called\n", __func__); |
02c18891 MM |
1692 | result = ps3_open_hv_device(dev); |
1693 | ||
1694 | if (result) { | |
589866f9 MM |
1695 | dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n", |
1696 | __func__); | |
02c18891 MM |
1697 | goto fail_open; |
1698 | } | |
1699 | ||
1700 | result = ps3_dma_region_create(dev->d_region); | |
1701 | ||
1702 | if (result) { | |
589866f9 MM |
1703 | dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n", |
1704 | __func__, result); | |
02c18891 MM |
1705 | BUG_ON("check region type"); |
1706 | goto fail_dma_region; | |
1707 | } | |
1708 | ||
589866f9 MM |
1709 | /* alloc card/netdevice */ |
1710 | card = gelic_alloc_card_net(&netdev); | |
1711 | if (!card) { | |
1712 | dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n", | |
1713 | __func__); | |
1714 | result = -ENOMEM; | |
1715 | goto fail_alloc_card; | |
1716 | } | |
03fa68c2 | 1717 | ps3_system_bus_set_drvdata(dev, card); |
589866f9 MM |
1718 | card->dev = dev; |
1719 | ||
1720 | /* get internal vlan info */ | |
1721 | gelic_card_get_vlan_info(card); | |
1722 | ||
55873ed8 HS |
1723 | card->link_mode = GELIC_LV1_ETHER_AUTO_NEG; |
1724 | ||
589866f9 | 1725 | /* setup interrupt */ |
02c18891 MM |
1726 | result = lv1_net_set_interrupt_status_indicator(bus_id(card), |
1727 | dev_id(card), | |
1728 | ps3_mm_phys_to_lpar(__pa(&card->irq_status)), | |
1729 | 0); | |
1730 | ||
1731 | if (result) { | |
1732 | dev_dbg(&dev->core, | |
589866f9 MM |
1733 | "%s:set_interrupt_status_indicator failed: %s\n", |
1734 | __func__, ps3_result(result)); | |
02c18891 MM |
1735 | result = -EIO; |
1736 | goto fail_status_indicator; | |
1737 | } | |
1738 | ||
589866f9 MM |
1739 | result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY, |
1740 | &card->irq); | |
1741 | ||
1742 | if (result) { | |
1743 | dev_info(ctodev(card), | |
1744 | "%s:gelic_net_open_device failed (%d)\n", | |
1745 | __func__, result); | |
1746 | result = -EPERM; | |
1747 | goto fail_alloc_irq; | |
1748 | } | |
1749 | result = request_irq(card->irq, gelic_card_interrupt, | |
1750 | IRQF_DISABLED, netdev->name, card); | |
1751 | ||
1752 | if (result) { | |
1753 | dev_info(ctodev(card), "%s:request_irq failed (%d)\n", | |
1754 | __func__, result); | |
1755 | goto fail_request_irq; | |
1756 | } | |
1757 | ||
1758 | /* setup card structure */ | |
1759 | card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT | | |
1760 | GELIC_CARD_PORT_STATUS_CHANGED; | |
1761 | card->rx_csum = GELIC_CARD_RX_CSUM_DEFAULT; | |
1762 | ||
1763 | ||
1764 | if (gelic_card_init_chain(card, &card->tx_chain, | |
1765 | card->descr, GELIC_NET_TX_DESCRIPTORS)) | |
1766 | goto fail_alloc_tx; | |
1767 | if (gelic_card_init_chain(card, &card->rx_chain, | |
1768 | card->descr + GELIC_NET_TX_DESCRIPTORS, | |
1769 | GELIC_NET_RX_DESCRIPTORS)) | |
1770 | goto fail_alloc_rx; | |
1771 | ||
1772 | /* head of chain */ | |
1773 | card->tx_top = card->tx_chain.head; | |
1774 | card->rx_top = card->rx_chain.head; | |
1775 | dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n", | |
1776 | card->rx_top, card->tx_top, sizeof(struct gelic_descr), | |
1777 | GELIC_NET_RX_DESCRIPTORS); | |
1778 | /* allocate rx skbs */ | |
1779 | if (gelic_card_alloc_rx_skbs(card)) | |
1780 | goto fail_alloc_skbs; | |
1781 | ||
1782 | spin_lock_init(&card->tx_lock); | |
1783 | card->tx_dma_progress = 0; | |
02c18891 | 1784 | |
589866f9 MM |
1785 | /* setup net_device structure */ |
1786 | netdev->irq = card->irq; | |
1787 | SET_NETDEV_DEV(netdev, &card->dev->core); | |
1788 | gelic_ether_setup_netdev_ops(netdev, &card->napi); | |
1789 | result = gelic_net_setup_netdev(netdev, card); | |
02c18891 | 1790 | if (result) { |
589866f9 MM |
1791 | dev_dbg(&dev->core, "%s: setup_netdev failed %d", |
1792 | __func__, result); | |
02c18891 MM |
1793 | goto fail_setup_netdev; |
1794 | } | |
1795 | ||
09dde54c MM |
1796 | #ifdef CONFIG_GELIC_WIRELESS |
1797 | if (gelic_wl_driver_probe(card)) { | |
1798 | dev_dbg(&dev->core, "%s: WL init failed\n", __func__); | |
1799 | goto fail_setup_netdev; | |
1800 | } | |
1801 | #endif | |
589866f9 | 1802 | pr_debug("%s: done\n", __func__); |
02c18891 MM |
1803 | return 0; |
1804 | ||
1805 | fail_setup_netdev: | |
589866f9 MM |
1806 | fail_alloc_skbs: |
1807 | gelic_card_free_chain(card, card->rx_chain.head); | |
1808 | fail_alloc_rx: | |
1809 | gelic_card_free_chain(card, card->tx_chain.head); | |
1810 | fail_alloc_tx: | |
1811 | free_irq(card->irq, card); | |
1812 | netdev->irq = NO_IRQ; | |
1813 | fail_request_irq: | |
1814 | ps3_sb_event_receive_port_destroy(dev, card->irq); | |
1815 | fail_alloc_irq: | |
02c18891 | 1816 | lv1_net_set_interrupt_status_indicator(bus_id(card), |
589866f9 MM |
1817 | bus_id(card), |
1818 | 0, 0); | |
02c18891 | 1819 | fail_status_indicator: |
03fa68c2 | 1820 | ps3_system_bus_set_drvdata(dev, NULL); |
589866f9 MM |
1821 | kfree(netdev_card(netdev)->unalign); |
1822 | free_netdev(netdev); | |
1823 | fail_alloc_card: | |
02c18891 MM |
1824 | ps3_dma_region_free(dev->d_region); |
1825 | fail_dma_region: | |
1826 | ps3_close_hv_device(dev); | |
1827 | fail_open: | |
02c18891 MM |
1828 | return result; |
1829 | } | |
1830 | ||
1831 | /** | |
1832 | * ps3_gelic_driver_remove - remove a device from the control of this driver | |
1833 | */ | |
1834 | ||
59e97327 | 1835 | static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev) |
02c18891 | 1836 | { |
03fa68c2 | 1837 | struct gelic_card *card = ps3_system_bus_get_drvdata(dev); |
589866f9 MM |
1838 | struct net_device *netdev0; |
1839 | pr_debug("%s: called\n", __func__); | |
1840 | ||
55873ed8 HS |
1841 | /* set auto-negotiation */ |
1842 | gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG); | |
1843 | ||
09dde54c MM |
1844 | #ifdef CONFIG_GELIC_WIRELESS |
1845 | gelic_wl_driver_remove(card); | |
1846 | #endif | |
589866f9 MM |
1847 | /* stop interrupt */ |
1848 | gelic_card_set_irq_mask(card, 0); | |
1849 | ||
1850 | /* turn off DMA, force end */ | |
1851 | gelic_card_disable_rxdmac(card); | |
1852 | gelic_card_disable_txdmac(card); | |
1853 | ||
1854 | /* release chains */ | |
1855 | gelic_card_release_tx_chain(card, 1); | |
1856 | gelic_card_release_rx_chain(card); | |
1857 | ||
1858 | gelic_card_free_chain(card, card->tx_top); | |
1859 | gelic_card_free_chain(card, card->rx_top); | |
1860 | ||
d4d7f1f9 | 1861 | netdev0 = card->netdev[GELIC_PORT_ETHERNET_0]; |
589866f9 MM |
1862 | /* disconnect event port */ |
1863 | free_irq(card->irq, card); | |
1864 | netdev0->irq = NO_IRQ; | |
1865 | ps3_sb_event_receive_port_destroy(card->dev, card->irq); | |
02c18891 MM |
1866 | |
1867 | wait_event(card->waitq, | |
1868 | atomic_read(&card->tx_timeout_task_counter) == 0); | |
1869 | ||
1870 | lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card), | |
1871 | 0 , 0); | |
1872 | ||
589866f9 MM |
1873 | unregister_netdev(netdev0); |
1874 | kfree(netdev_card(netdev0)->unalign); | |
1875 | free_netdev(netdev0); | |
02c18891 | 1876 | |
03fa68c2 | 1877 | ps3_system_bus_set_drvdata(dev, NULL); |
02c18891 MM |
1878 | |
1879 | ps3_dma_region_free(dev->d_region); | |
1880 | ||
1881 | ps3_close_hv_device(dev); | |
1882 | ||
589866f9 | 1883 | pr_debug("%s: done\n", __func__); |
02c18891 MM |
1884 | return 0; |
1885 | } | |
1886 | ||
1887 | static struct ps3_system_bus_driver ps3_gelic_driver = { | |
1888 | .match_id = PS3_MATCH_ID_GELIC, | |
1889 | .probe = ps3_gelic_driver_probe, | |
1890 | .remove = ps3_gelic_driver_remove, | |
1891 | .shutdown = ps3_gelic_driver_remove, | |
1892 | .core.name = "ps3_gelic_driver", | |
1893 | .core.owner = THIS_MODULE, | |
1894 | }; | |
1895 | ||
1896 | static int __init ps3_gelic_driver_init (void) | |
1897 | { | |
1898 | return firmware_has_feature(FW_FEATURE_PS3_LV1) | |
1899 | ? ps3_system_bus_driver_register(&ps3_gelic_driver) | |
1900 | : -ENODEV; | |
1901 | } | |
1902 | ||
1903 | static void __exit ps3_gelic_driver_exit (void) | |
1904 | { | |
1905 | ps3_system_bus_driver_unregister(&ps3_gelic_driver); | |
1906 | } | |
1907 | ||
59e97327 MM |
1908 | module_init(ps3_gelic_driver_init); |
1909 | module_exit(ps3_gelic_driver_exit); | |
02c18891 MM |
1910 | |
1911 | MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC); | |
1912 |