ps3_gelic_net.c 41 KB

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