ps3_gelic_net.c 40 KB

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