ixgbevf_main.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319
  1. /*******************************************************************************
  2. Intel 82599 Virtual Function driver
  3. Copyright(c) 1999 - 2012 Intel Corporation.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Contact Information:
  17. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. /******************************************************************************
  21. Copyright (c)2006 - 2007 Myricom, Inc. for some LRO specific code
  22. ******************************************************************************/
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/types.h>
  25. #include <linux/bitops.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/string.h>
  31. #include <linux/in.h>
  32. #include <linux/ip.h>
  33. #include <linux/tcp.h>
  34. #include <linux/sctp.h>
  35. #include <linux/ipv6.h>
  36. #include <linux/slab.h>
  37. #include <net/checksum.h>
  38. #include <net/ip6_checksum.h>
  39. #include <linux/ethtool.h>
  40. #include <linux/if.h>
  41. #include <linux/if_vlan.h>
  42. #include <linux/prefetch.h>
  43. #include "ixgbevf.h"
  44. const char ixgbevf_driver_name[] = "ixgbevf";
  45. static const char ixgbevf_driver_string[] =
  46. "Intel(R) 10 Gigabit PCI Express Virtual Function Network Driver";
  47. #define DRV_VERSION "2.6.0-k"
  48. const char ixgbevf_driver_version[] = DRV_VERSION;
  49. static char ixgbevf_copyright[] =
  50. "Copyright (c) 2009 - 2012 Intel Corporation.";
  51. static const struct ixgbevf_info *ixgbevf_info_tbl[] = {
  52. [board_82599_vf] = &ixgbevf_82599_vf_info,
  53. [board_X540_vf] = &ixgbevf_X540_vf_info,
  54. };
  55. /* ixgbevf_pci_tbl - PCI Device ID Table
  56. *
  57. * Wildcard entries (PCI_ANY_ID) should come last
  58. * Last entry must be all 0s
  59. *
  60. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  61. * Class, Class Mask, private data (not used) }
  62. */
  63. static struct pci_device_id ixgbevf_pci_tbl[] = {
  64. {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_82599_VF),
  65. board_82599_vf},
  66. {PCI_VDEVICE(INTEL, IXGBE_DEV_ID_X540_VF),
  67. board_X540_vf},
  68. /* required last entry */
  69. {0, }
  70. };
  71. MODULE_DEVICE_TABLE(pci, ixgbevf_pci_tbl);
  72. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  73. MODULE_DESCRIPTION("Intel(R) 82599 Virtual Function Driver");
  74. MODULE_LICENSE("GPL");
  75. MODULE_VERSION(DRV_VERSION);
  76. #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
  77. static int debug = -1;
  78. module_param(debug, int, 0);
  79. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  80. /* forward decls */
  81. static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector);
  82. static inline void ixgbevf_release_rx_desc(struct ixgbe_hw *hw,
  83. struct ixgbevf_ring *rx_ring,
  84. u32 val)
  85. {
  86. /*
  87. * Force memory writes to complete before letting h/w
  88. * know there are new descriptors to fetch. (Only
  89. * applicable for weak-ordered memory model archs,
  90. * such as IA-64).
  91. */
  92. wmb();
  93. IXGBE_WRITE_REG(hw, IXGBE_VFRDT(rx_ring->reg_idx), val);
  94. }
  95. /**
  96. * ixgbevf_set_ivar - set IVAR registers - maps interrupt causes to vectors
  97. * @adapter: pointer to adapter struct
  98. * @direction: 0 for Rx, 1 for Tx, -1 for other causes
  99. * @queue: queue to map the corresponding interrupt to
  100. * @msix_vector: the vector to map to the corresponding queue
  101. *
  102. */
  103. static void ixgbevf_set_ivar(struct ixgbevf_adapter *adapter, s8 direction,
  104. u8 queue, u8 msix_vector)
  105. {
  106. u32 ivar, index;
  107. struct ixgbe_hw *hw = &adapter->hw;
  108. if (direction == -1) {
  109. /* other causes */
  110. msix_vector |= IXGBE_IVAR_ALLOC_VAL;
  111. ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
  112. ivar &= ~0xFF;
  113. ivar |= msix_vector;
  114. IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, ivar);
  115. } else {
  116. /* tx or rx causes */
  117. msix_vector |= IXGBE_IVAR_ALLOC_VAL;
  118. index = ((16 * (queue & 1)) + (8 * direction));
  119. ivar = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
  120. ivar &= ~(0xFF << index);
  121. ivar |= (msix_vector << index);
  122. IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), ivar);
  123. }
  124. }
  125. static void ixgbevf_unmap_and_free_tx_resource(struct ixgbevf_ring *tx_ring,
  126. struct ixgbevf_tx_buffer
  127. *tx_buffer_info)
  128. {
  129. if (tx_buffer_info->dma) {
  130. if (tx_buffer_info->mapped_as_page)
  131. dma_unmap_page(tx_ring->dev,
  132. tx_buffer_info->dma,
  133. tx_buffer_info->length,
  134. DMA_TO_DEVICE);
  135. else
  136. dma_unmap_single(tx_ring->dev,
  137. tx_buffer_info->dma,
  138. tx_buffer_info->length,
  139. DMA_TO_DEVICE);
  140. tx_buffer_info->dma = 0;
  141. }
  142. if (tx_buffer_info->skb) {
  143. dev_kfree_skb_any(tx_buffer_info->skb);
  144. tx_buffer_info->skb = NULL;
  145. }
  146. tx_buffer_info->time_stamp = 0;
  147. /* tx_buffer_info must be completely set up in the transmit path */
  148. }
  149. #define IXGBE_MAX_TXD_PWR 14
  150. #define IXGBE_MAX_DATA_PER_TXD (1 << IXGBE_MAX_TXD_PWR)
  151. /* Tx Descriptors needed, worst case */
  152. #define TXD_USE_COUNT(S) DIV_ROUND_UP((S), IXGBE_MAX_DATA_PER_TXD)
  153. #define DESC_NEEDED (MAX_SKB_FRAGS + 4)
  154. static void ixgbevf_tx_timeout(struct net_device *netdev);
  155. /**
  156. * ixgbevf_clean_tx_irq - Reclaim resources after transmit completes
  157. * @q_vector: board private structure
  158. * @tx_ring: tx ring to clean
  159. **/
  160. static bool ixgbevf_clean_tx_irq(struct ixgbevf_q_vector *q_vector,
  161. struct ixgbevf_ring *tx_ring)
  162. {
  163. struct ixgbevf_adapter *adapter = q_vector->adapter;
  164. union ixgbe_adv_tx_desc *tx_desc, *eop_desc;
  165. struct ixgbevf_tx_buffer *tx_buffer_info;
  166. unsigned int i, eop, count = 0;
  167. unsigned int total_bytes = 0, total_packets = 0;
  168. if (test_bit(__IXGBEVF_DOWN, &adapter->state))
  169. return true;
  170. i = tx_ring->next_to_clean;
  171. eop = tx_ring->tx_buffer_info[i].next_to_watch;
  172. eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
  173. while ((eop_desc->wb.status & cpu_to_le32(IXGBE_TXD_STAT_DD)) &&
  174. (count < tx_ring->count)) {
  175. bool cleaned = false;
  176. rmb(); /* read buffer_info after eop_desc */
  177. /* eop could change between read and DD-check */
  178. if (unlikely(eop != tx_ring->tx_buffer_info[i].next_to_watch))
  179. goto cont_loop;
  180. for ( ; !cleaned; count++) {
  181. struct sk_buff *skb;
  182. tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
  183. tx_buffer_info = &tx_ring->tx_buffer_info[i];
  184. cleaned = (i == eop);
  185. skb = tx_buffer_info->skb;
  186. if (cleaned && skb) {
  187. unsigned int segs, bytecount;
  188. /* gso_segs is currently only valid for tcp */
  189. segs = skb_shinfo(skb)->gso_segs ?: 1;
  190. /* multiply data chunks by size of headers */
  191. bytecount = ((segs - 1) * skb_headlen(skb)) +
  192. skb->len;
  193. total_packets += segs;
  194. total_bytes += bytecount;
  195. }
  196. ixgbevf_unmap_and_free_tx_resource(tx_ring,
  197. tx_buffer_info);
  198. tx_desc->wb.status = 0;
  199. i++;
  200. if (i == tx_ring->count)
  201. i = 0;
  202. }
  203. cont_loop:
  204. eop = tx_ring->tx_buffer_info[i].next_to_watch;
  205. eop_desc = IXGBEVF_TX_DESC(tx_ring, eop);
  206. }
  207. tx_ring->next_to_clean = i;
  208. #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
  209. if (unlikely(count && netif_carrier_ok(tx_ring->netdev) &&
  210. (IXGBE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  211. /* Make sure that anybody stopping the queue after this
  212. * sees the new next_to_clean.
  213. */
  214. smp_mb();
  215. if (__netif_subqueue_stopped(tx_ring->netdev,
  216. tx_ring->queue_index) &&
  217. !test_bit(__IXGBEVF_DOWN, &adapter->state)) {
  218. netif_wake_subqueue(tx_ring->netdev,
  219. tx_ring->queue_index);
  220. ++adapter->restart_queue;
  221. }
  222. }
  223. u64_stats_update_begin(&tx_ring->syncp);
  224. tx_ring->total_bytes += total_bytes;
  225. tx_ring->total_packets += total_packets;
  226. u64_stats_update_end(&tx_ring->syncp);
  227. return count < tx_ring->count;
  228. }
  229. /**
  230. * ixgbevf_receive_skb - Send a completed packet up the stack
  231. * @q_vector: structure containing interrupt and ring information
  232. * @skb: packet to send up
  233. * @status: hardware indication of status of receive
  234. * @rx_ring: rx descriptor ring (for a specific queue) to setup
  235. * @rx_desc: rx descriptor
  236. **/
  237. static void ixgbevf_receive_skb(struct ixgbevf_q_vector *q_vector,
  238. struct sk_buff *skb, u8 status,
  239. struct ixgbevf_ring *ring,
  240. union ixgbe_adv_rx_desc *rx_desc)
  241. {
  242. struct ixgbevf_adapter *adapter = q_vector->adapter;
  243. bool is_vlan = (status & IXGBE_RXD_STAT_VP);
  244. u16 tag = le16_to_cpu(rx_desc->wb.upper.vlan);
  245. if (is_vlan && test_bit(tag & VLAN_VID_MASK, adapter->active_vlans))
  246. __vlan_hwaccel_put_tag(skb, tag);
  247. napi_gro_receive(&q_vector->napi, skb);
  248. }
  249. /**
  250. * ixgbevf_rx_checksum - indicate in skb if hw indicated a good cksum
  251. * @adapter: address of board private structure
  252. * @status_err: hardware indication of status of receive
  253. * @skb: skb currently being received and modified
  254. **/
  255. static inline void ixgbevf_rx_checksum(struct ixgbevf_adapter *adapter,
  256. struct ixgbevf_ring *ring,
  257. u32 status_err, struct sk_buff *skb)
  258. {
  259. skb_checksum_none_assert(skb);
  260. /* Rx csum disabled */
  261. if (!(ring->netdev->features & NETIF_F_RXCSUM))
  262. return;
  263. /* if IP and error */
  264. if ((status_err & IXGBE_RXD_STAT_IPCS) &&
  265. (status_err & IXGBE_RXDADV_ERR_IPE)) {
  266. adapter->hw_csum_rx_error++;
  267. return;
  268. }
  269. if (!(status_err & IXGBE_RXD_STAT_L4CS))
  270. return;
  271. if (status_err & IXGBE_RXDADV_ERR_TCPE) {
  272. adapter->hw_csum_rx_error++;
  273. return;
  274. }
  275. /* It must be a TCP or UDP packet with a valid checksum */
  276. skb->ip_summed = CHECKSUM_UNNECESSARY;
  277. adapter->hw_csum_rx_good++;
  278. }
  279. /**
  280. * ixgbevf_alloc_rx_buffers - Replace used receive buffers; packet split
  281. * @adapter: address of board private structure
  282. **/
  283. static void ixgbevf_alloc_rx_buffers(struct ixgbevf_adapter *adapter,
  284. struct ixgbevf_ring *rx_ring,
  285. int cleaned_count)
  286. {
  287. struct pci_dev *pdev = adapter->pdev;
  288. union ixgbe_adv_rx_desc *rx_desc;
  289. struct ixgbevf_rx_buffer *bi;
  290. struct sk_buff *skb;
  291. unsigned int i = rx_ring->next_to_use;
  292. bi = &rx_ring->rx_buffer_info[i];
  293. while (cleaned_count--) {
  294. rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
  295. skb = bi->skb;
  296. if (!skb) {
  297. skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
  298. rx_ring->rx_buf_len);
  299. if (!skb) {
  300. adapter->alloc_rx_buff_failed++;
  301. goto no_buffers;
  302. }
  303. bi->skb = skb;
  304. }
  305. if (!bi->dma) {
  306. bi->dma = dma_map_single(&pdev->dev, skb->data,
  307. rx_ring->rx_buf_len,
  308. DMA_FROM_DEVICE);
  309. }
  310. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
  311. i++;
  312. if (i == rx_ring->count)
  313. i = 0;
  314. bi = &rx_ring->rx_buffer_info[i];
  315. }
  316. no_buffers:
  317. if (rx_ring->next_to_use != i) {
  318. rx_ring->next_to_use = i;
  319. ixgbevf_release_rx_desc(&adapter->hw, rx_ring, i);
  320. }
  321. }
  322. static inline void ixgbevf_irq_enable_queues(struct ixgbevf_adapter *adapter,
  323. u32 qmask)
  324. {
  325. struct ixgbe_hw *hw = &adapter->hw;
  326. IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, qmask);
  327. }
  328. static bool ixgbevf_clean_rx_irq(struct ixgbevf_q_vector *q_vector,
  329. struct ixgbevf_ring *rx_ring,
  330. int budget)
  331. {
  332. struct ixgbevf_adapter *adapter = q_vector->adapter;
  333. struct pci_dev *pdev = adapter->pdev;
  334. union ixgbe_adv_rx_desc *rx_desc, *next_rxd;
  335. struct ixgbevf_rx_buffer *rx_buffer_info, *next_buffer;
  336. struct sk_buff *skb;
  337. unsigned int i;
  338. u32 len, staterr;
  339. int cleaned_count = 0;
  340. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  341. i = rx_ring->next_to_clean;
  342. rx_desc = IXGBEVF_RX_DESC(rx_ring, i);
  343. staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
  344. rx_buffer_info = &rx_ring->rx_buffer_info[i];
  345. while (staterr & IXGBE_RXD_STAT_DD) {
  346. if (!budget)
  347. break;
  348. budget--;
  349. rmb(); /* read descriptor and rx_buffer_info after status DD */
  350. len = le16_to_cpu(rx_desc->wb.upper.length);
  351. skb = rx_buffer_info->skb;
  352. prefetch(skb->data - NET_IP_ALIGN);
  353. rx_buffer_info->skb = NULL;
  354. if (rx_buffer_info->dma) {
  355. dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
  356. rx_ring->rx_buf_len,
  357. DMA_FROM_DEVICE);
  358. rx_buffer_info->dma = 0;
  359. skb_put(skb, len);
  360. }
  361. i++;
  362. if (i == rx_ring->count)
  363. i = 0;
  364. next_rxd = IXGBEVF_RX_DESC(rx_ring, i);
  365. prefetch(next_rxd);
  366. cleaned_count++;
  367. next_buffer = &rx_ring->rx_buffer_info[i];
  368. if (!(staterr & IXGBE_RXD_STAT_EOP)) {
  369. skb->next = next_buffer->skb;
  370. skb->next->prev = skb;
  371. adapter->non_eop_descs++;
  372. goto next_desc;
  373. }
  374. /* ERR_MASK will only have valid bits if EOP set */
  375. if (unlikely(staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK)) {
  376. dev_kfree_skb_irq(skb);
  377. goto next_desc;
  378. }
  379. ixgbevf_rx_checksum(adapter, rx_ring, staterr, skb);
  380. /* probably a little skewed due to removing CRC */
  381. total_rx_bytes += skb->len;
  382. total_rx_packets++;
  383. /*
  384. * Work around issue of some types of VM to VM loop back
  385. * packets not getting split correctly
  386. */
  387. if (staterr & IXGBE_RXD_STAT_LB) {
  388. u32 header_fixup_len = skb_headlen(skb);
  389. if (header_fixup_len < 14)
  390. skb_push(skb, header_fixup_len);
  391. }
  392. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  393. ixgbevf_receive_skb(q_vector, skb, staterr, rx_ring, rx_desc);
  394. next_desc:
  395. rx_desc->wb.upper.status_error = 0;
  396. /* return some buffers to hardware, one at a time is too slow */
  397. if (cleaned_count >= IXGBEVF_RX_BUFFER_WRITE) {
  398. ixgbevf_alloc_rx_buffers(adapter, rx_ring,
  399. cleaned_count);
  400. cleaned_count = 0;
  401. }
  402. /* use prefetched values */
  403. rx_desc = next_rxd;
  404. rx_buffer_info = &rx_ring->rx_buffer_info[i];
  405. staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
  406. }
  407. rx_ring->next_to_clean = i;
  408. cleaned_count = IXGBE_DESC_UNUSED(rx_ring);
  409. if (cleaned_count)
  410. ixgbevf_alloc_rx_buffers(adapter, rx_ring, cleaned_count);
  411. u64_stats_update_begin(&rx_ring->syncp);
  412. rx_ring->total_packets += total_rx_packets;
  413. rx_ring->total_bytes += total_rx_bytes;
  414. u64_stats_update_end(&rx_ring->syncp);
  415. return !!budget;
  416. }
  417. /**
  418. * ixgbevf_poll - NAPI polling calback
  419. * @napi: napi struct with our devices info in it
  420. * @budget: amount of work driver is allowed to do this pass, in packets
  421. *
  422. * This function will clean more than one or more rings associated with a
  423. * q_vector.
  424. **/
  425. static int ixgbevf_poll(struct napi_struct *napi, int budget)
  426. {
  427. struct ixgbevf_q_vector *q_vector =
  428. container_of(napi, struct ixgbevf_q_vector, napi);
  429. struct ixgbevf_adapter *adapter = q_vector->adapter;
  430. struct ixgbevf_ring *ring;
  431. int per_ring_budget;
  432. bool clean_complete = true;
  433. ixgbevf_for_each_ring(ring, q_vector->tx)
  434. clean_complete &= ixgbevf_clean_tx_irq(q_vector, ring);
  435. /* attempt to distribute budget to each queue fairly, but don't allow
  436. * the budget to go below 1 because we'll exit polling */
  437. if (q_vector->rx.count > 1)
  438. per_ring_budget = max(budget/q_vector->rx.count, 1);
  439. else
  440. per_ring_budget = budget;
  441. ixgbevf_for_each_ring(ring, q_vector->rx)
  442. clean_complete &= ixgbevf_clean_rx_irq(q_vector, ring,
  443. per_ring_budget);
  444. /* If all work not completed, return budget and keep polling */
  445. if (!clean_complete)
  446. return budget;
  447. /* all work done, exit the polling mode */
  448. napi_complete(napi);
  449. if (adapter->rx_itr_setting & 1)
  450. ixgbevf_set_itr(q_vector);
  451. if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
  452. ixgbevf_irq_enable_queues(adapter,
  453. 1 << q_vector->v_idx);
  454. return 0;
  455. }
  456. /**
  457. * ixgbevf_write_eitr - write VTEITR register in hardware specific way
  458. * @q_vector: structure containing interrupt and ring information
  459. */
  460. static void ixgbevf_write_eitr(struct ixgbevf_q_vector *q_vector)
  461. {
  462. struct ixgbevf_adapter *adapter = q_vector->adapter;
  463. struct ixgbe_hw *hw = &adapter->hw;
  464. int v_idx = q_vector->v_idx;
  465. u32 itr_reg = q_vector->itr & IXGBE_MAX_EITR;
  466. /*
  467. * set the WDIS bit to not clear the timer bits and cause an
  468. * immediate assertion of the interrupt
  469. */
  470. itr_reg |= IXGBE_EITR_CNT_WDIS;
  471. IXGBE_WRITE_REG(hw, IXGBE_VTEITR(v_idx), itr_reg);
  472. }
  473. /**
  474. * ixgbevf_configure_msix - Configure MSI-X hardware
  475. * @adapter: board private structure
  476. *
  477. * ixgbevf_configure_msix sets up the hardware to properly generate MSI-X
  478. * interrupts.
  479. **/
  480. static void ixgbevf_configure_msix(struct ixgbevf_adapter *adapter)
  481. {
  482. struct ixgbevf_q_vector *q_vector;
  483. int q_vectors, v_idx;
  484. q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  485. adapter->eims_enable_mask = 0;
  486. /*
  487. * Populate the IVAR table and set the ITR values to the
  488. * corresponding register.
  489. */
  490. for (v_idx = 0; v_idx < q_vectors; v_idx++) {
  491. struct ixgbevf_ring *ring;
  492. q_vector = adapter->q_vector[v_idx];
  493. ixgbevf_for_each_ring(ring, q_vector->rx)
  494. ixgbevf_set_ivar(adapter, 0, ring->reg_idx, v_idx);
  495. ixgbevf_for_each_ring(ring, q_vector->tx)
  496. ixgbevf_set_ivar(adapter, 1, ring->reg_idx, v_idx);
  497. if (q_vector->tx.ring && !q_vector->rx.ring) {
  498. /* tx only vector */
  499. if (adapter->tx_itr_setting == 1)
  500. q_vector->itr = IXGBE_10K_ITR;
  501. else
  502. q_vector->itr = adapter->tx_itr_setting;
  503. } else {
  504. /* rx or rx/tx vector */
  505. if (adapter->rx_itr_setting == 1)
  506. q_vector->itr = IXGBE_20K_ITR;
  507. else
  508. q_vector->itr = adapter->rx_itr_setting;
  509. }
  510. /* add q_vector eims value to global eims_enable_mask */
  511. adapter->eims_enable_mask |= 1 << v_idx;
  512. ixgbevf_write_eitr(q_vector);
  513. }
  514. ixgbevf_set_ivar(adapter, -1, 1, v_idx);
  515. /* setup eims_other and add value to global eims_enable_mask */
  516. adapter->eims_other = 1 << v_idx;
  517. adapter->eims_enable_mask |= adapter->eims_other;
  518. }
  519. enum latency_range {
  520. lowest_latency = 0,
  521. low_latency = 1,
  522. bulk_latency = 2,
  523. latency_invalid = 255
  524. };
  525. /**
  526. * ixgbevf_update_itr - update the dynamic ITR value based on statistics
  527. * @q_vector: structure containing interrupt and ring information
  528. * @ring_container: structure containing ring performance data
  529. *
  530. * Stores a new ITR value based on packets and byte
  531. * counts during the last interrupt. The advantage of per interrupt
  532. * computation is faster updates and more accurate ITR for the current
  533. * traffic pattern. Constants in this function were computed
  534. * based on theoretical maximum wire speed and thresholds were set based
  535. * on testing data as well as attempting to minimize response time
  536. * while increasing bulk throughput.
  537. **/
  538. static void ixgbevf_update_itr(struct ixgbevf_q_vector *q_vector,
  539. struct ixgbevf_ring_container *ring_container)
  540. {
  541. int bytes = ring_container->total_bytes;
  542. int packets = ring_container->total_packets;
  543. u32 timepassed_us;
  544. u64 bytes_perint;
  545. u8 itr_setting = ring_container->itr;
  546. if (packets == 0)
  547. return;
  548. /* simple throttlerate management
  549. * 0-20MB/s lowest (100000 ints/s)
  550. * 20-100MB/s low (20000 ints/s)
  551. * 100-1249MB/s bulk (8000 ints/s)
  552. */
  553. /* what was last interrupt timeslice? */
  554. timepassed_us = q_vector->itr >> 2;
  555. bytes_perint = bytes / timepassed_us; /* bytes/usec */
  556. switch (itr_setting) {
  557. case lowest_latency:
  558. if (bytes_perint > 10)
  559. itr_setting = low_latency;
  560. break;
  561. case low_latency:
  562. if (bytes_perint > 20)
  563. itr_setting = bulk_latency;
  564. else if (bytes_perint <= 10)
  565. itr_setting = lowest_latency;
  566. break;
  567. case bulk_latency:
  568. if (bytes_perint <= 20)
  569. itr_setting = low_latency;
  570. break;
  571. }
  572. /* clear work counters since we have the values we need */
  573. ring_container->total_bytes = 0;
  574. ring_container->total_packets = 0;
  575. /* write updated itr to ring container */
  576. ring_container->itr = itr_setting;
  577. }
  578. static void ixgbevf_set_itr(struct ixgbevf_q_vector *q_vector)
  579. {
  580. u32 new_itr = q_vector->itr;
  581. u8 current_itr;
  582. ixgbevf_update_itr(q_vector, &q_vector->tx);
  583. ixgbevf_update_itr(q_vector, &q_vector->rx);
  584. current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
  585. switch (current_itr) {
  586. /* counts and packets in update_itr are dependent on these numbers */
  587. case lowest_latency:
  588. new_itr = IXGBE_100K_ITR;
  589. break;
  590. case low_latency:
  591. new_itr = IXGBE_20K_ITR;
  592. break;
  593. case bulk_latency:
  594. default:
  595. new_itr = IXGBE_8K_ITR;
  596. break;
  597. }
  598. if (new_itr != q_vector->itr) {
  599. /* do an exponential smoothing */
  600. new_itr = (10 * new_itr * q_vector->itr) /
  601. ((9 * new_itr) + q_vector->itr);
  602. /* save the algorithm value here */
  603. q_vector->itr = new_itr;
  604. ixgbevf_write_eitr(q_vector);
  605. }
  606. }
  607. static irqreturn_t ixgbevf_msix_mbx(int irq, void *data)
  608. {
  609. struct ixgbevf_adapter *adapter = data;
  610. struct ixgbe_hw *hw = &adapter->hw;
  611. u32 msg;
  612. bool got_ack = false;
  613. if (!hw->mbx.ops.check_for_ack(hw))
  614. got_ack = true;
  615. if (!hw->mbx.ops.check_for_msg(hw)) {
  616. hw->mbx.ops.read(hw, &msg, 1);
  617. if ((msg & IXGBE_MBVFICR_VFREQ_MASK) == IXGBE_PF_CONTROL_MSG)
  618. mod_timer(&adapter->watchdog_timer,
  619. round_jiffies(jiffies + 1));
  620. if (msg & IXGBE_VT_MSGTYPE_NACK)
  621. pr_warn("Last Request of type %2.2x to PF Nacked\n",
  622. msg & 0xFF);
  623. /*
  624. * Restore the PFSTS bit in case someone is polling for a
  625. * return message from the PF
  626. */
  627. hw->mbx.v2p_mailbox |= IXGBE_VFMAILBOX_PFSTS;
  628. }
  629. /*
  630. * checking for the ack clears the PFACK bit. Place
  631. * it back in the v2p_mailbox cache so that anyone
  632. * polling for an ack will not miss it
  633. */
  634. if (got_ack)
  635. hw->mbx.v2p_mailbox |= IXGBE_VFMAILBOX_PFACK;
  636. IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_other);
  637. return IRQ_HANDLED;
  638. }
  639. /**
  640. * ixgbevf_msix_clean_rings - single unshared vector rx clean (all queues)
  641. * @irq: unused
  642. * @data: pointer to our q_vector struct for this interrupt vector
  643. **/
  644. static irqreturn_t ixgbevf_msix_clean_rings(int irq, void *data)
  645. {
  646. struct ixgbevf_q_vector *q_vector = data;
  647. /* EIAM disabled interrupts (on this vector) for us */
  648. if (q_vector->rx.ring || q_vector->tx.ring)
  649. napi_schedule(&q_vector->napi);
  650. return IRQ_HANDLED;
  651. }
  652. static inline void map_vector_to_rxq(struct ixgbevf_adapter *a, int v_idx,
  653. int r_idx)
  654. {
  655. struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
  656. a->rx_ring[r_idx].next = q_vector->rx.ring;
  657. q_vector->rx.ring = &a->rx_ring[r_idx];
  658. q_vector->rx.count++;
  659. }
  660. static inline void map_vector_to_txq(struct ixgbevf_adapter *a, int v_idx,
  661. int t_idx)
  662. {
  663. struct ixgbevf_q_vector *q_vector = a->q_vector[v_idx];
  664. a->tx_ring[t_idx].next = q_vector->tx.ring;
  665. q_vector->tx.ring = &a->tx_ring[t_idx];
  666. q_vector->tx.count++;
  667. }
  668. /**
  669. * ixgbevf_map_rings_to_vectors - Maps descriptor rings to vectors
  670. * @adapter: board private structure to initialize
  671. *
  672. * This function maps descriptor rings to the queue-specific vectors
  673. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  674. * one vector per ring/queue, but on a constrained vector budget, we
  675. * group the rings as "efficiently" as possible. You would add new
  676. * mapping configurations in here.
  677. **/
  678. static int ixgbevf_map_rings_to_vectors(struct ixgbevf_adapter *adapter)
  679. {
  680. int q_vectors;
  681. int v_start = 0;
  682. int rxr_idx = 0, txr_idx = 0;
  683. int rxr_remaining = adapter->num_rx_queues;
  684. int txr_remaining = adapter->num_tx_queues;
  685. int i, j;
  686. int rqpv, tqpv;
  687. int err = 0;
  688. q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  689. /*
  690. * The ideal configuration...
  691. * We have enough vectors to map one per queue.
  692. */
  693. if (q_vectors == adapter->num_rx_queues + adapter->num_tx_queues) {
  694. for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
  695. map_vector_to_rxq(adapter, v_start, rxr_idx);
  696. for (; txr_idx < txr_remaining; v_start++, txr_idx++)
  697. map_vector_to_txq(adapter, v_start, txr_idx);
  698. goto out;
  699. }
  700. /*
  701. * If we don't have enough vectors for a 1-to-1
  702. * mapping, we'll have to group them so there are
  703. * multiple queues per vector.
  704. */
  705. /* Re-adjusting *qpv takes care of the remainder. */
  706. for (i = v_start; i < q_vectors; i++) {
  707. rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
  708. for (j = 0; j < rqpv; j++) {
  709. map_vector_to_rxq(adapter, i, rxr_idx);
  710. rxr_idx++;
  711. rxr_remaining--;
  712. }
  713. }
  714. for (i = v_start; i < q_vectors; i++) {
  715. tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
  716. for (j = 0; j < tqpv; j++) {
  717. map_vector_to_txq(adapter, i, txr_idx);
  718. txr_idx++;
  719. txr_remaining--;
  720. }
  721. }
  722. out:
  723. return err;
  724. }
  725. /**
  726. * ixgbevf_request_msix_irqs - Initialize MSI-X interrupts
  727. * @adapter: board private structure
  728. *
  729. * ixgbevf_request_msix_irqs allocates MSI-X vectors and requests
  730. * interrupts from the kernel.
  731. **/
  732. static int ixgbevf_request_msix_irqs(struct ixgbevf_adapter *adapter)
  733. {
  734. struct net_device *netdev = adapter->netdev;
  735. int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  736. int vector, err;
  737. int ri = 0, ti = 0;
  738. for (vector = 0; vector < q_vectors; vector++) {
  739. struct ixgbevf_q_vector *q_vector = adapter->q_vector[vector];
  740. struct msix_entry *entry = &adapter->msix_entries[vector];
  741. if (q_vector->tx.ring && q_vector->rx.ring) {
  742. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  743. "%s-%s-%d", netdev->name, "TxRx", ri++);
  744. ti++;
  745. } else if (q_vector->rx.ring) {
  746. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  747. "%s-%s-%d", netdev->name, "rx", ri++);
  748. } else if (q_vector->tx.ring) {
  749. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  750. "%s-%s-%d", netdev->name, "tx", ti++);
  751. } else {
  752. /* skip this unused q_vector */
  753. continue;
  754. }
  755. err = request_irq(entry->vector, &ixgbevf_msix_clean_rings, 0,
  756. q_vector->name, q_vector);
  757. if (err) {
  758. hw_dbg(&adapter->hw,
  759. "request_irq failed for MSIX interrupt "
  760. "Error: %d\n", err);
  761. goto free_queue_irqs;
  762. }
  763. }
  764. err = request_irq(adapter->msix_entries[vector].vector,
  765. &ixgbevf_msix_mbx, 0, netdev->name, adapter);
  766. if (err) {
  767. hw_dbg(&adapter->hw,
  768. "request_irq for msix_mbx failed: %d\n", err);
  769. goto free_queue_irqs;
  770. }
  771. return 0;
  772. free_queue_irqs:
  773. while (vector) {
  774. vector--;
  775. free_irq(adapter->msix_entries[vector].vector,
  776. adapter->q_vector[vector]);
  777. }
  778. pci_disable_msix(adapter->pdev);
  779. kfree(adapter->msix_entries);
  780. adapter->msix_entries = NULL;
  781. return err;
  782. }
  783. static inline void ixgbevf_reset_q_vectors(struct ixgbevf_adapter *adapter)
  784. {
  785. int i, q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  786. for (i = 0; i < q_vectors; i++) {
  787. struct ixgbevf_q_vector *q_vector = adapter->q_vector[i];
  788. q_vector->rx.ring = NULL;
  789. q_vector->tx.ring = NULL;
  790. q_vector->rx.count = 0;
  791. q_vector->tx.count = 0;
  792. }
  793. }
  794. /**
  795. * ixgbevf_request_irq - initialize interrupts
  796. * @adapter: board private structure
  797. *
  798. * Attempts to configure interrupts using the best available
  799. * capabilities of the hardware and kernel.
  800. **/
  801. static int ixgbevf_request_irq(struct ixgbevf_adapter *adapter)
  802. {
  803. int err = 0;
  804. err = ixgbevf_request_msix_irqs(adapter);
  805. if (err)
  806. hw_dbg(&adapter->hw,
  807. "request_irq failed, Error %d\n", err);
  808. return err;
  809. }
  810. static void ixgbevf_free_irq(struct ixgbevf_adapter *adapter)
  811. {
  812. int i, q_vectors;
  813. q_vectors = adapter->num_msix_vectors;
  814. i = q_vectors - 1;
  815. free_irq(adapter->msix_entries[i].vector, adapter);
  816. i--;
  817. for (; i >= 0; i--) {
  818. /* free only the irqs that were actually requested */
  819. if (!adapter->q_vector[i]->rx.ring &&
  820. !adapter->q_vector[i]->tx.ring)
  821. continue;
  822. free_irq(adapter->msix_entries[i].vector,
  823. adapter->q_vector[i]);
  824. }
  825. ixgbevf_reset_q_vectors(adapter);
  826. }
  827. /**
  828. * ixgbevf_irq_disable - Mask off interrupt generation on the NIC
  829. * @adapter: board private structure
  830. **/
  831. static inline void ixgbevf_irq_disable(struct ixgbevf_adapter *adapter)
  832. {
  833. struct ixgbe_hw *hw = &adapter->hw;
  834. int i;
  835. IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, 0);
  836. IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, ~0);
  837. IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, 0);
  838. IXGBE_WRITE_FLUSH(hw);
  839. for (i = 0; i < adapter->num_msix_vectors; i++)
  840. synchronize_irq(adapter->msix_entries[i].vector);
  841. }
  842. /**
  843. * ixgbevf_irq_enable - Enable default interrupt generation settings
  844. * @adapter: board private structure
  845. **/
  846. static inline void ixgbevf_irq_enable(struct ixgbevf_adapter *adapter)
  847. {
  848. struct ixgbe_hw *hw = &adapter->hw;
  849. IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, adapter->eims_enable_mask);
  850. IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, adapter->eims_enable_mask);
  851. IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, adapter->eims_enable_mask);
  852. }
  853. /**
  854. * ixgbevf_configure_tx - Configure 82599 VF Transmit Unit after Reset
  855. * @adapter: board private structure
  856. *
  857. * Configure the Tx unit of the MAC after a reset.
  858. **/
  859. static void ixgbevf_configure_tx(struct ixgbevf_adapter *adapter)
  860. {
  861. u64 tdba;
  862. struct ixgbe_hw *hw = &adapter->hw;
  863. u32 i, j, tdlen, txctrl;
  864. /* Setup the HW Tx Head and Tail descriptor pointers */
  865. for (i = 0; i < adapter->num_tx_queues; i++) {
  866. struct ixgbevf_ring *ring = &adapter->tx_ring[i];
  867. j = ring->reg_idx;
  868. tdba = ring->dma;
  869. tdlen = ring->count * sizeof(union ixgbe_adv_tx_desc);
  870. IXGBE_WRITE_REG(hw, IXGBE_VFTDBAL(j),
  871. (tdba & DMA_BIT_MASK(32)));
  872. IXGBE_WRITE_REG(hw, IXGBE_VFTDBAH(j), (tdba >> 32));
  873. IXGBE_WRITE_REG(hw, IXGBE_VFTDLEN(j), tdlen);
  874. IXGBE_WRITE_REG(hw, IXGBE_VFTDH(j), 0);
  875. IXGBE_WRITE_REG(hw, IXGBE_VFTDT(j), 0);
  876. adapter->tx_ring[i].head = IXGBE_VFTDH(j);
  877. adapter->tx_ring[i].tail = IXGBE_VFTDT(j);
  878. /* Disable Tx Head Writeback RO bit, since this hoses
  879. * bookkeeping if things aren't delivered in order.
  880. */
  881. txctrl = IXGBE_READ_REG(hw, IXGBE_VFDCA_TXCTRL(j));
  882. txctrl &= ~IXGBE_DCA_TXCTRL_TX_WB_RO_EN;
  883. IXGBE_WRITE_REG(hw, IXGBE_VFDCA_TXCTRL(j), txctrl);
  884. }
  885. }
  886. #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
  887. static void ixgbevf_configure_srrctl(struct ixgbevf_adapter *adapter, int index)
  888. {
  889. struct ixgbevf_ring *rx_ring;
  890. struct ixgbe_hw *hw = &adapter->hw;
  891. u32 srrctl;
  892. rx_ring = &adapter->rx_ring[index];
  893. srrctl = IXGBE_SRRCTL_DROP_EN;
  894. srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
  895. if (rx_ring->rx_buf_len == MAXIMUM_ETHERNET_VLAN_SIZE)
  896. srrctl |= IXGBEVF_RXBUFFER_2048 >>
  897. IXGBE_SRRCTL_BSIZEPKT_SHIFT;
  898. else
  899. srrctl |= rx_ring->rx_buf_len >>
  900. IXGBE_SRRCTL_BSIZEPKT_SHIFT;
  901. IXGBE_WRITE_REG(hw, IXGBE_VFSRRCTL(index), srrctl);
  902. }
  903. /**
  904. * ixgbevf_configure_rx - Configure 82599 VF Receive Unit after Reset
  905. * @adapter: board private structure
  906. *
  907. * Configure the Rx unit of the MAC after a reset.
  908. **/
  909. static void ixgbevf_configure_rx(struct ixgbevf_adapter *adapter)
  910. {
  911. u64 rdba;
  912. struct ixgbe_hw *hw = &adapter->hw;
  913. struct net_device *netdev = adapter->netdev;
  914. int max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
  915. int i, j;
  916. u32 rdlen;
  917. int rx_buf_len;
  918. /* PSRTYPE must be initialized in 82599 */
  919. IXGBE_WRITE_REG(hw, IXGBE_VFPSRTYPE, 0);
  920. if (netdev->mtu <= ETH_DATA_LEN)
  921. rx_buf_len = MAXIMUM_ETHERNET_VLAN_SIZE;
  922. else
  923. rx_buf_len = ALIGN(max_frame, 1024);
  924. rdlen = adapter->rx_ring[0].count * sizeof(union ixgbe_adv_rx_desc);
  925. /* Setup the HW Rx Head and Tail Descriptor Pointers and
  926. * the Base and Length of the Rx Descriptor Ring */
  927. for (i = 0; i < adapter->num_rx_queues; i++) {
  928. rdba = adapter->rx_ring[i].dma;
  929. j = adapter->rx_ring[i].reg_idx;
  930. IXGBE_WRITE_REG(hw, IXGBE_VFRDBAL(j),
  931. (rdba & DMA_BIT_MASK(32)));
  932. IXGBE_WRITE_REG(hw, IXGBE_VFRDBAH(j), (rdba >> 32));
  933. IXGBE_WRITE_REG(hw, IXGBE_VFRDLEN(j), rdlen);
  934. IXGBE_WRITE_REG(hw, IXGBE_VFRDH(j), 0);
  935. IXGBE_WRITE_REG(hw, IXGBE_VFRDT(j), 0);
  936. adapter->rx_ring[i].head = IXGBE_VFRDH(j);
  937. adapter->rx_ring[i].tail = IXGBE_VFRDT(j);
  938. adapter->rx_ring[i].rx_buf_len = rx_buf_len;
  939. ixgbevf_configure_srrctl(adapter, j);
  940. }
  941. }
  942. static int ixgbevf_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
  943. {
  944. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  945. struct ixgbe_hw *hw = &adapter->hw;
  946. spin_lock(&adapter->mbx_lock);
  947. /* add VID to filter table */
  948. if (hw->mac.ops.set_vfta)
  949. hw->mac.ops.set_vfta(hw, vid, 0, true);
  950. spin_unlock(&adapter->mbx_lock);
  951. set_bit(vid, adapter->active_vlans);
  952. return 0;
  953. }
  954. static int ixgbevf_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
  955. {
  956. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  957. struct ixgbe_hw *hw = &adapter->hw;
  958. spin_lock(&adapter->mbx_lock);
  959. /* remove VID from filter table */
  960. if (hw->mac.ops.set_vfta)
  961. hw->mac.ops.set_vfta(hw, vid, 0, false);
  962. spin_unlock(&adapter->mbx_lock);
  963. clear_bit(vid, adapter->active_vlans);
  964. return 0;
  965. }
  966. static void ixgbevf_restore_vlan(struct ixgbevf_adapter *adapter)
  967. {
  968. u16 vid;
  969. for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
  970. ixgbevf_vlan_rx_add_vid(adapter->netdev, vid);
  971. }
  972. static int ixgbevf_write_uc_addr_list(struct net_device *netdev)
  973. {
  974. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  975. struct ixgbe_hw *hw = &adapter->hw;
  976. int count = 0;
  977. if ((netdev_uc_count(netdev)) > 10) {
  978. pr_err("Too many unicast filters - No Space\n");
  979. return -ENOSPC;
  980. }
  981. if (!netdev_uc_empty(netdev)) {
  982. struct netdev_hw_addr *ha;
  983. netdev_for_each_uc_addr(ha, netdev) {
  984. hw->mac.ops.set_uc_addr(hw, ++count, ha->addr);
  985. udelay(200);
  986. }
  987. } else {
  988. /*
  989. * If the list is empty then send message to PF driver to
  990. * clear all macvlans on this VF.
  991. */
  992. hw->mac.ops.set_uc_addr(hw, 0, NULL);
  993. }
  994. return count;
  995. }
  996. /**
  997. * ixgbevf_set_rx_mode - Multicast set
  998. * @netdev: network interface device structure
  999. *
  1000. * The set_rx_method entry point is called whenever the multicast address
  1001. * list or the network interface flags are updated. This routine is
  1002. * responsible for configuring the hardware for proper multicast mode.
  1003. **/
  1004. static void ixgbevf_set_rx_mode(struct net_device *netdev)
  1005. {
  1006. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  1007. struct ixgbe_hw *hw = &adapter->hw;
  1008. spin_lock(&adapter->mbx_lock);
  1009. /* reprogram multicast list */
  1010. if (hw->mac.ops.update_mc_addr_list)
  1011. hw->mac.ops.update_mc_addr_list(hw, netdev);
  1012. ixgbevf_write_uc_addr_list(netdev);
  1013. spin_unlock(&adapter->mbx_lock);
  1014. }
  1015. static void ixgbevf_napi_enable_all(struct ixgbevf_adapter *adapter)
  1016. {
  1017. int q_idx;
  1018. struct ixgbevf_q_vector *q_vector;
  1019. int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  1020. for (q_idx = 0; q_idx < q_vectors; q_idx++) {
  1021. q_vector = adapter->q_vector[q_idx];
  1022. napi_enable(&q_vector->napi);
  1023. }
  1024. }
  1025. static void ixgbevf_napi_disable_all(struct ixgbevf_adapter *adapter)
  1026. {
  1027. int q_idx;
  1028. struct ixgbevf_q_vector *q_vector;
  1029. int q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  1030. for (q_idx = 0; q_idx < q_vectors; q_idx++) {
  1031. q_vector = adapter->q_vector[q_idx];
  1032. napi_disable(&q_vector->napi);
  1033. }
  1034. }
  1035. static void ixgbevf_configure(struct ixgbevf_adapter *adapter)
  1036. {
  1037. struct net_device *netdev = adapter->netdev;
  1038. int i;
  1039. ixgbevf_set_rx_mode(netdev);
  1040. ixgbevf_restore_vlan(adapter);
  1041. ixgbevf_configure_tx(adapter);
  1042. ixgbevf_configure_rx(adapter);
  1043. for (i = 0; i < adapter->num_rx_queues; i++) {
  1044. struct ixgbevf_ring *ring = &adapter->rx_ring[i];
  1045. ixgbevf_alloc_rx_buffers(adapter, ring,
  1046. IXGBE_DESC_UNUSED(ring));
  1047. }
  1048. }
  1049. #define IXGBE_MAX_RX_DESC_POLL 10
  1050. static inline void ixgbevf_rx_desc_queue_enable(struct ixgbevf_adapter *adapter,
  1051. int rxr)
  1052. {
  1053. struct ixgbe_hw *hw = &adapter->hw;
  1054. int j = adapter->rx_ring[rxr].reg_idx;
  1055. int k;
  1056. for (k = 0; k < IXGBE_MAX_RX_DESC_POLL; k++) {
  1057. if (IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j)) & IXGBE_RXDCTL_ENABLE)
  1058. break;
  1059. else
  1060. msleep(1);
  1061. }
  1062. if (k >= IXGBE_MAX_RX_DESC_POLL) {
  1063. hw_dbg(hw, "RXDCTL.ENABLE on Rx queue %d "
  1064. "not set within the polling period\n", rxr);
  1065. }
  1066. ixgbevf_release_rx_desc(&adapter->hw, &adapter->rx_ring[rxr],
  1067. (adapter->rx_ring[rxr].count - 1));
  1068. }
  1069. static void ixgbevf_save_reset_stats(struct ixgbevf_adapter *adapter)
  1070. {
  1071. /* Only save pre-reset stats if there are some */
  1072. if (adapter->stats.vfgprc || adapter->stats.vfgptc) {
  1073. adapter->stats.saved_reset_vfgprc += adapter->stats.vfgprc -
  1074. adapter->stats.base_vfgprc;
  1075. adapter->stats.saved_reset_vfgptc += adapter->stats.vfgptc -
  1076. adapter->stats.base_vfgptc;
  1077. adapter->stats.saved_reset_vfgorc += adapter->stats.vfgorc -
  1078. adapter->stats.base_vfgorc;
  1079. adapter->stats.saved_reset_vfgotc += adapter->stats.vfgotc -
  1080. adapter->stats.base_vfgotc;
  1081. adapter->stats.saved_reset_vfmprc += adapter->stats.vfmprc -
  1082. adapter->stats.base_vfmprc;
  1083. }
  1084. }
  1085. static void ixgbevf_init_last_counter_stats(struct ixgbevf_adapter *adapter)
  1086. {
  1087. struct ixgbe_hw *hw = &adapter->hw;
  1088. adapter->stats.last_vfgprc = IXGBE_READ_REG(hw, IXGBE_VFGPRC);
  1089. adapter->stats.last_vfgorc = IXGBE_READ_REG(hw, IXGBE_VFGORC_LSB);
  1090. adapter->stats.last_vfgorc |=
  1091. (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGORC_MSB))) << 32);
  1092. adapter->stats.last_vfgptc = IXGBE_READ_REG(hw, IXGBE_VFGPTC);
  1093. adapter->stats.last_vfgotc = IXGBE_READ_REG(hw, IXGBE_VFGOTC_LSB);
  1094. adapter->stats.last_vfgotc |=
  1095. (((u64)(IXGBE_READ_REG(hw, IXGBE_VFGOTC_MSB))) << 32);
  1096. adapter->stats.last_vfmprc = IXGBE_READ_REG(hw, IXGBE_VFMPRC);
  1097. adapter->stats.base_vfgprc = adapter->stats.last_vfgprc;
  1098. adapter->stats.base_vfgorc = adapter->stats.last_vfgorc;
  1099. adapter->stats.base_vfgptc = adapter->stats.last_vfgptc;
  1100. adapter->stats.base_vfgotc = adapter->stats.last_vfgotc;
  1101. adapter->stats.base_vfmprc = adapter->stats.last_vfmprc;
  1102. }
  1103. static void ixgbevf_up_complete(struct ixgbevf_adapter *adapter)
  1104. {
  1105. struct net_device *netdev = adapter->netdev;
  1106. struct ixgbe_hw *hw = &adapter->hw;
  1107. int i, j = 0;
  1108. int num_rx_rings = adapter->num_rx_queues;
  1109. u32 txdctl, rxdctl;
  1110. u32 msg[2];
  1111. for (i = 0; i < adapter->num_tx_queues; i++) {
  1112. j = adapter->tx_ring[i].reg_idx;
  1113. txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
  1114. /* enable WTHRESH=8 descriptors, to encourage burst writeback */
  1115. txdctl |= (8 << 16);
  1116. IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
  1117. }
  1118. for (i = 0; i < adapter->num_tx_queues; i++) {
  1119. j = adapter->tx_ring[i].reg_idx;
  1120. txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
  1121. txdctl |= IXGBE_TXDCTL_ENABLE;
  1122. IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j), txdctl);
  1123. }
  1124. for (i = 0; i < num_rx_rings; i++) {
  1125. j = adapter->rx_ring[i].reg_idx;
  1126. rxdctl = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(j));
  1127. rxdctl |= IXGBE_RXDCTL_ENABLE | IXGBE_RXDCTL_VME;
  1128. if (hw->mac.type == ixgbe_mac_X540_vf) {
  1129. rxdctl &= ~IXGBE_RXDCTL_RLPMLMASK;
  1130. rxdctl |= ((netdev->mtu + ETH_HLEN + ETH_FCS_LEN) |
  1131. IXGBE_RXDCTL_RLPML_EN);
  1132. }
  1133. IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(j), rxdctl);
  1134. ixgbevf_rx_desc_queue_enable(adapter, i);
  1135. }
  1136. ixgbevf_configure_msix(adapter);
  1137. spin_lock(&adapter->mbx_lock);
  1138. if (hw->mac.ops.set_rar) {
  1139. if (is_valid_ether_addr(hw->mac.addr))
  1140. hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
  1141. else
  1142. hw->mac.ops.set_rar(hw, 0, hw->mac.perm_addr, 0);
  1143. }
  1144. msg[0] = IXGBE_VF_SET_LPE;
  1145. msg[1] = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
  1146. hw->mbx.ops.write_posted(hw, msg, 2);
  1147. spin_unlock(&adapter->mbx_lock);
  1148. clear_bit(__IXGBEVF_DOWN, &adapter->state);
  1149. ixgbevf_napi_enable_all(adapter);
  1150. /* enable transmits */
  1151. netif_tx_start_all_queues(netdev);
  1152. ixgbevf_save_reset_stats(adapter);
  1153. ixgbevf_init_last_counter_stats(adapter);
  1154. mod_timer(&adapter->watchdog_timer, jiffies);
  1155. }
  1156. void ixgbevf_up(struct ixgbevf_adapter *adapter)
  1157. {
  1158. struct ixgbe_hw *hw = &adapter->hw;
  1159. ixgbevf_configure(adapter);
  1160. ixgbevf_up_complete(adapter);
  1161. /* clear any pending interrupts, may auto mask */
  1162. IXGBE_READ_REG(hw, IXGBE_VTEICR);
  1163. ixgbevf_irq_enable(adapter);
  1164. }
  1165. /**
  1166. * ixgbevf_clean_rx_ring - Free Rx Buffers per Queue
  1167. * @adapter: board private structure
  1168. * @rx_ring: ring to free buffers from
  1169. **/
  1170. static void ixgbevf_clean_rx_ring(struct ixgbevf_adapter *adapter,
  1171. struct ixgbevf_ring *rx_ring)
  1172. {
  1173. struct pci_dev *pdev = adapter->pdev;
  1174. unsigned long size;
  1175. unsigned int i;
  1176. if (!rx_ring->rx_buffer_info)
  1177. return;
  1178. /* Free all the Rx ring sk_buffs */
  1179. for (i = 0; i < rx_ring->count; i++) {
  1180. struct ixgbevf_rx_buffer *rx_buffer_info;
  1181. rx_buffer_info = &rx_ring->rx_buffer_info[i];
  1182. if (rx_buffer_info->dma) {
  1183. dma_unmap_single(&pdev->dev, rx_buffer_info->dma,
  1184. rx_ring->rx_buf_len,
  1185. DMA_FROM_DEVICE);
  1186. rx_buffer_info->dma = 0;
  1187. }
  1188. if (rx_buffer_info->skb) {
  1189. struct sk_buff *skb = rx_buffer_info->skb;
  1190. rx_buffer_info->skb = NULL;
  1191. do {
  1192. struct sk_buff *this = skb;
  1193. skb = skb->prev;
  1194. dev_kfree_skb(this);
  1195. } while (skb);
  1196. }
  1197. }
  1198. size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
  1199. memset(rx_ring->rx_buffer_info, 0, size);
  1200. /* Zero out the descriptor ring */
  1201. memset(rx_ring->desc, 0, rx_ring->size);
  1202. rx_ring->next_to_clean = 0;
  1203. rx_ring->next_to_use = 0;
  1204. if (rx_ring->head)
  1205. writel(0, adapter->hw.hw_addr + rx_ring->head);
  1206. if (rx_ring->tail)
  1207. writel(0, adapter->hw.hw_addr + rx_ring->tail);
  1208. }
  1209. /**
  1210. * ixgbevf_clean_tx_ring - Free Tx Buffers
  1211. * @adapter: board private structure
  1212. * @tx_ring: ring to be cleaned
  1213. **/
  1214. static void ixgbevf_clean_tx_ring(struct ixgbevf_adapter *adapter,
  1215. struct ixgbevf_ring *tx_ring)
  1216. {
  1217. struct ixgbevf_tx_buffer *tx_buffer_info;
  1218. unsigned long size;
  1219. unsigned int i;
  1220. if (!tx_ring->tx_buffer_info)
  1221. return;
  1222. /* Free all the Tx ring sk_buffs */
  1223. for (i = 0; i < tx_ring->count; i++) {
  1224. tx_buffer_info = &tx_ring->tx_buffer_info[i];
  1225. ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
  1226. }
  1227. size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
  1228. memset(tx_ring->tx_buffer_info, 0, size);
  1229. memset(tx_ring->desc, 0, tx_ring->size);
  1230. tx_ring->next_to_use = 0;
  1231. tx_ring->next_to_clean = 0;
  1232. if (tx_ring->head)
  1233. writel(0, adapter->hw.hw_addr + tx_ring->head);
  1234. if (tx_ring->tail)
  1235. writel(0, adapter->hw.hw_addr + tx_ring->tail);
  1236. }
  1237. /**
  1238. * ixgbevf_clean_all_rx_rings - Free Rx Buffers for all queues
  1239. * @adapter: board private structure
  1240. **/
  1241. static void ixgbevf_clean_all_rx_rings(struct ixgbevf_adapter *adapter)
  1242. {
  1243. int i;
  1244. for (i = 0; i < adapter->num_rx_queues; i++)
  1245. ixgbevf_clean_rx_ring(adapter, &adapter->rx_ring[i]);
  1246. }
  1247. /**
  1248. * ixgbevf_clean_all_tx_rings - Free Tx Buffers for all queues
  1249. * @adapter: board private structure
  1250. **/
  1251. static void ixgbevf_clean_all_tx_rings(struct ixgbevf_adapter *adapter)
  1252. {
  1253. int i;
  1254. for (i = 0; i < adapter->num_tx_queues; i++)
  1255. ixgbevf_clean_tx_ring(adapter, &adapter->tx_ring[i]);
  1256. }
  1257. void ixgbevf_down(struct ixgbevf_adapter *adapter)
  1258. {
  1259. struct net_device *netdev = adapter->netdev;
  1260. struct ixgbe_hw *hw = &adapter->hw;
  1261. u32 txdctl;
  1262. int i, j;
  1263. /* signal that we are down to the interrupt handler */
  1264. set_bit(__IXGBEVF_DOWN, &adapter->state);
  1265. /* disable receives */
  1266. netif_tx_disable(netdev);
  1267. msleep(10);
  1268. netif_tx_stop_all_queues(netdev);
  1269. ixgbevf_irq_disable(adapter);
  1270. ixgbevf_napi_disable_all(adapter);
  1271. del_timer_sync(&adapter->watchdog_timer);
  1272. /* can't call flush scheduled work here because it can deadlock
  1273. * if linkwatch_event tries to acquire the rtnl_lock which we are
  1274. * holding */
  1275. while (adapter->flags & IXGBE_FLAG_IN_WATCHDOG_TASK)
  1276. msleep(1);
  1277. /* disable transmits in the hardware now that interrupts are off */
  1278. for (i = 0; i < adapter->num_tx_queues; i++) {
  1279. j = adapter->tx_ring[i].reg_idx;
  1280. txdctl = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(j));
  1281. IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(j),
  1282. (txdctl & ~IXGBE_TXDCTL_ENABLE));
  1283. }
  1284. netif_carrier_off(netdev);
  1285. if (!pci_channel_offline(adapter->pdev))
  1286. ixgbevf_reset(adapter);
  1287. ixgbevf_clean_all_tx_rings(adapter);
  1288. ixgbevf_clean_all_rx_rings(adapter);
  1289. }
  1290. void ixgbevf_reinit_locked(struct ixgbevf_adapter *adapter)
  1291. {
  1292. struct ixgbe_hw *hw = &adapter->hw;
  1293. WARN_ON(in_interrupt());
  1294. while (test_and_set_bit(__IXGBEVF_RESETTING, &adapter->state))
  1295. msleep(1);
  1296. /*
  1297. * Check if PF is up before re-init. If not then skip until
  1298. * later when the PF is up and ready to service requests from
  1299. * the VF via mailbox. If the VF is up and running then the
  1300. * watchdog task will continue to schedule reset tasks until
  1301. * the PF is up and running.
  1302. */
  1303. if (!hw->mac.ops.reset_hw(hw)) {
  1304. ixgbevf_down(adapter);
  1305. ixgbevf_up(adapter);
  1306. }
  1307. clear_bit(__IXGBEVF_RESETTING, &adapter->state);
  1308. }
  1309. void ixgbevf_reset(struct ixgbevf_adapter *adapter)
  1310. {
  1311. struct ixgbe_hw *hw = &adapter->hw;
  1312. struct net_device *netdev = adapter->netdev;
  1313. spin_lock(&adapter->mbx_lock);
  1314. if (hw->mac.ops.reset_hw(hw))
  1315. hw_dbg(hw, "PF still resetting\n");
  1316. else
  1317. hw->mac.ops.init_hw(hw);
  1318. spin_unlock(&adapter->mbx_lock);
  1319. if (is_valid_ether_addr(adapter->hw.mac.addr)) {
  1320. memcpy(netdev->dev_addr, adapter->hw.mac.addr,
  1321. netdev->addr_len);
  1322. memcpy(netdev->perm_addr, adapter->hw.mac.addr,
  1323. netdev->addr_len);
  1324. }
  1325. }
  1326. static void ixgbevf_acquire_msix_vectors(struct ixgbevf_adapter *adapter,
  1327. int vectors)
  1328. {
  1329. int err, vector_threshold;
  1330. /* We'll want at least 2 (vector_threshold):
  1331. * 1) TxQ[0] + RxQ[0] handler
  1332. * 2) Other (Link Status Change, etc.)
  1333. */
  1334. vector_threshold = MIN_MSIX_COUNT;
  1335. /* The more we get, the more we will assign to Tx/Rx Cleanup
  1336. * for the separate queues...where Rx Cleanup >= Tx Cleanup.
  1337. * Right now, we simply care about how many we'll get; we'll
  1338. * set them up later while requesting irq's.
  1339. */
  1340. while (vectors >= vector_threshold) {
  1341. err = pci_enable_msix(adapter->pdev, adapter->msix_entries,
  1342. vectors);
  1343. if (!err) /* Success in acquiring all requested vectors. */
  1344. break;
  1345. else if (err < 0)
  1346. vectors = 0; /* Nasty failure, quit now */
  1347. else /* err == number of vectors we should try again with */
  1348. vectors = err;
  1349. }
  1350. if (vectors < vector_threshold) {
  1351. /* Can't allocate enough MSI-X interrupts? Oh well.
  1352. * This just means we'll go with either a single MSI
  1353. * vector or fall back to legacy interrupts.
  1354. */
  1355. hw_dbg(&adapter->hw,
  1356. "Unable to allocate MSI-X interrupts\n");
  1357. kfree(adapter->msix_entries);
  1358. adapter->msix_entries = NULL;
  1359. } else {
  1360. /*
  1361. * Adjust for only the vectors we'll use, which is minimum
  1362. * of max_msix_q_vectors + NON_Q_VECTORS, or the number of
  1363. * vectors we were allocated.
  1364. */
  1365. adapter->num_msix_vectors = vectors;
  1366. }
  1367. }
  1368. /**
  1369. * ixgbevf_set_num_queues - Allocate queues for device, feature dependent
  1370. * @adapter: board private structure to initialize
  1371. *
  1372. * This is the top level queue allocation routine. The order here is very
  1373. * important, starting with the "most" number of features turned on at once,
  1374. * and ending with the smallest set of features. This way large combinations
  1375. * can be allocated if they're turned on, and smaller combinations are the
  1376. * fallthrough conditions.
  1377. *
  1378. **/
  1379. static void ixgbevf_set_num_queues(struct ixgbevf_adapter *adapter)
  1380. {
  1381. /* Start with base case */
  1382. adapter->num_rx_queues = 1;
  1383. adapter->num_tx_queues = 1;
  1384. }
  1385. /**
  1386. * ixgbevf_alloc_queues - Allocate memory for all rings
  1387. * @adapter: board private structure to initialize
  1388. *
  1389. * We allocate one ring per queue at run-time since we don't know the
  1390. * number of queues at compile-time. The polling_netdev array is
  1391. * intended for Multiqueue, but should work fine with a single queue.
  1392. **/
  1393. static int ixgbevf_alloc_queues(struct ixgbevf_adapter *adapter)
  1394. {
  1395. int i;
  1396. adapter->tx_ring = kcalloc(adapter->num_tx_queues,
  1397. sizeof(struct ixgbevf_ring), GFP_KERNEL);
  1398. if (!adapter->tx_ring)
  1399. goto err_tx_ring_allocation;
  1400. adapter->rx_ring = kcalloc(adapter->num_rx_queues,
  1401. sizeof(struct ixgbevf_ring), GFP_KERNEL);
  1402. if (!adapter->rx_ring)
  1403. goto err_rx_ring_allocation;
  1404. for (i = 0; i < adapter->num_tx_queues; i++) {
  1405. adapter->tx_ring[i].count = adapter->tx_ring_count;
  1406. adapter->tx_ring[i].queue_index = i;
  1407. adapter->tx_ring[i].reg_idx = i;
  1408. adapter->tx_ring[i].dev = &adapter->pdev->dev;
  1409. adapter->tx_ring[i].netdev = adapter->netdev;
  1410. }
  1411. for (i = 0; i < adapter->num_rx_queues; i++) {
  1412. adapter->rx_ring[i].count = adapter->rx_ring_count;
  1413. adapter->rx_ring[i].queue_index = i;
  1414. adapter->rx_ring[i].reg_idx = i;
  1415. adapter->rx_ring[i].dev = &adapter->pdev->dev;
  1416. adapter->rx_ring[i].netdev = adapter->netdev;
  1417. }
  1418. return 0;
  1419. err_rx_ring_allocation:
  1420. kfree(adapter->tx_ring);
  1421. err_tx_ring_allocation:
  1422. return -ENOMEM;
  1423. }
  1424. /**
  1425. * ixgbevf_set_interrupt_capability - set MSI-X or FAIL if not supported
  1426. * @adapter: board private structure to initialize
  1427. *
  1428. * Attempt to configure the interrupts using the best available
  1429. * capabilities of the hardware and the kernel.
  1430. **/
  1431. static int ixgbevf_set_interrupt_capability(struct ixgbevf_adapter *adapter)
  1432. {
  1433. int err = 0;
  1434. int vector, v_budget;
  1435. /*
  1436. * It's easy to be greedy for MSI-X vectors, but it really
  1437. * doesn't do us much good if we have a lot more vectors
  1438. * than CPU's. So let's be conservative and only ask for
  1439. * (roughly) the same number of vectors as there are CPU's.
  1440. * The default is to use pairs of vectors.
  1441. */
  1442. v_budget = max(adapter->num_rx_queues, adapter->num_tx_queues);
  1443. v_budget = min_t(int, v_budget, num_online_cpus());
  1444. v_budget += NON_Q_VECTORS;
  1445. /* A failure in MSI-X entry allocation isn't fatal, but it does
  1446. * mean we disable MSI-X capabilities of the adapter. */
  1447. adapter->msix_entries = kcalloc(v_budget,
  1448. sizeof(struct msix_entry), GFP_KERNEL);
  1449. if (!adapter->msix_entries) {
  1450. err = -ENOMEM;
  1451. goto out;
  1452. }
  1453. for (vector = 0; vector < v_budget; vector++)
  1454. adapter->msix_entries[vector].entry = vector;
  1455. ixgbevf_acquire_msix_vectors(adapter, v_budget);
  1456. out:
  1457. return err;
  1458. }
  1459. /**
  1460. * ixgbevf_alloc_q_vectors - Allocate memory for interrupt vectors
  1461. * @adapter: board private structure to initialize
  1462. *
  1463. * We allocate one q_vector per queue interrupt. If allocation fails we
  1464. * return -ENOMEM.
  1465. **/
  1466. static int ixgbevf_alloc_q_vectors(struct ixgbevf_adapter *adapter)
  1467. {
  1468. int q_idx, num_q_vectors;
  1469. struct ixgbevf_q_vector *q_vector;
  1470. num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  1471. for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
  1472. q_vector = kzalloc(sizeof(struct ixgbevf_q_vector), GFP_KERNEL);
  1473. if (!q_vector)
  1474. goto err_out;
  1475. q_vector->adapter = adapter;
  1476. q_vector->v_idx = q_idx;
  1477. netif_napi_add(adapter->netdev, &q_vector->napi,
  1478. ixgbevf_poll, 64);
  1479. adapter->q_vector[q_idx] = q_vector;
  1480. }
  1481. return 0;
  1482. err_out:
  1483. while (q_idx) {
  1484. q_idx--;
  1485. q_vector = adapter->q_vector[q_idx];
  1486. netif_napi_del(&q_vector->napi);
  1487. kfree(q_vector);
  1488. adapter->q_vector[q_idx] = NULL;
  1489. }
  1490. return -ENOMEM;
  1491. }
  1492. /**
  1493. * ixgbevf_free_q_vectors - Free memory allocated for interrupt vectors
  1494. * @adapter: board private structure to initialize
  1495. *
  1496. * This function frees the memory allocated to the q_vectors. In addition if
  1497. * NAPI is enabled it will delete any references to the NAPI struct prior
  1498. * to freeing the q_vector.
  1499. **/
  1500. static void ixgbevf_free_q_vectors(struct ixgbevf_adapter *adapter)
  1501. {
  1502. int q_idx, num_q_vectors;
  1503. int napi_vectors;
  1504. num_q_vectors = adapter->num_msix_vectors - NON_Q_VECTORS;
  1505. napi_vectors = adapter->num_rx_queues;
  1506. for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
  1507. struct ixgbevf_q_vector *q_vector = adapter->q_vector[q_idx];
  1508. adapter->q_vector[q_idx] = NULL;
  1509. if (q_idx < napi_vectors)
  1510. netif_napi_del(&q_vector->napi);
  1511. kfree(q_vector);
  1512. }
  1513. }
  1514. /**
  1515. * ixgbevf_reset_interrupt_capability - Reset MSIX setup
  1516. * @adapter: board private structure
  1517. *
  1518. **/
  1519. static void ixgbevf_reset_interrupt_capability(struct ixgbevf_adapter *adapter)
  1520. {
  1521. pci_disable_msix(adapter->pdev);
  1522. kfree(adapter->msix_entries);
  1523. adapter->msix_entries = NULL;
  1524. }
  1525. /**
  1526. * ixgbevf_init_interrupt_scheme - Determine if MSIX is supported and init
  1527. * @adapter: board private structure to initialize
  1528. *
  1529. **/
  1530. static int ixgbevf_init_interrupt_scheme(struct ixgbevf_adapter *adapter)
  1531. {
  1532. int err;
  1533. /* Number of supported queues */
  1534. ixgbevf_set_num_queues(adapter);
  1535. err = ixgbevf_set_interrupt_capability(adapter);
  1536. if (err) {
  1537. hw_dbg(&adapter->hw,
  1538. "Unable to setup interrupt capabilities\n");
  1539. goto err_set_interrupt;
  1540. }
  1541. err = ixgbevf_alloc_q_vectors(adapter);
  1542. if (err) {
  1543. hw_dbg(&adapter->hw, "Unable to allocate memory for queue "
  1544. "vectors\n");
  1545. goto err_alloc_q_vectors;
  1546. }
  1547. err = ixgbevf_alloc_queues(adapter);
  1548. if (err) {
  1549. pr_err("Unable to allocate memory for queues\n");
  1550. goto err_alloc_queues;
  1551. }
  1552. hw_dbg(&adapter->hw, "Multiqueue %s: Rx Queue count = %u, "
  1553. "Tx Queue count = %u\n",
  1554. (adapter->num_rx_queues > 1) ? "Enabled" :
  1555. "Disabled", adapter->num_rx_queues, adapter->num_tx_queues);
  1556. set_bit(__IXGBEVF_DOWN, &adapter->state);
  1557. return 0;
  1558. err_alloc_queues:
  1559. ixgbevf_free_q_vectors(adapter);
  1560. err_alloc_q_vectors:
  1561. ixgbevf_reset_interrupt_capability(adapter);
  1562. err_set_interrupt:
  1563. return err;
  1564. }
  1565. /**
  1566. * ixgbevf_sw_init - Initialize general software structures
  1567. * (struct ixgbevf_adapter)
  1568. * @adapter: board private structure to initialize
  1569. *
  1570. * ixgbevf_sw_init initializes the Adapter private data structure.
  1571. * Fields are initialized based on PCI device information and
  1572. * OS network device settings (MTU size).
  1573. **/
  1574. static int __devinit ixgbevf_sw_init(struct ixgbevf_adapter *adapter)
  1575. {
  1576. struct ixgbe_hw *hw = &adapter->hw;
  1577. struct pci_dev *pdev = adapter->pdev;
  1578. int err;
  1579. /* PCI config space info */
  1580. hw->vendor_id = pdev->vendor;
  1581. hw->device_id = pdev->device;
  1582. hw->revision_id = pdev->revision;
  1583. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  1584. hw->subsystem_device_id = pdev->subsystem_device;
  1585. hw->mbx.ops.init_params(hw);
  1586. hw->mac.max_tx_queues = MAX_TX_QUEUES;
  1587. hw->mac.max_rx_queues = MAX_RX_QUEUES;
  1588. err = hw->mac.ops.reset_hw(hw);
  1589. if (err) {
  1590. dev_info(&pdev->dev,
  1591. "PF still in reset state, assigning new address\n");
  1592. eth_hw_addr_random(adapter->netdev);
  1593. memcpy(adapter->hw.mac.addr, adapter->netdev->dev_addr,
  1594. adapter->netdev->addr_len);
  1595. } else {
  1596. err = hw->mac.ops.init_hw(hw);
  1597. if (err) {
  1598. pr_err("init_shared_code failed: %d\n", err);
  1599. goto out;
  1600. }
  1601. memcpy(adapter->netdev->dev_addr, adapter->hw.mac.addr,
  1602. adapter->netdev->addr_len);
  1603. }
  1604. /* lock to protect mailbox accesses */
  1605. spin_lock_init(&adapter->mbx_lock);
  1606. /* Enable dynamic interrupt throttling rates */
  1607. adapter->rx_itr_setting = 1;
  1608. adapter->tx_itr_setting = 1;
  1609. /* set default ring sizes */
  1610. adapter->tx_ring_count = IXGBEVF_DEFAULT_TXD;
  1611. adapter->rx_ring_count = IXGBEVF_DEFAULT_RXD;
  1612. set_bit(__IXGBEVF_DOWN, &adapter->state);
  1613. return 0;
  1614. out:
  1615. return err;
  1616. }
  1617. #define UPDATE_VF_COUNTER_32bit(reg, last_counter, counter) \
  1618. { \
  1619. u32 current_counter = IXGBE_READ_REG(hw, reg); \
  1620. if (current_counter < last_counter) \
  1621. counter += 0x100000000LL; \
  1622. last_counter = current_counter; \
  1623. counter &= 0xFFFFFFFF00000000LL; \
  1624. counter |= current_counter; \
  1625. }
  1626. #define UPDATE_VF_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
  1627. { \
  1628. u64 current_counter_lsb = IXGBE_READ_REG(hw, reg_lsb); \
  1629. u64 current_counter_msb = IXGBE_READ_REG(hw, reg_msb); \
  1630. u64 current_counter = (current_counter_msb << 32) | \
  1631. current_counter_lsb; \
  1632. if (current_counter < last_counter) \
  1633. counter += 0x1000000000LL; \
  1634. last_counter = current_counter; \
  1635. counter &= 0xFFFFFFF000000000LL; \
  1636. counter |= current_counter; \
  1637. }
  1638. /**
  1639. * ixgbevf_update_stats - Update the board statistics counters.
  1640. * @adapter: board private structure
  1641. **/
  1642. void ixgbevf_update_stats(struct ixgbevf_adapter *adapter)
  1643. {
  1644. struct ixgbe_hw *hw = &adapter->hw;
  1645. UPDATE_VF_COUNTER_32bit(IXGBE_VFGPRC, adapter->stats.last_vfgprc,
  1646. adapter->stats.vfgprc);
  1647. UPDATE_VF_COUNTER_32bit(IXGBE_VFGPTC, adapter->stats.last_vfgptc,
  1648. adapter->stats.vfgptc);
  1649. UPDATE_VF_COUNTER_36bit(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
  1650. adapter->stats.last_vfgorc,
  1651. adapter->stats.vfgorc);
  1652. UPDATE_VF_COUNTER_36bit(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
  1653. adapter->stats.last_vfgotc,
  1654. adapter->stats.vfgotc);
  1655. UPDATE_VF_COUNTER_32bit(IXGBE_VFMPRC, adapter->stats.last_vfmprc,
  1656. adapter->stats.vfmprc);
  1657. }
  1658. /**
  1659. * ixgbevf_watchdog - Timer Call-back
  1660. * @data: pointer to adapter cast into an unsigned long
  1661. **/
  1662. static void ixgbevf_watchdog(unsigned long data)
  1663. {
  1664. struct ixgbevf_adapter *adapter = (struct ixgbevf_adapter *)data;
  1665. struct ixgbe_hw *hw = &adapter->hw;
  1666. u32 eics = 0;
  1667. int i;
  1668. /*
  1669. * Do the watchdog outside of interrupt context due to the lovely
  1670. * delays that some of the newer hardware requires
  1671. */
  1672. if (test_bit(__IXGBEVF_DOWN, &adapter->state))
  1673. goto watchdog_short_circuit;
  1674. /* get one bit for every active tx/rx interrupt vector */
  1675. for (i = 0; i < adapter->num_msix_vectors - NON_Q_VECTORS; i++) {
  1676. struct ixgbevf_q_vector *qv = adapter->q_vector[i];
  1677. if (qv->rx.ring || qv->tx.ring)
  1678. eics |= 1 << i;
  1679. }
  1680. IXGBE_WRITE_REG(hw, IXGBE_VTEICS, eics);
  1681. watchdog_short_circuit:
  1682. schedule_work(&adapter->watchdog_task);
  1683. }
  1684. /**
  1685. * ixgbevf_tx_timeout - Respond to a Tx Hang
  1686. * @netdev: network interface device structure
  1687. **/
  1688. static void ixgbevf_tx_timeout(struct net_device *netdev)
  1689. {
  1690. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  1691. /* Do the reset outside of interrupt context */
  1692. schedule_work(&adapter->reset_task);
  1693. }
  1694. static void ixgbevf_reset_task(struct work_struct *work)
  1695. {
  1696. struct ixgbevf_adapter *adapter;
  1697. adapter = container_of(work, struct ixgbevf_adapter, reset_task);
  1698. /* If we're already down or resetting, just bail */
  1699. if (test_bit(__IXGBEVF_DOWN, &adapter->state) ||
  1700. test_bit(__IXGBEVF_RESETTING, &adapter->state))
  1701. return;
  1702. adapter->tx_timeout_count++;
  1703. ixgbevf_reinit_locked(adapter);
  1704. }
  1705. /**
  1706. * ixgbevf_watchdog_task - worker thread to bring link up
  1707. * @work: pointer to work_struct containing our data
  1708. **/
  1709. static void ixgbevf_watchdog_task(struct work_struct *work)
  1710. {
  1711. struct ixgbevf_adapter *adapter = container_of(work,
  1712. struct ixgbevf_adapter,
  1713. watchdog_task);
  1714. struct net_device *netdev = adapter->netdev;
  1715. struct ixgbe_hw *hw = &adapter->hw;
  1716. u32 link_speed = adapter->link_speed;
  1717. bool link_up = adapter->link_up;
  1718. adapter->flags |= IXGBE_FLAG_IN_WATCHDOG_TASK;
  1719. /*
  1720. * Always check the link on the watchdog because we have
  1721. * no LSC interrupt
  1722. */
  1723. if (hw->mac.ops.check_link) {
  1724. s32 need_reset;
  1725. spin_lock(&adapter->mbx_lock);
  1726. need_reset = hw->mac.ops.check_link(hw, &link_speed,
  1727. &link_up, false);
  1728. spin_unlock(&adapter->mbx_lock);
  1729. if (need_reset) {
  1730. adapter->link_up = link_up;
  1731. adapter->link_speed = link_speed;
  1732. netif_carrier_off(netdev);
  1733. netif_tx_stop_all_queues(netdev);
  1734. schedule_work(&adapter->reset_task);
  1735. goto pf_has_reset;
  1736. }
  1737. } else {
  1738. /* always assume link is up, if no check link
  1739. * function */
  1740. link_speed = IXGBE_LINK_SPEED_10GB_FULL;
  1741. link_up = true;
  1742. }
  1743. adapter->link_up = link_up;
  1744. adapter->link_speed = link_speed;
  1745. if (link_up) {
  1746. if (!netif_carrier_ok(netdev)) {
  1747. hw_dbg(&adapter->hw, "NIC Link is Up, %u Gbps\n",
  1748. (link_speed == IXGBE_LINK_SPEED_10GB_FULL) ?
  1749. 10 : 1);
  1750. netif_carrier_on(netdev);
  1751. netif_tx_wake_all_queues(netdev);
  1752. }
  1753. } else {
  1754. adapter->link_up = false;
  1755. adapter->link_speed = 0;
  1756. if (netif_carrier_ok(netdev)) {
  1757. hw_dbg(&adapter->hw, "NIC Link is Down\n");
  1758. netif_carrier_off(netdev);
  1759. netif_tx_stop_all_queues(netdev);
  1760. }
  1761. }
  1762. ixgbevf_update_stats(adapter);
  1763. pf_has_reset:
  1764. /* Reset the timer */
  1765. if (!test_bit(__IXGBEVF_DOWN, &adapter->state))
  1766. mod_timer(&adapter->watchdog_timer,
  1767. round_jiffies(jiffies + (2 * HZ)));
  1768. adapter->flags &= ~IXGBE_FLAG_IN_WATCHDOG_TASK;
  1769. }
  1770. /**
  1771. * ixgbevf_free_tx_resources - Free Tx Resources per Queue
  1772. * @adapter: board private structure
  1773. * @tx_ring: Tx descriptor ring for a specific queue
  1774. *
  1775. * Free all transmit software resources
  1776. **/
  1777. void ixgbevf_free_tx_resources(struct ixgbevf_adapter *adapter,
  1778. struct ixgbevf_ring *tx_ring)
  1779. {
  1780. struct pci_dev *pdev = adapter->pdev;
  1781. ixgbevf_clean_tx_ring(adapter, tx_ring);
  1782. vfree(tx_ring->tx_buffer_info);
  1783. tx_ring->tx_buffer_info = NULL;
  1784. dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
  1785. tx_ring->dma);
  1786. tx_ring->desc = NULL;
  1787. }
  1788. /**
  1789. * ixgbevf_free_all_tx_resources - Free Tx Resources for All Queues
  1790. * @adapter: board private structure
  1791. *
  1792. * Free all transmit software resources
  1793. **/
  1794. static void ixgbevf_free_all_tx_resources(struct ixgbevf_adapter *adapter)
  1795. {
  1796. int i;
  1797. for (i = 0; i < adapter->num_tx_queues; i++)
  1798. if (adapter->tx_ring[i].desc)
  1799. ixgbevf_free_tx_resources(adapter,
  1800. &adapter->tx_ring[i]);
  1801. }
  1802. /**
  1803. * ixgbevf_setup_tx_resources - allocate Tx resources (Descriptors)
  1804. * @adapter: board private structure
  1805. * @tx_ring: tx descriptor ring (for a specific queue) to setup
  1806. *
  1807. * Return 0 on success, negative on failure
  1808. **/
  1809. int ixgbevf_setup_tx_resources(struct ixgbevf_adapter *adapter,
  1810. struct ixgbevf_ring *tx_ring)
  1811. {
  1812. struct pci_dev *pdev = adapter->pdev;
  1813. int size;
  1814. size = sizeof(struct ixgbevf_tx_buffer) * tx_ring->count;
  1815. tx_ring->tx_buffer_info = vzalloc(size);
  1816. if (!tx_ring->tx_buffer_info)
  1817. goto err;
  1818. /* round up to nearest 4K */
  1819. tx_ring->size = tx_ring->count * sizeof(union ixgbe_adv_tx_desc);
  1820. tx_ring->size = ALIGN(tx_ring->size, 4096);
  1821. tx_ring->desc = dma_alloc_coherent(&pdev->dev, tx_ring->size,
  1822. &tx_ring->dma, GFP_KERNEL);
  1823. if (!tx_ring->desc)
  1824. goto err;
  1825. tx_ring->next_to_use = 0;
  1826. tx_ring->next_to_clean = 0;
  1827. return 0;
  1828. err:
  1829. vfree(tx_ring->tx_buffer_info);
  1830. tx_ring->tx_buffer_info = NULL;
  1831. hw_dbg(&adapter->hw, "Unable to allocate memory for the transmit "
  1832. "descriptor ring\n");
  1833. return -ENOMEM;
  1834. }
  1835. /**
  1836. * ixgbevf_setup_all_tx_resources - allocate all queues Tx resources
  1837. * @adapter: board private structure
  1838. *
  1839. * If this function returns with an error, then it's possible one or
  1840. * more of the rings is populated (while the rest are not). It is the
  1841. * callers duty to clean those orphaned rings.
  1842. *
  1843. * Return 0 on success, negative on failure
  1844. **/
  1845. static int ixgbevf_setup_all_tx_resources(struct ixgbevf_adapter *adapter)
  1846. {
  1847. int i, err = 0;
  1848. for (i = 0; i < adapter->num_tx_queues; i++) {
  1849. err = ixgbevf_setup_tx_resources(adapter, &adapter->tx_ring[i]);
  1850. if (!err)
  1851. continue;
  1852. hw_dbg(&adapter->hw,
  1853. "Allocation for Tx Queue %u failed\n", i);
  1854. break;
  1855. }
  1856. return err;
  1857. }
  1858. /**
  1859. * ixgbevf_setup_rx_resources - allocate Rx resources (Descriptors)
  1860. * @adapter: board private structure
  1861. * @rx_ring: rx descriptor ring (for a specific queue) to setup
  1862. *
  1863. * Returns 0 on success, negative on failure
  1864. **/
  1865. int ixgbevf_setup_rx_resources(struct ixgbevf_adapter *adapter,
  1866. struct ixgbevf_ring *rx_ring)
  1867. {
  1868. struct pci_dev *pdev = adapter->pdev;
  1869. int size;
  1870. size = sizeof(struct ixgbevf_rx_buffer) * rx_ring->count;
  1871. rx_ring->rx_buffer_info = vzalloc(size);
  1872. if (!rx_ring->rx_buffer_info)
  1873. goto alloc_failed;
  1874. /* Round up to nearest 4K */
  1875. rx_ring->size = rx_ring->count * sizeof(union ixgbe_adv_rx_desc);
  1876. rx_ring->size = ALIGN(rx_ring->size, 4096);
  1877. rx_ring->desc = dma_alloc_coherent(&pdev->dev, rx_ring->size,
  1878. &rx_ring->dma, GFP_KERNEL);
  1879. if (!rx_ring->desc) {
  1880. hw_dbg(&adapter->hw,
  1881. "Unable to allocate memory for "
  1882. "the receive descriptor ring\n");
  1883. vfree(rx_ring->rx_buffer_info);
  1884. rx_ring->rx_buffer_info = NULL;
  1885. goto alloc_failed;
  1886. }
  1887. rx_ring->next_to_clean = 0;
  1888. rx_ring->next_to_use = 0;
  1889. return 0;
  1890. alloc_failed:
  1891. return -ENOMEM;
  1892. }
  1893. /**
  1894. * ixgbevf_setup_all_rx_resources - allocate all queues Rx resources
  1895. * @adapter: board private structure
  1896. *
  1897. * If this function returns with an error, then it's possible one or
  1898. * more of the rings is populated (while the rest are not). It is the
  1899. * callers duty to clean those orphaned rings.
  1900. *
  1901. * Return 0 on success, negative on failure
  1902. **/
  1903. static int ixgbevf_setup_all_rx_resources(struct ixgbevf_adapter *adapter)
  1904. {
  1905. int i, err = 0;
  1906. for (i = 0; i < adapter->num_rx_queues; i++) {
  1907. err = ixgbevf_setup_rx_resources(adapter, &adapter->rx_ring[i]);
  1908. if (!err)
  1909. continue;
  1910. hw_dbg(&adapter->hw,
  1911. "Allocation for Rx Queue %u failed\n", i);
  1912. break;
  1913. }
  1914. return err;
  1915. }
  1916. /**
  1917. * ixgbevf_free_rx_resources - Free Rx Resources
  1918. * @adapter: board private structure
  1919. * @rx_ring: ring to clean the resources from
  1920. *
  1921. * Free all receive software resources
  1922. **/
  1923. void ixgbevf_free_rx_resources(struct ixgbevf_adapter *adapter,
  1924. struct ixgbevf_ring *rx_ring)
  1925. {
  1926. struct pci_dev *pdev = adapter->pdev;
  1927. ixgbevf_clean_rx_ring(adapter, rx_ring);
  1928. vfree(rx_ring->rx_buffer_info);
  1929. rx_ring->rx_buffer_info = NULL;
  1930. dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
  1931. rx_ring->dma);
  1932. rx_ring->desc = NULL;
  1933. }
  1934. /**
  1935. * ixgbevf_free_all_rx_resources - Free Rx Resources for All Queues
  1936. * @adapter: board private structure
  1937. *
  1938. * Free all receive software resources
  1939. **/
  1940. static void ixgbevf_free_all_rx_resources(struct ixgbevf_adapter *adapter)
  1941. {
  1942. int i;
  1943. for (i = 0; i < adapter->num_rx_queues; i++)
  1944. if (adapter->rx_ring[i].desc)
  1945. ixgbevf_free_rx_resources(adapter,
  1946. &adapter->rx_ring[i]);
  1947. }
  1948. /**
  1949. * ixgbevf_open - Called when a network interface is made active
  1950. * @netdev: network interface device structure
  1951. *
  1952. * Returns 0 on success, negative value on failure
  1953. *
  1954. * The open entry point is called when a network interface is made
  1955. * active by the system (IFF_UP). At this point all resources needed
  1956. * for transmit and receive operations are allocated, the interrupt
  1957. * handler is registered with the OS, the watchdog timer is started,
  1958. * and the stack is notified that the interface is ready.
  1959. **/
  1960. static int ixgbevf_open(struct net_device *netdev)
  1961. {
  1962. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  1963. struct ixgbe_hw *hw = &adapter->hw;
  1964. int err;
  1965. /* disallow open during test */
  1966. if (test_bit(__IXGBEVF_TESTING, &adapter->state))
  1967. return -EBUSY;
  1968. if (hw->adapter_stopped) {
  1969. ixgbevf_reset(adapter);
  1970. /* if adapter is still stopped then PF isn't up and
  1971. * the vf can't start. */
  1972. if (hw->adapter_stopped) {
  1973. err = IXGBE_ERR_MBX;
  1974. pr_err("Unable to start - perhaps the PF Driver isn't "
  1975. "up yet\n");
  1976. goto err_setup_reset;
  1977. }
  1978. }
  1979. /* allocate transmit descriptors */
  1980. err = ixgbevf_setup_all_tx_resources(adapter);
  1981. if (err)
  1982. goto err_setup_tx;
  1983. /* allocate receive descriptors */
  1984. err = ixgbevf_setup_all_rx_resources(adapter);
  1985. if (err)
  1986. goto err_setup_rx;
  1987. ixgbevf_configure(adapter);
  1988. /*
  1989. * Map the Tx/Rx rings to the vectors we were allotted.
  1990. * if request_irq will be called in this function map_rings
  1991. * must be called *before* up_complete
  1992. */
  1993. ixgbevf_map_rings_to_vectors(adapter);
  1994. ixgbevf_up_complete(adapter);
  1995. /* clear any pending interrupts, may auto mask */
  1996. IXGBE_READ_REG(hw, IXGBE_VTEICR);
  1997. err = ixgbevf_request_irq(adapter);
  1998. if (err)
  1999. goto err_req_irq;
  2000. ixgbevf_irq_enable(adapter);
  2001. return 0;
  2002. err_req_irq:
  2003. ixgbevf_down(adapter);
  2004. ixgbevf_free_irq(adapter);
  2005. err_setup_rx:
  2006. ixgbevf_free_all_rx_resources(adapter);
  2007. err_setup_tx:
  2008. ixgbevf_free_all_tx_resources(adapter);
  2009. ixgbevf_reset(adapter);
  2010. err_setup_reset:
  2011. return err;
  2012. }
  2013. /**
  2014. * ixgbevf_close - Disables a network interface
  2015. * @netdev: network interface device structure
  2016. *
  2017. * Returns 0, this is not allowed to fail
  2018. *
  2019. * The close entry point is called when an interface is de-activated
  2020. * by the OS. The hardware is still under the drivers control, but
  2021. * needs to be disabled. A global MAC reset is issued to stop the
  2022. * hardware, and all transmit and receive resources are freed.
  2023. **/
  2024. static int ixgbevf_close(struct net_device *netdev)
  2025. {
  2026. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2027. ixgbevf_down(adapter);
  2028. ixgbevf_free_irq(adapter);
  2029. ixgbevf_free_all_tx_resources(adapter);
  2030. ixgbevf_free_all_rx_resources(adapter);
  2031. return 0;
  2032. }
  2033. static void ixgbevf_tx_ctxtdesc(struct ixgbevf_ring *tx_ring,
  2034. u32 vlan_macip_lens, u32 type_tucmd,
  2035. u32 mss_l4len_idx)
  2036. {
  2037. struct ixgbe_adv_tx_context_desc *context_desc;
  2038. u16 i = tx_ring->next_to_use;
  2039. context_desc = IXGBEVF_TX_CTXTDESC(tx_ring, i);
  2040. i++;
  2041. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  2042. /* set bits to identify this as an advanced context descriptor */
  2043. type_tucmd |= IXGBE_TXD_CMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
  2044. context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
  2045. context_desc->seqnum_seed = 0;
  2046. context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
  2047. context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
  2048. }
  2049. static int ixgbevf_tso(struct ixgbevf_ring *tx_ring,
  2050. struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
  2051. {
  2052. u32 vlan_macip_lens, type_tucmd;
  2053. u32 mss_l4len_idx, l4len;
  2054. if (!skb_is_gso(skb))
  2055. return 0;
  2056. if (skb_header_cloned(skb)) {
  2057. int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2058. if (err)
  2059. return err;
  2060. }
  2061. /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
  2062. type_tucmd = IXGBE_ADVTXD_TUCMD_L4T_TCP;
  2063. if (skb->protocol == htons(ETH_P_IP)) {
  2064. struct iphdr *iph = ip_hdr(skb);
  2065. iph->tot_len = 0;
  2066. iph->check = 0;
  2067. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  2068. iph->daddr, 0,
  2069. IPPROTO_TCP,
  2070. 0);
  2071. type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
  2072. } else if (skb_is_gso_v6(skb)) {
  2073. ipv6_hdr(skb)->payload_len = 0;
  2074. tcp_hdr(skb)->check =
  2075. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  2076. &ipv6_hdr(skb)->daddr,
  2077. 0, IPPROTO_TCP, 0);
  2078. }
  2079. /* compute header lengths */
  2080. l4len = tcp_hdrlen(skb);
  2081. *hdr_len += l4len;
  2082. *hdr_len = skb_transport_offset(skb) + l4len;
  2083. /* mss_l4len_id: use 1 as index for TSO */
  2084. mss_l4len_idx = l4len << IXGBE_ADVTXD_L4LEN_SHIFT;
  2085. mss_l4len_idx |= skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
  2086. mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
  2087. /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
  2088. vlan_macip_lens = skb_network_header_len(skb);
  2089. vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
  2090. vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
  2091. ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
  2092. type_tucmd, mss_l4len_idx);
  2093. return 1;
  2094. }
  2095. static bool ixgbevf_tx_csum(struct ixgbevf_ring *tx_ring,
  2096. struct sk_buff *skb, u32 tx_flags)
  2097. {
  2098. u32 vlan_macip_lens = 0;
  2099. u32 mss_l4len_idx = 0;
  2100. u32 type_tucmd = 0;
  2101. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2102. u8 l4_hdr = 0;
  2103. switch (skb->protocol) {
  2104. case __constant_htons(ETH_P_IP):
  2105. vlan_macip_lens |= skb_network_header_len(skb);
  2106. type_tucmd |= IXGBE_ADVTXD_TUCMD_IPV4;
  2107. l4_hdr = ip_hdr(skb)->protocol;
  2108. break;
  2109. case __constant_htons(ETH_P_IPV6):
  2110. vlan_macip_lens |= skb_network_header_len(skb);
  2111. l4_hdr = ipv6_hdr(skb)->nexthdr;
  2112. break;
  2113. default:
  2114. if (unlikely(net_ratelimit())) {
  2115. dev_warn(tx_ring->dev,
  2116. "partial checksum but proto=%x!\n",
  2117. skb->protocol);
  2118. }
  2119. break;
  2120. }
  2121. switch (l4_hdr) {
  2122. case IPPROTO_TCP:
  2123. type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
  2124. mss_l4len_idx = tcp_hdrlen(skb) <<
  2125. IXGBE_ADVTXD_L4LEN_SHIFT;
  2126. break;
  2127. case IPPROTO_SCTP:
  2128. type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_SCTP;
  2129. mss_l4len_idx = sizeof(struct sctphdr) <<
  2130. IXGBE_ADVTXD_L4LEN_SHIFT;
  2131. break;
  2132. case IPPROTO_UDP:
  2133. mss_l4len_idx = sizeof(struct udphdr) <<
  2134. IXGBE_ADVTXD_L4LEN_SHIFT;
  2135. break;
  2136. default:
  2137. if (unlikely(net_ratelimit())) {
  2138. dev_warn(tx_ring->dev,
  2139. "partial checksum but l4 proto=%x!\n",
  2140. l4_hdr);
  2141. }
  2142. break;
  2143. }
  2144. }
  2145. /* vlan_macip_lens: MACLEN, VLAN tag */
  2146. vlan_macip_lens |= skb_network_offset(skb) << IXGBE_ADVTXD_MACLEN_SHIFT;
  2147. vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
  2148. ixgbevf_tx_ctxtdesc(tx_ring, vlan_macip_lens,
  2149. type_tucmd, mss_l4len_idx);
  2150. return (skb->ip_summed == CHECKSUM_PARTIAL);
  2151. }
  2152. static int ixgbevf_tx_map(struct ixgbevf_ring *tx_ring,
  2153. struct sk_buff *skb, u32 tx_flags,
  2154. unsigned int first)
  2155. {
  2156. struct ixgbevf_tx_buffer *tx_buffer_info;
  2157. unsigned int len;
  2158. unsigned int total = skb->len;
  2159. unsigned int offset = 0, size;
  2160. int count = 0;
  2161. unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
  2162. unsigned int f;
  2163. int i;
  2164. i = tx_ring->next_to_use;
  2165. len = min(skb_headlen(skb), total);
  2166. while (len) {
  2167. tx_buffer_info = &tx_ring->tx_buffer_info[i];
  2168. size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
  2169. tx_buffer_info->length = size;
  2170. tx_buffer_info->mapped_as_page = false;
  2171. tx_buffer_info->dma = dma_map_single(tx_ring->dev,
  2172. skb->data + offset,
  2173. size, DMA_TO_DEVICE);
  2174. if (dma_mapping_error(tx_ring->dev, tx_buffer_info->dma))
  2175. goto dma_error;
  2176. tx_buffer_info->next_to_watch = i;
  2177. len -= size;
  2178. total -= size;
  2179. offset += size;
  2180. count++;
  2181. i++;
  2182. if (i == tx_ring->count)
  2183. i = 0;
  2184. }
  2185. for (f = 0; f < nr_frags; f++) {
  2186. const struct skb_frag_struct *frag;
  2187. frag = &skb_shinfo(skb)->frags[f];
  2188. len = min((unsigned int)skb_frag_size(frag), total);
  2189. offset = 0;
  2190. while (len) {
  2191. tx_buffer_info = &tx_ring->tx_buffer_info[i];
  2192. size = min(len, (unsigned int)IXGBE_MAX_DATA_PER_TXD);
  2193. tx_buffer_info->length = size;
  2194. tx_buffer_info->dma =
  2195. skb_frag_dma_map(tx_ring->dev, frag,
  2196. offset, size, DMA_TO_DEVICE);
  2197. tx_buffer_info->mapped_as_page = true;
  2198. if (dma_mapping_error(tx_ring->dev,
  2199. tx_buffer_info->dma))
  2200. goto dma_error;
  2201. tx_buffer_info->next_to_watch = i;
  2202. len -= size;
  2203. total -= size;
  2204. offset += size;
  2205. count++;
  2206. i++;
  2207. if (i == tx_ring->count)
  2208. i = 0;
  2209. }
  2210. if (total == 0)
  2211. break;
  2212. }
  2213. if (i == 0)
  2214. i = tx_ring->count - 1;
  2215. else
  2216. i = i - 1;
  2217. tx_ring->tx_buffer_info[i].skb = skb;
  2218. tx_ring->tx_buffer_info[first].next_to_watch = i;
  2219. tx_ring->tx_buffer_info[first].time_stamp = jiffies;
  2220. return count;
  2221. dma_error:
  2222. dev_err(tx_ring->dev, "TX DMA map failed\n");
  2223. /* clear timestamp and dma mappings for failed tx_buffer_info map */
  2224. tx_buffer_info->dma = 0;
  2225. tx_buffer_info->next_to_watch = 0;
  2226. count--;
  2227. /* clear timestamp and dma mappings for remaining portion of packet */
  2228. while (count >= 0) {
  2229. count--;
  2230. i--;
  2231. if (i < 0)
  2232. i += tx_ring->count;
  2233. tx_buffer_info = &tx_ring->tx_buffer_info[i];
  2234. ixgbevf_unmap_and_free_tx_resource(tx_ring, tx_buffer_info);
  2235. }
  2236. return count;
  2237. }
  2238. static void ixgbevf_tx_queue(struct ixgbevf_ring *tx_ring, int tx_flags,
  2239. int count, u32 paylen, u8 hdr_len)
  2240. {
  2241. union ixgbe_adv_tx_desc *tx_desc = NULL;
  2242. struct ixgbevf_tx_buffer *tx_buffer_info;
  2243. u32 olinfo_status = 0, cmd_type_len = 0;
  2244. unsigned int i;
  2245. u32 txd_cmd = IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS | IXGBE_TXD_CMD_IFCS;
  2246. cmd_type_len |= IXGBE_ADVTXD_DTYP_DATA;
  2247. cmd_type_len |= IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT;
  2248. if (tx_flags & IXGBE_TX_FLAGS_VLAN)
  2249. cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
  2250. if (tx_flags & IXGBE_TX_FLAGS_CSUM)
  2251. olinfo_status |= IXGBE_ADVTXD_POPTS_TXSM;
  2252. if (tx_flags & IXGBE_TX_FLAGS_TSO) {
  2253. cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
  2254. /* use index 1 context for tso */
  2255. olinfo_status |= (1 << IXGBE_ADVTXD_IDX_SHIFT);
  2256. if (tx_flags & IXGBE_TX_FLAGS_IPV4)
  2257. olinfo_status |= IXGBE_ADVTXD_POPTS_IXSM;
  2258. }
  2259. /*
  2260. * Check Context must be set if Tx switch is enabled, which it
  2261. * always is for case where virtual functions are running
  2262. */
  2263. olinfo_status |= IXGBE_ADVTXD_CC;
  2264. olinfo_status |= ((paylen - hdr_len) << IXGBE_ADVTXD_PAYLEN_SHIFT);
  2265. i = tx_ring->next_to_use;
  2266. while (count--) {
  2267. tx_buffer_info = &tx_ring->tx_buffer_info[i];
  2268. tx_desc = IXGBEVF_TX_DESC(tx_ring, i);
  2269. tx_desc->read.buffer_addr = cpu_to_le64(tx_buffer_info->dma);
  2270. tx_desc->read.cmd_type_len =
  2271. cpu_to_le32(cmd_type_len | tx_buffer_info->length);
  2272. tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
  2273. i++;
  2274. if (i == tx_ring->count)
  2275. i = 0;
  2276. }
  2277. tx_desc->read.cmd_type_len |= cpu_to_le32(txd_cmd);
  2278. tx_ring->next_to_use = i;
  2279. }
  2280. static int __ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
  2281. {
  2282. struct ixgbevf_adapter *adapter = netdev_priv(tx_ring->netdev);
  2283. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2284. /* Herbert's original patch had:
  2285. * smp_mb__after_netif_stop_queue();
  2286. * but since that doesn't exist yet, just open code it. */
  2287. smp_mb();
  2288. /* We need to check again in a case another CPU has just
  2289. * made room available. */
  2290. if (likely(IXGBE_DESC_UNUSED(tx_ring) < size))
  2291. return -EBUSY;
  2292. /* A reprieve! - use start_queue because it doesn't call schedule */
  2293. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2294. ++adapter->restart_queue;
  2295. return 0;
  2296. }
  2297. static int ixgbevf_maybe_stop_tx(struct ixgbevf_ring *tx_ring, int size)
  2298. {
  2299. if (likely(IXGBE_DESC_UNUSED(tx_ring) >= size))
  2300. return 0;
  2301. return __ixgbevf_maybe_stop_tx(tx_ring, size);
  2302. }
  2303. static int ixgbevf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  2304. {
  2305. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2306. struct ixgbevf_ring *tx_ring;
  2307. unsigned int first;
  2308. unsigned int tx_flags = 0;
  2309. u8 hdr_len = 0;
  2310. int r_idx = 0, tso;
  2311. u16 count = TXD_USE_COUNT(skb_headlen(skb));
  2312. #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
  2313. unsigned short f;
  2314. #endif
  2315. tx_ring = &adapter->tx_ring[r_idx];
  2316. /*
  2317. * need: 1 descriptor per page * PAGE_SIZE/IXGBE_MAX_DATA_PER_TXD,
  2318. * + 1 desc for skb_headlen/IXGBE_MAX_DATA_PER_TXD,
  2319. * + 2 desc gap to keep tail from touching head,
  2320. * + 1 desc for context descriptor,
  2321. * otherwise try next time
  2322. */
  2323. #if PAGE_SIZE > IXGBE_MAX_DATA_PER_TXD
  2324. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  2325. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
  2326. #else
  2327. count += skb_shinfo(skb)->nr_frags;
  2328. #endif
  2329. if (ixgbevf_maybe_stop_tx(tx_ring, count + 3)) {
  2330. adapter->tx_busy++;
  2331. return NETDEV_TX_BUSY;
  2332. }
  2333. if (vlan_tx_tag_present(skb)) {
  2334. tx_flags |= vlan_tx_tag_get(skb);
  2335. tx_flags <<= IXGBE_TX_FLAGS_VLAN_SHIFT;
  2336. tx_flags |= IXGBE_TX_FLAGS_VLAN;
  2337. }
  2338. first = tx_ring->next_to_use;
  2339. if (skb->protocol == htons(ETH_P_IP))
  2340. tx_flags |= IXGBE_TX_FLAGS_IPV4;
  2341. tso = ixgbevf_tso(tx_ring, skb, tx_flags, &hdr_len);
  2342. if (tso < 0) {
  2343. dev_kfree_skb_any(skb);
  2344. return NETDEV_TX_OK;
  2345. }
  2346. if (tso)
  2347. tx_flags |= IXGBE_TX_FLAGS_TSO | IXGBE_TX_FLAGS_CSUM;
  2348. else if (ixgbevf_tx_csum(tx_ring, skb, tx_flags))
  2349. tx_flags |= IXGBE_TX_FLAGS_CSUM;
  2350. ixgbevf_tx_queue(tx_ring, tx_flags,
  2351. ixgbevf_tx_map(tx_ring, skb, tx_flags, first),
  2352. skb->len, hdr_len);
  2353. /*
  2354. * Force memory writes to complete before letting h/w
  2355. * know there are new descriptors to fetch. (Only
  2356. * applicable for weak-ordered memory model archs,
  2357. * such as IA-64).
  2358. */
  2359. wmb();
  2360. writel(tx_ring->next_to_use, adapter->hw.hw_addr + tx_ring->tail);
  2361. ixgbevf_maybe_stop_tx(tx_ring, DESC_NEEDED);
  2362. return NETDEV_TX_OK;
  2363. }
  2364. /**
  2365. * ixgbevf_set_mac - Change the Ethernet Address of the NIC
  2366. * @netdev: network interface device structure
  2367. * @p: pointer to an address structure
  2368. *
  2369. * Returns 0 on success, negative on failure
  2370. **/
  2371. static int ixgbevf_set_mac(struct net_device *netdev, void *p)
  2372. {
  2373. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2374. struct ixgbe_hw *hw = &adapter->hw;
  2375. struct sockaddr *addr = p;
  2376. if (!is_valid_ether_addr(addr->sa_data))
  2377. return -EADDRNOTAVAIL;
  2378. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  2379. memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
  2380. spin_lock(&adapter->mbx_lock);
  2381. if (hw->mac.ops.set_rar)
  2382. hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0);
  2383. spin_unlock(&adapter->mbx_lock);
  2384. return 0;
  2385. }
  2386. /**
  2387. * ixgbevf_change_mtu - Change the Maximum Transfer Unit
  2388. * @netdev: network interface device structure
  2389. * @new_mtu: new value for maximum frame size
  2390. *
  2391. * Returns 0 on success, negative on failure
  2392. **/
  2393. static int ixgbevf_change_mtu(struct net_device *netdev, int new_mtu)
  2394. {
  2395. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2396. struct ixgbe_hw *hw = &adapter->hw;
  2397. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
  2398. int max_possible_frame = MAXIMUM_ETHERNET_VLAN_SIZE;
  2399. u32 msg[2];
  2400. if (adapter->hw.mac.type == ixgbe_mac_X540_vf)
  2401. max_possible_frame = IXGBE_MAX_JUMBO_FRAME_SIZE;
  2402. /* MTU < 68 is an error and causes problems on some kernels */
  2403. if ((new_mtu < 68) || (max_frame > max_possible_frame))
  2404. return -EINVAL;
  2405. hw_dbg(&adapter->hw, "changing MTU from %d to %d\n",
  2406. netdev->mtu, new_mtu);
  2407. /* must set new MTU before calling down or up */
  2408. netdev->mtu = new_mtu;
  2409. if (!netif_running(netdev)) {
  2410. msg[0] = IXGBE_VF_SET_LPE;
  2411. msg[1] = max_frame;
  2412. hw->mbx.ops.write_posted(hw, msg, 2);
  2413. }
  2414. if (netif_running(netdev))
  2415. ixgbevf_reinit_locked(adapter);
  2416. return 0;
  2417. }
  2418. static void ixgbevf_shutdown(struct pci_dev *pdev)
  2419. {
  2420. struct net_device *netdev = pci_get_drvdata(pdev);
  2421. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2422. netif_device_detach(netdev);
  2423. if (netif_running(netdev)) {
  2424. ixgbevf_down(adapter);
  2425. ixgbevf_free_irq(adapter);
  2426. ixgbevf_free_all_tx_resources(adapter);
  2427. ixgbevf_free_all_rx_resources(adapter);
  2428. }
  2429. pci_save_state(pdev);
  2430. pci_disable_device(pdev);
  2431. }
  2432. static struct rtnl_link_stats64 *ixgbevf_get_stats(struct net_device *netdev,
  2433. struct rtnl_link_stats64 *stats)
  2434. {
  2435. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2436. unsigned int start;
  2437. u64 bytes, packets;
  2438. const struct ixgbevf_ring *ring;
  2439. int i;
  2440. ixgbevf_update_stats(adapter);
  2441. stats->multicast = adapter->stats.vfmprc - adapter->stats.base_vfmprc;
  2442. for (i = 0; i < adapter->num_rx_queues; i++) {
  2443. ring = &adapter->rx_ring[i];
  2444. do {
  2445. start = u64_stats_fetch_begin_bh(&ring->syncp);
  2446. bytes = ring->total_bytes;
  2447. packets = ring->total_packets;
  2448. } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
  2449. stats->rx_bytes += bytes;
  2450. stats->rx_packets += packets;
  2451. }
  2452. for (i = 0; i < adapter->num_tx_queues; i++) {
  2453. ring = &adapter->tx_ring[i];
  2454. do {
  2455. start = u64_stats_fetch_begin_bh(&ring->syncp);
  2456. bytes = ring->total_bytes;
  2457. packets = ring->total_packets;
  2458. } while (u64_stats_fetch_retry_bh(&ring->syncp, start));
  2459. stats->tx_bytes += bytes;
  2460. stats->tx_packets += packets;
  2461. }
  2462. return stats;
  2463. }
  2464. static const struct net_device_ops ixgbe_netdev_ops = {
  2465. .ndo_open = ixgbevf_open,
  2466. .ndo_stop = ixgbevf_close,
  2467. .ndo_start_xmit = ixgbevf_xmit_frame,
  2468. .ndo_set_rx_mode = ixgbevf_set_rx_mode,
  2469. .ndo_get_stats64 = ixgbevf_get_stats,
  2470. .ndo_validate_addr = eth_validate_addr,
  2471. .ndo_set_mac_address = ixgbevf_set_mac,
  2472. .ndo_change_mtu = ixgbevf_change_mtu,
  2473. .ndo_tx_timeout = ixgbevf_tx_timeout,
  2474. .ndo_vlan_rx_add_vid = ixgbevf_vlan_rx_add_vid,
  2475. .ndo_vlan_rx_kill_vid = ixgbevf_vlan_rx_kill_vid,
  2476. };
  2477. static void ixgbevf_assign_netdev_ops(struct net_device *dev)
  2478. {
  2479. dev->netdev_ops = &ixgbe_netdev_ops;
  2480. ixgbevf_set_ethtool_ops(dev);
  2481. dev->watchdog_timeo = 5 * HZ;
  2482. }
  2483. /**
  2484. * ixgbevf_probe - Device Initialization Routine
  2485. * @pdev: PCI device information struct
  2486. * @ent: entry in ixgbevf_pci_tbl
  2487. *
  2488. * Returns 0 on success, negative on failure
  2489. *
  2490. * ixgbevf_probe initializes an adapter identified by a pci_dev structure.
  2491. * The OS initialization, configuring of the adapter private structure,
  2492. * and a hardware reset occur.
  2493. **/
  2494. static int __devinit ixgbevf_probe(struct pci_dev *pdev,
  2495. const struct pci_device_id *ent)
  2496. {
  2497. struct net_device *netdev;
  2498. struct ixgbevf_adapter *adapter = NULL;
  2499. struct ixgbe_hw *hw = NULL;
  2500. const struct ixgbevf_info *ii = ixgbevf_info_tbl[ent->driver_data];
  2501. static int cards_found;
  2502. int err, pci_using_dac;
  2503. err = pci_enable_device(pdev);
  2504. if (err)
  2505. return err;
  2506. if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
  2507. !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
  2508. pci_using_dac = 1;
  2509. } else {
  2510. err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  2511. if (err) {
  2512. err = dma_set_coherent_mask(&pdev->dev,
  2513. DMA_BIT_MASK(32));
  2514. if (err) {
  2515. dev_err(&pdev->dev, "No usable DMA "
  2516. "configuration, aborting\n");
  2517. goto err_dma;
  2518. }
  2519. }
  2520. pci_using_dac = 0;
  2521. }
  2522. err = pci_request_regions(pdev, ixgbevf_driver_name);
  2523. if (err) {
  2524. dev_err(&pdev->dev, "pci_request_regions failed 0x%x\n", err);
  2525. goto err_pci_reg;
  2526. }
  2527. pci_set_master(pdev);
  2528. netdev = alloc_etherdev_mq(sizeof(struct ixgbevf_adapter),
  2529. MAX_TX_QUEUES);
  2530. if (!netdev) {
  2531. err = -ENOMEM;
  2532. goto err_alloc_etherdev;
  2533. }
  2534. SET_NETDEV_DEV(netdev, &pdev->dev);
  2535. pci_set_drvdata(pdev, netdev);
  2536. adapter = netdev_priv(netdev);
  2537. adapter->netdev = netdev;
  2538. adapter->pdev = pdev;
  2539. hw = &adapter->hw;
  2540. hw->back = adapter;
  2541. adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  2542. /*
  2543. * call save state here in standalone driver because it relies on
  2544. * adapter struct to exist, and needs to call netdev_priv
  2545. */
  2546. pci_save_state(pdev);
  2547. hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
  2548. pci_resource_len(pdev, 0));
  2549. if (!hw->hw_addr) {
  2550. err = -EIO;
  2551. goto err_ioremap;
  2552. }
  2553. ixgbevf_assign_netdev_ops(netdev);
  2554. adapter->bd_number = cards_found;
  2555. /* Setup hw api */
  2556. memcpy(&hw->mac.ops, ii->mac_ops, sizeof(hw->mac.ops));
  2557. hw->mac.type = ii->mac;
  2558. memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops,
  2559. sizeof(struct ixgbe_mbx_operations));
  2560. /* setup the private structure */
  2561. err = ixgbevf_sw_init(adapter);
  2562. if (err)
  2563. goto err_sw_init;
  2564. /* The HW MAC address was set and/or determined in sw_init */
  2565. memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
  2566. if (!is_valid_ether_addr(netdev->dev_addr)) {
  2567. pr_err("invalid MAC address\n");
  2568. err = -EIO;
  2569. goto err_sw_init;
  2570. }
  2571. netdev->hw_features = NETIF_F_SG |
  2572. NETIF_F_IP_CSUM |
  2573. NETIF_F_IPV6_CSUM |
  2574. NETIF_F_TSO |
  2575. NETIF_F_TSO6 |
  2576. NETIF_F_RXCSUM;
  2577. netdev->features = netdev->hw_features |
  2578. NETIF_F_HW_VLAN_TX |
  2579. NETIF_F_HW_VLAN_RX |
  2580. NETIF_F_HW_VLAN_FILTER;
  2581. netdev->vlan_features |= NETIF_F_TSO;
  2582. netdev->vlan_features |= NETIF_F_TSO6;
  2583. netdev->vlan_features |= NETIF_F_IP_CSUM;
  2584. netdev->vlan_features |= NETIF_F_IPV6_CSUM;
  2585. netdev->vlan_features |= NETIF_F_SG;
  2586. if (pci_using_dac)
  2587. netdev->features |= NETIF_F_HIGHDMA;
  2588. netdev->priv_flags |= IFF_UNICAST_FLT;
  2589. init_timer(&adapter->watchdog_timer);
  2590. adapter->watchdog_timer.function = ixgbevf_watchdog;
  2591. adapter->watchdog_timer.data = (unsigned long)adapter;
  2592. INIT_WORK(&adapter->reset_task, ixgbevf_reset_task);
  2593. INIT_WORK(&adapter->watchdog_task, ixgbevf_watchdog_task);
  2594. err = ixgbevf_init_interrupt_scheme(adapter);
  2595. if (err)
  2596. goto err_sw_init;
  2597. /* pick up the PCI bus settings for reporting later */
  2598. if (hw->mac.ops.get_bus_info)
  2599. hw->mac.ops.get_bus_info(hw);
  2600. strcpy(netdev->name, "eth%d");
  2601. err = register_netdev(netdev);
  2602. if (err)
  2603. goto err_register;
  2604. netif_carrier_off(netdev);
  2605. ixgbevf_init_last_counter_stats(adapter);
  2606. /* print the MAC address */
  2607. hw_dbg(hw, "%pM\n", netdev->dev_addr);
  2608. hw_dbg(hw, "MAC: %d\n", hw->mac.type);
  2609. hw_dbg(hw, "Intel(R) 82599 Virtual Function\n");
  2610. cards_found++;
  2611. return 0;
  2612. err_register:
  2613. err_sw_init:
  2614. ixgbevf_reset_interrupt_capability(adapter);
  2615. iounmap(hw->hw_addr);
  2616. err_ioremap:
  2617. free_netdev(netdev);
  2618. err_alloc_etherdev:
  2619. pci_release_regions(pdev);
  2620. err_pci_reg:
  2621. err_dma:
  2622. pci_disable_device(pdev);
  2623. return err;
  2624. }
  2625. /**
  2626. * ixgbevf_remove - Device Removal Routine
  2627. * @pdev: PCI device information struct
  2628. *
  2629. * ixgbevf_remove is called by the PCI subsystem to alert the driver
  2630. * that it should release a PCI device. The could be caused by a
  2631. * Hot-Plug event, or because the driver is going to be removed from
  2632. * memory.
  2633. **/
  2634. static void __devexit ixgbevf_remove(struct pci_dev *pdev)
  2635. {
  2636. struct net_device *netdev = pci_get_drvdata(pdev);
  2637. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2638. set_bit(__IXGBEVF_DOWN, &adapter->state);
  2639. del_timer_sync(&adapter->watchdog_timer);
  2640. cancel_work_sync(&adapter->reset_task);
  2641. cancel_work_sync(&adapter->watchdog_task);
  2642. if (netdev->reg_state == NETREG_REGISTERED)
  2643. unregister_netdev(netdev);
  2644. ixgbevf_reset_interrupt_capability(adapter);
  2645. iounmap(adapter->hw.hw_addr);
  2646. pci_release_regions(pdev);
  2647. hw_dbg(&adapter->hw, "Remove complete\n");
  2648. kfree(adapter->tx_ring);
  2649. kfree(adapter->rx_ring);
  2650. free_netdev(netdev);
  2651. pci_disable_device(pdev);
  2652. }
  2653. /**
  2654. * ixgbevf_io_error_detected - called when PCI error is detected
  2655. * @pdev: Pointer to PCI device
  2656. * @state: The current pci connection state
  2657. *
  2658. * This function is called after a PCI bus error affecting
  2659. * this device has been detected.
  2660. */
  2661. static pci_ers_result_t ixgbevf_io_error_detected(struct pci_dev *pdev,
  2662. pci_channel_state_t state)
  2663. {
  2664. struct net_device *netdev = pci_get_drvdata(pdev);
  2665. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2666. netif_device_detach(netdev);
  2667. if (state == pci_channel_io_perm_failure)
  2668. return PCI_ERS_RESULT_DISCONNECT;
  2669. if (netif_running(netdev))
  2670. ixgbevf_down(adapter);
  2671. pci_disable_device(pdev);
  2672. /* Request a slot slot reset. */
  2673. return PCI_ERS_RESULT_NEED_RESET;
  2674. }
  2675. /**
  2676. * ixgbevf_io_slot_reset - called after the pci bus has been reset.
  2677. * @pdev: Pointer to PCI device
  2678. *
  2679. * Restart the card from scratch, as if from a cold-boot. Implementation
  2680. * resembles the first-half of the ixgbevf_resume routine.
  2681. */
  2682. static pci_ers_result_t ixgbevf_io_slot_reset(struct pci_dev *pdev)
  2683. {
  2684. struct net_device *netdev = pci_get_drvdata(pdev);
  2685. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2686. if (pci_enable_device_mem(pdev)) {
  2687. dev_err(&pdev->dev,
  2688. "Cannot re-enable PCI device after reset.\n");
  2689. return PCI_ERS_RESULT_DISCONNECT;
  2690. }
  2691. pci_set_master(pdev);
  2692. ixgbevf_reset(adapter);
  2693. return PCI_ERS_RESULT_RECOVERED;
  2694. }
  2695. /**
  2696. * ixgbevf_io_resume - called when traffic can start flowing again.
  2697. * @pdev: Pointer to PCI device
  2698. *
  2699. * This callback is called when the error recovery driver tells us that
  2700. * its OK to resume normal operation. Implementation resembles the
  2701. * second-half of the ixgbevf_resume routine.
  2702. */
  2703. static void ixgbevf_io_resume(struct pci_dev *pdev)
  2704. {
  2705. struct net_device *netdev = pci_get_drvdata(pdev);
  2706. struct ixgbevf_adapter *adapter = netdev_priv(netdev);
  2707. if (netif_running(netdev))
  2708. ixgbevf_up(adapter);
  2709. netif_device_attach(netdev);
  2710. }
  2711. /* PCI Error Recovery (ERS) */
  2712. static struct pci_error_handlers ixgbevf_err_handler = {
  2713. .error_detected = ixgbevf_io_error_detected,
  2714. .slot_reset = ixgbevf_io_slot_reset,
  2715. .resume = ixgbevf_io_resume,
  2716. };
  2717. static struct pci_driver ixgbevf_driver = {
  2718. .name = ixgbevf_driver_name,
  2719. .id_table = ixgbevf_pci_tbl,
  2720. .probe = ixgbevf_probe,
  2721. .remove = __devexit_p(ixgbevf_remove),
  2722. .shutdown = ixgbevf_shutdown,
  2723. .err_handler = &ixgbevf_err_handler
  2724. };
  2725. /**
  2726. * ixgbevf_init_module - Driver Registration Routine
  2727. *
  2728. * ixgbevf_init_module is the first routine called when the driver is
  2729. * loaded. All it does is register with the PCI subsystem.
  2730. **/
  2731. static int __init ixgbevf_init_module(void)
  2732. {
  2733. int ret;
  2734. pr_info("%s - version %s\n", ixgbevf_driver_string,
  2735. ixgbevf_driver_version);
  2736. pr_info("%s\n", ixgbevf_copyright);
  2737. ret = pci_register_driver(&ixgbevf_driver);
  2738. return ret;
  2739. }
  2740. module_init(ixgbevf_init_module);
  2741. /**
  2742. * ixgbevf_exit_module - Driver Exit Cleanup Routine
  2743. *
  2744. * ixgbevf_exit_module is called just before the driver is removed
  2745. * from memory.
  2746. **/
  2747. static void __exit ixgbevf_exit_module(void)
  2748. {
  2749. pci_unregister_driver(&ixgbevf_driver);
  2750. }
  2751. #ifdef DEBUG
  2752. /**
  2753. * ixgbevf_get_hw_dev_name - return device name string
  2754. * used by hardware layer to print debugging information
  2755. **/
  2756. char *ixgbevf_get_hw_dev_name(struct ixgbe_hw *hw)
  2757. {
  2758. struct ixgbevf_adapter *adapter = hw->back;
  2759. return adapter->netdev->name;
  2760. }
  2761. #endif
  2762. module_exit(ixgbevf_exit_module);
  2763. /* ixgbevf_main.c */