tehuti.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469
  1. /*
  2. * Tehuti Networks(R) Network Driver
  3. * ethtool interface implementation
  4. * Copyright (C) 2007 Tehuti Networks Ltd. All rights reserved
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. /*
  12. * RX HW/SW interaction overview
  13. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. * There are 2 types of RX communication channels between driver and NIC.
  15. * 1) RX Free Fifo - RXF - holds descriptors of empty buffers to accept incoming
  16. * traffic. This Fifo is filled by SW and is readen by HW. Each descriptor holds
  17. * info about buffer's location, size and ID. An ID field is used to identify a
  18. * buffer when it's returned with data via RXD Fifo (see below)
  19. * 2) RX Data Fifo - RXD - holds descriptors of full buffers. This Fifo is
  20. * filled by HW and is readen by SW. Each descriptor holds status and ID.
  21. * HW pops descriptor from RXF Fifo, stores ID, fills buffer with incoming data,
  22. * via dma moves it into host memory, builds new RXD descriptor with same ID,
  23. * pushes it into RXD Fifo and raises interrupt to indicate new RX data.
  24. *
  25. * Current NIC configuration (registers + firmware) makes NIC use 2 RXF Fifos.
  26. * One holds 1.5K packets and another - 26K packets. Depending on incoming
  27. * packet size, HW desides on a RXF Fifo to pop buffer from. When packet is
  28. * filled with data, HW builds new RXD descriptor for it and push it into single
  29. * RXD Fifo.
  30. *
  31. * RX SW Data Structures
  32. * ~~~~~~~~~~~~~~~~~~~~~
  33. * skb db - used to keep track of all skbs owned by SW and their dma addresses.
  34. * For RX case, ownership lasts from allocating new empty skb for RXF until
  35. * accepting full skb from RXD and passing it to OS. Each RXF Fifo has its own
  36. * skb db. Implemented as array with bitmask.
  37. * fifo - keeps info about fifo's size and location, relevant HW registers,
  38. * usage and skb db. Each RXD and RXF Fifo has its own fifo structure.
  39. * Implemented as simple struct.
  40. *
  41. * RX SW Execution Flow
  42. * ~~~~~~~~~~~~~~~~~~~~
  43. * Upon initialization (ifconfig up) driver creates RX fifos and initializes
  44. * relevant registers. At the end of init phase, driver enables interrupts.
  45. * NIC sees that there is no RXF buffers and raises
  46. * RD_INTR interrupt, isr fills skbs and Rx begins.
  47. * Driver has two receive operation modes:
  48. * NAPI - interrupt-driven mixed with polling
  49. * interrupt-driven only
  50. *
  51. * Interrupt-driven only flow is following. When buffer is ready, HW raises
  52. * interrupt and isr is called. isr collects all available packets
  53. * (bdx_rx_receive), refills skbs (bdx_rx_alloc_skbs) and exit.
  54. * Rx buffer allocation note
  55. * ~~~~~~~~~~~~~~~~~~~~~~~~~
  56. * Driver cares to feed such amount of RxF descriptors that respective amount of
  57. * RxD descriptors can not fill entire RxD fifo. The main reason is lack of
  58. * overflow check in Bordeaux for RxD fifo free/used size.
  59. * FIXME: this is NOT fully implemented, more work should be done
  60. *
  61. */
  62. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  63. #include "tehuti.h"
  64. static const struct pci_device_id bdx_pci_tbl[] = {
  65. { PCI_VDEVICE(TEHUTI, 0x3009), },
  66. { PCI_VDEVICE(TEHUTI, 0x3010), },
  67. { PCI_VDEVICE(TEHUTI, 0x3014), },
  68. { 0 }
  69. };
  70. MODULE_DEVICE_TABLE(pci, bdx_pci_tbl);
  71. /* Definitions needed by ISR or NAPI functions */
  72. static void bdx_rx_alloc_skbs(struct bdx_priv *priv, struct rxf_fifo *f);
  73. static void bdx_tx_cleanup(struct bdx_priv *priv);
  74. static int bdx_rx_receive(struct bdx_priv *priv, struct rxd_fifo *f, int budget);
  75. /* Definitions needed by FW loading */
  76. static void bdx_tx_push_desc_safe(struct bdx_priv *priv, void *data, int size);
  77. /* Definitions needed by hw_start */
  78. static int bdx_tx_init(struct bdx_priv *priv);
  79. static int bdx_rx_init(struct bdx_priv *priv);
  80. /* Definitions needed by bdx_close */
  81. static void bdx_rx_free(struct bdx_priv *priv);
  82. static void bdx_tx_free(struct bdx_priv *priv);
  83. /* Definitions needed by bdx_probe */
  84. static void bdx_set_ethtool_ops(struct net_device *netdev);
  85. /*************************************************************************
  86. * Print Info *
  87. *************************************************************************/
  88. static void print_hw_id(struct pci_dev *pdev)
  89. {
  90. struct pci_nic *nic = pci_get_drvdata(pdev);
  91. u16 pci_link_status = 0;
  92. u16 pci_ctrl = 0;
  93. pci_read_config_word(pdev, PCI_LINK_STATUS_REG, &pci_link_status);
  94. pci_read_config_word(pdev, PCI_DEV_CTRL_REG, &pci_ctrl);
  95. pr_info("%s%s\n", BDX_NIC_NAME,
  96. nic->port_num == 1 ? "" : ", 2-Port");
  97. pr_info("srom 0x%x fpga %d build %u lane# %d max_pl 0x%x mrrs 0x%x\n",
  98. readl(nic->regs + SROM_VER), readl(nic->regs + FPGA_VER) & 0xFFF,
  99. readl(nic->regs + FPGA_SEED),
  100. GET_LINK_STATUS_LANES(pci_link_status),
  101. GET_DEV_CTRL_MAXPL(pci_ctrl), GET_DEV_CTRL_MRRS(pci_ctrl));
  102. }
  103. static void print_fw_id(struct pci_nic *nic)
  104. {
  105. pr_info("fw 0x%x\n", readl(nic->regs + FW_VER));
  106. }
  107. static void print_eth_id(struct net_device *ndev)
  108. {
  109. netdev_info(ndev, "%s, Port %c\n",
  110. BDX_NIC_NAME, (ndev->if_port == 0) ? 'A' : 'B');
  111. }
  112. /*************************************************************************
  113. * Code *
  114. *************************************************************************/
  115. #define bdx_enable_interrupts(priv) \
  116. do { WRITE_REG(priv, regIMR, IR_RUN); } while (0)
  117. #define bdx_disable_interrupts(priv) \
  118. do { WRITE_REG(priv, regIMR, 0); } while (0)
  119. /**
  120. * bdx_fifo_init - create TX/RX descriptor fifo for host-NIC communication.
  121. * @priv: NIC private structure
  122. * @f: fifo to initialize
  123. * @fsz_type: fifo size type: 0-4KB, 1-8KB, 2-16KB, 3-32KB
  124. * @reg_XXX: offsets of registers relative to base address
  125. *
  126. * 1K extra space is allocated at the end of the fifo to simplify
  127. * processing of descriptors that wraps around fifo's end
  128. *
  129. * Returns 0 on success, negative value on failure
  130. *
  131. */
  132. static int
  133. bdx_fifo_init(struct bdx_priv *priv, struct fifo *f, int fsz_type,
  134. u16 reg_CFG0, u16 reg_CFG1, u16 reg_RPTR, u16 reg_WPTR)
  135. {
  136. u16 memsz = FIFO_SIZE * (1 << fsz_type);
  137. memset(f, 0, sizeof(struct fifo));
  138. /* pci_alloc_consistent gives us 4k-aligned memory */
  139. f->va = pci_alloc_consistent(priv->pdev,
  140. memsz + FIFO_EXTRA_SPACE, &f->da);
  141. if (!f->va) {
  142. pr_err("pci_alloc_consistent failed\n");
  143. RET(-ENOMEM);
  144. }
  145. f->reg_CFG0 = reg_CFG0;
  146. f->reg_CFG1 = reg_CFG1;
  147. f->reg_RPTR = reg_RPTR;
  148. f->reg_WPTR = reg_WPTR;
  149. f->rptr = 0;
  150. f->wptr = 0;
  151. f->memsz = memsz;
  152. f->size_mask = memsz - 1;
  153. WRITE_REG(priv, reg_CFG0, (u32) ((f->da & TX_RX_CFG0_BASE) | fsz_type));
  154. WRITE_REG(priv, reg_CFG1, H32_64(f->da));
  155. RET(0);
  156. }
  157. /**
  158. * bdx_fifo_free - free all resources used by fifo
  159. * @priv: NIC private structure
  160. * @f: fifo to release
  161. */
  162. static void bdx_fifo_free(struct bdx_priv *priv, struct fifo *f)
  163. {
  164. ENTER;
  165. if (f->va) {
  166. pci_free_consistent(priv->pdev,
  167. f->memsz + FIFO_EXTRA_SPACE, f->va, f->da);
  168. f->va = NULL;
  169. }
  170. RET();
  171. }
  172. /**
  173. * bdx_link_changed - notifies OS about hw link state.
  174. * @priv: hw adapter structure
  175. */
  176. static void bdx_link_changed(struct bdx_priv *priv)
  177. {
  178. u32 link = READ_REG(priv, regMAC_LNK_STAT) & MAC_LINK_STAT;
  179. if (!link) {
  180. if (netif_carrier_ok(priv->ndev)) {
  181. netif_stop_queue(priv->ndev);
  182. netif_carrier_off(priv->ndev);
  183. netdev_err(priv->ndev, "Link Down\n");
  184. }
  185. } else {
  186. if (!netif_carrier_ok(priv->ndev)) {
  187. netif_wake_queue(priv->ndev);
  188. netif_carrier_on(priv->ndev);
  189. netdev_err(priv->ndev, "Link Up\n");
  190. }
  191. }
  192. }
  193. static void bdx_isr_extra(struct bdx_priv *priv, u32 isr)
  194. {
  195. if (isr & IR_RX_FREE_0) {
  196. bdx_rx_alloc_skbs(priv, &priv->rxf_fifo0);
  197. DBG("RX_FREE_0\n");
  198. }
  199. if (isr & IR_LNKCHG0)
  200. bdx_link_changed(priv);
  201. if (isr & IR_PCIE_LINK)
  202. netdev_err(priv->ndev, "PCI-E Link Fault\n");
  203. if (isr & IR_PCIE_TOUT)
  204. netdev_err(priv->ndev, "PCI-E Time Out\n");
  205. }
  206. /**
  207. * bdx_isr_napi - Interrupt Service Routine for Bordeaux NIC
  208. * @irq: interrupt number
  209. * @dev: network device
  210. *
  211. * Return IRQ_NONE if it was not our interrupt, IRQ_HANDLED - otherwise
  212. *
  213. * It reads ISR register to know interrupt reasons, and proceed them one by one.
  214. * Reasons of interest are:
  215. * RX_DESC - new packet has arrived and RXD fifo holds its descriptor
  216. * RX_FREE - number of free Rx buffers in RXF fifo gets low
  217. * TX_FREE - packet was transmited and RXF fifo holds its descriptor
  218. */
  219. static irqreturn_t bdx_isr_napi(int irq, void *dev)
  220. {
  221. struct net_device *ndev = dev;
  222. struct bdx_priv *priv = netdev_priv(ndev);
  223. u32 isr;
  224. ENTER;
  225. isr = (READ_REG(priv, regISR) & IR_RUN);
  226. if (unlikely(!isr)) {
  227. bdx_enable_interrupts(priv);
  228. return IRQ_NONE; /* Not our interrupt */
  229. }
  230. if (isr & IR_EXTRA)
  231. bdx_isr_extra(priv, isr);
  232. if (isr & (IR_RX_DESC_0 | IR_TX_FREE_0)) {
  233. if (likely(napi_schedule_prep(&priv->napi))) {
  234. __napi_schedule(&priv->napi);
  235. RET(IRQ_HANDLED);
  236. } else {
  237. /* NOTE: we get here if intr has slipped into window
  238. * between these lines in bdx_poll:
  239. * bdx_enable_interrupts(priv);
  240. * return 0;
  241. * currently intrs are disabled (since we read ISR),
  242. * and we have failed to register next poll.
  243. * so we read the regs to trigger chip
  244. * and allow further interupts. */
  245. READ_REG(priv, regTXF_WPTR_0);
  246. READ_REG(priv, regRXD_WPTR_0);
  247. }
  248. }
  249. bdx_enable_interrupts(priv);
  250. RET(IRQ_HANDLED);
  251. }
  252. static int bdx_poll(struct napi_struct *napi, int budget)
  253. {
  254. struct bdx_priv *priv = container_of(napi, struct bdx_priv, napi);
  255. int work_done;
  256. ENTER;
  257. bdx_tx_cleanup(priv);
  258. work_done = bdx_rx_receive(priv, &priv->rxd_fifo0, budget);
  259. if ((work_done < budget) ||
  260. (priv->napi_stop++ >= 30)) {
  261. DBG("rx poll is done. backing to isr-driven\n");
  262. /* from time to time we exit to let NAPI layer release
  263. * device lock and allow waiting tasks (eg rmmod) to advance) */
  264. priv->napi_stop = 0;
  265. napi_complete_done(napi, work_done);
  266. bdx_enable_interrupts(priv);
  267. }
  268. return work_done;
  269. }
  270. /**
  271. * bdx_fw_load - loads firmware to NIC
  272. * @priv: NIC private structure
  273. *
  274. * Firmware is loaded via TXD fifo, so it must be initialized first.
  275. * Firware must be loaded once per NIC not per PCI device provided by NIC (NIC
  276. * can have few of them). So all drivers use semaphore register to choose one
  277. * that will actually load FW to NIC.
  278. */
  279. static int bdx_fw_load(struct bdx_priv *priv)
  280. {
  281. const struct firmware *fw = NULL;
  282. int master, i;
  283. int rc;
  284. ENTER;
  285. master = READ_REG(priv, regINIT_SEMAPHORE);
  286. if (!READ_REG(priv, regINIT_STATUS) && master) {
  287. rc = request_firmware(&fw, "tehuti/bdx.bin", &priv->pdev->dev);
  288. if (rc)
  289. goto out;
  290. bdx_tx_push_desc_safe(priv, (char *)fw->data, fw->size);
  291. mdelay(100);
  292. }
  293. for (i = 0; i < 200; i++) {
  294. if (READ_REG(priv, regINIT_STATUS)) {
  295. rc = 0;
  296. goto out;
  297. }
  298. mdelay(2);
  299. }
  300. rc = -EIO;
  301. out:
  302. if (master)
  303. WRITE_REG(priv, regINIT_SEMAPHORE, 1);
  304. release_firmware(fw);
  305. if (rc) {
  306. netdev_err(priv->ndev, "firmware loading failed\n");
  307. if (rc == -EIO)
  308. DBG("VPC = 0x%x VIC = 0x%x INIT_STATUS = 0x%x i=%d\n",
  309. READ_REG(priv, regVPC),
  310. READ_REG(priv, regVIC),
  311. READ_REG(priv, regINIT_STATUS), i);
  312. RET(rc);
  313. } else {
  314. DBG("%s: firmware loading success\n", priv->ndev->name);
  315. RET(0);
  316. }
  317. }
  318. static void bdx_restore_mac(struct net_device *ndev, struct bdx_priv *priv)
  319. {
  320. u32 val;
  321. ENTER;
  322. DBG("mac0=%x mac1=%x mac2=%x\n",
  323. READ_REG(priv, regUNC_MAC0_A),
  324. READ_REG(priv, regUNC_MAC1_A), READ_REG(priv, regUNC_MAC2_A));
  325. val = (ndev->dev_addr[0] << 8) | (ndev->dev_addr[1]);
  326. WRITE_REG(priv, regUNC_MAC2_A, val);
  327. val = (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]);
  328. WRITE_REG(priv, regUNC_MAC1_A, val);
  329. val = (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]);
  330. WRITE_REG(priv, regUNC_MAC0_A, val);
  331. DBG("mac0=%x mac1=%x mac2=%x\n",
  332. READ_REG(priv, regUNC_MAC0_A),
  333. READ_REG(priv, regUNC_MAC1_A), READ_REG(priv, regUNC_MAC2_A));
  334. RET();
  335. }
  336. /**
  337. * bdx_hw_start - inits registers and starts HW's Rx and Tx engines
  338. * @priv: NIC private structure
  339. */
  340. static int bdx_hw_start(struct bdx_priv *priv)
  341. {
  342. int rc = -EIO;
  343. struct net_device *ndev = priv->ndev;
  344. ENTER;
  345. bdx_link_changed(priv);
  346. /* 10G overall max length (vlan, eth&ip header, ip payload, crc) */
  347. WRITE_REG(priv, regFRM_LENGTH, 0X3FE0);
  348. WRITE_REG(priv, regPAUSE_QUANT, 0x96);
  349. WRITE_REG(priv, regRX_FIFO_SECTION, 0x800010);
  350. WRITE_REG(priv, regTX_FIFO_SECTION, 0xE00010);
  351. WRITE_REG(priv, regRX_FULLNESS, 0);
  352. WRITE_REG(priv, regTX_FULLNESS, 0);
  353. WRITE_REG(priv, regCTRLST,
  354. regCTRLST_BASE | regCTRLST_RX_ENA | regCTRLST_TX_ENA);
  355. WRITE_REG(priv, regVGLB, 0);
  356. WRITE_REG(priv, regMAX_FRAME_A,
  357. priv->rxf_fifo0.m.pktsz & MAX_FRAME_AB_VAL);
  358. DBG("RDINTCM=%08x\n", priv->rdintcm); /*NOTE: test script uses this */
  359. WRITE_REG(priv, regRDINTCM0, priv->rdintcm);
  360. WRITE_REG(priv, regRDINTCM2, 0); /*cpu_to_le32(rcm.val)); */
  361. DBG("TDINTCM=%08x\n", priv->tdintcm); /*NOTE: test script uses this */
  362. WRITE_REG(priv, regTDINTCM0, priv->tdintcm); /* old val = 0x300064 */
  363. /* Enable timer interrupt once in 2 secs. */
  364. /*WRITE_REG(priv, regGTMR0, ((GTMR_SEC * 2) & GTMR_DATA)); */
  365. bdx_restore_mac(priv->ndev, priv);
  366. WRITE_REG(priv, regGMAC_RXF_A, GMAC_RX_FILTER_OSEN |
  367. GMAC_RX_FILTER_AM | GMAC_RX_FILTER_AB);
  368. #define BDX_IRQ_TYPE ((priv->nic->irq_type == IRQ_MSI) ? 0 : IRQF_SHARED)
  369. rc = request_irq(priv->pdev->irq, bdx_isr_napi, BDX_IRQ_TYPE,
  370. ndev->name, ndev);
  371. if (rc)
  372. goto err_irq;
  373. bdx_enable_interrupts(priv);
  374. RET(0);
  375. err_irq:
  376. RET(rc);
  377. }
  378. static void bdx_hw_stop(struct bdx_priv *priv)
  379. {
  380. ENTER;
  381. bdx_disable_interrupts(priv);
  382. free_irq(priv->pdev->irq, priv->ndev);
  383. netif_carrier_off(priv->ndev);
  384. netif_stop_queue(priv->ndev);
  385. RET();
  386. }
  387. static int bdx_hw_reset_direct(void __iomem *regs)
  388. {
  389. u32 val, i;
  390. ENTER;
  391. /* reset sequences: read, write 1, read, write 0 */
  392. val = readl(regs + regCLKPLL);
  393. writel((val | CLKPLL_SFTRST) + 0x8, regs + regCLKPLL);
  394. udelay(50);
  395. val = readl(regs + regCLKPLL);
  396. writel(val & ~CLKPLL_SFTRST, regs + regCLKPLL);
  397. /* check that the PLLs are locked and reset ended */
  398. for (i = 0; i < 70; i++, mdelay(10))
  399. if ((readl(regs + regCLKPLL) & CLKPLL_LKD) == CLKPLL_LKD) {
  400. /* do any PCI-E read transaction */
  401. readl(regs + regRXD_CFG0_0);
  402. return 0;
  403. }
  404. pr_err("HW reset failed\n");
  405. return 1; /* failure */
  406. }
  407. static int bdx_hw_reset(struct bdx_priv *priv)
  408. {
  409. u32 val, i;
  410. ENTER;
  411. if (priv->port == 0) {
  412. /* reset sequences: read, write 1, read, write 0 */
  413. val = READ_REG(priv, regCLKPLL);
  414. WRITE_REG(priv, regCLKPLL, (val | CLKPLL_SFTRST) + 0x8);
  415. udelay(50);
  416. val = READ_REG(priv, regCLKPLL);
  417. WRITE_REG(priv, regCLKPLL, val & ~CLKPLL_SFTRST);
  418. }
  419. /* check that the PLLs are locked and reset ended */
  420. for (i = 0; i < 70; i++, mdelay(10))
  421. if ((READ_REG(priv, regCLKPLL) & CLKPLL_LKD) == CLKPLL_LKD) {
  422. /* do any PCI-E read transaction */
  423. READ_REG(priv, regRXD_CFG0_0);
  424. return 0;
  425. }
  426. pr_err("HW reset failed\n");
  427. return 1; /* failure */
  428. }
  429. static int bdx_sw_reset(struct bdx_priv *priv)
  430. {
  431. int i;
  432. ENTER;
  433. /* 1. load MAC (obsolete) */
  434. /* 2. disable Rx (and Tx) */
  435. WRITE_REG(priv, regGMAC_RXF_A, 0);
  436. mdelay(100);
  437. /* 3. disable port */
  438. WRITE_REG(priv, regDIS_PORT, 1);
  439. /* 4. disable queue */
  440. WRITE_REG(priv, regDIS_QU, 1);
  441. /* 5. wait until hw is disabled */
  442. for (i = 0; i < 50; i++) {
  443. if (READ_REG(priv, regRST_PORT) & 1)
  444. break;
  445. mdelay(10);
  446. }
  447. if (i == 50)
  448. netdev_err(priv->ndev, "SW reset timeout. continuing anyway\n");
  449. /* 6. disable intrs */
  450. WRITE_REG(priv, regRDINTCM0, 0);
  451. WRITE_REG(priv, regTDINTCM0, 0);
  452. WRITE_REG(priv, regIMR, 0);
  453. READ_REG(priv, regISR);
  454. /* 7. reset queue */
  455. WRITE_REG(priv, regRST_QU, 1);
  456. /* 8. reset port */
  457. WRITE_REG(priv, regRST_PORT, 1);
  458. /* 9. zero all read and write pointers */
  459. for (i = regTXD_WPTR_0; i <= regTXF_RPTR_3; i += 0x10)
  460. DBG("%x = %x\n", i, READ_REG(priv, i) & TXF_WPTR_WR_PTR);
  461. for (i = regTXD_WPTR_0; i <= regTXF_RPTR_3; i += 0x10)
  462. WRITE_REG(priv, i, 0);
  463. /* 10. unseet port disable */
  464. WRITE_REG(priv, regDIS_PORT, 0);
  465. /* 11. unset queue disable */
  466. WRITE_REG(priv, regDIS_QU, 0);
  467. /* 12. unset queue reset */
  468. WRITE_REG(priv, regRST_QU, 0);
  469. /* 13. unset port reset */
  470. WRITE_REG(priv, regRST_PORT, 0);
  471. /* 14. enable Rx */
  472. /* skiped. will be done later */
  473. /* 15. save MAC (obsolete) */
  474. for (i = regTXD_WPTR_0; i <= regTXF_RPTR_3; i += 0x10)
  475. DBG("%x = %x\n", i, READ_REG(priv, i) & TXF_WPTR_WR_PTR);
  476. RET(0);
  477. }
  478. /* bdx_reset - performs right type of reset depending on hw type */
  479. static int bdx_reset(struct bdx_priv *priv)
  480. {
  481. ENTER;
  482. RET((priv->pdev->device == 0x3009)
  483. ? bdx_hw_reset(priv)
  484. : bdx_sw_reset(priv));
  485. }
  486. /**
  487. * bdx_close - Disables a network interface
  488. * @netdev: network interface device structure
  489. *
  490. * Returns 0, this is not allowed to fail
  491. *
  492. * The close entry point is called when an interface is de-activated
  493. * by the OS. The hardware is still under the drivers control, but
  494. * needs to be disabled. A global MAC reset is issued to stop the
  495. * hardware, and all transmit and receive resources are freed.
  496. **/
  497. static int bdx_close(struct net_device *ndev)
  498. {
  499. struct bdx_priv *priv = NULL;
  500. ENTER;
  501. priv = netdev_priv(ndev);
  502. napi_disable(&priv->napi);
  503. bdx_reset(priv);
  504. bdx_hw_stop(priv);
  505. bdx_rx_free(priv);
  506. bdx_tx_free(priv);
  507. RET(0);
  508. }
  509. /**
  510. * bdx_open - Called when a network interface is made active
  511. * @netdev: network interface device structure
  512. *
  513. * Returns 0 on success, negative value on failure
  514. *
  515. * The open entry point is called when a network interface is made
  516. * active by the system (IFF_UP). At this point all resources needed
  517. * for transmit and receive operations are allocated, the interrupt
  518. * handler is registered with the OS, the watchdog timer is started,
  519. * and the stack is notified that the interface is ready.
  520. **/
  521. static int bdx_open(struct net_device *ndev)
  522. {
  523. struct bdx_priv *priv;
  524. int rc;
  525. ENTER;
  526. priv = netdev_priv(ndev);
  527. bdx_reset(priv);
  528. if (netif_running(ndev))
  529. netif_stop_queue(priv->ndev);
  530. if ((rc = bdx_tx_init(priv)) ||
  531. (rc = bdx_rx_init(priv)) ||
  532. (rc = bdx_fw_load(priv)))
  533. goto err;
  534. bdx_rx_alloc_skbs(priv, &priv->rxf_fifo0);
  535. rc = bdx_hw_start(priv);
  536. if (rc)
  537. goto err;
  538. napi_enable(&priv->napi);
  539. print_fw_id(priv->nic);
  540. RET(0);
  541. err:
  542. bdx_close(ndev);
  543. RET(rc);
  544. }
  545. static int bdx_range_check(struct bdx_priv *priv, u32 offset)
  546. {
  547. return (offset > (u32) (BDX_REGS_SIZE / priv->nic->port_num)) ?
  548. -EINVAL : 0;
  549. }
  550. static int bdx_ioctl_priv(struct net_device *ndev, struct ifreq *ifr, int cmd)
  551. {
  552. struct bdx_priv *priv = netdev_priv(ndev);
  553. u32 data[3];
  554. int error;
  555. ENTER;
  556. DBG("jiffies=%ld cmd=%d\n", jiffies, cmd);
  557. if (cmd != SIOCDEVPRIVATE) {
  558. error = copy_from_user(data, ifr->ifr_data, sizeof(data));
  559. if (error) {
  560. pr_err("can't copy from user\n");
  561. RET(-EFAULT);
  562. }
  563. DBG("%d 0x%x 0x%x\n", data[0], data[1], data[2]);
  564. } else {
  565. return -EOPNOTSUPP;
  566. }
  567. if (!capable(CAP_SYS_RAWIO))
  568. return -EPERM;
  569. switch (data[0]) {
  570. case BDX_OP_READ:
  571. error = bdx_range_check(priv, data[1]);
  572. if (error < 0)
  573. return error;
  574. data[2] = READ_REG(priv, data[1]);
  575. DBG("read_reg(0x%x)=0x%x (dec %d)\n", data[1], data[2],
  576. data[2]);
  577. error = copy_to_user(ifr->ifr_data, data, sizeof(data));
  578. if (error)
  579. RET(-EFAULT);
  580. break;
  581. case BDX_OP_WRITE:
  582. error = bdx_range_check(priv, data[1]);
  583. if (error < 0)
  584. return error;
  585. WRITE_REG(priv, data[1], data[2]);
  586. DBG("write_reg(0x%x, 0x%x)\n", data[1], data[2]);
  587. break;
  588. default:
  589. RET(-EOPNOTSUPP);
  590. }
  591. return 0;
  592. }
  593. static int bdx_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd)
  594. {
  595. ENTER;
  596. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
  597. RET(bdx_ioctl_priv(ndev, ifr, cmd));
  598. else
  599. RET(-EOPNOTSUPP);
  600. }
  601. /**
  602. * __bdx_vlan_rx_vid - private helper for adding/killing VLAN vid
  603. * @ndev: network device
  604. * @vid: VLAN vid
  605. * @op: add or kill operation
  606. *
  607. * Passes VLAN filter table to hardware
  608. */
  609. static void __bdx_vlan_rx_vid(struct net_device *ndev, uint16_t vid, int enable)
  610. {
  611. struct bdx_priv *priv = netdev_priv(ndev);
  612. u32 reg, bit, val;
  613. ENTER;
  614. DBG2("vid=%d value=%d\n", (int)vid, enable);
  615. if (unlikely(vid >= 4096)) {
  616. pr_err("invalid VID: %u (> 4096)\n", vid);
  617. RET();
  618. }
  619. reg = regVLAN_0 + (vid / 32) * 4;
  620. bit = 1 << vid % 32;
  621. val = READ_REG(priv, reg);
  622. DBG2("reg=%x, val=%x, bit=%d\n", reg, val, bit);
  623. if (enable)
  624. val |= bit;
  625. else
  626. val &= ~bit;
  627. DBG2("new val %x\n", val);
  628. WRITE_REG(priv, reg, val);
  629. RET();
  630. }
  631. /**
  632. * bdx_vlan_rx_add_vid - kernel hook for adding VLAN vid to hw filtering table
  633. * @ndev: network device
  634. * @vid: VLAN vid to add
  635. */
  636. static int bdx_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid)
  637. {
  638. __bdx_vlan_rx_vid(ndev, vid, 1);
  639. return 0;
  640. }
  641. /**
  642. * bdx_vlan_rx_kill_vid - kernel hook for killing VLAN vid in hw filtering table
  643. * @ndev: network device
  644. * @vid: VLAN vid to kill
  645. */
  646. static int bdx_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid)
  647. {
  648. __bdx_vlan_rx_vid(ndev, vid, 0);
  649. return 0;
  650. }
  651. /**
  652. * bdx_change_mtu - Change the Maximum Transfer Unit
  653. * @netdev: network interface device structure
  654. * @new_mtu: new value for maximum frame size
  655. *
  656. * Returns 0 on success, negative on failure
  657. */
  658. static int bdx_change_mtu(struct net_device *ndev, int new_mtu)
  659. {
  660. ENTER;
  661. ndev->mtu = new_mtu;
  662. if (netif_running(ndev)) {
  663. bdx_close(ndev);
  664. bdx_open(ndev);
  665. }
  666. RET(0);
  667. }
  668. static void bdx_setmulti(struct net_device *ndev)
  669. {
  670. struct bdx_priv *priv = netdev_priv(ndev);
  671. u32 rxf_val =
  672. GMAC_RX_FILTER_AM | GMAC_RX_FILTER_AB | GMAC_RX_FILTER_OSEN;
  673. int i;
  674. ENTER;
  675. /* IMF - imperfect (hash) rx multicat filter */
  676. /* PMF - perfect rx multicat filter */
  677. /* FIXME: RXE(OFF) */
  678. if (ndev->flags & IFF_PROMISC) {
  679. rxf_val |= GMAC_RX_FILTER_PRM;
  680. } else if (ndev->flags & IFF_ALLMULTI) {
  681. /* set IMF to accept all multicast frmaes */
  682. for (i = 0; i < MAC_MCST_HASH_NUM; i++)
  683. WRITE_REG(priv, regRX_MCST_HASH0 + i * 4, ~0);
  684. } else if (!netdev_mc_empty(ndev)) {
  685. u8 hash;
  686. struct netdev_hw_addr *ha;
  687. u32 reg, val;
  688. /* set IMF to deny all multicast frames */
  689. for (i = 0; i < MAC_MCST_HASH_NUM; i++)
  690. WRITE_REG(priv, regRX_MCST_HASH0 + i * 4, 0);
  691. /* set PMF to deny all multicast frames */
  692. for (i = 0; i < MAC_MCST_NUM; i++) {
  693. WRITE_REG(priv, regRX_MAC_MCST0 + i * 8, 0);
  694. WRITE_REG(priv, regRX_MAC_MCST1 + i * 8, 0);
  695. }
  696. /* use PMF to accept first MAC_MCST_NUM (15) addresses */
  697. /* TBD: sort addresses and write them in ascending order
  698. * into RX_MAC_MCST regs. we skip this phase now and accept ALL
  699. * multicast frames throu IMF */
  700. /* accept the rest of addresses throu IMF */
  701. netdev_for_each_mc_addr(ha, ndev) {
  702. hash = 0;
  703. for (i = 0; i < ETH_ALEN; i++)
  704. hash ^= ha->addr[i];
  705. reg = regRX_MCST_HASH0 + ((hash >> 5) << 2);
  706. val = READ_REG(priv, reg);
  707. val |= (1 << (hash % 32));
  708. WRITE_REG(priv, reg, val);
  709. }
  710. } else {
  711. DBG("only own mac %d\n", netdev_mc_count(ndev));
  712. rxf_val |= GMAC_RX_FILTER_AB;
  713. }
  714. WRITE_REG(priv, regGMAC_RXF_A, rxf_val);
  715. /* enable RX */
  716. /* FIXME: RXE(ON) */
  717. RET();
  718. }
  719. static int bdx_set_mac(struct net_device *ndev, void *p)
  720. {
  721. struct bdx_priv *priv = netdev_priv(ndev);
  722. struct sockaddr *addr = p;
  723. ENTER;
  724. /*
  725. if (netif_running(dev))
  726. return -EBUSY
  727. */
  728. memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
  729. bdx_restore_mac(ndev, priv);
  730. RET(0);
  731. }
  732. static int bdx_read_mac(struct bdx_priv *priv)
  733. {
  734. u16 macAddress[3], i;
  735. ENTER;
  736. macAddress[2] = READ_REG(priv, regUNC_MAC0_A);
  737. macAddress[2] = READ_REG(priv, regUNC_MAC0_A);
  738. macAddress[1] = READ_REG(priv, regUNC_MAC1_A);
  739. macAddress[1] = READ_REG(priv, regUNC_MAC1_A);
  740. macAddress[0] = READ_REG(priv, regUNC_MAC2_A);
  741. macAddress[0] = READ_REG(priv, regUNC_MAC2_A);
  742. for (i = 0; i < 3; i++) {
  743. priv->ndev->dev_addr[i * 2 + 1] = macAddress[i];
  744. priv->ndev->dev_addr[i * 2] = macAddress[i] >> 8;
  745. }
  746. RET(0);
  747. }
  748. static u64 bdx_read_l2stat(struct bdx_priv *priv, int reg)
  749. {
  750. u64 val;
  751. val = READ_REG(priv, reg);
  752. val |= ((u64) READ_REG(priv, reg + 8)) << 32;
  753. return val;
  754. }
  755. /*Do the statistics-update work*/
  756. static void bdx_update_stats(struct bdx_priv *priv)
  757. {
  758. struct bdx_stats *stats = &priv->hw_stats;
  759. u64 *stats_vector = (u64 *) stats;
  760. int i;
  761. int addr;
  762. /*Fill HW structure */
  763. addr = 0x7200;
  764. /*First 12 statistics - 0x7200 - 0x72B0 */
  765. for (i = 0; i < 12; i++) {
  766. stats_vector[i] = bdx_read_l2stat(priv, addr);
  767. addr += 0x10;
  768. }
  769. BDX_ASSERT(addr != 0x72C0);
  770. /* 0x72C0-0x72E0 RSRV */
  771. addr = 0x72F0;
  772. for (; i < 16; i++) {
  773. stats_vector[i] = bdx_read_l2stat(priv, addr);
  774. addr += 0x10;
  775. }
  776. BDX_ASSERT(addr != 0x7330);
  777. /* 0x7330-0x7360 RSRV */
  778. addr = 0x7370;
  779. for (; i < 19; i++) {
  780. stats_vector[i] = bdx_read_l2stat(priv, addr);
  781. addr += 0x10;
  782. }
  783. BDX_ASSERT(addr != 0x73A0);
  784. /* 0x73A0-0x73B0 RSRV */
  785. addr = 0x73C0;
  786. for (; i < 23; i++) {
  787. stats_vector[i] = bdx_read_l2stat(priv, addr);
  788. addr += 0x10;
  789. }
  790. BDX_ASSERT(addr != 0x7400);
  791. BDX_ASSERT((sizeof(struct bdx_stats) / sizeof(u64)) != i);
  792. }
  793. static void print_rxdd(struct rxd_desc *rxdd, u32 rxd_val1, u16 len,
  794. u16 rxd_vlan);
  795. static void print_rxfd(struct rxf_desc *rxfd);
  796. /*************************************************************************
  797. * Rx DB *
  798. *************************************************************************/
  799. static void bdx_rxdb_destroy(struct rxdb *db)
  800. {
  801. vfree(db);
  802. }
  803. static struct rxdb *bdx_rxdb_create(int nelem)
  804. {
  805. struct rxdb *db;
  806. int i;
  807. db = vmalloc(sizeof(struct rxdb)
  808. + (nelem * sizeof(int))
  809. + (nelem * sizeof(struct rx_map)));
  810. if (likely(db != NULL)) {
  811. db->stack = (int *)(db + 1);
  812. db->elems = (void *)(db->stack + nelem);
  813. db->nelem = nelem;
  814. db->top = nelem;
  815. for (i = 0; i < nelem; i++)
  816. db->stack[i] = nelem - i - 1; /* to make first allocs
  817. close to db struct*/
  818. }
  819. return db;
  820. }
  821. static inline int bdx_rxdb_alloc_elem(struct rxdb *db)
  822. {
  823. BDX_ASSERT(db->top <= 0);
  824. return db->stack[--(db->top)];
  825. }
  826. static inline void *bdx_rxdb_addr_elem(struct rxdb *db, int n)
  827. {
  828. BDX_ASSERT((n < 0) || (n >= db->nelem));
  829. return db->elems + n;
  830. }
  831. static inline int bdx_rxdb_available(struct rxdb *db)
  832. {
  833. return db->top;
  834. }
  835. static inline void bdx_rxdb_free_elem(struct rxdb *db, int n)
  836. {
  837. BDX_ASSERT((n >= db->nelem) || (n < 0));
  838. db->stack[(db->top)++] = n;
  839. }
  840. /*************************************************************************
  841. * Rx Init *
  842. *************************************************************************/
  843. /**
  844. * bdx_rx_init - initialize RX all related HW and SW resources
  845. * @priv: NIC private structure
  846. *
  847. * Returns 0 on success, negative value on failure
  848. *
  849. * It creates rxf and rxd fifos, update relevant HW registers, preallocate
  850. * skb for rx. It assumes that Rx is desabled in HW
  851. * funcs are grouped for better cache usage
  852. *
  853. * RxD fifo is smaller than RxF fifo by design. Upon high load, RxD will be
  854. * filled and packets will be dropped by nic without getting into host or
  855. * cousing interrupt. Anyway, in that condition, host has no chance to process
  856. * all packets, but dropping in nic is cheaper, since it takes 0 cpu cycles
  857. */
  858. /* TBD: ensure proper packet size */
  859. static int bdx_rx_init(struct bdx_priv *priv)
  860. {
  861. ENTER;
  862. if (bdx_fifo_init(priv, &priv->rxd_fifo0.m, priv->rxd_size,
  863. regRXD_CFG0_0, regRXD_CFG1_0,
  864. regRXD_RPTR_0, regRXD_WPTR_0))
  865. goto err_mem;
  866. if (bdx_fifo_init(priv, &priv->rxf_fifo0.m, priv->rxf_size,
  867. regRXF_CFG0_0, regRXF_CFG1_0,
  868. regRXF_RPTR_0, regRXF_WPTR_0))
  869. goto err_mem;
  870. priv->rxdb = bdx_rxdb_create(priv->rxf_fifo0.m.memsz /
  871. sizeof(struct rxf_desc));
  872. if (!priv->rxdb)
  873. goto err_mem;
  874. priv->rxf_fifo0.m.pktsz = priv->ndev->mtu + VLAN_ETH_HLEN;
  875. return 0;
  876. err_mem:
  877. netdev_err(priv->ndev, "Rx init failed\n");
  878. return -ENOMEM;
  879. }
  880. /**
  881. * bdx_rx_free_skbs - frees and unmaps all skbs allocated for the fifo
  882. * @priv: NIC private structure
  883. * @f: RXF fifo
  884. */
  885. static void bdx_rx_free_skbs(struct bdx_priv *priv, struct rxf_fifo *f)
  886. {
  887. struct rx_map *dm;
  888. struct rxdb *db = priv->rxdb;
  889. u16 i;
  890. ENTER;
  891. DBG("total=%d free=%d busy=%d\n", db->nelem, bdx_rxdb_available(db),
  892. db->nelem - bdx_rxdb_available(db));
  893. while (bdx_rxdb_available(db) > 0) {
  894. i = bdx_rxdb_alloc_elem(db);
  895. dm = bdx_rxdb_addr_elem(db, i);
  896. dm->dma = 0;
  897. }
  898. for (i = 0; i < db->nelem; i++) {
  899. dm = bdx_rxdb_addr_elem(db, i);
  900. if (dm->dma) {
  901. pci_unmap_single(priv->pdev,
  902. dm->dma, f->m.pktsz,
  903. PCI_DMA_FROMDEVICE);
  904. dev_kfree_skb(dm->skb);
  905. }
  906. }
  907. }
  908. /**
  909. * bdx_rx_free - release all Rx resources
  910. * @priv: NIC private structure
  911. *
  912. * It assumes that Rx is desabled in HW
  913. */
  914. static void bdx_rx_free(struct bdx_priv *priv)
  915. {
  916. ENTER;
  917. if (priv->rxdb) {
  918. bdx_rx_free_skbs(priv, &priv->rxf_fifo0);
  919. bdx_rxdb_destroy(priv->rxdb);
  920. priv->rxdb = NULL;
  921. }
  922. bdx_fifo_free(priv, &priv->rxf_fifo0.m);
  923. bdx_fifo_free(priv, &priv->rxd_fifo0.m);
  924. RET();
  925. }
  926. /*************************************************************************
  927. * Rx Engine *
  928. *************************************************************************/
  929. /**
  930. * bdx_rx_alloc_skbs - fill rxf fifo with new skbs
  931. * @priv: nic's private structure
  932. * @f: RXF fifo that needs skbs
  933. *
  934. * It allocates skbs, build rxf descs and push it (rxf descr) into rxf fifo.
  935. * skb's virtual and physical addresses are stored in skb db.
  936. * To calculate free space, func uses cached values of RPTR and WPTR
  937. * When needed, it also updates RPTR and WPTR.
  938. */
  939. /* TBD: do not update WPTR if no desc were written */
  940. static void bdx_rx_alloc_skbs(struct bdx_priv *priv, struct rxf_fifo *f)
  941. {
  942. struct sk_buff *skb;
  943. struct rxf_desc *rxfd;
  944. struct rx_map *dm;
  945. int dno, delta, idx;
  946. struct rxdb *db = priv->rxdb;
  947. ENTER;
  948. dno = bdx_rxdb_available(db) - 1;
  949. while (dno > 0) {
  950. skb = netdev_alloc_skb(priv->ndev, f->m.pktsz + NET_IP_ALIGN);
  951. if (!skb)
  952. break;
  953. skb_reserve(skb, NET_IP_ALIGN);
  954. idx = bdx_rxdb_alloc_elem(db);
  955. dm = bdx_rxdb_addr_elem(db, idx);
  956. dm->dma = pci_map_single(priv->pdev,
  957. skb->data, f->m.pktsz,
  958. PCI_DMA_FROMDEVICE);
  959. dm->skb = skb;
  960. rxfd = (struct rxf_desc *)(f->m.va + f->m.wptr);
  961. rxfd->info = CPU_CHIP_SWAP32(0x10003); /* INFO=1 BC=3 */
  962. rxfd->va_lo = idx;
  963. rxfd->pa_lo = CPU_CHIP_SWAP32(L32_64(dm->dma));
  964. rxfd->pa_hi = CPU_CHIP_SWAP32(H32_64(dm->dma));
  965. rxfd->len = CPU_CHIP_SWAP32(f->m.pktsz);
  966. print_rxfd(rxfd);
  967. f->m.wptr += sizeof(struct rxf_desc);
  968. delta = f->m.wptr - f->m.memsz;
  969. if (unlikely(delta >= 0)) {
  970. f->m.wptr = delta;
  971. if (delta > 0) {
  972. memcpy(f->m.va, f->m.va + f->m.memsz, delta);
  973. DBG("wrapped descriptor\n");
  974. }
  975. }
  976. dno--;
  977. }
  978. /*TBD: to do - delayed rxf wptr like in txd */
  979. WRITE_REG(priv, f->m.reg_WPTR, f->m.wptr & TXF_WPTR_WR_PTR);
  980. RET();
  981. }
  982. static inline void
  983. NETIF_RX_MUX(struct bdx_priv *priv, u32 rxd_val1, u16 rxd_vlan,
  984. struct sk_buff *skb)
  985. {
  986. ENTER;
  987. DBG("rxdd->flags.bits.vtag=%d\n", GET_RXD_VTAG(rxd_val1));
  988. if (GET_RXD_VTAG(rxd_val1)) {
  989. DBG("%s: vlan rcv vlan '%x' vtag '%x'\n",
  990. priv->ndev->name,
  991. GET_RXD_VLAN_ID(rxd_vlan),
  992. GET_RXD_VTAG(rxd_val1));
  993. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), GET_RXD_VLAN_TCI(rxd_vlan));
  994. }
  995. netif_receive_skb(skb);
  996. }
  997. static void bdx_recycle_skb(struct bdx_priv *priv, struct rxd_desc *rxdd)
  998. {
  999. struct rxf_desc *rxfd;
  1000. struct rx_map *dm;
  1001. struct rxf_fifo *f;
  1002. struct rxdb *db;
  1003. int delta;
  1004. ENTER;
  1005. DBG("priv=%p rxdd=%p\n", priv, rxdd);
  1006. f = &priv->rxf_fifo0;
  1007. db = priv->rxdb;
  1008. DBG("db=%p f=%p\n", db, f);
  1009. dm = bdx_rxdb_addr_elem(db, rxdd->va_lo);
  1010. DBG("dm=%p\n", dm);
  1011. rxfd = (struct rxf_desc *)(f->m.va + f->m.wptr);
  1012. rxfd->info = CPU_CHIP_SWAP32(0x10003); /* INFO=1 BC=3 */
  1013. rxfd->va_lo = rxdd->va_lo;
  1014. rxfd->pa_lo = CPU_CHIP_SWAP32(L32_64(dm->dma));
  1015. rxfd->pa_hi = CPU_CHIP_SWAP32(H32_64(dm->dma));
  1016. rxfd->len = CPU_CHIP_SWAP32(f->m.pktsz);
  1017. print_rxfd(rxfd);
  1018. f->m.wptr += sizeof(struct rxf_desc);
  1019. delta = f->m.wptr - f->m.memsz;
  1020. if (unlikely(delta >= 0)) {
  1021. f->m.wptr = delta;
  1022. if (delta > 0) {
  1023. memcpy(f->m.va, f->m.va + f->m.memsz, delta);
  1024. DBG("wrapped descriptor\n");
  1025. }
  1026. }
  1027. RET();
  1028. }
  1029. /**
  1030. * bdx_rx_receive - receives full packets from RXD fifo and pass them to OS
  1031. * NOTE: a special treatment is given to non-continuous descriptors
  1032. * that start near the end, wraps around and continue at the beginning. a second
  1033. * part is copied right after the first, and then descriptor is interpreted as
  1034. * normal. fifo has an extra space to allow such operations
  1035. * @priv: nic's private structure
  1036. * @f: RXF fifo that needs skbs
  1037. * @budget: maximum number of packets to receive
  1038. */
  1039. /* TBD: replace memcpy func call by explicite inline asm */
  1040. static int bdx_rx_receive(struct bdx_priv *priv, struct rxd_fifo *f, int budget)
  1041. {
  1042. struct net_device *ndev = priv->ndev;
  1043. struct sk_buff *skb, *skb2;
  1044. struct rxd_desc *rxdd;
  1045. struct rx_map *dm;
  1046. struct rxf_fifo *rxf_fifo;
  1047. int tmp_len, size;
  1048. int done = 0;
  1049. int max_done = BDX_MAX_RX_DONE;
  1050. struct rxdb *db = NULL;
  1051. /* Unmarshalled descriptor - copy of descriptor in host order */
  1052. u32 rxd_val1;
  1053. u16 len;
  1054. u16 rxd_vlan;
  1055. ENTER;
  1056. max_done = budget;
  1057. f->m.wptr = READ_REG(priv, f->m.reg_WPTR) & TXF_WPTR_WR_PTR;
  1058. size = f->m.wptr - f->m.rptr;
  1059. if (size < 0)
  1060. size = f->m.memsz + size; /* size is negative :-) */
  1061. while (size > 0) {
  1062. rxdd = (struct rxd_desc *)(f->m.va + f->m.rptr);
  1063. rxd_val1 = CPU_CHIP_SWAP32(rxdd->rxd_val1);
  1064. len = CPU_CHIP_SWAP16(rxdd->len);
  1065. rxd_vlan = CPU_CHIP_SWAP16(rxdd->rxd_vlan);
  1066. print_rxdd(rxdd, rxd_val1, len, rxd_vlan);
  1067. tmp_len = GET_RXD_BC(rxd_val1) << 3;
  1068. BDX_ASSERT(tmp_len <= 0);
  1069. size -= tmp_len;
  1070. if (size < 0) /* test for partially arrived descriptor */
  1071. break;
  1072. f->m.rptr += tmp_len;
  1073. tmp_len = f->m.rptr - f->m.memsz;
  1074. if (unlikely(tmp_len >= 0)) {
  1075. f->m.rptr = tmp_len;
  1076. if (tmp_len > 0) {
  1077. DBG("wrapped desc rptr=%d tmp_len=%d\n",
  1078. f->m.rptr, tmp_len);
  1079. memcpy(f->m.va + f->m.memsz, f->m.va, tmp_len);
  1080. }
  1081. }
  1082. if (unlikely(GET_RXD_ERR(rxd_val1))) {
  1083. DBG("rxd_err = 0x%x\n", GET_RXD_ERR(rxd_val1));
  1084. ndev->stats.rx_errors++;
  1085. bdx_recycle_skb(priv, rxdd);
  1086. continue;
  1087. }
  1088. rxf_fifo = &priv->rxf_fifo0;
  1089. db = priv->rxdb;
  1090. dm = bdx_rxdb_addr_elem(db, rxdd->va_lo);
  1091. skb = dm->skb;
  1092. if (len < BDX_COPYBREAK &&
  1093. (skb2 = netdev_alloc_skb(priv->ndev, len + NET_IP_ALIGN))) {
  1094. skb_reserve(skb2, NET_IP_ALIGN);
  1095. /*skb_put(skb2, len); */
  1096. pci_dma_sync_single_for_cpu(priv->pdev,
  1097. dm->dma, rxf_fifo->m.pktsz,
  1098. PCI_DMA_FROMDEVICE);
  1099. memcpy(skb2->data, skb->data, len);
  1100. bdx_recycle_skb(priv, rxdd);
  1101. skb = skb2;
  1102. } else {
  1103. pci_unmap_single(priv->pdev,
  1104. dm->dma, rxf_fifo->m.pktsz,
  1105. PCI_DMA_FROMDEVICE);
  1106. bdx_rxdb_free_elem(db, rxdd->va_lo);
  1107. }
  1108. ndev->stats.rx_bytes += len;
  1109. skb_put(skb, len);
  1110. skb->protocol = eth_type_trans(skb, ndev);
  1111. /* Non-IP packets aren't checksum-offloaded */
  1112. if (GET_RXD_PKT_ID(rxd_val1) == 0)
  1113. skb_checksum_none_assert(skb);
  1114. else
  1115. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1116. NETIF_RX_MUX(priv, rxd_val1, rxd_vlan, skb);
  1117. if (++done >= max_done)
  1118. break;
  1119. }
  1120. ndev->stats.rx_packets += done;
  1121. /* FIXME: do smth to minimize pci accesses */
  1122. WRITE_REG(priv, f->m.reg_RPTR, f->m.rptr & TXF_WPTR_WR_PTR);
  1123. bdx_rx_alloc_skbs(priv, &priv->rxf_fifo0);
  1124. RET(done);
  1125. }
  1126. /*************************************************************************
  1127. * Debug / Temprorary Code *
  1128. *************************************************************************/
  1129. static void print_rxdd(struct rxd_desc *rxdd, u32 rxd_val1, u16 len,
  1130. u16 rxd_vlan)
  1131. {
  1132. DBG("ERROR: rxdd bc %d rxfq %d to %d type %d err %d rxp %d pkt_id %d vtag %d len %d vlan_id %d cfi %d prio %d va_lo %d va_hi %d\n",
  1133. GET_RXD_BC(rxd_val1), GET_RXD_RXFQ(rxd_val1), GET_RXD_TO(rxd_val1),
  1134. GET_RXD_TYPE(rxd_val1), GET_RXD_ERR(rxd_val1),
  1135. GET_RXD_RXP(rxd_val1), GET_RXD_PKT_ID(rxd_val1),
  1136. GET_RXD_VTAG(rxd_val1), len, GET_RXD_VLAN_ID(rxd_vlan),
  1137. GET_RXD_CFI(rxd_vlan), GET_RXD_PRIO(rxd_vlan), rxdd->va_lo,
  1138. rxdd->va_hi);
  1139. }
  1140. static void print_rxfd(struct rxf_desc *rxfd)
  1141. {
  1142. DBG("=== RxF desc CHIP ORDER/ENDIANNESS =============\n"
  1143. "info 0x%x va_lo %u pa_lo 0x%x pa_hi 0x%x len 0x%x\n",
  1144. rxfd->info, rxfd->va_lo, rxfd->pa_lo, rxfd->pa_hi, rxfd->len);
  1145. }
  1146. /*
  1147. * TX HW/SW interaction overview
  1148. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1149. * There are 2 types of TX communication channels between driver and NIC.
  1150. * 1) TX Free Fifo - TXF - holds ack descriptors for sent packets
  1151. * 2) TX Data Fifo - TXD - holds descriptors of full buffers.
  1152. *
  1153. * Currently NIC supports TSO, checksuming and gather DMA
  1154. * UFO and IP fragmentation is on the way
  1155. *
  1156. * RX SW Data Structures
  1157. * ~~~~~~~~~~~~~~~~~~~~~
  1158. * txdb - used to keep track of all skbs owned by SW and their dma addresses.
  1159. * For TX case, ownership lasts from geting packet via hard_xmit and until HW
  1160. * acknowledges sent by TXF descriptors.
  1161. * Implemented as cyclic buffer.
  1162. * fifo - keeps info about fifo's size and location, relevant HW registers,
  1163. * usage and skb db. Each RXD and RXF Fifo has its own fifo structure.
  1164. * Implemented as simple struct.
  1165. *
  1166. * TX SW Execution Flow
  1167. * ~~~~~~~~~~~~~~~~~~~~
  1168. * OS calls driver's hard_xmit method with packet to sent.
  1169. * Driver creates DMA mappings, builds TXD descriptors and kicks HW
  1170. * by updating TXD WPTR.
  1171. * When packet is sent, HW write us TXF descriptor and SW frees original skb.
  1172. * To prevent TXD fifo overflow without reading HW registers every time,
  1173. * SW deploys "tx level" technique.
  1174. * Upon strart up, tx level is initialized to TXD fifo length.
  1175. * For every sent packet, SW gets its TXD descriptor sizei
  1176. * (from precalculated array) and substructs it from tx level.
  1177. * The size is also stored in txdb. When TXF ack arrives, SW fetch size of
  1178. * original TXD descriptor from txdb and adds it to tx level.
  1179. * When Tx level drops under some predefined treshhold, the driver
  1180. * stops the TX queue. When TX level rises above that level,
  1181. * the tx queue is enabled again.
  1182. *
  1183. * This technique avoids eccessive reading of RPTR and WPTR registers.
  1184. * As our benchmarks shows, it adds 1.5 Gbit/sec to NIS's throuput.
  1185. */
  1186. /*************************************************************************
  1187. * Tx DB *
  1188. *************************************************************************/
  1189. static inline int bdx_tx_db_size(struct txdb *db)
  1190. {
  1191. int taken = db->wptr - db->rptr;
  1192. if (taken < 0)
  1193. taken = db->size + 1 + taken; /* (size + 1) equals memsz */
  1194. return db->size - taken;
  1195. }
  1196. /**
  1197. * __bdx_tx_db_ptr_next - helper function, increment read/write pointer + wrap
  1198. * @db: tx data base
  1199. * @pptr: read or write pointer
  1200. */
  1201. static inline void __bdx_tx_db_ptr_next(struct txdb *db, struct tx_map **pptr)
  1202. {
  1203. BDX_ASSERT(db == NULL || pptr == NULL); /* sanity */
  1204. BDX_ASSERT(*pptr != db->rptr && /* expect either read */
  1205. *pptr != db->wptr); /* or write pointer */
  1206. BDX_ASSERT(*pptr < db->start || /* pointer has to be */
  1207. *pptr >= db->end); /* in range */
  1208. ++*pptr;
  1209. if (unlikely(*pptr == db->end))
  1210. *pptr = db->start;
  1211. }
  1212. /**
  1213. * bdx_tx_db_inc_rptr - increment read pointer
  1214. * @db: tx data base
  1215. */
  1216. static inline void bdx_tx_db_inc_rptr(struct txdb *db)
  1217. {
  1218. BDX_ASSERT(db->rptr == db->wptr); /* can't read from empty db */
  1219. __bdx_tx_db_ptr_next(db, &db->rptr);
  1220. }
  1221. /**
  1222. * bdx_tx_db_inc_wptr - increment write pointer
  1223. * @db: tx data base
  1224. */
  1225. static inline void bdx_tx_db_inc_wptr(struct txdb *db)
  1226. {
  1227. __bdx_tx_db_ptr_next(db, &db->wptr);
  1228. BDX_ASSERT(db->rptr == db->wptr); /* we can not get empty db as
  1229. a result of write */
  1230. }
  1231. /**
  1232. * bdx_tx_db_init - creates and initializes tx db
  1233. * @d: tx data base
  1234. * @sz_type: size of tx fifo
  1235. *
  1236. * Returns 0 on success, error code otherwise
  1237. */
  1238. static int bdx_tx_db_init(struct txdb *d, int sz_type)
  1239. {
  1240. int memsz = FIFO_SIZE * (1 << (sz_type + 1));
  1241. d->start = vmalloc(memsz);
  1242. if (!d->start)
  1243. return -ENOMEM;
  1244. /*
  1245. * In order to differentiate between db is empty and db is full
  1246. * states at least one element should always be empty in order to
  1247. * avoid rptr == wptr which means db is empty
  1248. */
  1249. d->size = memsz / sizeof(struct tx_map) - 1;
  1250. d->end = d->start + d->size + 1; /* just after last element */
  1251. /* all dbs are created equally empty */
  1252. d->rptr = d->start;
  1253. d->wptr = d->start;
  1254. return 0;
  1255. }
  1256. /**
  1257. * bdx_tx_db_close - closes tx db and frees all memory
  1258. * @d: tx data base
  1259. */
  1260. static void bdx_tx_db_close(struct txdb *d)
  1261. {
  1262. BDX_ASSERT(d == NULL);
  1263. vfree(d->start);
  1264. d->start = NULL;
  1265. }
  1266. /*************************************************************************
  1267. * Tx Engine *
  1268. *************************************************************************/
  1269. /* sizes of tx desc (including padding if needed) as function
  1270. * of skb's frag number */
  1271. static struct {
  1272. u16 bytes;
  1273. u16 qwords; /* qword = 64 bit */
  1274. } txd_sizes[MAX_SKB_FRAGS + 1];
  1275. /**
  1276. * bdx_tx_map_skb - creates and stores dma mappings for skb's data blocks
  1277. * @priv: NIC private structure
  1278. * @skb: socket buffer to map
  1279. * @txdd: TX descriptor to use
  1280. *
  1281. * It makes dma mappings for skb's data blocks and writes them to PBL of
  1282. * new tx descriptor. It also stores them in the tx db, so they could be
  1283. * unmaped after data was sent. It is reponsibility of a caller to make
  1284. * sure that there is enough space in the tx db. Last element holds pointer
  1285. * to skb itself and marked with zero length
  1286. */
  1287. static inline void
  1288. bdx_tx_map_skb(struct bdx_priv *priv, struct sk_buff *skb,
  1289. struct txd_desc *txdd)
  1290. {
  1291. struct txdb *db = &priv->txdb;
  1292. struct pbl *pbl = &txdd->pbl[0];
  1293. int nr_frags = skb_shinfo(skb)->nr_frags;
  1294. int i;
  1295. db->wptr->len = skb_headlen(skb);
  1296. db->wptr->addr.dma = pci_map_single(priv->pdev, skb->data,
  1297. db->wptr->len, PCI_DMA_TODEVICE);
  1298. pbl->len = CPU_CHIP_SWAP32(db->wptr->len);
  1299. pbl->pa_lo = CPU_CHIP_SWAP32(L32_64(db->wptr->addr.dma));
  1300. pbl->pa_hi = CPU_CHIP_SWAP32(H32_64(db->wptr->addr.dma));
  1301. DBG("=== pbl len: 0x%x ================\n", pbl->len);
  1302. DBG("=== pbl pa_lo: 0x%x ================\n", pbl->pa_lo);
  1303. DBG("=== pbl pa_hi: 0x%x ================\n", pbl->pa_hi);
  1304. bdx_tx_db_inc_wptr(db);
  1305. for (i = 0; i < nr_frags; i++) {
  1306. const struct skb_frag_struct *frag;
  1307. frag = &skb_shinfo(skb)->frags[i];
  1308. db->wptr->len = skb_frag_size(frag);
  1309. db->wptr->addr.dma = skb_frag_dma_map(&priv->pdev->dev, frag,
  1310. 0, skb_frag_size(frag),
  1311. DMA_TO_DEVICE);
  1312. pbl++;
  1313. pbl->len = CPU_CHIP_SWAP32(db->wptr->len);
  1314. pbl->pa_lo = CPU_CHIP_SWAP32(L32_64(db->wptr->addr.dma));
  1315. pbl->pa_hi = CPU_CHIP_SWAP32(H32_64(db->wptr->addr.dma));
  1316. bdx_tx_db_inc_wptr(db);
  1317. }
  1318. /* add skb clean up info. */
  1319. db->wptr->len = -txd_sizes[nr_frags].bytes;
  1320. db->wptr->addr.skb = skb;
  1321. bdx_tx_db_inc_wptr(db);
  1322. }
  1323. /* init_txd_sizes - precalculate sizes of descriptors for skbs up to 16 frags
  1324. * number of frags is used as index to fetch correct descriptors size,
  1325. * instead of calculating it each time */
  1326. static void __init init_txd_sizes(void)
  1327. {
  1328. int i, lwords;
  1329. /* 7 - is number of lwords in txd with one phys buffer
  1330. * 3 - is number of lwords used for every additional phys buffer */
  1331. for (i = 0; i < MAX_SKB_FRAGS + 1; i++) {
  1332. lwords = 7 + (i * 3);
  1333. if (lwords & 1)
  1334. lwords++; /* pad it with 1 lword */
  1335. txd_sizes[i].qwords = lwords >> 1;
  1336. txd_sizes[i].bytes = lwords << 2;
  1337. }
  1338. }
  1339. /* bdx_tx_init - initialize all Tx related stuff.
  1340. * Namely, TXD and TXF fifos, database etc */
  1341. static int bdx_tx_init(struct bdx_priv *priv)
  1342. {
  1343. if (bdx_fifo_init(priv, &priv->txd_fifo0.m, priv->txd_size,
  1344. regTXD_CFG0_0,
  1345. regTXD_CFG1_0, regTXD_RPTR_0, regTXD_WPTR_0))
  1346. goto err_mem;
  1347. if (bdx_fifo_init(priv, &priv->txf_fifo0.m, priv->txf_size,
  1348. regTXF_CFG0_0,
  1349. regTXF_CFG1_0, regTXF_RPTR_0, regTXF_WPTR_0))
  1350. goto err_mem;
  1351. /* The TX db has to keep mappings for all packets sent (on TxD)
  1352. * and not yet reclaimed (on TxF) */
  1353. if (bdx_tx_db_init(&priv->txdb, max(priv->txd_size, priv->txf_size)))
  1354. goto err_mem;
  1355. priv->tx_level = BDX_MAX_TX_LEVEL;
  1356. #ifdef BDX_DELAY_WPTR
  1357. priv->tx_update_mark = priv->tx_level - 1024;
  1358. #endif
  1359. return 0;
  1360. err_mem:
  1361. netdev_err(priv->ndev, "Tx init failed\n");
  1362. return -ENOMEM;
  1363. }
  1364. /**
  1365. * bdx_tx_space - calculates available space in TX fifo
  1366. * @priv: NIC private structure
  1367. *
  1368. * Returns available space in TX fifo in bytes
  1369. */
  1370. static inline int bdx_tx_space(struct bdx_priv *priv)
  1371. {
  1372. struct txd_fifo *f = &priv->txd_fifo0;
  1373. int fsize;
  1374. f->m.rptr = READ_REG(priv, f->m.reg_RPTR) & TXF_WPTR_WR_PTR;
  1375. fsize = f->m.rptr - f->m.wptr;
  1376. if (fsize <= 0)
  1377. fsize = f->m.memsz + fsize;
  1378. return fsize;
  1379. }
  1380. /**
  1381. * bdx_tx_transmit - send packet to NIC
  1382. * @skb: packet to send
  1383. * @ndev: network device assigned to NIC
  1384. * Return codes:
  1385. * o NETDEV_TX_OK everything ok.
  1386. * o NETDEV_TX_BUSY Cannot transmit packet, try later
  1387. * Usually a bug, means queue start/stop flow control is broken in
  1388. * the driver. Note: the driver must NOT put the skb in its DMA ring.
  1389. */
  1390. static netdev_tx_t bdx_tx_transmit(struct sk_buff *skb,
  1391. struct net_device *ndev)
  1392. {
  1393. struct bdx_priv *priv = netdev_priv(ndev);
  1394. struct txd_fifo *f = &priv->txd_fifo0;
  1395. int txd_checksum = 7; /* full checksum */
  1396. int txd_lgsnd = 0;
  1397. int txd_vlan_id = 0;
  1398. int txd_vtag = 0;
  1399. int txd_mss = 0;
  1400. int nr_frags = skb_shinfo(skb)->nr_frags;
  1401. struct txd_desc *txdd;
  1402. int len;
  1403. unsigned long flags;
  1404. ENTER;
  1405. local_irq_save(flags);
  1406. spin_lock(&priv->tx_lock);
  1407. /* build tx descriptor */
  1408. BDX_ASSERT(f->m.wptr >= f->m.memsz); /* started with valid wptr */
  1409. txdd = (struct txd_desc *)(f->m.va + f->m.wptr);
  1410. if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
  1411. txd_checksum = 0;
  1412. if (skb_shinfo(skb)->gso_size) {
  1413. txd_mss = skb_shinfo(skb)->gso_size;
  1414. txd_lgsnd = 1;
  1415. DBG("skb %p skb len %d gso size = %d\n", skb, skb->len,
  1416. txd_mss);
  1417. }
  1418. if (skb_vlan_tag_present(skb)) {
  1419. /*Cut VLAN ID to 12 bits */
  1420. txd_vlan_id = skb_vlan_tag_get(skb) & BITS_MASK(12);
  1421. txd_vtag = 1;
  1422. }
  1423. txdd->length = CPU_CHIP_SWAP16(skb->len);
  1424. txdd->mss = CPU_CHIP_SWAP16(txd_mss);
  1425. txdd->txd_val1 =
  1426. CPU_CHIP_SWAP32(TXD_W1_VAL
  1427. (txd_sizes[nr_frags].qwords, txd_checksum, txd_vtag,
  1428. txd_lgsnd, txd_vlan_id));
  1429. DBG("=== TxD desc =====================\n");
  1430. DBG("=== w1: 0x%x ================\n", txdd->txd_val1);
  1431. DBG("=== w2: mss 0x%x len 0x%x\n", txdd->mss, txdd->length);
  1432. bdx_tx_map_skb(priv, skb, txdd);
  1433. /* increment TXD write pointer. In case of
  1434. fifo wrapping copy reminder of the descriptor
  1435. to the beginning */
  1436. f->m.wptr += txd_sizes[nr_frags].bytes;
  1437. len = f->m.wptr - f->m.memsz;
  1438. if (unlikely(len >= 0)) {
  1439. f->m.wptr = len;
  1440. if (len > 0) {
  1441. BDX_ASSERT(len > f->m.memsz);
  1442. memcpy(f->m.va, f->m.va + f->m.memsz, len);
  1443. }
  1444. }
  1445. BDX_ASSERT(f->m.wptr >= f->m.memsz); /* finished with valid wptr */
  1446. priv->tx_level -= txd_sizes[nr_frags].bytes;
  1447. BDX_ASSERT(priv->tx_level <= 0 || priv->tx_level > BDX_MAX_TX_LEVEL);
  1448. #ifdef BDX_DELAY_WPTR
  1449. if (priv->tx_level > priv->tx_update_mark) {
  1450. /* Force memory writes to complete before letting h/w
  1451. know there are new descriptors to fetch.
  1452. (might be needed on platforms like IA64)
  1453. wmb(); */
  1454. WRITE_REG(priv, f->m.reg_WPTR, f->m.wptr & TXF_WPTR_WR_PTR);
  1455. } else {
  1456. if (priv->tx_noupd++ > BDX_NO_UPD_PACKETS) {
  1457. priv->tx_noupd = 0;
  1458. WRITE_REG(priv, f->m.reg_WPTR,
  1459. f->m.wptr & TXF_WPTR_WR_PTR);
  1460. }
  1461. }
  1462. #else
  1463. /* Force memory writes to complete before letting h/w
  1464. know there are new descriptors to fetch.
  1465. (might be needed on platforms like IA64)
  1466. wmb(); */
  1467. WRITE_REG(priv, f->m.reg_WPTR, f->m.wptr & TXF_WPTR_WR_PTR);
  1468. #endif
  1469. #ifdef BDX_LLTX
  1470. netif_trans_update(ndev); /* NETIF_F_LLTX driver :( */
  1471. #endif
  1472. ndev->stats.tx_packets++;
  1473. ndev->stats.tx_bytes += skb->len;
  1474. if (priv->tx_level < BDX_MIN_TX_LEVEL) {
  1475. DBG("%s: %s: TX Q STOP level %d\n",
  1476. BDX_DRV_NAME, ndev->name, priv->tx_level);
  1477. netif_stop_queue(ndev);
  1478. }
  1479. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1480. return NETDEV_TX_OK;
  1481. }
  1482. /**
  1483. * bdx_tx_cleanup - clean TXF fifo, run in the context of IRQ.
  1484. * @priv: bdx adapter
  1485. *
  1486. * It scans TXF fifo for descriptors, frees DMA mappings and reports to OS
  1487. * that those packets were sent
  1488. */
  1489. static void bdx_tx_cleanup(struct bdx_priv *priv)
  1490. {
  1491. struct txf_fifo *f = &priv->txf_fifo0;
  1492. struct txdb *db = &priv->txdb;
  1493. int tx_level = 0;
  1494. ENTER;
  1495. f->m.wptr = READ_REG(priv, f->m.reg_WPTR) & TXF_WPTR_MASK;
  1496. BDX_ASSERT(f->m.rptr >= f->m.memsz); /* started with valid rptr */
  1497. while (f->m.wptr != f->m.rptr) {
  1498. f->m.rptr += BDX_TXF_DESC_SZ;
  1499. f->m.rptr &= f->m.size_mask;
  1500. /* unmap all the fragments */
  1501. /* first has to come tx_maps containing dma */
  1502. BDX_ASSERT(db->rptr->len == 0);
  1503. do {
  1504. BDX_ASSERT(db->rptr->addr.dma == 0);
  1505. pci_unmap_page(priv->pdev, db->rptr->addr.dma,
  1506. db->rptr->len, PCI_DMA_TODEVICE);
  1507. bdx_tx_db_inc_rptr(db);
  1508. } while (db->rptr->len > 0);
  1509. tx_level -= db->rptr->len; /* '-' koz len is negative */
  1510. /* now should come skb pointer - free it */
  1511. dev_kfree_skb_irq(db->rptr->addr.skb);
  1512. bdx_tx_db_inc_rptr(db);
  1513. }
  1514. /* let h/w know which TXF descriptors were cleaned */
  1515. BDX_ASSERT((f->m.wptr & TXF_WPTR_WR_PTR) >= f->m.memsz);
  1516. WRITE_REG(priv, f->m.reg_RPTR, f->m.rptr & TXF_WPTR_WR_PTR);
  1517. /* We reclaimed resources, so in case the Q is stopped by xmit callback,
  1518. * we resume the transmission and use tx_lock to synchronize with xmit.*/
  1519. spin_lock(&priv->tx_lock);
  1520. priv->tx_level += tx_level;
  1521. BDX_ASSERT(priv->tx_level <= 0 || priv->tx_level > BDX_MAX_TX_LEVEL);
  1522. #ifdef BDX_DELAY_WPTR
  1523. if (priv->tx_noupd) {
  1524. priv->tx_noupd = 0;
  1525. WRITE_REG(priv, priv->txd_fifo0.m.reg_WPTR,
  1526. priv->txd_fifo0.m.wptr & TXF_WPTR_WR_PTR);
  1527. }
  1528. #endif
  1529. if (unlikely(netif_queue_stopped(priv->ndev) &&
  1530. netif_carrier_ok(priv->ndev) &&
  1531. (priv->tx_level >= BDX_MIN_TX_LEVEL))) {
  1532. DBG("%s: %s: TX Q WAKE level %d\n",
  1533. BDX_DRV_NAME, priv->ndev->name, priv->tx_level);
  1534. netif_wake_queue(priv->ndev);
  1535. }
  1536. spin_unlock(&priv->tx_lock);
  1537. }
  1538. /**
  1539. * bdx_tx_free_skbs - frees all skbs from TXD fifo.
  1540. * It gets called when OS stops this dev, eg upon "ifconfig down" or rmmod
  1541. */
  1542. static void bdx_tx_free_skbs(struct bdx_priv *priv)
  1543. {
  1544. struct txdb *db = &priv->txdb;
  1545. ENTER;
  1546. while (db->rptr != db->wptr) {
  1547. if (likely(db->rptr->len))
  1548. pci_unmap_page(priv->pdev, db->rptr->addr.dma,
  1549. db->rptr->len, PCI_DMA_TODEVICE);
  1550. else
  1551. dev_kfree_skb(db->rptr->addr.skb);
  1552. bdx_tx_db_inc_rptr(db);
  1553. }
  1554. RET();
  1555. }
  1556. /* bdx_tx_free - frees all Tx resources */
  1557. static void bdx_tx_free(struct bdx_priv *priv)
  1558. {
  1559. ENTER;
  1560. bdx_tx_free_skbs(priv);
  1561. bdx_fifo_free(priv, &priv->txd_fifo0.m);
  1562. bdx_fifo_free(priv, &priv->txf_fifo0.m);
  1563. bdx_tx_db_close(&priv->txdb);
  1564. }
  1565. /**
  1566. * bdx_tx_push_desc - push descriptor to TxD fifo
  1567. * @priv: NIC private structure
  1568. * @data: desc's data
  1569. * @size: desc's size
  1570. *
  1571. * Pushes desc to TxD fifo and overlaps it if needed.
  1572. * NOTE: this func does not check for available space. this is responsibility
  1573. * of the caller. Neither does it check that data size is smaller than
  1574. * fifo size.
  1575. */
  1576. static void bdx_tx_push_desc(struct bdx_priv *priv, void *data, int size)
  1577. {
  1578. struct txd_fifo *f = &priv->txd_fifo0;
  1579. int i = f->m.memsz - f->m.wptr;
  1580. if (size == 0)
  1581. return;
  1582. if (i > size) {
  1583. memcpy(f->m.va + f->m.wptr, data, size);
  1584. f->m.wptr += size;
  1585. } else {
  1586. memcpy(f->m.va + f->m.wptr, data, i);
  1587. f->m.wptr = size - i;
  1588. memcpy(f->m.va, data + i, f->m.wptr);
  1589. }
  1590. WRITE_REG(priv, f->m.reg_WPTR, f->m.wptr & TXF_WPTR_WR_PTR);
  1591. }
  1592. /**
  1593. * bdx_tx_push_desc_safe - push descriptor to TxD fifo in a safe way
  1594. * @priv: NIC private structure
  1595. * @data: desc's data
  1596. * @size: desc's size
  1597. *
  1598. * NOTE: this func does check for available space and, if necessary, waits for
  1599. * NIC to read existing data before writing new one.
  1600. */
  1601. static void bdx_tx_push_desc_safe(struct bdx_priv *priv, void *data, int size)
  1602. {
  1603. int timer = 0;
  1604. ENTER;
  1605. while (size > 0) {
  1606. /* we substruct 8 because when fifo is full rptr == wptr
  1607. which also means that fifo is empty, we can understand
  1608. the difference, but could hw do the same ??? :) */
  1609. int avail = bdx_tx_space(priv) - 8;
  1610. if (avail <= 0) {
  1611. if (timer++ > 300) { /* prevent endless loop */
  1612. DBG("timeout while writing desc to TxD fifo\n");
  1613. break;
  1614. }
  1615. udelay(50); /* give hw a chance to clean fifo */
  1616. continue;
  1617. }
  1618. avail = min(avail, size);
  1619. DBG("about to push %d bytes starting %p size %d\n", avail,
  1620. data, size);
  1621. bdx_tx_push_desc(priv, data, avail);
  1622. size -= avail;
  1623. data += avail;
  1624. }
  1625. RET();
  1626. }
  1627. static const struct net_device_ops bdx_netdev_ops = {
  1628. .ndo_open = bdx_open,
  1629. .ndo_stop = bdx_close,
  1630. .ndo_start_xmit = bdx_tx_transmit,
  1631. .ndo_validate_addr = eth_validate_addr,
  1632. .ndo_do_ioctl = bdx_ioctl,
  1633. .ndo_set_rx_mode = bdx_setmulti,
  1634. .ndo_change_mtu = bdx_change_mtu,
  1635. .ndo_set_mac_address = bdx_set_mac,
  1636. .ndo_vlan_rx_add_vid = bdx_vlan_rx_add_vid,
  1637. .ndo_vlan_rx_kill_vid = bdx_vlan_rx_kill_vid,
  1638. };
  1639. /**
  1640. * bdx_probe - Device Initialization Routine
  1641. * @pdev: PCI device information struct
  1642. * @ent: entry in bdx_pci_tbl
  1643. *
  1644. * Returns 0 on success, negative on failure
  1645. *
  1646. * bdx_probe initializes an adapter identified by a pci_dev structure.
  1647. * The OS initialization, configuring of the adapter private structure,
  1648. * and a hardware reset occur.
  1649. *
  1650. * functions and their order used as explained in
  1651. * /usr/src/linux/Documentation/DMA-{API,mapping}.txt
  1652. *
  1653. */
  1654. /* TBD: netif_msg should be checked and implemented. I disable it for now */
  1655. static int
  1656. bdx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1657. {
  1658. struct net_device *ndev;
  1659. struct bdx_priv *priv;
  1660. int err, pci_using_dac, port;
  1661. unsigned long pciaddr;
  1662. u32 regionSize;
  1663. struct pci_nic *nic;
  1664. ENTER;
  1665. nic = vmalloc(sizeof(*nic));
  1666. if (!nic)
  1667. RET(-ENOMEM);
  1668. /************** pci *****************/
  1669. err = pci_enable_device(pdev);
  1670. if (err) /* it triggers interrupt, dunno why. */
  1671. goto err_pci; /* it's not a problem though */
  1672. if (!(err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) &&
  1673. !(err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))) {
  1674. pci_using_dac = 1;
  1675. } else {
  1676. if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
  1677. (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
  1678. pr_err("No usable DMA configuration, aborting\n");
  1679. goto err_dma;
  1680. }
  1681. pci_using_dac = 0;
  1682. }
  1683. err = pci_request_regions(pdev, BDX_DRV_NAME);
  1684. if (err)
  1685. goto err_dma;
  1686. pci_set_master(pdev);
  1687. pciaddr = pci_resource_start(pdev, 0);
  1688. if (!pciaddr) {
  1689. err = -EIO;
  1690. pr_err("no MMIO resource\n");
  1691. goto err_out_res;
  1692. }
  1693. regionSize = pci_resource_len(pdev, 0);
  1694. if (regionSize < BDX_REGS_SIZE) {
  1695. err = -EIO;
  1696. pr_err("MMIO resource (%x) too small\n", regionSize);
  1697. goto err_out_res;
  1698. }
  1699. nic->regs = ioremap(pciaddr, regionSize);
  1700. if (!nic->regs) {
  1701. err = -EIO;
  1702. pr_err("ioremap failed\n");
  1703. goto err_out_res;
  1704. }
  1705. if (pdev->irq < 2) {
  1706. err = -EIO;
  1707. pr_err("invalid irq (%d)\n", pdev->irq);
  1708. goto err_out_iomap;
  1709. }
  1710. pci_set_drvdata(pdev, nic);
  1711. if (pdev->device == 0x3014)
  1712. nic->port_num = 2;
  1713. else
  1714. nic->port_num = 1;
  1715. print_hw_id(pdev);
  1716. bdx_hw_reset_direct(nic->regs);
  1717. nic->irq_type = IRQ_INTX;
  1718. #ifdef BDX_MSI
  1719. if ((readl(nic->regs + FPGA_VER) & 0xFFF) >= 378) {
  1720. err = pci_enable_msi(pdev);
  1721. if (err)
  1722. pr_err("Can't enable msi. error is %d\n", err);
  1723. else
  1724. nic->irq_type = IRQ_MSI;
  1725. } else
  1726. DBG("HW does not support MSI\n");
  1727. #endif
  1728. /************** netdev **************/
  1729. for (port = 0; port < nic->port_num; port++) {
  1730. ndev = alloc_etherdev(sizeof(struct bdx_priv));
  1731. if (!ndev) {
  1732. err = -ENOMEM;
  1733. goto err_out_iomap;
  1734. }
  1735. ndev->netdev_ops = &bdx_netdev_ops;
  1736. ndev->tx_queue_len = BDX_NDEV_TXQ_LEN;
  1737. bdx_set_ethtool_ops(ndev); /* ethtool interface */
  1738. /* these fields are used for info purposes only
  1739. * so we can have them same for all ports of the board */
  1740. ndev->if_port = port;
  1741. ndev->features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO
  1742. | NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
  1743. NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXCSUM
  1744. ;
  1745. ndev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
  1746. NETIF_F_TSO | NETIF_F_HW_VLAN_CTAG_TX;
  1747. if (pci_using_dac)
  1748. ndev->features |= NETIF_F_HIGHDMA;
  1749. /************** priv ****************/
  1750. priv = nic->priv[port] = netdev_priv(ndev);
  1751. priv->pBdxRegs = nic->regs + port * 0x8000;
  1752. priv->port = port;
  1753. priv->pdev = pdev;
  1754. priv->ndev = ndev;
  1755. priv->nic = nic;
  1756. priv->msg_enable = BDX_DEF_MSG_ENABLE;
  1757. netif_napi_add(ndev, &priv->napi, bdx_poll, 64);
  1758. if ((readl(nic->regs + FPGA_VER) & 0xFFF) == 308) {
  1759. DBG("HW statistics not supported\n");
  1760. priv->stats_flag = 0;
  1761. } else {
  1762. priv->stats_flag = 1;
  1763. }
  1764. /* Initialize fifo sizes. */
  1765. priv->txd_size = 2;
  1766. priv->txf_size = 2;
  1767. priv->rxd_size = 2;
  1768. priv->rxf_size = 3;
  1769. /* Initialize the initial coalescing registers. */
  1770. priv->rdintcm = INT_REG_VAL(0x20, 1, 4, 12);
  1771. priv->tdintcm = INT_REG_VAL(0x20, 1, 0, 12);
  1772. /* ndev->xmit_lock spinlock is not used.
  1773. * Private priv->tx_lock is used for synchronization
  1774. * between transmit and TX irq cleanup. In addition
  1775. * set multicast list callback has to use priv->tx_lock.
  1776. */
  1777. #ifdef BDX_LLTX
  1778. ndev->features |= NETIF_F_LLTX;
  1779. #endif
  1780. /* MTU range: 60 - 16384 */
  1781. ndev->min_mtu = ETH_ZLEN;
  1782. ndev->max_mtu = BDX_MAX_MTU;
  1783. spin_lock_init(&priv->tx_lock);
  1784. /*bdx_hw_reset(priv); */
  1785. if (bdx_read_mac(priv)) {
  1786. pr_err("load MAC address failed\n");
  1787. goto err_out_iomap;
  1788. }
  1789. SET_NETDEV_DEV(ndev, &pdev->dev);
  1790. err = register_netdev(ndev);
  1791. if (err) {
  1792. pr_err("register_netdev failed\n");
  1793. goto err_out_free;
  1794. }
  1795. netif_carrier_off(ndev);
  1796. netif_stop_queue(ndev);
  1797. print_eth_id(ndev);
  1798. }
  1799. RET(0);
  1800. err_out_free:
  1801. free_netdev(ndev);
  1802. err_out_iomap:
  1803. iounmap(nic->regs);
  1804. err_out_res:
  1805. pci_release_regions(pdev);
  1806. err_dma:
  1807. pci_disable_device(pdev);
  1808. err_pci:
  1809. vfree(nic);
  1810. RET(err);
  1811. }
  1812. /****************** Ethtool interface *********************/
  1813. /* get strings for statistics counters */
  1814. static const char
  1815. bdx_stat_names[][ETH_GSTRING_LEN] = {
  1816. "InUCast", /* 0x7200 */
  1817. "InMCast", /* 0x7210 */
  1818. "InBCast", /* 0x7220 */
  1819. "InPkts", /* 0x7230 */
  1820. "InErrors", /* 0x7240 */
  1821. "InDropped", /* 0x7250 */
  1822. "FrameTooLong", /* 0x7260 */
  1823. "FrameSequenceErrors", /* 0x7270 */
  1824. "InVLAN", /* 0x7280 */
  1825. "InDroppedDFE", /* 0x7290 */
  1826. "InDroppedIntFull", /* 0x72A0 */
  1827. "InFrameAlignErrors", /* 0x72B0 */
  1828. /* 0x72C0-0x72E0 RSRV */
  1829. "OutUCast", /* 0x72F0 */
  1830. "OutMCast", /* 0x7300 */
  1831. "OutBCast", /* 0x7310 */
  1832. "OutPkts", /* 0x7320 */
  1833. /* 0x7330-0x7360 RSRV */
  1834. "OutVLAN", /* 0x7370 */
  1835. "InUCastOctects", /* 0x7380 */
  1836. "OutUCastOctects", /* 0x7390 */
  1837. /* 0x73A0-0x73B0 RSRV */
  1838. "InBCastOctects", /* 0x73C0 */
  1839. "OutBCastOctects", /* 0x73D0 */
  1840. "InOctects", /* 0x73E0 */
  1841. "OutOctects", /* 0x73F0 */
  1842. };
  1843. /*
  1844. * bdx_get_link_ksettings - get device-specific settings
  1845. * @netdev
  1846. * @ecmd
  1847. */
  1848. static int bdx_get_link_ksettings(struct net_device *netdev,
  1849. struct ethtool_link_ksettings *ecmd)
  1850. {
  1851. ethtool_link_ksettings_zero_link_mode(ecmd, supported);
  1852. ethtool_link_ksettings_add_link_mode(ecmd, supported,
  1853. 10000baseT_Full);
  1854. ethtool_link_ksettings_add_link_mode(ecmd, supported, FIBRE);
  1855. ethtool_link_ksettings_zero_link_mode(ecmd, advertising);
  1856. ethtool_link_ksettings_add_link_mode(ecmd, advertising,
  1857. 10000baseT_Full);
  1858. ethtool_link_ksettings_add_link_mode(ecmd, advertising, FIBRE);
  1859. ecmd->base.speed = SPEED_10000;
  1860. ecmd->base.duplex = DUPLEX_FULL;
  1861. ecmd->base.port = PORT_FIBRE;
  1862. ecmd->base.autoneg = AUTONEG_DISABLE;
  1863. return 0;
  1864. }
  1865. /*
  1866. * bdx_get_drvinfo - report driver information
  1867. * @netdev
  1868. * @drvinfo
  1869. */
  1870. static void
  1871. bdx_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
  1872. {
  1873. struct bdx_priv *priv = netdev_priv(netdev);
  1874. strlcpy(drvinfo->driver, BDX_DRV_NAME, sizeof(drvinfo->driver));
  1875. strlcpy(drvinfo->version, BDX_DRV_VERSION, sizeof(drvinfo->version));
  1876. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  1877. strlcpy(drvinfo->bus_info, pci_name(priv->pdev),
  1878. sizeof(drvinfo->bus_info));
  1879. }
  1880. /*
  1881. * bdx_get_coalesce - get interrupt coalescing parameters
  1882. * @netdev
  1883. * @ecoal
  1884. */
  1885. static int
  1886. bdx_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ecoal)
  1887. {
  1888. u32 rdintcm;
  1889. u32 tdintcm;
  1890. struct bdx_priv *priv = netdev_priv(netdev);
  1891. rdintcm = priv->rdintcm;
  1892. tdintcm = priv->tdintcm;
  1893. /* PCK_TH measures in multiples of FIFO bytes
  1894. We translate to packets */
  1895. ecoal->rx_coalesce_usecs = GET_INT_COAL(rdintcm) * INT_COAL_MULT;
  1896. ecoal->rx_max_coalesced_frames =
  1897. ((GET_PCK_TH(rdintcm) * PCK_TH_MULT) / sizeof(struct rxf_desc));
  1898. ecoal->tx_coalesce_usecs = GET_INT_COAL(tdintcm) * INT_COAL_MULT;
  1899. ecoal->tx_max_coalesced_frames =
  1900. ((GET_PCK_TH(tdintcm) * PCK_TH_MULT) / BDX_TXF_DESC_SZ);
  1901. /* adaptive parameters ignored */
  1902. return 0;
  1903. }
  1904. /*
  1905. * bdx_set_coalesce - set interrupt coalescing parameters
  1906. * @netdev
  1907. * @ecoal
  1908. */
  1909. static int
  1910. bdx_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ecoal)
  1911. {
  1912. u32 rdintcm;
  1913. u32 tdintcm;
  1914. struct bdx_priv *priv = netdev_priv(netdev);
  1915. int rx_coal;
  1916. int tx_coal;
  1917. int rx_max_coal;
  1918. int tx_max_coal;
  1919. /* Check for valid input */
  1920. rx_coal = ecoal->rx_coalesce_usecs / INT_COAL_MULT;
  1921. tx_coal = ecoal->tx_coalesce_usecs / INT_COAL_MULT;
  1922. rx_max_coal = ecoal->rx_max_coalesced_frames;
  1923. tx_max_coal = ecoal->tx_max_coalesced_frames;
  1924. /* Translate from packets to multiples of FIFO bytes */
  1925. rx_max_coal =
  1926. (((rx_max_coal * sizeof(struct rxf_desc)) + PCK_TH_MULT - 1)
  1927. / PCK_TH_MULT);
  1928. tx_max_coal =
  1929. (((tx_max_coal * BDX_TXF_DESC_SZ) + PCK_TH_MULT - 1)
  1930. / PCK_TH_MULT);
  1931. if ((rx_coal > 0x7FFF) || (tx_coal > 0x7FFF) ||
  1932. (rx_max_coal > 0xF) || (tx_max_coal > 0xF))
  1933. return -EINVAL;
  1934. rdintcm = INT_REG_VAL(rx_coal, GET_INT_COAL_RC(priv->rdintcm),
  1935. GET_RXF_TH(priv->rdintcm), rx_max_coal);
  1936. tdintcm = INT_REG_VAL(tx_coal, GET_INT_COAL_RC(priv->tdintcm), 0,
  1937. tx_max_coal);
  1938. priv->rdintcm = rdintcm;
  1939. priv->tdintcm = tdintcm;
  1940. WRITE_REG(priv, regRDINTCM0, rdintcm);
  1941. WRITE_REG(priv, regTDINTCM0, tdintcm);
  1942. return 0;
  1943. }
  1944. /* Convert RX fifo size to number of pending packets */
  1945. static inline int bdx_rx_fifo_size_to_packets(int rx_size)
  1946. {
  1947. return (FIFO_SIZE * (1 << rx_size)) / sizeof(struct rxf_desc);
  1948. }
  1949. /* Convert TX fifo size to number of pending packets */
  1950. static inline int bdx_tx_fifo_size_to_packets(int tx_size)
  1951. {
  1952. return (FIFO_SIZE * (1 << tx_size)) / BDX_TXF_DESC_SZ;
  1953. }
  1954. /*
  1955. * bdx_get_ringparam - report ring sizes
  1956. * @netdev
  1957. * @ring
  1958. */
  1959. static void
  1960. bdx_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
  1961. {
  1962. struct bdx_priv *priv = netdev_priv(netdev);
  1963. /*max_pending - the maximum-sized FIFO we allow */
  1964. ring->rx_max_pending = bdx_rx_fifo_size_to_packets(3);
  1965. ring->tx_max_pending = bdx_tx_fifo_size_to_packets(3);
  1966. ring->rx_pending = bdx_rx_fifo_size_to_packets(priv->rxf_size);
  1967. ring->tx_pending = bdx_tx_fifo_size_to_packets(priv->txd_size);
  1968. }
  1969. /*
  1970. * bdx_set_ringparam - set ring sizes
  1971. * @netdev
  1972. * @ring
  1973. */
  1974. static int
  1975. bdx_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
  1976. {
  1977. struct bdx_priv *priv = netdev_priv(netdev);
  1978. int rx_size = 0;
  1979. int tx_size = 0;
  1980. for (; rx_size < 4; rx_size++) {
  1981. if (bdx_rx_fifo_size_to_packets(rx_size) >= ring->rx_pending)
  1982. break;
  1983. }
  1984. if (rx_size == 4)
  1985. rx_size = 3;
  1986. for (; tx_size < 4; tx_size++) {
  1987. if (bdx_tx_fifo_size_to_packets(tx_size) >= ring->tx_pending)
  1988. break;
  1989. }
  1990. if (tx_size == 4)
  1991. tx_size = 3;
  1992. /*Is there anything to do? */
  1993. if ((rx_size == priv->rxf_size) &&
  1994. (tx_size == priv->txd_size))
  1995. return 0;
  1996. priv->rxf_size = rx_size;
  1997. if (rx_size > 1)
  1998. priv->rxd_size = rx_size - 1;
  1999. else
  2000. priv->rxd_size = rx_size;
  2001. priv->txf_size = priv->txd_size = tx_size;
  2002. if (netif_running(netdev)) {
  2003. bdx_close(netdev);
  2004. bdx_open(netdev);
  2005. }
  2006. return 0;
  2007. }
  2008. /*
  2009. * bdx_get_strings - return a set of strings that describe the requested objects
  2010. * @netdev
  2011. * @data
  2012. */
  2013. static void bdx_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  2014. {
  2015. switch (stringset) {
  2016. case ETH_SS_STATS:
  2017. memcpy(data, *bdx_stat_names, sizeof(bdx_stat_names));
  2018. break;
  2019. }
  2020. }
  2021. /*
  2022. * bdx_get_sset_count - return number of statistics or tests
  2023. * @netdev
  2024. */
  2025. static int bdx_get_sset_count(struct net_device *netdev, int stringset)
  2026. {
  2027. struct bdx_priv *priv = netdev_priv(netdev);
  2028. switch (stringset) {
  2029. case ETH_SS_STATS:
  2030. BDX_ASSERT(ARRAY_SIZE(bdx_stat_names)
  2031. != sizeof(struct bdx_stats) / sizeof(u64));
  2032. return (priv->stats_flag) ? ARRAY_SIZE(bdx_stat_names) : 0;
  2033. }
  2034. return -EINVAL;
  2035. }
  2036. /*
  2037. * bdx_get_ethtool_stats - return device's hardware L2 statistics
  2038. * @netdev
  2039. * @stats
  2040. * @data
  2041. */
  2042. static void bdx_get_ethtool_stats(struct net_device *netdev,
  2043. struct ethtool_stats *stats, u64 *data)
  2044. {
  2045. struct bdx_priv *priv = netdev_priv(netdev);
  2046. if (priv->stats_flag) {
  2047. /* Update stats from HW */
  2048. bdx_update_stats(priv);
  2049. /* Copy data to user buffer */
  2050. memcpy(data, &priv->hw_stats, sizeof(priv->hw_stats));
  2051. }
  2052. }
  2053. /*
  2054. * bdx_set_ethtool_ops - ethtool interface implementation
  2055. * @netdev
  2056. */
  2057. static void bdx_set_ethtool_ops(struct net_device *netdev)
  2058. {
  2059. static const struct ethtool_ops bdx_ethtool_ops = {
  2060. .get_drvinfo = bdx_get_drvinfo,
  2061. .get_link = ethtool_op_get_link,
  2062. .get_coalesce = bdx_get_coalesce,
  2063. .set_coalesce = bdx_set_coalesce,
  2064. .get_ringparam = bdx_get_ringparam,
  2065. .set_ringparam = bdx_set_ringparam,
  2066. .get_strings = bdx_get_strings,
  2067. .get_sset_count = bdx_get_sset_count,
  2068. .get_ethtool_stats = bdx_get_ethtool_stats,
  2069. .get_link_ksettings = bdx_get_link_ksettings,
  2070. };
  2071. netdev->ethtool_ops = &bdx_ethtool_ops;
  2072. }
  2073. /**
  2074. * bdx_remove - Device Removal Routine
  2075. * @pdev: PCI device information struct
  2076. *
  2077. * bdx_remove is called by the PCI subsystem to alert the driver
  2078. * that it should release a PCI device. The could be caused by a
  2079. * Hot-Plug event, or because the driver is going to be removed from
  2080. * memory.
  2081. **/
  2082. static void bdx_remove(struct pci_dev *pdev)
  2083. {
  2084. struct pci_nic *nic = pci_get_drvdata(pdev);
  2085. struct net_device *ndev;
  2086. int port;
  2087. for (port = 0; port < nic->port_num; port++) {
  2088. ndev = nic->priv[port]->ndev;
  2089. unregister_netdev(ndev);
  2090. free_netdev(ndev);
  2091. }
  2092. /*bdx_hw_reset_direct(nic->regs); */
  2093. #ifdef BDX_MSI
  2094. if (nic->irq_type == IRQ_MSI)
  2095. pci_disable_msi(pdev);
  2096. #endif
  2097. iounmap(nic->regs);
  2098. pci_release_regions(pdev);
  2099. pci_disable_device(pdev);
  2100. vfree(nic);
  2101. RET();
  2102. }
  2103. static struct pci_driver bdx_pci_driver = {
  2104. .name = BDX_DRV_NAME,
  2105. .id_table = bdx_pci_tbl,
  2106. .probe = bdx_probe,
  2107. .remove = bdx_remove,
  2108. };
  2109. /*
  2110. * print_driver_id - print parameters of the driver build
  2111. */
  2112. static void __init print_driver_id(void)
  2113. {
  2114. pr_info("%s, %s\n", BDX_DRV_DESC, BDX_DRV_VERSION);
  2115. pr_info("Options: hw_csum %s\n", BDX_MSI_STRING);
  2116. }
  2117. static int __init bdx_module_init(void)
  2118. {
  2119. ENTER;
  2120. init_txd_sizes();
  2121. print_driver_id();
  2122. RET(pci_register_driver(&bdx_pci_driver));
  2123. }
  2124. module_init(bdx_module_init);
  2125. static void __exit bdx_module_exit(void)
  2126. {
  2127. ENTER;
  2128. pci_unregister_driver(&bdx_pci_driver);
  2129. RET();
  2130. }
  2131. module_exit(bdx_module_exit);
  2132. MODULE_LICENSE("GPL");
  2133. MODULE_AUTHOR(DRIVER_AUTHOR);
  2134. MODULE_DESCRIPTION(BDX_DRV_DESC);
  2135. MODULE_FIRMWARE("tehuti/bdx.bin");