ps3_gelic_net.c 48 KB

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