ixgbe_common.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736
  1. /*******************************************************************************
  2. Intel 10 Gigabit PCI Express Linux 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. #include <linux/pci.h>
  21. #include <linux/delay.h>
  22. #include <linux/sched.h>
  23. #include <linux/netdevice.h>
  24. #include "ixgbe.h"
  25. #include "ixgbe_common.h"
  26. #include "ixgbe_phy.h"
  27. static s32 ixgbe_acquire_eeprom(struct ixgbe_hw *hw);
  28. static s32 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw);
  29. static void ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw);
  30. static s32 ixgbe_ready_eeprom(struct ixgbe_hw *hw);
  31. static void ixgbe_standby_eeprom(struct ixgbe_hw *hw);
  32. static void ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
  33. u16 count);
  34. static u16 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count);
  35. static void ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec);
  36. static void ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec);
  37. static void ixgbe_release_eeprom(struct ixgbe_hw *hw);
  38. static s32 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr);
  39. static s32 ixgbe_poll_eerd_eewr_done(struct ixgbe_hw *hw, u32 ee_reg);
  40. static s32 ixgbe_read_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
  41. u16 words, u16 *data);
  42. static s32 ixgbe_write_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
  43. u16 words, u16 *data);
  44. static s32 ixgbe_detect_eeprom_page_size_generic(struct ixgbe_hw *hw,
  45. u16 offset);
  46. static s32 ixgbe_disable_pcie_master(struct ixgbe_hw *hw);
  47. /**
  48. * ixgbe_device_supports_autoneg_fc - Check if phy supports autoneg flow
  49. * control
  50. * @hw: pointer to hardware structure
  51. *
  52. * There are several phys that do not support autoneg flow control. This
  53. * function check the device id to see if the associated phy supports
  54. * autoneg flow control.
  55. **/
  56. static s32 ixgbe_device_supports_autoneg_fc(struct ixgbe_hw *hw)
  57. {
  58. switch (hw->device_id) {
  59. case IXGBE_DEV_ID_X540T:
  60. case IXGBE_DEV_ID_X540T1:
  61. return 0;
  62. case IXGBE_DEV_ID_82599_T3_LOM:
  63. return 0;
  64. default:
  65. return IXGBE_ERR_FC_NOT_SUPPORTED;
  66. }
  67. }
  68. /**
  69. * ixgbe_setup_fc - Set up flow control
  70. * @hw: pointer to hardware structure
  71. *
  72. * Called at init time to set up flow control.
  73. **/
  74. static s32 ixgbe_setup_fc(struct ixgbe_hw *hw)
  75. {
  76. s32 ret_val = 0;
  77. u32 reg = 0, reg_bp = 0;
  78. u16 reg_cu = 0;
  79. /*
  80. * Validate the requested mode. Strict IEEE mode does not allow
  81. * ixgbe_fc_rx_pause because it will cause us to fail at UNH.
  82. */
  83. if (hw->fc.strict_ieee && hw->fc.requested_mode == ixgbe_fc_rx_pause) {
  84. hw_dbg(hw, "ixgbe_fc_rx_pause not valid in strict IEEE mode\n");
  85. ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
  86. goto out;
  87. }
  88. /*
  89. * 10gig parts do not have a word in the EEPROM to determine the
  90. * default flow control setting, so we explicitly set it to full.
  91. */
  92. if (hw->fc.requested_mode == ixgbe_fc_default)
  93. hw->fc.requested_mode = ixgbe_fc_full;
  94. /*
  95. * Set up the 1G and 10G flow control advertisement registers so the
  96. * HW will be able to do fc autoneg once the cable is plugged in. If
  97. * we link at 10G, the 1G advertisement is harmless and vice versa.
  98. */
  99. switch (hw->phy.media_type) {
  100. case ixgbe_media_type_fiber:
  101. case ixgbe_media_type_backplane:
  102. reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANA);
  103. reg_bp = IXGBE_READ_REG(hw, IXGBE_AUTOC);
  104. break;
  105. case ixgbe_media_type_copper:
  106. hw->phy.ops.read_reg(hw, MDIO_AN_ADVERTISE,
  107. MDIO_MMD_AN, &reg_cu);
  108. break;
  109. default:
  110. break;
  111. }
  112. /*
  113. * The possible values of fc.requested_mode are:
  114. * 0: Flow control is completely disabled
  115. * 1: Rx flow control is enabled (we can receive pause frames,
  116. * but not send pause frames).
  117. * 2: Tx flow control is enabled (we can send pause frames but
  118. * we do not support receiving pause frames).
  119. * 3: Both Rx and Tx flow control (symmetric) are enabled.
  120. * other: Invalid.
  121. */
  122. switch (hw->fc.requested_mode) {
  123. case ixgbe_fc_none:
  124. /* Flow control completely disabled by software override. */
  125. reg &= ~(IXGBE_PCS1GANA_SYM_PAUSE | IXGBE_PCS1GANA_ASM_PAUSE);
  126. if (hw->phy.media_type == ixgbe_media_type_backplane)
  127. reg_bp &= ~(IXGBE_AUTOC_SYM_PAUSE |
  128. IXGBE_AUTOC_ASM_PAUSE);
  129. else if (hw->phy.media_type == ixgbe_media_type_copper)
  130. reg_cu &= ~(IXGBE_TAF_SYM_PAUSE | IXGBE_TAF_ASM_PAUSE);
  131. break;
  132. case ixgbe_fc_tx_pause:
  133. /*
  134. * Tx Flow control is enabled, and Rx Flow control is
  135. * disabled by software override.
  136. */
  137. reg |= IXGBE_PCS1GANA_ASM_PAUSE;
  138. reg &= ~IXGBE_PCS1GANA_SYM_PAUSE;
  139. if (hw->phy.media_type == ixgbe_media_type_backplane) {
  140. reg_bp |= IXGBE_AUTOC_ASM_PAUSE;
  141. reg_bp &= ~IXGBE_AUTOC_SYM_PAUSE;
  142. } else if (hw->phy.media_type == ixgbe_media_type_copper) {
  143. reg_cu |= IXGBE_TAF_ASM_PAUSE;
  144. reg_cu &= ~IXGBE_TAF_SYM_PAUSE;
  145. }
  146. break;
  147. case ixgbe_fc_rx_pause:
  148. /*
  149. * Rx Flow control is enabled and Tx Flow control is
  150. * disabled by software override. Since there really
  151. * isn't a way to advertise that we are capable of RX
  152. * Pause ONLY, we will advertise that we support both
  153. * symmetric and asymmetric Rx PAUSE, as such we fall
  154. * through to the fc_full statement. Later, we will
  155. * disable the adapter's ability to send PAUSE frames.
  156. */
  157. case ixgbe_fc_full:
  158. /* Flow control (both Rx and Tx) is enabled by SW override. */
  159. reg |= IXGBE_PCS1GANA_SYM_PAUSE | IXGBE_PCS1GANA_ASM_PAUSE;
  160. if (hw->phy.media_type == ixgbe_media_type_backplane)
  161. reg_bp |= IXGBE_AUTOC_SYM_PAUSE |
  162. IXGBE_AUTOC_ASM_PAUSE;
  163. else if (hw->phy.media_type == ixgbe_media_type_copper)
  164. reg_cu |= IXGBE_TAF_SYM_PAUSE | IXGBE_TAF_ASM_PAUSE;
  165. break;
  166. default:
  167. hw_dbg(hw, "Flow control param set incorrectly\n");
  168. ret_val = IXGBE_ERR_CONFIG;
  169. goto out;
  170. break;
  171. }
  172. if (hw->mac.type != ixgbe_mac_X540) {
  173. /*
  174. * Enable auto-negotiation between the MAC & PHY;
  175. * the MAC will advertise clause 37 flow control.
  176. */
  177. IXGBE_WRITE_REG(hw, IXGBE_PCS1GANA, reg);
  178. reg = IXGBE_READ_REG(hw, IXGBE_PCS1GLCTL);
  179. /* Disable AN timeout */
  180. if (hw->fc.strict_ieee)
  181. reg &= ~IXGBE_PCS1GLCTL_AN_1G_TIMEOUT_EN;
  182. IXGBE_WRITE_REG(hw, IXGBE_PCS1GLCTL, reg);
  183. hw_dbg(hw, "Set up FC; PCS1GLCTL = 0x%08X\n", reg);
  184. }
  185. /*
  186. * AUTOC restart handles negotiation of 1G and 10G on backplane
  187. * and copper. There is no need to set the PCS1GCTL register.
  188. *
  189. */
  190. if (hw->phy.media_type == ixgbe_media_type_backplane) {
  191. reg_bp |= IXGBE_AUTOC_AN_RESTART;
  192. IXGBE_WRITE_REG(hw, IXGBE_AUTOC, reg_bp);
  193. } else if ((hw->phy.media_type == ixgbe_media_type_copper) &&
  194. (ixgbe_device_supports_autoneg_fc(hw) == 0)) {
  195. hw->phy.ops.write_reg(hw, MDIO_AN_ADVERTISE,
  196. MDIO_MMD_AN, reg_cu);
  197. }
  198. hw_dbg(hw, "Set up FC; IXGBE_AUTOC = 0x%08X\n", reg);
  199. out:
  200. return ret_val;
  201. }
  202. /**
  203. * ixgbe_start_hw_generic - Prepare hardware for Tx/Rx
  204. * @hw: pointer to hardware structure
  205. *
  206. * Starts the hardware by filling the bus info structure and media type, clears
  207. * all on chip counters, initializes receive address registers, multicast
  208. * table, VLAN filter table, calls routine to set up link and flow control
  209. * settings, and leaves transmit and receive units disabled and uninitialized
  210. **/
  211. s32 ixgbe_start_hw_generic(struct ixgbe_hw *hw)
  212. {
  213. u32 ctrl_ext;
  214. /* Set the media type */
  215. hw->phy.media_type = hw->mac.ops.get_media_type(hw);
  216. /* Identify the PHY */
  217. hw->phy.ops.identify(hw);
  218. /* Clear the VLAN filter table */
  219. hw->mac.ops.clear_vfta(hw);
  220. /* Clear statistics registers */
  221. hw->mac.ops.clear_hw_cntrs(hw);
  222. /* Set No Snoop Disable */
  223. ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
  224. ctrl_ext |= IXGBE_CTRL_EXT_NS_DIS;
  225. IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
  226. IXGBE_WRITE_FLUSH(hw);
  227. /* Setup flow control */
  228. ixgbe_setup_fc(hw);
  229. /* Clear adapter stopped flag */
  230. hw->adapter_stopped = false;
  231. return 0;
  232. }
  233. /**
  234. * ixgbe_start_hw_gen2 - Init sequence for common device family
  235. * @hw: pointer to hw structure
  236. *
  237. * Performs the init sequence common to the second generation
  238. * of 10 GbE devices.
  239. * Devices in the second generation:
  240. * 82599
  241. * X540
  242. **/
  243. s32 ixgbe_start_hw_gen2(struct ixgbe_hw *hw)
  244. {
  245. u32 i;
  246. u32 regval;
  247. /* Clear the rate limiters */
  248. for (i = 0; i < hw->mac.max_tx_queues; i++) {
  249. IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, i);
  250. IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, 0);
  251. }
  252. IXGBE_WRITE_FLUSH(hw);
  253. /* Disable relaxed ordering */
  254. for (i = 0; i < hw->mac.max_tx_queues; i++) {
  255. regval = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(i));
  256. regval &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
  257. IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(i), regval);
  258. }
  259. for (i = 0; i < hw->mac.max_rx_queues; i++) {
  260. regval = IXGBE_READ_REG(hw, IXGBE_DCA_RXCTRL(i));
  261. regval &= ~(IXGBE_DCA_RXCTRL_DATA_WRO_EN |
  262. IXGBE_DCA_RXCTRL_HEAD_WRO_EN);
  263. IXGBE_WRITE_REG(hw, IXGBE_DCA_RXCTRL(i), regval);
  264. }
  265. return 0;
  266. }
  267. /**
  268. * ixgbe_init_hw_generic - Generic hardware initialization
  269. * @hw: pointer to hardware structure
  270. *
  271. * Initialize the hardware by resetting the hardware, filling the bus info
  272. * structure and media type, clears all on chip counters, initializes receive
  273. * address registers, multicast table, VLAN filter table, calls routine to set
  274. * up link and flow control settings, and leaves transmit and receive units
  275. * disabled and uninitialized
  276. **/
  277. s32 ixgbe_init_hw_generic(struct ixgbe_hw *hw)
  278. {
  279. s32 status;
  280. /* Reset the hardware */
  281. status = hw->mac.ops.reset_hw(hw);
  282. if (status == 0) {
  283. /* Start the HW */
  284. status = hw->mac.ops.start_hw(hw);
  285. }
  286. return status;
  287. }
  288. /**
  289. * ixgbe_clear_hw_cntrs_generic - Generic clear hardware counters
  290. * @hw: pointer to hardware structure
  291. *
  292. * Clears all hardware statistics counters by reading them from the hardware
  293. * Statistics counters are clear on read.
  294. **/
  295. s32 ixgbe_clear_hw_cntrs_generic(struct ixgbe_hw *hw)
  296. {
  297. u16 i = 0;
  298. IXGBE_READ_REG(hw, IXGBE_CRCERRS);
  299. IXGBE_READ_REG(hw, IXGBE_ILLERRC);
  300. IXGBE_READ_REG(hw, IXGBE_ERRBC);
  301. IXGBE_READ_REG(hw, IXGBE_MSPDC);
  302. for (i = 0; i < 8; i++)
  303. IXGBE_READ_REG(hw, IXGBE_MPC(i));
  304. IXGBE_READ_REG(hw, IXGBE_MLFC);
  305. IXGBE_READ_REG(hw, IXGBE_MRFC);
  306. IXGBE_READ_REG(hw, IXGBE_RLEC);
  307. IXGBE_READ_REG(hw, IXGBE_LXONTXC);
  308. IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
  309. if (hw->mac.type >= ixgbe_mac_82599EB) {
  310. IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
  311. IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
  312. } else {
  313. IXGBE_READ_REG(hw, IXGBE_LXONRXC);
  314. IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
  315. }
  316. for (i = 0; i < 8; i++) {
  317. IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
  318. IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
  319. if (hw->mac.type >= ixgbe_mac_82599EB) {
  320. IXGBE_READ_REG(hw, IXGBE_PXONRXCNT(i));
  321. IXGBE_READ_REG(hw, IXGBE_PXOFFRXCNT(i));
  322. } else {
  323. IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
  324. IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
  325. }
  326. }
  327. if (hw->mac.type >= ixgbe_mac_82599EB)
  328. for (i = 0; i < 8; i++)
  329. IXGBE_READ_REG(hw, IXGBE_PXON2OFFCNT(i));
  330. IXGBE_READ_REG(hw, IXGBE_PRC64);
  331. IXGBE_READ_REG(hw, IXGBE_PRC127);
  332. IXGBE_READ_REG(hw, IXGBE_PRC255);
  333. IXGBE_READ_REG(hw, IXGBE_PRC511);
  334. IXGBE_READ_REG(hw, IXGBE_PRC1023);
  335. IXGBE_READ_REG(hw, IXGBE_PRC1522);
  336. IXGBE_READ_REG(hw, IXGBE_GPRC);
  337. IXGBE_READ_REG(hw, IXGBE_BPRC);
  338. IXGBE_READ_REG(hw, IXGBE_MPRC);
  339. IXGBE_READ_REG(hw, IXGBE_GPTC);
  340. IXGBE_READ_REG(hw, IXGBE_GORCL);
  341. IXGBE_READ_REG(hw, IXGBE_GORCH);
  342. IXGBE_READ_REG(hw, IXGBE_GOTCL);
  343. IXGBE_READ_REG(hw, IXGBE_GOTCH);
  344. if (hw->mac.type == ixgbe_mac_82598EB)
  345. for (i = 0; i < 8; i++)
  346. IXGBE_READ_REG(hw, IXGBE_RNBC(i));
  347. IXGBE_READ_REG(hw, IXGBE_RUC);
  348. IXGBE_READ_REG(hw, IXGBE_RFC);
  349. IXGBE_READ_REG(hw, IXGBE_ROC);
  350. IXGBE_READ_REG(hw, IXGBE_RJC);
  351. IXGBE_READ_REG(hw, IXGBE_MNGPRC);
  352. IXGBE_READ_REG(hw, IXGBE_MNGPDC);
  353. IXGBE_READ_REG(hw, IXGBE_MNGPTC);
  354. IXGBE_READ_REG(hw, IXGBE_TORL);
  355. IXGBE_READ_REG(hw, IXGBE_TORH);
  356. IXGBE_READ_REG(hw, IXGBE_TPR);
  357. IXGBE_READ_REG(hw, IXGBE_TPT);
  358. IXGBE_READ_REG(hw, IXGBE_PTC64);
  359. IXGBE_READ_REG(hw, IXGBE_PTC127);
  360. IXGBE_READ_REG(hw, IXGBE_PTC255);
  361. IXGBE_READ_REG(hw, IXGBE_PTC511);
  362. IXGBE_READ_REG(hw, IXGBE_PTC1023);
  363. IXGBE_READ_REG(hw, IXGBE_PTC1522);
  364. IXGBE_READ_REG(hw, IXGBE_MPTC);
  365. IXGBE_READ_REG(hw, IXGBE_BPTC);
  366. for (i = 0; i < 16; i++) {
  367. IXGBE_READ_REG(hw, IXGBE_QPRC(i));
  368. IXGBE_READ_REG(hw, IXGBE_QPTC(i));
  369. if (hw->mac.type >= ixgbe_mac_82599EB) {
  370. IXGBE_READ_REG(hw, IXGBE_QBRC_L(i));
  371. IXGBE_READ_REG(hw, IXGBE_QBRC_H(i));
  372. IXGBE_READ_REG(hw, IXGBE_QBTC_L(i));
  373. IXGBE_READ_REG(hw, IXGBE_QBTC_H(i));
  374. IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
  375. } else {
  376. IXGBE_READ_REG(hw, IXGBE_QBRC(i));
  377. IXGBE_READ_REG(hw, IXGBE_QBTC(i));
  378. }
  379. }
  380. if (hw->mac.type == ixgbe_mac_X540) {
  381. if (hw->phy.id == 0)
  382. hw->phy.ops.identify(hw);
  383. hw->phy.ops.read_reg(hw, IXGBE_PCRC8ECL, MDIO_MMD_PCS, &i);
  384. hw->phy.ops.read_reg(hw, IXGBE_PCRC8ECH, MDIO_MMD_PCS, &i);
  385. hw->phy.ops.read_reg(hw, IXGBE_LDPCECL, MDIO_MMD_PCS, &i);
  386. hw->phy.ops.read_reg(hw, IXGBE_LDPCECH, MDIO_MMD_PCS, &i);
  387. }
  388. return 0;
  389. }
  390. /**
  391. * ixgbe_read_pba_string_generic - Reads part number string from EEPROM
  392. * @hw: pointer to hardware structure
  393. * @pba_num: stores the part number string from the EEPROM
  394. * @pba_num_size: part number string buffer length
  395. *
  396. * Reads the part number string from the EEPROM.
  397. **/
  398. s32 ixgbe_read_pba_string_generic(struct ixgbe_hw *hw, u8 *pba_num,
  399. u32 pba_num_size)
  400. {
  401. s32 ret_val;
  402. u16 data;
  403. u16 pba_ptr;
  404. u16 offset;
  405. u16 length;
  406. if (pba_num == NULL) {
  407. hw_dbg(hw, "PBA string buffer was null\n");
  408. return IXGBE_ERR_INVALID_ARGUMENT;
  409. }
  410. ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM0_PTR, &data);
  411. if (ret_val) {
  412. hw_dbg(hw, "NVM Read Error\n");
  413. return ret_val;
  414. }
  415. ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM1_PTR, &pba_ptr);
  416. if (ret_val) {
  417. hw_dbg(hw, "NVM Read Error\n");
  418. return ret_val;
  419. }
  420. /*
  421. * if data is not ptr guard the PBA must be in legacy format which
  422. * means pba_ptr is actually our second data word for the PBA number
  423. * and we can decode it into an ascii string
  424. */
  425. if (data != IXGBE_PBANUM_PTR_GUARD) {
  426. hw_dbg(hw, "NVM PBA number is not stored as string\n");
  427. /* we will need 11 characters to store the PBA */
  428. if (pba_num_size < 11) {
  429. hw_dbg(hw, "PBA string buffer too small\n");
  430. return IXGBE_ERR_NO_SPACE;
  431. }
  432. /* extract hex string from data and pba_ptr */
  433. pba_num[0] = (data >> 12) & 0xF;
  434. pba_num[1] = (data >> 8) & 0xF;
  435. pba_num[2] = (data >> 4) & 0xF;
  436. pba_num[3] = data & 0xF;
  437. pba_num[4] = (pba_ptr >> 12) & 0xF;
  438. pba_num[5] = (pba_ptr >> 8) & 0xF;
  439. pba_num[6] = '-';
  440. pba_num[7] = 0;
  441. pba_num[8] = (pba_ptr >> 4) & 0xF;
  442. pba_num[9] = pba_ptr & 0xF;
  443. /* put a null character on the end of our string */
  444. pba_num[10] = '\0';
  445. /* switch all the data but the '-' to hex char */
  446. for (offset = 0; offset < 10; offset++) {
  447. if (pba_num[offset] < 0xA)
  448. pba_num[offset] += '0';
  449. else if (pba_num[offset] < 0x10)
  450. pba_num[offset] += 'A' - 0xA;
  451. }
  452. return 0;
  453. }
  454. ret_val = hw->eeprom.ops.read(hw, pba_ptr, &length);
  455. if (ret_val) {
  456. hw_dbg(hw, "NVM Read Error\n");
  457. return ret_val;
  458. }
  459. if (length == 0xFFFF || length == 0) {
  460. hw_dbg(hw, "NVM PBA number section invalid length\n");
  461. return IXGBE_ERR_PBA_SECTION;
  462. }
  463. /* check if pba_num buffer is big enough */
  464. if (pba_num_size < (((u32)length * 2) - 1)) {
  465. hw_dbg(hw, "PBA string buffer too small\n");
  466. return IXGBE_ERR_NO_SPACE;
  467. }
  468. /* trim pba length from start of string */
  469. pba_ptr++;
  470. length--;
  471. for (offset = 0; offset < length; offset++) {
  472. ret_val = hw->eeprom.ops.read(hw, pba_ptr + offset, &data);
  473. if (ret_val) {
  474. hw_dbg(hw, "NVM Read Error\n");
  475. return ret_val;
  476. }
  477. pba_num[offset * 2] = (u8)(data >> 8);
  478. pba_num[(offset * 2) + 1] = (u8)(data & 0xFF);
  479. }
  480. pba_num[offset * 2] = '\0';
  481. return 0;
  482. }
  483. /**
  484. * ixgbe_get_mac_addr_generic - Generic get MAC address
  485. * @hw: pointer to hardware structure
  486. * @mac_addr: Adapter MAC address
  487. *
  488. * Reads the adapter's MAC address from first Receive Address Register (RAR0)
  489. * A reset of the adapter must be performed prior to calling this function
  490. * in order for the MAC address to have been loaded from the EEPROM into RAR0
  491. **/
  492. s32 ixgbe_get_mac_addr_generic(struct ixgbe_hw *hw, u8 *mac_addr)
  493. {
  494. u32 rar_high;
  495. u32 rar_low;
  496. u16 i;
  497. rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(0));
  498. rar_low = IXGBE_READ_REG(hw, IXGBE_RAL(0));
  499. for (i = 0; i < 4; i++)
  500. mac_addr[i] = (u8)(rar_low >> (i*8));
  501. for (i = 0; i < 2; i++)
  502. mac_addr[i+4] = (u8)(rar_high >> (i*8));
  503. return 0;
  504. }
  505. /**
  506. * ixgbe_get_bus_info_generic - Generic set PCI bus info
  507. * @hw: pointer to hardware structure
  508. *
  509. * Sets the PCI bus info (speed, width, type) within the ixgbe_hw structure
  510. **/
  511. s32 ixgbe_get_bus_info_generic(struct ixgbe_hw *hw)
  512. {
  513. struct ixgbe_adapter *adapter = hw->back;
  514. struct ixgbe_mac_info *mac = &hw->mac;
  515. u16 link_status;
  516. hw->bus.type = ixgbe_bus_type_pci_express;
  517. /* Get the negotiated link width and speed from PCI config space */
  518. pci_read_config_word(adapter->pdev, IXGBE_PCI_LINK_STATUS,
  519. &link_status);
  520. switch (link_status & IXGBE_PCI_LINK_WIDTH) {
  521. case IXGBE_PCI_LINK_WIDTH_1:
  522. hw->bus.width = ixgbe_bus_width_pcie_x1;
  523. break;
  524. case IXGBE_PCI_LINK_WIDTH_2:
  525. hw->bus.width = ixgbe_bus_width_pcie_x2;
  526. break;
  527. case IXGBE_PCI_LINK_WIDTH_4:
  528. hw->bus.width = ixgbe_bus_width_pcie_x4;
  529. break;
  530. case IXGBE_PCI_LINK_WIDTH_8:
  531. hw->bus.width = ixgbe_bus_width_pcie_x8;
  532. break;
  533. default:
  534. hw->bus.width = ixgbe_bus_width_unknown;
  535. break;
  536. }
  537. switch (link_status & IXGBE_PCI_LINK_SPEED) {
  538. case IXGBE_PCI_LINK_SPEED_2500:
  539. hw->bus.speed = ixgbe_bus_speed_2500;
  540. break;
  541. case IXGBE_PCI_LINK_SPEED_5000:
  542. hw->bus.speed = ixgbe_bus_speed_5000;
  543. break;
  544. default:
  545. hw->bus.speed = ixgbe_bus_speed_unknown;
  546. break;
  547. }
  548. mac->ops.set_lan_id(hw);
  549. return 0;
  550. }
  551. /**
  552. * ixgbe_set_lan_id_multi_port_pcie - Set LAN id for PCIe multiple port devices
  553. * @hw: pointer to the HW structure
  554. *
  555. * Determines the LAN function id by reading memory-mapped registers
  556. * and swaps the port value if requested.
  557. **/
  558. void ixgbe_set_lan_id_multi_port_pcie(struct ixgbe_hw *hw)
  559. {
  560. struct ixgbe_bus_info *bus = &hw->bus;
  561. u32 reg;
  562. reg = IXGBE_READ_REG(hw, IXGBE_STATUS);
  563. bus->func = (reg & IXGBE_STATUS_LAN_ID) >> IXGBE_STATUS_LAN_ID_SHIFT;
  564. bus->lan_id = bus->func;
  565. /* check for a port swap */
  566. reg = IXGBE_READ_REG(hw, IXGBE_FACTPS);
  567. if (reg & IXGBE_FACTPS_LFS)
  568. bus->func ^= 0x1;
  569. }
  570. /**
  571. * ixgbe_stop_adapter_generic - Generic stop Tx/Rx units
  572. * @hw: pointer to hardware structure
  573. *
  574. * Sets the adapter_stopped flag within ixgbe_hw struct. Clears interrupts,
  575. * disables transmit and receive units. The adapter_stopped flag is used by
  576. * the shared code and drivers to determine if the adapter is in a stopped
  577. * state and should not touch the hardware.
  578. **/
  579. s32 ixgbe_stop_adapter_generic(struct ixgbe_hw *hw)
  580. {
  581. u32 reg_val;
  582. u16 i;
  583. /*
  584. * Set the adapter_stopped flag so other driver functions stop touching
  585. * the hardware
  586. */
  587. hw->adapter_stopped = true;
  588. /* Disable the receive unit */
  589. IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, 0);
  590. /* Clear interrupt mask to stop interrupts from being generated */
  591. IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_IRQ_CLEAR_MASK);
  592. /* Clear any pending interrupts, flush previous writes */
  593. IXGBE_READ_REG(hw, IXGBE_EICR);
  594. /* Disable the transmit unit. Each queue must be disabled. */
  595. for (i = 0; i < hw->mac.max_tx_queues; i++)
  596. IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(i), IXGBE_TXDCTL_SWFLSH);
  597. /* Disable the receive unit by stopping each queue */
  598. for (i = 0; i < hw->mac.max_rx_queues; i++) {
  599. reg_val = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
  600. reg_val &= ~IXGBE_RXDCTL_ENABLE;
  601. reg_val |= IXGBE_RXDCTL_SWFLSH;
  602. IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), reg_val);
  603. }
  604. /* flush all queues disables */
  605. IXGBE_WRITE_FLUSH(hw);
  606. usleep_range(1000, 2000);
  607. /*
  608. * Prevent the PCI-E bus from from hanging by disabling PCI-E master
  609. * access and verify no pending requests
  610. */
  611. return ixgbe_disable_pcie_master(hw);
  612. }
  613. /**
  614. * ixgbe_led_on_generic - Turns on the software controllable LEDs.
  615. * @hw: pointer to hardware structure
  616. * @index: led number to turn on
  617. **/
  618. s32 ixgbe_led_on_generic(struct ixgbe_hw *hw, u32 index)
  619. {
  620. u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
  621. /* To turn on the LED, set mode to ON. */
  622. led_reg &= ~IXGBE_LED_MODE_MASK(index);
  623. led_reg |= IXGBE_LED_ON << IXGBE_LED_MODE_SHIFT(index);
  624. IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
  625. IXGBE_WRITE_FLUSH(hw);
  626. return 0;
  627. }
  628. /**
  629. * ixgbe_led_off_generic - Turns off the software controllable LEDs.
  630. * @hw: pointer to hardware structure
  631. * @index: led number to turn off
  632. **/
  633. s32 ixgbe_led_off_generic(struct ixgbe_hw *hw, u32 index)
  634. {
  635. u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
  636. /* To turn off the LED, set mode to OFF. */
  637. led_reg &= ~IXGBE_LED_MODE_MASK(index);
  638. led_reg |= IXGBE_LED_OFF << IXGBE_LED_MODE_SHIFT(index);
  639. IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
  640. IXGBE_WRITE_FLUSH(hw);
  641. return 0;
  642. }
  643. /**
  644. * ixgbe_init_eeprom_params_generic - Initialize EEPROM params
  645. * @hw: pointer to hardware structure
  646. *
  647. * Initializes the EEPROM parameters ixgbe_eeprom_info within the
  648. * ixgbe_hw struct in order to set up EEPROM access.
  649. **/
  650. s32 ixgbe_init_eeprom_params_generic(struct ixgbe_hw *hw)
  651. {
  652. struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
  653. u32 eec;
  654. u16 eeprom_size;
  655. if (eeprom->type == ixgbe_eeprom_uninitialized) {
  656. eeprom->type = ixgbe_eeprom_none;
  657. /* Set default semaphore delay to 10ms which is a well
  658. * tested value */
  659. eeprom->semaphore_delay = 10;
  660. /* Clear EEPROM page size, it will be initialized as needed */
  661. eeprom->word_page_size = 0;
  662. /*
  663. * Check for EEPROM present first.
  664. * If not present leave as none
  665. */
  666. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  667. if (eec & IXGBE_EEC_PRES) {
  668. eeprom->type = ixgbe_eeprom_spi;
  669. /*
  670. * SPI EEPROM is assumed here. This code would need to
  671. * change if a future EEPROM is not SPI.
  672. */
  673. eeprom_size = (u16)((eec & IXGBE_EEC_SIZE) >>
  674. IXGBE_EEC_SIZE_SHIFT);
  675. eeprom->word_size = 1 << (eeprom_size +
  676. IXGBE_EEPROM_WORD_SIZE_SHIFT);
  677. }
  678. if (eec & IXGBE_EEC_ADDR_SIZE)
  679. eeprom->address_bits = 16;
  680. else
  681. eeprom->address_bits = 8;
  682. hw_dbg(hw, "Eeprom params: type = %d, size = %d, address bits: "
  683. "%d\n", eeprom->type, eeprom->word_size,
  684. eeprom->address_bits);
  685. }
  686. return 0;
  687. }
  688. /**
  689. * ixgbe_write_eeprom_buffer_bit_bang_generic - Write EEPROM using bit-bang
  690. * @hw: pointer to hardware structure
  691. * @offset: offset within the EEPROM to write
  692. * @words: number of words
  693. * @data: 16 bit word(s) to write to EEPROM
  694. *
  695. * Reads 16 bit word(s) from EEPROM through bit-bang method
  696. **/
  697. s32 ixgbe_write_eeprom_buffer_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
  698. u16 words, u16 *data)
  699. {
  700. s32 status = 0;
  701. u16 i, count;
  702. hw->eeprom.ops.init_params(hw);
  703. if (words == 0) {
  704. status = IXGBE_ERR_INVALID_ARGUMENT;
  705. goto out;
  706. }
  707. if (offset + words > hw->eeprom.word_size) {
  708. status = IXGBE_ERR_EEPROM;
  709. goto out;
  710. }
  711. /*
  712. * The EEPROM page size cannot be queried from the chip. We do lazy
  713. * initialization. It is worth to do that when we write large buffer.
  714. */
  715. if ((hw->eeprom.word_page_size == 0) &&
  716. (words > IXGBE_EEPROM_PAGE_SIZE_MAX))
  717. ixgbe_detect_eeprom_page_size_generic(hw, offset);
  718. /*
  719. * We cannot hold synchronization semaphores for too long
  720. * to avoid other entity starvation. However it is more efficient
  721. * to read in bursts than synchronizing access for each word.
  722. */
  723. for (i = 0; i < words; i += IXGBE_EEPROM_RD_BUFFER_MAX_COUNT) {
  724. count = (words - i) / IXGBE_EEPROM_RD_BUFFER_MAX_COUNT > 0 ?
  725. IXGBE_EEPROM_RD_BUFFER_MAX_COUNT : (words - i);
  726. status = ixgbe_write_eeprom_buffer_bit_bang(hw, offset + i,
  727. count, &data[i]);
  728. if (status != 0)
  729. break;
  730. }
  731. out:
  732. return status;
  733. }
  734. /**
  735. * ixgbe_write_eeprom_buffer_bit_bang - Writes 16 bit word(s) to EEPROM
  736. * @hw: pointer to hardware structure
  737. * @offset: offset within the EEPROM to be written to
  738. * @words: number of word(s)
  739. * @data: 16 bit word(s) to be written to the EEPROM
  740. *
  741. * If ixgbe_eeprom_update_checksum is not called after this function, the
  742. * EEPROM will most likely contain an invalid checksum.
  743. **/
  744. static s32 ixgbe_write_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
  745. u16 words, u16 *data)
  746. {
  747. s32 status;
  748. u16 word;
  749. u16 page_size;
  750. u16 i;
  751. u8 write_opcode = IXGBE_EEPROM_WRITE_OPCODE_SPI;
  752. /* Prepare the EEPROM for writing */
  753. status = ixgbe_acquire_eeprom(hw);
  754. if (status == 0) {
  755. if (ixgbe_ready_eeprom(hw) != 0) {
  756. ixgbe_release_eeprom(hw);
  757. status = IXGBE_ERR_EEPROM;
  758. }
  759. }
  760. if (status == 0) {
  761. for (i = 0; i < words; i++) {
  762. ixgbe_standby_eeprom(hw);
  763. /* Send the WRITE ENABLE command (8 bit opcode ) */
  764. ixgbe_shift_out_eeprom_bits(hw,
  765. IXGBE_EEPROM_WREN_OPCODE_SPI,
  766. IXGBE_EEPROM_OPCODE_BITS);
  767. ixgbe_standby_eeprom(hw);
  768. /*
  769. * Some SPI eeproms use the 8th address bit embedded
  770. * in the opcode
  771. */
  772. if ((hw->eeprom.address_bits == 8) &&
  773. ((offset + i) >= 128))
  774. write_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
  775. /* Send the Write command (8-bit opcode + addr) */
  776. ixgbe_shift_out_eeprom_bits(hw, write_opcode,
  777. IXGBE_EEPROM_OPCODE_BITS);
  778. ixgbe_shift_out_eeprom_bits(hw, (u16)((offset + i) * 2),
  779. hw->eeprom.address_bits);
  780. page_size = hw->eeprom.word_page_size;
  781. /* Send the data in burst via SPI*/
  782. do {
  783. word = data[i];
  784. word = (word >> 8) | (word << 8);
  785. ixgbe_shift_out_eeprom_bits(hw, word, 16);
  786. if (page_size == 0)
  787. break;
  788. /* do not wrap around page */
  789. if (((offset + i) & (page_size - 1)) ==
  790. (page_size - 1))
  791. break;
  792. } while (++i < words);
  793. ixgbe_standby_eeprom(hw);
  794. usleep_range(10000, 20000);
  795. }
  796. /* Done with writing - release the EEPROM */
  797. ixgbe_release_eeprom(hw);
  798. }
  799. return status;
  800. }
  801. /**
  802. * ixgbe_write_eeprom_generic - Writes 16 bit value to EEPROM
  803. * @hw: pointer to hardware structure
  804. * @offset: offset within the EEPROM to be written to
  805. * @data: 16 bit word to be written to the EEPROM
  806. *
  807. * If ixgbe_eeprom_update_checksum is not called after this function, the
  808. * EEPROM will most likely contain an invalid checksum.
  809. **/
  810. s32 ixgbe_write_eeprom_generic(struct ixgbe_hw *hw, u16 offset, u16 data)
  811. {
  812. s32 status;
  813. hw->eeprom.ops.init_params(hw);
  814. if (offset >= hw->eeprom.word_size) {
  815. status = IXGBE_ERR_EEPROM;
  816. goto out;
  817. }
  818. status = ixgbe_write_eeprom_buffer_bit_bang(hw, offset, 1, &data);
  819. out:
  820. return status;
  821. }
  822. /**
  823. * ixgbe_read_eeprom_buffer_bit_bang_generic - Read EEPROM using bit-bang
  824. * @hw: pointer to hardware structure
  825. * @offset: offset within the EEPROM to be read
  826. * @words: number of word(s)
  827. * @data: read 16 bit words(s) from EEPROM
  828. *
  829. * Reads 16 bit word(s) from EEPROM through bit-bang method
  830. **/
  831. s32 ixgbe_read_eeprom_buffer_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
  832. u16 words, u16 *data)
  833. {
  834. s32 status = 0;
  835. u16 i, count;
  836. hw->eeprom.ops.init_params(hw);
  837. if (words == 0) {
  838. status = IXGBE_ERR_INVALID_ARGUMENT;
  839. goto out;
  840. }
  841. if (offset + words > hw->eeprom.word_size) {
  842. status = IXGBE_ERR_EEPROM;
  843. goto out;
  844. }
  845. /*
  846. * We cannot hold synchronization semaphores for too long
  847. * to avoid other entity starvation. However it is more efficient
  848. * to read in bursts than synchronizing access for each word.
  849. */
  850. for (i = 0; i < words; i += IXGBE_EEPROM_RD_BUFFER_MAX_COUNT) {
  851. count = (words - i) / IXGBE_EEPROM_RD_BUFFER_MAX_COUNT > 0 ?
  852. IXGBE_EEPROM_RD_BUFFER_MAX_COUNT : (words - i);
  853. status = ixgbe_read_eeprom_buffer_bit_bang(hw, offset + i,
  854. count, &data[i]);
  855. if (status != 0)
  856. break;
  857. }
  858. out:
  859. return status;
  860. }
  861. /**
  862. * ixgbe_read_eeprom_buffer_bit_bang - Read EEPROM using bit-bang
  863. * @hw: pointer to hardware structure
  864. * @offset: offset within the EEPROM to be read
  865. * @words: number of word(s)
  866. * @data: read 16 bit word(s) from EEPROM
  867. *
  868. * Reads 16 bit word(s) from EEPROM through bit-bang method
  869. **/
  870. static s32 ixgbe_read_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
  871. u16 words, u16 *data)
  872. {
  873. s32 status;
  874. u16 word_in;
  875. u8 read_opcode = IXGBE_EEPROM_READ_OPCODE_SPI;
  876. u16 i;
  877. /* Prepare the EEPROM for reading */
  878. status = ixgbe_acquire_eeprom(hw);
  879. if (status == 0) {
  880. if (ixgbe_ready_eeprom(hw) != 0) {
  881. ixgbe_release_eeprom(hw);
  882. status = IXGBE_ERR_EEPROM;
  883. }
  884. }
  885. if (status == 0) {
  886. for (i = 0; i < words; i++) {
  887. ixgbe_standby_eeprom(hw);
  888. /*
  889. * Some SPI eeproms use the 8th address bit embedded
  890. * in the opcode
  891. */
  892. if ((hw->eeprom.address_bits == 8) &&
  893. ((offset + i) >= 128))
  894. read_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
  895. /* Send the READ command (opcode + addr) */
  896. ixgbe_shift_out_eeprom_bits(hw, read_opcode,
  897. IXGBE_EEPROM_OPCODE_BITS);
  898. ixgbe_shift_out_eeprom_bits(hw, (u16)((offset + i) * 2),
  899. hw->eeprom.address_bits);
  900. /* Read the data. */
  901. word_in = ixgbe_shift_in_eeprom_bits(hw, 16);
  902. data[i] = (word_in >> 8) | (word_in << 8);
  903. }
  904. /* End this read operation */
  905. ixgbe_release_eeprom(hw);
  906. }
  907. return status;
  908. }
  909. /**
  910. * ixgbe_read_eeprom_bit_bang_generic - Read EEPROM word using bit-bang
  911. * @hw: pointer to hardware structure
  912. * @offset: offset within the EEPROM to be read
  913. * @data: read 16 bit value from EEPROM
  914. *
  915. * Reads 16 bit value from EEPROM through bit-bang method
  916. **/
  917. s32 ixgbe_read_eeprom_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
  918. u16 *data)
  919. {
  920. s32 status;
  921. hw->eeprom.ops.init_params(hw);
  922. if (offset >= hw->eeprom.word_size) {
  923. status = IXGBE_ERR_EEPROM;
  924. goto out;
  925. }
  926. status = ixgbe_read_eeprom_buffer_bit_bang(hw, offset, 1, data);
  927. out:
  928. return status;
  929. }
  930. /**
  931. * ixgbe_read_eerd_buffer_generic - Read EEPROM word(s) using EERD
  932. * @hw: pointer to hardware structure
  933. * @offset: offset of word in the EEPROM to read
  934. * @words: number of word(s)
  935. * @data: 16 bit word(s) from the EEPROM
  936. *
  937. * Reads a 16 bit word(s) from the EEPROM using the EERD register.
  938. **/
  939. s32 ixgbe_read_eerd_buffer_generic(struct ixgbe_hw *hw, u16 offset,
  940. u16 words, u16 *data)
  941. {
  942. u32 eerd;
  943. s32 status = 0;
  944. u32 i;
  945. hw->eeprom.ops.init_params(hw);
  946. if (words == 0) {
  947. status = IXGBE_ERR_INVALID_ARGUMENT;
  948. goto out;
  949. }
  950. if (offset >= hw->eeprom.word_size) {
  951. status = IXGBE_ERR_EEPROM;
  952. goto out;
  953. }
  954. for (i = 0; i < words; i++) {
  955. eerd = ((offset + i) << IXGBE_EEPROM_RW_ADDR_SHIFT) +
  956. IXGBE_EEPROM_RW_REG_START;
  957. IXGBE_WRITE_REG(hw, IXGBE_EERD, eerd);
  958. status = ixgbe_poll_eerd_eewr_done(hw, IXGBE_NVM_POLL_READ);
  959. if (status == 0) {
  960. data[i] = (IXGBE_READ_REG(hw, IXGBE_EERD) >>
  961. IXGBE_EEPROM_RW_REG_DATA);
  962. } else {
  963. hw_dbg(hw, "Eeprom read timed out\n");
  964. goto out;
  965. }
  966. }
  967. out:
  968. return status;
  969. }
  970. /**
  971. * ixgbe_detect_eeprom_page_size_generic - Detect EEPROM page size
  972. * @hw: pointer to hardware structure
  973. * @offset: offset within the EEPROM to be used as a scratch pad
  974. *
  975. * Discover EEPROM page size by writing marching data at given offset.
  976. * This function is called only when we are writing a new large buffer
  977. * at given offset so the data would be overwritten anyway.
  978. **/
  979. static s32 ixgbe_detect_eeprom_page_size_generic(struct ixgbe_hw *hw,
  980. u16 offset)
  981. {
  982. u16 data[IXGBE_EEPROM_PAGE_SIZE_MAX];
  983. s32 status = 0;
  984. u16 i;
  985. for (i = 0; i < IXGBE_EEPROM_PAGE_SIZE_MAX; i++)
  986. data[i] = i;
  987. hw->eeprom.word_page_size = IXGBE_EEPROM_PAGE_SIZE_MAX;
  988. status = ixgbe_write_eeprom_buffer_bit_bang(hw, offset,
  989. IXGBE_EEPROM_PAGE_SIZE_MAX, data);
  990. hw->eeprom.word_page_size = 0;
  991. if (status != 0)
  992. goto out;
  993. status = ixgbe_read_eeprom_buffer_bit_bang(hw, offset, 1, data);
  994. if (status != 0)
  995. goto out;
  996. /*
  997. * When writing in burst more than the actual page size
  998. * EEPROM address wraps around current page.
  999. */
  1000. hw->eeprom.word_page_size = IXGBE_EEPROM_PAGE_SIZE_MAX - data[0];
  1001. hw_dbg(hw, "Detected EEPROM page size = %d words.",
  1002. hw->eeprom.word_page_size);
  1003. out:
  1004. return status;
  1005. }
  1006. /**
  1007. * ixgbe_read_eerd_generic - Read EEPROM word using EERD
  1008. * @hw: pointer to hardware structure
  1009. * @offset: offset of word in the EEPROM to read
  1010. * @data: word read from the EEPROM
  1011. *
  1012. * Reads a 16 bit word from the EEPROM using the EERD register.
  1013. **/
  1014. s32 ixgbe_read_eerd_generic(struct ixgbe_hw *hw, u16 offset, u16 *data)
  1015. {
  1016. return ixgbe_read_eerd_buffer_generic(hw, offset, 1, data);
  1017. }
  1018. /**
  1019. * ixgbe_write_eewr_buffer_generic - Write EEPROM word(s) using EEWR
  1020. * @hw: pointer to hardware structure
  1021. * @offset: offset of word in the EEPROM to write
  1022. * @words: number of words
  1023. * @data: word(s) write to the EEPROM
  1024. *
  1025. * Write a 16 bit word(s) to the EEPROM using the EEWR register.
  1026. **/
  1027. s32 ixgbe_write_eewr_buffer_generic(struct ixgbe_hw *hw, u16 offset,
  1028. u16 words, u16 *data)
  1029. {
  1030. u32 eewr;
  1031. s32 status = 0;
  1032. u16 i;
  1033. hw->eeprom.ops.init_params(hw);
  1034. if (words == 0) {
  1035. status = IXGBE_ERR_INVALID_ARGUMENT;
  1036. goto out;
  1037. }
  1038. if (offset >= hw->eeprom.word_size) {
  1039. status = IXGBE_ERR_EEPROM;
  1040. goto out;
  1041. }
  1042. for (i = 0; i < words; i++) {
  1043. eewr = ((offset + i) << IXGBE_EEPROM_RW_ADDR_SHIFT) |
  1044. (data[i] << IXGBE_EEPROM_RW_REG_DATA) |
  1045. IXGBE_EEPROM_RW_REG_START;
  1046. status = ixgbe_poll_eerd_eewr_done(hw, IXGBE_NVM_POLL_WRITE);
  1047. if (status != 0) {
  1048. hw_dbg(hw, "Eeprom write EEWR timed out\n");
  1049. goto out;
  1050. }
  1051. IXGBE_WRITE_REG(hw, IXGBE_EEWR, eewr);
  1052. status = ixgbe_poll_eerd_eewr_done(hw, IXGBE_NVM_POLL_WRITE);
  1053. if (status != 0) {
  1054. hw_dbg(hw, "Eeprom write EEWR timed out\n");
  1055. goto out;
  1056. }
  1057. }
  1058. out:
  1059. return status;
  1060. }
  1061. /**
  1062. * ixgbe_write_eewr_generic - Write EEPROM word using EEWR
  1063. * @hw: pointer to hardware structure
  1064. * @offset: offset of word in the EEPROM to write
  1065. * @data: word write to the EEPROM
  1066. *
  1067. * Write a 16 bit word to the EEPROM using the EEWR register.
  1068. **/
  1069. s32 ixgbe_write_eewr_generic(struct ixgbe_hw *hw, u16 offset, u16 data)
  1070. {
  1071. return ixgbe_write_eewr_buffer_generic(hw, offset, 1, &data);
  1072. }
  1073. /**
  1074. * ixgbe_poll_eerd_eewr_done - Poll EERD read or EEWR write status
  1075. * @hw: pointer to hardware structure
  1076. * @ee_reg: EEPROM flag for polling
  1077. *
  1078. * Polls the status bit (bit 1) of the EERD or EEWR to determine when the
  1079. * read or write is done respectively.
  1080. **/
  1081. static s32 ixgbe_poll_eerd_eewr_done(struct ixgbe_hw *hw, u32 ee_reg)
  1082. {
  1083. u32 i;
  1084. u32 reg;
  1085. s32 status = IXGBE_ERR_EEPROM;
  1086. for (i = 0; i < IXGBE_EERD_EEWR_ATTEMPTS; i++) {
  1087. if (ee_reg == IXGBE_NVM_POLL_READ)
  1088. reg = IXGBE_READ_REG(hw, IXGBE_EERD);
  1089. else
  1090. reg = IXGBE_READ_REG(hw, IXGBE_EEWR);
  1091. if (reg & IXGBE_EEPROM_RW_REG_DONE) {
  1092. status = 0;
  1093. break;
  1094. }
  1095. udelay(5);
  1096. }
  1097. return status;
  1098. }
  1099. /**
  1100. * ixgbe_acquire_eeprom - Acquire EEPROM using bit-bang
  1101. * @hw: pointer to hardware structure
  1102. *
  1103. * Prepares EEPROM for access using bit-bang method. This function should
  1104. * be called before issuing a command to the EEPROM.
  1105. **/
  1106. static s32 ixgbe_acquire_eeprom(struct ixgbe_hw *hw)
  1107. {
  1108. s32 status = 0;
  1109. u32 eec;
  1110. u32 i;
  1111. if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM) != 0)
  1112. status = IXGBE_ERR_SWFW_SYNC;
  1113. if (status == 0) {
  1114. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  1115. /* Request EEPROM Access */
  1116. eec |= IXGBE_EEC_REQ;
  1117. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  1118. for (i = 0; i < IXGBE_EEPROM_GRANT_ATTEMPTS; i++) {
  1119. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  1120. if (eec & IXGBE_EEC_GNT)
  1121. break;
  1122. udelay(5);
  1123. }
  1124. /* Release if grant not acquired */
  1125. if (!(eec & IXGBE_EEC_GNT)) {
  1126. eec &= ~IXGBE_EEC_REQ;
  1127. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  1128. hw_dbg(hw, "Could not acquire EEPROM grant\n");
  1129. hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
  1130. status = IXGBE_ERR_EEPROM;
  1131. }
  1132. /* Setup EEPROM for Read/Write */
  1133. if (status == 0) {
  1134. /* Clear CS and SK */
  1135. eec &= ~(IXGBE_EEC_CS | IXGBE_EEC_SK);
  1136. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  1137. IXGBE_WRITE_FLUSH(hw);
  1138. udelay(1);
  1139. }
  1140. }
  1141. return status;
  1142. }
  1143. /**
  1144. * ixgbe_get_eeprom_semaphore - Get hardware semaphore
  1145. * @hw: pointer to hardware structure
  1146. *
  1147. * Sets the hardware semaphores so EEPROM access can occur for bit-bang method
  1148. **/
  1149. static s32 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw)
  1150. {
  1151. s32 status = IXGBE_ERR_EEPROM;
  1152. u32 timeout = 2000;
  1153. u32 i;
  1154. u32 swsm;
  1155. /* Get SMBI software semaphore between device drivers first */
  1156. for (i = 0; i < timeout; i++) {
  1157. /*
  1158. * If the SMBI bit is 0 when we read it, then the bit will be
  1159. * set and we have the semaphore
  1160. */
  1161. swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
  1162. if (!(swsm & IXGBE_SWSM_SMBI)) {
  1163. status = 0;
  1164. break;
  1165. }
  1166. udelay(50);
  1167. }
  1168. if (i == timeout) {
  1169. hw_dbg(hw, "Driver can't access the Eeprom - SMBI Semaphore "
  1170. "not granted.\n");
  1171. /*
  1172. * this release is particularly important because our attempts
  1173. * above to get the semaphore may have succeeded, and if there
  1174. * was a timeout, we should unconditionally clear the semaphore
  1175. * bits to free the driver to make progress
  1176. */
  1177. ixgbe_release_eeprom_semaphore(hw);
  1178. udelay(50);
  1179. /*
  1180. * one last try
  1181. * If the SMBI bit is 0 when we read it, then the bit will be
  1182. * set and we have the semaphore
  1183. */
  1184. swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
  1185. if (!(swsm & IXGBE_SWSM_SMBI))
  1186. status = 0;
  1187. }
  1188. /* Now get the semaphore between SW/FW through the SWESMBI bit */
  1189. if (status == 0) {
  1190. for (i = 0; i < timeout; i++) {
  1191. swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
  1192. /* Set the SW EEPROM semaphore bit to request access */
  1193. swsm |= IXGBE_SWSM_SWESMBI;
  1194. IXGBE_WRITE_REG(hw, IXGBE_SWSM, swsm);
  1195. /*
  1196. * If we set the bit successfully then we got the
  1197. * semaphore.
  1198. */
  1199. swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
  1200. if (swsm & IXGBE_SWSM_SWESMBI)
  1201. break;
  1202. udelay(50);
  1203. }
  1204. /*
  1205. * Release semaphores and return error if SW EEPROM semaphore
  1206. * was not granted because we don't have access to the EEPROM
  1207. */
  1208. if (i >= timeout) {
  1209. hw_dbg(hw, "SWESMBI Software EEPROM semaphore "
  1210. "not granted.\n");
  1211. ixgbe_release_eeprom_semaphore(hw);
  1212. status = IXGBE_ERR_EEPROM;
  1213. }
  1214. } else {
  1215. hw_dbg(hw, "Software semaphore SMBI between device drivers "
  1216. "not granted.\n");
  1217. }
  1218. return status;
  1219. }
  1220. /**
  1221. * ixgbe_release_eeprom_semaphore - Release hardware semaphore
  1222. * @hw: pointer to hardware structure
  1223. *
  1224. * This function clears hardware semaphore bits.
  1225. **/
  1226. static void ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw)
  1227. {
  1228. u32 swsm;
  1229. swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
  1230. /* Release both semaphores by writing 0 to the bits SWESMBI and SMBI */
  1231. swsm &= ~(IXGBE_SWSM_SWESMBI | IXGBE_SWSM_SMBI);
  1232. IXGBE_WRITE_REG(hw, IXGBE_SWSM, swsm);
  1233. IXGBE_WRITE_FLUSH(hw);
  1234. }
  1235. /**
  1236. * ixgbe_ready_eeprom - Polls for EEPROM ready
  1237. * @hw: pointer to hardware structure
  1238. **/
  1239. static s32 ixgbe_ready_eeprom(struct ixgbe_hw *hw)
  1240. {
  1241. s32 status = 0;
  1242. u16 i;
  1243. u8 spi_stat_reg;
  1244. /*
  1245. * Read "Status Register" repeatedly until the LSB is cleared. The
  1246. * EEPROM will signal that the command has been completed by clearing
  1247. * bit 0 of the internal status register. If it's not cleared within
  1248. * 5 milliseconds, then error out.
  1249. */
  1250. for (i = 0; i < IXGBE_EEPROM_MAX_RETRY_SPI; i += 5) {
  1251. ixgbe_shift_out_eeprom_bits(hw, IXGBE_EEPROM_RDSR_OPCODE_SPI,
  1252. IXGBE_EEPROM_OPCODE_BITS);
  1253. spi_stat_reg = (u8)ixgbe_shift_in_eeprom_bits(hw, 8);
  1254. if (!(spi_stat_reg & IXGBE_EEPROM_STATUS_RDY_SPI))
  1255. break;
  1256. udelay(5);
  1257. ixgbe_standby_eeprom(hw);
  1258. }
  1259. /*
  1260. * On some parts, SPI write time could vary from 0-20mSec on 3.3V
  1261. * devices (and only 0-5mSec on 5V devices)
  1262. */
  1263. if (i >= IXGBE_EEPROM_MAX_RETRY_SPI) {
  1264. hw_dbg(hw, "SPI EEPROM Status error\n");
  1265. status = IXGBE_ERR_EEPROM;
  1266. }
  1267. return status;
  1268. }
  1269. /**
  1270. * ixgbe_standby_eeprom - Returns EEPROM to a "standby" state
  1271. * @hw: pointer to hardware structure
  1272. **/
  1273. static void ixgbe_standby_eeprom(struct ixgbe_hw *hw)
  1274. {
  1275. u32 eec;
  1276. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  1277. /* Toggle CS to flush commands */
  1278. eec |= IXGBE_EEC_CS;
  1279. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  1280. IXGBE_WRITE_FLUSH(hw);
  1281. udelay(1);
  1282. eec &= ~IXGBE_EEC_CS;
  1283. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  1284. IXGBE_WRITE_FLUSH(hw);
  1285. udelay(1);
  1286. }
  1287. /**
  1288. * ixgbe_shift_out_eeprom_bits - Shift data bits out to the EEPROM.
  1289. * @hw: pointer to hardware structure
  1290. * @data: data to send to the EEPROM
  1291. * @count: number of bits to shift out
  1292. **/
  1293. static void ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
  1294. u16 count)
  1295. {
  1296. u32 eec;
  1297. u32 mask;
  1298. u32 i;
  1299. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  1300. /*
  1301. * Mask is used to shift "count" bits of "data" out to the EEPROM
  1302. * one bit at a time. Determine the starting bit based on count
  1303. */
  1304. mask = 0x01 << (count - 1);
  1305. for (i = 0; i < count; i++) {
  1306. /*
  1307. * A "1" is shifted out to the EEPROM by setting bit "DI" to a
  1308. * "1", and then raising and then lowering the clock (the SK
  1309. * bit controls the clock input to the EEPROM). A "0" is
  1310. * shifted out to the EEPROM by setting "DI" to "0" and then
  1311. * raising and then lowering the clock.
  1312. */
  1313. if (data & mask)
  1314. eec |= IXGBE_EEC_DI;
  1315. else
  1316. eec &= ~IXGBE_EEC_DI;
  1317. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  1318. IXGBE_WRITE_FLUSH(hw);
  1319. udelay(1);
  1320. ixgbe_raise_eeprom_clk(hw, &eec);
  1321. ixgbe_lower_eeprom_clk(hw, &eec);
  1322. /*
  1323. * Shift mask to signify next bit of data to shift in to the
  1324. * EEPROM
  1325. */
  1326. mask = mask >> 1;
  1327. }
  1328. /* We leave the "DI" bit set to "0" when we leave this routine. */
  1329. eec &= ~IXGBE_EEC_DI;
  1330. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  1331. IXGBE_WRITE_FLUSH(hw);
  1332. }
  1333. /**
  1334. * ixgbe_shift_in_eeprom_bits - Shift data bits in from the EEPROM
  1335. * @hw: pointer to hardware structure
  1336. **/
  1337. static u16 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count)
  1338. {
  1339. u32 eec;
  1340. u32 i;
  1341. u16 data = 0;
  1342. /*
  1343. * In order to read a register from the EEPROM, we need to shift
  1344. * 'count' bits in from the EEPROM. Bits are "shifted in" by raising
  1345. * the clock input to the EEPROM (setting the SK bit), and then reading
  1346. * the value of the "DO" bit. During this "shifting in" process the
  1347. * "DI" bit should always be clear.
  1348. */
  1349. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  1350. eec &= ~(IXGBE_EEC_DO | IXGBE_EEC_DI);
  1351. for (i = 0; i < count; i++) {
  1352. data = data << 1;
  1353. ixgbe_raise_eeprom_clk(hw, &eec);
  1354. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  1355. eec &= ~(IXGBE_EEC_DI);
  1356. if (eec & IXGBE_EEC_DO)
  1357. data |= 1;
  1358. ixgbe_lower_eeprom_clk(hw, &eec);
  1359. }
  1360. return data;
  1361. }
  1362. /**
  1363. * ixgbe_raise_eeprom_clk - Raises the EEPROM's clock input.
  1364. * @hw: pointer to hardware structure
  1365. * @eec: EEC register's current value
  1366. **/
  1367. static void ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
  1368. {
  1369. /*
  1370. * Raise the clock input to the EEPROM
  1371. * (setting the SK bit), then delay
  1372. */
  1373. *eec = *eec | IXGBE_EEC_SK;
  1374. IXGBE_WRITE_REG(hw, IXGBE_EEC, *eec);
  1375. IXGBE_WRITE_FLUSH(hw);
  1376. udelay(1);
  1377. }
  1378. /**
  1379. * ixgbe_lower_eeprom_clk - Lowers the EEPROM's clock input.
  1380. * @hw: pointer to hardware structure
  1381. * @eecd: EECD's current value
  1382. **/
  1383. static void ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
  1384. {
  1385. /*
  1386. * Lower the clock input to the EEPROM (clearing the SK bit), then
  1387. * delay
  1388. */
  1389. *eec = *eec & ~IXGBE_EEC_SK;
  1390. IXGBE_WRITE_REG(hw, IXGBE_EEC, *eec);
  1391. IXGBE_WRITE_FLUSH(hw);
  1392. udelay(1);
  1393. }
  1394. /**
  1395. * ixgbe_release_eeprom - Release EEPROM, release semaphores
  1396. * @hw: pointer to hardware structure
  1397. **/
  1398. static void ixgbe_release_eeprom(struct ixgbe_hw *hw)
  1399. {
  1400. u32 eec;
  1401. eec = IXGBE_READ_REG(hw, IXGBE_EEC);
  1402. eec |= IXGBE_EEC_CS; /* Pull CS high */
  1403. eec &= ~IXGBE_EEC_SK; /* Lower SCK */
  1404. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  1405. IXGBE_WRITE_FLUSH(hw);
  1406. udelay(1);
  1407. /* Stop requesting EEPROM access */
  1408. eec &= ~IXGBE_EEC_REQ;
  1409. IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
  1410. hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
  1411. /*
  1412. * Delay before attempt to obtain semaphore again to allow FW
  1413. * access. semaphore_delay is in ms we need us for usleep_range
  1414. */
  1415. usleep_range(hw->eeprom.semaphore_delay * 1000,
  1416. hw->eeprom.semaphore_delay * 2000);
  1417. }
  1418. /**
  1419. * ixgbe_calc_eeprom_checksum_generic - Calculates and returns the checksum
  1420. * @hw: pointer to hardware structure
  1421. **/
  1422. u16 ixgbe_calc_eeprom_checksum_generic(struct ixgbe_hw *hw)
  1423. {
  1424. u16 i;
  1425. u16 j;
  1426. u16 checksum = 0;
  1427. u16 length = 0;
  1428. u16 pointer = 0;
  1429. u16 word = 0;
  1430. /* Include 0x0-0x3F in the checksum */
  1431. for (i = 0; i < IXGBE_EEPROM_CHECKSUM; i++) {
  1432. if (hw->eeprom.ops.read(hw, i, &word) != 0) {
  1433. hw_dbg(hw, "EEPROM read failed\n");
  1434. break;
  1435. }
  1436. checksum += word;
  1437. }
  1438. /* Include all data from pointers except for the fw pointer */
  1439. for (i = IXGBE_PCIE_ANALOG_PTR; i < IXGBE_FW_PTR; i++) {
  1440. hw->eeprom.ops.read(hw, i, &pointer);
  1441. /* Make sure the pointer seems valid */
  1442. if (pointer != 0xFFFF && pointer != 0) {
  1443. hw->eeprom.ops.read(hw, pointer, &length);
  1444. if (length != 0xFFFF && length != 0) {
  1445. for (j = pointer+1; j <= pointer+length; j++) {
  1446. hw->eeprom.ops.read(hw, j, &word);
  1447. checksum += word;
  1448. }
  1449. }
  1450. }
  1451. }
  1452. checksum = (u16)IXGBE_EEPROM_SUM - checksum;
  1453. return checksum;
  1454. }
  1455. /**
  1456. * ixgbe_validate_eeprom_checksum_generic - Validate EEPROM checksum
  1457. * @hw: pointer to hardware structure
  1458. * @checksum_val: calculated checksum
  1459. *
  1460. * Performs checksum calculation and validates the EEPROM checksum. If the
  1461. * caller does not need checksum_val, the value can be NULL.
  1462. **/
  1463. s32 ixgbe_validate_eeprom_checksum_generic(struct ixgbe_hw *hw,
  1464. u16 *checksum_val)
  1465. {
  1466. s32 status;
  1467. u16 checksum;
  1468. u16 read_checksum = 0;
  1469. /*
  1470. * Read the first word from the EEPROM. If this times out or fails, do
  1471. * not continue or we could be in for a very long wait while every
  1472. * EEPROM read fails
  1473. */
  1474. status = hw->eeprom.ops.read(hw, 0, &checksum);
  1475. if (status == 0) {
  1476. checksum = hw->eeprom.ops.calc_checksum(hw);
  1477. hw->eeprom.ops.read(hw, IXGBE_EEPROM_CHECKSUM, &read_checksum);
  1478. /*
  1479. * Verify read checksum from EEPROM is the same as
  1480. * calculated checksum
  1481. */
  1482. if (read_checksum != checksum)
  1483. status = IXGBE_ERR_EEPROM_CHECKSUM;
  1484. /* If the user cares, return the calculated checksum */
  1485. if (checksum_val)
  1486. *checksum_val = checksum;
  1487. } else {
  1488. hw_dbg(hw, "EEPROM read failed\n");
  1489. }
  1490. return status;
  1491. }
  1492. /**
  1493. * ixgbe_update_eeprom_checksum_generic - Updates the EEPROM checksum
  1494. * @hw: pointer to hardware structure
  1495. **/
  1496. s32 ixgbe_update_eeprom_checksum_generic(struct ixgbe_hw *hw)
  1497. {
  1498. s32 status;
  1499. u16 checksum;
  1500. /*
  1501. * Read the first word from the EEPROM. If this times out or fails, do
  1502. * not continue or we could be in for a very long wait while every
  1503. * EEPROM read fails
  1504. */
  1505. status = hw->eeprom.ops.read(hw, 0, &checksum);
  1506. if (status == 0) {
  1507. checksum = hw->eeprom.ops.calc_checksum(hw);
  1508. status = hw->eeprom.ops.write(hw, IXGBE_EEPROM_CHECKSUM,
  1509. checksum);
  1510. } else {
  1511. hw_dbg(hw, "EEPROM read failed\n");
  1512. }
  1513. return status;
  1514. }
  1515. /**
  1516. * ixgbe_validate_mac_addr - Validate MAC address
  1517. * @mac_addr: pointer to MAC address.
  1518. *
  1519. * Tests a MAC address to ensure it is a valid Individual Address
  1520. **/
  1521. s32 ixgbe_validate_mac_addr(u8 *mac_addr)
  1522. {
  1523. s32 status = 0;
  1524. /* Make sure it is not a multicast address */
  1525. if (IXGBE_IS_MULTICAST(mac_addr))
  1526. status = IXGBE_ERR_INVALID_MAC_ADDR;
  1527. /* Not a broadcast address */
  1528. else if (IXGBE_IS_BROADCAST(mac_addr))
  1529. status = IXGBE_ERR_INVALID_MAC_ADDR;
  1530. /* Reject the zero address */
  1531. else if (is_zero_ether_addr(mac_addr))
  1532. status = IXGBE_ERR_INVALID_MAC_ADDR;
  1533. return status;
  1534. }
  1535. /**
  1536. * ixgbe_set_rar_generic - Set Rx address register
  1537. * @hw: pointer to hardware structure
  1538. * @index: Receive address register to write
  1539. * @addr: Address to put into receive address register
  1540. * @vmdq: VMDq "set" or "pool" index
  1541. * @enable_addr: set flag that address is active
  1542. *
  1543. * Puts an ethernet address into a receive address register.
  1544. **/
  1545. s32 ixgbe_set_rar_generic(struct ixgbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
  1546. u32 enable_addr)
  1547. {
  1548. u32 rar_low, rar_high;
  1549. u32 rar_entries = hw->mac.num_rar_entries;
  1550. /* Make sure we are using a valid rar index range */
  1551. if (index >= rar_entries) {
  1552. hw_dbg(hw, "RAR index %d is out of range.\n", index);
  1553. return IXGBE_ERR_INVALID_ARGUMENT;
  1554. }
  1555. /* setup VMDq pool selection before this RAR gets enabled */
  1556. hw->mac.ops.set_vmdq(hw, index, vmdq);
  1557. /*
  1558. * HW expects these in little endian so we reverse the byte
  1559. * order from network order (big endian) to little endian
  1560. */
  1561. rar_low = ((u32)addr[0] |
  1562. ((u32)addr[1] << 8) |
  1563. ((u32)addr[2] << 16) |
  1564. ((u32)addr[3] << 24));
  1565. /*
  1566. * Some parts put the VMDq setting in the extra RAH bits,
  1567. * so save everything except the lower 16 bits that hold part
  1568. * of the address and the address valid bit.
  1569. */
  1570. rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
  1571. rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
  1572. rar_high |= ((u32)addr[4] | ((u32)addr[5] << 8));
  1573. if (enable_addr != 0)
  1574. rar_high |= IXGBE_RAH_AV;
  1575. IXGBE_WRITE_REG(hw, IXGBE_RAL(index), rar_low);
  1576. IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
  1577. return 0;
  1578. }
  1579. /**
  1580. * ixgbe_clear_rar_generic - Remove Rx address register
  1581. * @hw: pointer to hardware structure
  1582. * @index: Receive address register to write
  1583. *
  1584. * Clears an ethernet address from a receive address register.
  1585. **/
  1586. s32 ixgbe_clear_rar_generic(struct ixgbe_hw *hw, u32 index)
  1587. {
  1588. u32 rar_high;
  1589. u32 rar_entries = hw->mac.num_rar_entries;
  1590. /* Make sure we are using a valid rar index range */
  1591. if (index >= rar_entries) {
  1592. hw_dbg(hw, "RAR index %d is out of range.\n", index);
  1593. return IXGBE_ERR_INVALID_ARGUMENT;
  1594. }
  1595. /*
  1596. * Some parts put the VMDq setting in the extra RAH bits,
  1597. * so save everything except the lower 16 bits that hold part
  1598. * of the address and the address valid bit.
  1599. */
  1600. rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
  1601. rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
  1602. IXGBE_WRITE_REG(hw, IXGBE_RAL(index), 0);
  1603. IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
  1604. /* clear VMDq pool/queue selection for this RAR */
  1605. hw->mac.ops.clear_vmdq(hw, index, IXGBE_CLEAR_VMDQ_ALL);
  1606. return 0;
  1607. }
  1608. /**
  1609. * ixgbe_init_rx_addrs_generic - Initializes receive address filters.
  1610. * @hw: pointer to hardware structure
  1611. *
  1612. * Places the MAC address in receive address register 0 and clears the rest
  1613. * of the receive address registers. Clears the multicast table. Assumes
  1614. * the receiver is in reset when the routine is called.
  1615. **/
  1616. s32 ixgbe_init_rx_addrs_generic(struct ixgbe_hw *hw)
  1617. {
  1618. u32 i;
  1619. u32 rar_entries = hw->mac.num_rar_entries;
  1620. /*
  1621. * If the current mac address is valid, assume it is a software override
  1622. * to the permanent address.
  1623. * Otherwise, use the permanent address from the eeprom.
  1624. */
  1625. if (ixgbe_validate_mac_addr(hw->mac.addr) ==
  1626. IXGBE_ERR_INVALID_MAC_ADDR) {
  1627. /* Get the MAC address from the RAR0 for later reference */
  1628. hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
  1629. hw_dbg(hw, " Keeping Current RAR0 Addr =%pM\n", hw->mac.addr);
  1630. } else {
  1631. /* Setup the receive address. */
  1632. hw_dbg(hw, "Overriding MAC Address in RAR[0]\n");
  1633. hw_dbg(hw, " New MAC Addr =%pM\n", hw->mac.addr);
  1634. hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
  1635. /* clear VMDq pool/queue selection for RAR 0 */
  1636. hw->mac.ops.clear_vmdq(hw, 0, IXGBE_CLEAR_VMDQ_ALL);
  1637. }
  1638. hw->addr_ctrl.overflow_promisc = 0;
  1639. hw->addr_ctrl.rar_used_count = 1;
  1640. /* Zero out the other receive addresses. */
  1641. hw_dbg(hw, "Clearing RAR[1-%d]\n", rar_entries - 1);
  1642. for (i = 1; i < rar_entries; i++) {
  1643. IXGBE_WRITE_REG(hw, IXGBE_RAL(i), 0);
  1644. IXGBE_WRITE_REG(hw, IXGBE_RAH(i), 0);
  1645. }
  1646. /* Clear the MTA */
  1647. hw->addr_ctrl.mta_in_use = 0;
  1648. IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
  1649. hw_dbg(hw, " Clearing MTA\n");
  1650. for (i = 0; i < hw->mac.mcft_size; i++)
  1651. IXGBE_WRITE_REG(hw, IXGBE_MTA(i), 0);
  1652. if (hw->mac.ops.init_uta_tables)
  1653. hw->mac.ops.init_uta_tables(hw);
  1654. return 0;
  1655. }
  1656. /**
  1657. * ixgbe_mta_vector - Determines bit-vector in multicast table to set
  1658. * @hw: pointer to hardware structure
  1659. * @mc_addr: the multicast address
  1660. *
  1661. * Extracts the 12 bits, from a multicast address, to determine which
  1662. * bit-vector to set in the multicast table. The hardware uses 12 bits, from
  1663. * incoming rx multicast addresses, to determine the bit-vector to check in
  1664. * the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
  1665. * by the MO field of the MCSTCTRL. The MO field is set during initialization
  1666. * to mc_filter_type.
  1667. **/
  1668. static s32 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr)
  1669. {
  1670. u32 vector = 0;
  1671. switch (hw->mac.mc_filter_type) {
  1672. case 0: /* use bits [47:36] of the address */
  1673. vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
  1674. break;
  1675. case 1: /* use bits [46:35] of the address */
  1676. vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
  1677. break;
  1678. case 2: /* use bits [45:34] of the address */
  1679. vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
  1680. break;
  1681. case 3: /* use bits [43:32] of the address */
  1682. vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
  1683. break;
  1684. default: /* Invalid mc_filter_type */
  1685. hw_dbg(hw, "MC filter type param set incorrectly\n");
  1686. break;
  1687. }
  1688. /* vector can only be 12-bits or boundary will be exceeded */
  1689. vector &= 0xFFF;
  1690. return vector;
  1691. }
  1692. /**
  1693. * ixgbe_set_mta - Set bit-vector in multicast table
  1694. * @hw: pointer to hardware structure
  1695. * @hash_value: Multicast address hash value
  1696. *
  1697. * Sets the bit-vector in the multicast table.
  1698. **/
  1699. static void ixgbe_set_mta(struct ixgbe_hw *hw, u8 *mc_addr)
  1700. {
  1701. u32 vector;
  1702. u32 vector_bit;
  1703. u32 vector_reg;
  1704. hw->addr_ctrl.mta_in_use++;
  1705. vector = ixgbe_mta_vector(hw, mc_addr);
  1706. hw_dbg(hw, " bit-vector = 0x%03X\n", vector);
  1707. /*
  1708. * The MTA is a register array of 128 32-bit registers. It is treated
  1709. * like an array of 4096 bits. We want to set bit
  1710. * BitArray[vector_value]. So we figure out what register the bit is
  1711. * in, read it, OR in the new bit, then write back the new value. The
  1712. * register is determined by the upper 7 bits of the vector value and
  1713. * the bit within that register are determined by the lower 5 bits of
  1714. * the value.
  1715. */
  1716. vector_reg = (vector >> 5) & 0x7F;
  1717. vector_bit = vector & 0x1F;
  1718. hw->mac.mta_shadow[vector_reg] |= (1 << vector_bit);
  1719. }
  1720. /**
  1721. * ixgbe_update_mc_addr_list_generic - Updates MAC list of multicast addresses
  1722. * @hw: pointer to hardware structure
  1723. * @netdev: pointer to net device structure
  1724. *
  1725. * The given list replaces any existing list. Clears the MC addrs from receive
  1726. * address registers and the multicast table. Uses unused receive address
  1727. * registers for the first multicast addresses, and hashes the rest into the
  1728. * multicast table.
  1729. **/
  1730. s32 ixgbe_update_mc_addr_list_generic(struct ixgbe_hw *hw,
  1731. struct net_device *netdev)
  1732. {
  1733. struct netdev_hw_addr *ha;
  1734. u32 i;
  1735. /*
  1736. * Set the new number of MC addresses that we are being requested to
  1737. * use.
  1738. */
  1739. hw->addr_ctrl.num_mc_addrs = netdev_mc_count(netdev);
  1740. hw->addr_ctrl.mta_in_use = 0;
  1741. /* Clear mta_shadow */
  1742. hw_dbg(hw, " Clearing MTA\n");
  1743. memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
  1744. /* Update mta shadow */
  1745. netdev_for_each_mc_addr(ha, netdev) {
  1746. hw_dbg(hw, " Adding the multicast addresses:\n");
  1747. ixgbe_set_mta(hw, ha->addr);
  1748. }
  1749. /* Enable mta */
  1750. for (i = 0; i < hw->mac.mcft_size; i++)
  1751. IXGBE_WRITE_REG_ARRAY(hw, IXGBE_MTA(0), i,
  1752. hw->mac.mta_shadow[i]);
  1753. if (hw->addr_ctrl.mta_in_use > 0)
  1754. IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL,
  1755. IXGBE_MCSTCTRL_MFE | hw->mac.mc_filter_type);
  1756. hw_dbg(hw, "ixgbe_update_mc_addr_list_generic Complete\n");
  1757. return 0;
  1758. }
  1759. /**
  1760. * ixgbe_enable_mc_generic - Enable multicast address in RAR
  1761. * @hw: pointer to hardware structure
  1762. *
  1763. * Enables multicast address in RAR and the use of the multicast hash table.
  1764. **/
  1765. s32 ixgbe_enable_mc_generic(struct ixgbe_hw *hw)
  1766. {
  1767. struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
  1768. if (a->mta_in_use > 0)
  1769. IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, IXGBE_MCSTCTRL_MFE |
  1770. hw->mac.mc_filter_type);
  1771. return 0;
  1772. }
  1773. /**
  1774. * ixgbe_disable_mc_generic - Disable multicast address in RAR
  1775. * @hw: pointer to hardware structure
  1776. *
  1777. * Disables multicast address in RAR and the use of the multicast hash table.
  1778. **/
  1779. s32 ixgbe_disable_mc_generic(struct ixgbe_hw *hw)
  1780. {
  1781. struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
  1782. if (a->mta_in_use > 0)
  1783. IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
  1784. return 0;
  1785. }
  1786. /**
  1787. * ixgbe_fc_enable_generic - Enable flow control
  1788. * @hw: pointer to hardware structure
  1789. *
  1790. * Enable flow control according to the current settings.
  1791. **/
  1792. s32 ixgbe_fc_enable_generic(struct ixgbe_hw *hw)
  1793. {
  1794. s32 ret_val = 0;
  1795. u32 mflcn_reg, fccfg_reg;
  1796. u32 reg;
  1797. u32 fcrtl, fcrth;
  1798. int i;
  1799. /*
  1800. * Validate the water mark configuration for packet buffer 0. Zero
  1801. * water marks indicate that the packet buffer was not configured
  1802. * and the watermarks for packet buffer 0 should always be configured.
  1803. */
  1804. if (!hw->fc.low_water ||
  1805. !hw->fc.high_water[0] ||
  1806. !hw->fc.pause_time) {
  1807. hw_dbg(hw, "Invalid water mark configuration\n");
  1808. ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
  1809. goto out;
  1810. }
  1811. /* Negotiate the fc mode to use */
  1812. ixgbe_fc_autoneg(hw);
  1813. /* Disable any previous flow control settings */
  1814. mflcn_reg = IXGBE_READ_REG(hw, IXGBE_MFLCN);
  1815. mflcn_reg &= ~(IXGBE_MFLCN_RPFCE_MASK | IXGBE_MFLCN_RFCE);
  1816. fccfg_reg = IXGBE_READ_REG(hw, IXGBE_FCCFG);
  1817. fccfg_reg &= ~(IXGBE_FCCFG_TFCE_802_3X | IXGBE_FCCFG_TFCE_PRIORITY);
  1818. /*
  1819. * The possible values of fc.current_mode are:
  1820. * 0: Flow control is completely disabled
  1821. * 1: Rx flow control is enabled (we can receive pause frames,
  1822. * but not send pause frames).
  1823. * 2: Tx flow control is enabled (we can send pause frames but
  1824. * we do not support receiving pause frames).
  1825. * 3: Both Rx and Tx flow control (symmetric) are enabled.
  1826. * other: Invalid.
  1827. */
  1828. switch (hw->fc.current_mode) {
  1829. case ixgbe_fc_none:
  1830. /*
  1831. * Flow control is disabled by software override or autoneg.
  1832. * The code below will actually disable it in the HW.
  1833. */
  1834. break;
  1835. case ixgbe_fc_rx_pause:
  1836. /*
  1837. * Rx Flow control is enabled and Tx Flow control is
  1838. * disabled by software override. Since there really
  1839. * isn't a way to advertise that we are capable of RX
  1840. * Pause ONLY, we will advertise that we support both
  1841. * symmetric and asymmetric Rx PAUSE. Later, we will
  1842. * disable the adapter's ability to send PAUSE frames.
  1843. */
  1844. mflcn_reg |= IXGBE_MFLCN_RFCE;
  1845. break;
  1846. case ixgbe_fc_tx_pause:
  1847. /*
  1848. * Tx Flow control is enabled, and Rx Flow control is
  1849. * disabled by software override.
  1850. */
  1851. fccfg_reg |= IXGBE_FCCFG_TFCE_802_3X;
  1852. break;
  1853. case ixgbe_fc_full:
  1854. /* Flow control (both Rx and Tx) is enabled by SW override. */
  1855. mflcn_reg |= IXGBE_MFLCN_RFCE;
  1856. fccfg_reg |= IXGBE_FCCFG_TFCE_802_3X;
  1857. break;
  1858. default:
  1859. hw_dbg(hw, "Flow control param set incorrectly\n");
  1860. ret_val = IXGBE_ERR_CONFIG;
  1861. goto out;
  1862. break;
  1863. }
  1864. /* Set 802.3x based flow control settings. */
  1865. mflcn_reg |= IXGBE_MFLCN_DPF;
  1866. IXGBE_WRITE_REG(hw, IXGBE_MFLCN, mflcn_reg);
  1867. IXGBE_WRITE_REG(hw, IXGBE_FCCFG, fccfg_reg);
  1868. fcrtl = (hw->fc.low_water << 10) | IXGBE_FCRTL_XONE;
  1869. /* Set up and enable Rx high/low water mark thresholds, enable XON. */
  1870. for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
  1871. if ((hw->fc.current_mode & ixgbe_fc_tx_pause) &&
  1872. hw->fc.high_water[i]) {
  1873. IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(i), fcrtl);
  1874. fcrth = (hw->fc.high_water[i] << 10) | IXGBE_FCRTH_FCEN;
  1875. } else {
  1876. IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(i), 0);
  1877. /*
  1878. * In order to prevent Tx hangs when the internal Tx
  1879. * switch is enabled we must set the high water mark
  1880. * to the maximum FCRTH value. This allows the Tx
  1881. * switch to function even under heavy Rx workloads.
  1882. */
  1883. fcrth = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(i)) - 32;
  1884. }
  1885. IXGBE_WRITE_REG(hw, IXGBE_FCRTH_82599(i), fcrth);
  1886. }
  1887. /* Configure pause time (2 TCs per register) */
  1888. reg = hw->fc.pause_time * 0x00010001;
  1889. for (i = 0; i < (MAX_TRAFFIC_CLASS / 2); i++)
  1890. IXGBE_WRITE_REG(hw, IXGBE_FCTTV(i), reg);
  1891. IXGBE_WRITE_REG(hw, IXGBE_FCRTV, hw->fc.pause_time / 2);
  1892. out:
  1893. return ret_val;
  1894. }
  1895. /**
  1896. * ixgbe_negotiate_fc - Negotiate flow control
  1897. * @hw: pointer to hardware structure
  1898. * @adv_reg: flow control advertised settings
  1899. * @lp_reg: link partner's flow control settings
  1900. * @adv_sym: symmetric pause bit in advertisement
  1901. * @adv_asm: asymmetric pause bit in advertisement
  1902. * @lp_sym: symmetric pause bit in link partner advertisement
  1903. * @lp_asm: asymmetric pause bit in link partner advertisement
  1904. *
  1905. * Find the intersection between advertised settings and link partner's
  1906. * advertised settings
  1907. **/
  1908. static s32 ixgbe_negotiate_fc(struct ixgbe_hw *hw, u32 adv_reg, u32 lp_reg,
  1909. u32 adv_sym, u32 adv_asm, u32 lp_sym, u32 lp_asm)
  1910. {
  1911. if ((!(adv_reg)) || (!(lp_reg)))
  1912. return IXGBE_ERR_FC_NOT_NEGOTIATED;
  1913. if ((adv_reg & adv_sym) && (lp_reg & lp_sym)) {
  1914. /*
  1915. * Now we need to check if the user selected Rx ONLY
  1916. * of pause frames. In this case, we had to advertise
  1917. * FULL flow control because we could not advertise RX
  1918. * ONLY. Hence, we must now check to see if we need to
  1919. * turn OFF the TRANSMISSION of PAUSE frames.
  1920. */
  1921. if (hw->fc.requested_mode == ixgbe_fc_full) {
  1922. hw->fc.current_mode = ixgbe_fc_full;
  1923. hw_dbg(hw, "Flow Control = FULL.\n");
  1924. } else {
  1925. hw->fc.current_mode = ixgbe_fc_rx_pause;
  1926. hw_dbg(hw, "Flow Control=RX PAUSE frames only\n");
  1927. }
  1928. } else if (!(adv_reg & adv_sym) && (adv_reg & adv_asm) &&
  1929. (lp_reg & lp_sym) && (lp_reg & lp_asm)) {
  1930. hw->fc.current_mode = ixgbe_fc_tx_pause;
  1931. hw_dbg(hw, "Flow Control = TX PAUSE frames only.\n");
  1932. } else if ((adv_reg & adv_sym) && (adv_reg & adv_asm) &&
  1933. !(lp_reg & lp_sym) && (lp_reg & lp_asm)) {
  1934. hw->fc.current_mode = ixgbe_fc_rx_pause;
  1935. hw_dbg(hw, "Flow Control = RX PAUSE frames only.\n");
  1936. } else {
  1937. hw->fc.current_mode = ixgbe_fc_none;
  1938. hw_dbg(hw, "Flow Control = NONE.\n");
  1939. }
  1940. return 0;
  1941. }
  1942. /**
  1943. * ixgbe_fc_autoneg_fiber - Enable flow control on 1 gig fiber
  1944. * @hw: pointer to hardware structure
  1945. *
  1946. * Enable flow control according on 1 gig fiber.
  1947. **/
  1948. static s32 ixgbe_fc_autoneg_fiber(struct ixgbe_hw *hw)
  1949. {
  1950. u32 pcs_anadv_reg, pcs_lpab_reg, linkstat;
  1951. s32 ret_val = IXGBE_ERR_FC_NOT_NEGOTIATED;
  1952. /*
  1953. * On multispeed fiber at 1g, bail out if
  1954. * - link is up but AN did not complete, or if
  1955. * - link is up and AN completed but timed out
  1956. */
  1957. linkstat = IXGBE_READ_REG(hw, IXGBE_PCS1GLSTA);
  1958. if ((!!(linkstat & IXGBE_PCS1GLSTA_AN_COMPLETE) == 0) ||
  1959. (!!(linkstat & IXGBE_PCS1GLSTA_AN_TIMED_OUT) == 1))
  1960. goto out;
  1961. pcs_anadv_reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANA);
  1962. pcs_lpab_reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANLP);
  1963. ret_val = ixgbe_negotiate_fc(hw, pcs_anadv_reg,
  1964. pcs_lpab_reg, IXGBE_PCS1GANA_SYM_PAUSE,
  1965. IXGBE_PCS1GANA_ASM_PAUSE,
  1966. IXGBE_PCS1GANA_SYM_PAUSE,
  1967. IXGBE_PCS1GANA_ASM_PAUSE);
  1968. out:
  1969. return ret_val;
  1970. }
  1971. /**
  1972. * ixgbe_fc_autoneg_backplane - Enable flow control IEEE clause 37
  1973. * @hw: pointer to hardware structure
  1974. *
  1975. * Enable flow control according to IEEE clause 37.
  1976. **/
  1977. static s32 ixgbe_fc_autoneg_backplane(struct ixgbe_hw *hw)
  1978. {
  1979. u32 links2, anlp1_reg, autoc_reg, links;
  1980. s32 ret_val = IXGBE_ERR_FC_NOT_NEGOTIATED;
  1981. /*
  1982. * On backplane, bail out if
  1983. * - backplane autoneg was not completed, or if
  1984. * - we are 82599 and link partner is not AN enabled
  1985. */
  1986. links = IXGBE_READ_REG(hw, IXGBE_LINKS);
  1987. if ((links & IXGBE_LINKS_KX_AN_COMP) == 0)
  1988. goto out;
  1989. if (hw->mac.type == ixgbe_mac_82599EB) {
  1990. links2 = IXGBE_READ_REG(hw, IXGBE_LINKS2);
  1991. if ((links2 & IXGBE_LINKS2_AN_SUPPORTED) == 0)
  1992. goto out;
  1993. }
  1994. /*
  1995. * Read the 10g AN autoc and LP ability registers and resolve
  1996. * local flow control settings accordingly
  1997. */
  1998. autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
  1999. anlp1_reg = IXGBE_READ_REG(hw, IXGBE_ANLP1);
  2000. ret_val = ixgbe_negotiate_fc(hw, autoc_reg,
  2001. anlp1_reg, IXGBE_AUTOC_SYM_PAUSE, IXGBE_AUTOC_ASM_PAUSE,
  2002. IXGBE_ANLP1_SYM_PAUSE, IXGBE_ANLP1_ASM_PAUSE);
  2003. out:
  2004. return ret_val;
  2005. }
  2006. /**
  2007. * ixgbe_fc_autoneg_copper - Enable flow control IEEE clause 37
  2008. * @hw: pointer to hardware structure
  2009. *
  2010. * Enable flow control according to IEEE clause 37.
  2011. **/
  2012. static s32 ixgbe_fc_autoneg_copper(struct ixgbe_hw *hw)
  2013. {
  2014. u16 technology_ability_reg = 0;
  2015. u16 lp_technology_ability_reg = 0;
  2016. hw->phy.ops.read_reg(hw, MDIO_AN_ADVERTISE,
  2017. MDIO_MMD_AN,
  2018. &technology_ability_reg);
  2019. hw->phy.ops.read_reg(hw, MDIO_AN_LPA,
  2020. MDIO_MMD_AN,
  2021. &lp_technology_ability_reg);
  2022. return ixgbe_negotiate_fc(hw, (u32)technology_ability_reg,
  2023. (u32)lp_technology_ability_reg,
  2024. IXGBE_TAF_SYM_PAUSE, IXGBE_TAF_ASM_PAUSE,
  2025. IXGBE_TAF_SYM_PAUSE, IXGBE_TAF_ASM_PAUSE);
  2026. }
  2027. /**
  2028. * ixgbe_fc_autoneg - Configure flow control
  2029. * @hw: pointer to hardware structure
  2030. *
  2031. * Compares our advertised flow control capabilities to those advertised by
  2032. * our link partner, and determines the proper flow control mode to use.
  2033. **/
  2034. void ixgbe_fc_autoneg(struct ixgbe_hw *hw)
  2035. {
  2036. s32 ret_val = IXGBE_ERR_FC_NOT_NEGOTIATED;
  2037. ixgbe_link_speed speed;
  2038. bool link_up;
  2039. /*
  2040. * AN should have completed when the cable was plugged in.
  2041. * Look for reasons to bail out. Bail out if:
  2042. * - FC autoneg is disabled, or if
  2043. * - link is not up.
  2044. *
  2045. * Since we're being called from an LSC, link is already known to be up.
  2046. * So use link_up_wait_to_complete=false.
  2047. */
  2048. if (hw->fc.disable_fc_autoneg)
  2049. goto out;
  2050. hw->mac.ops.check_link(hw, &speed, &link_up, false);
  2051. if (!link_up)
  2052. goto out;
  2053. switch (hw->phy.media_type) {
  2054. /* Autoneg flow control on fiber adapters */
  2055. case ixgbe_media_type_fiber:
  2056. if (speed == IXGBE_LINK_SPEED_1GB_FULL)
  2057. ret_val = ixgbe_fc_autoneg_fiber(hw);
  2058. break;
  2059. /* Autoneg flow control on backplane adapters */
  2060. case ixgbe_media_type_backplane:
  2061. ret_val = ixgbe_fc_autoneg_backplane(hw);
  2062. break;
  2063. /* Autoneg flow control on copper adapters */
  2064. case ixgbe_media_type_copper:
  2065. if (ixgbe_device_supports_autoneg_fc(hw) == 0)
  2066. ret_val = ixgbe_fc_autoneg_copper(hw);
  2067. break;
  2068. default:
  2069. break;
  2070. }
  2071. out:
  2072. if (ret_val == 0) {
  2073. hw->fc.fc_was_autonegged = true;
  2074. } else {
  2075. hw->fc.fc_was_autonegged = false;
  2076. hw->fc.current_mode = hw->fc.requested_mode;
  2077. }
  2078. }
  2079. /**
  2080. * ixgbe_disable_pcie_master - Disable PCI-express master access
  2081. * @hw: pointer to hardware structure
  2082. *
  2083. * Disables PCI-Express master access and verifies there are no pending
  2084. * requests. IXGBE_ERR_MASTER_REQUESTS_PENDING is returned if master disable
  2085. * bit hasn't caused the master requests to be disabled, else 0
  2086. * is returned signifying master requests disabled.
  2087. **/
  2088. static s32 ixgbe_disable_pcie_master(struct ixgbe_hw *hw)
  2089. {
  2090. struct ixgbe_adapter *adapter = hw->back;
  2091. s32 status = 0;
  2092. u32 i;
  2093. u16 value;
  2094. /* Always set this bit to ensure any future transactions are blocked */
  2095. IXGBE_WRITE_REG(hw, IXGBE_CTRL, IXGBE_CTRL_GIO_DIS);
  2096. /* Exit if master requests are blocked */
  2097. if (!(IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_GIO))
  2098. goto out;
  2099. /* Poll for master request bit to clear */
  2100. for (i = 0; i < IXGBE_PCI_MASTER_DISABLE_TIMEOUT; i++) {
  2101. udelay(100);
  2102. if (!(IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_GIO))
  2103. goto out;
  2104. }
  2105. /*
  2106. * Two consecutive resets are required via CTRL.RST per datasheet
  2107. * 5.2.5.3.2 Master Disable. We set a flag to inform the reset routine
  2108. * of this need. The first reset prevents new master requests from
  2109. * being issued by our device. We then must wait 1usec or more for any
  2110. * remaining completions from the PCIe bus to trickle in, and then reset
  2111. * again to clear out any effects they may have had on our device.
  2112. */
  2113. hw_dbg(hw, "GIO Master Disable bit didn't clear - requesting resets\n");
  2114. hw->mac.flags |= IXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
  2115. /*
  2116. * Before proceeding, make sure that the PCIe block does not have
  2117. * transactions pending.
  2118. */
  2119. for (i = 0; i < IXGBE_PCI_MASTER_DISABLE_TIMEOUT; i++) {
  2120. udelay(100);
  2121. pci_read_config_word(adapter->pdev, IXGBE_PCI_DEVICE_STATUS,
  2122. &value);
  2123. if (!(value & IXGBE_PCI_DEVICE_STATUS_TRANSACTION_PENDING))
  2124. goto out;
  2125. }
  2126. hw_dbg(hw, "PCIe transaction pending bit also did not clear.\n");
  2127. status = IXGBE_ERR_MASTER_REQUESTS_PENDING;
  2128. out:
  2129. return status;
  2130. }
  2131. /**
  2132. * ixgbe_acquire_swfw_sync - Acquire SWFW semaphore
  2133. * @hw: pointer to hardware structure
  2134. * @mask: Mask to specify which semaphore to acquire
  2135. *
  2136. * Acquires the SWFW semaphore through the GSSR register for the specified
  2137. * function (CSR, PHY0, PHY1, EEPROM, Flash)
  2138. **/
  2139. s32 ixgbe_acquire_swfw_sync(struct ixgbe_hw *hw, u16 mask)
  2140. {
  2141. u32 gssr;
  2142. u32 swmask = mask;
  2143. u32 fwmask = mask << 5;
  2144. s32 timeout = 200;
  2145. while (timeout) {
  2146. /*
  2147. * SW EEPROM semaphore bit is used for access to all
  2148. * SW_FW_SYNC/GSSR bits (not just EEPROM)
  2149. */
  2150. if (ixgbe_get_eeprom_semaphore(hw))
  2151. return IXGBE_ERR_SWFW_SYNC;
  2152. gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
  2153. if (!(gssr & (fwmask | swmask)))
  2154. break;
  2155. /*
  2156. * Firmware currently using resource (fwmask) or other software
  2157. * thread currently using resource (swmask)
  2158. */
  2159. ixgbe_release_eeprom_semaphore(hw);
  2160. usleep_range(5000, 10000);
  2161. timeout--;
  2162. }
  2163. if (!timeout) {
  2164. hw_dbg(hw, "Driver can't access resource, SW_FW_SYNC timeout.\n");
  2165. return IXGBE_ERR_SWFW_SYNC;
  2166. }
  2167. gssr |= swmask;
  2168. IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
  2169. ixgbe_release_eeprom_semaphore(hw);
  2170. return 0;
  2171. }
  2172. /**
  2173. * ixgbe_release_swfw_sync - Release SWFW semaphore
  2174. * @hw: pointer to hardware structure
  2175. * @mask: Mask to specify which semaphore to release
  2176. *
  2177. * Releases the SWFW semaphore through the GSSR register for the specified
  2178. * function (CSR, PHY0, PHY1, EEPROM, Flash)
  2179. **/
  2180. void ixgbe_release_swfw_sync(struct ixgbe_hw *hw, u16 mask)
  2181. {
  2182. u32 gssr;
  2183. u32 swmask = mask;
  2184. ixgbe_get_eeprom_semaphore(hw);
  2185. gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
  2186. gssr &= ~swmask;
  2187. IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
  2188. ixgbe_release_eeprom_semaphore(hw);
  2189. }
  2190. /**
  2191. * ixgbe_disable_rx_buff_generic - Stops the receive data path
  2192. * @hw: pointer to hardware structure
  2193. *
  2194. * Stops the receive data path and waits for the HW to internally
  2195. * empty the Rx security block.
  2196. **/
  2197. s32 ixgbe_disable_rx_buff_generic(struct ixgbe_hw *hw)
  2198. {
  2199. #define IXGBE_MAX_SECRX_POLL 40
  2200. int i;
  2201. int secrxreg;
  2202. secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
  2203. secrxreg |= IXGBE_SECRXCTRL_RX_DIS;
  2204. IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, secrxreg);
  2205. for (i = 0; i < IXGBE_MAX_SECRX_POLL; i++) {
  2206. secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT);
  2207. if (secrxreg & IXGBE_SECRXSTAT_SECRX_RDY)
  2208. break;
  2209. else
  2210. /* Use interrupt-safe sleep just in case */
  2211. udelay(1000);
  2212. }
  2213. /* For informational purposes only */
  2214. if (i >= IXGBE_MAX_SECRX_POLL)
  2215. hw_dbg(hw, "Rx unit being enabled before security "
  2216. "path fully disabled. Continuing with init.\n");
  2217. return 0;
  2218. }
  2219. /**
  2220. * ixgbe_enable_rx_buff - Enables the receive data path
  2221. * @hw: pointer to hardware structure
  2222. *
  2223. * Enables the receive data path
  2224. **/
  2225. s32 ixgbe_enable_rx_buff_generic(struct ixgbe_hw *hw)
  2226. {
  2227. int secrxreg;
  2228. secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
  2229. secrxreg &= ~IXGBE_SECRXCTRL_RX_DIS;
  2230. IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, secrxreg);
  2231. IXGBE_WRITE_FLUSH(hw);
  2232. return 0;
  2233. }
  2234. /**
  2235. * ixgbe_enable_rx_dma_generic - Enable the Rx DMA unit
  2236. * @hw: pointer to hardware structure
  2237. * @regval: register value to write to RXCTRL
  2238. *
  2239. * Enables the Rx DMA unit
  2240. **/
  2241. s32 ixgbe_enable_rx_dma_generic(struct ixgbe_hw *hw, u32 regval)
  2242. {
  2243. IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, regval);
  2244. return 0;
  2245. }
  2246. /**
  2247. * ixgbe_blink_led_start_generic - Blink LED based on index.
  2248. * @hw: pointer to hardware structure
  2249. * @index: led number to blink
  2250. **/
  2251. s32 ixgbe_blink_led_start_generic(struct ixgbe_hw *hw, u32 index)
  2252. {
  2253. ixgbe_link_speed speed = 0;
  2254. bool link_up = false;
  2255. u32 autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
  2256. u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
  2257. /*
  2258. * Link must be up to auto-blink the LEDs;
  2259. * Force it if link is down.
  2260. */
  2261. hw->mac.ops.check_link(hw, &speed, &link_up, false);
  2262. if (!link_up) {
  2263. autoc_reg |= IXGBE_AUTOC_AN_RESTART;
  2264. autoc_reg |= IXGBE_AUTOC_FLU;
  2265. IXGBE_WRITE_REG(hw, IXGBE_AUTOC, autoc_reg);
  2266. IXGBE_WRITE_FLUSH(hw);
  2267. usleep_range(10000, 20000);
  2268. }
  2269. led_reg &= ~IXGBE_LED_MODE_MASK(index);
  2270. led_reg |= IXGBE_LED_BLINK(index);
  2271. IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
  2272. IXGBE_WRITE_FLUSH(hw);
  2273. return 0;
  2274. }
  2275. /**
  2276. * ixgbe_blink_led_stop_generic - Stop blinking LED based on index.
  2277. * @hw: pointer to hardware structure
  2278. * @index: led number to stop blinking
  2279. **/
  2280. s32 ixgbe_blink_led_stop_generic(struct ixgbe_hw *hw, u32 index)
  2281. {
  2282. u32 autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
  2283. u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
  2284. autoc_reg &= ~IXGBE_AUTOC_FLU;
  2285. autoc_reg |= IXGBE_AUTOC_AN_RESTART;
  2286. IXGBE_WRITE_REG(hw, IXGBE_AUTOC, autoc_reg);
  2287. led_reg &= ~IXGBE_LED_MODE_MASK(index);
  2288. led_reg &= ~IXGBE_LED_BLINK(index);
  2289. led_reg |= IXGBE_LED_LINK_ACTIVE << IXGBE_LED_MODE_SHIFT(index);
  2290. IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
  2291. IXGBE_WRITE_FLUSH(hw);
  2292. return 0;
  2293. }
  2294. /**
  2295. * ixgbe_get_san_mac_addr_offset - Get SAN MAC address offset from the EEPROM
  2296. * @hw: pointer to hardware structure
  2297. * @san_mac_offset: SAN MAC address offset
  2298. *
  2299. * This function will read the EEPROM location for the SAN MAC address
  2300. * pointer, and returns the value at that location. This is used in both
  2301. * get and set mac_addr routines.
  2302. **/
  2303. static s32 ixgbe_get_san_mac_addr_offset(struct ixgbe_hw *hw,
  2304. u16 *san_mac_offset)
  2305. {
  2306. /*
  2307. * First read the EEPROM pointer to see if the MAC addresses are
  2308. * available.
  2309. */
  2310. hw->eeprom.ops.read(hw, IXGBE_SAN_MAC_ADDR_PTR, san_mac_offset);
  2311. return 0;
  2312. }
  2313. /**
  2314. * ixgbe_get_san_mac_addr_generic - SAN MAC address retrieval from the EEPROM
  2315. * @hw: pointer to hardware structure
  2316. * @san_mac_addr: SAN MAC address
  2317. *
  2318. * Reads the SAN MAC address from the EEPROM, if it's available. This is
  2319. * per-port, so set_lan_id() must be called before reading the addresses.
  2320. * set_lan_id() is called by identify_sfp(), but this cannot be relied
  2321. * upon for non-SFP connections, so we must call it here.
  2322. **/
  2323. s32 ixgbe_get_san_mac_addr_generic(struct ixgbe_hw *hw, u8 *san_mac_addr)
  2324. {
  2325. u16 san_mac_data, san_mac_offset;
  2326. u8 i;
  2327. /*
  2328. * First read the EEPROM pointer to see if the MAC addresses are
  2329. * available. If they're not, no point in calling set_lan_id() here.
  2330. */
  2331. ixgbe_get_san_mac_addr_offset(hw, &san_mac_offset);
  2332. if ((san_mac_offset == 0) || (san_mac_offset == 0xFFFF)) {
  2333. /*
  2334. * No addresses available in this EEPROM. It's not an
  2335. * error though, so just wipe the local address and return.
  2336. */
  2337. for (i = 0; i < 6; i++)
  2338. san_mac_addr[i] = 0xFF;
  2339. goto san_mac_addr_out;
  2340. }
  2341. /* make sure we know which port we need to program */
  2342. hw->mac.ops.set_lan_id(hw);
  2343. /* apply the port offset to the address offset */
  2344. (hw->bus.func) ? (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT1_OFFSET) :
  2345. (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT0_OFFSET);
  2346. for (i = 0; i < 3; i++) {
  2347. hw->eeprom.ops.read(hw, san_mac_offset, &san_mac_data);
  2348. san_mac_addr[i * 2] = (u8)(san_mac_data);
  2349. san_mac_addr[i * 2 + 1] = (u8)(san_mac_data >> 8);
  2350. san_mac_offset++;
  2351. }
  2352. san_mac_addr_out:
  2353. return 0;
  2354. }
  2355. /**
  2356. * ixgbe_get_pcie_msix_count_generic - Gets MSI-X vector count
  2357. * @hw: pointer to hardware structure
  2358. *
  2359. * Read PCIe configuration space, and get the MSI-X vector count from
  2360. * the capabilities table.
  2361. **/
  2362. u16 ixgbe_get_pcie_msix_count_generic(struct ixgbe_hw *hw)
  2363. {
  2364. struct ixgbe_adapter *adapter = hw->back;
  2365. u16 msix_count = 1;
  2366. u16 max_msix_count;
  2367. u16 pcie_offset;
  2368. switch (hw->mac.type) {
  2369. case ixgbe_mac_82598EB:
  2370. pcie_offset = IXGBE_PCIE_MSIX_82598_CAPS;
  2371. max_msix_count = IXGBE_MAX_MSIX_VECTORS_82598;
  2372. break;
  2373. case ixgbe_mac_82599EB:
  2374. case ixgbe_mac_X540:
  2375. pcie_offset = IXGBE_PCIE_MSIX_82599_CAPS;
  2376. max_msix_count = IXGBE_MAX_MSIX_VECTORS_82599;
  2377. break;
  2378. default:
  2379. return msix_count;
  2380. }
  2381. pci_read_config_word(adapter->pdev, pcie_offset, &msix_count);
  2382. msix_count &= IXGBE_PCIE_MSIX_TBL_SZ_MASK;
  2383. /* MSI-X count is zero-based in HW */
  2384. msix_count++;
  2385. if (msix_count > max_msix_count)
  2386. msix_count = max_msix_count;
  2387. return msix_count;
  2388. }
  2389. /**
  2390. * ixgbe_clear_vmdq_generic - Disassociate a VMDq pool index from a rx address
  2391. * @hw: pointer to hardware struct
  2392. * @rar: receive address register index to disassociate
  2393. * @vmdq: VMDq pool index to remove from the rar
  2394. **/
  2395. s32 ixgbe_clear_vmdq_generic(struct ixgbe_hw *hw, u32 rar, u32 vmdq)
  2396. {
  2397. u32 mpsar_lo, mpsar_hi;
  2398. u32 rar_entries = hw->mac.num_rar_entries;
  2399. /* Make sure we are using a valid rar index range */
  2400. if (rar >= rar_entries) {
  2401. hw_dbg(hw, "RAR index %d is out of range.\n", rar);
  2402. return IXGBE_ERR_INVALID_ARGUMENT;
  2403. }
  2404. mpsar_lo = IXGBE_READ_REG(hw, IXGBE_MPSAR_LO(rar));
  2405. mpsar_hi = IXGBE_READ_REG(hw, IXGBE_MPSAR_HI(rar));
  2406. if (!mpsar_lo && !mpsar_hi)
  2407. goto done;
  2408. if (vmdq == IXGBE_CLEAR_VMDQ_ALL) {
  2409. if (mpsar_lo) {
  2410. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 0);
  2411. mpsar_lo = 0;
  2412. }
  2413. if (mpsar_hi) {
  2414. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 0);
  2415. mpsar_hi = 0;
  2416. }
  2417. } else if (vmdq < 32) {
  2418. mpsar_lo &= ~(1 << vmdq);
  2419. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), mpsar_lo);
  2420. } else {
  2421. mpsar_hi &= ~(1 << (vmdq - 32));
  2422. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), mpsar_hi);
  2423. }
  2424. /* was that the last pool using this rar? */
  2425. if (mpsar_lo == 0 && mpsar_hi == 0 && rar != 0)
  2426. hw->mac.ops.clear_rar(hw, rar);
  2427. done:
  2428. return 0;
  2429. }
  2430. /**
  2431. * ixgbe_set_vmdq_generic - Associate a VMDq pool index with a rx address
  2432. * @hw: pointer to hardware struct
  2433. * @rar: receive address register index to associate with a VMDq index
  2434. * @vmdq: VMDq pool index
  2435. **/
  2436. s32 ixgbe_set_vmdq_generic(struct ixgbe_hw *hw, u32 rar, u32 vmdq)
  2437. {
  2438. u32 mpsar;
  2439. u32 rar_entries = hw->mac.num_rar_entries;
  2440. /* Make sure we are using a valid rar index range */
  2441. if (rar >= rar_entries) {
  2442. hw_dbg(hw, "RAR index %d is out of range.\n", rar);
  2443. return IXGBE_ERR_INVALID_ARGUMENT;
  2444. }
  2445. if (vmdq < 32) {
  2446. mpsar = IXGBE_READ_REG(hw, IXGBE_MPSAR_LO(rar));
  2447. mpsar |= 1 << vmdq;
  2448. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), mpsar);
  2449. } else {
  2450. mpsar = IXGBE_READ_REG(hw, IXGBE_MPSAR_HI(rar));
  2451. mpsar |= 1 << (vmdq - 32);
  2452. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), mpsar);
  2453. }
  2454. return 0;
  2455. }
  2456. /**
  2457. * This function should only be involved in the IOV mode.
  2458. * In IOV mode, Default pool is next pool after the number of
  2459. * VFs advertized and not 0.
  2460. * MPSAR table needs to be updated for SAN_MAC RAR [hw->mac.san_mac_rar_index]
  2461. *
  2462. * ixgbe_set_vmdq_san_mac - Associate default VMDq pool index with a rx address
  2463. * @hw: pointer to hardware struct
  2464. * @vmdq: VMDq pool index
  2465. **/
  2466. s32 ixgbe_set_vmdq_san_mac_generic(struct ixgbe_hw *hw, u32 vmdq)
  2467. {
  2468. u32 rar = hw->mac.san_mac_rar_index;
  2469. if (vmdq < 32) {
  2470. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 1 << vmdq);
  2471. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 0);
  2472. } else {
  2473. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 0);
  2474. IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 1 << (vmdq - 32));
  2475. }
  2476. return 0;
  2477. }
  2478. /**
  2479. * ixgbe_init_uta_tables_generic - Initialize the Unicast Table Array
  2480. * @hw: pointer to hardware structure
  2481. **/
  2482. s32 ixgbe_init_uta_tables_generic(struct ixgbe_hw *hw)
  2483. {
  2484. int i;
  2485. for (i = 0; i < 128; i++)
  2486. IXGBE_WRITE_REG(hw, IXGBE_UTA(i), 0);
  2487. return 0;
  2488. }
  2489. /**
  2490. * ixgbe_find_vlvf_slot - find the vlanid or the first empty slot
  2491. * @hw: pointer to hardware structure
  2492. * @vlan: VLAN id to write to VLAN filter
  2493. *
  2494. * return the VLVF index where this VLAN id should be placed
  2495. *
  2496. **/
  2497. static s32 ixgbe_find_vlvf_slot(struct ixgbe_hw *hw, u32 vlan)
  2498. {
  2499. u32 bits = 0;
  2500. u32 first_empty_slot = 0;
  2501. s32 regindex;
  2502. /* short cut the special case */
  2503. if (vlan == 0)
  2504. return 0;
  2505. /*
  2506. * Search for the vlan id in the VLVF entries. Save off the first empty
  2507. * slot found along the way
  2508. */
  2509. for (regindex = 1; regindex < IXGBE_VLVF_ENTRIES; regindex++) {
  2510. bits = IXGBE_READ_REG(hw, IXGBE_VLVF(regindex));
  2511. if (!bits && !(first_empty_slot))
  2512. first_empty_slot = regindex;
  2513. else if ((bits & 0x0FFF) == vlan)
  2514. break;
  2515. }
  2516. /*
  2517. * If regindex is less than IXGBE_VLVF_ENTRIES, then we found the vlan
  2518. * in the VLVF. Else use the first empty VLVF register for this
  2519. * vlan id.
  2520. */
  2521. if (regindex >= IXGBE_VLVF_ENTRIES) {
  2522. if (first_empty_slot)
  2523. regindex = first_empty_slot;
  2524. else {
  2525. hw_dbg(hw, "No space in VLVF.\n");
  2526. regindex = IXGBE_ERR_NO_SPACE;
  2527. }
  2528. }
  2529. return regindex;
  2530. }
  2531. /**
  2532. * ixgbe_set_vfta_generic - Set VLAN filter table
  2533. * @hw: pointer to hardware structure
  2534. * @vlan: VLAN id to write to VLAN filter
  2535. * @vind: VMDq output index that maps queue to VLAN id in VFVFB
  2536. * @vlan_on: boolean flag to turn on/off VLAN in VFVF
  2537. *
  2538. * Turn on/off specified VLAN in the VLAN filter table.
  2539. **/
  2540. s32 ixgbe_set_vfta_generic(struct ixgbe_hw *hw, u32 vlan, u32 vind,
  2541. bool vlan_on)
  2542. {
  2543. s32 regindex;
  2544. u32 bitindex;
  2545. u32 vfta;
  2546. u32 bits;
  2547. u32 vt;
  2548. u32 targetbit;
  2549. bool vfta_changed = false;
  2550. if (vlan > 4095)
  2551. return IXGBE_ERR_PARAM;
  2552. /*
  2553. * this is a 2 part operation - first the VFTA, then the
  2554. * VLVF and VLVFB if VT Mode is set
  2555. * We don't write the VFTA until we know the VLVF part succeeded.
  2556. */
  2557. /* Part 1
  2558. * The VFTA is a bitstring made up of 128 32-bit registers
  2559. * that enable the particular VLAN id, much like the MTA:
  2560. * bits[11-5]: which register
  2561. * bits[4-0]: which bit in the register
  2562. */
  2563. regindex = (vlan >> 5) & 0x7F;
  2564. bitindex = vlan & 0x1F;
  2565. targetbit = (1 << bitindex);
  2566. vfta = IXGBE_READ_REG(hw, IXGBE_VFTA(regindex));
  2567. if (vlan_on) {
  2568. if (!(vfta & targetbit)) {
  2569. vfta |= targetbit;
  2570. vfta_changed = true;
  2571. }
  2572. } else {
  2573. if ((vfta & targetbit)) {
  2574. vfta &= ~targetbit;
  2575. vfta_changed = true;
  2576. }
  2577. }
  2578. /* Part 2
  2579. * If VT Mode is set
  2580. * Either vlan_on
  2581. * make sure the vlan is in VLVF
  2582. * set the vind bit in the matching VLVFB
  2583. * Or !vlan_on
  2584. * clear the pool bit and possibly the vind
  2585. */
  2586. vt = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
  2587. if (vt & IXGBE_VT_CTL_VT_ENABLE) {
  2588. s32 vlvf_index;
  2589. vlvf_index = ixgbe_find_vlvf_slot(hw, vlan);
  2590. if (vlvf_index < 0)
  2591. return vlvf_index;
  2592. if (vlan_on) {
  2593. /* set the pool bit */
  2594. if (vind < 32) {
  2595. bits = IXGBE_READ_REG(hw,
  2596. IXGBE_VLVFB(vlvf_index*2));
  2597. bits |= (1 << vind);
  2598. IXGBE_WRITE_REG(hw,
  2599. IXGBE_VLVFB(vlvf_index*2),
  2600. bits);
  2601. } else {
  2602. bits = IXGBE_READ_REG(hw,
  2603. IXGBE_VLVFB((vlvf_index*2)+1));
  2604. bits |= (1 << (vind-32));
  2605. IXGBE_WRITE_REG(hw,
  2606. IXGBE_VLVFB((vlvf_index*2)+1),
  2607. bits);
  2608. }
  2609. } else {
  2610. /* clear the pool bit */
  2611. if (vind < 32) {
  2612. bits = IXGBE_READ_REG(hw,
  2613. IXGBE_VLVFB(vlvf_index*2));
  2614. bits &= ~(1 << vind);
  2615. IXGBE_WRITE_REG(hw,
  2616. IXGBE_VLVFB(vlvf_index*2),
  2617. bits);
  2618. bits |= IXGBE_READ_REG(hw,
  2619. IXGBE_VLVFB((vlvf_index*2)+1));
  2620. } else {
  2621. bits = IXGBE_READ_REG(hw,
  2622. IXGBE_VLVFB((vlvf_index*2)+1));
  2623. bits &= ~(1 << (vind-32));
  2624. IXGBE_WRITE_REG(hw,
  2625. IXGBE_VLVFB((vlvf_index*2)+1),
  2626. bits);
  2627. bits |= IXGBE_READ_REG(hw,
  2628. IXGBE_VLVFB(vlvf_index*2));
  2629. }
  2630. }
  2631. /*
  2632. * If there are still bits set in the VLVFB registers
  2633. * for the VLAN ID indicated we need to see if the
  2634. * caller is requesting that we clear the VFTA entry bit.
  2635. * If the caller has requested that we clear the VFTA
  2636. * entry bit but there are still pools/VFs using this VLAN
  2637. * ID entry then ignore the request. We're not worried
  2638. * about the case where we're turning the VFTA VLAN ID
  2639. * entry bit on, only when requested to turn it off as
  2640. * there may be multiple pools and/or VFs using the
  2641. * VLAN ID entry. In that case we cannot clear the
  2642. * VFTA bit until all pools/VFs using that VLAN ID have also
  2643. * been cleared. This will be indicated by "bits" being
  2644. * zero.
  2645. */
  2646. if (bits) {
  2647. IXGBE_WRITE_REG(hw, IXGBE_VLVF(vlvf_index),
  2648. (IXGBE_VLVF_VIEN | vlan));
  2649. if (!vlan_on) {
  2650. /* someone wants to clear the vfta entry
  2651. * but some pools/VFs are still using it.
  2652. * Ignore it. */
  2653. vfta_changed = false;
  2654. }
  2655. }
  2656. else
  2657. IXGBE_WRITE_REG(hw, IXGBE_VLVF(vlvf_index), 0);
  2658. }
  2659. if (vfta_changed)
  2660. IXGBE_WRITE_REG(hw, IXGBE_VFTA(regindex), vfta);
  2661. return 0;
  2662. }
  2663. /**
  2664. * ixgbe_clear_vfta_generic - Clear VLAN filter table
  2665. * @hw: pointer to hardware structure
  2666. *
  2667. * Clears the VLAN filer table, and the VMDq index associated with the filter
  2668. **/
  2669. s32 ixgbe_clear_vfta_generic(struct ixgbe_hw *hw)
  2670. {
  2671. u32 offset;
  2672. for (offset = 0; offset < hw->mac.vft_size; offset++)
  2673. IXGBE_WRITE_REG(hw, IXGBE_VFTA(offset), 0);
  2674. for (offset = 0; offset < IXGBE_VLVF_ENTRIES; offset++) {
  2675. IXGBE_WRITE_REG(hw, IXGBE_VLVF(offset), 0);
  2676. IXGBE_WRITE_REG(hw, IXGBE_VLVFB(offset*2), 0);
  2677. IXGBE_WRITE_REG(hw, IXGBE_VLVFB((offset*2)+1), 0);
  2678. }
  2679. return 0;
  2680. }
  2681. /**
  2682. * ixgbe_check_mac_link_generic - Determine link and speed status
  2683. * @hw: pointer to hardware structure
  2684. * @speed: pointer to link speed
  2685. * @link_up: true when link is up
  2686. * @link_up_wait_to_complete: bool used to wait for link up or not
  2687. *
  2688. * Reads the links register to determine if link is up and the current speed
  2689. **/
  2690. s32 ixgbe_check_mac_link_generic(struct ixgbe_hw *hw, ixgbe_link_speed *speed,
  2691. bool *link_up, bool link_up_wait_to_complete)
  2692. {
  2693. u32 links_reg, links_orig;
  2694. u32 i;
  2695. /* clear the old state */
  2696. links_orig = IXGBE_READ_REG(hw, IXGBE_LINKS);
  2697. links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
  2698. if (links_orig != links_reg) {
  2699. hw_dbg(hw, "LINKS changed from %08X to %08X\n",
  2700. links_orig, links_reg);
  2701. }
  2702. if (link_up_wait_to_complete) {
  2703. for (i = 0; i < IXGBE_LINK_UP_TIME; i++) {
  2704. if (links_reg & IXGBE_LINKS_UP) {
  2705. *link_up = true;
  2706. break;
  2707. } else {
  2708. *link_up = false;
  2709. }
  2710. msleep(100);
  2711. links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
  2712. }
  2713. } else {
  2714. if (links_reg & IXGBE_LINKS_UP)
  2715. *link_up = true;
  2716. else
  2717. *link_up = false;
  2718. }
  2719. if ((links_reg & IXGBE_LINKS_SPEED_82599) ==
  2720. IXGBE_LINKS_SPEED_10G_82599)
  2721. *speed = IXGBE_LINK_SPEED_10GB_FULL;
  2722. else if ((links_reg & IXGBE_LINKS_SPEED_82599) ==
  2723. IXGBE_LINKS_SPEED_1G_82599)
  2724. *speed = IXGBE_LINK_SPEED_1GB_FULL;
  2725. else if ((links_reg & IXGBE_LINKS_SPEED_82599) ==
  2726. IXGBE_LINKS_SPEED_100_82599)
  2727. *speed = IXGBE_LINK_SPEED_100_FULL;
  2728. else
  2729. *speed = IXGBE_LINK_SPEED_UNKNOWN;
  2730. return 0;
  2731. }
  2732. /**
  2733. * ixgbe_get_wwn_prefix_generic - Get alternative WWNN/WWPN prefix from
  2734. * the EEPROM
  2735. * @hw: pointer to hardware structure
  2736. * @wwnn_prefix: the alternative WWNN prefix
  2737. * @wwpn_prefix: the alternative WWPN prefix
  2738. *
  2739. * This function will read the EEPROM from the alternative SAN MAC address
  2740. * block to check the support for the alternative WWNN/WWPN prefix support.
  2741. **/
  2742. s32 ixgbe_get_wwn_prefix_generic(struct ixgbe_hw *hw, u16 *wwnn_prefix,
  2743. u16 *wwpn_prefix)
  2744. {
  2745. u16 offset, caps;
  2746. u16 alt_san_mac_blk_offset;
  2747. /* clear output first */
  2748. *wwnn_prefix = 0xFFFF;
  2749. *wwpn_prefix = 0xFFFF;
  2750. /* check if alternative SAN MAC is supported */
  2751. hw->eeprom.ops.read(hw, IXGBE_ALT_SAN_MAC_ADDR_BLK_PTR,
  2752. &alt_san_mac_blk_offset);
  2753. if ((alt_san_mac_blk_offset == 0) ||
  2754. (alt_san_mac_blk_offset == 0xFFFF))
  2755. goto wwn_prefix_out;
  2756. /* check capability in alternative san mac address block */
  2757. offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_CAPS_OFFSET;
  2758. hw->eeprom.ops.read(hw, offset, &caps);
  2759. if (!(caps & IXGBE_ALT_SAN_MAC_ADDR_CAPS_ALTWWN))
  2760. goto wwn_prefix_out;
  2761. /* get the corresponding prefix for WWNN/WWPN */
  2762. offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_WWNN_OFFSET;
  2763. hw->eeprom.ops.read(hw, offset, wwnn_prefix);
  2764. offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_WWPN_OFFSET;
  2765. hw->eeprom.ops.read(hw, offset, wwpn_prefix);
  2766. wwn_prefix_out:
  2767. return 0;
  2768. }
  2769. /**
  2770. * ixgbe_set_mac_anti_spoofing - Enable/Disable MAC anti-spoofing
  2771. * @hw: pointer to hardware structure
  2772. * @enable: enable or disable switch for anti-spoofing
  2773. * @pf: Physical Function pool - do not enable anti-spoofing for the PF
  2774. *
  2775. **/
  2776. void ixgbe_set_mac_anti_spoofing(struct ixgbe_hw *hw, bool enable, int pf)
  2777. {
  2778. int j;
  2779. int pf_target_reg = pf >> 3;
  2780. int pf_target_shift = pf % 8;
  2781. u32 pfvfspoof = 0;
  2782. if (hw->mac.type == ixgbe_mac_82598EB)
  2783. return;
  2784. if (enable)
  2785. pfvfspoof = IXGBE_SPOOF_MACAS_MASK;
  2786. /*
  2787. * PFVFSPOOF register array is size 8 with 8 bits assigned to
  2788. * MAC anti-spoof enables in each register array element.
  2789. */
  2790. for (j = 0; j < pf_target_reg; j++)
  2791. IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(j), pfvfspoof);
  2792. /*
  2793. * The PF should be allowed to spoof so that it can support
  2794. * emulation mode NICs. Do not set the bits assigned to the PF
  2795. */
  2796. pfvfspoof &= (1 << pf_target_shift) - 1;
  2797. IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(j), pfvfspoof);
  2798. /*
  2799. * Remaining pools belong to the PF so they do not need to have
  2800. * anti-spoofing enabled.
  2801. */
  2802. for (j++; j < IXGBE_PFVFSPOOF_REG_COUNT; j++)
  2803. IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(j), 0);
  2804. }
  2805. /**
  2806. * ixgbe_set_vlan_anti_spoofing - Enable/Disable VLAN anti-spoofing
  2807. * @hw: pointer to hardware structure
  2808. * @enable: enable or disable switch for VLAN anti-spoofing
  2809. * @pf: Virtual Function pool - VF Pool to set for VLAN anti-spoofing
  2810. *
  2811. **/
  2812. void ixgbe_set_vlan_anti_spoofing(struct ixgbe_hw *hw, bool enable, int vf)
  2813. {
  2814. int vf_target_reg = vf >> 3;
  2815. int vf_target_shift = vf % 8 + IXGBE_SPOOF_VLANAS_SHIFT;
  2816. u32 pfvfspoof;
  2817. if (hw->mac.type == ixgbe_mac_82598EB)
  2818. return;
  2819. pfvfspoof = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
  2820. if (enable)
  2821. pfvfspoof |= (1 << vf_target_shift);
  2822. else
  2823. pfvfspoof &= ~(1 << vf_target_shift);
  2824. IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), pfvfspoof);
  2825. }
  2826. /**
  2827. * ixgbe_get_device_caps_generic - Get additional device capabilities
  2828. * @hw: pointer to hardware structure
  2829. * @device_caps: the EEPROM word with the extra device capabilities
  2830. *
  2831. * This function will read the EEPROM location for the device capabilities,
  2832. * and return the word through device_caps.
  2833. **/
  2834. s32 ixgbe_get_device_caps_generic(struct ixgbe_hw *hw, u16 *device_caps)
  2835. {
  2836. hw->eeprom.ops.read(hw, IXGBE_DEVICE_CAPS, device_caps);
  2837. return 0;
  2838. }
  2839. /**
  2840. * ixgbe_set_rxpba_generic - Initialize RX packet buffer
  2841. * @hw: pointer to hardware structure
  2842. * @num_pb: number of packet buffers to allocate
  2843. * @headroom: reserve n KB of headroom
  2844. * @strategy: packet buffer allocation strategy
  2845. **/
  2846. void ixgbe_set_rxpba_generic(struct ixgbe_hw *hw,
  2847. int num_pb,
  2848. u32 headroom,
  2849. int strategy)
  2850. {
  2851. u32 pbsize = hw->mac.rx_pb_size;
  2852. int i = 0;
  2853. u32 rxpktsize, txpktsize, txpbthresh;
  2854. /* Reserve headroom */
  2855. pbsize -= headroom;
  2856. if (!num_pb)
  2857. num_pb = 1;
  2858. /* Divide remaining packet buffer space amongst the number
  2859. * of packet buffers requested using supplied strategy.
  2860. */
  2861. switch (strategy) {
  2862. case (PBA_STRATEGY_WEIGHTED):
  2863. /* pba_80_48 strategy weight first half of packet buffer with
  2864. * 5/8 of the packet buffer space.
  2865. */
  2866. rxpktsize = ((pbsize * 5 * 2) / (num_pb * 8));
  2867. pbsize -= rxpktsize * (num_pb / 2);
  2868. rxpktsize <<= IXGBE_RXPBSIZE_SHIFT;
  2869. for (; i < (num_pb / 2); i++)
  2870. IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), rxpktsize);
  2871. /* Fall through to configure remaining packet buffers */
  2872. case (PBA_STRATEGY_EQUAL):
  2873. /* Divide the remaining Rx packet buffer evenly among the TCs */
  2874. rxpktsize = (pbsize / (num_pb - i)) << IXGBE_RXPBSIZE_SHIFT;
  2875. for (; i < num_pb; i++)
  2876. IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), rxpktsize);
  2877. break;
  2878. default:
  2879. break;
  2880. }
  2881. /*
  2882. * Setup Tx packet buffer and threshold equally for all TCs
  2883. * TXPBTHRESH register is set in K so divide by 1024 and subtract
  2884. * 10 since the largest packet we support is just over 9K.
  2885. */
  2886. txpktsize = IXGBE_TXPBSIZE_MAX / num_pb;
  2887. txpbthresh = (txpktsize / 1024) - IXGBE_TXPKT_SIZE_MAX;
  2888. for (i = 0; i < num_pb; i++) {
  2889. IXGBE_WRITE_REG(hw, IXGBE_TXPBSIZE(i), txpktsize);
  2890. IXGBE_WRITE_REG(hw, IXGBE_TXPBTHRESH(i), txpbthresh);
  2891. }
  2892. /* Clear unused TCs, if any, to zero buffer size*/
  2893. for (; i < IXGBE_MAX_PB; i++) {
  2894. IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), 0);
  2895. IXGBE_WRITE_REG(hw, IXGBE_TXPBSIZE(i), 0);
  2896. IXGBE_WRITE_REG(hw, IXGBE_TXPBTHRESH(i), 0);
  2897. }
  2898. }
  2899. /**
  2900. * ixgbe_calculate_checksum - Calculate checksum for buffer
  2901. * @buffer: pointer to EEPROM
  2902. * @length: size of EEPROM to calculate a checksum for
  2903. *
  2904. * Calculates the checksum for some buffer on a specified length. The
  2905. * checksum calculated is returned.
  2906. **/
  2907. static u8 ixgbe_calculate_checksum(u8 *buffer, u32 length)
  2908. {
  2909. u32 i;
  2910. u8 sum = 0;
  2911. if (!buffer)
  2912. return 0;
  2913. for (i = 0; i < length; i++)
  2914. sum += buffer[i];
  2915. return (u8) (0 - sum);
  2916. }
  2917. /**
  2918. * ixgbe_host_interface_command - Issue command to manageability block
  2919. * @hw: pointer to the HW structure
  2920. * @buffer: contains the command to write and where the return status will
  2921. * be placed
  2922. * @length: length of buffer, must be multiple of 4 bytes
  2923. *
  2924. * Communicates with the manageability block. On success return 0
  2925. * else return IXGBE_ERR_HOST_INTERFACE_COMMAND.
  2926. **/
  2927. static s32 ixgbe_host_interface_command(struct ixgbe_hw *hw, u32 *buffer,
  2928. u32 length)
  2929. {
  2930. u32 hicr, i, bi;
  2931. u32 hdr_size = sizeof(struct ixgbe_hic_hdr);
  2932. u8 buf_len, dword_len;
  2933. s32 ret_val = 0;
  2934. if (length == 0 || length & 0x3 ||
  2935. length > IXGBE_HI_MAX_BLOCK_BYTE_LENGTH) {
  2936. hw_dbg(hw, "Buffer length failure.\n");
  2937. ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
  2938. goto out;
  2939. }
  2940. /* Check that the host interface is enabled. */
  2941. hicr = IXGBE_READ_REG(hw, IXGBE_HICR);
  2942. if ((hicr & IXGBE_HICR_EN) == 0) {
  2943. hw_dbg(hw, "IXGBE_HOST_EN bit disabled.\n");
  2944. ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
  2945. goto out;
  2946. }
  2947. /* Calculate length in DWORDs */
  2948. dword_len = length >> 2;
  2949. /*
  2950. * The device driver writes the relevant command block
  2951. * into the ram area.
  2952. */
  2953. for (i = 0; i < dword_len; i++)
  2954. IXGBE_WRITE_REG_ARRAY(hw, IXGBE_FLEX_MNG,
  2955. i, cpu_to_le32(buffer[i]));
  2956. /* Setting this bit tells the ARC that a new command is pending. */
  2957. IXGBE_WRITE_REG(hw, IXGBE_HICR, hicr | IXGBE_HICR_C);
  2958. for (i = 0; i < IXGBE_HI_COMMAND_TIMEOUT; i++) {
  2959. hicr = IXGBE_READ_REG(hw, IXGBE_HICR);
  2960. if (!(hicr & IXGBE_HICR_C))
  2961. break;
  2962. usleep_range(1000, 2000);
  2963. }
  2964. /* Check command successful completion. */
  2965. if (i == IXGBE_HI_COMMAND_TIMEOUT ||
  2966. (!(IXGBE_READ_REG(hw, IXGBE_HICR) & IXGBE_HICR_SV))) {
  2967. hw_dbg(hw, "Command has failed with no status valid.\n");
  2968. ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
  2969. goto out;
  2970. }
  2971. /* Calculate length in DWORDs */
  2972. dword_len = hdr_size >> 2;
  2973. /* first pull in the header so we know the buffer length */
  2974. for (bi = 0; bi < dword_len; bi++) {
  2975. buffer[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi);
  2976. le32_to_cpus(&buffer[bi]);
  2977. }
  2978. /* If there is any thing in data position pull it in */
  2979. buf_len = ((struct ixgbe_hic_hdr *)buffer)->buf_len;
  2980. if (buf_len == 0)
  2981. goto out;
  2982. if (length < (buf_len + hdr_size)) {
  2983. hw_dbg(hw, "Buffer not large enough for reply message.\n");
  2984. ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
  2985. goto out;
  2986. }
  2987. /* Calculate length in DWORDs, add 3 for odd lengths */
  2988. dword_len = (buf_len + 3) >> 2;
  2989. /* Pull in the rest of the buffer (bi is where we left off)*/
  2990. for (; bi <= dword_len; bi++) {
  2991. buffer[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi);
  2992. le32_to_cpus(&buffer[bi]);
  2993. }
  2994. out:
  2995. return ret_val;
  2996. }
  2997. /**
  2998. * ixgbe_set_fw_drv_ver_generic - Sends driver version to firmware
  2999. * @hw: pointer to the HW structure
  3000. * @maj: driver version major number
  3001. * @min: driver version minor number
  3002. * @build: driver version build number
  3003. * @sub: driver version sub build number
  3004. *
  3005. * Sends driver version number to firmware through the manageability
  3006. * block. On success return 0
  3007. * else returns IXGBE_ERR_SWFW_SYNC when encountering an error acquiring
  3008. * semaphore or IXGBE_ERR_HOST_INTERFACE_COMMAND when command fails.
  3009. **/
  3010. s32 ixgbe_set_fw_drv_ver_generic(struct ixgbe_hw *hw, u8 maj, u8 min,
  3011. u8 build, u8 sub)
  3012. {
  3013. struct ixgbe_hic_drv_info fw_cmd;
  3014. int i;
  3015. s32 ret_val = 0;
  3016. if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_SW_MNG_SM) != 0) {
  3017. ret_val = IXGBE_ERR_SWFW_SYNC;
  3018. goto out;
  3019. }
  3020. fw_cmd.hdr.cmd = FW_CEM_CMD_DRIVER_INFO;
  3021. fw_cmd.hdr.buf_len = FW_CEM_CMD_DRIVER_INFO_LEN;
  3022. fw_cmd.hdr.cmd_or_resp.cmd_resv = FW_CEM_CMD_RESERVED;
  3023. fw_cmd.port_num = (u8)hw->bus.func;
  3024. fw_cmd.ver_maj = maj;
  3025. fw_cmd.ver_min = min;
  3026. fw_cmd.ver_build = build;
  3027. fw_cmd.ver_sub = sub;
  3028. fw_cmd.hdr.checksum = 0;
  3029. fw_cmd.hdr.checksum = ixgbe_calculate_checksum((u8 *)&fw_cmd,
  3030. (FW_CEM_HDR_LEN + fw_cmd.hdr.buf_len));
  3031. fw_cmd.pad = 0;
  3032. fw_cmd.pad2 = 0;
  3033. for (i = 0; i <= FW_CEM_MAX_RETRIES; i++) {
  3034. ret_val = ixgbe_host_interface_command(hw, (u32 *)&fw_cmd,
  3035. sizeof(fw_cmd));
  3036. if (ret_val != 0)
  3037. continue;
  3038. if (fw_cmd.hdr.cmd_or_resp.ret_status ==
  3039. FW_CEM_RESP_STATUS_SUCCESS)
  3040. ret_val = 0;
  3041. else
  3042. ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
  3043. break;
  3044. }
  3045. hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_SW_MNG_SM);
  3046. out:
  3047. return ret_val;
  3048. }
  3049. /**
  3050. * ixgbe_clear_tx_pending - Clear pending TX work from the PCIe fifo
  3051. * @hw: pointer to the hardware structure
  3052. *
  3053. * The 82599 and x540 MACs can experience issues if TX work is still pending
  3054. * when a reset occurs. This function prevents this by flushing the PCIe
  3055. * buffers on the system.
  3056. **/
  3057. void ixgbe_clear_tx_pending(struct ixgbe_hw *hw)
  3058. {
  3059. u32 gcr_ext, hlreg0;
  3060. /*
  3061. * If double reset is not requested then all transactions should
  3062. * already be clear and as such there is no work to do
  3063. */
  3064. if (!(hw->mac.flags & IXGBE_FLAGS_DOUBLE_RESET_REQUIRED))
  3065. return;
  3066. /*
  3067. * Set loopback enable to prevent any transmits from being sent
  3068. * should the link come up. This assumes that the RXCTRL.RXEN bit
  3069. * has already been cleared.
  3070. */
  3071. hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
  3072. IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0 | IXGBE_HLREG0_LPBK);
  3073. /* initiate cleaning flow for buffers in the PCIe transaction layer */
  3074. gcr_ext = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
  3075. IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT,
  3076. gcr_ext | IXGBE_GCR_EXT_BUFFERS_CLEAR);
  3077. /* Flush all writes and allow 20usec for all transactions to clear */
  3078. IXGBE_WRITE_FLUSH(hw);
  3079. udelay(20);
  3080. /* restore previous register values */
  3081. IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr_ext);
  3082. IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
  3083. }
  3084. static const u8 ixgbe_emc_temp_data[4] = {
  3085. IXGBE_EMC_INTERNAL_DATA,
  3086. IXGBE_EMC_DIODE1_DATA,
  3087. IXGBE_EMC_DIODE2_DATA,
  3088. IXGBE_EMC_DIODE3_DATA
  3089. };
  3090. static const u8 ixgbe_emc_therm_limit[4] = {
  3091. IXGBE_EMC_INTERNAL_THERM_LIMIT,
  3092. IXGBE_EMC_DIODE1_THERM_LIMIT,
  3093. IXGBE_EMC_DIODE2_THERM_LIMIT,
  3094. IXGBE_EMC_DIODE3_THERM_LIMIT
  3095. };
  3096. /**
  3097. * ixgbe_get_ets_data - Extracts the ETS bit data
  3098. * @hw: pointer to hardware structure
  3099. * @ets_cfg: extected ETS data
  3100. * @ets_offset: offset of ETS data
  3101. *
  3102. * Returns error code.
  3103. **/
  3104. static s32 ixgbe_get_ets_data(struct ixgbe_hw *hw, u16 *ets_cfg,
  3105. u16 *ets_offset)
  3106. {
  3107. s32 status = 0;
  3108. status = hw->eeprom.ops.read(hw, IXGBE_ETS_CFG, ets_offset);
  3109. if (status)
  3110. goto out;
  3111. if ((*ets_offset == 0x0000) || (*ets_offset == 0xFFFF)) {
  3112. status = IXGBE_NOT_IMPLEMENTED;
  3113. goto out;
  3114. }
  3115. status = hw->eeprom.ops.read(hw, *ets_offset, ets_cfg);
  3116. if (status)
  3117. goto out;
  3118. if ((*ets_cfg & IXGBE_ETS_TYPE_MASK) != IXGBE_ETS_TYPE_EMC_SHIFTED) {
  3119. status = IXGBE_NOT_IMPLEMENTED;
  3120. goto out;
  3121. }
  3122. out:
  3123. return status;
  3124. }
  3125. /**
  3126. * ixgbe_get_thermal_sensor_data - Gathers thermal sensor data
  3127. * @hw: pointer to hardware structure
  3128. *
  3129. * Returns the thermal sensor data structure
  3130. **/
  3131. s32 ixgbe_get_thermal_sensor_data_generic(struct ixgbe_hw *hw)
  3132. {
  3133. s32 status = 0;
  3134. u16 ets_offset;
  3135. u16 ets_cfg;
  3136. u16 ets_sensor;
  3137. u8 num_sensors;
  3138. u8 i;
  3139. struct ixgbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
  3140. /* Only support thermal sensors attached to physical port 0 */
  3141. if ((IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1)) {
  3142. status = IXGBE_NOT_IMPLEMENTED;
  3143. goto out;
  3144. }
  3145. status = ixgbe_get_ets_data(hw, &ets_cfg, &ets_offset);
  3146. if (status)
  3147. goto out;
  3148. num_sensors = (ets_cfg & IXGBE_ETS_NUM_SENSORS_MASK);
  3149. if (num_sensors > IXGBE_MAX_SENSORS)
  3150. num_sensors = IXGBE_MAX_SENSORS;
  3151. for (i = 0; i < num_sensors; i++) {
  3152. u8 sensor_index;
  3153. u8 sensor_location;
  3154. status = hw->eeprom.ops.read(hw, (ets_offset + 1 + i),
  3155. &ets_sensor);
  3156. if (status)
  3157. goto out;
  3158. sensor_index = ((ets_sensor & IXGBE_ETS_DATA_INDEX_MASK) >>
  3159. IXGBE_ETS_DATA_INDEX_SHIFT);
  3160. sensor_location = ((ets_sensor & IXGBE_ETS_DATA_LOC_MASK) >>
  3161. IXGBE_ETS_DATA_LOC_SHIFT);
  3162. if (sensor_location != 0) {
  3163. status = hw->phy.ops.read_i2c_byte(hw,
  3164. ixgbe_emc_temp_data[sensor_index],
  3165. IXGBE_I2C_THERMAL_SENSOR_ADDR,
  3166. &data->sensor[i].temp);
  3167. if (status)
  3168. goto out;
  3169. }
  3170. }
  3171. out:
  3172. return status;
  3173. }
  3174. /**
  3175. * ixgbe_init_thermal_sensor_thresh_generic - Inits thermal sensor thresholds
  3176. * @hw: pointer to hardware structure
  3177. *
  3178. * Inits the thermal sensor thresholds according to the NVM map
  3179. * and save off the threshold and location values into mac.thermal_sensor_data
  3180. **/
  3181. s32 ixgbe_init_thermal_sensor_thresh_generic(struct ixgbe_hw *hw)
  3182. {
  3183. s32 status = 0;
  3184. u16 ets_offset;
  3185. u16 ets_cfg;
  3186. u16 ets_sensor;
  3187. u8 low_thresh_delta;
  3188. u8 num_sensors;
  3189. u8 therm_limit;
  3190. u8 i;
  3191. struct ixgbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
  3192. memset(data, 0, sizeof(struct ixgbe_thermal_sensor_data));
  3193. /* Only support thermal sensors attached to physical port 0 */
  3194. if ((IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1)) {
  3195. status = IXGBE_NOT_IMPLEMENTED;
  3196. goto out;
  3197. }
  3198. status = ixgbe_get_ets_data(hw, &ets_cfg, &ets_offset);
  3199. if (status)
  3200. goto out;
  3201. low_thresh_delta = ((ets_cfg & IXGBE_ETS_LTHRES_DELTA_MASK) >>
  3202. IXGBE_ETS_LTHRES_DELTA_SHIFT);
  3203. num_sensors = (ets_cfg & IXGBE_ETS_NUM_SENSORS_MASK);
  3204. if (num_sensors > IXGBE_MAX_SENSORS)
  3205. num_sensors = IXGBE_MAX_SENSORS;
  3206. for (i = 0; i < num_sensors; i++) {
  3207. u8 sensor_index;
  3208. u8 sensor_location;
  3209. hw->eeprom.ops.read(hw, (ets_offset + 1 + i), &ets_sensor);
  3210. sensor_index = ((ets_sensor & IXGBE_ETS_DATA_INDEX_MASK) >>
  3211. IXGBE_ETS_DATA_INDEX_SHIFT);
  3212. sensor_location = ((ets_sensor & IXGBE_ETS_DATA_LOC_MASK) >>
  3213. IXGBE_ETS_DATA_LOC_SHIFT);
  3214. therm_limit = ets_sensor & IXGBE_ETS_DATA_HTHRESH_MASK;
  3215. hw->phy.ops.write_i2c_byte(hw,
  3216. ixgbe_emc_therm_limit[sensor_index],
  3217. IXGBE_I2C_THERMAL_SENSOR_ADDR, therm_limit);
  3218. if (sensor_location == 0)
  3219. continue;
  3220. data->sensor[i].location = sensor_location;
  3221. data->sensor[i].caution_thresh = therm_limit;
  3222. data->sensor[i].max_op_thresh = therm_limit - low_thresh_delta;
  3223. }
  3224. out:
  3225. return status;
  3226. }