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" | |
49 | ||
50 | #define DRV_NAME "Gelic Network Driver" | |
51 | #define DRV_VERSION "1.0" | |
52 | ||
53 | MODULE_AUTHOR("SCE Inc."); | |
54 | MODULE_DESCRIPTION("Gelic Network driver"); | |
55 | MODULE_LICENSE("GPL"); | |
56 | ||
57 | static inline struct device *ctodev(struct gelic_net_card *card) | |
58 | { | |
59 | return &card->dev->core; | |
60 | } | |
034e0ab5 | 61 | static inline u64 bus_id(struct gelic_net_card *card) |
02c18891 MM |
62 | { |
63 | return card->dev->bus_id; | |
64 | } | |
034e0ab5 | 65 | static inline u64 dev_id(struct gelic_net_card *card) |
02c18891 MM |
66 | { |
67 | return card->dev->dev_id; | |
68 | } | |
69 | ||
70 | /* set irq_mask */ | |
71 | static int gelic_net_set_irq_mask(struct gelic_net_card *card, u64 mask) | |
72 | { | |
73 | int status; | |
74 | ||
75 | status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card), | |
76 | mask, 0); | |
77 | if (status) | |
78 | dev_info(ctodev(card), | |
79 | "lv1_net_set_interrupt_mask failed %d\n", status); | |
80 | return status; | |
81 | } | |
82 | static inline void gelic_net_rx_irq_on(struct gelic_net_card *card) | |
83 | { | |
84 | gelic_net_set_irq_mask(card, card->ghiintmask | GELIC_NET_RXINT); | |
85 | } | |
86 | static inline void gelic_net_rx_irq_off(struct gelic_net_card *card) | |
87 | { | |
88 | gelic_net_set_irq_mask(card, card->ghiintmask & ~GELIC_NET_RXINT); | |
89 | } | |
90 | /** | |
91 | * gelic_net_get_descr_status -- returns the status of a descriptor | |
92 | * @descr: descriptor to look at | |
93 | * | |
94 | * returns the status as in the dmac_cmd_status field of the descriptor | |
95 | */ | |
96 | static enum gelic_net_descr_status | |
97 | gelic_net_get_descr_status(struct gelic_net_descr *descr) | |
98 | { | |
99 | u32 cmd_status; | |
100 | ||
101 | cmd_status = descr->dmac_cmd_status; | |
102 | cmd_status >>= GELIC_NET_DESCR_IND_PROC_SHIFT; | |
103 | return cmd_status; | |
104 | } | |
105 | ||
106 | /** | |
107 | * gelic_net_set_descr_status -- sets the status of a descriptor | |
108 | * @descr: descriptor to change | |
109 | * @status: status to set in the descriptor | |
110 | * | |
111 | * changes the status to the specified value. Doesn't change other bits | |
112 | * in the status | |
113 | */ | |
114 | static void gelic_net_set_descr_status(struct gelic_net_descr *descr, | |
115 | enum gelic_net_descr_status status) | |
116 | { | |
117 | u32 cmd_status; | |
118 | ||
119 | /* read the status */ | |
120 | cmd_status = descr->dmac_cmd_status; | |
121 | /* clean the upper 4 bits */ | |
122 | cmd_status &= GELIC_NET_DESCR_IND_PROC_MASKO; | |
123 | /* add the status to it */ | |
124 | cmd_status |= ((u32)status) << GELIC_NET_DESCR_IND_PROC_SHIFT; | |
125 | /* and write it back */ | |
126 | descr->dmac_cmd_status = cmd_status; | |
127 | /* | |
128 | * dma_cmd_status field is used to indicate whether the descriptor | |
129 | * is valid or not. | |
130 | * Usually caller of this function wants to inform that to the | |
131 | * hardware, so we assure here the hardware sees the change. | |
132 | */ | |
133 | wmb(); | |
134 | } | |
135 | ||
136 | /** | |
137 | * gelic_net_free_chain - free descriptor chain | |
138 | * @card: card structure | |
139 | * @descr_in: address of desc | |
140 | */ | |
141 | static void gelic_net_free_chain(struct gelic_net_card *card, | |
142 | struct gelic_net_descr *descr_in) | |
143 | { | |
144 | struct gelic_net_descr *descr; | |
145 | ||
146 | for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) { | |
147 | dma_unmap_single(ctodev(card), descr->bus_addr, | |
148 | GELIC_NET_DESCR_SIZE, DMA_BIDIRECTIONAL); | |
149 | descr->bus_addr = 0; | |
150 | } | |
151 | } | |
152 | ||
153 | /** | |
154 | * gelic_net_init_chain - links descriptor chain | |
155 | * @card: card structure | |
156 | * @chain: address of chain | |
157 | * @start_descr: address of descriptor array | |
158 | * @no: number of descriptors | |
159 | * | |
160 | * we manage a circular list that mirrors the hardware structure, | |
161 | * except that the hardware uses bus addresses. | |
162 | * | |
163 | * returns 0 on success, <0 on failure | |
164 | */ | |
165 | static int gelic_net_init_chain(struct gelic_net_card *card, | |
166 | struct gelic_net_descr_chain *chain, | |
167 | struct gelic_net_descr *start_descr, int no) | |
168 | { | |
169 | int i; | |
170 | struct gelic_net_descr *descr; | |
171 | ||
172 | descr = start_descr; | |
173 | memset(descr, 0, sizeof(*descr) * no); | |
174 | ||
175 | /* set up the hardware pointers in each descriptor */ | |
176 | for (i = 0; i < no; i++, descr++) { | |
177 | gelic_net_set_descr_status(descr, GELIC_NET_DESCR_NOT_IN_USE); | |
178 | descr->bus_addr = | |
179 | dma_map_single(ctodev(card), descr, | |
180 | GELIC_NET_DESCR_SIZE, | |
181 | DMA_BIDIRECTIONAL); | |
182 | ||
183 | if (!descr->bus_addr) | |
184 | goto iommu_error; | |
185 | ||
186 | descr->next = descr + 1; | |
187 | descr->prev = descr - 1; | |
188 | } | |
189 | /* make them as ring */ | |
190 | (descr - 1)->next = start_descr; | |
191 | start_descr->prev = (descr - 1); | |
192 | ||
193 | /* chain bus addr of hw descriptor */ | |
194 | descr = start_descr; | |
195 | for (i = 0; i < no; i++, descr++) { | |
196 | descr->next_descr_addr = descr->next->bus_addr; | |
197 | } | |
198 | ||
199 | chain->head = start_descr; | |
200 | chain->tail = start_descr; | |
201 | ||
202 | /* do not chain last hw descriptor */ | |
203 | (descr - 1)->next_descr_addr = 0; | |
204 | ||
205 | return 0; | |
206 | ||
207 | iommu_error: | |
208 | for (i--, descr--; 0 <= i; i--, descr--) | |
209 | if (descr->bus_addr) | |
210 | dma_unmap_single(ctodev(card), descr->bus_addr, | |
211 | GELIC_NET_DESCR_SIZE, | |
212 | DMA_BIDIRECTIONAL); | |
213 | return -ENOMEM; | |
214 | } | |
215 | ||
216 | /** | |
217 | * gelic_net_prepare_rx_descr - reinitializes a rx descriptor | |
218 | * @card: card structure | |
219 | * @descr: descriptor to re-init | |
220 | * | |
221 | * return 0 on succes, <0 on failure | |
222 | * | |
223 | * allocates a new rx skb, iommu-maps it and attaches it to the descriptor. | |
224 | * Activate the descriptor state-wise | |
225 | */ | |
226 | static int gelic_net_prepare_rx_descr(struct gelic_net_card *card, | |
227 | struct gelic_net_descr *descr) | |
228 | { | |
229 | int offset; | |
230 | unsigned int bufsize; | |
231 | ||
232 | if (gelic_net_get_descr_status(descr) != GELIC_NET_DESCR_NOT_IN_USE) { | |
233 | dev_info(ctodev(card), "%s: ERROR status \n", __func__); | |
234 | } | |
235 | /* we need to round up the buffer size to a multiple of 128 */ | |
236 | bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN); | |
237 | ||
238 | /* and we need to have it 128 byte aligned, therefore we allocate a | |
239 | * bit more */ | |
240 | descr->skb = netdev_alloc_skb(card->netdev, | |
241 | bufsize + GELIC_NET_RXBUF_ALIGN - 1); | |
242 | if (!descr->skb) { | |
243 | descr->buf_addr = 0; /* tell DMAC don't touch memory */ | |
244 | dev_info(ctodev(card), | |
245 | "%s:allocate skb failed !!\n", __func__); | |
246 | return -ENOMEM; | |
247 | } | |
248 | descr->buf_size = bufsize; | |
249 | descr->dmac_cmd_status = 0; | |
250 | descr->result_size = 0; | |
251 | descr->valid_size = 0; | |
252 | descr->data_error = 0; | |
253 | ||
254 | offset = ((unsigned long)descr->skb->data) & | |
255 | (GELIC_NET_RXBUF_ALIGN - 1); | |
256 | if (offset) | |
257 | skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset); | |
258 | /* io-mmu-map the skb */ | |
259 | descr->buf_addr = dma_map_single(ctodev(card), descr->skb->data, | |
260 | GELIC_NET_MAX_MTU, | |
261 | DMA_FROM_DEVICE); | |
262 | if (!descr->buf_addr) { | |
263 | dev_kfree_skb_any(descr->skb); | |
264 | descr->skb = NULL; | |
265 | dev_info(ctodev(card), | |
266 | "%s:Could not iommu-map rx buffer\n", __func__); | |
267 | gelic_net_set_descr_status(descr, GELIC_NET_DESCR_NOT_IN_USE); | |
268 | return -ENOMEM; | |
269 | } else { | |
270 | gelic_net_set_descr_status(descr, GELIC_NET_DESCR_CARDOWNED); | |
271 | return 0; | |
272 | } | |
273 | } | |
274 | ||
275 | /** | |
276 | * gelic_net_release_rx_chain - free all skb of rx descr | |
277 | * @card: card structure | |
278 | * | |
279 | */ | |
280 | static void gelic_net_release_rx_chain(struct gelic_net_card *card) | |
281 | { | |
282 | struct gelic_net_descr *descr = card->rx_chain.head; | |
283 | ||
284 | do { | |
285 | if (descr->skb) { | |
286 | dma_unmap_single(ctodev(card), | |
287 | descr->buf_addr, | |
288 | descr->skb->len, | |
289 | DMA_FROM_DEVICE); | |
290 | descr->buf_addr = 0; | |
291 | dev_kfree_skb_any(descr->skb); | |
292 | descr->skb = NULL; | |
ea6992aa MM |
293 | gelic_net_set_descr_status(descr, |
294 | GELIC_NET_DESCR_NOT_IN_USE); | |
02c18891 MM |
295 | } |
296 | descr = descr->next; | |
297 | } while (descr != card->rx_chain.head); | |
298 | } | |
299 | ||
300 | /** | |
301 | * gelic_net_fill_rx_chain - fills descriptors/skbs in the rx chains | |
302 | * @card: card structure | |
303 | * | |
304 | * fills all descriptors in the rx chain: allocates skbs | |
305 | * and iommu-maps them. | |
306 | * returns 0 on success, <0 on failure | |
307 | */ | |
308 | static int gelic_net_fill_rx_chain(struct gelic_net_card *card) | |
309 | { | |
310 | struct gelic_net_descr *descr = card->rx_chain.head; | |
311 | int ret; | |
312 | ||
313 | do { | |
314 | if (!descr->skb) { | |
315 | ret = gelic_net_prepare_rx_descr(card, descr); | |
316 | if (ret) | |
317 | goto rewind; | |
318 | } | |
319 | descr = descr->next; | |
320 | } while (descr != card->rx_chain.head); | |
321 | ||
322 | return 0; | |
323 | rewind: | |
324 | gelic_net_release_rx_chain(card); | |
325 | return ret; | |
326 | } | |
327 | ||
328 | /** | |
329 | * gelic_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains | |
330 | * @card: card structure | |
331 | * | |
332 | * returns 0 on success, <0 on failure | |
333 | */ | |
334 | static int gelic_net_alloc_rx_skbs(struct gelic_net_card *card) | |
335 | { | |
336 | struct gelic_net_descr_chain *chain; | |
337 | int ret; | |
338 | chain = &card->rx_chain; | |
339 | ret = gelic_net_fill_rx_chain(card); | |
340 | chain->head = card->rx_top->prev; /* point to the last */ | |
341 | return ret; | |
342 | } | |
343 | ||
344 | /** | |
345 | * gelic_net_release_tx_descr - processes a used tx descriptor | |
346 | * @card: card structure | |
347 | * @descr: descriptor to release | |
348 | * | |
349 | * releases a used tx descriptor (unmapping, freeing of skb) | |
350 | */ | |
351 | static void gelic_net_release_tx_descr(struct gelic_net_card *card, | |
352 | struct gelic_net_descr *descr) | |
353 | { | |
173261ed | 354 | struct sk_buff *skb = descr->skb; |
02c18891 | 355 | |
173261ed | 356 | BUG_ON(!(descr->data_status & (1 << GELIC_NET_TXDESC_TAIL))); |
02c18891 | 357 | |
173261ed MM |
358 | dma_unmap_single(ctodev(card), descr->buf_addr, skb->len, |
359 | DMA_TO_DEVICE); | |
360 | dev_kfree_skb_any(skb); | |
02c18891 MM |
361 | |
362 | descr->buf_addr = 0; | |
363 | descr->buf_size = 0; | |
364 | descr->next_descr_addr = 0; | |
365 | descr->result_size = 0; | |
366 | descr->valid_size = 0; | |
367 | descr->data_status = 0; | |
368 | descr->data_error = 0; | |
369 | descr->skb = NULL; | |
370 | ||
371 | /* set descr status */ | |
ea6992aa | 372 | gelic_net_set_descr_status(descr, GELIC_NET_DESCR_NOT_IN_USE); |
02c18891 MM |
373 | } |
374 | ||
375 | /** | |
376 | * gelic_net_release_tx_chain - processes sent tx descriptors | |
377 | * @card: adapter structure | |
378 | * @stop: net_stop sequence | |
379 | * | |
380 | * releases the tx descriptors that gelic has finished with | |
381 | */ | |
382 | static void gelic_net_release_tx_chain(struct gelic_net_card *card, int stop) | |
383 | { | |
384 | struct gelic_net_descr_chain *tx_chain; | |
385 | enum gelic_net_descr_status status; | |
386 | int release = 0; | |
387 | ||
388 | for (tx_chain = &card->tx_chain; | |
389 | tx_chain->head != tx_chain->tail && tx_chain->tail; | |
390 | tx_chain->tail = tx_chain->tail->next) { | |
391 | status = gelic_net_get_descr_status(tx_chain->tail); | |
392 | switch (status) { | |
393 | case GELIC_NET_DESCR_RESPONSE_ERROR: | |
394 | case GELIC_NET_DESCR_PROTECTION_ERROR: | |
395 | case GELIC_NET_DESCR_FORCE_END: | |
396 | if (printk_ratelimit()) | |
397 | dev_info(ctodev(card), | |
398 | "%s: forcing end of tx descriptor " \ | |
399 | "with status %x\n", | |
400 | __func__, status); | |
92548d60 | 401 | card->netdev->stats.tx_dropped++; |
02c18891 MM |
402 | break; |
403 | ||
404 | case GELIC_NET_DESCR_COMPLETE: | |
48544cc2 | 405 | if (tx_chain->tail->skb) { |
92548d60 MM |
406 | card->netdev->stats.tx_packets++; |
407 | card->netdev->stats.tx_bytes += | |
48544cc2 MM |
408 | tx_chain->tail->skb->len; |
409 | } | |
02c18891 MM |
410 | break; |
411 | ||
412 | case GELIC_NET_DESCR_CARDOWNED: | |
413 | /* pending tx request */ | |
414 | default: | |
415 | /* any other value (== GELIC_NET_DESCR_NOT_IN_USE) */ | |
48544cc2 MM |
416 | if (!stop) |
417 | goto out; | |
02c18891 MM |
418 | } |
419 | gelic_net_release_tx_descr(card, tx_chain->tail); | |
48544cc2 | 420 | release ++; |
02c18891 MM |
421 | } |
422 | out: | |
173261ed | 423 | if (!stop && release) |
02c18891 MM |
424 | netif_wake_queue(card->netdev); |
425 | } | |
426 | ||
427 | /** | |
428 | * gelic_net_set_multi - sets multicast addresses and promisc flags | |
429 | * @netdev: interface device structure | |
430 | * | |
431 | * gelic_net_set_multi configures multicast addresses as needed for the | |
432 | * netdev interface. It also sets up multicast, allmulti and promisc | |
433 | * flags appropriately | |
434 | */ | |
435 | static void gelic_net_set_multi(struct net_device *netdev) | |
436 | { | |
437 | struct gelic_net_card *card = netdev_priv(netdev); | |
438 | struct dev_mc_list *mc; | |
439 | unsigned int i; | |
440 | uint8_t *p; | |
441 | u64 addr; | |
442 | int status; | |
443 | ||
444 | /* clear all multicast address */ | |
445 | status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card), | |
446 | 0, 1); | |
447 | if (status) | |
448 | dev_err(ctodev(card), | |
449 | "lv1_net_remove_multicast_address failed %d\n", | |
450 | status); | |
451 | /* set broadcast address */ | |
452 | status = lv1_net_add_multicast_address(bus_id(card), dev_id(card), | |
453 | GELIC_NET_BROADCAST_ADDR, 0); | |
454 | if (status) | |
455 | dev_err(ctodev(card), | |
456 | "lv1_net_add_multicast_address failed, %d\n", | |
457 | status); | |
458 | ||
459 | if (netdev->flags & IFF_ALLMULTI | |
460 | || netdev->mc_count > GELIC_NET_MC_COUNT_MAX) { /* list max */ | |
461 | status = lv1_net_add_multicast_address(bus_id(card), | |
462 | dev_id(card), | |
463 | 0, 1); | |
464 | if (status) | |
465 | dev_err(ctodev(card), | |
466 | "lv1_net_add_multicast_address failed, %d\n", | |
467 | status); | |
468 | return; | |
469 | } | |
470 | ||
471 | /* set multicast address */ | |
472 | for (mc = netdev->mc_list; mc; mc = mc->next) { | |
473 | addr = 0; | |
474 | p = mc->dmi_addr; | |
475 | for (i = 0; i < ETH_ALEN; i++) { | |
476 | addr <<= 8; | |
477 | addr |= *p++; | |
478 | } | |
479 | status = lv1_net_add_multicast_address(bus_id(card), | |
480 | dev_id(card), | |
481 | addr, 0); | |
482 | if (status) | |
483 | dev_err(ctodev(card), | |
484 | "lv1_net_add_multicast_address failed, %d\n", | |
485 | status); | |
486 | } | |
487 | } | |
488 | ||
489 | /** | |
490 | * gelic_net_enable_rxdmac - enables the receive DMA controller | |
491 | * @card: card structure | |
492 | * | |
493 | * gelic_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN | |
494 | * in the GDADMACCNTR register | |
495 | */ | |
496 | static inline void gelic_net_enable_rxdmac(struct gelic_net_card *card) | |
497 | { | |
498 | int status; | |
499 | ||
500 | status = lv1_net_start_rx_dma(bus_id(card), dev_id(card), | |
501 | card->rx_chain.tail->bus_addr, 0); | |
502 | if (status) | |
503 | dev_info(ctodev(card), | |
504 | "lv1_net_start_rx_dma failed, status=%d\n", status); | |
505 | } | |
506 | ||
507 | /** | |
508 | * gelic_net_disable_rxdmac - disables the receive DMA controller | |
509 | * @card: card structure | |
510 | * | |
511 | * gelic_net_disable_rxdmac terminates processing on the DMA controller by | |
512 | * turing off DMA and issueing a force end | |
513 | */ | |
514 | static inline void gelic_net_disable_rxdmac(struct gelic_net_card *card) | |
515 | { | |
516 | int status; | |
517 | ||
518 | /* this hvc blocks until the DMA in progress really stopped */ | |
519 | status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0); | |
520 | if (status) | |
521 | dev_err(ctodev(card), | |
522 | "lv1_net_stop_rx_dma faild, %d\n", status); | |
523 | } | |
524 | ||
525 | /** | |
526 | * gelic_net_disable_txdmac - disables the transmit DMA controller | |
527 | * @card: card structure | |
528 | * | |
529 | * gelic_net_disable_txdmac terminates processing on the DMA controller by | |
530 | * turing off DMA and issueing a force end | |
531 | */ | |
532 | static inline void gelic_net_disable_txdmac(struct gelic_net_card *card) | |
533 | { | |
534 | int status; | |
535 | ||
536 | /* this hvc blocks until the DMA in progress really stopped */ | |
537 | status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0); | |
538 | if (status) | |
539 | dev_err(ctodev(card), | |
540 | "lv1_net_stop_tx_dma faild, status=%d\n", status); | |
541 | } | |
542 | ||
543 | /** | |
544 | * gelic_net_stop - called upon ifconfig down | |
545 | * @netdev: interface device structure | |
546 | * | |
547 | * always returns 0 | |
548 | */ | |
549 | static int gelic_net_stop(struct net_device *netdev) | |
550 | { | |
551 | struct gelic_net_card *card = netdev_priv(netdev); | |
552 | ||
bea3348e | 553 | napi_disable(&card->napi); |
02c18891 MM |
554 | netif_stop_queue(netdev); |
555 | ||
556 | /* turn off DMA, force end */ | |
557 | gelic_net_disable_rxdmac(card); | |
558 | gelic_net_disable_txdmac(card); | |
559 | ||
560 | gelic_net_set_irq_mask(card, 0); | |
561 | ||
562 | /* disconnect event port */ | |
563 | free_irq(card->netdev->irq, card->netdev); | |
564 | ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq); | |
565 | card->netdev->irq = NO_IRQ; | |
566 | ||
567 | netif_carrier_off(netdev); | |
568 | ||
569 | /* release chains */ | |
570 | gelic_net_release_tx_chain(card, 1); | |
571 | gelic_net_release_rx_chain(card); | |
572 | ||
573 | gelic_net_free_chain(card, card->tx_top); | |
574 | gelic_net_free_chain(card, card->rx_top); | |
575 | ||
576 | return 0; | |
577 | } | |
578 | ||
579 | /** | |
580 | * gelic_net_get_next_tx_descr - returns the next available tx descriptor | |
581 | * @card: device structure to get descriptor from | |
582 | * | |
583 | * returns the address of the next descriptor, or NULL if not available. | |
584 | */ | |
585 | static struct gelic_net_descr * | |
586 | gelic_net_get_next_tx_descr(struct gelic_net_card *card) | |
587 | { | |
588 | if (!card->tx_chain.head) | |
589 | return NULL; | |
173261ed | 590 | /* see if the next descriptor is free */ |
02c18891 MM |
591 | if (card->tx_chain.tail != card->tx_chain.head->next && |
592 | gelic_net_get_descr_status(card->tx_chain.head) == | |
173261ed | 593 | GELIC_NET_DESCR_NOT_IN_USE) |
02c18891 MM |
594 | return card->tx_chain.head; |
595 | else | |
596 | return NULL; | |
597 | ||
598 | } | |
599 | ||
600 | /** | |
601 | * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field | |
602 | * @descr: descriptor structure to fill out | |
603 | * @skb: packet to consider | |
02c18891 MM |
604 | * |
605 | * fills out the command and status field of the descriptor structure, | |
606 | * depending on hardware checksum settings. This function assumes a wmb() | |
607 | * has executed before. | |
608 | */ | |
609 | static void gelic_net_set_txdescr_cmdstat(struct gelic_net_descr *descr, | |
173261ed | 610 | struct sk_buff *skb) |
02c18891 | 611 | { |
02c18891 | 612 | if (skb->ip_summed != CHECKSUM_PARTIAL) |
173261ed MM |
613 | descr->dmac_cmd_status = GELIC_NET_DMAC_CMDSTAT_NOCS | |
614 | GELIC_NET_DMAC_CMDSTAT_END_FRAME; | |
02c18891 MM |
615 | else { |
616 | /* is packet ip? | |
617 | * if yes: tcp? udp? */ | |
618 | if (skb->protocol == htons(ETH_P_IP)) { | |
619 | if (ip_hdr(skb)->protocol == IPPROTO_TCP) | |
620 | descr->dmac_cmd_status = | |
173261ed MM |
621 | GELIC_NET_DMAC_CMDSTAT_TCPCS | |
622 | GELIC_NET_DMAC_CMDSTAT_END_FRAME; | |
623 | ||
02c18891 MM |
624 | else if (ip_hdr(skb)->protocol == IPPROTO_UDP) |
625 | descr->dmac_cmd_status = | |
173261ed MM |
626 | GELIC_NET_DMAC_CMDSTAT_UDPCS | |
627 | GELIC_NET_DMAC_CMDSTAT_END_FRAME; | |
02c18891 MM |
628 | else /* |
629 | * the stack should checksum non-tcp and non-udp | |
630 | * packets on his own: NETIF_F_IP_CSUM | |
631 | */ | |
632 | descr->dmac_cmd_status = | |
173261ed MM |
633 | GELIC_NET_DMAC_CMDSTAT_NOCS | |
634 | GELIC_NET_DMAC_CMDSTAT_END_FRAME; | |
02c18891 MM |
635 | } |
636 | } | |
637 | } | |
638 | ||
173261ed MM |
639 | static inline struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb, |
640 | unsigned short tag) | |
641 | { | |
642 | struct vlan_ethhdr *veth; | |
643 | static unsigned int c; | |
644 | ||
645 | if (skb_headroom(skb) < VLAN_HLEN) { | |
646 | struct sk_buff *sk_tmp = skb; | |
647 | pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c); | |
648 | skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN); | |
649 | if (!skb) | |
650 | return NULL; | |
651 | dev_kfree_skb_any(sk_tmp); | |
652 | } | |
653 | veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN); | |
654 | ||
655 | /* Move the mac addresses to the top of buffer */ | |
656 | memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN); | |
657 | ||
658 | veth->h_vlan_proto = __constant_htons(ETH_P_8021Q); | |
659 | veth->h_vlan_TCI = htons(tag); | |
660 | ||
661 | return skb; | |
662 | } | |
663 | ||
02c18891 MM |
664 | /** |
665 | * gelic_net_prepare_tx_descr_v - get dma address of skb_data | |
666 | * @card: card structure | |
667 | * @descr: descriptor structure | |
668 | * @skb: packet to use | |
669 | * | |
670 | * returns 0 on success, <0 on failure. | |
671 | * | |
672 | */ | |
673 | static int gelic_net_prepare_tx_descr_v(struct gelic_net_card *card, | |
674 | struct gelic_net_descr *descr, | |
675 | struct sk_buff *skb) | |
676 | { | |
173261ed | 677 | dma_addr_t buf; |
02c18891 | 678 | |
02c18891 | 679 | if (card->vlan_index != -1) { |
173261ed MM |
680 | struct sk_buff *skb_tmp; |
681 | skb_tmp = gelic_put_vlan_tag(skb, | |
682 | card->vlan_id[card->vlan_index]); | |
683 | if (!skb_tmp) | |
684 | return -ENOMEM; | |
685 | skb = skb_tmp; | |
02c18891 MM |
686 | } |
687 | ||
173261ed | 688 | buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE); |
02c18891 | 689 | |
173261ed | 690 | if (!buf) { |
02c18891 MM |
691 | dev_err(ctodev(card), |
692 | "dma map 2 failed (%p, %i). Dropping packet\n", | |
173261ed | 693 | skb->data, skb->len); |
02c18891 MM |
694 | return -ENOMEM; |
695 | } | |
696 | ||
173261ed MM |
697 | descr->buf_addr = buf; |
698 | descr->buf_size = skb->len; | |
699 | descr->skb = skb; | |
02c18891 | 700 | descr->data_status = 0; |
173261ed MM |
701 | descr->next_descr_addr = 0; /* terminate hw descr */ |
702 | gelic_net_set_txdescr_cmdstat(descr, skb); | |
48544cc2 MM |
703 | |
704 | /* bump free descriptor pointer */ | |
173261ed | 705 | card->tx_chain.head = descr->next; |
02c18891 MM |
706 | return 0; |
707 | } | |
708 | ||
709 | /** | |
710 | * gelic_net_kick_txdma - enables TX DMA processing | |
711 | * @card: card structure | |
712 | * @descr: descriptor address to enable TX processing at | |
713 | * | |
714 | */ | |
715 | static int gelic_net_kick_txdma(struct gelic_net_card *card, | |
716 | struct gelic_net_descr *descr) | |
717 | { | |
48544cc2 | 718 | int status = 0; |
02c18891 MM |
719 | |
720 | if (card->tx_dma_progress) | |
721 | return 0; | |
722 | ||
723 | if (gelic_net_get_descr_status(descr) == GELIC_NET_DESCR_CARDOWNED) { | |
724 | card->tx_dma_progress = 1; | |
dc029ad9 MM |
725 | status = lv1_net_start_tx_dma(bus_id(card), dev_id(card), |
726 | descr->bus_addr, 0); | |
727 | if (status) | |
02c18891 | 728 | dev_info(ctodev(card), "lv1_net_start_txdma failed," \ |
dc029ad9 | 729 | "status=%d\n", status); |
02c18891 MM |
730 | } |
731 | return status; | |
732 | } | |
733 | ||
734 | /** | |
735 | * gelic_net_xmit - transmits a frame over the device | |
736 | * @skb: packet to send out | |
737 | * @netdev: interface device structure | |
738 | * | |
739 | * returns 0 on success, <0 on failure | |
740 | */ | |
741 | static int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev) | |
742 | { | |
743 | struct gelic_net_card *card = netdev_priv(netdev); | |
48544cc2 | 744 | struct gelic_net_descr *descr; |
02c18891 MM |
745 | int result; |
746 | unsigned long flags; | |
747 | ||
748 | spin_lock_irqsave(&card->tx_dma_lock, flags); | |
749 | ||
750 | gelic_net_release_tx_chain(card, 0); | |
48544cc2 | 751 | |
02c18891 MM |
752 | descr = gelic_net_get_next_tx_descr(card); |
753 | if (!descr) { | |
48544cc2 MM |
754 | /* |
755 | * no more descriptors free | |
756 | */ | |
02c18891 MM |
757 | netif_stop_queue(netdev); |
758 | spin_unlock_irqrestore(&card->tx_dma_lock, flags); | |
759 | return NETDEV_TX_BUSY; | |
760 | } | |
02c18891 | 761 | |
48544cc2 MM |
762 | result = gelic_net_prepare_tx_descr_v(card, descr, skb); |
763 | if (result) { | |
764 | /* | |
765 | * DMA map failed. As chanses are that failure | |
766 | * would continue, just release skb and return | |
767 | */ | |
92548d60 | 768 | card->netdev->stats.tx_dropped++; |
48544cc2 MM |
769 | dev_kfree_skb_any(skb); |
770 | spin_unlock_irqrestore(&card->tx_dma_lock, flags); | |
771 | return NETDEV_TX_OK; | |
772 | } | |
773 | /* | |
774 | * link this prepared descriptor to previous one | |
775 | * to achieve high performance | |
776 | */ | |
777 | descr->prev->next_descr_addr = descr->bus_addr; | |
02c18891 MM |
778 | /* |
779 | * as hardware descriptor is modified in the above lines, | |
780 | * ensure that the hardware sees it | |
781 | */ | |
782 | wmb(); | |
48544cc2 MM |
783 | if (gelic_net_kick_txdma(card, descr)) { |
784 | /* | |
785 | * kick failed. | |
786 | * release descriptors which were just prepared | |
787 | */ | |
92548d60 | 788 | card->netdev->stats.tx_dropped++; |
48544cc2 MM |
789 | gelic_net_release_tx_descr(card, descr); |
790 | gelic_net_release_tx_descr(card, descr->next); | |
791 | card->tx_chain.tail = descr->next->next; | |
792 | dev_info(ctodev(card), "%s: kick failure\n", __func__); | |
793 | } else { | |
794 | /* OK, DMA started/reserved */ | |
795 | netdev->trans_start = jiffies; | |
796 | } | |
02c18891 | 797 | |
02c18891 MM |
798 | spin_unlock_irqrestore(&card->tx_dma_lock, flags); |
799 | return NETDEV_TX_OK; | |
02c18891 MM |
800 | } |
801 | ||
802 | /** | |
803 | * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on | |
804 | * @descr: descriptor to process | |
805 | * @card: card structure | |
806 | * | |
807 | * iommu-unmaps the skb, fills out skb structure and passes the data to the | |
808 | * stack. The descriptor state is not changed. | |
809 | */ | |
810 | static void gelic_net_pass_skb_up(struct gelic_net_descr *descr, | |
811 | struct gelic_net_card *card) | |
812 | { | |
813 | struct sk_buff *skb; | |
814 | struct net_device *netdev; | |
815 | u32 data_status, data_error; | |
816 | ||
817 | data_status = descr->data_status; | |
818 | data_error = descr->data_error; | |
819 | netdev = card->netdev; | |
820 | /* unmap skb buffer */ | |
821 | skb = descr->skb; | |
822 | dma_unmap_single(ctodev(card), descr->buf_addr, GELIC_NET_MAX_MTU, | |
823 | DMA_FROM_DEVICE); | |
824 | ||
825 | skb_put(skb, descr->valid_size? descr->valid_size : descr->result_size); | |
826 | if (!descr->valid_size) | |
827 | dev_info(ctodev(card), "buffer full %x %x %x\n", | |
828 | descr->result_size, descr->buf_size, | |
829 | descr->dmac_cmd_status); | |
830 | ||
831 | descr->skb = NULL; | |
832 | /* | |
833 | * the card put 2 bytes vlan tag in front | |
834 | * of the ethernet frame | |
835 | */ | |
836 | skb_pull(skb, 2); | |
837 | skb->protocol = eth_type_trans(skb, netdev); | |
838 | ||
839 | /* checksum offload */ | |
840 | if (card->rx_csum) { | |
841 | if ((data_status & GELIC_NET_DATA_STATUS_CHK_MASK) && | |
842 | (!(data_error & GELIC_NET_DATA_ERROR_CHK_MASK))) | |
843 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
844 | else | |
845 | skb->ip_summed = CHECKSUM_NONE; | |
846 | } else | |
847 | skb->ip_summed = CHECKSUM_NONE; | |
848 | ||
849 | /* update netdevice statistics */ | |
92548d60 MM |
850 | card->netdev->stats.rx_packets++; |
851 | card->netdev->stats.rx_bytes += skb->len; | |
02c18891 MM |
852 | |
853 | /* pass skb up to stack */ | |
854 | netif_receive_skb(skb); | |
855 | } | |
856 | ||
857 | /** | |
858 | * gelic_net_decode_one_descr - processes an rx descriptor | |
859 | * @card: card structure | |
860 | * | |
861 | * returns 1 if a packet has been sent to the stack, otherwise 0 | |
862 | * | |
863 | * processes an rx descriptor by iommu-unmapping the data buffer and passing | |
864 | * the packet up to the stack | |
865 | */ | |
866 | static int gelic_net_decode_one_descr(struct gelic_net_card *card) | |
867 | { | |
868 | enum gelic_net_descr_status status; | |
869 | struct gelic_net_descr_chain *chain = &card->rx_chain; | |
870 | struct gelic_net_descr *descr = chain->tail; | |
871 | int dmac_chain_ended; | |
872 | ||
873 | status = gelic_net_get_descr_status(descr); | |
874 | /* is this descriptor terminated with next_descr == NULL? */ | |
875 | dmac_chain_ended = | |
876 | descr->dmac_cmd_status & GELIC_NET_DMAC_CMDSTAT_RXDCEIS; | |
877 | ||
878 | if (status == GELIC_NET_DESCR_CARDOWNED) | |
879 | return 0; | |
880 | ||
881 | if (status == GELIC_NET_DESCR_NOT_IN_USE) { | |
882 | dev_dbg(ctodev(card), "dormant descr? %p\n", descr); | |
883 | return 0; | |
884 | } | |
885 | ||
886 | if ((status == GELIC_NET_DESCR_RESPONSE_ERROR) || | |
887 | (status == GELIC_NET_DESCR_PROTECTION_ERROR) || | |
888 | (status == GELIC_NET_DESCR_FORCE_END)) { | |
889 | dev_info(ctodev(card), "dropping RX descriptor with state %x\n", | |
890 | status); | |
92548d60 | 891 | card->netdev->stats.rx_dropped++; |
02c18891 MM |
892 | goto refill; |
893 | } | |
894 | ||
fe6d3a40 MM |
895 | if (status == GELIC_NET_DESCR_BUFFER_FULL) { |
896 | /* | |
897 | * Buffer full would occur if and only if | |
898 | * the frame length was longer than the size of this | |
899 | * descriptor's buffer. If the frame length was equal | |
900 | * to or shorter than buffer'size, FRAME_END condition | |
901 | * would occur. | |
902 | * Anyway this frame was longer than the MTU, | |
903 | * just drop it. | |
904 | */ | |
905 | dev_info(ctodev(card), "overlength frame\n"); | |
906 | goto refill; | |
907 | } | |
908 | /* | |
909 | * descriptoers any other than FRAME_END here should | |
910 | * be treated as error. | |
911 | */ | |
912 | if (status != GELIC_NET_DESCR_FRAME_END) { | |
02c18891 MM |
913 | dev_dbg(ctodev(card), "RX descriptor with state %x\n", |
914 | status); | |
915 | goto refill; | |
916 | } | |
917 | ||
918 | /* ok, we've got a packet in descr */ | |
fe6d3a40 | 919 | gelic_net_pass_skb_up(descr, card); |
02c18891 | 920 | refill: |
fe6d3a40 MM |
921 | /* |
922 | * So that always DMAC can see the end | |
923 | * of the descriptor chain to avoid | |
924 | * from unwanted DMAC overrun. | |
925 | */ | |
926 | descr->next_descr_addr = 0; | |
02c18891 MM |
927 | |
928 | /* change the descriptor state: */ | |
929 | gelic_net_set_descr_status(descr, GELIC_NET_DESCR_NOT_IN_USE); | |
930 | ||
fe6d3a40 MM |
931 | /* |
932 | * this call can fail, but for now, just leave this | |
933 | * decriptor without skb | |
02c18891 MM |
934 | */ |
935 | gelic_net_prepare_rx_descr(card, descr); | |
fe6d3a40 | 936 | |
02c18891 MM |
937 | chain->head = descr; |
938 | chain->tail = descr->next; | |
fe6d3a40 MM |
939 | |
940 | /* | |
941 | * Set this descriptor the end of the chain. | |
942 | */ | |
02c18891 MM |
943 | descr->prev->next_descr_addr = descr->bus_addr; |
944 | ||
fe6d3a40 MM |
945 | /* |
946 | * If dmac chain was met, DMAC stopped. | |
947 | * thus re-enable it | |
948 | */ | |
02c18891 | 949 | if (dmac_chain_ended) { |
583aae10 MM |
950 | card->rx_dma_restart_required = 1; |
951 | dev_dbg(ctodev(card), "reenable rx dma scheduled\n"); | |
02c18891 MM |
952 | } |
953 | ||
954 | return 1; | |
955 | } | |
956 | ||
957 | /** | |
958 | * gelic_net_poll - NAPI poll function called by the stack to return packets | |
959 | * @netdev: interface device structure | |
960 | * @budget: number of packets we can pass to the stack at most | |
961 | * | |
962 | * returns 0 if no more packets available to the driver/stack. Returns 1, | |
963 | * if the quota is exceeded, but the driver has still packets. | |
964 | * | |
965 | */ | |
bea3348e | 966 | static int gelic_net_poll(struct napi_struct *napi, int budget) |
02c18891 | 967 | { |
bea3348e SH |
968 | struct gelic_net_card *card = container_of(napi, struct gelic_net_card, napi); |
969 | struct net_device *netdev = card->netdev; | |
970 | int packets_done = 0; | |
02c18891 | 971 | |
bea3348e SH |
972 | while (packets_done < budget) { |
973 | if (!gelic_net_decode_one_descr(card)) | |
02c18891 | 974 | break; |
bea3348e SH |
975 | |
976 | packets_done++; | |
02c18891 | 977 | } |
bea3348e SH |
978 | |
979 | if (packets_done < budget) { | |
980 | netif_rx_complete(netdev, napi); | |
02c18891 | 981 | gelic_net_rx_irq_on(card); |
bea3348e SH |
982 | } |
983 | return packets_done; | |
02c18891 | 984 | } |
02c18891 MM |
985 | /** |
986 | * gelic_net_change_mtu - changes the MTU of an interface | |
987 | * @netdev: interface device structure | |
988 | * @new_mtu: new MTU value | |
989 | * | |
990 | * returns 0 on success, <0 on failure | |
991 | */ | |
992 | static int gelic_net_change_mtu(struct net_device *netdev, int new_mtu) | |
993 | { | |
994 | /* no need to re-alloc skbs or so -- the max mtu is about 2.3k | |
995 | * and mtu is outbound only anyway */ | |
996 | if ((new_mtu < GELIC_NET_MIN_MTU) || | |
997 | (new_mtu > GELIC_NET_MAX_MTU)) { | |
998 | return -EINVAL; | |
999 | } | |
1000 | netdev->mtu = new_mtu; | |
1001 | return 0; | |
1002 | } | |
1003 | ||
1004 | /** | |
1005 | * gelic_net_interrupt - event handler for gelic_net | |
1006 | */ | |
1007 | static irqreturn_t gelic_net_interrupt(int irq, void *ptr) | |
1008 | { | |
1009 | unsigned long flags; | |
1010 | struct net_device *netdev = ptr; | |
1011 | struct gelic_net_card *card = netdev_priv(netdev); | |
1012 | u64 status; | |
1013 | ||
1014 | status = card->irq_status; | |
1015 | ||
1016 | if (!status) | |
1017 | return IRQ_NONE; | |
1018 | ||
583aae10 MM |
1019 | if (card->rx_dma_restart_required) { |
1020 | card->rx_dma_restart_required = 0; | |
1021 | gelic_net_enable_rxdmac(card); | |
1022 | } | |
1023 | ||
02c18891 MM |
1024 | if (status & GELIC_NET_RXINT) { |
1025 | gelic_net_rx_irq_off(card); | |
bea3348e | 1026 | netif_rx_schedule(netdev, &card->napi); |
02c18891 MM |
1027 | } |
1028 | ||
1029 | if (status & GELIC_NET_TXINT) { | |
1030 | spin_lock_irqsave(&card->tx_dma_lock, flags); | |
1031 | card->tx_dma_progress = 0; | |
48544cc2 MM |
1032 | gelic_net_release_tx_chain(card, 0); |
1033 | /* kick outstanding tx descriptor if any */ | |
1034 | gelic_net_kick_txdma(card, card->tx_chain.tail); | |
02c18891 | 1035 | spin_unlock_irqrestore(&card->tx_dma_lock, flags); |
02c18891 MM |
1036 | } |
1037 | return IRQ_HANDLED; | |
1038 | } | |
1039 | ||
1040 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
1041 | /** | |
1042 | * gelic_net_poll_controller - artificial interrupt for netconsole etc. | |
1043 | * @netdev: interface device structure | |
1044 | * | |
1045 | * see Documentation/networking/netconsole.txt | |
1046 | */ | |
1047 | static void gelic_net_poll_controller(struct net_device *netdev) | |
1048 | { | |
1049 | struct gelic_net_card *card = netdev_priv(netdev); | |
1050 | ||
1051 | gelic_net_set_irq_mask(card, 0); | |
1052 | gelic_net_interrupt(netdev->irq, netdev); | |
1053 | gelic_net_set_irq_mask(card, card->ghiintmask); | |
1054 | } | |
1055 | #endif /* CONFIG_NET_POLL_CONTROLLER */ | |
1056 | ||
1057 | /** | |
1058 | * gelic_net_open_device - open device and map dma region | |
1059 | * @card: card structure | |
1060 | */ | |
1061 | static int gelic_net_open_device(struct gelic_net_card *card) | |
1062 | { | |
1063 | int result; | |
1064 | ||
1065 | result = ps3_sb_event_receive_port_setup(card->dev, PS3_BINDING_CPU_ANY, | |
1066 | &card->netdev->irq); | |
1067 | ||
1068 | if (result) { | |
1069 | dev_info(ctodev(card), | |
1070 | "%s:%d: gelic_net_open_device failed (%d)\n", | |
1071 | __func__, __LINE__, result); | |
1072 | result = -EPERM; | |
1073 | goto fail_alloc_irq; | |
1074 | } | |
1075 | ||
1076 | result = request_irq(card->netdev->irq, gelic_net_interrupt, | |
f0861f82 | 1077 | IRQF_DISABLED, card->netdev->name, card->netdev); |
02c18891 MM |
1078 | |
1079 | if (result) { | |
1080 | dev_info(ctodev(card), "%s:%d: request_irq failed (%d)\n", | |
1081 | __func__, __LINE__, result); | |
1082 | goto fail_request_irq; | |
1083 | } | |
1084 | ||
1085 | return 0; | |
1086 | ||
1087 | fail_request_irq: | |
1088 | ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq); | |
1089 | card->netdev->irq = NO_IRQ; | |
1090 | fail_alloc_irq: | |
1091 | return result; | |
1092 | } | |
1093 | ||
1094 | ||
1095 | /** | |
1096 | * gelic_net_open - called upon ifonfig up | |
1097 | * @netdev: interface device structure | |
1098 | * | |
1099 | * returns 0 on success, <0 on failure | |
1100 | * | |
1101 | * gelic_net_open allocates all the descriptors and memory needed for | |
1102 | * operation, sets up multicast list and enables interrupts | |
1103 | */ | |
1104 | static int gelic_net_open(struct net_device *netdev) | |
1105 | { | |
1106 | struct gelic_net_card *card = netdev_priv(netdev); | |
1107 | ||
1108 | dev_dbg(ctodev(card), " -> %s:%d\n", __func__, __LINE__); | |
1109 | ||
1110 | gelic_net_open_device(card); | |
1111 | ||
1112 | if (gelic_net_init_chain(card, &card->tx_chain, | |
1113 | card->descr, GELIC_NET_TX_DESCRIPTORS)) | |
1114 | goto alloc_tx_failed; | |
1115 | if (gelic_net_init_chain(card, &card->rx_chain, | |
9f6c9a8c | 1116 | card->descr + GELIC_NET_TX_DESCRIPTORS, |
02c18891 MM |
1117 | GELIC_NET_RX_DESCRIPTORS)) |
1118 | goto alloc_rx_failed; | |
1119 | ||
1120 | /* head of chain */ | |
1121 | card->tx_top = card->tx_chain.head; | |
1122 | card->rx_top = card->rx_chain.head; | |
1123 | dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n", | |
1124 | card->rx_top, card->tx_top, sizeof(struct gelic_net_descr), | |
1125 | GELIC_NET_RX_DESCRIPTORS); | |
1126 | /* allocate rx skbs */ | |
1127 | if (gelic_net_alloc_rx_skbs(card)) | |
1128 | goto alloc_skbs_failed; | |
1129 | ||
bea3348e SH |
1130 | napi_enable(&card->napi); |
1131 | ||
02c18891 MM |
1132 | card->tx_dma_progress = 0; |
1133 | card->ghiintmask = GELIC_NET_RXINT | GELIC_NET_TXINT; | |
1134 | ||
1135 | gelic_net_set_irq_mask(card, card->ghiintmask); | |
1136 | gelic_net_enable_rxdmac(card); | |
1137 | ||
1138 | netif_start_queue(netdev); | |
1139 | netif_carrier_on(netdev); | |
02c18891 MM |
1140 | |
1141 | return 0; | |
1142 | ||
1143 | alloc_skbs_failed: | |
1144 | gelic_net_free_chain(card, card->rx_top); | |
1145 | alloc_rx_failed: | |
1146 | gelic_net_free_chain(card, card->tx_top); | |
1147 | alloc_tx_failed: | |
1148 | return -ENOMEM; | |
1149 | } | |
1150 | ||
02c18891 MM |
1151 | static void gelic_net_get_drvinfo (struct net_device *netdev, |
1152 | struct ethtool_drvinfo *info) | |
1153 | { | |
1154 | strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1); | |
1155 | strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1); | |
1156 | } | |
1157 | ||
1158 | static int gelic_net_get_settings(struct net_device *netdev, | |
1159 | struct ethtool_cmd *cmd) | |
1160 | { | |
1161 | struct gelic_net_card *card = netdev_priv(netdev); | |
1162 | int status; | |
1163 | u64 v1, v2; | |
1164 | int speed, duplex; | |
1165 | ||
1166 | speed = duplex = -1; | |
1167 | status = lv1_net_control(bus_id(card), dev_id(card), | |
1168 | GELIC_NET_GET_ETH_PORT_STATUS, GELIC_NET_PORT, 0, 0, | |
1169 | &v1, &v2); | |
1170 | if (status) { | |
1171 | /* link down */ | |
1172 | } else { | |
1173 | if (v1 & GELIC_NET_FULL_DUPLEX) { | |
1174 | duplex = DUPLEX_FULL; | |
1175 | } else { | |
1176 | duplex = DUPLEX_HALF; | |
1177 | } | |
1178 | ||
1179 | if (v1 & GELIC_NET_SPEED_10 ) { | |
1180 | speed = SPEED_10; | |
1181 | } else if (v1 & GELIC_NET_SPEED_100) { | |
1182 | speed = SPEED_100; | |
1183 | } else if (v1 & GELIC_NET_SPEED_1000) { | |
1184 | speed = SPEED_1000; | |
1185 | } | |
1186 | } | |
1187 | cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg | | |
1188 | SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | | |
1189 | SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full | | |
1190 | SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full; | |
1191 | cmd->advertising = cmd->supported; | |
1192 | cmd->speed = speed; | |
1193 | cmd->duplex = duplex; | |
1194 | cmd->autoneg = AUTONEG_ENABLE; /* always enabled */ | |
1195 | cmd->port = PORT_TP; | |
1196 | ||
1197 | return 0; | |
1198 | } | |
1199 | ||
1200 | static u32 gelic_net_get_link(struct net_device *netdev) | |
1201 | { | |
1202 | struct gelic_net_card *card = netdev_priv(netdev); | |
1203 | int status; | |
1204 | u64 v1, v2; | |
1205 | int link; | |
1206 | ||
1207 | status = lv1_net_control(bus_id(card), dev_id(card), | |
1208 | GELIC_NET_GET_ETH_PORT_STATUS, GELIC_NET_PORT, 0, 0, | |
1209 | &v1, &v2); | |
1210 | if (status) | |
1211 | return 0; /* link down */ | |
1212 | ||
1213 | if (v1 & GELIC_NET_LINK_UP) | |
1214 | link = 1; | |
1215 | else | |
1216 | link = 0; | |
1217 | ||
1218 | return link; | |
1219 | } | |
1220 | ||
1221 | static int gelic_net_nway_reset(struct net_device *netdev) | |
1222 | { | |
1223 | if (netif_running(netdev)) { | |
1224 | gelic_net_stop(netdev); | |
1225 | gelic_net_open(netdev); | |
1226 | } | |
1227 | return 0; | |
1228 | } | |
1229 | ||
1230 | static u32 gelic_net_get_tx_csum(struct net_device *netdev) | |
1231 | { | |
1232 | return (netdev->features & NETIF_F_IP_CSUM) != 0; | |
1233 | } | |
1234 | ||
1235 | static int gelic_net_set_tx_csum(struct net_device *netdev, u32 data) | |
1236 | { | |
1237 | if (data) | |
1238 | netdev->features |= NETIF_F_IP_CSUM; | |
1239 | else | |
1240 | netdev->features &= ~NETIF_F_IP_CSUM; | |
1241 | ||
1242 | return 0; | |
1243 | } | |
1244 | ||
1245 | static u32 gelic_net_get_rx_csum(struct net_device *netdev) | |
1246 | { | |
1247 | struct gelic_net_card *card = netdev_priv(netdev); | |
1248 | ||
1249 | return card->rx_csum; | |
1250 | } | |
1251 | ||
1252 | static int gelic_net_set_rx_csum(struct net_device *netdev, u32 data) | |
1253 | { | |
1254 | struct gelic_net_card *card = netdev_priv(netdev); | |
1255 | ||
1256 | card->rx_csum = data; | |
1257 | return 0; | |
1258 | } | |
1259 | ||
1260 | static struct ethtool_ops gelic_net_ethtool_ops = { | |
1261 | .get_drvinfo = gelic_net_get_drvinfo, | |
1262 | .get_settings = gelic_net_get_settings, | |
1263 | .get_link = gelic_net_get_link, | |
1264 | .nway_reset = gelic_net_nway_reset, | |
1265 | .get_tx_csum = gelic_net_get_tx_csum, | |
1266 | .set_tx_csum = gelic_net_set_tx_csum, | |
1267 | .get_rx_csum = gelic_net_get_rx_csum, | |
1268 | .set_rx_csum = gelic_net_set_rx_csum, | |
1269 | }; | |
02c18891 MM |
1270 | |
1271 | /** | |
1272 | * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout | |
1273 | * function (to be called not under interrupt status) | |
1274 | * @work: work is context of tx timout task | |
1275 | * | |
1276 | * called as task when tx hangs, resets interface (if interface is up) | |
1277 | */ | |
1278 | static void gelic_net_tx_timeout_task(struct work_struct *work) | |
1279 | { | |
1280 | struct gelic_net_card *card = | |
1281 | container_of(work, struct gelic_net_card, tx_timeout_task); | |
1282 | struct net_device *netdev = card->netdev; | |
1283 | ||
1284 | dev_info(ctodev(card), "%s:Timed out. Restarting... \n", __func__); | |
1285 | ||
1286 | if (!(netdev->flags & IFF_UP)) | |
1287 | goto out; | |
1288 | ||
1289 | netif_device_detach(netdev); | |
1290 | gelic_net_stop(netdev); | |
1291 | ||
1292 | gelic_net_open(netdev); | |
1293 | netif_device_attach(netdev); | |
1294 | ||
1295 | out: | |
1296 | atomic_dec(&card->tx_timeout_task_counter); | |
1297 | } | |
1298 | ||
1299 | /** | |
1300 | * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in. | |
1301 | * @netdev: interface device structure | |
1302 | * | |
1303 | * called, if tx hangs. Schedules a task that resets the interface | |
1304 | */ | |
1305 | static void gelic_net_tx_timeout(struct net_device *netdev) | |
1306 | { | |
1307 | struct gelic_net_card *card; | |
1308 | ||
1309 | card = netdev_priv(netdev); | |
1310 | atomic_inc(&card->tx_timeout_task_counter); | |
1311 | if (netdev->flags & IFF_UP) | |
1312 | schedule_work(&card->tx_timeout_task); | |
1313 | else | |
1314 | atomic_dec(&card->tx_timeout_task_counter); | |
1315 | } | |
1316 | ||
1317 | /** | |
1318 | * gelic_net_setup_netdev_ops - initialization of net_device operations | |
1319 | * @netdev: net_device structure | |
1320 | * | |
1321 | * fills out function pointers in the net_device structure | |
1322 | */ | |
1323 | static void gelic_net_setup_netdev_ops(struct net_device *netdev) | |
1324 | { | |
1325 | netdev->open = &gelic_net_open; | |
1326 | netdev->stop = &gelic_net_stop; | |
1327 | netdev->hard_start_xmit = &gelic_net_xmit; | |
02c18891 MM |
1328 | netdev->set_multicast_list = &gelic_net_set_multi; |
1329 | netdev->change_mtu = &gelic_net_change_mtu; | |
1330 | /* tx watchdog */ | |
1331 | netdev->tx_timeout = &gelic_net_tx_timeout; | |
1332 | netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT; | |
02c18891 | 1333 | netdev->ethtool_ops = &gelic_net_ethtool_ops; |
02c18891 MM |
1334 | } |
1335 | ||
1336 | /** | |
1337 | * gelic_net_setup_netdev - initialization of net_device | |
1338 | * @card: card structure | |
1339 | * | |
1340 | * Returns 0 on success or <0 on failure | |
1341 | * | |
1342 | * gelic_net_setup_netdev initializes the net_device structure | |
1343 | **/ | |
1344 | static int gelic_net_setup_netdev(struct gelic_net_card *card) | |
1345 | { | |
1346 | struct net_device *netdev = card->netdev; | |
1347 | struct sockaddr addr; | |
1348 | unsigned int i; | |
1349 | int status; | |
1350 | u64 v1, v2; | |
0795af57 | 1351 | DECLARE_MAC_BUF(mac); |
02c18891 | 1352 | |
02c18891 MM |
1353 | SET_NETDEV_DEV(netdev, &card->dev->core); |
1354 | spin_lock_init(&card->tx_dma_lock); | |
1355 | ||
1356 | card->rx_csum = GELIC_NET_RX_CSUM_DEFAULT; | |
1357 | ||
1358 | gelic_net_setup_netdev_ops(netdev); | |
1359 | ||
bea3348e SH |
1360 | netif_napi_add(netdev, &card->napi, |
1361 | gelic_net_poll, GELIC_NET_NAPI_WEIGHT); | |
1362 | ||
02c18891 MM |
1363 | netdev->features = NETIF_F_IP_CSUM; |
1364 | ||
1365 | status = lv1_net_control(bus_id(card), dev_id(card), | |
1366 | GELIC_NET_GET_MAC_ADDRESS, | |
1367 | 0, 0, 0, &v1, &v2); | |
1368 | if (status || !is_valid_ether_addr((u8 *)&v1)) { | |
1369 | dev_info(ctodev(card), | |
1370 | "%s:lv1_net_control GET_MAC_ADDR failed %d\n", | |
1371 | __func__, status); | |
1372 | return -EINVAL; | |
1373 | } | |
1374 | v1 <<= 16; | |
1375 | memcpy(addr.sa_data, &v1, ETH_ALEN); | |
1376 | memcpy(netdev->dev_addr, addr.sa_data, ETH_ALEN); | |
0795af57 JP |
1377 | dev_info(ctodev(card), "MAC addr %s\n", |
1378 | print_mac(mac, netdev->dev_addr)); | |
02c18891 MM |
1379 | |
1380 | card->vlan_index = -1; /* no vlan */ | |
1381 | for (i = 0; i < GELIC_NET_VLAN_MAX; i++) { | |
1382 | status = lv1_net_control(bus_id(card), dev_id(card), | |
1383 | GELIC_NET_GET_VLAN_ID, | |
1384 | i + 1, /* index; one based */ | |
1385 | 0, 0, &v1, &v2); | |
1386 | if (status == GELIC_NET_VLAN_NO_ENTRY) { | |
1387 | dev_dbg(ctodev(card), | |
1388 | "GELIC_VLAN_ID no entry:%d, VLAN disabled\n", | |
1389 | status); | |
1390 | card->vlan_id[i] = 0; | |
1391 | } else if (status) { | |
1392 | dev_dbg(ctodev(card), | |
1393 | "%s:GELIC_NET_VLAN_ID faild, status=%d\n", | |
1394 | __func__, status); | |
1395 | card->vlan_id[i] = 0; | |
1396 | } else { | |
1397 | card->vlan_id[i] = (u32)v1; | |
1398 | dev_dbg(ctodev(card), "vlan_id:%d, %lx\n", i, v1); | |
1399 | } | |
1400 | } | |
173261ed MM |
1401 | |
1402 | if (card->vlan_id[GELIC_NET_VLAN_WIRED - 1]) { | |
02c18891 | 1403 | card->vlan_index = GELIC_NET_VLAN_WIRED - 1; |
173261ed MM |
1404 | netdev->hard_header_len += VLAN_HLEN; |
1405 | } | |
02c18891 MM |
1406 | |
1407 | status = register_netdev(netdev); | |
1408 | if (status) { | |
1409 | dev_err(ctodev(card), "%s:Couldn't register net_device: %d\n", | |
1410 | __func__, status); | |
1411 | return status; | |
1412 | } | |
1413 | ||
1414 | return 0; | |
1415 | } | |
1416 | ||
1417 | /** | |
1418 | * gelic_net_alloc_card - allocates net_device and card structure | |
1419 | * | |
1420 | * returns the card structure or NULL in case of errors | |
1421 | * | |
1422 | * the card and net_device structures are linked to each other | |
1423 | */ | |
1424 | static struct gelic_net_card *gelic_net_alloc_card(void) | |
1425 | { | |
1426 | struct net_device *netdev; | |
1427 | struct gelic_net_card *card; | |
1428 | size_t alloc_size; | |
1429 | ||
1430 | alloc_size = sizeof (*card) + | |
1431 | sizeof (struct gelic_net_descr) * GELIC_NET_RX_DESCRIPTORS + | |
1432 | sizeof (struct gelic_net_descr) * GELIC_NET_TX_DESCRIPTORS; | |
1433 | /* | |
1434 | * we assume private data is allocated 32 bytes (or more) aligned | |
1435 | * so that gelic_net_descr should be 32 bytes aligned. | |
1436 | * Current alloc_etherdev() does do it because NETDEV_ALIGN | |
1437 | * is 32. | |
1438 | * check this assumption here. | |
1439 | */ | |
1440 | BUILD_BUG_ON(NETDEV_ALIGN < 32); | |
1441 | BUILD_BUG_ON(offsetof(struct gelic_net_card, irq_status) % 8); | |
1442 | BUILD_BUG_ON(offsetof(struct gelic_net_card, descr) % 32); | |
1443 | ||
1444 | netdev = alloc_etherdev(alloc_size); | |
1445 | if (!netdev) | |
1446 | return NULL; | |
1447 | ||
1448 | card = netdev_priv(netdev); | |
1449 | card->netdev = netdev; | |
1450 | INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task); | |
1451 | init_waitqueue_head(&card->waitq); | |
1452 | atomic_set(&card->tx_timeout_task_counter, 0); | |
1453 | ||
1454 | return card; | |
1455 | } | |
1456 | ||
1457 | /** | |
1458 | * ps3_gelic_driver_probe - add a device to the control of this driver | |
1459 | */ | |
1460 | static int ps3_gelic_driver_probe (struct ps3_system_bus_device *dev) | |
1461 | { | |
1462 | struct gelic_net_card *card = gelic_net_alloc_card(); | |
1463 | int result; | |
1464 | ||
1465 | if (!card) { | |
1466 | dev_info(&dev->core, "gelic_net_alloc_card failed\n"); | |
1467 | result = -ENOMEM; | |
1468 | goto fail_alloc_card; | |
1469 | } | |
1470 | ||
1471 | ps3_system_bus_set_driver_data(dev, card); | |
1472 | card->dev = dev; | |
1473 | ||
1474 | result = ps3_open_hv_device(dev); | |
1475 | ||
1476 | if (result) { | |
1477 | dev_dbg(&dev->core, "ps3_open_hv_device failed\n"); | |
1478 | goto fail_open; | |
1479 | } | |
1480 | ||
1481 | result = ps3_dma_region_create(dev->d_region); | |
1482 | ||
1483 | if (result) { | |
1484 | dev_dbg(&dev->core, "ps3_dma_region_create failed(%d)\n", | |
1485 | result); | |
1486 | BUG_ON("check region type"); | |
1487 | goto fail_dma_region; | |
1488 | } | |
1489 | ||
1490 | result = lv1_net_set_interrupt_status_indicator(bus_id(card), | |
1491 | dev_id(card), | |
1492 | ps3_mm_phys_to_lpar(__pa(&card->irq_status)), | |
1493 | 0); | |
1494 | ||
1495 | if (result) { | |
1496 | dev_dbg(&dev->core, | |
1497 | "lv1_net_set_interrupt_status_indicator failed: %s\n", | |
1498 | ps3_result(result)); | |
1499 | result = -EIO; | |
1500 | goto fail_status_indicator; | |
1501 | } | |
1502 | ||
1503 | result = gelic_net_setup_netdev(card); | |
1504 | ||
1505 | if (result) { | |
1506 | dev_dbg(&dev->core, "%s:%d: ps3_dma_region_create failed: " | |
1507 | "(%d)\n", __func__, __LINE__, result); | |
1508 | goto fail_setup_netdev; | |
1509 | } | |
1510 | ||
1511 | return 0; | |
1512 | ||
1513 | fail_setup_netdev: | |
1514 | lv1_net_set_interrupt_status_indicator(bus_id(card), | |
b94e1d47 | 1515 | dev_id(card), |
02c18891 MM |
1516 | 0 , 0); |
1517 | fail_status_indicator: | |
1518 | ps3_dma_region_free(dev->d_region); | |
1519 | fail_dma_region: | |
1520 | ps3_close_hv_device(dev); | |
1521 | fail_open: | |
1522 | ps3_system_bus_set_driver_data(dev, NULL); | |
1523 | free_netdev(card->netdev); | |
1524 | fail_alloc_card: | |
1525 | return result; | |
1526 | } | |
1527 | ||
1528 | /** | |
1529 | * ps3_gelic_driver_remove - remove a device from the control of this driver | |
1530 | */ | |
1531 | ||
1532 | static int ps3_gelic_driver_remove (struct ps3_system_bus_device *dev) | |
1533 | { | |
1534 | struct gelic_net_card *card = ps3_system_bus_get_driver_data(dev); | |
1535 | ||
1536 | wait_event(card->waitq, | |
1537 | atomic_read(&card->tx_timeout_task_counter) == 0); | |
1538 | ||
1539 | lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card), | |
1540 | 0 , 0); | |
1541 | ||
1542 | unregister_netdev(card->netdev); | |
1543 | free_netdev(card->netdev); | |
1544 | ||
1545 | ps3_system_bus_set_driver_data(dev, NULL); | |
1546 | ||
1547 | ps3_dma_region_free(dev->d_region); | |
1548 | ||
1549 | ps3_close_hv_device(dev); | |
1550 | ||
1551 | return 0; | |
1552 | } | |
1553 | ||
1554 | static struct ps3_system_bus_driver ps3_gelic_driver = { | |
1555 | .match_id = PS3_MATCH_ID_GELIC, | |
1556 | .probe = ps3_gelic_driver_probe, | |
1557 | .remove = ps3_gelic_driver_remove, | |
1558 | .shutdown = ps3_gelic_driver_remove, | |
1559 | .core.name = "ps3_gelic_driver", | |
1560 | .core.owner = THIS_MODULE, | |
1561 | }; | |
1562 | ||
1563 | static int __init ps3_gelic_driver_init (void) | |
1564 | { | |
1565 | return firmware_has_feature(FW_FEATURE_PS3_LV1) | |
1566 | ? ps3_system_bus_driver_register(&ps3_gelic_driver) | |
1567 | : -ENODEV; | |
1568 | } | |
1569 | ||
1570 | static void __exit ps3_gelic_driver_exit (void) | |
1571 | { | |
1572 | ps3_system_bus_driver_unregister(&ps3_gelic_driver); | |
1573 | } | |
1574 | ||
1575 | module_init (ps3_gelic_driver_init); | |
1576 | module_exit (ps3_gelic_driver_exit); | |
1577 | ||
1578 | MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC); | |
1579 |