b44.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613
  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 <asm/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 60
  54. #define B44_MAX_MTU 1500
  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(unsigned long __opaque)
  506. {
  507. struct b44 *bp = (struct b44 *) __opaque;
  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(napi);
  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 (new_mtu < B44_MIN_MTU || new_mtu > B44_MAX_MTU)
  879. return -EINVAL;
  880. if (!netif_running(dev)) {
  881. /* We'll just catch it later when the
  882. * device is up'd.
  883. */
  884. dev->mtu = new_mtu;
  885. return 0;
  886. }
  887. spin_lock_irq(&bp->lock);
  888. b44_halt(bp);
  889. dev->mtu = new_mtu;
  890. b44_init_rings(bp);
  891. b44_init_hw(bp, B44_FULL_RESET);
  892. spin_unlock_irq(&bp->lock);
  893. b44_enable_ints(bp);
  894. return 0;
  895. }
  896. /* Free up pending packets in all rx/tx rings.
  897. *
  898. * The chip has been shut down and the driver detached from
  899. * the networking, so no interrupts or new tx packets will
  900. * end up in the driver. bp->lock is not held and we are not
  901. * in an interrupt context and thus may sleep.
  902. */
  903. static void b44_free_rings(struct b44 *bp)
  904. {
  905. struct ring_info *rp;
  906. int i;
  907. for (i = 0; i < B44_RX_RING_SIZE; i++) {
  908. rp = &bp->rx_buffers[i];
  909. if (rp->skb == NULL)
  910. continue;
  911. dma_unmap_single(bp->sdev->dma_dev, rp->mapping, RX_PKT_BUF_SZ,
  912. DMA_FROM_DEVICE);
  913. dev_kfree_skb_any(rp->skb);
  914. rp->skb = NULL;
  915. }
  916. /* XXX needs changes once NETIF_F_SG is set... */
  917. for (i = 0; i < B44_TX_RING_SIZE; i++) {
  918. rp = &bp->tx_buffers[i];
  919. if (rp->skb == NULL)
  920. continue;
  921. dma_unmap_single(bp->sdev->dma_dev, rp->mapping, rp->skb->len,
  922. DMA_TO_DEVICE);
  923. dev_kfree_skb_any(rp->skb);
  924. rp->skb = NULL;
  925. }
  926. }
  927. /* Initialize tx/rx rings for packet processing.
  928. *
  929. * The chip has been shut down and the driver detached from
  930. * the networking, so no interrupts or new tx packets will
  931. * end up in the driver.
  932. */
  933. static void b44_init_rings(struct b44 *bp)
  934. {
  935. int i;
  936. b44_free_rings(bp);
  937. memset(bp->rx_ring, 0, B44_RX_RING_BYTES);
  938. memset(bp->tx_ring, 0, B44_TX_RING_BYTES);
  939. if (bp->flags & B44_FLAG_RX_RING_HACK)
  940. dma_sync_single_for_device(bp->sdev->dma_dev, bp->rx_ring_dma,
  941. DMA_TABLE_BYTES, DMA_BIDIRECTIONAL);
  942. if (bp->flags & B44_FLAG_TX_RING_HACK)
  943. dma_sync_single_for_device(bp->sdev->dma_dev, bp->tx_ring_dma,
  944. DMA_TABLE_BYTES, DMA_TO_DEVICE);
  945. for (i = 0; i < bp->rx_pending; i++) {
  946. if (b44_alloc_rx_skb(bp, -1, i) < 0)
  947. break;
  948. }
  949. }
  950. /*
  951. * Must not be invoked with interrupt sources disabled and
  952. * the hardware shutdown down.
  953. */
  954. static void b44_free_consistent(struct b44 *bp)
  955. {
  956. kfree(bp->rx_buffers);
  957. bp->rx_buffers = NULL;
  958. kfree(bp->tx_buffers);
  959. bp->tx_buffers = NULL;
  960. if (bp->rx_ring) {
  961. if (bp->flags & B44_FLAG_RX_RING_HACK) {
  962. dma_unmap_single(bp->sdev->dma_dev, bp->rx_ring_dma,
  963. DMA_TABLE_BYTES, DMA_BIDIRECTIONAL);
  964. kfree(bp->rx_ring);
  965. } else
  966. dma_free_coherent(bp->sdev->dma_dev, DMA_TABLE_BYTES,
  967. bp->rx_ring, bp->rx_ring_dma);
  968. bp->rx_ring = NULL;
  969. bp->flags &= ~B44_FLAG_RX_RING_HACK;
  970. }
  971. if (bp->tx_ring) {
  972. if (bp->flags & B44_FLAG_TX_RING_HACK) {
  973. dma_unmap_single(bp->sdev->dma_dev, bp->tx_ring_dma,
  974. DMA_TABLE_BYTES, DMA_TO_DEVICE);
  975. kfree(bp->tx_ring);
  976. } else
  977. dma_free_coherent(bp->sdev->dma_dev, DMA_TABLE_BYTES,
  978. bp->tx_ring, bp->tx_ring_dma);
  979. bp->tx_ring = NULL;
  980. bp->flags &= ~B44_FLAG_TX_RING_HACK;
  981. }
  982. }
  983. /*
  984. * Must not be invoked with interrupt sources disabled and
  985. * the hardware shutdown down. Can sleep.
  986. */
  987. static int b44_alloc_consistent(struct b44 *bp, gfp_t gfp)
  988. {
  989. int size;
  990. size = B44_RX_RING_SIZE * sizeof(struct ring_info);
  991. bp->rx_buffers = kzalloc(size, gfp);
  992. if (!bp->rx_buffers)
  993. goto out_err;
  994. size = B44_TX_RING_SIZE * sizeof(struct ring_info);
  995. bp->tx_buffers = kzalloc(size, gfp);
  996. if (!bp->tx_buffers)
  997. goto out_err;
  998. size = DMA_TABLE_BYTES;
  999. bp->rx_ring = dma_alloc_coherent(bp->sdev->dma_dev, size,
  1000. &bp->rx_ring_dma, gfp);
  1001. if (!bp->rx_ring) {
  1002. /* Allocation may have failed due to pci_alloc_consistent
  1003. insisting on use of GFP_DMA, which is more restrictive
  1004. than necessary... */
  1005. struct dma_desc *rx_ring;
  1006. dma_addr_t rx_ring_dma;
  1007. rx_ring = kzalloc(size, gfp);
  1008. if (!rx_ring)
  1009. goto out_err;
  1010. rx_ring_dma = dma_map_single(bp->sdev->dma_dev, rx_ring,
  1011. DMA_TABLE_BYTES,
  1012. DMA_BIDIRECTIONAL);
  1013. if (dma_mapping_error(bp->sdev->dma_dev, rx_ring_dma) ||
  1014. rx_ring_dma + size > DMA_BIT_MASK(30)) {
  1015. kfree(rx_ring);
  1016. goto out_err;
  1017. }
  1018. bp->rx_ring = rx_ring;
  1019. bp->rx_ring_dma = rx_ring_dma;
  1020. bp->flags |= B44_FLAG_RX_RING_HACK;
  1021. }
  1022. bp->tx_ring = dma_alloc_coherent(bp->sdev->dma_dev, size,
  1023. &bp->tx_ring_dma, gfp);
  1024. if (!bp->tx_ring) {
  1025. /* Allocation may have failed due to ssb_dma_alloc_consistent
  1026. insisting on use of GFP_DMA, which is more restrictive
  1027. than necessary... */
  1028. struct dma_desc *tx_ring;
  1029. dma_addr_t tx_ring_dma;
  1030. tx_ring = kzalloc(size, gfp);
  1031. if (!tx_ring)
  1032. goto out_err;
  1033. tx_ring_dma = dma_map_single(bp->sdev->dma_dev, tx_ring,
  1034. DMA_TABLE_BYTES,
  1035. DMA_TO_DEVICE);
  1036. if (dma_mapping_error(bp->sdev->dma_dev, tx_ring_dma) ||
  1037. tx_ring_dma + size > DMA_BIT_MASK(30)) {
  1038. kfree(tx_ring);
  1039. goto out_err;
  1040. }
  1041. bp->tx_ring = tx_ring;
  1042. bp->tx_ring_dma = tx_ring_dma;
  1043. bp->flags |= B44_FLAG_TX_RING_HACK;
  1044. }
  1045. return 0;
  1046. out_err:
  1047. b44_free_consistent(bp);
  1048. return -ENOMEM;
  1049. }
  1050. /* bp->lock is held. */
  1051. static void b44_clear_stats(struct b44 *bp)
  1052. {
  1053. unsigned long reg;
  1054. bw32(bp, B44_MIB_CTRL, MIB_CTRL_CLR_ON_READ);
  1055. for (reg = B44_TX_GOOD_O; reg <= B44_TX_PAUSE; reg += 4UL)
  1056. br32(bp, reg);
  1057. for (reg = B44_RX_GOOD_O; reg <= B44_RX_NPAUSE; reg += 4UL)
  1058. br32(bp, reg);
  1059. }
  1060. /* bp->lock is held. */
  1061. static void b44_chip_reset(struct b44 *bp, int reset_kind)
  1062. {
  1063. struct ssb_device *sdev = bp->sdev;
  1064. bool was_enabled;
  1065. was_enabled = ssb_device_is_enabled(bp->sdev);
  1066. ssb_device_enable(bp->sdev, 0);
  1067. ssb_pcicore_dev_irqvecs_enable(&sdev->bus->pcicore, sdev);
  1068. if (was_enabled) {
  1069. bw32(bp, B44_RCV_LAZY, 0);
  1070. bw32(bp, B44_ENET_CTRL, ENET_CTRL_DISABLE);
  1071. b44_wait_bit(bp, B44_ENET_CTRL, ENET_CTRL_DISABLE, 200, 1);
  1072. bw32(bp, B44_DMATX_CTRL, 0);
  1073. bp->tx_prod = bp->tx_cons = 0;
  1074. if (br32(bp, B44_DMARX_STAT) & DMARX_STAT_EMASK) {
  1075. b44_wait_bit(bp, B44_DMARX_STAT, DMARX_STAT_SIDLE,
  1076. 100, 0);
  1077. }
  1078. bw32(bp, B44_DMARX_CTRL, 0);
  1079. bp->rx_prod = bp->rx_cons = 0;
  1080. }
  1081. b44_clear_stats(bp);
  1082. /*
  1083. * Don't enable PHY if we are doing a partial reset
  1084. * we are probably going to power down
  1085. */
  1086. if (reset_kind == B44_CHIP_RESET_PARTIAL)
  1087. return;
  1088. switch (sdev->bus->bustype) {
  1089. case SSB_BUSTYPE_SSB:
  1090. bw32(bp, B44_MDIO_CTRL, (MDIO_CTRL_PREAMBLE |
  1091. (DIV_ROUND_CLOSEST(ssb_clockspeed(sdev->bus),
  1092. B44_MDC_RATIO)
  1093. & MDIO_CTRL_MAXF_MASK)));
  1094. break;
  1095. case SSB_BUSTYPE_PCI:
  1096. bw32(bp, B44_MDIO_CTRL, (MDIO_CTRL_PREAMBLE |
  1097. (0x0d & MDIO_CTRL_MAXF_MASK)));
  1098. break;
  1099. case SSB_BUSTYPE_PCMCIA:
  1100. case SSB_BUSTYPE_SDIO:
  1101. WARN_ON(1); /* A device with this bus does not exist. */
  1102. break;
  1103. }
  1104. br32(bp, B44_MDIO_CTRL);
  1105. if (!(br32(bp, B44_DEVCTRL) & DEVCTRL_IPP)) {
  1106. bw32(bp, B44_ENET_CTRL, ENET_CTRL_EPSEL);
  1107. br32(bp, B44_ENET_CTRL);
  1108. bp->flags |= B44_FLAG_EXTERNAL_PHY;
  1109. } else {
  1110. u32 val = br32(bp, B44_DEVCTRL);
  1111. if (val & DEVCTRL_EPR) {
  1112. bw32(bp, B44_DEVCTRL, (val & ~DEVCTRL_EPR));
  1113. br32(bp, B44_DEVCTRL);
  1114. udelay(100);
  1115. }
  1116. bp->flags &= ~B44_FLAG_EXTERNAL_PHY;
  1117. }
  1118. }
  1119. /* bp->lock is held. */
  1120. static void b44_halt(struct b44 *bp)
  1121. {
  1122. b44_disable_ints(bp);
  1123. /* reset PHY */
  1124. b44_phy_reset(bp);
  1125. /* power down PHY */
  1126. netdev_info(bp->dev, "powering down PHY\n");
  1127. bw32(bp, B44_MAC_CTRL, MAC_CTRL_PHY_PDOWN);
  1128. /* now reset the chip, but without enabling the MAC&PHY
  1129. * part of it. This has to be done _after_ we shut down the PHY */
  1130. if (bp->flags & B44_FLAG_EXTERNAL_PHY)
  1131. b44_chip_reset(bp, B44_CHIP_RESET_FULL);
  1132. else
  1133. b44_chip_reset(bp, B44_CHIP_RESET_PARTIAL);
  1134. }
  1135. /* bp->lock is held. */
  1136. static void __b44_set_mac_addr(struct b44 *bp)
  1137. {
  1138. bw32(bp, B44_CAM_CTRL, 0);
  1139. if (!(bp->dev->flags & IFF_PROMISC)) {
  1140. u32 val;
  1141. __b44_cam_write(bp, bp->dev->dev_addr, 0);
  1142. val = br32(bp, B44_CAM_CTRL);
  1143. bw32(bp, B44_CAM_CTRL, val | CAM_CTRL_ENABLE);
  1144. }
  1145. }
  1146. static int b44_set_mac_addr(struct net_device *dev, void *p)
  1147. {
  1148. struct b44 *bp = netdev_priv(dev);
  1149. struct sockaddr *addr = p;
  1150. u32 val;
  1151. if (netif_running(dev))
  1152. return -EBUSY;
  1153. if (!is_valid_ether_addr(addr->sa_data))
  1154. return -EINVAL;
  1155. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1156. spin_lock_irq(&bp->lock);
  1157. val = br32(bp, B44_RXCONFIG);
  1158. if (!(val & RXCONFIG_CAM_ABSENT))
  1159. __b44_set_mac_addr(bp);
  1160. spin_unlock_irq(&bp->lock);
  1161. return 0;
  1162. }
  1163. /* Called at device open time to get the chip ready for
  1164. * packet processing. Invoked with bp->lock held.
  1165. */
  1166. static void __b44_set_rx_mode(struct net_device *);
  1167. static void b44_init_hw(struct b44 *bp, int reset_kind)
  1168. {
  1169. u32 val;
  1170. b44_chip_reset(bp, B44_CHIP_RESET_FULL);
  1171. if (reset_kind == B44_FULL_RESET) {
  1172. b44_phy_reset(bp);
  1173. b44_setup_phy(bp);
  1174. }
  1175. /* Enable CRC32, set proper LED modes and power on PHY */
  1176. bw32(bp, B44_MAC_CTRL, MAC_CTRL_CRC32_ENAB | MAC_CTRL_PHY_LEDCTRL);
  1177. bw32(bp, B44_RCV_LAZY, (1 << RCV_LAZY_FC_SHIFT));
  1178. /* This sets the MAC address too. */
  1179. __b44_set_rx_mode(bp->dev);
  1180. /* MTU + eth header + possible VLAN tag + struct rx_header */
  1181. bw32(bp, B44_RXMAXLEN, bp->dev->mtu + ETH_HLEN + 8 + RX_HEADER_LEN);
  1182. bw32(bp, B44_TXMAXLEN, bp->dev->mtu + ETH_HLEN + 8 + RX_HEADER_LEN);
  1183. bw32(bp, B44_TX_WMARK, 56); /* XXX magic */
  1184. if (reset_kind == B44_PARTIAL_RESET) {
  1185. bw32(bp, B44_DMARX_CTRL, (DMARX_CTRL_ENABLE |
  1186. (RX_PKT_OFFSET << DMARX_CTRL_ROSHIFT)));
  1187. } else {
  1188. bw32(bp, B44_DMATX_CTRL, DMATX_CTRL_ENABLE);
  1189. bw32(bp, B44_DMATX_ADDR, bp->tx_ring_dma + bp->dma_offset);
  1190. bw32(bp, B44_DMARX_CTRL, (DMARX_CTRL_ENABLE |
  1191. (RX_PKT_OFFSET << DMARX_CTRL_ROSHIFT)));
  1192. bw32(bp, B44_DMARX_ADDR, bp->rx_ring_dma + bp->dma_offset);
  1193. bw32(bp, B44_DMARX_PTR, bp->rx_pending);
  1194. bp->rx_prod = bp->rx_pending;
  1195. bw32(bp, B44_MIB_CTRL, MIB_CTRL_CLR_ON_READ);
  1196. }
  1197. val = br32(bp, B44_ENET_CTRL);
  1198. bw32(bp, B44_ENET_CTRL, (val | ENET_CTRL_ENABLE));
  1199. netdev_reset_queue(bp->dev);
  1200. }
  1201. static int b44_open(struct net_device *dev)
  1202. {
  1203. struct b44 *bp = netdev_priv(dev);
  1204. int err;
  1205. err = b44_alloc_consistent(bp, GFP_KERNEL);
  1206. if (err)
  1207. goto out;
  1208. napi_enable(&bp->napi);
  1209. b44_init_rings(bp);
  1210. b44_init_hw(bp, B44_FULL_RESET);
  1211. b44_check_phy(bp);
  1212. err = request_irq(dev->irq, b44_interrupt, IRQF_SHARED, dev->name, dev);
  1213. if (unlikely(err < 0)) {
  1214. napi_disable(&bp->napi);
  1215. b44_chip_reset(bp, B44_CHIP_RESET_PARTIAL);
  1216. b44_free_rings(bp);
  1217. b44_free_consistent(bp);
  1218. goto out;
  1219. }
  1220. init_timer(&bp->timer);
  1221. bp->timer.expires = jiffies + HZ;
  1222. bp->timer.data = (unsigned long) bp;
  1223. bp->timer.function = b44_timer;
  1224. add_timer(&bp->timer);
  1225. b44_enable_ints(bp);
  1226. if (bp->flags & B44_FLAG_EXTERNAL_PHY)
  1227. phy_start(dev->phydev);
  1228. netif_start_queue(dev);
  1229. out:
  1230. return err;
  1231. }
  1232. #ifdef CONFIG_NET_POLL_CONTROLLER
  1233. /*
  1234. * Polling receive - used by netconsole and other diagnostic tools
  1235. * to allow network i/o with interrupts disabled.
  1236. */
  1237. static void b44_poll_controller(struct net_device *dev)
  1238. {
  1239. disable_irq(dev->irq);
  1240. b44_interrupt(dev->irq, dev);
  1241. enable_irq(dev->irq);
  1242. }
  1243. #endif
  1244. static void bwfilter_table(struct b44 *bp, u8 *pp, u32 bytes, u32 table_offset)
  1245. {
  1246. u32 i;
  1247. u32 *pattern = (u32 *) pp;
  1248. for (i = 0; i < bytes; i += sizeof(u32)) {
  1249. bw32(bp, B44_FILT_ADDR, table_offset + i);
  1250. bw32(bp, B44_FILT_DATA, pattern[i / sizeof(u32)]);
  1251. }
  1252. }
  1253. static int b44_magic_pattern(u8 *macaddr, u8 *ppattern, u8 *pmask, int offset)
  1254. {
  1255. int magicsync = 6;
  1256. int k, j, len = offset;
  1257. int ethaddr_bytes = ETH_ALEN;
  1258. memset(ppattern + offset, 0xff, magicsync);
  1259. for (j = 0; j < magicsync; j++)
  1260. set_bit(len++, (unsigned long *) pmask);
  1261. for (j = 0; j < B44_MAX_PATTERNS; j++) {
  1262. if ((B44_PATTERN_SIZE - len) >= ETH_ALEN)
  1263. ethaddr_bytes = ETH_ALEN;
  1264. else
  1265. ethaddr_bytes = B44_PATTERN_SIZE - len;
  1266. if (ethaddr_bytes <=0)
  1267. break;
  1268. for (k = 0; k< ethaddr_bytes; k++) {
  1269. ppattern[offset + magicsync +
  1270. (j * ETH_ALEN) + k] = macaddr[k];
  1271. set_bit(len++, (unsigned long *) pmask);
  1272. }
  1273. }
  1274. return len - 1;
  1275. }
  1276. /* Setup magic packet patterns in the b44 WOL
  1277. * pattern matching filter.
  1278. */
  1279. static void b44_setup_pseudo_magicp(struct b44 *bp)
  1280. {
  1281. u32 val;
  1282. int plen0, plen1, plen2;
  1283. u8 *pwol_pattern;
  1284. u8 pwol_mask[B44_PMASK_SIZE];
  1285. pwol_pattern = kzalloc(B44_PATTERN_SIZE, GFP_KERNEL);
  1286. if (!pwol_pattern)
  1287. return;
  1288. /* Ipv4 magic packet pattern - pattern 0.*/
  1289. memset(pwol_mask, 0, B44_PMASK_SIZE);
  1290. plen0 = b44_magic_pattern(bp->dev->dev_addr, pwol_pattern, pwol_mask,
  1291. B44_ETHIPV4UDP_HLEN);
  1292. bwfilter_table(bp, pwol_pattern, B44_PATTERN_SIZE, B44_PATTERN_BASE);
  1293. bwfilter_table(bp, pwol_mask, B44_PMASK_SIZE, B44_PMASK_BASE);
  1294. /* Raw ethernet II magic packet pattern - pattern 1 */
  1295. memset(pwol_pattern, 0, B44_PATTERN_SIZE);
  1296. memset(pwol_mask, 0, B44_PMASK_SIZE);
  1297. plen1 = b44_magic_pattern(bp->dev->dev_addr, pwol_pattern, pwol_mask,
  1298. ETH_HLEN);
  1299. bwfilter_table(bp, pwol_pattern, B44_PATTERN_SIZE,
  1300. B44_PATTERN_BASE + B44_PATTERN_SIZE);
  1301. bwfilter_table(bp, pwol_mask, B44_PMASK_SIZE,
  1302. B44_PMASK_BASE + B44_PMASK_SIZE);
  1303. /* Ipv6 magic packet pattern - pattern 2 */
  1304. memset(pwol_pattern, 0, B44_PATTERN_SIZE);
  1305. memset(pwol_mask, 0, B44_PMASK_SIZE);
  1306. plen2 = b44_magic_pattern(bp->dev->dev_addr, pwol_pattern, pwol_mask,
  1307. B44_ETHIPV6UDP_HLEN);
  1308. bwfilter_table(bp, pwol_pattern, B44_PATTERN_SIZE,
  1309. B44_PATTERN_BASE + B44_PATTERN_SIZE + B44_PATTERN_SIZE);
  1310. bwfilter_table(bp, pwol_mask, B44_PMASK_SIZE,
  1311. B44_PMASK_BASE + B44_PMASK_SIZE + B44_PMASK_SIZE);
  1312. kfree(pwol_pattern);
  1313. /* set these pattern's lengths: one less than each real length */
  1314. val = plen0 | (plen1 << 8) | (plen2 << 16) | WKUP_LEN_ENABLE_THREE;
  1315. bw32(bp, B44_WKUP_LEN, val);
  1316. /* enable wakeup pattern matching */
  1317. val = br32(bp, B44_DEVCTRL);
  1318. bw32(bp, B44_DEVCTRL, val | DEVCTRL_PFE);
  1319. }
  1320. #ifdef CONFIG_B44_PCI
  1321. static void b44_setup_wol_pci(struct b44 *bp)
  1322. {
  1323. u16 val;
  1324. if (bp->sdev->bus->bustype != SSB_BUSTYPE_SSB) {
  1325. bw32(bp, SSB_TMSLOW, br32(bp, SSB_TMSLOW) | SSB_TMSLOW_PE);
  1326. pci_read_config_word(bp->sdev->bus->host_pci, SSB_PMCSR, &val);
  1327. pci_write_config_word(bp->sdev->bus->host_pci, SSB_PMCSR, val | SSB_PE);
  1328. }
  1329. }
  1330. #else
  1331. static inline void b44_setup_wol_pci(struct b44 *bp) { }
  1332. #endif /* CONFIG_B44_PCI */
  1333. static void b44_setup_wol(struct b44 *bp)
  1334. {
  1335. u32 val;
  1336. bw32(bp, B44_RXCONFIG, RXCONFIG_ALLMULTI);
  1337. if (bp->flags & B44_FLAG_B0_ANDLATER) {
  1338. bw32(bp, B44_WKUP_LEN, WKUP_LEN_DISABLE);
  1339. val = bp->dev->dev_addr[2] << 24 |
  1340. bp->dev->dev_addr[3] << 16 |
  1341. bp->dev->dev_addr[4] << 8 |
  1342. bp->dev->dev_addr[5];
  1343. bw32(bp, B44_ADDR_LO, val);
  1344. val = bp->dev->dev_addr[0] << 8 |
  1345. bp->dev->dev_addr[1];
  1346. bw32(bp, B44_ADDR_HI, val);
  1347. val = br32(bp, B44_DEVCTRL);
  1348. bw32(bp, B44_DEVCTRL, val | DEVCTRL_MPM | DEVCTRL_PFE);
  1349. } else {
  1350. b44_setup_pseudo_magicp(bp);
  1351. }
  1352. b44_setup_wol_pci(bp);
  1353. }
  1354. static int b44_close(struct net_device *dev)
  1355. {
  1356. struct b44 *bp = netdev_priv(dev);
  1357. netif_stop_queue(dev);
  1358. if (bp->flags & B44_FLAG_EXTERNAL_PHY)
  1359. phy_stop(dev->phydev);
  1360. napi_disable(&bp->napi);
  1361. del_timer_sync(&bp->timer);
  1362. spin_lock_irq(&bp->lock);
  1363. b44_halt(bp);
  1364. b44_free_rings(bp);
  1365. netif_carrier_off(dev);
  1366. spin_unlock_irq(&bp->lock);
  1367. free_irq(dev->irq, dev);
  1368. if (bp->flags & B44_FLAG_WOL_ENABLE) {
  1369. b44_init_hw(bp, B44_PARTIAL_RESET);
  1370. b44_setup_wol(bp);
  1371. }
  1372. b44_free_consistent(bp);
  1373. return 0;
  1374. }
  1375. static struct rtnl_link_stats64 *b44_get_stats64(struct net_device *dev,
  1376. struct rtnl_link_stats64 *nstat)
  1377. {
  1378. struct b44 *bp = netdev_priv(dev);
  1379. struct b44_hw_stats *hwstat = &bp->hw_stats;
  1380. unsigned int start;
  1381. do {
  1382. start = u64_stats_fetch_begin_irq(&hwstat->syncp);
  1383. /* Convert HW stats into rtnl_link_stats64 stats. */
  1384. nstat->rx_packets = hwstat->rx_pkts;
  1385. nstat->tx_packets = hwstat->tx_pkts;
  1386. nstat->rx_bytes = hwstat->rx_octets;
  1387. nstat->tx_bytes = hwstat->tx_octets;
  1388. nstat->tx_errors = (hwstat->tx_jabber_pkts +
  1389. hwstat->tx_oversize_pkts +
  1390. hwstat->tx_underruns +
  1391. hwstat->tx_excessive_cols +
  1392. hwstat->tx_late_cols);
  1393. nstat->multicast = hwstat->rx_multicast_pkts;
  1394. nstat->collisions = hwstat->tx_total_cols;
  1395. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  1396. hwstat->rx_undersize);
  1397. nstat->rx_over_errors = hwstat->rx_missed_pkts;
  1398. nstat->rx_frame_errors = hwstat->rx_align_errs;
  1399. nstat->rx_crc_errors = hwstat->rx_crc_errs;
  1400. nstat->rx_errors = (hwstat->rx_jabber_pkts +
  1401. hwstat->rx_oversize_pkts +
  1402. hwstat->rx_missed_pkts +
  1403. hwstat->rx_crc_align_errs +
  1404. hwstat->rx_undersize +
  1405. hwstat->rx_crc_errs +
  1406. hwstat->rx_align_errs +
  1407. hwstat->rx_symbol_errs);
  1408. nstat->tx_aborted_errors = hwstat->tx_underruns;
  1409. #if 0
  1410. /* Carrier lost counter seems to be broken for some devices */
  1411. nstat->tx_carrier_errors = hwstat->tx_carrier_lost;
  1412. #endif
  1413. } while (u64_stats_fetch_retry_irq(&hwstat->syncp, start));
  1414. return nstat;
  1415. }
  1416. static int __b44_load_mcast(struct b44 *bp, struct net_device *dev)
  1417. {
  1418. struct netdev_hw_addr *ha;
  1419. int i, num_ents;
  1420. num_ents = min_t(int, netdev_mc_count(dev), B44_MCAST_TABLE_SIZE);
  1421. i = 0;
  1422. netdev_for_each_mc_addr(ha, dev) {
  1423. if (i == num_ents)
  1424. break;
  1425. __b44_cam_write(bp, ha->addr, i++ + 1);
  1426. }
  1427. return i+1;
  1428. }
  1429. static void __b44_set_rx_mode(struct net_device *dev)
  1430. {
  1431. struct b44 *bp = netdev_priv(dev);
  1432. u32 val;
  1433. val = br32(bp, B44_RXCONFIG);
  1434. val &= ~(RXCONFIG_PROMISC | RXCONFIG_ALLMULTI);
  1435. if ((dev->flags & IFF_PROMISC) || (val & RXCONFIG_CAM_ABSENT)) {
  1436. val |= RXCONFIG_PROMISC;
  1437. bw32(bp, B44_RXCONFIG, val);
  1438. } else {
  1439. unsigned char zero[6] = {0, 0, 0, 0, 0, 0};
  1440. int i = 1;
  1441. __b44_set_mac_addr(bp);
  1442. if ((dev->flags & IFF_ALLMULTI) ||
  1443. (netdev_mc_count(dev) > B44_MCAST_TABLE_SIZE))
  1444. val |= RXCONFIG_ALLMULTI;
  1445. else
  1446. i = __b44_load_mcast(bp, dev);
  1447. for (; i < 64; i++)
  1448. __b44_cam_write(bp, zero, i);
  1449. bw32(bp, B44_RXCONFIG, val);
  1450. val = br32(bp, B44_CAM_CTRL);
  1451. bw32(bp, B44_CAM_CTRL, val | CAM_CTRL_ENABLE);
  1452. }
  1453. }
  1454. static void b44_set_rx_mode(struct net_device *dev)
  1455. {
  1456. struct b44 *bp = netdev_priv(dev);
  1457. spin_lock_irq(&bp->lock);
  1458. __b44_set_rx_mode(dev);
  1459. spin_unlock_irq(&bp->lock);
  1460. }
  1461. static u32 b44_get_msglevel(struct net_device *dev)
  1462. {
  1463. struct b44 *bp = netdev_priv(dev);
  1464. return bp->msg_enable;
  1465. }
  1466. static void b44_set_msglevel(struct net_device *dev, u32 value)
  1467. {
  1468. struct b44 *bp = netdev_priv(dev);
  1469. bp->msg_enable = value;
  1470. }
  1471. static void b44_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
  1472. {
  1473. struct b44 *bp = netdev_priv(dev);
  1474. struct ssb_bus *bus = bp->sdev->bus;
  1475. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  1476. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  1477. switch (bus->bustype) {
  1478. case SSB_BUSTYPE_PCI:
  1479. strlcpy(info->bus_info, pci_name(bus->host_pci), sizeof(info->bus_info));
  1480. break;
  1481. case SSB_BUSTYPE_SSB:
  1482. strlcpy(info->bus_info, "SSB", sizeof(info->bus_info));
  1483. break;
  1484. case SSB_BUSTYPE_PCMCIA:
  1485. case SSB_BUSTYPE_SDIO:
  1486. WARN_ON(1); /* A device with this bus does not exist. */
  1487. break;
  1488. }
  1489. }
  1490. static int b44_nway_reset(struct net_device *dev)
  1491. {
  1492. struct b44 *bp = netdev_priv(dev);
  1493. u32 bmcr;
  1494. int r;
  1495. spin_lock_irq(&bp->lock);
  1496. b44_readphy(bp, MII_BMCR, &bmcr);
  1497. b44_readphy(bp, MII_BMCR, &bmcr);
  1498. r = -EINVAL;
  1499. if (bmcr & BMCR_ANENABLE) {
  1500. b44_writephy(bp, MII_BMCR,
  1501. bmcr | BMCR_ANRESTART);
  1502. r = 0;
  1503. }
  1504. spin_unlock_irq(&bp->lock);
  1505. return r;
  1506. }
  1507. static int b44_get_link_ksettings(struct net_device *dev,
  1508. struct ethtool_link_ksettings *cmd)
  1509. {
  1510. struct b44 *bp = netdev_priv(dev);
  1511. u32 supported, advertising;
  1512. if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
  1513. BUG_ON(!dev->phydev);
  1514. return phy_ethtool_ksettings_get(dev->phydev, cmd);
  1515. }
  1516. supported = (SUPPORTED_Autoneg);
  1517. supported |= (SUPPORTED_100baseT_Half |
  1518. SUPPORTED_100baseT_Full |
  1519. SUPPORTED_10baseT_Half |
  1520. SUPPORTED_10baseT_Full |
  1521. SUPPORTED_MII);
  1522. advertising = 0;
  1523. if (bp->flags & B44_FLAG_ADV_10HALF)
  1524. advertising |= ADVERTISED_10baseT_Half;
  1525. if (bp->flags & B44_FLAG_ADV_10FULL)
  1526. advertising |= ADVERTISED_10baseT_Full;
  1527. if (bp->flags & B44_FLAG_ADV_100HALF)
  1528. advertising |= ADVERTISED_100baseT_Half;
  1529. if (bp->flags & B44_FLAG_ADV_100FULL)
  1530. advertising |= ADVERTISED_100baseT_Full;
  1531. advertising |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
  1532. cmd->base.speed = (bp->flags & B44_FLAG_100_BASE_T) ?
  1533. SPEED_100 : SPEED_10;
  1534. cmd->base.duplex = (bp->flags & B44_FLAG_FULL_DUPLEX) ?
  1535. DUPLEX_FULL : DUPLEX_HALF;
  1536. cmd->base.port = 0;
  1537. cmd->base.phy_address = bp->phy_addr;
  1538. cmd->base.autoneg = (bp->flags & B44_FLAG_FORCE_LINK) ?
  1539. AUTONEG_DISABLE : AUTONEG_ENABLE;
  1540. if (cmd->base.autoneg == AUTONEG_ENABLE)
  1541. advertising |= ADVERTISED_Autoneg;
  1542. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  1543. supported);
  1544. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  1545. advertising);
  1546. if (!netif_running(dev)){
  1547. cmd->base.speed = 0;
  1548. cmd->base.duplex = 0xff;
  1549. }
  1550. return 0;
  1551. }
  1552. static int b44_set_link_ksettings(struct net_device *dev,
  1553. const struct ethtool_link_ksettings *cmd)
  1554. {
  1555. struct b44 *bp = netdev_priv(dev);
  1556. u32 speed;
  1557. int ret;
  1558. u32 advertising;
  1559. if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
  1560. BUG_ON(!dev->phydev);
  1561. spin_lock_irq(&bp->lock);
  1562. if (netif_running(dev))
  1563. b44_setup_phy(bp);
  1564. ret = phy_ethtool_ksettings_set(dev->phydev, cmd);
  1565. spin_unlock_irq(&bp->lock);
  1566. return ret;
  1567. }
  1568. speed = cmd->base.speed;
  1569. ethtool_convert_link_mode_to_legacy_u32(&advertising,
  1570. cmd->link_modes.advertising);
  1571. /* We do not support gigabit. */
  1572. if (cmd->base.autoneg == AUTONEG_ENABLE) {
  1573. if (advertising &
  1574. (ADVERTISED_1000baseT_Half |
  1575. ADVERTISED_1000baseT_Full))
  1576. return -EINVAL;
  1577. } else if ((speed != SPEED_100 &&
  1578. speed != SPEED_10) ||
  1579. (cmd->base.duplex != DUPLEX_HALF &&
  1580. cmd->base.duplex != DUPLEX_FULL)) {
  1581. return -EINVAL;
  1582. }
  1583. spin_lock_irq(&bp->lock);
  1584. if (cmd->base.autoneg == AUTONEG_ENABLE) {
  1585. bp->flags &= ~(B44_FLAG_FORCE_LINK |
  1586. B44_FLAG_100_BASE_T |
  1587. B44_FLAG_FULL_DUPLEX |
  1588. B44_FLAG_ADV_10HALF |
  1589. B44_FLAG_ADV_10FULL |
  1590. B44_FLAG_ADV_100HALF |
  1591. B44_FLAG_ADV_100FULL);
  1592. if (advertising == 0) {
  1593. bp->flags |= (B44_FLAG_ADV_10HALF |
  1594. B44_FLAG_ADV_10FULL |
  1595. B44_FLAG_ADV_100HALF |
  1596. B44_FLAG_ADV_100FULL);
  1597. } else {
  1598. if (advertising & ADVERTISED_10baseT_Half)
  1599. bp->flags |= B44_FLAG_ADV_10HALF;
  1600. if (advertising & ADVERTISED_10baseT_Full)
  1601. bp->flags |= B44_FLAG_ADV_10FULL;
  1602. if (advertising & ADVERTISED_100baseT_Half)
  1603. bp->flags |= B44_FLAG_ADV_100HALF;
  1604. if (advertising & ADVERTISED_100baseT_Full)
  1605. bp->flags |= B44_FLAG_ADV_100FULL;
  1606. }
  1607. } else {
  1608. bp->flags |= B44_FLAG_FORCE_LINK;
  1609. bp->flags &= ~(B44_FLAG_100_BASE_T | B44_FLAG_FULL_DUPLEX);
  1610. if (speed == SPEED_100)
  1611. bp->flags |= B44_FLAG_100_BASE_T;
  1612. if (cmd->base.duplex == DUPLEX_FULL)
  1613. bp->flags |= B44_FLAG_FULL_DUPLEX;
  1614. }
  1615. if (netif_running(dev))
  1616. b44_setup_phy(bp);
  1617. spin_unlock_irq(&bp->lock);
  1618. return 0;
  1619. }
  1620. static void b44_get_ringparam(struct net_device *dev,
  1621. struct ethtool_ringparam *ering)
  1622. {
  1623. struct b44 *bp = netdev_priv(dev);
  1624. ering->rx_max_pending = B44_RX_RING_SIZE - 1;
  1625. ering->rx_pending = bp->rx_pending;
  1626. /* XXX ethtool lacks a tx_max_pending, oops... */
  1627. }
  1628. static int b44_set_ringparam(struct net_device *dev,
  1629. struct ethtool_ringparam *ering)
  1630. {
  1631. struct b44 *bp = netdev_priv(dev);
  1632. if ((ering->rx_pending > B44_RX_RING_SIZE - 1) ||
  1633. (ering->rx_mini_pending != 0) ||
  1634. (ering->rx_jumbo_pending != 0) ||
  1635. (ering->tx_pending > B44_TX_RING_SIZE - 1))
  1636. return -EINVAL;
  1637. spin_lock_irq(&bp->lock);
  1638. bp->rx_pending = ering->rx_pending;
  1639. bp->tx_pending = ering->tx_pending;
  1640. b44_halt(bp);
  1641. b44_init_rings(bp);
  1642. b44_init_hw(bp, B44_FULL_RESET);
  1643. netif_wake_queue(bp->dev);
  1644. spin_unlock_irq(&bp->lock);
  1645. b44_enable_ints(bp);
  1646. return 0;
  1647. }
  1648. static void b44_get_pauseparam(struct net_device *dev,
  1649. struct ethtool_pauseparam *epause)
  1650. {
  1651. struct b44 *bp = netdev_priv(dev);
  1652. epause->autoneg =
  1653. (bp->flags & B44_FLAG_PAUSE_AUTO) != 0;
  1654. epause->rx_pause =
  1655. (bp->flags & B44_FLAG_RX_PAUSE) != 0;
  1656. epause->tx_pause =
  1657. (bp->flags & B44_FLAG_TX_PAUSE) != 0;
  1658. }
  1659. static int b44_set_pauseparam(struct net_device *dev,
  1660. struct ethtool_pauseparam *epause)
  1661. {
  1662. struct b44 *bp = netdev_priv(dev);
  1663. spin_lock_irq(&bp->lock);
  1664. if (epause->autoneg)
  1665. bp->flags |= B44_FLAG_PAUSE_AUTO;
  1666. else
  1667. bp->flags &= ~B44_FLAG_PAUSE_AUTO;
  1668. if (epause->rx_pause)
  1669. bp->flags |= B44_FLAG_RX_PAUSE;
  1670. else
  1671. bp->flags &= ~B44_FLAG_RX_PAUSE;
  1672. if (epause->tx_pause)
  1673. bp->flags |= B44_FLAG_TX_PAUSE;
  1674. else
  1675. bp->flags &= ~B44_FLAG_TX_PAUSE;
  1676. if (bp->flags & B44_FLAG_PAUSE_AUTO) {
  1677. b44_halt(bp);
  1678. b44_init_rings(bp);
  1679. b44_init_hw(bp, B44_FULL_RESET);
  1680. } else {
  1681. __b44_set_flow_ctrl(bp, bp->flags);
  1682. }
  1683. spin_unlock_irq(&bp->lock);
  1684. b44_enable_ints(bp);
  1685. return 0;
  1686. }
  1687. static void b44_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1688. {
  1689. switch(stringset) {
  1690. case ETH_SS_STATS:
  1691. memcpy(data, *b44_gstrings, sizeof(b44_gstrings));
  1692. break;
  1693. }
  1694. }
  1695. static int b44_get_sset_count(struct net_device *dev, int sset)
  1696. {
  1697. switch (sset) {
  1698. case ETH_SS_STATS:
  1699. return ARRAY_SIZE(b44_gstrings);
  1700. default:
  1701. return -EOPNOTSUPP;
  1702. }
  1703. }
  1704. static void b44_get_ethtool_stats(struct net_device *dev,
  1705. struct ethtool_stats *stats, u64 *data)
  1706. {
  1707. struct b44 *bp = netdev_priv(dev);
  1708. struct b44_hw_stats *hwstat = &bp->hw_stats;
  1709. u64 *data_src, *data_dst;
  1710. unsigned int start;
  1711. u32 i;
  1712. spin_lock_irq(&bp->lock);
  1713. b44_stats_update(bp);
  1714. spin_unlock_irq(&bp->lock);
  1715. do {
  1716. data_src = &hwstat->tx_good_octets;
  1717. data_dst = data;
  1718. start = u64_stats_fetch_begin_irq(&hwstat->syncp);
  1719. for (i = 0; i < ARRAY_SIZE(b44_gstrings); i++)
  1720. *data_dst++ = *data_src++;
  1721. } while (u64_stats_fetch_retry_irq(&hwstat->syncp, start));
  1722. }
  1723. static void b44_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1724. {
  1725. struct b44 *bp = netdev_priv(dev);
  1726. wol->supported = WAKE_MAGIC;
  1727. if (bp->flags & B44_FLAG_WOL_ENABLE)
  1728. wol->wolopts = WAKE_MAGIC;
  1729. else
  1730. wol->wolopts = 0;
  1731. memset(&wol->sopass, 0, sizeof(wol->sopass));
  1732. }
  1733. static int b44_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1734. {
  1735. struct b44 *bp = netdev_priv(dev);
  1736. spin_lock_irq(&bp->lock);
  1737. if (wol->wolopts & WAKE_MAGIC)
  1738. bp->flags |= B44_FLAG_WOL_ENABLE;
  1739. else
  1740. bp->flags &= ~B44_FLAG_WOL_ENABLE;
  1741. spin_unlock_irq(&bp->lock);
  1742. device_set_wakeup_enable(bp->sdev->dev, wol->wolopts & WAKE_MAGIC);
  1743. return 0;
  1744. }
  1745. static const struct ethtool_ops b44_ethtool_ops = {
  1746. .get_drvinfo = b44_get_drvinfo,
  1747. .nway_reset = b44_nway_reset,
  1748. .get_link = ethtool_op_get_link,
  1749. .get_wol = b44_get_wol,
  1750. .set_wol = b44_set_wol,
  1751. .get_ringparam = b44_get_ringparam,
  1752. .set_ringparam = b44_set_ringparam,
  1753. .get_pauseparam = b44_get_pauseparam,
  1754. .set_pauseparam = b44_set_pauseparam,
  1755. .get_msglevel = b44_get_msglevel,
  1756. .set_msglevel = b44_set_msglevel,
  1757. .get_strings = b44_get_strings,
  1758. .get_sset_count = b44_get_sset_count,
  1759. .get_ethtool_stats = b44_get_ethtool_stats,
  1760. .get_link_ksettings = b44_get_link_ksettings,
  1761. .set_link_ksettings = b44_set_link_ksettings,
  1762. };
  1763. static int b44_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1764. {
  1765. struct b44 *bp = netdev_priv(dev);
  1766. int err = -EINVAL;
  1767. if (!netif_running(dev))
  1768. goto out;
  1769. spin_lock_irq(&bp->lock);
  1770. if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
  1771. BUG_ON(!dev->phydev);
  1772. err = phy_mii_ioctl(dev->phydev, ifr, cmd);
  1773. } else {
  1774. err = generic_mii_ioctl(&bp->mii_if, if_mii(ifr), cmd, NULL);
  1775. }
  1776. spin_unlock_irq(&bp->lock);
  1777. out:
  1778. return err;
  1779. }
  1780. static int b44_get_invariants(struct b44 *bp)
  1781. {
  1782. struct ssb_device *sdev = bp->sdev;
  1783. int err = 0;
  1784. u8 *addr;
  1785. bp->dma_offset = ssb_dma_translation(sdev);
  1786. if (sdev->bus->bustype == SSB_BUSTYPE_SSB &&
  1787. instance > 1) {
  1788. addr = sdev->bus->sprom.et1mac;
  1789. bp->phy_addr = sdev->bus->sprom.et1phyaddr;
  1790. } else {
  1791. addr = sdev->bus->sprom.et0mac;
  1792. bp->phy_addr = sdev->bus->sprom.et0phyaddr;
  1793. }
  1794. /* Some ROMs have buggy PHY addresses with the high
  1795. * bits set (sign extension?). Truncate them to a
  1796. * valid PHY address. */
  1797. bp->phy_addr &= 0x1F;
  1798. memcpy(bp->dev->dev_addr, addr, ETH_ALEN);
  1799. if (!is_valid_ether_addr(&bp->dev->dev_addr[0])){
  1800. pr_err("Invalid MAC address found in EEPROM\n");
  1801. return -EINVAL;
  1802. }
  1803. bp->imask = IMASK_DEF;
  1804. /* XXX - really required?
  1805. bp->flags |= B44_FLAG_BUGGY_TXPTR;
  1806. */
  1807. if (bp->sdev->id.revision >= 7)
  1808. bp->flags |= B44_FLAG_B0_ANDLATER;
  1809. return err;
  1810. }
  1811. static const struct net_device_ops b44_netdev_ops = {
  1812. .ndo_open = b44_open,
  1813. .ndo_stop = b44_close,
  1814. .ndo_start_xmit = b44_start_xmit,
  1815. .ndo_get_stats64 = b44_get_stats64,
  1816. .ndo_set_rx_mode = b44_set_rx_mode,
  1817. .ndo_set_mac_address = b44_set_mac_addr,
  1818. .ndo_validate_addr = eth_validate_addr,
  1819. .ndo_do_ioctl = b44_ioctl,
  1820. .ndo_tx_timeout = b44_tx_timeout,
  1821. .ndo_change_mtu = b44_change_mtu,
  1822. #ifdef CONFIG_NET_POLL_CONTROLLER
  1823. .ndo_poll_controller = b44_poll_controller,
  1824. #endif
  1825. };
  1826. static void b44_adjust_link(struct net_device *dev)
  1827. {
  1828. struct b44 *bp = netdev_priv(dev);
  1829. struct phy_device *phydev = dev->phydev;
  1830. bool status_changed = 0;
  1831. BUG_ON(!phydev);
  1832. if (bp->old_link != phydev->link) {
  1833. status_changed = 1;
  1834. bp->old_link = phydev->link;
  1835. }
  1836. /* reflect duplex change */
  1837. if (phydev->link) {
  1838. if ((phydev->duplex == DUPLEX_HALF) &&
  1839. (bp->flags & B44_FLAG_FULL_DUPLEX)) {
  1840. status_changed = 1;
  1841. bp->flags &= ~B44_FLAG_FULL_DUPLEX;
  1842. } else if ((phydev->duplex == DUPLEX_FULL) &&
  1843. !(bp->flags & B44_FLAG_FULL_DUPLEX)) {
  1844. status_changed = 1;
  1845. bp->flags |= B44_FLAG_FULL_DUPLEX;
  1846. }
  1847. }
  1848. if (status_changed) {
  1849. u32 val = br32(bp, B44_TX_CTRL);
  1850. if (bp->flags & B44_FLAG_FULL_DUPLEX)
  1851. val |= TX_CTRL_DUPLEX;
  1852. else
  1853. val &= ~TX_CTRL_DUPLEX;
  1854. bw32(bp, B44_TX_CTRL, val);
  1855. phy_print_status(phydev);
  1856. }
  1857. }
  1858. static int b44_register_phy_one(struct b44 *bp)
  1859. {
  1860. struct mii_bus *mii_bus;
  1861. struct ssb_device *sdev = bp->sdev;
  1862. struct phy_device *phydev;
  1863. char bus_id[MII_BUS_ID_SIZE + 3];
  1864. struct ssb_sprom *sprom = &sdev->bus->sprom;
  1865. int err;
  1866. mii_bus = mdiobus_alloc();
  1867. if (!mii_bus) {
  1868. dev_err(sdev->dev, "mdiobus_alloc() failed\n");
  1869. err = -ENOMEM;
  1870. goto err_out;
  1871. }
  1872. mii_bus->priv = bp;
  1873. mii_bus->read = b44_mdio_read_phylib;
  1874. mii_bus->write = b44_mdio_write_phylib;
  1875. mii_bus->name = "b44_eth_mii";
  1876. mii_bus->parent = sdev->dev;
  1877. mii_bus->phy_mask = ~(1 << bp->phy_addr);
  1878. snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%x", instance);
  1879. bp->mii_bus = mii_bus;
  1880. err = mdiobus_register(mii_bus);
  1881. if (err) {
  1882. dev_err(sdev->dev, "failed to register MII bus\n");
  1883. goto err_out_mdiobus;
  1884. }
  1885. if (!mdiobus_is_registered_device(bp->mii_bus, bp->phy_addr) &&
  1886. (sprom->boardflags_lo & (B44_BOARDFLAG_ROBO | B44_BOARDFLAG_ADM))) {
  1887. dev_info(sdev->dev,
  1888. "could not find PHY at %i, use fixed one\n",
  1889. bp->phy_addr);
  1890. bp->phy_addr = 0;
  1891. snprintf(bus_id, sizeof(bus_id), PHY_ID_FMT, "fixed-0",
  1892. bp->phy_addr);
  1893. } else {
  1894. snprintf(bus_id, sizeof(bus_id), PHY_ID_FMT, mii_bus->id,
  1895. bp->phy_addr);
  1896. }
  1897. phydev = phy_connect(bp->dev, bus_id, &b44_adjust_link,
  1898. PHY_INTERFACE_MODE_MII);
  1899. if (IS_ERR(phydev)) {
  1900. dev_err(sdev->dev, "could not attach PHY at %i\n",
  1901. bp->phy_addr);
  1902. err = PTR_ERR(phydev);
  1903. goto err_out_mdiobus_unregister;
  1904. }
  1905. /* mask with MAC supported features */
  1906. phydev->supported &= (SUPPORTED_100baseT_Half |
  1907. SUPPORTED_100baseT_Full |
  1908. SUPPORTED_Autoneg |
  1909. SUPPORTED_MII);
  1910. phydev->advertising = phydev->supported;
  1911. bp->old_link = 0;
  1912. bp->phy_addr = phydev->mdio.addr;
  1913. phy_attached_info(phydev);
  1914. return 0;
  1915. err_out_mdiobus_unregister:
  1916. mdiobus_unregister(mii_bus);
  1917. err_out_mdiobus:
  1918. mdiobus_free(mii_bus);
  1919. err_out:
  1920. return err;
  1921. }
  1922. static void b44_unregister_phy_one(struct b44 *bp)
  1923. {
  1924. struct net_device *dev = bp->dev;
  1925. struct mii_bus *mii_bus = bp->mii_bus;
  1926. phy_disconnect(dev->phydev);
  1927. mdiobus_unregister(mii_bus);
  1928. mdiobus_free(mii_bus);
  1929. }
  1930. static int b44_init_one(struct ssb_device *sdev,
  1931. const struct ssb_device_id *ent)
  1932. {
  1933. struct net_device *dev;
  1934. struct b44 *bp;
  1935. int err;
  1936. instance++;
  1937. pr_info_once("%s version %s\n", DRV_DESCRIPTION, DRV_MODULE_VERSION);
  1938. dev = alloc_etherdev(sizeof(*bp));
  1939. if (!dev) {
  1940. err = -ENOMEM;
  1941. goto out;
  1942. }
  1943. SET_NETDEV_DEV(dev, sdev->dev);
  1944. /* No interesting netdevice features in this card... */
  1945. dev->features |= 0;
  1946. bp = netdev_priv(dev);
  1947. bp->sdev = sdev;
  1948. bp->dev = dev;
  1949. bp->force_copybreak = 0;
  1950. bp->msg_enable = netif_msg_init(b44_debug, B44_DEF_MSG_ENABLE);
  1951. spin_lock_init(&bp->lock);
  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->irq = sdev->irq;
  1958. dev->ethtool_ops = &b44_ethtool_ops;
  1959. err = ssb_bus_powerup(sdev->bus, 0);
  1960. if (err) {
  1961. dev_err(sdev->dev,
  1962. "Failed to powerup the bus\n");
  1963. goto err_out_free_dev;
  1964. }
  1965. if (dma_set_mask_and_coherent(sdev->dma_dev, DMA_BIT_MASK(30))) {
  1966. dev_err(sdev->dev,
  1967. "Required 30BIT DMA mask unsupported by the system\n");
  1968. goto err_out_powerdown;
  1969. }
  1970. err = b44_get_invariants(bp);
  1971. if (err) {
  1972. dev_err(sdev->dev,
  1973. "Problem fetching invariants of chip, aborting\n");
  1974. goto err_out_powerdown;
  1975. }
  1976. if (bp->phy_addr == B44_PHY_ADDR_NO_PHY) {
  1977. dev_err(sdev->dev, "No PHY present on this MAC, aborting\n");
  1978. err = -ENODEV;
  1979. goto err_out_powerdown;
  1980. }
  1981. bp->mii_if.dev = dev;
  1982. bp->mii_if.mdio_read = b44_mdio_read_mii;
  1983. bp->mii_if.mdio_write = b44_mdio_write_mii;
  1984. bp->mii_if.phy_id = bp->phy_addr;
  1985. bp->mii_if.phy_id_mask = 0x1f;
  1986. bp->mii_if.reg_num_mask = 0x1f;
  1987. /* By default, advertise all speed/duplex settings. */
  1988. bp->flags |= (B44_FLAG_ADV_10HALF | B44_FLAG_ADV_10FULL |
  1989. B44_FLAG_ADV_100HALF | B44_FLAG_ADV_100FULL);
  1990. /* By default, auto-negotiate PAUSE. */
  1991. bp->flags |= B44_FLAG_PAUSE_AUTO;
  1992. err = register_netdev(dev);
  1993. if (err) {
  1994. dev_err(sdev->dev, "Cannot register net device, aborting\n");
  1995. goto err_out_powerdown;
  1996. }
  1997. netif_carrier_off(dev);
  1998. ssb_set_drvdata(sdev, dev);
  1999. /* Chip reset provides power to the b44 MAC & PCI cores, which
  2000. * is necessary for MAC register access.
  2001. */
  2002. b44_chip_reset(bp, B44_CHIP_RESET_FULL);
  2003. /* do a phy reset to test if there is an active phy */
  2004. err = b44_phy_reset(bp);
  2005. if (err < 0) {
  2006. dev_err(sdev->dev, "phy reset failed\n");
  2007. goto err_out_unregister_netdev;
  2008. }
  2009. if (bp->flags & B44_FLAG_EXTERNAL_PHY) {
  2010. err = b44_register_phy_one(bp);
  2011. if (err) {
  2012. dev_err(sdev->dev, "Cannot register PHY, aborting\n");
  2013. goto err_out_unregister_netdev;
  2014. }
  2015. }
  2016. device_set_wakeup_capable(sdev->dev, true);
  2017. netdev_info(dev, "%s %pM\n", DRV_DESCRIPTION, dev->dev_addr);
  2018. return 0;
  2019. err_out_unregister_netdev:
  2020. unregister_netdev(dev);
  2021. err_out_powerdown:
  2022. ssb_bus_may_powerdown(sdev->bus);
  2023. err_out_free_dev:
  2024. netif_napi_del(&bp->napi);
  2025. free_netdev(dev);
  2026. out:
  2027. return err;
  2028. }
  2029. static void b44_remove_one(struct ssb_device *sdev)
  2030. {
  2031. struct net_device *dev = ssb_get_drvdata(sdev);
  2032. struct b44 *bp = netdev_priv(dev);
  2033. unregister_netdev(dev);
  2034. if (bp->flags & B44_FLAG_EXTERNAL_PHY)
  2035. b44_unregister_phy_one(bp);
  2036. ssb_device_disable(sdev, 0);
  2037. ssb_bus_may_powerdown(sdev->bus);
  2038. netif_napi_del(&bp->napi);
  2039. free_netdev(dev);
  2040. ssb_pcihost_set_power_state(sdev, PCI_D3hot);
  2041. ssb_set_drvdata(sdev, NULL);
  2042. }
  2043. static int b44_suspend(struct ssb_device *sdev, pm_message_t state)
  2044. {
  2045. struct net_device *dev = ssb_get_drvdata(sdev);
  2046. struct b44 *bp = netdev_priv(dev);
  2047. if (!netif_running(dev))
  2048. return 0;
  2049. del_timer_sync(&bp->timer);
  2050. spin_lock_irq(&bp->lock);
  2051. b44_halt(bp);
  2052. netif_carrier_off(bp->dev);
  2053. netif_device_detach(bp->dev);
  2054. b44_free_rings(bp);
  2055. spin_unlock_irq(&bp->lock);
  2056. free_irq(dev->irq, dev);
  2057. if (bp->flags & B44_FLAG_WOL_ENABLE) {
  2058. b44_init_hw(bp, B44_PARTIAL_RESET);
  2059. b44_setup_wol(bp);
  2060. }
  2061. ssb_pcihost_set_power_state(sdev, PCI_D3hot);
  2062. return 0;
  2063. }
  2064. static int b44_resume(struct ssb_device *sdev)
  2065. {
  2066. struct net_device *dev = ssb_get_drvdata(sdev);
  2067. struct b44 *bp = netdev_priv(dev);
  2068. int rc = 0;
  2069. rc = ssb_bus_powerup(sdev->bus, 0);
  2070. if (rc) {
  2071. dev_err(sdev->dev,
  2072. "Failed to powerup the bus\n");
  2073. return rc;
  2074. }
  2075. if (!netif_running(dev))
  2076. return 0;
  2077. spin_lock_irq(&bp->lock);
  2078. b44_init_rings(bp);
  2079. b44_init_hw(bp, B44_FULL_RESET);
  2080. spin_unlock_irq(&bp->lock);
  2081. /*
  2082. * As a shared interrupt, the handler can be called immediately. To be
  2083. * able to check the interrupt status the hardware must already be
  2084. * powered back on (b44_init_hw).
  2085. */
  2086. rc = request_irq(dev->irq, b44_interrupt, IRQF_SHARED, dev->name, dev);
  2087. if (rc) {
  2088. netdev_err(dev, "request_irq failed\n");
  2089. spin_lock_irq(&bp->lock);
  2090. b44_halt(bp);
  2091. b44_free_rings(bp);
  2092. spin_unlock_irq(&bp->lock);
  2093. return rc;
  2094. }
  2095. netif_device_attach(bp->dev);
  2096. b44_enable_ints(bp);
  2097. netif_wake_queue(dev);
  2098. mod_timer(&bp->timer, jiffies + 1);
  2099. return 0;
  2100. }
  2101. static struct ssb_driver b44_ssb_driver = {
  2102. .name = DRV_MODULE_NAME,
  2103. .id_table = b44_ssb_tbl,
  2104. .probe = b44_init_one,
  2105. .remove = b44_remove_one,
  2106. .suspend = b44_suspend,
  2107. .resume = b44_resume,
  2108. };
  2109. static inline int __init b44_pci_init(void)
  2110. {
  2111. int err = 0;
  2112. #ifdef CONFIG_B44_PCI
  2113. err = ssb_pcihost_register(&b44_pci_driver);
  2114. #endif
  2115. return err;
  2116. }
  2117. static inline void b44_pci_exit(void)
  2118. {
  2119. #ifdef CONFIG_B44_PCI
  2120. ssb_pcihost_unregister(&b44_pci_driver);
  2121. #endif
  2122. }
  2123. static int __init b44_init(void)
  2124. {
  2125. unsigned int dma_desc_align_size = dma_get_cache_alignment();
  2126. int err;
  2127. /* Setup paramaters for syncing RX/TX DMA descriptors */
  2128. dma_desc_sync_size = max_t(unsigned int, dma_desc_align_size, sizeof(struct dma_desc));
  2129. err = b44_pci_init();
  2130. if (err)
  2131. return err;
  2132. err = ssb_driver_register(&b44_ssb_driver);
  2133. if (err)
  2134. b44_pci_exit();
  2135. return err;
  2136. }
  2137. static void __exit b44_cleanup(void)
  2138. {
  2139. ssb_driver_unregister(&b44_ssb_driver);
  2140. b44_pci_exit();
  2141. }
  2142. module_init(b44_init);
  2143. module_exit(b44_cleanup);