ps3_gelic_net.c 45 KB

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