ps3_gelic_net.c 40 KB

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