ps3_gelic_net.c 40 KB

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