ps3_gelic_net.c 48 KB

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