tehuti.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482
  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(napi);
  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 = reject_firmware(&fw, "/*(DEBLOBBED)*/", &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. }
  565. if (!capable(CAP_SYS_RAWIO))
  566. return -EPERM;
  567. switch (data[0]) {
  568. case BDX_OP_READ:
  569. error = bdx_range_check(priv, data[1]);
  570. if (error < 0)
  571. return error;
  572. data[2] = READ_REG(priv, data[1]);
  573. DBG("read_reg(0x%x)=0x%x (dec %d)\n", data[1], data[2],
  574. data[2]);
  575. error = copy_to_user(ifr->ifr_data, data, sizeof(data));
  576. if (error)
  577. RET(-EFAULT);
  578. break;
  579. case BDX_OP_WRITE:
  580. error = bdx_range_check(priv, data[1]);
  581. if (error < 0)
  582. return error;
  583. WRITE_REG(priv, data[1], data[2]);
  584. DBG("write_reg(0x%x, 0x%x)\n", data[1], data[2]);
  585. break;
  586. default:
  587. RET(-EOPNOTSUPP);
  588. }
  589. return 0;
  590. }
  591. static int bdx_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd)
  592. {
  593. ENTER;
  594. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
  595. RET(bdx_ioctl_priv(ndev, ifr, cmd));
  596. else
  597. RET(-EOPNOTSUPP);
  598. }
  599. /**
  600. * __bdx_vlan_rx_vid - private helper for adding/killing VLAN vid
  601. * @ndev: network device
  602. * @vid: VLAN vid
  603. * @op: add or kill operation
  604. *
  605. * Passes VLAN filter table to hardware
  606. */
  607. static void __bdx_vlan_rx_vid(struct net_device *ndev, uint16_t vid, int enable)
  608. {
  609. struct bdx_priv *priv = netdev_priv(ndev);
  610. u32 reg, bit, val;
  611. ENTER;
  612. DBG2("vid=%d value=%d\n", (int)vid, enable);
  613. if (unlikely(vid >= 4096)) {
  614. pr_err("invalid VID: %u (> 4096)\n", vid);
  615. RET();
  616. }
  617. reg = regVLAN_0 + (vid / 32) * 4;
  618. bit = 1 << vid % 32;
  619. val = READ_REG(priv, reg);
  620. DBG2("reg=%x, val=%x, bit=%d\n", reg, val, bit);
  621. if (enable)
  622. val |= bit;
  623. else
  624. val &= ~bit;
  625. DBG2("new val %x\n", val);
  626. WRITE_REG(priv, reg, val);
  627. RET();
  628. }
  629. /**
  630. * bdx_vlan_rx_add_vid - kernel hook for adding VLAN vid to hw filtering table
  631. * @ndev: network device
  632. * @vid: VLAN vid to add
  633. */
  634. static int bdx_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid)
  635. {
  636. __bdx_vlan_rx_vid(ndev, vid, 1);
  637. return 0;
  638. }
  639. /**
  640. * bdx_vlan_rx_kill_vid - kernel hook for killing VLAN vid in hw filtering table
  641. * @ndev: network device
  642. * @vid: VLAN vid to kill
  643. */
  644. static int bdx_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid)
  645. {
  646. __bdx_vlan_rx_vid(ndev, vid, 0);
  647. return 0;
  648. }
  649. /**
  650. * bdx_change_mtu - Change the Maximum Transfer Unit
  651. * @netdev: network interface device structure
  652. * @new_mtu: new value for maximum frame size
  653. *
  654. * Returns 0 on success, negative on failure
  655. */
  656. static int bdx_change_mtu(struct net_device *ndev, int new_mtu)
  657. {
  658. ENTER;
  659. if (new_mtu == ndev->mtu)
  660. RET(0);
  661. /* enforce minimum frame size */
  662. if (new_mtu < ETH_ZLEN) {
  663. netdev_err(ndev, "mtu %d is less then minimal %d\n",
  664. new_mtu, ETH_ZLEN);
  665. RET(-EINVAL);
  666. }
  667. ndev->mtu = new_mtu;
  668. if (netif_running(ndev)) {
  669. bdx_close(ndev);
  670. bdx_open(ndev);
  671. }
  672. RET(0);
  673. }
  674. static void bdx_setmulti(struct net_device *ndev)
  675. {
  676. struct bdx_priv *priv = netdev_priv(ndev);
  677. u32 rxf_val =
  678. GMAC_RX_FILTER_AM | GMAC_RX_FILTER_AB | GMAC_RX_FILTER_OSEN;
  679. int i;
  680. ENTER;
  681. /* IMF - imperfect (hash) rx multicat filter */
  682. /* PMF - perfect rx multicat filter */
  683. /* FIXME: RXE(OFF) */
  684. if (ndev->flags & IFF_PROMISC) {
  685. rxf_val |= GMAC_RX_FILTER_PRM;
  686. } else if (ndev->flags & IFF_ALLMULTI) {
  687. /* set IMF to accept all multicast frmaes */
  688. for (i = 0; i < MAC_MCST_HASH_NUM; i++)
  689. WRITE_REG(priv, regRX_MCST_HASH0 + i * 4, ~0);
  690. } else if (!netdev_mc_empty(ndev)) {
  691. u8 hash;
  692. struct netdev_hw_addr *ha;
  693. u32 reg, val;
  694. /* set IMF to deny all multicast frames */
  695. for (i = 0; i < MAC_MCST_HASH_NUM; i++)
  696. WRITE_REG(priv, regRX_MCST_HASH0 + i * 4, 0);
  697. /* set PMF to deny all multicast frames */
  698. for (i = 0; i < MAC_MCST_NUM; i++) {
  699. WRITE_REG(priv, regRX_MAC_MCST0 + i * 8, 0);
  700. WRITE_REG(priv, regRX_MAC_MCST1 + i * 8, 0);
  701. }
  702. /* use PMF to accept first MAC_MCST_NUM (15) addresses */
  703. /* TBD: sort addresses and write them in ascending order
  704. * into RX_MAC_MCST regs. we skip this phase now and accept ALL
  705. * multicast frames throu IMF */
  706. /* accept the rest of addresses throu IMF */
  707. netdev_for_each_mc_addr(ha, ndev) {
  708. hash = 0;
  709. for (i = 0; i < ETH_ALEN; i++)
  710. hash ^= ha->addr[i];
  711. reg = regRX_MCST_HASH0 + ((hash >> 5) << 2);
  712. val = READ_REG(priv, reg);
  713. val |= (1 << (hash % 32));
  714. WRITE_REG(priv, reg, val);
  715. }
  716. } else {
  717. DBG("only own mac %d\n", netdev_mc_count(ndev));
  718. rxf_val |= GMAC_RX_FILTER_AB;
  719. }
  720. WRITE_REG(priv, regGMAC_RXF_A, rxf_val);
  721. /* enable RX */
  722. /* FIXME: RXE(ON) */
  723. RET();
  724. }
  725. static int bdx_set_mac(struct net_device *ndev, void *p)
  726. {
  727. struct bdx_priv *priv = netdev_priv(ndev);
  728. struct sockaddr *addr = p;
  729. ENTER;
  730. /*
  731. if (netif_running(dev))
  732. return -EBUSY
  733. */
  734. memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
  735. bdx_restore_mac(ndev, priv);
  736. RET(0);
  737. }
  738. static int bdx_read_mac(struct bdx_priv *priv)
  739. {
  740. u16 macAddress[3], i;
  741. ENTER;
  742. macAddress[2] = READ_REG(priv, regUNC_MAC0_A);
  743. macAddress[2] = READ_REG(priv, regUNC_MAC0_A);
  744. macAddress[1] = READ_REG(priv, regUNC_MAC1_A);
  745. macAddress[1] = READ_REG(priv, regUNC_MAC1_A);
  746. macAddress[0] = READ_REG(priv, regUNC_MAC2_A);
  747. macAddress[0] = READ_REG(priv, regUNC_MAC2_A);
  748. for (i = 0; i < 3; i++) {
  749. priv->ndev->dev_addr[i * 2 + 1] = macAddress[i];
  750. priv->ndev->dev_addr[i * 2] = macAddress[i] >> 8;
  751. }
  752. RET(0);
  753. }
  754. static u64 bdx_read_l2stat(struct bdx_priv *priv, int reg)
  755. {
  756. u64 val;
  757. val = READ_REG(priv, reg);
  758. val |= ((u64) READ_REG(priv, reg + 8)) << 32;
  759. return val;
  760. }
  761. /*Do the statistics-update work*/
  762. static void bdx_update_stats(struct bdx_priv *priv)
  763. {
  764. struct bdx_stats *stats = &priv->hw_stats;
  765. u64 *stats_vector = (u64 *) stats;
  766. int i;
  767. int addr;
  768. /*Fill HW structure */
  769. addr = 0x7200;
  770. /*First 12 statistics - 0x7200 - 0x72B0 */
  771. for (i = 0; i < 12; i++) {
  772. stats_vector[i] = bdx_read_l2stat(priv, addr);
  773. addr += 0x10;
  774. }
  775. BDX_ASSERT(addr != 0x72C0);
  776. /* 0x72C0-0x72E0 RSRV */
  777. addr = 0x72F0;
  778. for (; i < 16; i++) {
  779. stats_vector[i] = bdx_read_l2stat(priv, addr);
  780. addr += 0x10;
  781. }
  782. BDX_ASSERT(addr != 0x7330);
  783. /* 0x7330-0x7360 RSRV */
  784. addr = 0x7370;
  785. for (; i < 19; i++) {
  786. stats_vector[i] = bdx_read_l2stat(priv, addr);
  787. addr += 0x10;
  788. }
  789. BDX_ASSERT(addr != 0x73A0);
  790. /* 0x73A0-0x73B0 RSRV */
  791. addr = 0x73C0;
  792. for (; i < 23; i++) {
  793. stats_vector[i] = bdx_read_l2stat(priv, addr);
  794. addr += 0x10;
  795. }
  796. BDX_ASSERT(addr != 0x7400);
  797. BDX_ASSERT((sizeof(struct bdx_stats) / sizeof(u64)) != i);
  798. }
  799. static void print_rxdd(struct rxd_desc *rxdd, u32 rxd_val1, u16 len,
  800. u16 rxd_vlan);
  801. static void print_rxfd(struct rxf_desc *rxfd);
  802. /*************************************************************************
  803. * Rx DB *
  804. *************************************************************************/
  805. static void bdx_rxdb_destroy(struct rxdb *db)
  806. {
  807. vfree(db);
  808. }
  809. static struct rxdb *bdx_rxdb_create(int nelem)
  810. {
  811. struct rxdb *db;
  812. int i;
  813. db = vmalloc(sizeof(struct rxdb)
  814. + (nelem * sizeof(int))
  815. + (nelem * sizeof(struct rx_map)));
  816. if (likely(db != NULL)) {
  817. db->stack = (int *)(db + 1);
  818. db->elems = (void *)(db->stack + nelem);
  819. db->nelem = nelem;
  820. db->top = nelem;
  821. for (i = 0; i < nelem; i++)
  822. db->stack[i] = nelem - i - 1; /* to make first allocs
  823. close to db struct*/
  824. }
  825. return db;
  826. }
  827. static inline int bdx_rxdb_alloc_elem(struct rxdb *db)
  828. {
  829. BDX_ASSERT(db->top <= 0);
  830. return db->stack[--(db->top)];
  831. }
  832. static inline void *bdx_rxdb_addr_elem(struct rxdb *db, int n)
  833. {
  834. BDX_ASSERT((n < 0) || (n >= db->nelem));
  835. return db->elems + n;
  836. }
  837. static inline int bdx_rxdb_available(struct rxdb *db)
  838. {
  839. return db->top;
  840. }
  841. static inline void bdx_rxdb_free_elem(struct rxdb *db, int n)
  842. {
  843. BDX_ASSERT((n >= db->nelem) || (n < 0));
  844. db->stack[(db->top)++] = n;
  845. }
  846. /*************************************************************************
  847. * Rx Init *
  848. *************************************************************************/
  849. /**
  850. * bdx_rx_init - initialize RX all related HW and SW resources
  851. * @priv: NIC private structure
  852. *
  853. * Returns 0 on success, negative value on failure
  854. *
  855. * It creates rxf and rxd fifos, update relevant HW registers, preallocate
  856. * skb for rx. It assumes that Rx is desabled in HW
  857. * funcs are grouped for better cache usage
  858. *
  859. * RxD fifo is smaller than RxF fifo by design. Upon high load, RxD will be
  860. * filled and packets will be dropped by nic without getting into host or
  861. * cousing interrupt. Anyway, in that condition, host has no chance to process
  862. * all packets, but dropping in nic is cheaper, since it takes 0 cpu cycles
  863. */
  864. /* TBD: ensure proper packet size */
  865. static int bdx_rx_init(struct bdx_priv *priv)
  866. {
  867. ENTER;
  868. if (bdx_fifo_init(priv, &priv->rxd_fifo0.m, priv->rxd_size,
  869. regRXD_CFG0_0, regRXD_CFG1_0,
  870. regRXD_RPTR_0, regRXD_WPTR_0))
  871. goto err_mem;
  872. if (bdx_fifo_init(priv, &priv->rxf_fifo0.m, priv->rxf_size,
  873. regRXF_CFG0_0, regRXF_CFG1_0,
  874. regRXF_RPTR_0, regRXF_WPTR_0))
  875. goto err_mem;
  876. priv->rxdb = bdx_rxdb_create(priv->rxf_fifo0.m.memsz /
  877. sizeof(struct rxf_desc));
  878. if (!priv->rxdb)
  879. goto err_mem;
  880. priv->rxf_fifo0.m.pktsz = priv->ndev->mtu + VLAN_ETH_HLEN;
  881. return 0;
  882. err_mem:
  883. netdev_err(priv->ndev, "Rx init failed\n");
  884. return -ENOMEM;
  885. }
  886. /**
  887. * bdx_rx_free_skbs - frees and unmaps all skbs allocated for the fifo
  888. * @priv: NIC private structure
  889. * @f: RXF fifo
  890. */
  891. static void bdx_rx_free_skbs(struct bdx_priv *priv, struct rxf_fifo *f)
  892. {
  893. struct rx_map *dm;
  894. struct rxdb *db = priv->rxdb;
  895. u16 i;
  896. ENTER;
  897. DBG("total=%d free=%d busy=%d\n", db->nelem, bdx_rxdb_available(db),
  898. db->nelem - bdx_rxdb_available(db));
  899. while (bdx_rxdb_available(db) > 0) {
  900. i = bdx_rxdb_alloc_elem(db);
  901. dm = bdx_rxdb_addr_elem(db, i);
  902. dm->dma = 0;
  903. }
  904. for (i = 0; i < db->nelem; i++) {
  905. dm = bdx_rxdb_addr_elem(db, i);
  906. if (dm->dma) {
  907. pci_unmap_single(priv->pdev,
  908. dm->dma, f->m.pktsz,
  909. PCI_DMA_FROMDEVICE);
  910. dev_kfree_skb(dm->skb);
  911. }
  912. }
  913. }
  914. /**
  915. * bdx_rx_free - release all Rx resources
  916. * @priv: NIC private structure
  917. *
  918. * It assumes that Rx is desabled in HW
  919. */
  920. static void bdx_rx_free(struct bdx_priv *priv)
  921. {
  922. ENTER;
  923. if (priv->rxdb) {
  924. bdx_rx_free_skbs(priv, &priv->rxf_fifo0);
  925. bdx_rxdb_destroy(priv->rxdb);
  926. priv->rxdb = NULL;
  927. }
  928. bdx_fifo_free(priv, &priv->rxf_fifo0.m);
  929. bdx_fifo_free(priv, &priv->rxd_fifo0.m);
  930. RET();
  931. }
  932. /*************************************************************************
  933. * Rx Engine *
  934. *************************************************************************/
  935. /**
  936. * bdx_rx_alloc_skbs - fill rxf fifo with new skbs
  937. * @priv: nic's private structure
  938. * @f: RXF fifo that needs skbs
  939. *
  940. * It allocates skbs, build rxf descs and push it (rxf descr) into rxf fifo.
  941. * skb's virtual and physical addresses are stored in skb db.
  942. * To calculate free space, func uses cached values of RPTR and WPTR
  943. * When needed, it also updates RPTR and WPTR.
  944. */
  945. /* TBD: do not update WPTR if no desc were written */
  946. static void bdx_rx_alloc_skbs(struct bdx_priv *priv, struct rxf_fifo *f)
  947. {
  948. struct sk_buff *skb;
  949. struct rxf_desc *rxfd;
  950. struct rx_map *dm;
  951. int dno, delta, idx;
  952. struct rxdb *db = priv->rxdb;
  953. ENTER;
  954. dno = bdx_rxdb_available(db) - 1;
  955. while (dno > 0) {
  956. skb = netdev_alloc_skb(priv->ndev, f->m.pktsz + NET_IP_ALIGN);
  957. if (!skb)
  958. break;
  959. skb_reserve(skb, NET_IP_ALIGN);
  960. idx = bdx_rxdb_alloc_elem(db);
  961. dm = bdx_rxdb_addr_elem(db, idx);
  962. dm->dma = pci_map_single(priv->pdev,
  963. skb->data, f->m.pktsz,
  964. PCI_DMA_FROMDEVICE);
  965. dm->skb = skb;
  966. rxfd = (struct rxf_desc *)(f->m.va + f->m.wptr);
  967. rxfd->info = CPU_CHIP_SWAP32(0x10003); /* INFO=1 BC=3 */
  968. rxfd->va_lo = idx;
  969. rxfd->pa_lo = CPU_CHIP_SWAP32(L32_64(dm->dma));
  970. rxfd->pa_hi = CPU_CHIP_SWAP32(H32_64(dm->dma));
  971. rxfd->len = CPU_CHIP_SWAP32(f->m.pktsz);
  972. print_rxfd(rxfd);
  973. f->m.wptr += sizeof(struct rxf_desc);
  974. delta = f->m.wptr - f->m.memsz;
  975. if (unlikely(delta >= 0)) {
  976. f->m.wptr = delta;
  977. if (delta > 0) {
  978. memcpy(f->m.va, f->m.va + f->m.memsz, delta);
  979. DBG("wrapped descriptor\n");
  980. }
  981. }
  982. dno--;
  983. }
  984. /*TBD: to do - delayed rxf wptr like in txd */
  985. WRITE_REG(priv, f->m.reg_WPTR, f->m.wptr & TXF_WPTR_WR_PTR);
  986. RET();
  987. }
  988. static inline void
  989. NETIF_RX_MUX(struct bdx_priv *priv, u32 rxd_val1, u16 rxd_vlan,
  990. struct sk_buff *skb)
  991. {
  992. ENTER;
  993. DBG("rxdd->flags.bits.vtag=%d\n", GET_RXD_VTAG(rxd_val1));
  994. if (GET_RXD_VTAG(rxd_val1)) {
  995. DBG("%s: vlan rcv vlan '%x' vtag '%x'\n",
  996. priv->ndev->name,
  997. GET_RXD_VLAN_ID(rxd_vlan),
  998. GET_RXD_VTAG(rxd_val1));
  999. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), GET_RXD_VLAN_TCI(rxd_vlan));
  1000. }
  1001. netif_receive_skb(skb);
  1002. }
  1003. static void bdx_recycle_skb(struct bdx_priv *priv, struct rxd_desc *rxdd)
  1004. {
  1005. struct rxf_desc *rxfd;
  1006. struct rx_map *dm;
  1007. struct rxf_fifo *f;
  1008. struct rxdb *db;
  1009. struct sk_buff *skb;
  1010. int delta;
  1011. ENTER;
  1012. DBG("priv=%p rxdd=%p\n", priv, rxdd);
  1013. f = &priv->rxf_fifo0;
  1014. db = priv->rxdb;
  1015. DBG("db=%p f=%p\n", db, f);
  1016. dm = bdx_rxdb_addr_elem(db, rxdd->va_lo);
  1017. DBG("dm=%p\n", dm);
  1018. skb = dm->skb;
  1019. rxfd = (struct rxf_desc *)(f->m.va + f->m.wptr);
  1020. rxfd->info = CPU_CHIP_SWAP32(0x10003); /* INFO=1 BC=3 */
  1021. rxfd->va_lo = rxdd->va_lo;
  1022. rxfd->pa_lo = CPU_CHIP_SWAP32(L32_64(dm->dma));
  1023. rxfd->pa_hi = CPU_CHIP_SWAP32(H32_64(dm->dma));
  1024. rxfd->len = CPU_CHIP_SWAP32(f->m.pktsz);
  1025. print_rxfd(rxfd);
  1026. f->m.wptr += sizeof(struct rxf_desc);
  1027. delta = f->m.wptr - f->m.memsz;
  1028. if (unlikely(delta >= 0)) {
  1029. f->m.wptr = delta;
  1030. if (delta > 0) {
  1031. memcpy(f->m.va, f->m.va + f->m.memsz, delta);
  1032. DBG("wrapped descriptor\n");
  1033. }
  1034. }
  1035. RET();
  1036. }
  1037. /**
  1038. * bdx_rx_receive - receives full packets from RXD fifo and pass them to OS
  1039. * NOTE: a special treatment is given to non-continuous descriptors
  1040. * that start near the end, wraps around and continue at the beginning. a second
  1041. * part is copied right after the first, and then descriptor is interpreted as
  1042. * normal. fifo has an extra space to allow such operations
  1043. * @priv: nic's private structure
  1044. * @f: RXF fifo that needs skbs
  1045. * @budget: maximum number of packets to receive
  1046. */
  1047. /* TBD: replace memcpy func call by explicite inline asm */
  1048. static int bdx_rx_receive(struct bdx_priv *priv, struct rxd_fifo *f, int budget)
  1049. {
  1050. struct net_device *ndev = priv->ndev;
  1051. struct sk_buff *skb, *skb2;
  1052. struct rxd_desc *rxdd;
  1053. struct rx_map *dm;
  1054. struct rxf_fifo *rxf_fifo;
  1055. int tmp_len, size;
  1056. int done = 0;
  1057. int max_done = BDX_MAX_RX_DONE;
  1058. struct rxdb *db = NULL;
  1059. /* Unmarshalled descriptor - copy of descriptor in host order */
  1060. u32 rxd_val1;
  1061. u16 len;
  1062. u16 rxd_vlan;
  1063. ENTER;
  1064. max_done = budget;
  1065. f->m.wptr = READ_REG(priv, f->m.reg_WPTR) & TXF_WPTR_WR_PTR;
  1066. size = f->m.wptr - f->m.rptr;
  1067. if (size < 0)
  1068. size = f->m.memsz + size; /* size is negative :-) */
  1069. while (size > 0) {
  1070. rxdd = (struct rxd_desc *)(f->m.va + f->m.rptr);
  1071. rxd_val1 = CPU_CHIP_SWAP32(rxdd->rxd_val1);
  1072. len = CPU_CHIP_SWAP16(rxdd->len);
  1073. rxd_vlan = CPU_CHIP_SWAP16(rxdd->rxd_vlan);
  1074. print_rxdd(rxdd, rxd_val1, len, rxd_vlan);
  1075. tmp_len = GET_RXD_BC(rxd_val1) << 3;
  1076. BDX_ASSERT(tmp_len <= 0);
  1077. size -= tmp_len;
  1078. if (size < 0) /* test for partially arrived descriptor */
  1079. break;
  1080. f->m.rptr += tmp_len;
  1081. tmp_len = f->m.rptr - f->m.memsz;
  1082. if (unlikely(tmp_len >= 0)) {
  1083. f->m.rptr = tmp_len;
  1084. if (tmp_len > 0) {
  1085. DBG("wrapped desc rptr=%d tmp_len=%d\n",
  1086. f->m.rptr, tmp_len);
  1087. memcpy(f->m.va + f->m.memsz, f->m.va, tmp_len);
  1088. }
  1089. }
  1090. if (unlikely(GET_RXD_ERR(rxd_val1))) {
  1091. DBG("rxd_err = 0x%x\n", GET_RXD_ERR(rxd_val1));
  1092. ndev->stats.rx_errors++;
  1093. bdx_recycle_skb(priv, rxdd);
  1094. continue;
  1095. }
  1096. rxf_fifo = &priv->rxf_fifo0;
  1097. db = priv->rxdb;
  1098. dm = bdx_rxdb_addr_elem(db, rxdd->va_lo);
  1099. skb = dm->skb;
  1100. if (len < BDX_COPYBREAK &&
  1101. (skb2 = netdev_alloc_skb(priv->ndev, len + NET_IP_ALIGN))) {
  1102. skb_reserve(skb2, NET_IP_ALIGN);
  1103. /*skb_put(skb2, len); */
  1104. pci_dma_sync_single_for_cpu(priv->pdev,
  1105. dm->dma, rxf_fifo->m.pktsz,
  1106. PCI_DMA_FROMDEVICE);
  1107. memcpy(skb2->data, skb->data, len);
  1108. bdx_recycle_skb(priv, rxdd);
  1109. skb = skb2;
  1110. } else {
  1111. pci_unmap_single(priv->pdev,
  1112. dm->dma, rxf_fifo->m.pktsz,
  1113. PCI_DMA_FROMDEVICE);
  1114. bdx_rxdb_free_elem(db, rxdd->va_lo);
  1115. }
  1116. ndev->stats.rx_bytes += len;
  1117. skb_put(skb, len);
  1118. skb->protocol = eth_type_trans(skb, ndev);
  1119. /* Non-IP packets aren't checksum-offloaded */
  1120. if (GET_RXD_PKT_ID(rxd_val1) == 0)
  1121. skb_checksum_none_assert(skb);
  1122. else
  1123. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1124. NETIF_RX_MUX(priv, rxd_val1, rxd_vlan, skb);
  1125. if (++done >= max_done)
  1126. break;
  1127. }
  1128. ndev->stats.rx_packets += done;
  1129. /* FIXME: do smth to minimize pci accesses */
  1130. WRITE_REG(priv, f->m.reg_RPTR, f->m.rptr & TXF_WPTR_WR_PTR);
  1131. bdx_rx_alloc_skbs(priv, &priv->rxf_fifo0);
  1132. RET(done);
  1133. }
  1134. /*************************************************************************
  1135. * Debug / Temprorary Code *
  1136. *************************************************************************/
  1137. static void print_rxdd(struct rxd_desc *rxdd, u32 rxd_val1, u16 len,
  1138. u16 rxd_vlan)
  1139. {
  1140. 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",
  1141. GET_RXD_BC(rxd_val1), GET_RXD_RXFQ(rxd_val1), GET_RXD_TO(rxd_val1),
  1142. GET_RXD_TYPE(rxd_val1), GET_RXD_ERR(rxd_val1),
  1143. GET_RXD_RXP(rxd_val1), GET_RXD_PKT_ID(rxd_val1),
  1144. GET_RXD_VTAG(rxd_val1), len, GET_RXD_VLAN_ID(rxd_vlan),
  1145. GET_RXD_CFI(rxd_vlan), GET_RXD_PRIO(rxd_vlan), rxdd->va_lo,
  1146. rxdd->va_hi);
  1147. }
  1148. static void print_rxfd(struct rxf_desc *rxfd)
  1149. {
  1150. DBG("=== RxF desc CHIP ORDER/ENDIANNESS =============\n"
  1151. "info 0x%x va_lo %u pa_lo 0x%x pa_hi 0x%x len 0x%x\n",
  1152. rxfd->info, rxfd->va_lo, rxfd->pa_lo, rxfd->pa_hi, rxfd->len);
  1153. }
  1154. /*
  1155. * TX HW/SW interaction overview
  1156. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1157. * There are 2 types of TX communication channels between driver and NIC.
  1158. * 1) TX Free Fifo - TXF - holds ack descriptors for sent packets
  1159. * 2) TX Data Fifo - TXD - holds descriptors of full buffers.
  1160. *
  1161. * Currently NIC supports TSO, checksuming and gather DMA
  1162. * UFO and IP fragmentation is on the way
  1163. *
  1164. * RX SW Data Structures
  1165. * ~~~~~~~~~~~~~~~~~~~~~
  1166. * txdb - used to keep track of all skbs owned by SW and their dma addresses.
  1167. * For TX case, ownership lasts from geting packet via hard_xmit and until HW
  1168. * acknowledges sent by TXF descriptors.
  1169. * Implemented as cyclic buffer.
  1170. * fifo - keeps info about fifo's size and location, relevant HW registers,
  1171. * usage and skb db. Each RXD and RXF Fifo has its own fifo structure.
  1172. * Implemented as simple struct.
  1173. *
  1174. * TX SW Execution Flow
  1175. * ~~~~~~~~~~~~~~~~~~~~
  1176. * OS calls driver's hard_xmit method with packet to sent.
  1177. * Driver creates DMA mappings, builds TXD descriptors and kicks HW
  1178. * by updating TXD WPTR.
  1179. * When packet is sent, HW write us TXF descriptor and SW frees original skb.
  1180. * To prevent TXD fifo overflow without reading HW registers every time,
  1181. * SW deploys "tx level" technique.
  1182. * Upon strart up, tx level is initialized to TXD fifo length.
  1183. * For every sent packet, SW gets its TXD descriptor sizei
  1184. * (from precalculated array) and substructs it from tx level.
  1185. * The size is also stored in txdb. When TXF ack arrives, SW fetch size of
  1186. * original TXD descriptor from txdb and adds it to tx level.
  1187. * When Tx level drops under some predefined treshhold, the driver
  1188. * stops the TX queue. When TX level rises above that level,
  1189. * the tx queue is enabled again.
  1190. *
  1191. * This technique avoids eccessive reading of RPTR and WPTR registers.
  1192. * As our benchmarks shows, it adds 1.5 Gbit/sec to NIS's throuput.
  1193. */
  1194. /*************************************************************************
  1195. * Tx DB *
  1196. *************************************************************************/
  1197. static inline int bdx_tx_db_size(struct txdb *db)
  1198. {
  1199. int taken = db->wptr - db->rptr;
  1200. if (taken < 0)
  1201. taken = db->size + 1 + taken; /* (size + 1) equals memsz */
  1202. return db->size - taken;
  1203. }
  1204. /**
  1205. * __bdx_tx_db_ptr_next - helper function, increment read/write pointer + wrap
  1206. * @db: tx data base
  1207. * @pptr: read or write pointer
  1208. */
  1209. static inline void __bdx_tx_db_ptr_next(struct txdb *db, struct tx_map **pptr)
  1210. {
  1211. BDX_ASSERT(db == NULL || pptr == NULL); /* sanity */
  1212. BDX_ASSERT(*pptr != db->rptr && /* expect either read */
  1213. *pptr != db->wptr); /* or write pointer */
  1214. BDX_ASSERT(*pptr < db->start || /* pointer has to be */
  1215. *pptr >= db->end); /* in range */
  1216. ++*pptr;
  1217. if (unlikely(*pptr == db->end))
  1218. *pptr = db->start;
  1219. }
  1220. /**
  1221. * bdx_tx_db_inc_rptr - increment read pointer
  1222. * @db: tx data base
  1223. */
  1224. static inline void bdx_tx_db_inc_rptr(struct txdb *db)
  1225. {
  1226. BDX_ASSERT(db->rptr == db->wptr); /* can't read from empty db */
  1227. __bdx_tx_db_ptr_next(db, &db->rptr);
  1228. }
  1229. /**
  1230. * bdx_tx_db_inc_wptr - increment write pointer
  1231. * @db: tx data base
  1232. */
  1233. static inline void bdx_tx_db_inc_wptr(struct txdb *db)
  1234. {
  1235. __bdx_tx_db_ptr_next(db, &db->wptr);
  1236. BDX_ASSERT(db->rptr == db->wptr); /* we can not get empty db as
  1237. a result of write */
  1238. }
  1239. /**
  1240. * bdx_tx_db_init - creates and initializes tx db
  1241. * @d: tx data base
  1242. * @sz_type: size of tx fifo
  1243. *
  1244. * Returns 0 on success, error code otherwise
  1245. */
  1246. static int bdx_tx_db_init(struct txdb *d, int sz_type)
  1247. {
  1248. int memsz = FIFO_SIZE * (1 << (sz_type + 1));
  1249. d->start = vmalloc(memsz);
  1250. if (!d->start)
  1251. return -ENOMEM;
  1252. /*
  1253. * In order to differentiate between db is empty and db is full
  1254. * states at least one element should always be empty in order to
  1255. * avoid rptr == wptr which means db is empty
  1256. */
  1257. d->size = memsz / sizeof(struct tx_map) - 1;
  1258. d->end = d->start + d->size + 1; /* just after last element */
  1259. /* all dbs are created equally empty */
  1260. d->rptr = d->start;
  1261. d->wptr = d->start;
  1262. return 0;
  1263. }
  1264. /**
  1265. * bdx_tx_db_close - closes tx db and frees all memory
  1266. * @d: tx data base
  1267. */
  1268. static void bdx_tx_db_close(struct txdb *d)
  1269. {
  1270. BDX_ASSERT(d == NULL);
  1271. vfree(d->start);
  1272. d->start = NULL;
  1273. }
  1274. /*************************************************************************
  1275. * Tx Engine *
  1276. *************************************************************************/
  1277. /* sizes of tx desc (including padding if needed) as function
  1278. * of skb's frag number */
  1279. static struct {
  1280. u16 bytes;
  1281. u16 qwords; /* qword = 64 bit */
  1282. } txd_sizes[MAX_SKB_FRAGS + 1];
  1283. /**
  1284. * bdx_tx_map_skb - creates and stores dma mappings for skb's data blocks
  1285. * @priv: NIC private structure
  1286. * @skb: socket buffer to map
  1287. * @txdd: TX descriptor to use
  1288. *
  1289. * It makes dma mappings for skb's data blocks and writes them to PBL of
  1290. * new tx descriptor. It also stores them in the tx db, so they could be
  1291. * unmaped after data was sent. It is reponsibility of a caller to make
  1292. * sure that there is enough space in the tx db. Last element holds pointer
  1293. * to skb itself and marked with zero length
  1294. */
  1295. static inline void
  1296. bdx_tx_map_skb(struct bdx_priv *priv, struct sk_buff *skb,
  1297. struct txd_desc *txdd)
  1298. {
  1299. struct txdb *db = &priv->txdb;
  1300. struct pbl *pbl = &txdd->pbl[0];
  1301. int nr_frags = skb_shinfo(skb)->nr_frags;
  1302. int i;
  1303. db->wptr->len = skb_headlen(skb);
  1304. db->wptr->addr.dma = pci_map_single(priv->pdev, skb->data,
  1305. db->wptr->len, PCI_DMA_TODEVICE);
  1306. pbl->len = CPU_CHIP_SWAP32(db->wptr->len);
  1307. pbl->pa_lo = CPU_CHIP_SWAP32(L32_64(db->wptr->addr.dma));
  1308. pbl->pa_hi = CPU_CHIP_SWAP32(H32_64(db->wptr->addr.dma));
  1309. DBG("=== pbl len: 0x%x ================\n", pbl->len);
  1310. DBG("=== pbl pa_lo: 0x%x ================\n", pbl->pa_lo);
  1311. DBG("=== pbl pa_hi: 0x%x ================\n", pbl->pa_hi);
  1312. bdx_tx_db_inc_wptr(db);
  1313. for (i = 0; i < nr_frags; i++) {
  1314. const struct skb_frag_struct *frag;
  1315. frag = &skb_shinfo(skb)->frags[i];
  1316. db->wptr->len = skb_frag_size(frag);
  1317. db->wptr->addr.dma = skb_frag_dma_map(&priv->pdev->dev, frag,
  1318. 0, skb_frag_size(frag),
  1319. DMA_TO_DEVICE);
  1320. pbl++;
  1321. pbl->len = CPU_CHIP_SWAP32(db->wptr->len);
  1322. pbl->pa_lo = CPU_CHIP_SWAP32(L32_64(db->wptr->addr.dma));
  1323. pbl->pa_hi = CPU_CHIP_SWAP32(H32_64(db->wptr->addr.dma));
  1324. bdx_tx_db_inc_wptr(db);
  1325. }
  1326. /* add skb clean up info. */
  1327. db->wptr->len = -txd_sizes[nr_frags].bytes;
  1328. db->wptr->addr.skb = skb;
  1329. bdx_tx_db_inc_wptr(db);
  1330. }
  1331. /* init_txd_sizes - precalculate sizes of descriptors for skbs up to 16 frags
  1332. * number of frags is used as index to fetch correct descriptors size,
  1333. * instead of calculating it each time */
  1334. static void __init init_txd_sizes(void)
  1335. {
  1336. int i, lwords;
  1337. /* 7 - is number of lwords in txd with one phys buffer
  1338. * 3 - is number of lwords used for every additional phys buffer */
  1339. for (i = 0; i < MAX_SKB_FRAGS + 1; i++) {
  1340. lwords = 7 + (i * 3);
  1341. if (lwords & 1)
  1342. lwords++; /* pad it with 1 lword */
  1343. txd_sizes[i].qwords = lwords >> 1;
  1344. txd_sizes[i].bytes = lwords << 2;
  1345. }
  1346. }
  1347. /* bdx_tx_init - initialize all Tx related stuff.
  1348. * Namely, TXD and TXF fifos, database etc */
  1349. static int bdx_tx_init(struct bdx_priv *priv)
  1350. {
  1351. if (bdx_fifo_init(priv, &priv->txd_fifo0.m, priv->txd_size,
  1352. regTXD_CFG0_0,
  1353. regTXD_CFG1_0, regTXD_RPTR_0, regTXD_WPTR_0))
  1354. goto err_mem;
  1355. if (bdx_fifo_init(priv, &priv->txf_fifo0.m, priv->txf_size,
  1356. regTXF_CFG0_0,
  1357. regTXF_CFG1_0, regTXF_RPTR_0, regTXF_WPTR_0))
  1358. goto err_mem;
  1359. /* The TX db has to keep mappings for all packets sent (on TxD)
  1360. * and not yet reclaimed (on TxF) */
  1361. if (bdx_tx_db_init(&priv->txdb, max(priv->txd_size, priv->txf_size)))
  1362. goto err_mem;
  1363. priv->tx_level = BDX_MAX_TX_LEVEL;
  1364. #ifdef BDX_DELAY_WPTR
  1365. priv->tx_update_mark = priv->tx_level - 1024;
  1366. #endif
  1367. return 0;
  1368. err_mem:
  1369. netdev_err(priv->ndev, "Tx init failed\n");
  1370. return -ENOMEM;
  1371. }
  1372. /**
  1373. * bdx_tx_space - calculates available space in TX fifo
  1374. * @priv: NIC private structure
  1375. *
  1376. * Returns available space in TX fifo in bytes
  1377. */
  1378. static inline int bdx_tx_space(struct bdx_priv *priv)
  1379. {
  1380. struct txd_fifo *f = &priv->txd_fifo0;
  1381. int fsize;
  1382. f->m.rptr = READ_REG(priv, f->m.reg_RPTR) & TXF_WPTR_WR_PTR;
  1383. fsize = f->m.rptr - f->m.wptr;
  1384. if (fsize <= 0)
  1385. fsize = f->m.memsz + fsize;
  1386. return fsize;
  1387. }
  1388. /**
  1389. * bdx_tx_transmit - send packet to NIC
  1390. * @skb: packet to send
  1391. * @ndev: network device assigned to NIC
  1392. * Return codes:
  1393. * o NETDEV_TX_OK everything ok.
  1394. * o NETDEV_TX_BUSY Cannot transmit packet, try later
  1395. * Usually a bug, means queue start/stop flow control is broken in
  1396. * the driver. Note: the driver must NOT put the skb in its DMA ring.
  1397. */
  1398. static netdev_tx_t bdx_tx_transmit(struct sk_buff *skb,
  1399. struct net_device *ndev)
  1400. {
  1401. struct bdx_priv *priv = netdev_priv(ndev);
  1402. struct txd_fifo *f = &priv->txd_fifo0;
  1403. int txd_checksum = 7; /* full checksum */
  1404. int txd_lgsnd = 0;
  1405. int txd_vlan_id = 0;
  1406. int txd_vtag = 0;
  1407. int txd_mss = 0;
  1408. int nr_frags = skb_shinfo(skb)->nr_frags;
  1409. struct txd_desc *txdd;
  1410. int len;
  1411. unsigned long flags;
  1412. ENTER;
  1413. local_irq_save(flags);
  1414. spin_lock(&priv->tx_lock);
  1415. /* build tx descriptor */
  1416. BDX_ASSERT(f->m.wptr >= f->m.memsz); /* started with valid wptr */
  1417. txdd = (struct txd_desc *)(f->m.va + f->m.wptr);
  1418. if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL))
  1419. txd_checksum = 0;
  1420. if (skb_shinfo(skb)->gso_size) {
  1421. txd_mss = skb_shinfo(skb)->gso_size;
  1422. txd_lgsnd = 1;
  1423. DBG("skb %p skb len %d gso size = %d\n", skb, skb->len,
  1424. txd_mss);
  1425. }
  1426. if (skb_vlan_tag_present(skb)) {
  1427. /*Cut VLAN ID to 12 bits */
  1428. txd_vlan_id = skb_vlan_tag_get(skb) & BITS_MASK(12);
  1429. txd_vtag = 1;
  1430. }
  1431. txdd->length = CPU_CHIP_SWAP16(skb->len);
  1432. txdd->mss = CPU_CHIP_SWAP16(txd_mss);
  1433. txdd->txd_val1 =
  1434. CPU_CHIP_SWAP32(TXD_W1_VAL
  1435. (txd_sizes[nr_frags].qwords, txd_checksum, txd_vtag,
  1436. txd_lgsnd, txd_vlan_id));
  1437. DBG("=== TxD desc =====================\n");
  1438. DBG("=== w1: 0x%x ================\n", txdd->txd_val1);
  1439. DBG("=== w2: mss 0x%x len 0x%x\n", txdd->mss, txdd->length);
  1440. bdx_tx_map_skb(priv, skb, txdd);
  1441. /* increment TXD write pointer. In case of
  1442. fifo wrapping copy reminder of the descriptor
  1443. to the beginning */
  1444. f->m.wptr += txd_sizes[nr_frags].bytes;
  1445. len = f->m.wptr - f->m.memsz;
  1446. if (unlikely(len >= 0)) {
  1447. f->m.wptr = len;
  1448. if (len > 0) {
  1449. BDX_ASSERT(len > f->m.memsz);
  1450. memcpy(f->m.va, f->m.va + f->m.memsz, len);
  1451. }
  1452. }
  1453. BDX_ASSERT(f->m.wptr >= f->m.memsz); /* finished with valid wptr */
  1454. priv->tx_level -= txd_sizes[nr_frags].bytes;
  1455. BDX_ASSERT(priv->tx_level <= 0 || priv->tx_level > BDX_MAX_TX_LEVEL);
  1456. #ifdef BDX_DELAY_WPTR
  1457. if (priv->tx_level > priv->tx_update_mark) {
  1458. /* Force memory writes to complete before letting h/w
  1459. know there are new descriptors to fetch.
  1460. (might be needed on platforms like IA64)
  1461. wmb(); */
  1462. WRITE_REG(priv, f->m.reg_WPTR, f->m.wptr & TXF_WPTR_WR_PTR);
  1463. } else {
  1464. if (priv->tx_noupd++ > BDX_NO_UPD_PACKETS) {
  1465. priv->tx_noupd = 0;
  1466. WRITE_REG(priv, f->m.reg_WPTR,
  1467. f->m.wptr & TXF_WPTR_WR_PTR);
  1468. }
  1469. }
  1470. #else
  1471. /* Force memory writes to complete before letting h/w
  1472. know there are new descriptors to fetch.
  1473. (might be needed on platforms like IA64)
  1474. wmb(); */
  1475. WRITE_REG(priv, f->m.reg_WPTR, f->m.wptr & TXF_WPTR_WR_PTR);
  1476. #endif
  1477. #ifdef BDX_LLTX
  1478. netif_trans_update(ndev); /* NETIF_F_LLTX driver :( */
  1479. #endif
  1480. ndev->stats.tx_packets++;
  1481. ndev->stats.tx_bytes += skb->len;
  1482. if (priv->tx_level < BDX_MIN_TX_LEVEL) {
  1483. DBG("%s: %s: TX Q STOP level %d\n",
  1484. BDX_DRV_NAME, ndev->name, priv->tx_level);
  1485. netif_stop_queue(ndev);
  1486. }
  1487. spin_unlock_irqrestore(&priv->tx_lock, flags);
  1488. return NETDEV_TX_OK;
  1489. }
  1490. /**
  1491. * bdx_tx_cleanup - clean TXF fifo, run in the context of IRQ.
  1492. * @priv: bdx adapter
  1493. *
  1494. * It scans TXF fifo for descriptors, frees DMA mappings and reports to OS
  1495. * that those packets were sent
  1496. */
  1497. static void bdx_tx_cleanup(struct bdx_priv *priv)
  1498. {
  1499. struct txf_fifo *f = &priv->txf_fifo0;
  1500. struct txdb *db = &priv->txdb;
  1501. int tx_level = 0;
  1502. ENTER;
  1503. f->m.wptr = READ_REG(priv, f->m.reg_WPTR) & TXF_WPTR_MASK;
  1504. BDX_ASSERT(f->m.rptr >= f->m.memsz); /* started with valid rptr */
  1505. while (f->m.wptr != f->m.rptr) {
  1506. f->m.rptr += BDX_TXF_DESC_SZ;
  1507. f->m.rptr &= f->m.size_mask;
  1508. /* unmap all the fragments */
  1509. /* first has to come tx_maps containing dma */
  1510. BDX_ASSERT(db->rptr->len == 0);
  1511. do {
  1512. BDX_ASSERT(db->rptr->addr.dma == 0);
  1513. pci_unmap_page(priv->pdev, db->rptr->addr.dma,
  1514. db->rptr->len, PCI_DMA_TODEVICE);
  1515. bdx_tx_db_inc_rptr(db);
  1516. } while (db->rptr->len > 0);
  1517. tx_level -= db->rptr->len; /* '-' koz len is negative */
  1518. /* now should come skb pointer - free it */
  1519. dev_kfree_skb_irq(db->rptr->addr.skb);
  1520. bdx_tx_db_inc_rptr(db);
  1521. }
  1522. /* let h/w know which TXF descriptors were cleaned */
  1523. BDX_ASSERT((f->m.wptr & TXF_WPTR_WR_PTR) >= f->m.memsz);
  1524. WRITE_REG(priv, f->m.reg_RPTR, f->m.rptr & TXF_WPTR_WR_PTR);
  1525. /* We reclaimed resources, so in case the Q is stopped by xmit callback,
  1526. * we resume the transmission and use tx_lock to synchronize with xmit.*/
  1527. spin_lock(&priv->tx_lock);
  1528. priv->tx_level += tx_level;
  1529. BDX_ASSERT(priv->tx_level <= 0 || priv->tx_level > BDX_MAX_TX_LEVEL);
  1530. #ifdef BDX_DELAY_WPTR
  1531. if (priv->tx_noupd) {
  1532. priv->tx_noupd = 0;
  1533. WRITE_REG(priv, priv->txd_fifo0.m.reg_WPTR,
  1534. priv->txd_fifo0.m.wptr & TXF_WPTR_WR_PTR);
  1535. }
  1536. #endif
  1537. if (unlikely(netif_queue_stopped(priv->ndev) &&
  1538. netif_carrier_ok(priv->ndev) &&
  1539. (priv->tx_level >= BDX_MIN_TX_LEVEL))) {
  1540. DBG("%s: %s: TX Q WAKE level %d\n",
  1541. BDX_DRV_NAME, priv->ndev->name, priv->tx_level);
  1542. netif_wake_queue(priv->ndev);
  1543. }
  1544. spin_unlock(&priv->tx_lock);
  1545. }
  1546. /**
  1547. * bdx_tx_free_skbs - frees all skbs from TXD fifo.
  1548. * It gets called when OS stops this dev, eg upon "ifconfig down" or rmmod
  1549. */
  1550. static void bdx_tx_free_skbs(struct bdx_priv *priv)
  1551. {
  1552. struct txdb *db = &priv->txdb;
  1553. ENTER;
  1554. while (db->rptr != db->wptr) {
  1555. if (likely(db->rptr->len))
  1556. pci_unmap_page(priv->pdev, db->rptr->addr.dma,
  1557. db->rptr->len, PCI_DMA_TODEVICE);
  1558. else
  1559. dev_kfree_skb(db->rptr->addr.skb);
  1560. bdx_tx_db_inc_rptr(db);
  1561. }
  1562. RET();
  1563. }
  1564. /* bdx_tx_free - frees all Tx resources */
  1565. static void bdx_tx_free(struct bdx_priv *priv)
  1566. {
  1567. ENTER;
  1568. bdx_tx_free_skbs(priv);
  1569. bdx_fifo_free(priv, &priv->txd_fifo0.m);
  1570. bdx_fifo_free(priv, &priv->txf_fifo0.m);
  1571. bdx_tx_db_close(&priv->txdb);
  1572. }
  1573. /**
  1574. * bdx_tx_push_desc - push descriptor to TxD fifo
  1575. * @priv: NIC private structure
  1576. * @data: desc's data
  1577. * @size: desc's size
  1578. *
  1579. * Pushes desc to TxD fifo and overlaps it if needed.
  1580. * NOTE: this func does not check for available space. this is responsibility
  1581. * of the caller. Neither does it check that data size is smaller than
  1582. * fifo size.
  1583. */
  1584. static void bdx_tx_push_desc(struct bdx_priv *priv, void *data, int size)
  1585. {
  1586. struct txd_fifo *f = &priv->txd_fifo0;
  1587. int i = f->m.memsz - f->m.wptr;
  1588. if (size == 0)
  1589. return;
  1590. if (i > size) {
  1591. memcpy(f->m.va + f->m.wptr, data, size);
  1592. f->m.wptr += size;
  1593. } else {
  1594. memcpy(f->m.va + f->m.wptr, data, i);
  1595. f->m.wptr = size - i;
  1596. memcpy(f->m.va, data + i, f->m.wptr);
  1597. }
  1598. WRITE_REG(priv, f->m.reg_WPTR, f->m.wptr & TXF_WPTR_WR_PTR);
  1599. }
  1600. /**
  1601. * bdx_tx_push_desc_safe - push descriptor to TxD fifo in a safe way
  1602. * @priv: NIC private structure
  1603. * @data: desc's data
  1604. * @size: desc's size
  1605. *
  1606. * NOTE: this func does check for available space and, if necessary, waits for
  1607. * NIC to read existing data before writing new one.
  1608. */
  1609. static void bdx_tx_push_desc_safe(struct bdx_priv *priv, void *data, int size)
  1610. {
  1611. int timer = 0;
  1612. ENTER;
  1613. while (size > 0) {
  1614. /* we substruct 8 because when fifo is full rptr == wptr
  1615. which also means that fifo is empty, we can understand
  1616. the difference, but could hw do the same ??? :) */
  1617. int avail = bdx_tx_space(priv) - 8;
  1618. if (avail <= 0) {
  1619. if (timer++ > 300) { /* prevent endless loop */
  1620. DBG("timeout while writing desc to TxD fifo\n");
  1621. break;
  1622. }
  1623. udelay(50); /* give hw a chance to clean fifo */
  1624. continue;
  1625. }
  1626. avail = min(avail, size);
  1627. DBG("about to push %d bytes starting %p size %d\n", avail,
  1628. data, size);
  1629. bdx_tx_push_desc(priv, data, avail);
  1630. size -= avail;
  1631. data += avail;
  1632. }
  1633. RET();
  1634. }
  1635. static const struct net_device_ops bdx_netdev_ops = {
  1636. .ndo_open = bdx_open,
  1637. .ndo_stop = bdx_close,
  1638. .ndo_start_xmit = bdx_tx_transmit,
  1639. .ndo_validate_addr = eth_validate_addr,
  1640. .ndo_do_ioctl = bdx_ioctl,
  1641. .ndo_set_rx_mode = bdx_setmulti,
  1642. .ndo_change_mtu = bdx_change_mtu,
  1643. .ndo_set_mac_address = bdx_set_mac,
  1644. .ndo_vlan_rx_add_vid = bdx_vlan_rx_add_vid,
  1645. .ndo_vlan_rx_kill_vid = bdx_vlan_rx_kill_vid,
  1646. };
  1647. /**
  1648. * bdx_probe - Device Initialization Routine
  1649. * @pdev: PCI device information struct
  1650. * @ent: entry in bdx_pci_tbl
  1651. *
  1652. * Returns 0 on success, negative on failure
  1653. *
  1654. * bdx_probe initializes an adapter identified by a pci_dev structure.
  1655. * The OS initialization, configuring of the adapter private structure,
  1656. * and a hardware reset occur.
  1657. *
  1658. * functions and their order used as explained in
  1659. * /usr/src/linux/Documentation/DMA-{API,mapping}.txt
  1660. *
  1661. */
  1662. /* TBD: netif_msg should be checked and implemented. I disable it for now */
  1663. static int
  1664. bdx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1665. {
  1666. struct net_device *ndev;
  1667. struct bdx_priv *priv;
  1668. int err, pci_using_dac, port;
  1669. unsigned long pciaddr;
  1670. u32 regionSize;
  1671. struct pci_nic *nic;
  1672. ENTER;
  1673. nic = vmalloc(sizeof(*nic));
  1674. if (!nic)
  1675. RET(-ENOMEM);
  1676. /************** pci *****************/
  1677. err = pci_enable_device(pdev);
  1678. if (err) /* it triggers interrupt, dunno why. */
  1679. goto err_pci; /* it's not a problem though */
  1680. if (!(err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) &&
  1681. !(err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))) {
  1682. pci_using_dac = 1;
  1683. } else {
  1684. if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) ||
  1685. (err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))) {
  1686. pr_err("No usable DMA configuration, aborting\n");
  1687. goto err_dma;
  1688. }
  1689. pci_using_dac = 0;
  1690. }
  1691. err = pci_request_regions(pdev, BDX_DRV_NAME);
  1692. if (err)
  1693. goto err_dma;
  1694. pci_set_master(pdev);
  1695. pciaddr = pci_resource_start(pdev, 0);
  1696. if (!pciaddr) {
  1697. err = -EIO;
  1698. pr_err("no MMIO resource\n");
  1699. goto err_out_res;
  1700. }
  1701. regionSize = pci_resource_len(pdev, 0);
  1702. if (regionSize < BDX_REGS_SIZE) {
  1703. err = -EIO;
  1704. pr_err("MMIO resource (%x) too small\n", regionSize);
  1705. goto err_out_res;
  1706. }
  1707. nic->regs = ioremap(pciaddr, regionSize);
  1708. if (!nic->regs) {
  1709. err = -EIO;
  1710. pr_err("ioremap failed\n");
  1711. goto err_out_res;
  1712. }
  1713. if (pdev->irq < 2) {
  1714. err = -EIO;
  1715. pr_err("invalid irq (%d)\n", pdev->irq);
  1716. goto err_out_iomap;
  1717. }
  1718. pci_set_drvdata(pdev, nic);
  1719. if (pdev->device == 0x3014)
  1720. nic->port_num = 2;
  1721. else
  1722. nic->port_num = 1;
  1723. print_hw_id(pdev);
  1724. bdx_hw_reset_direct(nic->regs);
  1725. nic->irq_type = IRQ_INTX;
  1726. #ifdef BDX_MSI
  1727. if ((readl(nic->regs + FPGA_VER) & 0xFFF) >= 378) {
  1728. err = pci_enable_msi(pdev);
  1729. if (err)
  1730. pr_err("Can't enable msi. error is %d\n", err);
  1731. else
  1732. nic->irq_type = IRQ_MSI;
  1733. } else
  1734. DBG("HW does not support MSI\n");
  1735. #endif
  1736. /************** netdev **************/
  1737. for (port = 0; port < nic->port_num; port++) {
  1738. ndev = alloc_etherdev(sizeof(struct bdx_priv));
  1739. if (!ndev) {
  1740. err = -ENOMEM;
  1741. goto err_out_iomap;
  1742. }
  1743. ndev->netdev_ops = &bdx_netdev_ops;
  1744. ndev->tx_queue_len = BDX_NDEV_TXQ_LEN;
  1745. bdx_set_ethtool_ops(ndev); /* ethtool interface */
  1746. /* these fields are used for info purposes only
  1747. * so we can have them same for all ports of the board */
  1748. ndev->if_port = port;
  1749. ndev->features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_TSO
  1750. | NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
  1751. NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXCSUM
  1752. ;
  1753. ndev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
  1754. NETIF_F_TSO | NETIF_F_HW_VLAN_CTAG_TX;
  1755. if (pci_using_dac)
  1756. ndev->features |= NETIF_F_HIGHDMA;
  1757. /************** priv ****************/
  1758. priv = nic->priv[port] = netdev_priv(ndev);
  1759. priv->pBdxRegs = nic->regs + port * 0x8000;
  1760. priv->port = port;
  1761. priv->pdev = pdev;
  1762. priv->ndev = ndev;
  1763. priv->nic = nic;
  1764. priv->msg_enable = BDX_DEF_MSG_ENABLE;
  1765. netif_napi_add(ndev, &priv->napi, bdx_poll, 64);
  1766. if ((readl(nic->regs + FPGA_VER) & 0xFFF) == 308) {
  1767. DBG("HW statistics not supported\n");
  1768. priv->stats_flag = 0;
  1769. } else {
  1770. priv->stats_flag = 1;
  1771. }
  1772. /* Initialize fifo sizes. */
  1773. priv->txd_size = 2;
  1774. priv->txf_size = 2;
  1775. priv->rxd_size = 2;
  1776. priv->rxf_size = 3;
  1777. /* Initialize the initial coalescing registers. */
  1778. priv->rdintcm = INT_REG_VAL(0x20, 1, 4, 12);
  1779. priv->tdintcm = INT_REG_VAL(0x20, 1, 0, 12);
  1780. /* ndev->xmit_lock spinlock is not used.
  1781. * Private priv->tx_lock is used for synchronization
  1782. * between transmit and TX irq cleanup. In addition
  1783. * set multicast list callback has to use priv->tx_lock.
  1784. */
  1785. #ifdef BDX_LLTX
  1786. ndev->features |= NETIF_F_LLTX;
  1787. #endif
  1788. spin_lock_init(&priv->tx_lock);
  1789. /*bdx_hw_reset(priv); */
  1790. if (bdx_read_mac(priv)) {
  1791. pr_err("load MAC address failed\n");
  1792. goto err_out_iomap;
  1793. }
  1794. SET_NETDEV_DEV(ndev, &pdev->dev);
  1795. err = register_netdev(ndev);
  1796. if (err) {
  1797. pr_err("register_netdev failed\n");
  1798. goto err_out_free;
  1799. }
  1800. netif_carrier_off(ndev);
  1801. netif_stop_queue(ndev);
  1802. print_eth_id(ndev);
  1803. }
  1804. RET(0);
  1805. err_out_free:
  1806. free_netdev(ndev);
  1807. err_out_iomap:
  1808. iounmap(nic->regs);
  1809. err_out_res:
  1810. pci_release_regions(pdev);
  1811. err_dma:
  1812. pci_disable_device(pdev);
  1813. err_pci:
  1814. vfree(nic);
  1815. RET(err);
  1816. }
  1817. /****************** Ethtool interface *********************/
  1818. /* get strings for statistics counters */
  1819. static const char
  1820. bdx_stat_names[][ETH_GSTRING_LEN] = {
  1821. "InUCast", /* 0x7200 */
  1822. "InMCast", /* 0x7210 */
  1823. "InBCast", /* 0x7220 */
  1824. "InPkts", /* 0x7230 */
  1825. "InErrors", /* 0x7240 */
  1826. "InDropped", /* 0x7250 */
  1827. "FrameTooLong", /* 0x7260 */
  1828. "FrameSequenceErrors", /* 0x7270 */
  1829. "InVLAN", /* 0x7280 */
  1830. "InDroppedDFE", /* 0x7290 */
  1831. "InDroppedIntFull", /* 0x72A0 */
  1832. "InFrameAlignErrors", /* 0x72B0 */
  1833. /* 0x72C0-0x72E0 RSRV */
  1834. "OutUCast", /* 0x72F0 */
  1835. "OutMCast", /* 0x7300 */
  1836. "OutBCast", /* 0x7310 */
  1837. "OutPkts", /* 0x7320 */
  1838. /* 0x7330-0x7360 RSRV */
  1839. "OutVLAN", /* 0x7370 */
  1840. "InUCastOctects", /* 0x7380 */
  1841. "OutUCastOctects", /* 0x7390 */
  1842. /* 0x73A0-0x73B0 RSRV */
  1843. "InBCastOctects", /* 0x73C0 */
  1844. "OutBCastOctects", /* 0x73D0 */
  1845. "InOctects", /* 0x73E0 */
  1846. "OutOctects", /* 0x73F0 */
  1847. };
  1848. /*
  1849. * bdx_get_settings - get device-specific settings
  1850. * @netdev
  1851. * @ecmd
  1852. */
  1853. static int bdx_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
  1854. {
  1855. u32 rdintcm;
  1856. u32 tdintcm;
  1857. struct bdx_priv *priv = netdev_priv(netdev);
  1858. rdintcm = priv->rdintcm;
  1859. tdintcm = priv->tdintcm;
  1860. ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
  1861. ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
  1862. ethtool_cmd_speed_set(ecmd, SPEED_10000);
  1863. ecmd->duplex = DUPLEX_FULL;
  1864. ecmd->port = PORT_FIBRE;
  1865. ecmd->transceiver = XCVR_EXTERNAL; /* what does it mean? */
  1866. ecmd->autoneg = AUTONEG_DISABLE;
  1867. /* PCK_TH measures in multiples of FIFO bytes
  1868. We translate to packets */
  1869. ecmd->maxtxpkt =
  1870. ((GET_PCK_TH(tdintcm) * PCK_TH_MULT) / BDX_TXF_DESC_SZ);
  1871. ecmd->maxrxpkt =
  1872. ((GET_PCK_TH(rdintcm) * PCK_TH_MULT) / sizeof(struct rxf_desc));
  1873. return 0;
  1874. }
  1875. /*
  1876. * bdx_get_drvinfo - report driver information
  1877. * @netdev
  1878. * @drvinfo
  1879. */
  1880. static void
  1881. bdx_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
  1882. {
  1883. struct bdx_priv *priv = netdev_priv(netdev);
  1884. strlcpy(drvinfo->driver, BDX_DRV_NAME, sizeof(drvinfo->driver));
  1885. strlcpy(drvinfo->version, BDX_DRV_VERSION, sizeof(drvinfo->version));
  1886. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  1887. strlcpy(drvinfo->bus_info, pci_name(priv->pdev),
  1888. sizeof(drvinfo->bus_info));
  1889. }
  1890. /*
  1891. * bdx_get_coalesce - get interrupt coalescing parameters
  1892. * @netdev
  1893. * @ecoal
  1894. */
  1895. static int
  1896. bdx_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ecoal)
  1897. {
  1898. u32 rdintcm;
  1899. u32 tdintcm;
  1900. struct bdx_priv *priv = netdev_priv(netdev);
  1901. rdintcm = priv->rdintcm;
  1902. tdintcm = priv->tdintcm;
  1903. /* PCK_TH measures in multiples of FIFO bytes
  1904. We translate to packets */
  1905. ecoal->rx_coalesce_usecs = GET_INT_COAL(rdintcm) * INT_COAL_MULT;
  1906. ecoal->rx_max_coalesced_frames =
  1907. ((GET_PCK_TH(rdintcm) * PCK_TH_MULT) / sizeof(struct rxf_desc));
  1908. ecoal->tx_coalesce_usecs = GET_INT_COAL(tdintcm) * INT_COAL_MULT;
  1909. ecoal->tx_max_coalesced_frames =
  1910. ((GET_PCK_TH(tdintcm) * PCK_TH_MULT) / BDX_TXF_DESC_SZ);
  1911. /* adaptive parameters ignored */
  1912. return 0;
  1913. }
  1914. /*
  1915. * bdx_set_coalesce - set interrupt coalescing parameters
  1916. * @netdev
  1917. * @ecoal
  1918. */
  1919. static int
  1920. bdx_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ecoal)
  1921. {
  1922. u32 rdintcm;
  1923. u32 tdintcm;
  1924. struct bdx_priv *priv = netdev_priv(netdev);
  1925. int rx_coal;
  1926. int tx_coal;
  1927. int rx_max_coal;
  1928. int tx_max_coal;
  1929. /* Check for valid input */
  1930. rx_coal = ecoal->rx_coalesce_usecs / INT_COAL_MULT;
  1931. tx_coal = ecoal->tx_coalesce_usecs / INT_COAL_MULT;
  1932. rx_max_coal = ecoal->rx_max_coalesced_frames;
  1933. tx_max_coal = ecoal->tx_max_coalesced_frames;
  1934. /* Translate from packets to multiples of FIFO bytes */
  1935. rx_max_coal =
  1936. (((rx_max_coal * sizeof(struct rxf_desc)) + PCK_TH_MULT - 1)
  1937. / PCK_TH_MULT);
  1938. tx_max_coal =
  1939. (((tx_max_coal * BDX_TXF_DESC_SZ) + PCK_TH_MULT - 1)
  1940. / PCK_TH_MULT);
  1941. if ((rx_coal > 0x7FFF) || (tx_coal > 0x7FFF) ||
  1942. (rx_max_coal > 0xF) || (tx_max_coal > 0xF))
  1943. return -EINVAL;
  1944. rdintcm = INT_REG_VAL(rx_coal, GET_INT_COAL_RC(priv->rdintcm),
  1945. GET_RXF_TH(priv->rdintcm), rx_max_coal);
  1946. tdintcm = INT_REG_VAL(tx_coal, GET_INT_COAL_RC(priv->tdintcm), 0,
  1947. tx_max_coal);
  1948. priv->rdintcm = rdintcm;
  1949. priv->tdintcm = tdintcm;
  1950. WRITE_REG(priv, regRDINTCM0, rdintcm);
  1951. WRITE_REG(priv, regTDINTCM0, tdintcm);
  1952. return 0;
  1953. }
  1954. /* Convert RX fifo size to number of pending packets */
  1955. static inline int bdx_rx_fifo_size_to_packets(int rx_size)
  1956. {
  1957. return (FIFO_SIZE * (1 << rx_size)) / sizeof(struct rxf_desc);
  1958. }
  1959. /* Convert TX fifo size to number of pending packets */
  1960. static inline int bdx_tx_fifo_size_to_packets(int tx_size)
  1961. {
  1962. return (FIFO_SIZE * (1 << tx_size)) / BDX_TXF_DESC_SZ;
  1963. }
  1964. /*
  1965. * bdx_get_ringparam - report ring sizes
  1966. * @netdev
  1967. * @ring
  1968. */
  1969. static void
  1970. bdx_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
  1971. {
  1972. struct bdx_priv *priv = netdev_priv(netdev);
  1973. /*max_pending - the maximum-sized FIFO we allow */
  1974. ring->rx_max_pending = bdx_rx_fifo_size_to_packets(3);
  1975. ring->tx_max_pending = bdx_tx_fifo_size_to_packets(3);
  1976. ring->rx_pending = bdx_rx_fifo_size_to_packets(priv->rxf_size);
  1977. ring->tx_pending = bdx_tx_fifo_size_to_packets(priv->txd_size);
  1978. }
  1979. /*
  1980. * bdx_set_ringparam - set ring sizes
  1981. * @netdev
  1982. * @ring
  1983. */
  1984. static int
  1985. bdx_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
  1986. {
  1987. struct bdx_priv *priv = netdev_priv(netdev);
  1988. int rx_size = 0;
  1989. int tx_size = 0;
  1990. for (; rx_size < 4; rx_size++) {
  1991. if (bdx_rx_fifo_size_to_packets(rx_size) >= ring->rx_pending)
  1992. break;
  1993. }
  1994. if (rx_size == 4)
  1995. rx_size = 3;
  1996. for (; tx_size < 4; tx_size++) {
  1997. if (bdx_tx_fifo_size_to_packets(tx_size) >= ring->tx_pending)
  1998. break;
  1999. }
  2000. if (tx_size == 4)
  2001. tx_size = 3;
  2002. /*Is there anything to do? */
  2003. if ((rx_size == priv->rxf_size) &&
  2004. (tx_size == priv->txd_size))
  2005. return 0;
  2006. priv->rxf_size = rx_size;
  2007. if (rx_size > 1)
  2008. priv->rxd_size = rx_size - 1;
  2009. else
  2010. priv->rxd_size = rx_size;
  2011. priv->txf_size = priv->txd_size = tx_size;
  2012. if (netif_running(netdev)) {
  2013. bdx_close(netdev);
  2014. bdx_open(netdev);
  2015. }
  2016. return 0;
  2017. }
  2018. /*
  2019. * bdx_get_strings - return a set of strings that describe the requested objects
  2020. * @netdev
  2021. * @data
  2022. */
  2023. static void bdx_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  2024. {
  2025. switch (stringset) {
  2026. case ETH_SS_STATS:
  2027. memcpy(data, *bdx_stat_names, sizeof(bdx_stat_names));
  2028. break;
  2029. }
  2030. }
  2031. /*
  2032. * bdx_get_sset_count - return number of statistics or tests
  2033. * @netdev
  2034. */
  2035. static int bdx_get_sset_count(struct net_device *netdev, int stringset)
  2036. {
  2037. struct bdx_priv *priv = netdev_priv(netdev);
  2038. switch (stringset) {
  2039. case ETH_SS_STATS:
  2040. BDX_ASSERT(ARRAY_SIZE(bdx_stat_names)
  2041. != sizeof(struct bdx_stats) / sizeof(u64));
  2042. return (priv->stats_flag) ? ARRAY_SIZE(bdx_stat_names) : 0;
  2043. }
  2044. return -EINVAL;
  2045. }
  2046. /*
  2047. * bdx_get_ethtool_stats - return device's hardware L2 statistics
  2048. * @netdev
  2049. * @stats
  2050. * @data
  2051. */
  2052. static void bdx_get_ethtool_stats(struct net_device *netdev,
  2053. struct ethtool_stats *stats, u64 *data)
  2054. {
  2055. struct bdx_priv *priv = netdev_priv(netdev);
  2056. if (priv->stats_flag) {
  2057. /* Update stats from HW */
  2058. bdx_update_stats(priv);
  2059. /* Copy data to user buffer */
  2060. memcpy(data, &priv->hw_stats, sizeof(priv->hw_stats));
  2061. }
  2062. }
  2063. /*
  2064. * bdx_set_ethtool_ops - ethtool interface implementation
  2065. * @netdev
  2066. */
  2067. static void bdx_set_ethtool_ops(struct net_device *netdev)
  2068. {
  2069. static const struct ethtool_ops bdx_ethtool_ops = {
  2070. .get_settings = bdx_get_settings,
  2071. .get_drvinfo = bdx_get_drvinfo,
  2072. .get_link = ethtool_op_get_link,
  2073. .get_coalesce = bdx_get_coalesce,
  2074. .set_coalesce = bdx_set_coalesce,
  2075. .get_ringparam = bdx_get_ringparam,
  2076. .set_ringparam = bdx_set_ringparam,
  2077. .get_strings = bdx_get_strings,
  2078. .get_sset_count = bdx_get_sset_count,
  2079. .get_ethtool_stats = bdx_get_ethtool_stats,
  2080. };
  2081. netdev->ethtool_ops = &bdx_ethtool_ops;
  2082. }
  2083. /**
  2084. * bdx_remove - Device Removal Routine
  2085. * @pdev: PCI device information struct
  2086. *
  2087. * bdx_remove is called by the PCI subsystem to alert the driver
  2088. * that it should release a PCI device. The could be caused by a
  2089. * Hot-Plug event, or because the driver is going to be removed from
  2090. * memory.
  2091. **/
  2092. static void bdx_remove(struct pci_dev *pdev)
  2093. {
  2094. struct pci_nic *nic = pci_get_drvdata(pdev);
  2095. struct net_device *ndev;
  2096. int port;
  2097. for (port = 0; port < nic->port_num; port++) {
  2098. ndev = nic->priv[port]->ndev;
  2099. unregister_netdev(ndev);
  2100. free_netdev(ndev);
  2101. }
  2102. /*bdx_hw_reset_direct(nic->regs); */
  2103. #ifdef BDX_MSI
  2104. if (nic->irq_type == IRQ_MSI)
  2105. pci_disable_msi(pdev);
  2106. #endif
  2107. iounmap(nic->regs);
  2108. pci_release_regions(pdev);
  2109. pci_disable_device(pdev);
  2110. vfree(nic);
  2111. RET();
  2112. }
  2113. static struct pci_driver bdx_pci_driver = {
  2114. .name = BDX_DRV_NAME,
  2115. .id_table = bdx_pci_tbl,
  2116. .probe = bdx_probe,
  2117. .remove = bdx_remove,
  2118. };
  2119. /*
  2120. * print_driver_id - print parameters of the driver build
  2121. */
  2122. static void __init print_driver_id(void)
  2123. {
  2124. pr_info("%s, %s\n", BDX_DRV_DESC, BDX_DRV_VERSION);
  2125. pr_info("Options: hw_csum %s\n", BDX_MSI_STRING);
  2126. }
  2127. static int __init bdx_module_init(void)
  2128. {
  2129. ENTER;
  2130. init_txd_sizes();
  2131. print_driver_id();
  2132. RET(pci_register_driver(&bdx_pci_driver));
  2133. }
  2134. module_init(bdx_module_init);
  2135. static void __exit bdx_module_exit(void)
  2136. {
  2137. ENTER;
  2138. pci_unregister_driver(&bdx_pci_driver);
  2139. RET();
  2140. }
  2141. module_exit(bdx_module_exit);
  2142. MODULE_LICENSE("GPL");
  2143. MODULE_AUTHOR(DRIVER_AUTHOR);
  2144. MODULE_DESCRIPTION(BDX_DRV_DESC);
  2145. /*(DEBLOBBED)*/