ps3_gelic_net.c 48 KB

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