b44.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615
  1. /* b44.c: Broadcom 44xx/47xx Fast Ethernet device driver.
  2. *
  3. * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  4. * Copyright (C) 2004 Pekka Pietikainen (pp@ee.oulu.fi)
  5. * Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
  6. * Copyright (C) 2006 Felix Fietkau (nbd@openwrt.org)
  7. * Copyright (C) 2006 Broadcom Corporation.
  8. * Copyright (C) 2007 Michael Buesch <m@bues.ch>
  9. * Copyright (C) 2013 Hauke Mehrtens <hauke@hauke-m.de>
  10. *
  11. * Distribute under GPL.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/types.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/ethtool.h>
  20. #include <linux/mii.h>
  21. #include <linux/if_ether.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/pci.h>
  25. #include <linux/delay.h>
  26. #include <linux/init.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/ssb/ssb.h>
  30. #include <linux/slab.h>
  31. #include <linux/phy.h>
  32. #include <linux/uaccess.h>
  33. #include <asm/io.h>
  34. #include <asm/irq.h>
  35. #include "b44.h"
  36. #define DRV_MODULE_NAME "b44"
  37. #define DRV_MODULE_VERSION "2.0"
  38. #define DRV_DESCRIPTION "Broadcom 44xx/47xx 10/100 PCI ethernet driver"
  39. #define B44_DEF_MSG_ENABLE \
  40. (NETIF_MSG_DRV | \
  41. NETIF_MSG_PROBE | \
  42. NETIF_MSG_LINK | \
  43. NETIF_MSG_TIMER | \
  44. NETIF_MSG_IFDOWN | \
  45. NETIF_MSG_IFUP | \
  46. NETIF_MSG_RX_ERR | \
  47. NETIF_MSG_TX_ERR)
  48. /* length of time before we decide the hardware is borked,
  49. * and dev->tx_timeout() should be called to fix the problem
  50. */
  51. #define B44_TX_TIMEOUT (5 * HZ)
  52. /* hardware minimum and maximum for a single frame's data payload */
  53. #define B44_MIN_MTU ETH_ZLEN
  54. #define B44_MAX_MTU ETH_DATA_LEN
  55. #define B44_RX_RING_SIZE 512
  56. #define B44_DEF_RX_RING_PENDING 200
  57. #define B44_RX_RING_BYTES (sizeof(struct dma_desc) * \
  58. B44_RX_RING_SIZE)
  59. #define B44_TX_RING_SIZE 512
  60. #define B44_DEF_TX_RING_PENDING (B44_TX_RING_SIZE - 1)
  61. #define B44_TX_RING_BYTES (sizeof(struct dma_desc) * \
  62. B44_TX_RING_SIZE)
  63. #define TX_RING_GAP(BP) \
  64. (B44_TX_RING_SIZE - (BP)->tx_pending)
  65. #define TX_BUFFS_AVAIL(BP) \
  66. (((BP)->tx_cons <= (BP)->tx_prod) ? \
  67. (BP)->tx_cons + (BP)->tx_pending - (BP)->tx_prod : \
  68. (BP)->tx_cons - (BP)->tx_prod - TX_RING_GAP(BP))
  69. #define NEXT_TX(N) (((N) + 1) & (B44_TX_RING_SIZE - 1))
  70. #define RX_PKT_OFFSET (RX_HEADER_LEN + 2)
  71. #define RX_PKT_BUF_SZ (1536 + RX_PKT_OFFSET)
  72. /* minimum number of free TX descriptors required to wake up TX process */
  73. #define B44_TX_WAKEUP_THRESH (B44_TX_RING_SIZE / 4)
  74. /* b44 internal pattern match filter info */
  75. #define B44_PATTERN_BASE 0x400
  76. #define B44_PATTERN_SIZE 0x80
  77. #define B44_PMASK_BASE 0x600
  78. #define B44_PMASK_SIZE 0x10
  79. #define B44_MAX_PATTERNS 16
  80. #define B44_ETHIPV6UDP_HLEN 62
  81. #define B44_ETHIPV4UDP_HLEN 42
  82. MODULE_AUTHOR("Felix Fietkau, Florian Schirmer, Pekka Pietikainen, David S. Miller");
  83. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  84. MODULE_LICENSE("GPL");
  85. MODULE_VERSION(DRV_MODULE_VERSION);
  86. static int b44_debug = -1; /* -1 == use B44_DEF_MSG_ENABLE as value */
  87. module_param(b44_debug, int, 0);
  88. MODULE_PARM_DESC(b44_debug, "B44 bitmapped debugging message enable value");
  89. #ifdef CONFIG_B44_PCI
  90. static const struct pci_device_id b44_pci_tbl[] = {
  91. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_BCM4401) },
  92. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_BCM4401B0) },
  93. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_BCM4401B1) },
  94. { 0 } /* terminate list with empty entry */
  95. };
  96. MODULE_DEVICE_TABLE(pci, b44_pci_tbl);
  97. static struct pci_driver b44_pci_driver = {
  98. .name = DRV_MODULE_NAME,
  99. .id_table = b44_pci_tbl,
  100. };
  101. #endif /* CONFIG_B44_PCI */
  102. static const struct ssb_device_id b44_ssb_tbl[] = {
  103. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_ETHERNET, SSB_ANY_REV),
  104. {},
  105. };
  106. MODULE_DEVICE_TABLE(ssb, b44_ssb_tbl);
  107. static void b44_halt(struct b44 *);
  108. static void b44_init_rings(struct b44 *);
  109. #define B44_FULL_RESET 1
  110. #define B44_FULL_RESET_SKIP_PHY 2
  111. #define B44_PARTIAL_RESET 3
  112. #define B44_CHIP_RESET_FULL 4
  113. #define B44_CHIP_RESET_PARTIAL 5
  114. static void b44_init_hw(struct b44 *, int);
  115. static int dma_desc_sync_size;
  116. static int instance;
  117. static const char b44_gstrings[][ETH_GSTRING_LEN] = {
  118. #define _B44(x...) # x,
  119. B44_STAT_REG_DECLARE
  120. #undef _B44
  121. };
  122. static inline void b44_sync_dma_desc_for_device(struct ssb_device *sdev,
  123. dma_addr_t dma_base,
  124. unsigned long offset,
  125. enum dma_data_direction dir)
  126. {
  127. dma_sync_single_for_device(sdev->dma_dev, dma_base + offset,
  128. dma_desc_sync_size, dir);
  129. }
  130. static inline void b44_sync_dma_desc_for_cpu(struct ssb_device *sdev,
  131. dma_addr_t dma_base,
  132. unsigned long offset,
  133. enum dma_data_direction dir)
  134. {
  135. dma_sync_single_for_cpu(sdev->dma_dev, dma_base + offset,
  136. dma_desc_sync_size, dir);
  137. }
  138. static inline unsigned long br32(const struct b44 *bp, unsigned long reg)
  139. {
  140. return ssb_read32(bp->sdev, reg);
  141. }
  142. static inline void bw32(const struct b44 *bp,
  143. unsigned long reg, unsigned long val)
  144. {
  145. ssb_write32(bp->sdev, reg, val);
  146. }
  147. static int b44_wait_bit(struct b44 *bp, unsigned long reg,
  148. u32 bit, unsigned long timeout, const int clear)
  149. {
  150. unsigned long i;
  151. for (i = 0; i < timeout; i++) {
  152. u32 val = br32(bp, reg);
  153. if (clear && !(val & bit))
  154. break;
  155. if (!clear && (val & bit))
  156. break;
  157. udelay(10);
  158. }
  159. if (i == timeout) {
  160. if (net_ratelimit())
  161. netdev_err(bp->dev, "BUG! Timeout waiting for bit %08x of register %lx to %s\n",
  162. bit, reg, clear ? "clear" : "set");
  163. return -ENODEV;
  164. }
  165. return 0;
  166. }
  167. static inline void __b44_cam_read(struct b44 *bp, unsigned char *data, int index)
  168. {
  169. u32 val;
  170. bw32(bp, B44_CAM_CTRL, (CAM_CTRL_READ |
  171. (index << CAM_CTRL_INDEX_SHIFT)));
  172. b44_wait_bit(bp, B44_CAM_CTRL, CAM_CTRL_BUSY, 100, 1);
  173. val = br32(bp, B44_CAM_DATA_LO);
  174. data[2] = (val >> 24) & 0xFF;
  175. data[3] = (val >> 16) & 0xFF;
  176. data[4] = (val >> 8) & 0xFF;
  177. data[5] = (val >> 0) & 0xFF;
  178. val = br32(bp, B44_CAM_DATA_HI);
  179. data[0] = (val >> 8) & 0xFF;
  180. data[1] = (val >> 0) & 0xFF;
  181. }
  182. static inline void __b44_cam_write(struct b44 *bp, unsigned char *data, int index)
  183. {
  184. u32 val;
  185. val = ((u32) data[2]) << 24;
  186. val |= ((u32) data[3]) << 16;
  187. val |= ((u32) data[4]) << 8;
  188. val |= ((u32) data[5]) << 0;
  189. bw32(bp, B44_CAM_DATA_LO, val);
  190. val = (CAM_DATA_HI_VALID |
  191. (((u32) data[0]) << 8) |
  192. (((u32) data[1]) << 0));
  193. bw32(bp, B44_CAM_DATA_HI, val);
  194. bw32(bp, B44_CAM_CTRL, (CAM_CTRL_WRITE |
  195. (index << CAM_CTRL_INDEX_SHIFT)));
  196. b44_wait_bit(bp, B44_CAM_CTRL, CAM_CTRL_BUSY, 100, 1);
  197. }
  198. static inline void __b44_disable_ints(struct b44 *bp)
  199. {
  200. bw32(bp, B44_IMASK, 0);
  201. }
  202. static void b44_disable_ints(struct b44 *bp)
  203. {
  204. __b44_disable_ints(bp);
  205. /* Flush posted writes. */
  206. br32(bp, B44_IMASK);
  207. }
  208. static void b44_enable_ints(struct b44 *bp)
  209. {
  210. bw32(bp, B44_IMASK, bp->imask);
  211. }
  212. static int __b44_readphy(struct b44 *bp, int phy_addr, int reg, u32 *val)
  213. {
  214. int err;
  215. bw32(bp, B44_EMAC_ISTAT, EMAC_INT_MII);
  216. bw32(bp, B44_MDIO_DATA, (MDIO_DATA_SB_START |
  217. (MDIO_OP_READ << MDIO_DATA_OP_SHIFT) |
  218. (phy_addr << MDIO_DATA_PMD_SHIFT) |
  219. (reg << MDIO_DATA_RA_SHIFT) |
  220. (MDIO_TA_VALID << MDIO_DATA_TA_SHIFT)));
  221. err = b44_wait_bit(bp, B44_EMAC_ISTAT, EMAC_INT_MII, 100, 0);
  222. *val = br32(bp, B44_MDIO_DATA) & MDIO_DATA_DATA;
  223. return err;
  224. }
  225. static int __b44_writephy(struct b44 *bp, int phy_addr, int reg, u32 val)
  226. {
  227. bw32(bp, B44_EMAC_ISTAT, EMAC_INT_MII);
  228. bw32(bp, B44_MDIO_DATA, (MDIO_DATA_SB_START |
  229. (MDIO_OP_WRITE << MDIO_DATA_OP_SHIFT) |
  230. (phy_addr << MDIO_DATA_PMD_SHIFT) |
  231. (reg << MDIO_DATA_RA_SHIFT) |
  232. (MDIO_TA_VALID << MDIO_DATA_TA_SHIFT) |
  233. (val & MDIO_DATA_DATA)));
  234. return b44_wait_bit(bp, B44_EMAC_ISTAT, EMAC_INT_MII, 100, 0);
  235. }
  236. static inline int b44_readphy(struct b44 *bp, int reg, u32 *val)
  237. {
  238. if (bp->flags & B44_FLAG_EXTERNAL_PHY)
  239. return 0;
  240. return __b44_readphy(bp, bp->phy_addr, reg, val);
  241. }
  242. static inline int b44_writephy(struct b44 *bp, int reg, u32 val)
  243. {
  244. if (bp->flags & B44_FLAG_EXTERNAL_PHY)
  245. return 0;
  246. return __b44_writephy(bp, bp->phy_addr, reg, val);
  247. }
  248. /* miilib interface */
  249. static int b44_mdio_read_mii(struct net_device *dev, int phy_id, int location)
  250. {
  251. u32 val;
  252. struct b44 *bp = netdev_priv(dev);
  253. int rc = __b44_readphy(bp, phy_id, location, &val);
  254. if (rc)
  255. return 0xffffffff;
  256. return val;
  257. }
  258. static void b44_mdio_write_mii(struct net_device *dev, int phy_id, int location,
  259. int val)
  260. {
  261. struct b44 *bp = netdev_priv(dev);
  262. __b44_writephy(bp, phy_id, location, val);
  263. }
  264. static int b44_mdio_read_phylib(struct mii_bus *bus, int phy_id, int location)
  265. {
  266. u32 val;
  267. struct b44 *bp = bus->priv;
  268. int rc = __b44_readphy(bp, phy_id, location, &val);
  269. if (rc)
  270. return 0xffffffff;
  271. return val;
  272. }
  273. static int b44_mdio_write_phylib(struct mii_bus *bus, int phy_id, int location,
  274. u16 val)
  275. {
  276. struct b44 *bp = bus->priv;
  277. return __b44_writephy(bp, phy_id, location, val);
  278. }
  279. static int b44_phy_reset(struct b44 *bp)
  280. {
  281. u32 val;
  282. int err;
  283. if (bp->flags & B44_FLAG_EXTERNAL_PHY)
  284. return 0;
  285. err = b44_writephy(bp, MII_BMCR, BMCR_RESET);
  286. if (err)
  287. return err;
  288. udelay(100);
  289. err = b44_readphy(bp, MII_BMCR, &val);
  290. if (!err) {
  291. if (val & BMCR_RESET) {
  292. netdev_err(bp->dev, "PHY Reset would not complete\n");
  293. err = -ENODEV;
  294. }
  295. }
  296. return err;
  297. }
  298. static void __b44_set_flow_ctrl(struct b44 *bp, u32 pause_flags)
  299. {
  300. u32 val;
  301. bp->flags &= ~(B44_FLAG_TX_PAUSE | B44_FLAG_RX_PAUSE);
  302. bp->flags |= pause_flags;
  303. val = br32(bp, B44_RXCONFIG);
  304. if (pause_flags & B44_FLAG_RX_PAUSE)
  305. val |= RXCONFIG_FLOW;
  306. else
  307. val &= ~RXCONFIG_FLOW;
  308. bw32(bp, B44_RXCONFIG, val);
  309. val = br32(bp, B44_MAC_FLOW);
  310. if (pause_flags & B44_FLAG_TX_PAUSE)
  311. val |= (MAC_FLOW_PAUSE_ENAB |
  312. (0xc0 & MAC_FLOW_RX_HI_WATER));
  313. else
  314. val &= ~MAC_FLOW_PAUSE_ENAB;
  315. bw32(bp, B44_MAC_FLOW, val);
  316. }
  317. static void b44_set_flow_ctrl(struct b44 *bp, u32 local, u32 remote)
  318. {
  319. u32 pause_enab = 0;
  320. /* The driver supports only rx pause by default because
  321. the b44 mac tx pause mechanism generates excessive
  322. pause frames.
  323. Use ethtool to turn on b44 tx pause if necessary.
  324. */
  325. if ((local & ADVERTISE_PAUSE_CAP) &&
  326. (local & ADVERTISE_PAUSE_ASYM)){
  327. if ((remote & LPA_PAUSE_ASYM) &&
  328. !(remote & LPA_PAUSE_CAP))
  329. pause_enab |= B44_FLAG_RX_PAUSE;
  330. }
  331. __b44_set_flow_ctrl(bp, pause_enab);
  332. }
  333. #ifdef CONFIG_BCM47XX
  334. #include <linux/bcm47xx_nvram.h>
  335. static void b44_wap54g10_workaround(struct b44 *bp)
  336. {
  337. char buf[20];
  338. u32 val;
  339. int err;
  340. /*
  341. * workaround for bad hardware design in Linksys WAP54G v1.0
  342. * see https://dev.openwrt.org/ticket/146
  343. * check and reset bit "isolate"
  344. */
  345. if (bcm47xx_nvram_getenv("boardnum", buf, sizeof(buf)) < 0)
  346. return;
  347. if (simple_strtoul(buf, NULL, 0) == 2) {
  348. err = __b44_readphy(bp, 0, MII_BMCR, &val);
  349. if (err)
  350. goto error;
  351. if (!(val & BMCR_ISOLATE))
  352. return;
  353. val &= ~BMCR_ISOLATE;
  354. err = __b44_writephy(bp, 0, MII_BMCR, val);
  355. if (err)
  356. goto error;
  357. }
  358. return;
  359. error:
  360. pr_warn("PHY: cannot reset MII transceiver isolate bit\n");
  361. }
  362. #else
  363. static inline void b44_wap54g10_workaround(struct b44 *bp)
  364. {
  365. }
  366. #endif
  367. static int b44_setup_phy(struct b44 *bp)
  368. {
  369. u32 val;
  370. int err;
  371. b44_wap54g10_workaround(bp);
  372. if (bp->flags & B44_FLAG_EXTERNAL_PHY)
  373. return 0;
  374. if ((err = b44_readphy(bp, B44_MII_ALEDCTRL, &val)) != 0)
  375. goto out;
  376. if ((err = b44_writephy(bp, B44_MII_ALEDCTRL,
  377. val & MII_ALEDCTRL_ALLMSK)) != 0)
  378. goto out;
  379. if ((err = b44_readphy(bp, B44_MII_TLEDCTRL, &val)) != 0)
  380. goto out;
  381. if ((err = b44_writephy(bp, B44_MII_TLEDCTRL,
  382. val | MII_TLEDCTRL_ENABLE)) != 0)
  383. goto out;
  384. if (!(bp->flags & B44_FLAG_FORCE_LINK)) {
  385. u32 adv = ADVERTISE_CSMA;
  386. if (bp->flags & B44_FLAG_ADV_10HALF)
  387. adv |= ADVERTISE_10HALF;
  388. if (bp->flags & B44_FLAG_ADV_10FULL)
  389. adv |= ADVERTISE_10FULL;
  390. if (bp->flags & B44_FLAG_ADV_100HALF)
  391. adv |= ADVERTISE_100HALF;
  392. if (bp->flags & B44_FLAG_ADV_100FULL)
  393. adv |= ADVERTISE_100FULL;
  394. if (bp->flags & B44_FLAG_PAUSE_AUTO)
  395. adv |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  396. if ((err = b44_writephy(bp, MII_ADVERTISE, adv)) != 0)
  397. goto out;
  398. if ((err = b44_writephy(bp, MII_BMCR, (BMCR_ANENABLE |
  399. BMCR_ANRESTART))) != 0)
  400. goto out;
  401. } else {
  402. u32 bmcr;
  403. if ((err = b44_readphy(bp, MII_BMCR, &bmcr)) != 0)
  404. goto out;
  405. bmcr &= ~(BMCR_FULLDPLX | BMCR_ANENABLE | BMCR_SPEED100);
  406. if (bp->flags & B44_FLAG_100_BASE_T)
  407. bmcr |= BMCR_SPEED100;
  408. if (bp->flags & B44_FLAG_FULL_DUPLEX)
  409. bmcr |= BMCR_FULLDPLX;
  410. if ((err = b44_writephy(bp, MII_BMCR, bmcr)) != 0)
  411. goto out;
  412. /* Since we will not be negotiating there is no safe way
  413. * to determine if the link partner supports flow control
  414. * or not. So just disable it completely in this case.
  415. */
  416. b44_set_flow_ctrl(bp, 0, 0);
  417. }
  418. out:
  419. return err;
  420. }
  421. static void b44_stats_update(struct b44 *bp)
  422. {
  423. unsigned long reg;
  424. u64 *val;
  425. val = &bp->hw_stats.tx_good_octets;
  426. u64_stats_update_begin(&bp->hw_stats.syncp);
  427. for (reg = B44_TX_GOOD_O; reg <= B44_TX_PAUSE; reg += 4UL) {
  428. *val++ += br32(bp, reg);
  429. }
  430. /* Pad */
  431. reg += 8*4UL;
  432. for (reg = B44_RX_GOOD_O; reg <= B44_RX_NPAUSE; reg += 4UL) {
  433. *val++ += br32(bp, reg);
  434. }
  435. u64_stats_update_end(&bp->hw_stats.syncp);
  436. }
  437. static void b44_link_report(struct b44 *bp)
  438. {
  439. if (!netif_carrier_ok(bp->dev)) {
  440. netdev_info(bp->dev, "Link is down\n");
  441. } else {
  442. netdev_info(bp->dev, "Link is up at %d Mbps, %s duplex\n",
  443. (bp->flags & B44_FLAG_100_BASE_T) ? 100 : 10,
  444. (bp->flags & B44_FLAG_FULL_DUPLEX) ? "full" : "half");
  445. netdev_info(bp->dev, "Flow control is %s for TX and %s for RX\n",
  446. (bp->flags & B44_FLAG_TX_PAUSE) ? "on" : "off",
  447. (bp->flags & B44_FLAG_RX_PAUSE) ? "on" : "off");
  448. }
  449. }
  450. static void b44_check_phy(struct b44 *bp)
  451. {
  452. u32 bmsr, aux;
  453. if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
  454. bp->flags |= B44_FLAG_100_BASE_T;
  455. if (!netif_carrier_ok(bp->dev)) {
  456. u32 val = br32(bp, B44_TX_CTRL);
  457. if (bp->flags & B44_FLAG_FULL_DUPLEX)
  458. val |= TX_CTRL_DUPLEX;
  459. else
  460. val &= ~TX_CTRL_DUPLEX;
  461. bw32(bp, B44_TX_CTRL, val);
  462. netif_carrier_on(bp->dev);
  463. b44_link_report(bp);
  464. }
  465. return;
  466. }
  467. if (!b44_readphy(bp, MII_BMSR, &bmsr) &&
  468. !b44_readphy(bp, B44_MII_AUXCTRL, &aux) &&
  469. (bmsr != 0xffff)) {
  470. if (aux & MII_AUXCTRL_SPEED)
  471. bp->flags |= B44_FLAG_100_BASE_T;
  472. else
  473. bp->flags &= ~B44_FLAG_100_BASE_T;
  474. if (aux & MII_AUXCTRL_DUPLEX)
  475. bp->flags |= B44_FLAG_FULL_DUPLEX;
  476. else
  477. bp->flags &= ~B44_FLAG_FULL_DUPLEX;
  478. if (!netif_carrier_ok(bp->dev) &&
  479. (bmsr & BMSR_LSTATUS)) {
  480. u32 val = br32(bp, B44_TX_CTRL);
  481. u32 local_adv, remote_adv;
  482. if (bp->flags & B44_FLAG_FULL_DUPLEX)
  483. val |= TX_CTRL_DUPLEX;
  484. else
  485. val &= ~TX_CTRL_DUPLEX;
  486. bw32(bp, B44_TX_CTRL, val);
  487. if (!(bp->flags & B44_FLAG_FORCE_LINK) &&
  488. !b44_readphy(bp, MII_ADVERTISE, &local_adv) &&
  489. !b44_readphy(bp, MII_LPA, &remote_adv))
  490. b44_set_flow_ctrl(bp, local_adv, remote_adv);
  491. /* Link now up */
  492. netif_carrier_on(bp->dev);
  493. b44_link_report(bp);
  494. } else if (netif_carrier_ok(bp->dev) && !(bmsr & BMSR_LSTATUS)) {
  495. /* Link now down */
  496. netif_carrier_off(bp->dev);
  497. b44_link_report(bp);
  498. }
  499. if (bmsr & BMSR_RFAULT)
  500. netdev_warn(bp->dev, "Remote fault detected in PHY\n");
  501. if (bmsr & BMSR_JCD)
  502. netdev_warn(bp->dev, "Jabber detected in PHY\n");
  503. }
  504. }
  505. static void b44_timer(struct timer_list *t)
  506. {
  507. struct b44 *bp = from_timer(bp, t, timer);
  508. spin_lock_irq(&bp->lock);
  509. b44_check_phy(bp);
  510. b44_stats_update(bp);
  511. spin_unlock_irq(&bp->lock);
  512. mod_timer(&bp->timer, round_jiffies(jiffies + HZ));
  513. }
  514. static void b44_tx(struct b44 *bp)
  515. {
  516. u32 cur, cons;
  517. unsigned bytes_compl = 0, pkts_compl = 0;
  518. cur = br32(bp, B44_DMATX_STAT) & DMATX_STAT_CDMASK;
  519. cur /= sizeof(struct dma_desc);
  520. /* XXX needs updating when NETIF_F_SG is supported */
  521. for (cons = bp->tx_cons; cons != cur; cons = NEXT_TX(cons)) {
  522. struct ring_info *rp = &bp->tx_buffers[cons];
  523. struct sk_buff *skb = rp->skb;
  524. BUG_ON(skb == NULL);
  525. dma_unmap_single(bp->sdev->dma_dev,
  526. rp->mapping,
  527. skb->len,
  528. DMA_TO_DEVICE);
  529. rp->skb = NULL;
  530. bytes_compl += skb->len;
  531. pkts_compl++;
  532. dev_kfree_skb_irq(skb);
  533. }
  534. netdev_completed_queue(bp->dev, pkts_compl, bytes_compl);
  535. bp->tx_cons = cons;
  536. if (netif_queue_stopped(bp->dev) &&
  537. TX_BUFFS_AVAIL(bp) > B44_TX_WAKEUP_THRESH)
  538. netif_wake_queue(bp->dev);
  539. bw32(bp, B44_GPTIMER, 0);
  540. }
  541. /* Works like this. This chip writes a 'struct rx_header" 30 bytes
  542. * before the DMA address you give it. So we allocate 30 more bytes
  543. * for the RX buffer, DMA map all of it, skb_reserve the 30 bytes, then
  544. * point the chip at 30 bytes past where the rx_header will go.
  545. */
  546. static int b44_alloc_rx_skb(struct b44 *bp, int src_idx, u32 dest_idx_unmasked)
  547. {
  548. struct dma_desc *dp;
  549. struct ring_info *src_map, *map;
  550. struct rx_header *rh;
  551. struct sk_buff *skb;
  552. dma_addr_t mapping;
  553. int dest_idx;
  554. u32 ctrl;
  555. src_map = NULL;
  556. if (src_idx >= 0)
  557. src_map = &bp->rx_buffers[src_idx];
  558. dest_idx = dest_idx_unmasked & (B44_RX_RING_SIZE - 1);
  559. map = &bp->rx_buffers[dest_idx];
  560. skb = netdev_alloc_skb(bp->dev, RX_PKT_BUF_SZ);
  561. if (skb == NULL)
  562. return -ENOMEM;
  563. mapping = dma_map_single(bp->sdev->dma_dev, skb->data,
  564. RX_PKT_BUF_SZ,
  565. DMA_FROM_DEVICE);
  566. /* Hardware bug work-around, the chip is unable to do PCI DMA
  567. to/from anything above 1GB :-( */
  568. if (dma_mapping_error(bp->sdev->dma_dev, mapping) ||
  569. mapping + RX_PKT_BUF_SZ > DMA_BIT_MASK(30)) {
  570. /* Sigh... */
  571. if (!dma_mapping_error(bp->sdev->dma_dev, mapping))
  572. dma_unmap_single(bp->sdev->dma_dev, mapping,
  573. RX_PKT_BUF_SZ, DMA_FROM_DEVICE);
  574. dev_kfree_skb_any(skb);
  575. skb = alloc_skb(RX_PKT_BUF_SZ, GFP_ATOMIC | GFP_DMA);
  576. if (skb == NULL)
  577. return -ENOMEM;
  578. mapping = dma_map_single(bp->sdev->dma_dev, skb->data,
  579. RX_PKT_BUF_SZ,
  580. DMA_FROM_DEVICE);
  581. if (dma_mapping_error(bp->sdev->dma_dev, mapping) ||
  582. mapping + RX_PKT_BUF_SZ > DMA_BIT_MASK(30)) {
  583. if (!dma_mapping_error(bp->sdev->dma_dev, mapping))
  584. dma_unmap_single(bp->sdev->dma_dev, mapping, RX_PKT_BUF_SZ,DMA_FROM_DEVICE);
  585. dev_kfree_skb_any(skb);
  586. return -ENOMEM;
  587. }
  588. bp->force_copybreak = 1;
  589. }
  590. rh = (struct rx_header *) skb->data;
  591. rh->len = 0;
  592. rh->flags = 0;
  593. map->skb = skb;
  594. map->mapping = mapping;
  595. if (src_map != NULL)
  596. src_map->skb = NULL;
  597. ctrl = (DESC_CTRL_LEN & RX_PKT_BUF_SZ);
  598. if (dest_idx == (B44_RX_RING_SIZE - 1))
  599. ctrl |= DESC_CTRL_EOT;
  600. dp = &bp->rx_ring[dest_idx];
  601. dp->ctrl = cpu_to_le32(ctrl);
  602. dp->addr = cpu_to_le32((u32) mapping + bp->dma_offset);
  603. if (bp->flags & B44_FLAG_RX_RING_HACK)
  604. b44_sync_dma_desc_for_device(bp->sdev, bp->rx_ring_dma,
  605. dest_idx * sizeof(*dp),
  606. DMA_BIDIRECTIONAL);
  607. return RX_PKT_BUF_SZ;
  608. }
  609. static void b44_recycle_rx(struct b44 *bp, int src_idx, u32 dest_idx_unmasked)
  610. {
  611. struct dma_desc *src_desc, *dest_desc;
  612. struct ring_info *src_map, *dest_map;
  613. struct rx_header *rh;
  614. int dest_idx;
  615. __le32 ctrl;
  616. dest_idx = dest_idx_unmasked & (B44_RX_RING_SIZE - 1);
  617. dest_desc = &bp->rx_ring[dest_idx];
  618. dest_map = &bp->rx_buffers[dest_idx];
  619. src_desc = &bp->rx_ring[src_idx];
  620. src_map = &bp->rx_buffers[src_idx];
  621. dest_map->skb = src_map->skb;
  622. rh = (struct rx_header *) src_map->skb->data;
  623. rh->len = 0;
  624. rh->flags = 0;
  625. dest_map->mapping = src_map->mapping;
  626. if (bp->flags & B44_FLAG_RX_RING_HACK)
  627. b44_sync_dma_desc_for_cpu(bp->sdev, bp->rx_ring_dma,
  628. src_idx * sizeof(*src_desc),
  629. DMA_BIDIRECTIONAL);
  630. ctrl = src_desc->ctrl;
  631. if (dest_idx == (B44_RX_RING_SIZE - 1))
  632. ctrl |= cpu_to_le32(DESC_CTRL_EOT);
  633. else
  634. ctrl &= cpu_to_le32(~DESC_CTRL_EOT);
  635. dest_desc->ctrl = ctrl;
  636. dest_desc->addr = src_desc->addr;
  637. src_map->skb = NULL;
  638. if (bp->flags & B44_FLAG_RX_RING_HACK)
  639. b44_sync_dma_desc_for_device(bp->sdev, bp->rx_ring_dma,
  640. dest_idx * sizeof(*dest_desc),
  641. DMA_BIDIRECTIONAL);
  642. dma_sync_single_for_device(bp->sdev->dma_dev, dest_map->mapping,
  643. RX_PKT_BUF_SZ,
  644. DMA_FROM_DEVICE);
  645. }
  646. static int b44_rx(struct b44 *bp, int budget)
  647. {
  648. int received;
  649. u32 cons, prod;
  650. received = 0;
  651. prod = br32(bp, B44_DMARX_STAT) & DMARX_STAT_CDMASK;
  652. prod /= sizeof(struct dma_desc);
  653. cons = bp->rx_cons;
  654. while (cons != prod && budget > 0) {
  655. struct ring_info *rp = &bp->rx_buffers[cons];
  656. struct sk_buff *skb = rp->skb;
  657. dma_addr_t map = rp->mapping;
  658. struct rx_header *rh;
  659. u16 len;
  660. dma_sync_single_for_cpu(bp->sdev->dma_dev, map,
  661. RX_PKT_BUF_SZ,
  662. DMA_FROM_DEVICE);
  663. rh = (struct rx_header *) skb->data;
  664. len = le16_to_cpu(rh->len);
  665. if ((len > (RX_PKT_BUF_SZ - RX_PKT_OFFSET)) ||
  666. (rh->flags & cpu_to_le16(RX_FLAG_ERRORS))) {
  667. drop_it:
  668. b44_recycle_rx(bp, cons, bp->rx_prod);
  669. drop_it_no_recycle:
  670. bp->dev->stats.rx_dropped++;
  671. goto next_pkt;
  672. }
  673. if (len == 0) {
  674. int i = 0;
  675. do {
  676. udelay(2);
  677. barrier();
  678. len = le16_to_cpu(rh->len);
  679. } while (len == 0 && i++ < 5);
  680. if (len == 0)
  681. goto drop_it;
  682. }
  683. /* Omit CRC. */
  684. len -= 4;
  685. if (!bp->force_copybreak && len > RX_COPY_THRESHOLD) {
  686. int skb_size;
  687. skb_size = b44_alloc_rx_skb(bp, cons, bp->rx_prod);
  688. if (skb_size < 0)
  689. goto drop_it;
  690. dma_unmap_single(bp->sdev->dma_dev, map,
  691. skb_size, DMA_FROM_DEVICE);
  692. /* Leave out rx_header */
  693. skb_put(skb, len + RX_PKT_OFFSET);
  694. skb_pull(skb, RX_PKT_OFFSET);
  695. } else {
  696. struct sk_buff *copy_skb;
  697. b44_recycle_rx(bp, cons, bp->rx_prod);
  698. copy_skb = napi_alloc_skb(&bp->napi, len);
  699. if (copy_skb == NULL)
  700. goto drop_it_no_recycle;
  701. skb_put(copy_skb, len);
  702. /* DMA sync done above, copy just the actual packet */
  703. skb_copy_from_linear_data_offset(skb, RX_PKT_OFFSET,
  704. copy_skb->data, len);
  705. skb = copy_skb;
  706. }
  707. skb_checksum_none_assert(skb);
  708. skb->protocol = eth_type_trans(skb, bp->dev);
  709. netif_receive_skb(skb);
  710. received++;
  711. budget--;
  712. next_pkt:
  713. bp->rx_prod = (bp->rx_prod + 1) &
  714. (B44_RX_RING_SIZE - 1);
  715. cons = (cons + 1) & (B44_RX_RING_SIZE - 1);
  716. }
  717. bp->rx_cons = cons;
  718. bw32(bp, B44_DMARX_PTR, cons * sizeof(struct dma_desc));
  719. return received;
  720. }
  721. static int b44_poll(struct napi_struct *napi, int budget)
  722. {
  723. struct b44 *bp = container_of(napi, struct b44, napi);
  724. int work_done;
  725. unsigned long flags;
  726. spin_lock_irqsave(&bp->lock, flags);
  727. if (bp->istat & (ISTAT_TX | ISTAT_TO)) {
  728. /* spin_lock(&bp->tx_lock); */
  729. b44_tx(bp);
  730. /* spin_unlock(&bp->tx_lock); */
  731. }
  732. if (bp->istat & ISTAT_RFO) { /* fast recovery, in ~20msec */
  733. bp->istat &= ~ISTAT_RFO;
  734. b44_disable_ints(bp);
  735. ssb_device_enable(bp->sdev, 0); /* resets ISTAT_RFO */
  736. b44_init_rings(bp);
  737. b44_init_hw(bp, B44_FULL_RESET_SKIP_PHY);
  738. netif_wake_queue(bp->dev);
  739. }
  740. spin_unlock_irqrestore(&bp->lock, flags);
  741. work_done = 0;
  742. if (bp->istat & ISTAT_RX)
  743. work_done += b44_rx(bp, budget);
  744. if (bp->istat & ISTAT_ERRORS) {
  745. spin_lock_irqsave(&bp->lock, flags);
  746. b44_halt(bp);
  747. b44_init_rings(bp);
  748. b44_init_hw(bp, B44_FULL_RESET_SKIP_PHY);
  749. netif_wake_queue(bp->dev);
  750. spin_unlock_irqrestore(&bp->lock, flags);
  751. work_done = 0;
  752. }
  753. if (work_done < budget) {
  754. napi_complete_done(napi, work_done);
  755. b44_enable_ints(bp);
  756. }
  757. return work_done;
  758. }
  759. static irqreturn_t b44_interrupt(int irq, void *dev_id)
  760. {
  761. struct net_device *dev = dev_id;
  762. struct b44 *bp = netdev_priv(dev);
  763. u32 istat, imask;
  764. int handled = 0;
  765. spin_lock(&bp->lock);
  766. istat = br32(bp, B44_ISTAT);
  767. imask = br32(bp, B44_IMASK);
  768. /* The interrupt mask register controls which interrupt bits
  769. * will actually raise an interrupt to the CPU when set by hw/firmware,
  770. * but doesn't mask off the bits.
  771. */
  772. istat &= imask;
  773. if (istat) {
  774. handled = 1;
  775. if (unlikely(!netif_running(dev))) {
  776. netdev_info(dev, "late interrupt\n");
  777. goto irq_ack;
  778. }
  779. if (napi_schedule_prep(&bp->napi)) {
  780. /* NOTE: These writes are posted by the readback of
  781. * the ISTAT register below.
  782. */
  783. bp->istat = istat;
  784. __b44_disable_ints(bp);
  785. __napi_schedule(&bp->napi);
  786. }
  787. irq_ack:
  788. bw32(bp, B44_ISTAT, istat);
  789. br32(bp, B44_ISTAT);
  790. }
  791. spin_unlock(&bp->lock);
  792. return IRQ_RETVAL(handled);
  793. }
  794. static void b44_tx_timeout(struct net_device *dev)
  795. {
  796. struct b44 *bp = netdev_priv(dev);
  797. netdev_err(dev, "transmit timed out, resetting\n");
  798. spin_lock_irq(&bp->lock);
  799. b44_halt(bp);
  800. b44_init_rings(bp);
  801. b44_init_hw(bp, B44_FULL_RESET);
  802. spin_unlock_irq(&bp->lock);
  803. b44_enable_ints(bp);
  804. netif_wake_queue(dev);
  805. }
  806. static netdev_tx_t b44_start_xmit(struct sk_buff *skb, struct net_device *dev)
  807. {
  808. struct b44 *bp = netdev_priv(dev);
  809. int rc = NETDEV_TX_OK;
  810. dma_addr_t mapping;
  811. u32 len, entry, ctrl;
  812. unsigned long flags;
  813. len = skb->len;
  814. spin_lock_irqsave(&bp->lock, flags);
  815. /* This is a hard error, log it. */
  816. if (unlikely(TX_BUFFS_AVAIL(bp) < 1)) {
  817. netif_stop_queue(dev);
  818. netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
  819. goto err_out;
  820. }
  821. mapping = dma_map_single(bp->sdev->dma_dev, skb->data, len, DMA_TO_DEVICE);
  822. if (dma_mapping_error(bp->sdev->dma_dev, mapping) || mapping + len > DMA_BIT_MASK(30)) {
  823. struct sk_buff *bounce_skb;
  824. /* Chip can't handle DMA to/from >1GB, use bounce buffer */
  825. if (!dma_mapping_error(bp->sdev->dma_dev, mapping))
  826. dma_unmap_single(bp->sdev->dma_dev, mapping, len,
  827. DMA_TO_DEVICE);
  828. bounce_skb = alloc_skb(len, GFP_ATOMIC | GFP_DMA);
  829. if (!bounce_skb)
  830. goto err_out;
  831. mapping = dma_map_single(bp->sdev->dma_dev, bounce_skb->data,
  832. len, DMA_TO_DEVICE);
  833. if (dma_mapping_error(bp->sdev->dma_dev, mapping) || mapping + len > DMA_BIT_MASK(30)) {
  834. if (!dma_mapping_error(bp->sdev->dma_dev, mapping))
  835. dma_unmap_single(bp->sdev->dma_dev, mapping,
  836. len, DMA_TO_DEVICE);
  837. dev_kfree_skb_any(bounce_skb);
  838. goto err_out;
  839. }
  840. skb_copy_from_linear_data(skb, skb_put(bounce_skb, len), len);
  841. dev_kfree_skb_any(skb);
  842. skb = bounce_skb;
  843. }
  844. entry = bp->tx_prod;
  845. bp->tx_buffers[entry].skb = skb;
  846. bp->tx_buffers[entry].mapping = mapping;
  847. ctrl = (len & DESC_CTRL_LEN);
  848. ctrl |= DESC_CTRL_IOC | DESC_CTRL_SOF | DESC_CTRL_EOF;
  849. if (entry == (B44_TX_RING_SIZE - 1))
  850. ctrl |= DESC_CTRL_EOT;
  851. bp->tx_ring[entry].ctrl = cpu_to_le32(ctrl);
  852. bp->tx_ring[entry].addr = cpu_to_le32((u32) mapping+bp->dma_offset);
  853. if (bp->flags & B44_FLAG_TX_RING_HACK)
  854. b44_sync_dma_desc_for_device(bp->sdev, bp->tx_ring_dma,
  855. entry * sizeof(bp->tx_ring[0]),
  856. DMA_TO_DEVICE);
  857. entry = NEXT_TX(entry);
  858. bp->tx_prod = entry;
  859. wmb();
  860. bw32(bp, B44_DMATX_PTR, entry * sizeof(struct dma_desc));
  861. if (bp->flags & B44_FLAG_BUGGY_TXPTR)
  862. bw32(bp, B44_DMATX_PTR, entry * sizeof(struct dma_desc));
  863. if (bp->flags & B44_FLAG_REORDER_BUG)
  864. br32(bp, B44_DMATX_PTR);
  865. netdev_sent_queue(dev, skb->len);
  866. if (TX_BUFFS_AVAIL(bp) < 1)
  867. netif_stop_queue(dev);
  868. out_unlock:
  869. spin_unlock_irqrestore(&bp->lock, flags);
  870. return rc;
  871. err_out:
  872. rc = NETDEV_TX_BUSY;
  873. goto out_unlock;
  874. }
  875. static int b44_change_mtu(struct net_device *dev, int new_mtu)
  876. {
  877. struct b44 *bp = netdev_priv(dev);
  878. if (!netif_running(dev)) {
  879. /* We'll just catch it later when the
  880. * device is up'd.
  881. */
  882. dev->mtu = new_mtu;
  883. return 0;
  884. }
  885. spin_lock_irq(&bp->lock);
  886. b44_halt(bp);
  887. dev->mtu = new_mtu;
  888. b44_init_rings(bp);
  889. b44_init_hw(bp, B44_FULL_RESET);
  890. spin_unlock_irq(&bp->lock);
  891. b44_enable_ints(bp);
  892. return 0;
  893. }
  894. /* Free up pending packets in all rx/tx rings.
  895. *
  896. * The chip has been shut down and the driver detached from
  897. * the networking, so no interrupts or new tx packets will
  898. * end up in the driver. bp->lock is not held and we are not
  899. * in an interrupt context and thus may sleep.
  900. */
  901. static void b44_free_rings(struct b44 *bp)
  902. {
  903. struct ring_info *rp;
  904. int i;
  905. for (i = 0; i < B44_RX_RING_SIZE; i++) {
  906. rp = &bp->rx_buffers[i];
  907. if (rp->skb == NULL)
  908. continue;
  909. dma_unmap_single(bp->sdev->dma_dev, rp->mapping, RX_PKT_BUF_SZ,
  910. DMA_FROM_DEVICE);
  911. dev_kfree_skb_any(rp->skb);
  912. rp->skb = NULL;
  913. }
  914. /* XXX needs changes once NETIF_F_SG is set... */
  915. for (i = 0; i < B44_TX_RING_SIZE; i++) {
  916. rp = &bp->tx_buffers[i];
  917. if (rp->skb == NULL)
  918. continue;
  919. dma_unmap_single(bp->sdev->dma_dev, rp->mapping, rp->skb->len,
  920. DMA_TO_DEVICE);
  921. dev_kfree_skb_any(rp->skb);
  922. rp->skb = NULL;
  923. }
  924. }
  925. /* Initialize tx/rx rings for packet processing.
  926. *
  927. * The chip has been shut down and the driver detached from
  928. * the networking, so no interrupts or new tx packets will
  929. * end up in the driver.
  930. */
  931. static void b44_init_rings(struct b44 *bp)
  932. {
  933. int i;
  934. b44_free_rings(bp);
  935. memset(bp->rx_ring, 0, B44_RX_RING_BYTES);
  936. memset(bp->tx_ring, 0, B44_TX_RING_BYTES);
  937. if (bp->flags & B44_FLAG_RX_RING_HACK)
  938. dma_sync_single_for_device(bp->sdev->dma_dev, bp->rx_ring_dma,
  939. DMA_TABLE_BYTES, DMA_BIDIRECTIONAL);
  940. if (bp->flags & B44_FLAG_TX_RING_HACK)
  941. dma_sync_single_for_device(bp->sdev->dma_dev, bp->tx_ring_dma,
  942. DMA_TABLE_BYTES, DMA_TO_DEVICE);
  943. for (i = 0; i < bp->rx_pending; i++) {
  944. if (b44_alloc_rx_skb(bp, -1, i) < 0)
  945. break;
  946. }
  947. }
  948. /*
  949. * Must not be invoked with interrupt sources disabled and
  950. * the hardware shutdown down.
  951. */
  952. static void b44_free_consistent(struct b44 *bp)
  953. {
  954. kfree(bp->rx_buffers);
  955. bp->rx_buffers = NULL;
  956. kfree(bp->tx_buffers);
  957. bp->tx_buffers = NULL;
  958. if (bp->rx_ring) {
  959. if (bp->flags & B44_FLAG_RX_RING_HACK) {
  960. dma_unmap_single(bp->sdev->dma_dev, bp->rx_ring_dma,
  961. DMA_TABLE_BYTES, DMA_BIDIRECTIONAL);
  962. kfree(bp->rx_ring);
  963. } else
  964. dma_free_coherent(bp->sdev->dma_dev, DMA_TABLE_BYTES,
  965. bp->rx_ring, bp->rx_ring_dma);
  966. bp->rx_ring = NULL;
  967. bp->flags &= ~B44_FLAG_RX_RING_HACK;
  968. }
  969. if (bp->tx_ring) {
  970. if (bp->flags & B44_FLAG_TX_RING_HACK) {
  971. dma_unmap_single(bp->sdev->dma_dev, bp->tx_ring_dma,
  972. DMA_TABLE_BYTES, DMA_TO_DEVICE);
  973. kfree(bp->tx_ring);
  974. } else
  975. dma_free_coherent(bp->sdev->dma_dev, DMA_TABLE_BYTES,
  976. bp->tx_ring, bp->tx_ring_dma);
  977. bp->tx_ring = NULL;
  978. bp->flags &= ~B44_FLAG_TX_RING_HACK;
  979. }
  980. }
  981. /*
  982. * Must not be invoked with interrupt sources disabled and
  983. * the hardware shutdown down. Can sleep.
  984. */
  985. static int b44_alloc_consistent(struct b44 *bp, gfp_t gfp)
  986. {
  987. int size;
  988. size = B44_RX_RING_SIZE * sizeof(struct ring_info);
  989. bp->rx_buffers = kzalloc(size, gfp);
  990. if (!bp->rx_buffers)
  991. goto out_err;
  992. size = B44_TX_RING_SIZE * sizeof(struct ring_info);
  993. bp->tx_buffers = kzalloc(size, gfp);
  994. if (!bp->tx_buffers)
  995. goto out_err;
  996. size = DMA_TABLE_BYTES;
  997. bp->rx_ring = dma_alloc_coherent(bp->sdev->dma_dev, size,
  998. &bp->rx_ring_dma, gfp);
  999. if (!bp->rx_ring) {
  1000. /* Allocation may have failed due to pci_alloc_consistent
  1001. insisting on use of GFP_DMA, which is more restrictive
  1002. than necessary... */
  1003. struct dma_desc *rx_ring;
  1004. dma_addr_t rx_ring_dma;
  1005. rx_ring = kzalloc(size, gfp);
  1006. if (!rx_ring)
  1007. goto out_err;
  1008. rx_ring_dma = dma_map_single(bp->sdev->dma_dev, rx_ring,
  1009. DMA_TABLE_BYTES,
  1010. DMA_BIDIRECTIONAL);
  1011. if (dma_mapping_error(bp->sdev->dma_dev, rx_ring_dma) ||
  1012. rx_ring_dma + size > DMA_BIT_MASK(30)) {
  1013. kfree(rx_ring);
  1014. goto out_err;
  1015. }
  1016. bp->rx_ring = rx_ring;
  1017. bp->rx_ring_dma = rx_ring_dma;
  1018. bp->flags |= B44_FLAG_RX_RING_HACK;
  1019. }
  1020. bp->tx_ring = dma_alloc_coherent(bp->sdev->dma_dev, size,
  1021. &bp->tx_ring_dma, gfp);
  1022. if (!bp->tx_ring) {
  1023. /* Allocation may have failed due to ssb_dma_alloc_consistent
  1024. insisting on use of GFP_DMA, which is more restrictive
  1025. than necessary... */
  1026. struct dma_desc *tx_ring;
  1027. dma_addr_t tx_ring_dma;
  1028. tx_ring = kzalloc(size, gfp);
  1029. if (!tx_ring)
  1030. goto out_err;
  1031. tx_ring_dma = dma_map_single(bp->sdev->dma_dev, tx_ring,
  1032. DMA_TABLE_BYTES,
  1033. DMA_TO_DEVICE);
  1034. if (dma_mapping_error(bp->sdev->dma_dev, tx_ring_dma) ||
  1035. tx_ring_dma + size > DMA_BIT_MASK(30)) {
  1036. kfree(tx_ring);
  1037. goto out_err;
  1038. }
  1039. bp->tx_ring = tx_ring;
  1040. bp->tx_ring_dma = tx_ring_dma;
  1041. bp->flags |= B44_FLAG_TX_RING_HACK;
  1042. }
  1043. return 0;
  1044. out_err:
  1045. b44_free_consistent(bp);
  1046. return -ENOMEM;
  1047. }
  1048. /* bp->lock is held. */
  1049. static void b44_clear_stats(struct b44 *bp)
  1050. {
  1051. unsigned long reg;
  1052. bw32(bp, B44_MIB_CTRL, MIB_CTRL_CLR_ON_READ);
  1053. for (reg = B44_TX_GOOD_O; reg <= B44_TX_PAUSE; reg += 4UL)
  1054. br32(bp, reg);
  1055. for (reg = B44_RX_GOOD_O; reg <= B44_RX_NPAUSE; reg += 4UL)
  1056. br32(bp, reg);
  1057. }
  1058. /* bp->lock is held. */
  1059. static void b44_chip_reset(struct b44 *bp, int reset_kind)
  1060. {
  1061. struct ssb_device *sdev = bp->sdev;
  1062. bool was_enabled;
  1063. was_enabled = ssb_device_is_enabled(bp->sdev);
  1064. ssb_device_enable(bp->sdev, 0);
  1065. ssb_pcicore_dev_irqvecs_enable(&sdev->bus->pcicore, sdev);
  1066. if (was_enabled) {
  1067. bw32(bp, B44_RCV_LAZY, 0);
  1068. bw32(bp, B44_ENET_CTRL, ENET_CTRL_DISABLE);
  1069. b44_wait_bit(bp, B44_ENET_CTRL, ENET_CTRL_DISABLE, 200, 1);
  1070. bw32(bp, B44_DMATX_CTRL, 0);
  1071. bp->tx_prod = bp->tx_cons = 0;
  1072. if (br32(bp, B44_DMARX_STAT) & DMARX_STAT_EMASK) {
  1073. b44_wait_bit(bp, B44_DMARX_STAT, DMARX_STAT_SIDLE,
  1074. 100, 0);
  1075. }
  1076. bw32(bp, B44_DMARX_CTRL, 0);
  1077. bp->rx_prod = bp->rx_cons = 0;
  1078. }
  1079. b44_clear_stats(bp);
  1080. /*
  1081. * Don't enable PHY if we are doing a partial reset
  1082. * we are probably going to power down
  1083. */
  1084. if (reset_kind == B44_CHIP_RESET_PARTIAL)
  1085. return;
  1086. switch (sdev->bus->bustype) {
  1087. case SSB_BUSTYPE_SSB:
  1088. bw32(bp, B44_MDIO_CTRL, (MDIO_CTRL_PREAMBLE |
  1089. (DIV_ROUND_CLOSEST(ssb_clockspeed(sdev->bus),
  1090. B44_MDC_RATIO)
  1091. & MDIO_CTRL_MAXF_MASK)));
  1092. break;
  1093. case SSB_BUSTYPE_PCI:
  1094. bw32(bp, B44_MDIO_CTRL, (MDIO_CTRL_PREAMBLE |
  1095. (0x0d & MDIO_CTRL_MAXF_MASK)));
  1096. break;
  1097. case SSB_BUSTYPE_PCMCIA:
  1098. case SSB_BUSTYPE_SDIO:
  1099. WARN_ON(1); /* A device with this bus does not exist. */
  1100. break;
  1101. }
  1102. br32(bp, B44_MDIO_CTRL);
  1103. if (!(br32(bp, B44_DEVCTRL) & DEVCTRL_IPP)) {
  1104. bw32(bp, B44_ENET_CTRL, ENET_CTRL_EPSEL);
  1105. br32(bp, B44_ENET_CTRL);
  1106. bp->flags |= B44_FLAG_EXTERNAL_PHY;
  1107. } else {
  1108. u32 val = br32(bp, B44_DEVCTRL);
  1109. if (val & DEVCTRL_EPR) {
  1110. bw32(bp, B44_DEVCTRL, (val & ~DEVCTRL_EPR));
  1111. br32(bp, B44_DEVCTRL);
  1112. udelay(100);
  1113. }
  1114. bp->flags &= ~B44_FLAG_EXTERNAL_PHY;
  1115. }
  1116. }
  1117. /* bp->lock is held. */
  1118. static void b44_halt(struct b44 *bp)
  1119. {
  1120. b44_disable_ints(bp);
  1121. /* reset PHY */
  1122. b44_phy_reset(bp);
  1123. /* power down PHY */
  1124. netdev_info(bp->dev, "powering down PHY\n");
  1125. bw32(bp, B44_MAC_CTRL, MAC_CTRL_PHY_PDOWN);
  1126. /* now reset the chip, but without enabling the MAC&PHY
  1127. * part of it. This has to be done _after_ we shut down the PHY */
  1128. if (bp->flags & B44_FLAG_EXTERNAL_PHY)
  1129. b44_chip_reset(bp, B44_CHIP_RESET_FULL);
  1130. else
  1131. b44_chip_reset(bp, B44_CHIP_RESET_PARTIAL);
  1132. }
  1133. /* bp->lock is held. */
  1134. static void __b44_set_mac_addr(struct b44 *bp)
  1135. {
  1136. bw32(bp, B44_CAM_CTRL, 0);
  1137. if (!(bp->dev->flags & IFF_PROMISC)) {
  1138. u32 val;
  1139. __b44_cam_write(bp, bp->dev->dev_addr, 0);
  1140. val = br32(bp, B44_CAM_CTRL);
  1141. bw32(bp, B44_CAM_CTRL, val | CAM_CTRL_ENABLE);
  1142. }
  1143. }
  1144. static int b44_set_mac_addr(struct net_device *dev, void *p)
  1145. {
  1146. struct b44 *bp = netdev_priv(dev);
  1147. struct sockaddr *addr = p;
  1148. u32 val;
  1149. if (netif_running(dev))
  1150. return -EBUSY;
  1151. if (!is_valid_ether_addr(addr->sa_data))
  1152. return -EINVAL;
  1153. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1154. spin_lock_irq(&bp->lock);
  1155. val = br32(bp, B44_RXCONFIG);
  1156. if (!(val & RXCONFIG_CAM_ABSENT))
  1157. __b44_set_mac_addr(bp);
  1158. spin_unlock_irq(&bp->lock);
  1159. return 0;
  1160. }
  1161. /* Called at device open time to get the chip ready for
  1162. * packet processing. Invoked with bp->lock held.
  1163. */
  1164. static void __b44_set_rx_mode(struct net_device *);
  1165. static void b44_init_hw(struct b44 *bp, int reset_kind)
  1166. {
  1167. u32 val;
  1168. b44_chip_reset(bp, B44_CHIP_RESET_FULL);
  1169. if (reset_kind == B44_FULL_RESET) {
  1170. b44_phy_reset(bp);
  1171. b44_setup_phy(bp);
  1172. }
  1173. /* Enable CRC32, set proper LED modes and power on PHY */
  1174. bw32(bp, B44_MAC_CTRL, MAC_CTRL_CRC32_ENAB | MAC_CTRL_PHY_LEDCTRL);
  1175. bw32(bp, B44_RCV_LAZY, (1 << RCV_LAZY_FC_SHIFT));
  1176. /* This sets the MAC address too. */
  1177. __b44_set_rx_mode(bp->dev);
  1178. /* MTU + eth header + possible VLAN tag + struct rx_header */
  1179. bw32(bp, B44_RXMAXLEN, bp->dev->mtu + ETH_HLEN + 8 + RX_HEADER_LEN);
  1180. bw32(bp, B44_TXMAXLEN, bp->dev->mtu + ETH_HLEN + 8 + RX_HEADER_LEN);
  1181. bw32(bp, B44_TX_WMARK, 56); /* XXX magic */
  1182. if (reset_kind == B44_PARTIAL_RESET) {
  1183. bw32(bp, B44_DMARX_CTRL, (DMARX_CTRL_ENABLE |
  1184. (RX_PKT_OFFSET << DMARX_CTRL_ROSHIFT)));
  1185. } else {
  1186. bw32(bp, B44_DMATX_CTRL, DMATX_CTRL_ENABLE);
  1187. bw32(bp, B44_DMATX_ADDR, bp->tx_ring_dma + bp->dma_offset);
  1188. bw32(bp, B44_DMARX_CTRL, (DMARX_CTRL_ENABLE |
  1189. (RX_PKT_OFFSET << DMARX_CTRL_ROSHIFT)));
  1190. bw32(bp, B44_DMARX_ADDR, bp->rx_ring_dma + bp->dma_offset);
  1191. bw32(bp, B44_DMARX_PTR, bp->rx_pending);
  1192. bp->rx_prod = bp->rx_pending;
  1193. bw32(bp, B44_MIB_CTRL, MIB_CTRL_CLR_ON_READ);
  1194. }
  1195. val = br32(bp, B44_ENET_CTRL);
  1196. bw32(bp, B44_ENET_CTRL, (val | ENET_CTRL_ENABLE));
  1197. netdev_reset_queue(bp->dev);
  1198. }
  1199. static int b44_open(struct net_device *dev)
  1200. {
  1201. struct b44 *bp = netdev_priv(dev);
  1202. int err;
  1203. err = b44_alloc_consistent(bp, GFP_KERNEL);
  1204. if (err)
  1205. goto out;
  1206. napi_enable(&bp->napi);
  1207. b44_init_rings(bp);
  1208. b44_init_hw(bp, B44_FULL_RESET);
  1209. b44_check_phy(bp);
  1210. err = request_irq(dev->irq, b44_interrupt, IRQF_SHARED, dev->name, dev);
  1211. if (unlikely(err < 0)) {
  1212. napi_disable(&bp->napi);
  1213. b44_chip_reset(bp, B44_CHIP_RESET_PARTIAL);
  1214. b44_free_rings(bp);
  1215. b44_free_consistent(bp);
  1216. goto out;
  1217. }
  1218. timer_setup(&bp->timer, b44_timer, 0);
  1219. bp->timer.expires = jiffies + HZ;
  1220. add_timer(&bp->timer);
  1221. b44_enable_ints(bp);
  1222. if (bp->flags & B44_FLAG_EXTERNAL_PHY)
  1223. phy_start(dev->phydev);
  1224. netif_start_queue(dev);
  1225. out:
  1226. return err;
  1227. }
  1228. #ifdef CONFIG_NET_POLL_CONTROLLER
  1229. /*
  1230. * Polling receive - used by netconsole and other diagnostic tools
  1231. * to allow network i/o with interrupts disabled.
  1232. */
  1233. static void b44_poll_controller(struct net_device *dev)
  1234. {
  1235. disable_irq(dev->irq);
  1236. b44_interrupt(dev->irq, dev);
  1237. enable_irq(dev->irq);
  1238. }
  1239. #endif
  1240. static void bwfilter_table(struct b44 *bp, u8 *pp, u32 bytes, u32 table_offset)
  1241. {
  1242. u32 i;
  1243. u32 *pattern = (u32 *) pp;
  1244. for (i = 0; i < bytes; i += sizeof(u32)) {
  1245. bw32(bp, B44_FILT_ADDR, table_offset + i);
  1246. bw32(bp, B44_FILT_DATA, pattern[i / sizeof(u32)]);
  1247. }
  1248. }
  1249. static int b44_magic_pattern(u8 *macaddr, u8 *ppattern, u8 *pmask, int offset)
  1250. {
  1251. int magicsync = 6;
  1252. int k, j, len = offset;
  1253. int ethaddr_bytes = ETH_ALEN;
  1254. memset(ppattern + offset, 0xff, magicsync);
  1255. for (j = 0; j < magicsync; j++) {
  1256. pmask[len >> 3] |= BIT(len & 7);
  1257. len++;
  1258. }
  1259. for (j = 0; j < B44_MAX_PATTERNS; j++) {
  1260. if ((B44_PATTERN_SIZE - len) >= ETH_ALEN)
  1261. ethaddr_bytes = ETH_ALEN;
  1262. else
  1263. ethaddr_bytes = B44_PATTERN_SIZE - len;
  1264. if (ethaddr_bytes <=0)
  1265. break;
  1266. for (k = 0; k< ethaddr_bytes; k++) {
  1267. ppattern[offset + magicsync +
  1268. (j * ETH_ALEN) + k] = macaddr[k];
  1269. pmask[len >> 3] |= BIT(len & 7);
  1270. len++;
  1271. }
  1272. }
  1273. return len - 1;
  1274. }
  1275. /* Setup magic packet patterns in the b44 WOL
  1276. * pattern matching filter.
  1277. */
  1278. static void b44_setup_pseudo_magicp(struct b44 *bp)
  1279. {
  1280. u32 val;
  1281. int plen0, plen1, plen2;
  1282. u8 *pwol_pattern;
  1283. u8 pwol_mask[B44_PMASK_SIZE];
  1284. pwol_pattern = kzalloc(B44_PATTERN_SIZE, GFP_KERNEL);
  1285. if (!pwol_pattern)
  1286. return;
  1287. /* Ipv4 magic packet pattern - pattern 0.*/
  1288. memset(pwol_mask, 0, B44_PMASK_SIZE);
  1289. plen0 = b44_magic_pattern(bp->dev->dev_addr, pwol_pattern, pwol_mask,
  1290. B44_ETHIPV4UDP_HLEN);
  1291. bwfilter_table(bp, pwol_pattern, B44_PATTERN_SIZE, B44_PATTERN_BASE);
  1292. bwfilter_table(bp, pwol_mask, B44_PMASK_SIZE, B44_PMASK_BASE);
  1293. /* Raw ethernet II magic packet pattern - pattern 1 */
  1294. memset(pwol_pattern, 0, B44_PATTERN_SIZE);
  1295. memset(pwol_mask, 0, B44_PMASK_SIZE);
  1296. plen1 = b44_magic_pattern(bp->dev->dev_addr, pwol_pattern, pwol_mask,
  1297. ETH_HLEN);
  1298. bwfilter_table(bp, pwol_pattern, B44_PATTERN_SIZE,
  1299. B44_PATTERN_BASE + B44_PATTERN_SIZE);
  1300. bwfilter_table(bp, pwol_mask, B44_PMASK_SIZE,
  1301. B44_PMASK_BASE + B44_PMASK_SIZE);
  1302. /* Ipv6 magic packet pattern - pattern 2 */
  1303. memset(pwol_pattern, 0, B44_PATTERN_SIZE);
  1304. memset(pwol_mask, 0, B44_PMASK_SIZE);
  1305. plen2 = b44_magic_pattern(bp->dev->dev_addr, pwol_pattern, pwol_mask,
  1306. B44_ETHIPV6UDP_HLEN);
  1307. bwfilter_table(bp, pwol_pattern, B44_PATTERN_SIZE,
  1308. B44_PATTERN_BASE + B44_PATTERN_SIZE + B44_PATTERN_SIZE);
  1309. bwfilter_table(bp, pwol_mask, B44_PMASK_SIZE,
  1310. B44_PMASK_BASE + B44_PMASK_SIZE + B44_PMASK_SIZE);
  1311. kfree(pwol_pattern);
  1312. /* set these pattern's lengths: one less than each real length */
  1313. val = plen0 | (plen1 << 8) | (plen2 << 16) | WKUP_LEN_ENABLE_THREE;
  1314. bw32(bp, B44_WKUP_LEN, val);
  1315. /* enable wakeup pattern matching */
  1316. val = br32(bp, B44_DEVCTRL);
  1317. bw32(bp, B44_DEVCTRL, val | DEVCTRL_PFE);
  1318. }
  1319. #ifdef CONFIG_B44_PCI
  1320. static void b44_setup_wol_pci(struct b44 *bp)
  1321. {
  1322. u16 val;
  1323. if (bp->sdev->bus->bustype != SSB_BUSTYPE_SSB) {
  1324. bw32(bp, SSB_TMSLOW, br32(bp, SSB_TMSLOW) | SSB_TMSLOW_PE);
  1325. pci_read_config_word(bp->sdev->bus->host_pci, SSB_PMCSR, &val);
  1326. pci_write_config_word(bp->sdev->bus->host_pci, SSB_PMCSR, val | SSB_PE);
  1327. }
  1328. }
  1329. #else
  1330. static inline void b44_setup_wol_pci(struct b44 *bp) { }
  1331. #endif /* CONFIG_B44_PCI */
  1332. static void b44_setup_wol(struct b44 *bp)
  1333. {
  1334. u32 val;
  1335. bw32(bp, B44_RXCONFIG, RXCONFIG_ALLMULTI);
  1336. if (bp->flags & B44_FLAG_B0_ANDLATER) {
  1337. bw32(bp, B44_WKUP_LEN, WKUP_LEN_DISABLE);
  1338. val = bp->dev->dev_addr[2] << 24 |
  1339. bp->dev->dev_addr[3] << 16 |
  1340. bp->dev->dev_addr[4] << 8 |
  1341. bp->dev->dev_addr[5];
  1342. bw32(bp, B44_ADDR_LO, val);
  1343. val = bp->dev->dev_addr[0] << 8 |
  1344. bp->dev->dev_addr[1];
  1345. bw32(bp, B44_ADDR_HI, val);
  1346. val = br32(bp, B44_DEVCTRL);
  1347. bw32(bp, B44_DEVCTRL, val | DEVCTRL_MPM | DEVCTRL_PFE);
  1348. } else {
  1349. b44_setup_pseudo_magicp(bp);
  1350. }
  1351. b44_setup_wol_pci(bp);
  1352. }
  1353. static int b44_close(struct net_device *dev)
  1354. {
  1355. struct b44 *bp = netdev_priv(dev);
  1356. netif_stop_queue(dev);
  1357. if (bp->flags & B44_FLAG_EXTERNAL_PHY)
  1358. phy_stop(dev->phydev);
  1359. napi_disable(&bp->napi);
  1360. del_timer_sync(&bp->timer);
  1361. spin_lock_irq(&bp->lock);
  1362. b44_halt(bp);
  1363. b44_free_rings(bp);
  1364. netif_carrier_off(dev);
  1365. spin_unlock_irq(&bp->lock);
  1366. free_irq(dev->irq, dev);
  1367. if (bp->flags & B44_FLAG_WOL_ENABLE) {
  1368. b44_init_hw(bp, B44_PARTIAL_RESET);
  1369. b44_setup_wol(bp);
  1370. }
  1371. b44_free_consistent(bp);
  1372. return 0;
  1373. }
  1374. static void b44_get_stats64(struct net_device *dev,
  1375. struct rtnl_link_stats64 *nstat)
  1376. {
  1377. struct b44 *bp = netdev_priv(dev);
  1378. struct b44_hw_stats *hwstat = &bp->hw_stats;
  1379. unsigned int start;
  1380. do {
  1381. start = u64_stats_fetch_begin_irq(&hwstat->syncp);
  1382. /* Convert HW stats into rtnl_link_stats64 stats. */
  1383. nstat->rx_packets = hwstat->rx_pkts;
  1384. nstat->tx_packets = hwstat->tx_pkts;
  1385. nstat->rx_bytes = hwstat->rx_octets;
  1386. nstat->tx_bytes = hwstat->tx_octets;
  1387. nstat->tx_errors = (hwstat->tx_jabber_pkts +
  1388. hwstat->tx_oversize_pkts +
  1389. hwstat->tx_underruns +
  1390. hwstat->tx_excessive_cols +
  1391. hwstat->tx_late_cols);
  1392. nstat->multicast = hwstat->rx_multicast_pkts;
  1393. nstat->collisions = hwstat->tx_total_cols;
  1394. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  1395. hwstat->rx_undersize);
  1396. nstat->rx_over_errors = hwstat->rx_missed_pkts;
  1397. nstat->rx_frame_errors = hwstat->rx_align_errs;
  1398. nstat->rx_crc_errors = hwstat->rx_crc_errs;
  1399. nstat->rx_errors = (hwstat->rx_jabber_pkts +
  1400. hwstat->rx_oversize_pkts +
  1401. hwstat->rx_missed_pkts +
  1402. hwstat->rx_crc_align_errs +
  1403. hwstat->rx_undersize +
  1404. hwstat->rx_crc_errs +
  1405. hwstat->rx_align_errs +
  1406. hwstat->rx_symbol_errs);
  1407. nstat->tx_aborted_errors = hwstat->tx_underruns;
  1408. #if 0
  1409. /* Carrier lost counter seems to be broken for some devices */
  1410. nstat->tx_carrier_errors = hwstat->tx_carrier_lost;
  1411. #endif
  1412. } while (u64_stats_fetch_retry_irq(&hwstat->syncp, start));
  1413. }
  1414. static int __b44_load_mcast(struct b44 *bp, struct net_device *dev)
  1415. {
  1416. struct netdev_hw_addr *ha;
  1417. int i, num_ents;
  1418. num_ents = min_t(int, netdev_mc_count(dev), B44_MCAST_TABLE_SIZE);
  1419. i = 0;
  1420. netdev_for_each_mc_addr(ha, dev) {
  1421. if (i == num_ents)
  1422. break;
  1423. __b44_cam_write(bp, ha->addr, i++ + 1);
  1424. }
  1425. return i+1;
  1426. }
  1427. static void __b44_set_rx_mode(struct net_device *dev)
  1428. {
  1429. struct b44 *bp = netdev_priv(dev);
  1430. u32 val;
  1431. val = br32(bp, B44_RXCONFIG);
  1432. val &= ~(RXCONFIG_PROMISC | RXCONFIG_ALLMULTI);
  1433. if ((dev->flags & IFF_PROMISC) || (val & RXCONFIG_CAM_ABSENT)) {
  1434. val |= RXCONFIG_PROMISC;
  1435. bw32(bp, B44_RXCONFIG, val);
  1436. } else {
  1437. unsigned char zero[6] = {0, 0, 0, 0, 0, 0};
  1438. int i = 1;
  1439. __b44_set_mac_addr(bp);
  1440. if ((dev->flags & IFF_ALLMULTI) ||
  1441. (netdev_mc_count(dev) > B44_MCAST_TABLE_SIZE))
  1442. val |= RXCONFIG_ALLMULTI;
  1443. else
  1444. i = __b44_load_mcast(bp, dev);
  1445. for (; i < 64; i++)
  1446. __b44_cam_write(bp, zero, i);
  1447. bw32(bp, B44_RXCONFIG, val);
  1448. val = br32(bp, B44_CAM_CTRL);
  1449. bw32(bp, B44_CAM_CTRL, val | CAM_CTRL_ENABLE);
  1450. }
  1451. }
  1452. static void b44_set_rx_mode(struct net_device *dev)
  1453. {
  1454. struct b44 *bp = netdev_priv(dev);
  1455. spin_lock_irq(&bp->lock);
  1456. __b44_set_rx_mode(dev);
  1457. spin_unlock_irq(&bp->lock);
  1458. }
  1459. static u32 b44_get_msglevel(struct net_device *dev)
  1460. {
  1461. struct b44 *bp = netdev_priv(dev);
  1462. return bp->msg_enable;
  1463. }
  1464. static void b44_set_msglevel(struct net_device *dev, u32 value)
  1465. {
  1466. struct b44 *bp = netdev_priv(dev);
  1467. bp->msg_enable = value;
  1468. }
  1469. static void b44_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
  1470. {
  1471. struct b44 *bp = netdev_priv(dev);
  1472. struct ssb_bus *bus = bp->sdev->bus;
  1473. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  1474. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  1475. switch (bus->bustype) {
  1476. case SSB_BUSTYPE_PCI:
  1477. strlcpy(info->bus_info, pci_name(bus->host_pci), sizeof(info->bus_info));
  1478. break;
  1479. case SSB_BUSTYPE_SSB:
  1480. strlcpy(info->bus_info, "SSB", sizeof(info->bus_info));
  1481. break;
  1482. case SSB_BUSTYPE_PCMCIA:
  1483. case SSB_BUSTYPE_SDIO:
  1484. WARN_ON(1); /* A device with this bus does not exist. */
  1485. break;
  1486. }
  1487. }
  1488. static int b44_nway_reset(struct net_device *dev)
  1489. {
  1490. struct b44 *bp = netdev_priv(dev);
  1491. u32 bmcr;
  1492. int r;
  1493. spin_lock_irq(&bp->lock);
  1494. b44_readphy(bp, MII_BMCR, &bmcr);
  1495. b44_readphy(bp, MII_BMCR, &bmcr);
  1496. r = -EINVAL;
  1497. if (bmcr & BMCR_ANENABLE) {
  1498. b44_writephy(bp, MII_BMCR,
  1499. bmcr | BMCR_ANRESTART);
  1500. r = 0;
  1501. }
  1502. spin_unlock_irq(&bp->lock);
  1503. return r;
  1504. }
  1505. static int b44_get_link_ksettings(struct net_device *dev,
  1506. struct ethtool_link_ksettings *cmd)
  1507. {
  1508. struct b44 *bp = netdev_priv(dev);
  1509. u32 supported, advertising;
  1510. if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
  1511. BUG_ON(!dev->phydev);
  1512. phy_ethtool_ksettings_get(dev->phydev, cmd);
  1513. return 0;
  1514. }
  1515. supported = (SUPPORTED_Autoneg);
  1516. supported |= (SUPPORTED_100baseT_Half |
  1517. SUPPORTED_100baseT_Full |
  1518. SUPPORTED_10baseT_Half |
  1519. SUPPORTED_10baseT_Full |
  1520. SUPPORTED_MII);
  1521. advertising = 0;
  1522. if (bp->flags & B44_FLAG_ADV_10HALF)
  1523. advertising |= ADVERTISED_10baseT_Half;
  1524. if (bp->flags & B44_FLAG_ADV_10FULL)
  1525. advertising |= ADVERTISED_10baseT_Full;
  1526. if (bp->flags & B44_FLAG_ADV_100HALF)
  1527. advertising |= ADVERTISED_100baseT_Half;
  1528. if (bp->flags & B44_FLAG_ADV_100FULL)
  1529. advertising |= ADVERTISED_100baseT_Full;
  1530. advertising |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
  1531. cmd->base.speed = (bp->flags & B44_FLAG_100_BASE_T) ?
  1532. SPEED_100 : SPEED_10;
  1533. cmd->base.duplex = (bp->flags & B44_FLAG_FULL_DUPLEX) ?
  1534. DUPLEX_FULL : DUPLEX_HALF;
  1535. cmd->base.port = 0;
  1536. cmd->base.phy_address = bp->phy_addr;
  1537. cmd->base.autoneg = (bp->flags & B44_FLAG_FORCE_LINK) ?
  1538. AUTONEG_DISABLE : AUTONEG_ENABLE;
  1539. if (cmd->base.autoneg == AUTONEG_ENABLE)
  1540. advertising |= ADVERTISED_Autoneg;
  1541. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  1542. supported);
  1543. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  1544. advertising);
  1545. if (!netif_running(dev)){
  1546. cmd->base.speed = 0;
  1547. cmd->base.duplex = 0xff;
  1548. }
  1549. return 0;
  1550. }
  1551. static int b44_set_link_ksettings(struct net_device *dev,
  1552. const struct ethtool_link_ksettings *cmd)
  1553. {
  1554. struct b44 *bp = netdev_priv(dev);
  1555. u32 speed;
  1556. int ret;
  1557. u32 advertising;
  1558. if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
  1559. BUG_ON(!dev->phydev);
  1560. spin_lock_irq(&bp->lock);
  1561. if (netif_running(dev))
  1562. b44_setup_phy(bp);
  1563. ret = phy_ethtool_ksettings_set(dev->phydev, cmd);
  1564. spin_unlock_irq(&bp->lock);
  1565. return ret;
  1566. }
  1567. speed = cmd->base.speed;
  1568. ethtool_convert_link_mode_to_legacy_u32(&advertising,
  1569. cmd->link_modes.advertising);
  1570. /* We do not support gigabit. */
  1571. if (cmd->base.autoneg == AUTONEG_ENABLE) {
  1572. if (advertising &
  1573. (ADVERTISED_1000baseT_Half |
  1574. ADVERTISED_1000baseT_Full))
  1575. return -EINVAL;
  1576. } else if ((speed != SPEED_100 &&
  1577. speed != SPEED_10) ||
  1578. (cmd->base.duplex != DUPLEX_HALF &&
  1579. cmd->base.duplex != DUPLEX_FULL)) {
  1580. return -EINVAL;
  1581. }
  1582. spin_lock_irq(&bp->lock);
  1583. if (cmd->base.autoneg == AUTONEG_ENABLE) {
  1584. bp->flags &= ~(B44_FLAG_FORCE_LINK |
  1585. B44_FLAG_100_BASE_T |
  1586. B44_FLAG_FULL_DUPLEX |
  1587. B44_FLAG_ADV_10HALF |
  1588. B44_FLAG_ADV_10FULL |
  1589. B44_FLAG_ADV_100HALF |
  1590. B44_FLAG_ADV_100FULL);
  1591. if (advertising == 0) {
  1592. bp->flags |= (B44_FLAG_ADV_10HALF |
  1593. B44_FLAG_ADV_10FULL |
  1594. B44_FLAG_ADV_100HALF |
  1595. B44_FLAG_ADV_100FULL);
  1596. } else {
  1597. if (advertising & ADVERTISED_10baseT_Half)
  1598. bp->flags |= B44_FLAG_ADV_10HALF;
  1599. if (advertising & ADVERTISED_10baseT_Full)
  1600. bp->flags |= B44_FLAG_ADV_10FULL;
  1601. if (advertising & ADVERTISED_100baseT_Half)
  1602. bp->flags |= B44_FLAG_ADV_100HALF;
  1603. if (advertising & ADVERTISED_100baseT_Full)
  1604. bp->flags |= B44_FLAG_ADV_100FULL;
  1605. }
  1606. } else {
  1607. bp->flags |= B44_FLAG_FORCE_LINK;
  1608. bp->flags &= ~(B44_FLAG_100_BASE_T | B44_FLAG_FULL_DUPLEX);
  1609. if (speed == SPEED_100)
  1610. bp->flags |= B44_FLAG_100_BASE_T;
  1611. if (cmd->base.duplex == DUPLEX_FULL)
  1612. bp->flags |= B44_FLAG_FULL_DUPLEX;
  1613. }
  1614. if (netif_running(dev))
  1615. b44_setup_phy(bp);
  1616. spin_unlock_irq(&bp->lock);
  1617. return 0;
  1618. }
  1619. static void b44_get_ringparam(struct net_device *dev,
  1620. struct ethtool_ringparam *ering)
  1621. {
  1622. struct b44 *bp = netdev_priv(dev);
  1623. ering->rx_max_pending = B44_RX_RING_SIZE - 1;
  1624. ering->rx_pending = bp->rx_pending;
  1625. /* XXX ethtool lacks a tx_max_pending, oops... */
  1626. }
  1627. static int b44_set_ringparam(struct net_device *dev,
  1628. struct ethtool_ringparam *ering)
  1629. {
  1630. struct b44 *bp = netdev_priv(dev);
  1631. if ((ering->rx_pending > B44_RX_RING_SIZE - 1) ||
  1632. (ering->rx_mini_pending != 0) ||
  1633. (ering->rx_jumbo_pending != 0) ||
  1634. (ering->tx_pending > B44_TX_RING_SIZE - 1))
  1635. return -EINVAL;
  1636. spin_lock_irq(&bp->lock);
  1637. bp->rx_pending = ering->rx_pending;
  1638. bp->tx_pending = ering->tx_pending;
  1639. b44_halt(bp);
  1640. b44_init_rings(bp);
  1641. b44_init_hw(bp, B44_FULL_RESET);
  1642. netif_wake_queue(bp->dev);
  1643. spin_unlock_irq(&bp->lock);
  1644. b44_enable_ints(bp);
  1645. return 0;
  1646. }
  1647. static void b44_get_pauseparam(struct net_device *dev,
  1648. struct ethtool_pauseparam *epause)
  1649. {
  1650. struct b44 *bp = netdev_priv(dev);
  1651. epause->autoneg =
  1652. (bp->flags & B44_FLAG_PAUSE_AUTO) != 0;
  1653. epause->rx_pause =
  1654. (bp->flags & B44_FLAG_RX_PAUSE) != 0;
  1655. epause->tx_pause =
  1656. (bp->flags & B44_FLAG_TX_PAUSE) != 0;
  1657. }
  1658. static int b44_set_pauseparam(struct net_device *dev,
  1659. struct ethtool_pauseparam *epause)
  1660. {
  1661. struct b44 *bp = netdev_priv(dev);
  1662. spin_lock_irq(&bp->lock);
  1663. if (epause->autoneg)
  1664. bp->flags |= B44_FLAG_PAUSE_AUTO;
  1665. else
  1666. bp->flags &= ~B44_FLAG_PAUSE_AUTO;
  1667. if (epause->rx_pause)
  1668. bp->flags |= B44_FLAG_RX_PAUSE;
  1669. else
  1670. bp->flags &= ~B44_FLAG_RX_PAUSE;
  1671. if (epause->tx_pause)
  1672. bp->flags |= B44_FLAG_TX_PAUSE;
  1673. else
  1674. bp->flags &= ~B44_FLAG_TX_PAUSE;
  1675. if (bp->flags & B44_FLAG_PAUSE_AUTO) {
  1676. b44_halt(bp);
  1677. b44_init_rings(bp);
  1678. b44_init_hw(bp, B44_FULL_RESET);
  1679. } else {
  1680. __b44_set_flow_ctrl(bp, bp->flags);
  1681. }
  1682. spin_unlock_irq(&bp->lock);
  1683. b44_enable_ints(bp);
  1684. return 0;
  1685. }
  1686. static void b44_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1687. {
  1688. switch(stringset) {
  1689. case ETH_SS_STATS:
  1690. memcpy(data, *b44_gstrings, sizeof(b44_gstrings));
  1691. break;
  1692. }
  1693. }
  1694. static int b44_get_sset_count(struct net_device *dev, int sset)
  1695. {
  1696. switch (sset) {
  1697. case ETH_SS_STATS:
  1698. return ARRAY_SIZE(b44_gstrings);
  1699. default:
  1700. return -EOPNOTSUPP;
  1701. }
  1702. }
  1703. static void b44_get_ethtool_stats(struct net_device *dev,
  1704. struct ethtool_stats *stats, u64 *data)
  1705. {
  1706. struct b44 *bp = netdev_priv(dev);
  1707. struct b44_hw_stats *hwstat = &bp->hw_stats;
  1708. u64 *data_src, *data_dst;
  1709. unsigned int start;
  1710. u32 i;
  1711. spin_lock_irq(&bp->lock);
  1712. b44_stats_update(bp);
  1713. spin_unlock_irq(&bp->lock);
  1714. do {
  1715. data_src = &hwstat->tx_good_octets;
  1716. data_dst = data;
  1717. start = u64_stats_fetch_begin_irq(&hwstat->syncp);
  1718. for (i = 0; i < ARRAY_SIZE(b44_gstrings); i++)
  1719. *data_dst++ = *data_src++;
  1720. } while (u64_stats_fetch_retry_irq(&hwstat->syncp, start));
  1721. }
  1722. static void b44_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1723. {
  1724. struct b44 *bp = netdev_priv(dev);
  1725. wol->supported = WAKE_MAGIC;
  1726. if (bp->flags & B44_FLAG_WOL_ENABLE)
  1727. wol->wolopts = WAKE_MAGIC;
  1728. else
  1729. wol->wolopts = 0;
  1730. memset(&wol->sopass, 0, sizeof(wol->sopass));
  1731. }
  1732. static int b44_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1733. {
  1734. struct b44 *bp = netdev_priv(dev);
  1735. spin_lock_irq(&bp->lock);
  1736. if (wol->wolopts & WAKE_MAGIC)
  1737. bp->flags |= B44_FLAG_WOL_ENABLE;
  1738. else
  1739. bp->flags &= ~B44_FLAG_WOL_ENABLE;
  1740. spin_unlock_irq(&bp->lock);
  1741. device_set_wakeup_enable(bp->sdev->dev, wol->wolopts & WAKE_MAGIC);
  1742. return 0;
  1743. }
  1744. static const struct ethtool_ops b44_ethtool_ops = {
  1745. .get_drvinfo = b44_get_drvinfo,
  1746. .nway_reset = b44_nway_reset,
  1747. .get_link = ethtool_op_get_link,
  1748. .get_wol = b44_get_wol,
  1749. .set_wol = b44_set_wol,
  1750. .get_ringparam = b44_get_ringparam,
  1751. .set_ringparam = b44_set_ringparam,
  1752. .get_pauseparam = b44_get_pauseparam,
  1753. .set_pauseparam = b44_set_pauseparam,
  1754. .get_msglevel = b44_get_msglevel,
  1755. .set_msglevel = b44_set_msglevel,
  1756. .get_strings = b44_get_strings,
  1757. .get_sset_count = b44_get_sset_count,
  1758. .get_ethtool_stats = b44_get_ethtool_stats,
  1759. .get_link_ksettings = b44_get_link_ksettings,
  1760. .set_link_ksettings = b44_set_link_ksettings,
  1761. };
  1762. static int b44_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1763. {
  1764. struct b44 *bp = netdev_priv(dev);
  1765. int err = -EINVAL;
  1766. if (!netif_running(dev))
  1767. goto out;
  1768. spin_lock_irq(&bp->lock);
  1769. if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
  1770. BUG_ON(!dev->phydev);
  1771. err = phy_mii_ioctl(dev->phydev, ifr, cmd);
  1772. } else {
  1773. err = generic_mii_ioctl(&bp->mii_if, if_mii(ifr), cmd, NULL);
  1774. }
  1775. spin_unlock_irq(&bp->lock);
  1776. out:
  1777. return err;
  1778. }
  1779. static int b44_get_invariants(struct b44 *bp)
  1780. {
  1781. struct ssb_device *sdev = bp->sdev;
  1782. int err = 0;
  1783. u8 *addr;
  1784. bp->dma_offset = ssb_dma_translation(sdev);
  1785. if (sdev->bus->bustype == SSB_BUSTYPE_SSB &&
  1786. instance > 1) {
  1787. addr = sdev->bus->sprom.et1mac;
  1788. bp->phy_addr = sdev->bus->sprom.et1phyaddr;
  1789. } else {
  1790. addr = sdev->bus->sprom.et0mac;
  1791. bp->phy_addr = sdev->bus->sprom.et0phyaddr;
  1792. }
  1793. /* Some ROMs have buggy PHY addresses with the high
  1794. * bits set (sign extension?). Truncate them to a
  1795. * valid PHY address. */
  1796. bp->phy_addr &= 0x1F;
  1797. memcpy(bp->dev->dev_addr, addr, ETH_ALEN);
  1798. if (!is_valid_ether_addr(&bp->dev->dev_addr[0])){
  1799. pr_err("Invalid MAC address found in EEPROM\n");
  1800. return -EINVAL;
  1801. }
  1802. bp->imask = IMASK_DEF;
  1803. /* XXX - really required?
  1804. bp->flags |= B44_FLAG_BUGGY_TXPTR;
  1805. */
  1806. if (bp->sdev->id.revision >= 7)
  1807. bp->flags |= B44_FLAG_B0_ANDLATER;
  1808. return err;
  1809. }
  1810. static const struct net_device_ops b44_netdev_ops = {
  1811. .ndo_open = b44_open,
  1812. .ndo_stop = b44_close,
  1813. .ndo_start_xmit = b44_start_xmit,
  1814. .ndo_get_stats64 = b44_get_stats64,
  1815. .ndo_set_rx_mode = b44_set_rx_mode,
  1816. .ndo_set_mac_address = b44_set_mac_addr,
  1817. .ndo_validate_addr = eth_validate_addr,
  1818. .ndo_do_ioctl = b44_ioctl,
  1819. .ndo_tx_timeout = b44_tx_timeout,
  1820. .ndo_change_mtu = b44_change_mtu,
  1821. #ifdef CONFIG_NET_POLL_CONTROLLER
  1822. .ndo_poll_controller = b44_poll_controller,
  1823. #endif
  1824. };
  1825. static void b44_adjust_link(struct net_device *dev)
  1826. {
  1827. struct b44 *bp = netdev_priv(dev);
  1828. struct phy_device *phydev = dev->phydev;
  1829. bool status_changed = 0;
  1830. BUG_ON(!phydev);
  1831. if (bp->old_link != phydev->link) {
  1832. status_changed = 1;
  1833. bp->old_link = phydev->link;
  1834. }
  1835. /* reflect duplex change */
  1836. if (phydev->link) {
  1837. if ((phydev->duplex == DUPLEX_HALF) &&
  1838. (bp->flags & B44_FLAG_FULL_DUPLEX)) {
  1839. status_changed = 1;
  1840. bp->flags &= ~B44_FLAG_FULL_DUPLEX;
  1841. } else if ((phydev->duplex == DUPLEX_FULL) &&
  1842. !(bp->flags & B44_FLAG_FULL_DUPLEX)) {
  1843. status_changed = 1;
  1844. bp->flags |= B44_FLAG_FULL_DUPLEX;
  1845. }
  1846. }
  1847. if (status_changed) {
  1848. u32 val = br32(bp, B44_TX_CTRL);
  1849. if (bp->flags & B44_FLAG_FULL_DUPLEX)
  1850. val |= TX_CTRL_DUPLEX;
  1851. else
  1852. val &= ~TX_CTRL_DUPLEX;
  1853. bw32(bp, B44_TX_CTRL, val);
  1854. phy_print_status(phydev);
  1855. }
  1856. }
  1857. static int b44_register_phy_one(struct b44 *bp)
  1858. {
  1859. struct mii_bus *mii_bus;
  1860. struct ssb_device *sdev = bp->sdev;
  1861. struct phy_device *phydev;
  1862. char bus_id[MII_BUS_ID_SIZE + 3];
  1863. struct ssb_sprom *sprom = &sdev->bus->sprom;
  1864. int err;
  1865. mii_bus = mdiobus_alloc();
  1866. if (!mii_bus) {
  1867. dev_err(sdev->dev, "mdiobus_alloc() failed\n");
  1868. err = -ENOMEM;
  1869. goto err_out;
  1870. }
  1871. mii_bus->priv = bp;
  1872. mii_bus->read = b44_mdio_read_phylib;
  1873. mii_bus->write = b44_mdio_write_phylib;
  1874. mii_bus->name = "b44_eth_mii";
  1875. mii_bus->parent = sdev->dev;
  1876. mii_bus->phy_mask = ~(1 << bp->phy_addr);
  1877. snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%x", instance);
  1878. bp->mii_bus = mii_bus;
  1879. err = mdiobus_register(mii_bus);
  1880. if (err) {
  1881. dev_err(sdev->dev, "failed to register MII bus\n");
  1882. goto err_out_mdiobus;
  1883. }
  1884. if (!mdiobus_is_registered_device(bp->mii_bus, bp->phy_addr) &&
  1885. (sprom->boardflags_lo & (B44_BOARDFLAG_ROBO | B44_BOARDFLAG_ADM))) {
  1886. dev_info(sdev->dev,
  1887. "could not find PHY at %i, use fixed one\n",
  1888. bp->phy_addr);
  1889. bp->phy_addr = 0;
  1890. snprintf(bus_id, sizeof(bus_id), PHY_ID_FMT, "fixed-0",
  1891. bp->phy_addr);
  1892. } else {
  1893. snprintf(bus_id, sizeof(bus_id), PHY_ID_FMT, mii_bus->id,
  1894. bp->phy_addr);
  1895. }
  1896. phydev = phy_connect(bp->dev, bus_id, &b44_adjust_link,
  1897. PHY_INTERFACE_MODE_MII);
  1898. if (IS_ERR(phydev)) {
  1899. dev_err(sdev->dev, "could not attach PHY at %i\n",
  1900. bp->phy_addr);
  1901. err = PTR_ERR(phydev);
  1902. goto err_out_mdiobus_unregister;
  1903. }
  1904. /* mask with MAC supported features */
  1905. phydev->supported &= (SUPPORTED_100baseT_Half |
  1906. SUPPORTED_100baseT_Full |
  1907. SUPPORTED_Autoneg |
  1908. SUPPORTED_MII);
  1909. phydev->advertising = phydev->supported;
  1910. bp->old_link = 0;
  1911. bp->phy_addr = phydev->mdio.addr;
  1912. phy_attached_info(phydev);
  1913. return 0;
  1914. err_out_mdiobus_unregister:
  1915. mdiobus_unregister(mii_bus);
  1916. err_out_mdiobus:
  1917. mdiobus_free(mii_bus);
  1918. err_out:
  1919. return err;
  1920. }
  1921. static void b44_unregister_phy_one(struct b44 *bp)
  1922. {
  1923. struct net_device *dev = bp->dev;
  1924. struct mii_bus *mii_bus = bp->mii_bus;
  1925. phy_disconnect(dev->phydev);
  1926. mdiobus_unregister(mii_bus);
  1927. mdiobus_free(mii_bus);
  1928. }
  1929. static int b44_init_one(struct ssb_device *sdev,
  1930. const struct ssb_device_id *ent)
  1931. {
  1932. struct net_device *dev;
  1933. struct b44 *bp;
  1934. int err;
  1935. instance++;
  1936. pr_info_once("%s version %s\n", DRV_DESCRIPTION, DRV_MODULE_VERSION);
  1937. dev = alloc_etherdev(sizeof(*bp));
  1938. if (!dev) {
  1939. err = -ENOMEM;
  1940. goto out;
  1941. }
  1942. SET_NETDEV_DEV(dev, sdev->dev);
  1943. /* No interesting netdevice features in this card... */
  1944. dev->features |= 0;
  1945. bp = netdev_priv(dev);
  1946. bp->sdev = sdev;
  1947. bp->dev = dev;
  1948. bp->force_copybreak = 0;
  1949. bp->msg_enable = netif_msg_init(b44_debug, B44_DEF_MSG_ENABLE);
  1950. spin_lock_init(&bp->lock);
  1951. u64_stats_init(&bp->hw_stats.syncp);
  1952. bp->rx_pending = B44_DEF_RX_RING_PENDING;
  1953. bp->tx_pending = B44_DEF_TX_RING_PENDING;
  1954. dev->netdev_ops = &b44_netdev_ops;
  1955. netif_napi_add(dev, &bp->napi, b44_poll, 64);
  1956. dev->watchdog_timeo = B44_TX_TIMEOUT;
  1957. dev->min_mtu = B44_MIN_MTU;
  1958. dev->max_mtu = B44_MAX_MTU;
  1959. dev->irq = sdev->irq;
  1960. dev->ethtool_ops = &b44_ethtool_ops;
  1961. err = ssb_bus_powerup(sdev->bus, 0);
  1962. if (err) {
  1963. dev_err(sdev->dev,
  1964. "Failed to powerup the bus\n");
  1965. goto err_out_free_dev;
  1966. }
  1967. if (dma_set_mask_and_coherent(sdev->dma_dev, DMA_BIT_MASK(30))) {
  1968. dev_err(sdev->dev,
  1969. "Required 30BIT DMA mask unsupported by the system\n");
  1970. goto err_out_powerdown;
  1971. }
  1972. err = b44_get_invariants(bp);
  1973. if (err) {
  1974. dev_err(sdev->dev,
  1975. "Problem fetching invariants of chip, aborting\n");
  1976. goto err_out_powerdown;
  1977. }
  1978. if (bp->phy_addr == B44_PHY_ADDR_NO_PHY) {
  1979. dev_err(sdev->dev, "No PHY present on this MAC, aborting\n");
  1980. err = -ENODEV;
  1981. goto err_out_powerdown;
  1982. }
  1983. bp->mii_if.dev = dev;
  1984. bp->mii_if.mdio_read = b44_mdio_read_mii;
  1985. bp->mii_if.mdio_write = b44_mdio_write_mii;
  1986. bp->mii_if.phy_id = bp->phy_addr;
  1987. bp->mii_if.phy_id_mask = 0x1f;
  1988. bp->mii_if.reg_num_mask = 0x1f;
  1989. /* By default, advertise all speed/duplex settings. */
  1990. bp->flags |= (B44_FLAG_ADV_10HALF | B44_FLAG_ADV_10FULL |
  1991. B44_FLAG_ADV_100HALF | B44_FLAG_ADV_100FULL);
  1992. /* By default, auto-negotiate PAUSE. */
  1993. bp->flags |= B44_FLAG_PAUSE_AUTO;
  1994. err = register_netdev(dev);
  1995. if (err) {
  1996. dev_err(sdev->dev, "Cannot register net device, aborting\n");
  1997. goto err_out_powerdown;
  1998. }
  1999. netif_carrier_off(dev);
  2000. ssb_set_drvdata(sdev, dev);
  2001. /* Chip reset provides power to the b44 MAC & PCI cores, which
  2002. * is necessary for MAC register access.
  2003. */
  2004. b44_chip_reset(bp, B44_CHIP_RESET_FULL);
  2005. /* do a phy reset to test if there is an active phy */
  2006. err = b44_phy_reset(bp);
  2007. if (err < 0) {
  2008. dev_err(sdev->dev, "phy reset failed\n");
  2009. goto err_out_unregister_netdev;
  2010. }
  2011. if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
  2012. err = b44_register_phy_one(bp);
  2013. if (err) {
  2014. dev_err(sdev->dev, "Cannot register PHY, aborting\n");
  2015. goto err_out_unregister_netdev;
  2016. }
  2017. }
  2018. device_set_wakeup_capable(sdev->dev, true);
  2019. netdev_info(dev, "%s %pM\n", DRV_DESCRIPTION, dev->dev_addr);
  2020. return 0;
  2021. err_out_unregister_netdev:
  2022. unregister_netdev(dev);
  2023. err_out_powerdown:
  2024. ssb_bus_may_powerdown(sdev->bus);
  2025. err_out_free_dev:
  2026. netif_napi_del(&bp->napi);
  2027. free_netdev(dev);
  2028. out:
  2029. return err;
  2030. }
  2031. static void b44_remove_one(struct ssb_device *sdev)
  2032. {
  2033. struct net_device *dev = ssb_get_drvdata(sdev);
  2034. struct b44 *bp = netdev_priv(dev);
  2035. unregister_netdev(dev);
  2036. if (bp->flags & B44_FLAG_EXTERNAL_PHY)
  2037. b44_unregister_phy_one(bp);
  2038. ssb_device_disable(sdev, 0);
  2039. ssb_bus_may_powerdown(sdev->bus);
  2040. netif_napi_del(&bp->napi);
  2041. free_netdev(dev);
  2042. ssb_pcihost_set_power_state(sdev, PCI_D3hot);
  2043. ssb_set_drvdata(sdev, NULL);
  2044. }
  2045. static int b44_suspend(struct ssb_device *sdev, pm_message_t state)
  2046. {
  2047. struct net_device *dev = ssb_get_drvdata(sdev);
  2048. struct b44 *bp = netdev_priv(dev);
  2049. if (!netif_running(dev))
  2050. return 0;
  2051. del_timer_sync(&bp->timer);
  2052. spin_lock_irq(&bp->lock);
  2053. b44_halt(bp);
  2054. netif_carrier_off(bp->dev);
  2055. netif_device_detach(bp->dev);
  2056. b44_free_rings(bp);
  2057. spin_unlock_irq(&bp->lock);
  2058. free_irq(dev->irq, dev);
  2059. if (bp->flags & B44_FLAG_WOL_ENABLE) {
  2060. b44_init_hw(bp, B44_PARTIAL_RESET);
  2061. b44_setup_wol(bp);
  2062. }
  2063. ssb_pcihost_set_power_state(sdev, PCI_D3hot);
  2064. return 0;
  2065. }
  2066. static int b44_resume(struct ssb_device *sdev)
  2067. {
  2068. struct net_device *dev = ssb_get_drvdata(sdev);
  2069. struct b44 *bp = netdev_priv(dev);
  2070. int rc = 0;
  2071. rc = ssb_bus_powerup(sdev->bus, 0);
  2072. if (rc) {
  2073. dev_err(sdev->dev,
  2074. "Failed to powerup the bus\n");
  2075. return rc;
  2076. }
  2077. if (!netif_running(dev))
  2078. return 0;
  2079. spin_lock_irq(&bp->lock);
  2080. b44_init_rings(bp);
  2081. b44_init_hw(bp, B44_FULL_RESET);
  2082. spin_unlock_irq(&bp->lock);
  2083. /*
  2084. * As a shared interrupt, the handler can be called immediately. To be
  2085. * able to check the interrupt status the hardware must already be
  2086. * powered back on (b44_init_hw).
  2087. */
  2088. rc = request_irq(dev->irq, b44_interrupt, IRQF_SHARED, dev->name, dev);
  2089. if (rc) {
  2090. netdev_err(dev, "request_irq failed\n");
  2091. spin_lock_irq(&bp->lock);
  2092. b44_halt(bp);
  2093. b44_free_rings(bp);
  2094. spin_unlock_irq(&bp->lock);
  2095. return rc;
  2096. }
  2097. netif_device_attach(bp->dev);
  2098. b44_enable_ints(bp);
  2099. netif_wake_queue(dev);
  2100. mod_timer(&bp->timer, jiffies + 1);
  2101. return 0;
  2102. }
  2103. static struct ssb_driver b44_ssb_driver = {
  2104. .name = DRV_MODULE_NAME,
  2105. .id_table = b44_ssb_tbl,
  2106. .probe = b44_init_one,
  2107. .remove = b44_remove_one,
  2108. .suspend = b44_suspend,
  2109. .resume = b44_resume,
  2110. };
  2111. static inline int __init b44_pci_init(void)
  2112. {
  2113. int err = 0;
  2114. #ifdef CONFIG_B44_PCI
  2115. err = ssb_pcihost_register(&b44_pci_driver);
  2116. #endif
  2117. return err;
  2118. }
  2119. static inline void b44_pci_exit(void)
  2120. {
  2121. #ifdef CONFIG_B44_PCI
  2122. ssb_pcihost_unregister(&b44_pci_driver);
  2123. #endif
  2124. }
  2125. static int __init b44_init(void)
  2126. {
  2127. unsigned int dma_desc_align_size = dma_get_cache_alignment();
  2128. int err;
  2129. /* Setup paramaters for syncing RX/TX DMA descriptors */
  2130. dma_desc_sync_size = max_t(unsigned int, dma_desc_align_size, sizeof(struct dma_desc));
  2131. err = b44_pci_init();
  2132. if (err)
  2133. return err;
  2134. err = ssb_driver_register(&b44_ssb_driver);
  2135. if (err)
  2136. b44_pci_exit();
  2137. return err;
  2138. }
  2139. static void __exit b44_cleanup(void)
  2140. {
  2141. ssb_driver_unregister(&b44_ssb_driver);
  2142. b44_pci_exit();
  2143. }
  2144. module_init(b44_init);
  2145. module_exit(b44_cleanup);