tehuti.c 66 KB

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