qed_dev.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076
  1. /* QLogic qed NIC Driver
  2. * Copyright (c) 2015-2017 QLogic Corporation
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and /or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/types.h>
  33. #include <asm/byteorder.h>
  34. #include <linux/io.h>
  35. #include <linux/delay.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/errno.h>
  38. #include <linux/kernel.h>
  39. #include <linux/mutex.h>
  40. #include <linux/pci.h>
  41. #include <linux/slab.h>
  42. #include <linux/string.h>
  43. #include <linux/vmalloc.h>
  44. #include <linux/etherdevice.h>
  45. #include <linux/qed/qed_chain.h>
  46. #include <linux/qed/qed_if.h>
  47. #include "qed.h"
  48. #include "qed_cxt.h"
  49. #include "qed_dcbx.h"
  50. #include "qed_dev_api.h"
  51. #include "qed_fcoe.h"
  52. #include "qed_hsi.h"
  53. #include "qed_hw.h"
  54. #include "qed_init_ops.h"
  55. #include "qed_int.h"
  56. #include "qed_iscsi.h"
  57. #include "qed_ll2.h"
  58. #include "qed_mcp.h"
  59. #include "qed_ooo.h"
  60. #include "qed_reg_addr.h"
  61. #include "qed_sp.h"
  62. #include "qed_sriov.h"
  63. #include "qed_vf.h"
  64. #include "qed_roce.h"
  65. static DEFINE_SPINLOCK(qm_lock);
  66. #define QED_MIN_DPIS (4)
  67. #define QED_MIN_PWM_REGION (QED_WID_SIZE * QED_MIN_DPIS)
  68. /* API common to all protocols */
  69. enum BAR_ID {
  70. BAR_ID_0, /* used for GRC */
  71. BAR_ID_1 /* Used for doorbells */
  72. };
  73. static u32 qed_hw_bar_size(struct qed_hwfn *p_hwfn,
  74. struct qed_ptt *p_ptt, enum BAR_ID bar_id)
  75. {
  76. u32 bar_reg = (bar_id == BAR_ID_0 ?
  77. PGLUE_B_REG_PF_BAR0_SIZE : PGLUE_B_REG_PF_BAR1_SIZE);
  78. u32 val;
  79. if (IS_VF(p_hwfn->cdev))
  80. return 1 << 17;
  81. val = qed_rd(p_hwfn, p_ptt, bar_reg);
  82. if (val)
  83. return 1 << (val + 15);
  84. /* Old MFW initialized above registered only conditionally */
  85. if (p_hwfn->cdev->num_hwfns > 1) {
  86. DP_INFO(p_hwfn,
  87. "BAR size not configured. Assuming BAR size of 256kB for GRC and 512kB for DB\n");
  88. return BAR_ID_0 ? 256 * 1024 : 512 * 1024;
  89. } else {
  90. DP_INFO(p_hwfn,
  91. "BAR size not configured. Assuming BAR size of 512kB for GRC and 512kB for DB\n");
  92. return 512 * 1024;
  93. }
  94. }
  95. void qed_init_dp(struct qed_dev *cdev, u32 dp_module, u8 dp_level)
  96. {
  97. u32 i;
  98. cdev->dp_level = dp_level;
  99. cdev->dp_module = dp_module;
  100. for (i = 0; i < MAX_HWFNS_PER_DEVICE; i++) {
  101. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  102. p_hwfn->dp_level = dp_level;
  103. p_hwfn->dp_module = dp_module;
  104. }
  105. }
  106. void qed_init_struct(struct qed_dev *cdev)
  107. {
  108. u8 i;
  109. for (i = 0; i < MAX_HWFNS_PER_DEVICE; i++) {
  110. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  111. p_hwfn->cdev = cdev;
  112. p_hwfn->my_id = i;
  113. p_hwfn->b_active = false;
  114. mutex_init(&p_hwfn->dmae_info.mutex);
  115. }
  116. /* hwfn 0 is always active */
  117. cdev->hwfns[0].b_active = true;
  118. /* set the default cache alignment to 128 */
  119. cdev->cache_shift = 7;
  120. }
  121. static void qed_qm_info_free(struct qed_hwfn *p_hwfn)
  122. {
  123. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  124. kfree(qm_info->qm_pq_params);
  125. qm_info->qm_pq_params = NULL;
  126. kfree(qm_info->qm_vport_params);
  127. qm_info->qm_vport_params = NULL;
  128. kfree(qm_info->qm_port_params);
  129. qm_info->qm_port_params = NULL;
  130. kfree(qm_info->wfq_data);
  131. qm_info->wfq_data = NULL;
  132. }
  133. void qed_resc_free(struct qed_dev *cdev)
  134. {
  135. int i;
  136. if (IS_VF(cdev))
  137. return;
  138. kfree(cdev->fw_data);
  139. cdev->fw_data = NULL;
  140. kfree(cdev->reset_stats);
  141. cdev->reset_stats = NULL;
  142. for_each_hwfn(cdev, i) {
  143. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  144. qed_cxt_mngr_free(p_hwfn);
  145. qed_qm_info_free(p_hwfn);
  146. qed_spq_free(p_hwfn);
  147. qed_eq_free(p_hwfn);
  148. qed_consq_free(p_hwfn);
  149. qed_int_free(p_hwfn);
  150. #ifdef CONFIG_QED_LL2
  151. qed_ll2_free(p_hwfn);
  152. #endif
  153. if (p_hwfn->hw_info.personality == QED_PCI_FCOE)
  154. qed_fcoe_free(p_hwfn);
  155. if (p_hwfn->hw_info.personality == QED_PCI_ISCSI) {
  156. qed_iscsi_free(p_hwfn);
  157. qed_ooo_free(p_hwfn);
  158. }
  159. qed_iov_free(p_hwfn);
  160. qed_dmae_info_free(p_hwfn);
  161. qed_dcbx_info_free(p_hwfn);
  162. }
  163. }
  164. /******************** QM initialization *******************/
  165. #define ACTIVE_TCS_BMAP 0x9f
  166. #define ACTIVE_TCS_BMAP_4PORT_K2 0xf
  167. /* determines the physical queue flags for a given PF. */
  168. static u32 qed_get_pq_flags(struct qed_hwfn *p_hwfn)
  169. {
  170. u32 flags;
  171. /* common flags */
  172. flags = PQ_FLAGS_LB;
  173. /* feature flags */
  174. if (IS_QED_SRIOV(p_hwfn->cdev))
  175. flags |= PQ_FLAGS_VFS;
  176. /* protocol flags */
  177. switch (p_hwfn->hw_info.personality) {
  178. case QED_PCI_ETH:
  179. flags |= PQ_FLAGS_MCOS;
  180. break;
  181. case QED_PCI_FCOE:
  182. flags |= PQ_FLAGS_OFLD;
  183. break;
  184. case QED_PCI_ISCSI:
  185. flags |= PQ_FLAGS_ACK | PQ_FLAGS_OOO | PQ_FLAGS_OFLD;
  186. break;
  187. case QED_PCI_ETH_ROCE:
  188. flags |= PQ_FLAGS_MCOS | PQ_FLAGS_OFLD | PQ_FLAGS_LLT;
  189. break;
  190. default:
  191. DP_ERR(p_hwfn,
  192. "unknown personality %d\n", p_hwfn->hw_info.personality);
  193. return 0;
  194. }
  195. return flags;
  196. }
  197. /* Getters for resource amounts necessary for qm initialization */
  198. u8 qed_init_qm_get_num_tcs(struct qed_hwfn *p_hwfn)
  199. {
  200. return p_hwfn->hw_info.num_hw_tc;
  201. }
  202. u16 qed_init_qm_get_num_vfs(struct qed_hwfn *p_hwfn)
  203. {
  204. return IS_QED_SRIOV(p_hwfn->cdev) ?
  205. p_hwfn->cdev->p_iov_info->total_vfs : 0;
  206. }
  207. #define NUM_DEFAULT_RLS 1
  208. u16 qed_init_qm_get_num_pf_rls(struct qed_hwfn *p_hwfn)
  209. {
  210. u16 num_pf_rls, num_vfs = qed_init_qm_get_num_vfs(p_hwfn);
  211. /* num RLs can't exceed resource amount of rls or vports */
  212. num_pf_rls = (u16) min_t(u32, RESC_NUM(p_hwfn, QED_RL),
  213. RESC_NUM(p_hwfn, QED_VPORT));
  214. /* Make sure after we reserve there's something left */
  215. if (num_pf_rls < num_vfs + NUM_DEFAULT_RLS)
  216. return 0;
  217. /* subtract rls necessary for VFs and one default one for the PF */
  218. num_pf_rls -= num_vfs + NUM_DEFAULT_RLS;
  219. return num_pf_rls;
  220. }
  221. u16 qed_init_qm_get_num_vports(struct qed_hwfn *p_hwfn)
  222. {
  223. u32 pq_flags = qed_get_pq_flags(p_hwfn);
  224. /* all pqs share the same vport, except for vfs and pf_rl pqs */
  225. return (!!(PQ_FLAGS_RLS & pq_flags)) *
  226. qed_init_qm_get_num_pf_rls(p_hwfn) +
  227. (!!(PQ_FLAGS_VFS & pq_flags)) *
  228. qed_init_qm_get_num_vfs(p_hwfn) + 1;
  229. }
  230. /* calc amount of PQs according to the requested flags */
  231. u16 qed_init_qm_get_num_pqs(struct qed_hwfn *p_hwfn)
  232. {
  233. u32 pq_flags = qed_get_pq_flags(p_hwfn);
  234. return (!!(PQ_FLAGS_RLS & pq_flags)) *
  235. qed_init_qm_get_num_pf_rls(p_hwfn) +
  236. (!!(PQ_FLAGS_MCOS & pq_flags)) *
  237. qed_init_qm_get_num_tcs(p_hwfn) +
  238. (!!(PQ_FLAGS_LB & pq_flags)) + (!!(PQ_FLAGS_OOO & pq_flags)) +
  239. (!!(PQ_FLAGS_ACK & pq_flags)) + (!!(PQ_FLAGS_OFLD & pq_flags)) +
  240. (!!(PQ_FLAGS_LLT & pq_flags)) +
  241. (!!(PQ_FLAGS_VFS & pq_flags)) * qed_init_qm_get_num_vfs(p_hwfn);
  242. }
  243. /* initialize the top level QM params */
  244. static void qed_init_qm_params(struct qed_hwfn *p_hwfn)
  245. {
  246. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  247. bool four_port;
  248. /* pq and vport bases for this PF */
  249. qm_info->start_pq = (u16) RESC_START(p_hwfn, QED_PQ);
  250. qm_info->start_vport = (u8) RESC_START(p_hwfn, QED_VPORT);
  251. /* rate limiting and weighted fair queueing are always enabled */
  252. qm_info->vport_rl_en = 1;
  253. qm_info->vport_wfq_en = 1;
  254. /* TC config is different for AH 4 port */
  255. four_port = p_hwfn->cdev->num_ports_in_engine == MAX_NUM_PORTS_K2;
  256. /* in AH 4 port we have fewer TCs per port */
  257. qm_info->max_phys_tcs_per_port = four_port ? NUM_PHYS_TCS_4PORT_K2 :
  258. NUM_OF_PHYS_TCS;
  259. /* unless MFW indicated otherwise, ooo_tc == 3 for
  260. * AH 4-port and 4 otherwise.
  261. */
  262. if (!qm_info->ooo_tc)
  263. qm_info->ooo_tc = four_port ? DCBX_TCP_OOO_K2_4PORT_TC :
  264. DCBX_TCP_OOO_TC;
  265. }
  266. /* initialize qm vport params */
  267. static void qed_init_qm_vport_params(struct qed_hwfn *p_hwfn)
  268. {
  269. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  270. u8 i;
  271. /* all vports participate in weighted fair queueing */
  272. for (i = 0; i < qed_init_qm_get_num_vports(p_hwfn); i++)
  273. qm_info->qm_vport_params[i].vport_wfq = 1;
  274. }
  275. /* initialize qm port params */
  276. static void qed_init_qm_port_params(struct qed_hwfn *p_hwfn)
  277. {
  278. /* Initialize qm port parameters */
  279. u8 i, active_phys_tcs, num_ports = p_hwfn->cdev->num_ports_in_engine;
  280. /* indicate how ooo and high pri traffic is dealt with */
  281. active_phys_tcs = num_ports == MAX_NUM_PORTS_K2 ?
  282. ACTIVE_TCS_BMAP_4PORT_K2 :
  283. ACTIVE_TCS_BMAP;
  284. for (i = 0; i < num_ports; i++) {
  285. struct init_qm_port_params *p_qm_port =
  286. &p_hwfn->qm_info.qm_port_params[i];
  287. p_qm_port->active = 1;
  288. p_qm_port->active_phys_tcs = active_phys_tcs;
  289. p_qm_port->num_pbf_cmd_lines = PBF_MAX_CMD_LINES / num_ports;
  290. p_qm_port->num_btb_blocks = BTB_MAX_BLOCKS / num_ports;
  291. }
  292. }
  293. /* Reset the params which must be reset for qm init. QM init may be called as
  294. * a result of flows other than driver load (e.g. dcbx renegotiation). Other
  295. * params may be affected by the init but would simply recalculate to the same
  296. * values. The allocations made for QM init, ports, vports, pqs and vfqs are not
  297. * affected as these amounts stay the same.
  298. */
  299. static void qed_init_qm_reset_params(struct qed_hwfn *p_hwfn)
  300. {
  301. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  302. qm_info->num_pqs = 0;
  303. qm_info->num_vports = 0;
  304. qm_info->num_pf_rls = 0;
  305. qm_info->num_vf_pqs = 0;
  306. qm_info->first_vf_pq = 0;
  307. qm_info->first_mcos_pq = 0;
  308. qm_info->first_rl_pq = 0;
  309. }
  310. static void qed_init_qm_advance_vport(struct qed_hwfn *p_hwfn)
  311. {
  312. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  313. qm_info->num_vports++;
  314. if (qm_info->num_vports > qed_init_qm_get_num_vports(p_hwfn))
  315. DP_ERR(p_hwfn,
  316. "vport overflow! qm_info->num_vports %d, qm_init_get_num_vports() %d\n",
  317. qm_info->num_vports, qed_init_qm_get_num_vports(p_hwfn));
  318. }
  319. /* initialize a single pq and manage qm_info resources accounting.
  320. * The pq_init_flags param determines whether the PQ is rate limited
  321. * (for VF or PF) and whether a new vport is allocated to the pq or not
  322. * (i.e. vport will be shared).
  323. */
  324. /* flags for pq init */
  325. #define PQ_INIT_SHARE_VPORT (1 << 0)
  326. #define PQ_INIT_PF_RL (1 << 1)
  327. #define PQ_INIT_VF_RL (1 << 2)
  328. /* defines for pq init */
  329. #define PQ_INIT_DEFAULT_WRR_GROUP 1
  330. #define PQ_INIT_DEFAULT_TC 0
  331. #define PQ_INIT_OFLD_TC (p_hwfn->hw_info.offload_tc)
  332. static void qed_init_qm_pq(struct qed_hwfn *p_hwfn,
  333. struct qed_qm_info *qm_info,
  334. u8 tc, u32 pq_init_flags)
  335. {
  336. u16 pq_idx = qm_info->num_pqs, max_pq = qed_init_qm_get_num_pqs(p_hwfn);
  337. if (pq_idx > max_pq)
  338. DP_ERR(p_hwfn,
  339. "pq overflow! pq %d, max pq %d\n", pq_idx, max_pq);
  340. /* init pq params */
  341. qm_info->qm_pq_params[pq_idx].vport_id = qm_info->start_vport +
  342. qm_info->num_vports;
  343. qm_info->qm_pq_params[pq_idx].tc_id = tc;
  344. qm_info->qm_pq_params[pq_idx].wrr_group = PQ_INIT_DEFAULT_WRR_GROUP;
  345. qm_info->qm_pq_params[pq_idx].rl_valid =
  346. (pq_init_flags & PQ_INIT_PF_RL || pq_init_flags & PQ_INIT_VF_RL);
  347. /* qm params accounting */
  348. qm_info->num_pqs++;
  349. if (!(pq_init_flags & PQ_INIT_SHARE_VPORT))
  350. qm_info->num_vports++;
  351. if (pq_init_flags & PQ_INIT_PF_RL)
  352. qm_info->num_pf_rls++;
  353. if (qm_info->num_vports > qed_init_qm_get_num_vports(p_hwfn))
  354. DP_ERR(p_hwfn,
  355. "vport overflow! qm_info->num_vports %d, qm_init_get_num_vports() %d\n",
  356. qm_info->num_vports, qed_init_qm_get_num_vports(p_hwfn));
  357. if (qm_info->num_pf_rls > qed_init_qm_get_num_pf_rls(p_hwfn))
  358. DP_ERR(p_hwfn,
  359. "rl overflow! qm_info->num_pf_rls %d, qm_init_get_num_pf_rls() %d\n",
  360. qm_info->num_pf_rls, qed_init_qm_get_num_pf_rls(p_hwfn));
  361. }
  362. /* get pq index according to PQ_FLAGS */
  363. static u16 *qed_init_qm_get_idx_from_flags(struct qed_hwfn *p_hwfn,
  364. u32 pq_flags)
  365. {
  366. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  367. /* Can't have multiple flags set here */
  368. if (bitmap_weight((unsigned long *)&pq_flags, sizeof(pq_flags)) > 1)
  369. goto err;
  370. switch (pq_flags) {
  371. case PQ_FLAGS_RLS:
  372. return &qm_info->first_rl_pq;
  373. case PQ_FLAGS_MCOS:
  374. return &qm_info->first_mcos_pq;
  375. case PQ_FLAGS_LB:
  376. return &qm_info->pure_lb_pq;
  377. case PQ_FLAGS_OOO:
  378. return &qm_info->ooo_pq;
  379. case PQ_FLAGS_ACK:
  380. return &qm_info->pure_ack_pq;
  381. case PQ_FLAGS_OFLD:
  382. return &qm_info->offload_pq;
  383. case PQ_FLAGS_LLT:
  384. return &qm_info->low_latency_pq;
  385. case PQ_FLAGS_VFS:
  386. return &qm_info->first_vf_pq;
  387. default:
  388. goto err;
  389. }
  390. err:
  391. DP_ERR(p_hwfn, "BAD pq flags %d\n", pq_flags);
  392. return NULL;
  393. }
  394. /* save pq index in qm info */
  395. static void qed_init_qm_set_idx(struct qed_hwfn *p_hwfn,
  396. u32 pq_flags, u16 pq_val)
  397. {
  398. u16 *base_pq_idx = qed_init_qm_get_idx_from_flags(p_hwfn, pq_flags);
  399. *base_pq_idx = p_hwfn->qm_info.start_pq + pq_val;
  400. }
  401. /* get tx pq index, with the PQ TX base already set (ready for context init) */
  402. u16 qed_get_cm_pq_idx(struct qed_hwfn *p_hwfn, u32 pq_flags)
  403. {
  404. u16 *base_pq_idx = qed_init_qm_get_idx_from_flags(p_hwfn, pq_flags);
  405. return *base_pq_idx + CM_TX_PQ_BASE;
  406. }
  407. u16 qed_get_cm_pq_idx_mcos(struct qed_hwfn *p_hwfn, u8 tc)
  408. {
  409. u8 max_tc = qed_init_qm_get_num_tcs(p_hwfn);
  410. if (tc > max_tc)
  411. DP_ERR(p_hwfn, "tc %d must be smaller than %d\n", tc, max_tc);
  412. return qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_MCOS) + tc;
  413. }
  414. u16 qed_get_cm_pq_idx_vf(struct qed_hwfn *p_hwfn, u16 vf)
  415. {
  416. u16 max_vf = qed_init_qm_get_num_vfs(p_hwfn);
  417. if (vf > max_vf)
  418. DP_ERR(p_hwfn, "vf %d must be smaller than %d\n", vf, max_vf);
  419. return qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_VFS) + vf;
  420. }
  421. u16 qed_get_cm_pq_idx_rl(struct qed_hwfn *p_hwfn, u8 rl)
  422. {
  423. u16 max_rl = qed_init_qm_get_num_pf_rls(p_hwfn);
  424. if (rl > max_rl)
  425. DP_ERR(p_hwfn, "rl %d must be smaller than %d\n", rl, max_rl);
  426. return qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_RLS) + rl;
  427. }
  428. /* Functions for creating specific types of pqs */
  429. static void qed_init_qm_lb_pq(struct qed_hwfn *p_hwfn)
  430. {
  431. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  432. if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_LB))
  433. return;
  434. qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_LB, qm_info->num_pqs);
  435. qed_init_qm_pq(p_hwfn, qm_info, PURE_LB_TC, PQ_INIT_SHARE_VPORT);
  436. }
  437. static void qed_init_qm_ooo_pq(struct qed_hwfn *p_hwfn)
  438. {
  439. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  440. if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_OOO))
  441. return;
  442. qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_OOO, qm_info->num_pqs);
  443. qed_init_qm_pq(p_hwfn, qm_info, qm_info->ooo_tc, PQ_INIT_SHARE_VPORT);
  444. }
  445. static void qed_init_qm_pure_ack_pq(struct qed_hwfn *p_hwfn)
  446. {
  447. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  448. if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_ACK))
  449. return;
  450. qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_ACK, qm_info->num_pqs);
  451. qed_init_qm_pq(p_hwfn, qm_info, PQ_INIT_OFLD_TC, PQ_INIT_SHARE_VPORT);
  452. }
  453. static void qed_init_qm_offload_pq(struct qed_hwfn *p_hwfn)
  454. {
  455. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  456. if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_OFLD))
  457. return;
  458. qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_OFLD, qm_info->num_pqs);
  459. qed_init_qm_pq(p_hwfn, qm_info, PQ_INIT_OFLD_TC, PQ_INIT_SHARE_VPORT);
  460. }
  461. static void qed_init_qm_low_latency_pq(struct qed_hwfn *p_hwfn)
  462. {
  463. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  464. if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_LLT))
  465. return;
  466. qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_LLT, qm_info->num_pqs);
  467. qed_init_qm_pq(p_hwfn, qm_info, PQ_INIT_OFLD_TC, PQ_INIT_SHARE_VPORT);
  468. }
  469. static void qed_init_qm_mcos_pqs(struct qed_hwfn *p_hwfn)
  470. {
  471. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  472. u8 tc_idx;
  473. if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_MCOS))
  474. return;
  475. qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_MCOS, qm_info->num_pqs);
  476. for (tc_idx = 0; tc_idx < qed_init_qm_get_num_tcs(p_hwfn); tc_idx++)
  477. qed_init_qm_pq(p_hwfn, qm_info, tc_idx, PQ_INIT_SHARE_VPORT);
  478. }
  479. static void qed_init_qm_vf_pqs(struct qed_hwfn *p_hwfn)
  480. {
  481. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  482. u16 vf_idx, num_vfs = qed_init_qm_get_num_vfs(p_hwfn);
  483. if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_VFS))
  484. return;
  485. qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_VFS, qm_info->num_pqs);
  486. qm_info->num_vf_pqs = num_vfs;
  487. for (vf_idx = 0; vf_idx < num_vfs; vf_idx++)
  488. qed_init_qm_pq(p_hwfn,
  489. qm_info, PQ_INIT_DEFAULT_TC, PQ_INIT_VF_RL);
  490. }
  491. static void qed_init_qm_rl_pqs(struct qed_hwfn *p_hwfn)
  492. {
  493. u16 pf_rls_idx, num_pf_rls = qed_init_qm_get_num_pf_rls(p_hwfn);
  494. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  495. if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_RLS))
  496. return;
  497. qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_RLS, qm_info->num_pqs);
  498. for (pf_rls_idx = 0; pf_rls_idx < num_pf_rls; pf_rls_idx++)
  499. qed_init_qm_pq(p_hwfn, qm_info, PQ_INIT_OFLD_TC, PQ_INIT_PF_RL);
  500. }
  501. static void qed_init_qm_pq_params(struct qed_hwfn *p_hwfn)
  502. {
  503. /* rate limited pqs, must come first (FW assumption) */
  504. qed_init_qm_rl_pqs(p_hwfn);
  505. /* pqs for multi cos */
  506. qed_init_qm_mcos_pqs(p_hwfn);
  507. /* pure loopback pq */
  508. qed_init_qm_lb_pq(p_hwfn);
  509. /* out of order pq */
  510. qed_init_qm_ooo_pq(p_hwfn);
  511. /* pure ack pq */
  512. qed_init_qm_pure_ack_pq(p_hwfn);
  513. /* pq for offloaded protocol */
  514. qed_init_qm_offload_pq(p_hwfn);
  515. /* low latency pq */
  516. qed_init_qm_low_latency_pq(p_hwfn);
  517. /* done sharing vports */
  518. qed_init_qm_advance_vport(p_hwfn);
  519. /* pqs for vfs */
  520. qed_init_qm_vf_pqs(p_hwfn);
  521. }
  522. /* compare values of getters against resources amounts */
  523. static int qed_init_qm_sanity(struct qed_hwfn *p_hwfn)
  524. {
  525. if (qed_init_qm_get_num_vports(p_hwfn) > RESC_NUM(p_hwfn, QED_VPORT)) {
  526. DP_ERR(p_hwfn, "requested amount of vports exceeds resource\n");
  527. return -EINVAL;
  528. }
  529. if (qed_init_qm_get_num_pqs(p_hwfn) > RESC_NUM(p_hwfn, QED_PQ)) {
  530. DP_ERR(p_hwfn, "requested amount of pqs exceeds resource\n");
  531. return -EINVAL;
  532. }
  533. return 0;
  534. }
  535. static void qed_dp_init_qm_params(struct qed_hwfn *p_hwfn)
  536. {
  537. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  538. struct init_qm_vport_params *vport;
  539. struct init_qm_port_params *port;
  540. struct init_qm_pq_params *pq;
  541. int i, tc;
  542. /* top level params */
  543. DP_VERBOSE(p_hwfn,
  544. NETIF_MSG_HW,
  545. "qm init top level params: start_pq %d, start_vport %d, pure_lb_pq %d, offload_pq %d, pure_ack_pq %d\n",
  546. qm_info->start_pq,
  547. qm_info->start_vport,
  548. qm_info->pure_lb_pq,
  549. qm_info->offload_pq, qm_info->pure_ack_pq);
  550. DP_VERBOSE(p_hwfn,
  551. NETIF_MSG_HW,
  552. "ooo_pq %d, first_vf_pq %d, num_pqs %d, num_vf_pqs %d, num_vports %d, max_phys_tcs_per_port %d\n",
  553. qm_info->ooo_pq,
  554. qm_info->first_vf_pq,
  555. qm_info->num_pqs,
  556. qm_info->num_vf_pqs,
  557. qm_info->num_vports, qm_info->max_phys_tcs_per_port);
  558. DP_VERBOSE(p_hwfn,
  559. NETIF_MSG_HW,
  560. "pf_rl_en %d, pf_wfq_en %d, vport_rl_en %d, vport_wfq_en %d, pf_wfq %d, pf_rl %d, num_pf_rls %d, pq_flags %x\n",
  561. qm_info->pf_rl_en,
  562. qm_info->pf_wfq_en,
  563. qm_info->vport_rl_en,
  564. qm_info->vport_wfq_en,
  565. qm_info->pf_wfq,
  566. qm_info->pf_rl,
  567. qm_info->num_pf_rls, qed_get_pq_flags(p_hwfn));
  568. /* port table */
  569. for (i = 0; i < p_hwfn->cdev->num_ports_in_engine; i++) {
  570. port = &(qm_info->qm_port_params[i]);
  571. DP_VERBOSE(p_hwfn,
  572. NETIF_MSG_HW,
  573. "port idx %d, active %d, active_phys_tcs %d, num_pbf_cmd_lines %d, num_btb_blocks %d, reserved %d\n",
  574. i,
  575. port->active,
  576. port->active_phys_tcs,
  577. port->num_pbf_cmd_lines,
  578. port->num_btb_blocks, port->reserved);
  579. }
  580. /* vport table */
  581. for (i = 0; i < qm_info->num_vports; i++) {
  582. vport = &(qm_info->qm_vport_params[i]);
  583. DP_VERBOSE(p_hwfn,
  584. NETIF_MSG_HW,
  585. "vport idx %d, vport_rl %d, wfq %d, first_tx_pq_id [ ",
  586. qm_info->start_vport + i,
  587. vport->vport_rl, vport->vport_wfq);
  588. for (tc = 0; tc < NUM_OF_TCS; tc++)
  589. DP_VERBOSE(p_hwfn,
  590. NETIF_MSG_HW,
  591. "%d ", vport->first_tx_pq_id[tc]);
  592. DP_VERBOSE(p_hwfn, NETIF_MSG_HW, "]\n");
  593. }
  594. /* pq table */
  595. for (i = 0; i < qm_info->num_pqs; i++) {
  596. pq = &(qm_info->qm_pq_params[i]);
  597. DP_VERBOSE(p_hwfn,
  598. NETIF_MSG_HW,
  599. "pq idx %d, vport_id %d, tc %d, wrr_grp %d, rl_valid %d\n",
  600. qm_info->start_pq + i,
  601. pq->vport_id,
  602. pq->tc_id, pq->wrr_group, pq->rl_valid);
  603. }
  604. }
  605. static void qed_init_qm_info(struct qed_hwfn *p_hwfn)
  606. {
  607. /* reset params required for init run */
  608. qed_init_qm_reset_params(p_hwfn);
  609. /* init QM top level params */
  610. qed_init_qm_params(p_hwfn);
  611. /* init QM port params */
  612. qed_init_qm_port_params(p_hwfn);
  613. /* init QM vport params */
  614. qed_init_qm_vport_params(p_hwfn);
  615. /* init QM physical queue params */
  616. qed_init_qm_pq_params(p_hwfn);
  617. /* display all that init */
  618. qed_dp_init_qm_params(p_hwfn);
  619. }
  620. /* This function reconfigures the QM pf on the fly.
  621. * For this purpose we:
  622. * 1. reconfigure the QM database
  623. * 2. set new values to runtime arrat
  624. * 3. send an sdm_qm_cmd through the rbc interface to stop the QM
  625. * 4. activate init tool in QM_PF stage
  626. * 5. send an sdm_qm_cmd through rbc interface to release the QM
  627. */
  628. int qed_qm_reconf(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  629. {
  630. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  631. bool b_rc;
  632. int rc;
  633. /* initialize qed's qm data structure */
  634. qed_init_qm_info(p_hwfn);
  635. /* stop PF's qm queues */
  636. spin_lock_bh(&qm_lock);
  637. b_rc = qed_send_qm_stop_cmd(p_hwfn, p_ptt, false, true,
  638. qm_info->start_pq, qm_info->num_pqs);
  639. spin_unlock_bh(&qm_lock);
  640. if (!b_rc)
  641. return -EINVAL;
  642. /* clear the QM_PF runtime phase leftovers from previous init */
  643. qed_init_clear_rt_data(p_hwfn);
  644. /* prepare QM portion of runtime array */
  645. qed_qm_init_pf(p_hwfn, p_ptt);
  646. /* activate init tool on runtime array */
  647. rc = qed_init_run(p_hwfn, p_ptt, PHASE_QM_PF, p_hwfn->rel_pf_id,
  648. p_hwfn->hw_info.hw_mode);
  649. if (rc)
  650. return rc;
  651. /* start PF's qm queues */
  652. spin_lock_bh(&qm_lock);
  653. b_rc = qed_send_qm_stop_cmd(p_hwfn, p_ptt, true, true,
  654. qm_info->start_pq, qm_info->num_pqs);
  655. spin_unlock_bh(&qm_lock);
  656. if (!b_rc)
  657. return -EINVAL;
  658. return 0;
  659. }
  660. static int qed_alloc_qm_data(struct qed_hwfn *p_hwfn)
  661. {
  662. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  663. int rc;
  664. rc = qed_init_qm_sanity(p_hwfn);
  665. if (rc)
  666. goto alloc_err;
  667. qm_info->qm_pq_params = kzalloc(sizeof(*qm_info->qm_pq_params) *
  668. qed_init_qm_get_num_pqs(p_hwfn),
  669. GFP_KERNEL);
  670. if (!qm_info->qm_pq_params)
  671. goto alloc_err;
  672. qm_info->qm_vport_params = kzalloc(sizeof(*qm_info->qm_vport_params) *
  673. qed_init_qm_get_num_vports(p_hwfn),
  674. GFP_KERNEL);
  675. if (!qm_info->qm_vport_params)
  676. goto alloc_err;
  677. qm_info->qm_port_params = kzalloc(sizeof(*qm_info->qm_port_params) *
  678. p_hwfn->cdev->num_ports_in_engine,
  679. GFP_KERNEL);
  680. if (!qm_info->qm_port_params)
  681. goto alloc_err;
  682. qm_info->wfq_data = kzalloc(sizeof(*qm_info->wfq_data) *
  683. qed_init_qm_get_num_vports(p_hwfn),
  684. GFP_KERNEL);
  685. if (!qm_info->wfq_data)
  686. goto alloc_err;
  687. return 0;
  688. alloc_err:
  689. DP_NOTICE(p_hwfn, "Failed to allocate memory for QM params\n");
  690. qed_qm_info_free(p_hwfn);
  691. return -ENOMEM;
  692. }
  693. int qed_resc_alloc(struct qed_dev *cdev)
  694. {
  695. u32 rdma_tasks, excess_tasks;
  696. u32 line_count;
  697. int i, rc = 0;
  698. if (IS_VF(cdev))
  699. return rc;
  700. cdev->fw_data = kzalloc(sizeof(*cdev->fw_data), GFP_KERNEL);
  701. if (!cdev->fw_data)
  702. return -ENOMEM;
  703. for_each_hwfn(cdev, i) {
  704. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  705. u32 n_eqes, num_cons;
  706. /* First allocate the context manager structure */
  707. rc = qed_cxt_mngr_alloc(p_hwfn);
  708. if (rc)
  709. goto alloc_err;
  710. /* Set the HW cid/tid numbers (in the contest manager)
  711. * Must be done prior to any further computations.
  712. */
  713. rc = qed_cxt_set_pf_params(p_hwfn, RDMA_MAX_TIDS);
  714. if (rc)
  715. goto alloc_err;
  716. rc = qed_alloc_qm_data(p_hwfn);
  717. if (rc)
  718. goto alloc_err;
  719. /* init qm info */
  720. qed_init_qm_info(p_hwfn);
  721. /* Compute the ILT client partition */
  722. rc = qed_cxt_cfg_ilt_compute(p_hwfn, &line_count);
  723. if (rc) {
  724. DP_NOTICE(p_hwfn,
  725. "too many ILT lines; re-computing with less lines\n");
  726. /* In case there are not enough ILT lines we reduce the
  727. * number of RDMA tasks and re-compute.
  728. */
  729. excess_tasks =
  730. qed_cxt_cfg_ilt_compute_excess(p_hwfn, line_count);
  731. if (!excess_tasks)
  732. goto alloc_err;
  733. rdma_tasks = RDMA_MAX_TIDS - excess_tasks;
  734. rc = qed_cxt_set_pf_params(p_hwfn, rdma_tasks);
  735. if (rc)
  736. goto alloc_err;
  737. rc = qed_cxt_cfg_ilt_compute(p_hwfn, &line_count);
  738. if (rc) {
  739. DP_ERR(p_hwfn,
  740. "failed ILT compute. Requested too many lines: %u\n",
  741. line_count);
  742. goto alloc_err;
  743. }
  744. }
  745. /* CID map / ILT shadow table / T2
  746. * The talbes sizes are determined by the computations above
  747. */
  748. rc = qed_cxt_tables_alloc(p_hwfn);
  749. if (rc)
  750. goto alloc_err;
  751. /* SPQ, must follow ILT because initializes SPQ context */
  752. rc = qed_spq_alloc(p_hwfn);
  753. if (rc)
  754. goto alloc_err;
  755. /* SP status block allocation */
  756. p_hwfn->p_dpc_ptt = qed_get_reserved_ptt(p_hwfn,
  757. RESERVED_PTT_DPC);
  758. rc = qed_int_alloc(p_hwfn, p_hwfn->p_main_ptt);
  759. if (rc)
  760. goto alloc_err;
  761. rc = qed_iov_alloc(p_hwfn);
  762. if (rc)
  763. goto alloc_err;
  764. /* EQ */
  765. n_eqes = qed_chain_get_capacity(&p_hwfn->p_spq->chain);
  766. if (p_hwfn->hw_info.personality == QED_PCI_ETH_ROCE) {
  767. num_cons = qed_cxt_get_proto_cid_count(p_hwfn,
  768. PROTOCOLID_ROCE,
  769. NULL) * 2;
  770. n_eqes += num_cons + 2 * MAX_NUM_VFS_BB;
  771. } else if (p_hwfn->hw_info.personality == QED_PCI_ISCSI) {
  772. num_cons =
  773. qed_cxt_get_proto_cid_count(p_hwfn,
  774. PROTOCOLID_ISCSI,
  775. NULL);
  776. n_eqes += 2 * num_cons;
  777. }
  778. if (n_eqes > 0xFFFF) {
  779. DP_ERR(p_hwfn,
  780. "Cannot allocate 0x%x EQ elements. The maximum of a u16 chain is 0x%x\n",
  781. n_eqes, 0xFFFF);
  782. goto alloc_no_mem;
  783. }
  784. rc = qed_eq_alloc(p_hwfn, (u16) n_eqes);
  785. if (rc)
  786. goto alloc_err;
  787. rc = qed_consq_alloc(p_hwfn);
  788. if (rc)
  789. goto alloc_err;
  790. #ifdef CONFIG_QED_LL2
  791. if (p_hwfn->using_ll2) {
  792. rc = qed_ll2_alloc(p_hwfn);
  793. if (rc)
  794. goto alloc_err;
  795. }
  796. #endif
  797. if (p_hwfn->hw_info.personality == QED_PCI_FCOE) {
  798. rc = qed_fcoe_alloc(p_hwfn);
  799. if (rc)
  800. goto alloc_err;
  801. }
  802. if (p_hwfn->hw_info.personality == QED_PCI_ISCSI) {
  803. rc = qed_iscsi_alloc(p_hwfn);
  804. if (rc)
  805. goto alloc_err;
  806. rc = qed_ooo_alloc(p_hwfn);
  807. if (rc)
  808. goto alloc_err;
  809. }
  810. /* DMA info initialization */
  811. rc = qed_dmae_info_alloc(p_hwfn);
  812. if (rc)
  813. goto alloc_err;
  814. /* DCBX initialization */
  815. rc = qed_dcbx_info_alloc(p_hwfn);
  816. if (rc)
  817. goto alloc_err;
  818. }
  819. cdev->reset_stats = kzalloc(sizeof(*cdev->reset_stats), GFP_KERNEL);
  820. if (!cdev->reset_stats)
  821. goto alloc_no_mem;
  822. return 0;
  823. alloc_no_mem:
  824. rc = -ENOMEM;
  825. alloc_err:
  826. qed_resc_free(cdev);
  827. return rc;
  828. }
  829. void qed_resc_setup(struct qed_dev *cdev)
  830. {
  831. int i;
  832. if (IS_VF(cdev))
  833. return;
  834. for_each_hwfn(cdev, i) {
  835. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  836. qed_cxt_mngr_setup(p_hwfn);
  837. qed_spq_setup(p_hwfn);
  838. qed_eq_setup(p_hwfn);
  839. qed_consq_setup(p_hwfn);
  840. /* Read shadow of current MFW mailbox */
  841. qed_mcp_read_mb(p_hwfn, p_hwfn->p_main_ptt);
  842. memcpy(p_hwfn->mcp_info->mfw_mb_shadow,
  843. p_hwfn->mcp_info->mfw_mb_cur,
  844. p_hwfn->mcp_info->mfw_mb_length);
  845. qed_int_setup(p_hwfn, p_hwfn->p_main_ptt);
  846. qed_iov_setup(p_hwfn, p_hwfn->p_main_ptt);
  847. #ifdef CONFIG_QED_LL2
  848. if (p_hwfn->using_ll2)
  849. qed_ll2_setup(p_hwfn);
  850. #endif
  851. if (p_hwfn->hw_info.personality == QED_PCI_FCOE)
  852. qed_fcoe_setup(p_hwfn);
  853. if (p_hwfn->hw_info.personality == QED_PCI_ISCSI) {
  854. qed_iscsi_setup(p_hwfn);
  855. qed_ooo_setup(p_hwfn);
  856. }
  857. }
  858. }
  859. #define FINAL_CLEANUP_POLL_CNT (100)
  860. #define FINAL_CLEANUP_POLL_TIME (10)
  861. int qed_final_cleanup(struct qed_hwfn *p_hwfn,
  862. struct qed_ptt *p_ptt, u16 id, bool is_vf)
  863. {
  864. u32 command = 0, addr, count = FINAL_CLEANUP_POLL_CNT;
  865. int rc = -EBUSY;
  866. addr = GTT_BAR0_MAP_REG_USDM_RAM +
  867. USTORM_FLR_FINAL_ACK_OFFSET(p_hwfn->rel_pf_id);
  868. if (is_vf)
  869. id += 0x10;
  870. command |= X_FINAL_CLEANUP_AGG_INT <<
  871. SDM_AGG_INT_COMP_PARAMS_AGG_INT_INDEX_SHIFT;
  872. command |= 1 << SDM_AGG_INT_COMP_PARAMS_AGG_VECTOR_ENABLE_SHIFT;
  873. command |= id << SDM_AGG_INT_COMP_PARAMS_AGG_VECTOR_BIT_SHIFT;
  874. command |= SDM_COMP_TYPE_AGG_INT << SDM_OP_GEN_COMP_TYPE_SHIFT;
  875. /* Make sure notification is not set before initiating final cleanup */
  876. if (REG_RD(p_hwfn, addr)) {
  877. DP_NOTICE(p_hwfn,
  878. "Unexpected; Found final cleanup notification before initiating final cleanup\n");
  879. REG_WR(p_hwfn, addr, 0);
  880. }
  881. DP_VERBOSE(p_hwfn, QED_MSG_IOV,
  882. "Sending final cleanup for PFVF[%d] [Command %08x\n]",
  883. id, command);
  884. qed_wr(p_hwfn, p_ptt, XSDM_REG_OPERATION_GEN, command);
  885. /* Poll until completion */
  886. while (!REG_RD(p_hwfn, addr) && count--)
  887. msleep(FINAL_CLEANUP_POLL_TIME);
  888. if (REG_RD(p_hwfn, addr))
  889. rc = 0;
  890. else
  891. DP_NOTICE(p_hwfn,
  892. "Failed to receive FW final cleanup notification\n");
  893. /* Cleanup afterwards */
  894. REG_WR(p_hwfn, addr, 0);
  895. return rc;
  896. }
  897. static int qed_calc_hw_mode(struct qed_hwfn *p_hwfn)
  898. {
  899. int hw_mode = 0;
  900. if (QED_IS_BB_B0(p_hwfn->cdev)) {
  901. hw_mode |= 1 << MODE_BB;
  902. } else if (QED_IS_AH(p_hwfn->cdev)) {
  903. hw_mode |= 1 << MODE_K2;
  904. } else {
  905. DP_NOTICE(p_hwfn, "Unknown chip type %#x\n",
  906. p_hwfn->cdev->type);
  907. return -EINVAL;
  908. }
  909. switch (p_hwfn->cdev->num_ports_in_engine) {
  910. case 1:
  911. hw_mode |= 1 << MODE_PORTS_PER_ENG_1;
  912. break;
  913. case 2:
  914. hw_mode |= 1 << MODE_PORTS_PER_ENG_2;
  915. break;
  916. case 4:
  917. hw_mode |= 1 << MODE_PORTS_PER_ENG_4;
  918. break;
  919. default:
  920. DP_NOTICE(p_hwfn, "num_ports_in_engine = %d not supported\n",
  921. p_hwfn->cdev->num_ports_in_engine);
  922. return -EINVAL;
  923. }
  924. switch (p_hwfn->cdev->mf_mode) {
  925. case QED_MF_DEFAULT:
  926. case QED_MF_NPAR:
  927. hw_mode |= 1 << MODE_MF_SI;
  928. break;
  929. case QED_MF_OVLAN:
  930. hw_mode |= 1 << MODE_MF_SD;
  931. break;
  932. default:
  933. DP_NOTICE(p_hwfn, "Unsupported MF mode, init as DEFAULT\n");
  934. hw_mode |= 1 << MODE_MF_SI;
  935. }
  936. hw_mode |= 1 << MODE_ASIC;
  937. if (p_hwfn->cdev->num_hwfns > 1)
  938. hw_mode |= 1 << MODE_100G;
  939. p_hwfn->hw_info.hw_mode = hw_mode;
  940. DP_VERBOSE(p_hwfn, (NETIF_MSG_PROBE | NETIF_MSG_IFUP),
  941. "Configuring function for hw_mode: 0x%08x\n",
  942. p_hwfn->hw_info.hw_mode);
  943. return 0;
  944. }
  945. /* Init run time data for all PFs on an engine. */
  946. static void qed_init_cau_rt_data(struct qed_dev *cdev)
  947. {
  948. u32 offset = CAU_REG_SB_VAR_MEMORY_RT_OFFSET;
  949. int i, sb_id;
  950. for_each_hwfn(cdev, i) {
  951. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  952. struct qed_igu_info *p_igu_info;
  953. struct qed_igu_block *p_block;
  954. struct cau_sb_entry sb_entry;
  955. p_igu_info = p_hwfn->hw_info.p_igu_info;
  956. for (sb_id = 0; sb_id < QED_MAPPING_MEMORY_SIZE(cdev);
  957. sb_id++) {
  958. p_block = &p_igu_info->igu_map.igu_blocks[sb_id];
  959. if (!p_block->is_pf)
  960. continue;
  961. qed_init_cau_sb_entry(p_hwfn, &sb_entry,
  962. p_block->function_id, 0, 0);
  963. STORE_RT_REG_AGG(p_hwfn, offset + sb_id * 2, sb_entry);
  964. }
  965. }
  966. }
  967. static void qed_init_cache_line_size(struct qed_hwfn *p_hwfn,
  968. struct qed_ptt *p_ptt)
  969. {
  970. u32 val, wr_mbs, cache_line_size;
  971. val = qed_rd(p_hwfn, p_ptt, PSWRQ2_REG_WR_MBS0);
  972. switch (val) {
  973. case 0:
  974. wr_mbs = 128;
  975. break;
  976. case 1:
  977. wr_mbs = 256;
  978. break;
  979. case 2:
  980. wr_mbs = 512;
  981. break;
  982. default:
  983. DP_INFO(p_hwfn,
  984. "Unexpected value of PSWRQ2_REG_WR_MBS0 [0x%x]. Avoid configuring PGLUE_B_REG_CACHE_LINE_SIZE.\n",
  985. val);
  986. return;
  987. }
  988. cache_line_size = min_t(u32, L1_CACHE_BYTES, wr_mbs);
  989. switch (cache_line_size) {
  990. case 32:
  991. val = 0;
  992. break;
  993. case 64:
  994. val = 1;
  995. break;
  996. case 128:
  997. val = 2;
  998. break;
  999. case 256:
  1000. val = 3;
  1001. break;
  1002. default:
  1003. DP_INFO(p_hwfn,
  1004. "Unexpected value of cache line size [0x%x]. Avoid configuring PGLUE_B_REG_CACHE_LINE_SIZE.\n",
  1005. cache_line_size);
  1006. }
  1007. if (L1_CACHE_BYTES > wr_mbs)
  1008. DP_INFO(p_hwfn,
  1009. "The cache line size for padding is suboptimal for performance [OS cache line size 0x%x, wr mbs 0x%x]\n",
  1010. L1_CACHE_BYTES, wr_mbs);
  1011. STORE_RT_REG(p_hwfn, PGLUE_REG_B_CACHE_LINE_SIZE_RT_OFFSET, val);
  1012. }
  1013. static int qed_hw_init_common(struct qed_hwfn *p_hwfn,
  1014. struct qed_ptt *p_ptt, int hw_mode)
  1015. {
  1016. struct qed_qm_info *qm_info = &p_hwfn->qm_info;
  1017. struct qed_qm_common_rt_init_params params;
  1018. struct qed_dev *cdev = p_hwfn->cdev;
  1019. u8 vf_id, max_num_vfs;
  1020. u16 num_pfs, pf_id;
  1021. u32 concrete_fid;
  1022. int rc = 0;
  1023. qed_init_cau_rt_data(cdev);
  1024. /* Program GTT windows */
  1025. qed_gtt_init(p_hwfn);
  1026. if (p_hwfn->mcp_info) {
  1027. if (p_hwfn->mcp_info->func_info.bandwidth_max)
  1028. qm_info->pf_rl_en = 1;
  1029. if (p_hwfn->mcp_info->func_info.bandwidth_min)
  1030. qm_info->pf_wfq_en = 1;
  1031. }
  1032. memset(&params, 0, sizeof(params));
  1033. params.max_ports_per_engine = p_hwfn->cdev->num_ports_in_engine;
  1034. params.max_phys_tcs_per_port = qm_info->max_phys_tcs_per_port;
  1035. params.pf_rl_en = qm_info->pf_rl_en;
  1036. params.pf_wfq_en = qm_info->pf_wfq_en;
  1037. params.vport_rl_en = qm_info->vport_rl_en;
  1038. params.vport_wfq_en = qm_info->vport_wfq_en;
  1039. params.port_params = qm_info->qm_port_params;
  1040. qed_qm_common_rt_init(p_hwfn, &params);
  1041. qed_cxt_hw_init_common(p_hwfn);
  1042. qed_init_cache_line_size(p_hwfn, p_ptt);
  1043. rc = qed_init_run(p_hwfn, p_ptt, PHASE_ENGINE, ANY_PHASE_ID, hw_mode);
  1044. if (rc)
  1045. return rc;
  1046. qed_wr(p_hwfn, p_ptt, PSWRQ2_REG_L2P_VALIDATE_VFID, 0);
  1047. qed_wr(p_hwfn, p_ptt, PGLUE_B_REG_USE_CLIENTID_IN_TAG, 1);
  1048. if (QED_IS_BB(p_hwfn->cdev)) {
  1049. num_pfs = NUM_OF_ENG_PFS(p_hwfn->cdev);
  1050. for (pf_id = 0; pf_id < num_pfs; pf_id++) {
  1051. qed_fid_pretend(p_hwfn, p_ptt, pf_id);
  1052. qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
  1053. qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
  1054. }
  1055. /* pretend to original PF */
  1056. qed_fid_pretend(p_hwfn, p_ptt, p_hwfn->rel_pf_id);
  1057. }
  1058. max_num_vfs = QED_IS_AH(cdev) ? MAX_NUM_VFS_K2 : MAX_NUM_VFS_BB;
  1059. for (vf_id = 0; vf_id < max_num_vfs; vf_id++) {
  1060. concrete_fid = qed_vfid_to_concrete(p_hwfn, vf_id);
  1061. qed_fid_pretend(p_hwfn, p_ptt, (u16) concrete_fid);
  1062. qed_wr(p_hwfn, p_ptt, CCFC_REG_STRONG_ENABLE_VF, 0x1);
  1063. qed_wr(p_hwfn, p_ptt, CCFC_REG_WEAK_ENABLE_VF, 0x0);
  1064. qed_wr(p_hwfn, p_ptt, TCFC_REG_STRONG_ENABLE_VF, 0x1);
  1065. qed_wr(p_hwfn, p_ptt, TCFC_REG_WEAK_ENABLE_VF, 0x0);
  1066. }
  1067. /* pretend to original PF */
  1068. qed_fid_pretend(p_hwfn, p_ptt, p_hwfn->rel_pf_id);
  1069. return rc;
  1070. }
  1071. static int
  1072. qed_hw_init_dpi_size(struct qed_hwfn *p_hwfn,
  1073. struct qed_ptt *p_ptt, u32 pwm_region_size, u32 n_cpus)
  1074. {
  1075. u32 dpi_bit_shift, dpi_count, dpi_page_size;
  1076. u32 min_dpis;
  1077. u32 n_wids;
  1078. /* Calculate DPI size */
  1079. n_wids = max_t(u32, QED_MIN_WIDS, n_cpus);
  1080. dpi_page_size = QED_WID_SIZE * roundup_pow_of_two(n_wids);
  1081. dpi_page_size = (dpi_page_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
  1082. dpi_bit_shift = ilog2(dpi_page_size / 4096);
  1083. dpi_count = pwm_region_size / dpi_page_size;
  1084. min_dpis = p_hwfn->pf_params.rdma_pf_params.min_dpis;
  1085. min_dpis = max_t(u32, QED_MIN_DPIS, min_dpis);
  1086. p_hwfn->dpi_size = dpi_page_size;
  1087. p_hwfn->dpi_count = dpi_count;
  1088. qed_wr(p_hwfn, p_ptt, DORQ_REG_PF_DPI_BIT_SHIFT, dpi_bit_shift);
  1089. if (dpi_count < min_dpis)
  1090. return -EINVAL;
  1091. return 0;
  1092. }
  1093. enum QED_ROCE_EDPM_MODE {
  1094. QED_ROCE_EDPM_MODE_ENABLE = 0,
  1095. QED_ROCE_EDPM_MODE_FORCE_ON = 1,
  1096. QED_ROCE_EDPM_MODE_DISABLE = 2,
  1097. };
  1098. static int
  1099. qed_hw_init_pf_doorbell_bar(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  1100. {
  1101. u32 pwm_regsize, norm_regsize;
  1102. u32 non_pwm_conn, min_addr_reg1;
  1103. u32 db_bar_size, n_cpus = 1;
  1104. u32 roce_edpm_mode;
  1105. u32 pf_dems_shift;
  1106. int rc = 0;
  1107. u8 cond;
  1108. db_bar_size = qed_hw_bar_size(p_hwfn, p_ptt, BAR_ID_1);
  1109. if (p_hwfn->cdev->num_hwfns > 1)
  1110. db_bar_size /= 2;
  1111. /* Calculate doorbell regions */
  1112. non_pwm_conn = qed_cxt_get_proto_cid_start(p_hwfn, PROTOCOLID_CORE) +
  1113. qed_cxt_get_proto_cid_count(p_hwfn, PROTOCOLID_CORE,
  1114. NULL) +
  1115. qed_cxt_get_proto_cid_count(p_hwfn, PROTOCOLID_ETH,
  1116. NULL);
  1117. norm_regsize = roundup(QED_PF_DEMS_SIZE * non_pwm_conn, PAGE_SIZE);
  1118. min_addr_reg1 = norm_regsize / 4096;
  1119. pwm_regsize = db_bar_size - norm_regsize;
  1120. /* Check that the normal and PWM sizes are valid */
  1121. if (db_bar_size < norm_regsize) {
  1122. DP_ERR(p_hwfn->cdev,
  1123. "Doorbell BAR size 0x%x is too small (normal region is 0x%0x )\n",
  1124. db_bar_size, norm_regsize);
  1125. return -EINVAL;
  1126. }
  1127. if (pwm_regsize < QED_MIN_PWM_REGION) {
  1128. DP_ERR(p_hwfn->cdev,
  1129. "PWM region size 0x%0x is too small. Should be at least 0x%0x (Doorbell BAR size is 0x%x and normal region size is 0x%0x)\n",
  1130. pwm_regsize,
  1131. QED_MIN_PWM_REGION, db_bar_size, norm_regsize);
  1132. return -EINVAL;
  1133. }
  1134. /* Calculate number of DPIs */
  1135. roce_edpm_mode = p_hwfn->pf_params.rdma_pf_params.roce_edpm_mode;
  1136. if ((roce_edpm_mode == QED_ROCE_EDPM_MODE_ENABLE) ||
  1137. ((roce_edpm_mode == QED_ROCE_EDPM_MODE_FORCE_ON))) {
  1138. /* Either EDPM is mandatory, or we are attempting to allocate a
  1139. * WID per CPU.
  1140. */
  1141. n_cpus = num_present_cpus();
  1142. rc = qed_hw_init_dpi_size(p_hwfn, p_ptt, pwm_regsize, n_cpus);
  1143. }
  1144. cond = (rc && (roce_edpm_mode == QED_ROCE_EDPM_MODE_ENABLE)) ||
  1145. (roce_edpm_mode == QED_ROCE_EDPM_MODE_DISABLE);
  1146. if (cond || p_hwfn->dcbx_no_edpm) {
  1147. /* Either EDPM is disabled from user configuration, or it is
  1148. * disabled via DCBx, or it is not mandatory and we failed to
  1149. * allocated a WID per CPU.
  1150. */
  1151. n_cpus = 1;
  1152. rc = qed_hw_init_dpi_size(p_hwfn, p_ptt, pwm_regsize, n_cpus);
  1153. if (cond)
  1154. qed_rdma_dpm_bar(p_hwfn, p_ptt);
  1155. }
  1156. p_hwfn->wid_count = (u16) n_cpus;
  1157. DP_INFO(p_hwfn,
  1158. "doorbell bar: normal_region_size=%d, pwm_region_size=%d, dpi_size=%d, dpi_count=%d, roce_edpm=%s\n",
  1159. norm_regsize,
  1160. pwm_regsize,
  1161. p_hwfn->dpi_size,
  1162. p_hwfn->dpi_count,
  1163. ((p_hwfn->dcbx_no_edpm) || (p_hwfn->db_bar_no_edpm)) ?
  1164. "disabled" : "enabled");
  1165. if (rc) {
  1166. DP_ERR(p_hwfn,
  1167. "Failed to allocate enough DPIs. Allocated %d but the current minimum is %d.\n",
  1168. p_hwfn->dpi_count,
  1169. p_hwfn->pf_params.rdma_pf_params.min_dpis);
  1170. return -EINVAL;
  1171. }
  1172. p_hwfn->dpi_start_offset = norm_regsize;
  1173. /* DEMS size is configured log2 of DWORDs, hence the division by 4 */
  1174. pf_dems_shift = ilog2(QED_PF_DEMS_SIZE / 4);
  1175. qed_wr(p_hwfn, p_ptt, DORQ_REG_PF_ICID_BIT_SHIFT_NORM, pf_dems_shift);
  1176. qed_wr(p_hwfn, p_ptt, DORQ_REG_PF_MIN_ADDR_REG1, min_addr_reg1);
  1177. return 0;
  1178. }
  1179. static int qed_hw_init_port(struct qed_hwfn *p_hwfn,
  1180. struct qed_ptt *p_ptt, int hw_mode)
  1181. {
  1182. return qed_init_run(p_hwfn, p_ptt, PHASE_PORT,
  1183. p_hwfn->port_id, hw_mode);
  1184. }
  1185. static int qed_hw_init_pf(struct qed_hwfn *p_hwfn,
  1186. struct qed_ptt *p_ptt,
  1187. struct qed_tunnel_info *p_tunn,
  1188. int hw_mode,
  1189. bool b_hw_start,
  1190. enum qed_int_mode int_mode,
  1191. bool allow_npar_tx_switch)
  1192. {
  1193. u8 rel_pf_id = p_hwfn->rel_pf_id;
  1194. int rc = 0;
  1195. if (p_hwfn->mcp_info) {
  1196. struct qed_mcp_function_info *p_info;
  1197. p_info = &p_hwfn->mcp_info->func_info;
  1198. if (p_info->bandwidth_min)
  1199. p_hwfn->qm_info.pf_wfq = p_info->bandwidth_min;
  1200. /* Update rate limit once we'll actually have a link */
  1201. p_hwfn->qm_info.pf_rl = 100000;
  1202. }
  1203. qed_cxt_hw_init_pf(p_hwfn, p_ptt);
  1204. qed_int_igu_init_rt(p_hwfn);
  1205. /* Set VLAN in NIG if needed */
  1206. if (hw_mode & BIT(MODE_MF_SD)) {
  1207. DP_VERBOSE(p_hwfn, NETIF_MSG_HW, "Configuring LLH_FUNC_TAG\n");
  1208. STORE_RT_REG(p_hwfn, NIG_REG_LLH_FUNC_TAG_EN_RT_OFFSET, 1);
  1209. STORE_RT_REG(p_hwfn, NIG_REG_LLH_FUNC_TAG_VALUE_RT_OFFSET,
  1210. p_hwfn->hw_info.ovlan);
  1211. }
  1212. /* Enable classification by MAC if needed */
  1213. if (hw_mode & BIT(MODE_MF_SI)) {
  1214. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  1215. "Configuring TAGMAC_CLS_TYPE\n");
  1216. STORE_RT_REG(p_hwfn,
  1217. NIG_REG_LLH_FUNC_TAGMAC_CLS_TYPE_RT_OFFSET, 1);
  1218. }
  1219. /* Protocl Configuration */
  1220. STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_TCP_RT_OFFSET,
  1221. (p_hwfn->hw_info.personality == QED_PCI_ISCSI) ? 1 : 0);
  1222. STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_FCOE_RT_OFFSET,
  1223. (p_hwfn->hw_info.personality == QED_PCI_FCOE) ? 1 : 0);
  1224. STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_ROCE_RT_OFFSET, 0);
  1225. /* Cleanup chip from previous driver if such remains exist */
  1226. rc = qed_final_cleanup(p_hwfn, p_ptt, rel_pf_id, false);
  1227. if (rc)
  1228. return rc;
  1229. /* PF Init sequence */
  1230. rc = qed_init_run(p_hwfn, p_ptt, PHASE_PF, rel_pf_id, hw_mode);
  1231. if (rc)
  1232. return rc;
  1233. /* QM_PF Init sequence (may be invoked separately e.g. for DCB) */
  1234. rc = qed_init_run(p_hwfn, p_ptt, PHASE_QM_PF, rel_pf_id, hw_mode);
  1235. if (rc)
  1236. return rc;
  1237. /* Pure runtime initializations - directly to the HW */
  1238. qed_int_igu_init_pure_rt(p_hwfn, p_ptt, true, true);
  1239. rc = qed_hw_init_pf_doorbell_bar(p_hwfn, p_ptt);
  1240. if (rc)
  1241. return rc;
  1242. if (b_hw_start) {
  1243. /* enable interrupts */
  1244. qed_int_igu_enable(p_hwfn, p_ptt, int_mode);
  1245. /* send function start command */
  1246. rc = qed_sp_pf_start(p_hwfn, p_ptt, p_tunn,
  1247. p_hwfn->cdev->mf_mode,
  1248. allow_npar_tx_switch);
  1249. if (rc) {
  1250. DP_NOTICE(p_hwfn, "Function start ramrod failed\n");
  1251. return rc;
  1252. }
  1253. if (p_hwfn->hw_info.personality == QED_PCI_FCOE) {
  1254. qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TAG1, BIT(2));
  1255. qed_wr(p_hwfn, p_ptt,
  1256. PRS_REG_PKT_LEN_STAT_TAGS_NOT_COUNTED_FIRST,
  1257. 0x100);
  1258. }
  1259. }
  1260. return rc;
  1261. }
  1262. static int qed_change_pci_hwfn(struct qed_hwfn *p_hwfn,
  1263. struct qed_ptt *p_ptt,
  1264. u8 enable)
  1265. {
  1266. u32 delay_idx = 0, val, set_val = enable ? 1 : 0;
  1267. /* Change PF in PXP */
  1268. qed_wr(p_hwfn, p_ptt,
  1269. PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, set_val);
  1270. /* wait until value is set - try for 1 second every 50us */
  1271. for (delay_idx = 0; delay_idx < 20000; delay_idx++) {
  1272. val = qed_rd(p_hwfn, p_ptt,
  1273. PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
  1274. if (val == set_val)
  1275. break;
  1276. usleep_range(50, 60);
  1277. }
  1278. if (val != set_val) {
  1279. DP_NOTICE(p_hwfn,
  1280. "PFID_ENABLE_MASTER wasn't changed after a second\n");
  1281. return -EAGAIN;
  1282. }
  1283. return 0;
  1284. }
  1285. static void qed_reset_mb_shadow(struct qed_hwfn *p_hwfn,
  1286. struct qed_ptt *p_main_ptt)
  1287. {
  1288. /* Read shadow of current MFW mailbox */
  1289. qed_mcp_read_mb(p_hwfn, p_main_ptt);
  1290. memcpy(p_hwfn->mcp_info->mfw_mb_shadow,
  1291. p_hwfn->mcp_info->mfw_mb_cur, p_hwfn->mcp_info->mfw_mb_length);
  1292. }
  1293. static void
  1294. qed_fill_load_req_params(struct qed_load_req_params *p_load_req,
  1295. struct qed_drv_load_params *p_drv_load)
  1296. {
  1297. memset(p_load_req, 0, sizeof(*p_load_req));
  1298. p_load_req->drv_role = p_drv_load->is_crash_kernel ?
  1299. QED_DRV_ROLE_KDUMP : QED_DRV_ROLE_OS;
  1300. p_load_req->timeout_val = p_drv_load->mfw_timeout_val;
  1301. p_load_req->avoid_eng_reset = p_drv_load->avoid_eng_reset;
  1302. p_load_req->override_force_load = p_drv_load->override_force_load;
  1303. }
  1304. static int qed_vf_start(struct qed_hwfn *p_hwfn,
  1305. struct qed_hw_init_params *p_params)
  1306. {
  1307. if (p_params->p_tunn) {
  1308. qed_vf_set_vf_start_tunn_update_param(p_params->p_tunn);
  1309. qed_vf_pf_tunnel_param_update(p_hwfn, p_params->p_tunn);
  1310. }
  1311. p_hwfn->b_int_enabled = 1;
  1312. return 0;
  1313. }
  1314. int qed_hw_init(struct qed_dev *cdev, struct qed_hw_init_params *p_params)
  1315. {
  1316. struct qed_load_req_params load_req_params;
  1317. u32 load_code, param, drv_mb_param;
  1318. bool b_default_mtu = true;
  1319. struct qed_hwfn *p_hwfn;
  1320. int rc = 0, mfw_rc, i;
  1321. if ((p_params->int_mode == QED_INT_MODE_MSI) && (cdev->num_hwfns > 1)) {
  1322. DP_NOTICE(cdev, "MSI mode is not supported for CMT devices\n");
  1323. return -EINVAL;
  1324. }
  1325. if (IS_PF(cdev)) {
  1326. rc = qed_init_fw_data(cdev, p_params->bin_fw_data);
  1327. if (rc)
  1328. return rc;
  1329. }
  1330. for_each_hwfn(cdev, i) {
  1331. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  1332. /* If management didn't provide a default, set one of our own */
  1333. if (!p_hwfn->hw_info.mtu) {
  1334. p_hwfn->hw_info.mtu = 1500;
  1335. b_default_mtu = false;
  1336. }
  1337. if (IS_VF(cdev)) {
  1338. qed_vf_start(p_hwfn, p_params);
  1339. continue;
  1340. }
  1341. /* Enable DMAE in PXP */
  1342. rc = qed_change_pci_hwfn(p_hwfn, p_hwfn->p_main_ptt, true);
  1343. rc = qed_calc_hw_mode(p_hwfn);
  1344. if (rc)
  1345. return rc;
  1346. qed_fill_load_req_params(&load_req_params,
  1347. p_params->p_drv_load_params);
  1348. rc = qed_mcp_load_req(p_hwfn, p_hwfn->p_main_ptt,
  1349. &load_req_params);
  1350. if (rc) {
  1351. DP_NOTICE(p_hwfn, "Failed sending a LOAD_REQ command\n");
  1352. return rc;
  1353. }
  1354. load_code = load_req_params.load_code;
  1355. DP_VERBOSE(p_hwfn, QED_MSG_SP,
  1356. "Load request was sent. Load code: 0x%x\n",
  1357. load_code);
  1358. qed_reset_mb_shadow(p_hwfn, p_hwfn->p_main_ptt);
  1359. p_hwfn->first_on_engine = (load_code ==
  1360. FW_MSG_CODE_DRV_LOAD_ENGINE);
  1361. switch (load_code) {
  1362. case FW_MSG_CODE_DRV_LOAD_ENGINE:
  1363. rc = qed_hw_init_common(p_hwfn, p_hwfn->p_main_ptt,
  1364. p_hwfn->hw_info.hw_mode);
  1365. if (rc)
  1366. break;
  1367. /* Fall into */
  1368. case FW_MSG_CODE_DRV_LOAD_PORT:
  1369. rc = qed_hw_init_port(p_hwfn, p_hwfn->p_main_ptt,
  1370. p_hwfn->hw_info.hw_mode);
  1371. if (rc)
  1372. break;
  1373. /* Fall into */
  1374. case FW_MSG_CODE_DRV_LOAD_FUNCTION:
  1375. rc = qed_hw_init_pf(p_hwfn, p_hwfn->p_main_ptt,
  1376. p_params->p_tunn,
  1377. p_hwfn->hw_info.hw_mode,
  1378. p_params->b_hw_start,
  1379. p_params->int_mode,
  1380. p_params->allow_npar_tx_switch);
  1381. break;
  1382. default:
  1383. DP_NOTICE(p_hwfn,
  1384. "Unexpected load code [0x%08x]", load_code);
  1385. rc = -EINVAL;
  1386. break;
  1387. }
  1388. if (rc)
  1389. DP_NOTICE(p_hwfn,
  1390. "init phase failed for loadcode 0x%x (rc %d)\n",
  1391. load_code, rc);
  1392. /* ACK mfw regardless of success or failure of initialization */
  1393. mfw_rc = qed_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
  1394. DRV_MSG_CODE_LOAD_DONE,
  1395. 0, &load_code, &param);
  1396. if (rc)
  1397. return rc;
  1398. if (mfw_rc) {
  1399. DP_NOTICE(p_hwfn, "Failed sending LOAD_DONE command\n");
  1400. return mfw_rc;
  1401. }
  1402. /* Check if there is a DID mismatch between nvm-cfg/efuse */
  1403. if (param & FW_MB_PARAM_LOAD_DONE_DID_EFUSE_ERROR)
  1404. DP_NOTICE(p_hwfn,
  1405. "warning: device configuration is not supported on this board type. The device may not function as expected.\n");
  1406. /* send DCBX attention request command */
  1407. DP_VERBOSE(p_hwfn,
  1408. QED_MSG_DCB,
  1409. "sending phony dcbx set command to trigger DCBx attention handling\n");
  1410. mfw_rc = qed_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
  1411. DRV_MSG_CODE_SET_DCBX,
  1412. 1 << DRV_MB_PARAM_DCBX_NOTIFY_SHIFT,
  1413. &load_code, &param);
  1414. if (mfw_rc) {
  1415. DP_NOTICE(p_hwfn,
  1416. "Failed to send DCBX attention request\n");
  1417. return mfw_rc;
  1418. }
  1419. p_hwfn->hw_init_done = true;
  1420. }
  1421. if (IS_PF(cdev)) {
  1422. p_hwfn = QED_LEADING_HWFN(cdev);
  1423. drv_mb_param = STORM_FW_VERSION;
  1424. rc = qed_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
  1425. DRV_MSG_CODE_OV_UPDATE_STORM_FW_VER,
  1426. drv_mb_param, &load_code, &param);
  1427. if (rc)
  1428. DP_INFO(p_hwfn, "Failed to update firmware version\n");
  1429. if (!b_default_mtu) {
  1430. rc = qed_mcp_ov_update_mtu(p_hwfn, p_hwfn->p_main_ptt,
  1431. p_hwfn->hw_info.mtu);
  1432. if (rc)
  1433. DP_INFO(p_hwfn,
  1434. "Failed to update default mtu\n");
  1435. }
  1436. rc = qed_mcp_ov_update_driver_state(p_hwfn,
  1437. p_hwfn->p_main_ptt,
  1438. QED_OV_DRIVER_STATE_DISABLED);
  1439. if (rc)
  1440. DP_INFO(p_hwfn, "Failed to update driver state\n");
  1441. rc = qed_mcp_ov_update_eswitch(p_hwfn, p_hwfn->p_main_ptt,
  1442. QED_OV_ESWITCH_VEB);
  1443. if (rc)
  1444. DP_INFO(p_hwfn, "Failed to update eswitch mode\n");
  1445. }
  1446. return 0;
  1447. }
  1448. #define QED_HW_STOP_RETRY_LIMIT (10)
  1449. static void qed_hw_timers_stop(struct qed_dev *cdev,
  1450. struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  1451. {
  1452. int i;
  1453. /* close timers */
  1454. qed_wr(p_hwfn, p_ptt, TM_REG_PF_ENABLE_CONN, 0x0);
  1455. qed_wr(p_hwfn, p_ptt, TM_REG_PF_ENABLE_TASK, 0x0);
  1456. for (i = 0; i < QED_HW_STOP_RETRY_LIMIT; i++) {
  1457. if ((!qed_rd(p_hwfn, p_ptt,
  1458. TM_REG_PF_SCAN_ACTIVE_CONN)) &&
  1459. (!qed_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_TASK)))
  1460. break;
  1461. /* Dependent on number of connection/tasks, possibly
  1462. * 1ms sleep is required between polls
  1463. */
  1464. usleep_range(1000, 2000);
  1465. }
  1466. if (i < QED_HW_STOP_RETRY_LIMIT)
  1467. return;
  1468. DP_NOTICE(p_hwfn,
  1469. "Timers linear scans are not over [Connection %02x Tasks %02x]\n",
  1470. (u8)qed_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_CONN),
  1471. (u8)qed_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_TASK));
  1472. }
  1473. void qed_hw_timers_stop_all(struct qed_dev *cdev)
  1474. {
  1475. int j;
  1476. for_each_hwfn(cdev, j) {
  1477. struct qed_hwfn *p_hwfn = &cdev->hwfns[j];
  1478. struct qed_ptt *p_ptt = p_hwfn->p_main_ptt;
  1479. qed_hw_timers_stop(cdev, p_hwfn, p_ptt);
  1480. }
  1481. }
  1482. int qed_hw_stop(struct qed_dev *cdev)
  1483. {
  1484. struct qed_hwfn *p_hwfn;
  1485. struct qed_ptt *p_ptt;
  1486. int rc, rc2 = 0;
  1487. int j;
  1488. for_each_hwfn(cdev, j) {
  1489. p_hwfn = &cdev->hwfns[j];
  1490. p_ptt = p_hwfn->p_main_ptt;
  1491. DP_VERBOSE(p_hwfn, NETIF_MSG_IFDOWN, "Stopping hw/fw\n");
  1492. if (IS_VF(cdev)) {
  1493. qed_vf_pf_int_cleanup(p_hwfn);
  1494. rc = qed_vf_pf_reset(p_hwfn);
  1495. if (rc) {
  1496. DP_NOTICE(p_hwfn,
  1497. "qed_vf_pf_reset failed. rc = %d.\n",
  1498. rc);
  1499. rc2 = -EINVAL;
  1500. }
  1501. continue;
  1502. }
  1503. /* mark the hw as uninitialized... */
  1504. p_hwfn->hw_init_done = false;
  1505. /* Send unload command to MCP */
  1506. rc = qed_mcp_unload_req(p_hwfn, p_ptt);
  1507. if (rc) {
  1508. DP_NOTICE(p_hwfn,
  1509. "Failed sending a UNLOAD_REQ command. rc = %d.\n",
  1510. rc);
  1511. rc2 = -EINVAL;
  1512. }
  1513. qed_slowpath_irq_sync(p_hwfn);
  1514. /* After this point no MFW attentions are expected, e.g. prevent
  1515. * race between pf stop and dcbx pf update.
  1516. */
  1517. rc = qed_sp_pf_stop(p_hwfn);
  1518. if (rc) {
  1519. DP_NOTICE(p_hwfn,
  1520. "Failed to close PF against FW [rc = %d]. Continue to stop HW to prevent illegal host access by the device.\n",
  1521. rc);
  1522. rc2 = -EINVAL;
  1523. }
  1524. qed_wr(p_hwfn, p_ptt,
  1525. NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x1);
  1526. qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
  1527. qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_UDP, 0x0);
  1528. qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_FCOE, 0x0);
  1529. qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
  1530. qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_OPENFLOW, 0x0);
  1531. qed_hw_timers_stop(cdev, p_hwfn, p_ptt);
  1532. /* Disable Attention Generation */
  1533. qed_int_igu_disable_int(p_hwfn, p_ptt);
  1534. qed_wr(p_hwfn, p_ptt, IGU_REG_LEADING_EDGE_LATCH, 0);
  1535. qed_wr(p_hwfn, p_ptt, IGU_REG_TRAILING_EDGE_LATCH, 0);
  1536. qed_int_igu_init_pure_rt(p_hwfn, p_ptt, false, true);
  1537. /* Need to wait 1ms to guarantee SBs are cleared */
  1538. usleep_range(1000, 2000);
  1539. /* Disable PF in HW blocks */
  1540. qed_wr(p_hwfn, p_ptt, DORQ_REG_PF_DB_ENABLE, 0);
  1541. qed_wr(p_hwfn, p_ptt, QM_REG_PF_EN, 0);
  1542. qed_mcp_unload_done(p_hwfn, p_ptt);
  1543. if (rc) {
  1544. DP_NOTICE(p_hwfn,
  1545. "Failed sending a UNLOAD_DONE command. rc = %d.\n",
  1546. rc);
  1547. rc2 = -EINVAL;
  1548. }
  1549. }
  1550. if (IS_PF(cdev)) {
  1551. p_hwfn = QED_LEADING_HWFN(cdev);
  1552. p_ptt = QED_LEADING_HWFN(cdev)->p_main_ptt;
  1553. /* Disable DMAE in PXP - in CMT, this should only be done for
  1554. * first hw-function, and only after all transactions have
  1555. * stopped for all active hw-functions.
  1556. */
  1557. rc = qed_change_pci_hwfn(p_hwfn, p_ptt, false);
  1558. if (rc) {
  1559. DP_NOTICE(p_hwfn,
  1560. "qed_change_pci_hwfn failed. rc = %d.\n", rc);
  1561. rc2 = -EINVAL;
  1562. }
  1563. }
  1564. return rc2;
  1565. }
  1566. int qed_hw_stop_fastpath(struct qed_dev *cdev)
  1567. {
  1568. int j;
  1569. for_each_hwfn(cdev, j) {
  1570. struct qed_hwfn *p_hwfn = &cdev->hwfns[j];
  1571. struct qed_ptt *p_ptt;
  1572. if (IS_VF(cdev)) {
  1573. qed_vf_pf_int_cleanup(p_hwfn);
  1574. continue;
  1575. }
  1576. p_ptt = qed_ptt_acquire(p_hwfn);
  1577. if (!p_ptt)
  1578. return -EAGAIN;
  1579. DP_VERBOSE(p_hwfn,
  1580. NETIF_MSG_IFDOWN, "Shutting down the fastpath\n");
  1581. qed_wr(p_hwfn, p_ptt,
  1582. NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x1);
  1583. qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
  1584. qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_UDP, 0x0);
  1585. qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_FCOE, 0x0);
  1586. qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
  1587. qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_OPENFLOW, 0x0);
  1588. qed_int_igu_init_pure_rt(p_hwfn, p_ptt, false, false);
  1589. /* Need to wait 1ms to guarantee SBs are cleared */
  1590. usleep_range(1000, 2000);
  1591. qed_ptt_release(p_hwfn, p_ptt);
  1592. }
  1593. return 0;
  1594. }
  1595. int qed_hw_start_fastpath(struct qed_hwfn *p_hwfn)
  1596. {
  1597. struct qed_ptt *p_ptt;
  1598. if (IS_VF(p_hwfn->cdev))
  1599. return 0;
  1600. p_ptt = qed_ptt_acquire(p_hwfn);
  1601. if (!p_ptt)
  1602. return -EAGAIN;
  1603. /* Re-open incoming traffic */
  1604. qed_wr(p_hwfn, p_ptt, NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x0);
  1605. qed_ptt_release(p_hwfn, p_ptt);
  1606. return 0;
  1607. }
  1608. /* Free hwfn memory and resources acquired in hw_hwfn_prepare */
  1609. static void qed_hw_hwfn_free(struct qed_hwfn *p_hwfn)
  1610. {
  1611. qed_ptt_pool_free(p_hwfn);
  1612. kfree(p_hwfn->hw_info.p_igu_info);
  1613. p_hwfn->hw_info.p_igu_info = NULL;
  1614. }
  1615. /* Setup bar access */
  1616. static void qed_hw_hwfn_prepare(struct qed_hwfn *p_hwfn)
  1617. {
  1618. /* clear indirect access */
  1619. if (QED_IS_AH(p_hwfn->cdev)) {
  1620. qed_wr(p_hwfn, p_hwfn->p_main_ptt,
  1621. PGLUE_B_REG_PGL_ADDR_E8_F0_K2, 0);
  1622. qed_wr(p_hwfn, p_hwfn->p_main_ptt,
  1623. PGLUE_B_REG_PGL_ADDR_EC_F0_K2, 0);
  1624. qed_wr(p_hwfn, p_hwfn->p_main_ptt,
  1625. PGLUE_B_REG_PGL_ADDR_F0_F0_K2, 0);
  1626. qed_wr(p_hwfn, p_hwfn->p_main_ptt,
  1627. PGLUE_B_REG_PGL_ADDR_F4_F0_K2, 0);
  1628. } else {
  1629. qed_wr(p_hwfn, p_hwfn->p_main_ptt,
  1630. PGLUE_B_REG_PGL_ADDR_88_F0_BB, 0);
  1631. qed_wr(p_hwfn, p_hwfn->p_main_ptt,
  1632. PGLUE_B_REG_PGL_ADDR_8C_F0_BB, 0);
  1633. qed_wr(p_hwfn, p_hwfn->p_main_ptt,
  1634. PGLUE_B_REG_PGL_ADDR_90_F0_BB, 0);
  1635. qed_wr(p_hwfn, p_hwfn->p_main_ptt,
  1636. PGLUE_B_REG_PGL_ADDR_94_F0_BB, 0);
  1637. }
  1638. /* Clean Previous errors if such exist */
  1639. qed_wr(p_hwfn, p_hwfn->p_main_ptt,
  1640. PGLUE_B_REG_WAS_ERROR_PF_31_0_CLR, 1 << p_hwfn->abs_pf_id);
  1641. /* enable internal target-read */
  1642. qed_wr(p_hwfn, p_hwfn->p_main_ptt,
  1643. PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
  1644. }
  1645. static void get_function_id(struct qed_hwfn *p_hwfn)
  1646. {
  1647. /* ME Register */
  1648. p_hwfn->hw_info.opaque_fid = (u16) REG_RD(p_hwfn,
  1649. PXP_PF_ME_OPAQUE_ADDR);
  1650. p_hwfn->hw_info.concrete_fid = REG_RD(p_hwfn, PXP_PF_ME_CONCRETE_ADDR);
  1651. p_hwfn->abs_pf_id = (p_hwfn->hw_info.concrete_fid >> 16) & 0xf;
  1652. p_hwfn->rel_pf_id = GET_FIELD(p_hwfn->hw_info.concrete_fid,
  1653. PXP_CONCRETE_FID_PFID);
  1654. p_hwfn->port_id = GET_FIELD(p_hwfn->hw_info.concrete_fid,
  1655. PXP_CONCRETE_FID_PORT);
  1656. DP_VERBOSE(p_hwfn, NETIF_MSG_PROBE,
  1657. "Read ME register: Concrete 0x%08x Opaque 0x%04x\n",
  1658. p_hwfn->hw_info.concrete_fid, p_hwfn->hw_info.opaque_fid);
  1659. }
  1660. static void qed_hw_set_feat(struct qed_hwfn *p_hwfn)
  1661. {
  1662. u32 *feat_num = p_hwfn->hw_info.feat_num;
  1663. struct qed_sb_cnt_info sb_cnt_info;
  1664. u32 non_l2_sbs = 0;
  1665. if (IS_ENABLED(CONFIG_QED_RDMA) &&
  1666. p_hwfn->hw_info.personality == QED_PCI_ETH_ROCE) {
  1667. /* Roce CNQ each requires: 1 status block + 1 CNQ. We divide
  1668. * the status blocks equally between L2 / RoCE but with
  1669. * consideration as to how many l2 queues / cnqs we have.
  1670. */
  1671. feat_num[QED_RDMA_CNQ] =
  1672. min_t(u32, RESC_NUM(p_hwfn, QED_SB) / 2,
  1673. RESC_NUM(p_hwfn, QED_RDMA_CNQ_RAM));
  1674. non_l2_sbs = feat_num[QED_RDMA_CNQ];
  1675. }
  1676. if (p_hwfn->hw_info.personality == QED_PCI_ETH_ROCE ||
  1677. p_hwfn->hw_info.personality == QED_PCI_ETH) {
  1678. /* Start by allocating VF queues, then PF's */
  1679. memset(&sb_cnt_info, 0, sizeof(sb_cnt_info));
  1680. qed_int_get_num_sbs(p_hwfn, &sb_cnt_info);
  1681. feat_num[QED_VF_L2_QUE] = min_t(u32,
  1682. RESC_NUM(p_hwfn, QED_L2_QUEUE),
  1683. sb_cnt_info.sb_iov_cnt);
  1684. feat_num[QED_PF_L2_QUE] = min_t(u32,
  1685. RESC_NUM(p_hwfn, QED_SB) -
  1686. non_l2_sbs,
  1687. RESC_NUM(p_hwfn,
  1688. QED_L2_QUEUE) -
  1689. FEAT_NUM(p_hwfn,
  1690. QED_VF_L2_QUE));
  1691. }
  1692. if (p_hwfn->hw_info.personality == QED_PCI_ISCSI)
  1693. feat_num[QED_ISCSI_CQ] = min_t(u32, RESC_NUM(p_hwfn, QED_SB),
  1694. RESC_NUM(p_hwfn,
  1695. QED_CMDQS_CQS));
  1696. DP_VERBOSE(p_hwfn,
  1697. NETIF_MSG_PROBE,
  1698. "#PF_L2_QUEUES=%d VF_L2_QUEUES=%d #ROCE_CNQ=%d ISCSI_CQ=%d #SBS=%d\n",
  1699. (int)FEAT_NUM(p_hwfn, QED_PF_L2_QUE),
  1700. (int)FEAT_NUM(p_hwfn, QED_VF_L2_QUE),
  1701. (int)FEAT_NUM(p_hwfn, QED_RDMA_CNQ),
  1702. (int)FEAT_NUM(p_hwfn, QED_ISCSI_CQ),
  1703. RESC_NUM(p_hwfn, QED_SB));
  1704. }
  1705. const char *qed_hw_get_resc_name(enum qed_resources res_id)
  1706. {
  1707. switch (res_id) {
  1708. case QED_L2_QUEUE:
  1709. return "L2_QUEUE";
  1710. case QED_VPORT:
  1711. return "VPORT";
  1712. case QED_RSS_ENG:
  1713. return "RSS_ENG";
  1714. case QED_PQ:
  1715. return "PQ";
  1716. case QED_RL:
  1717. return "RL";
  1718. case QED_MAC:
  1719. return "MAC";
  1720. case QED_VLAN:
  1721. return "VLAN";
  1722. case QED_RDMA_CNQ_RAM:
  1723. return "RDMA_CNQ_RAM";
  1724. case QED_ILT:
  1725. return "ILT";
  1726. case QED_LL2_QUEUE:
  1727. return "LL2_QUEUE";
  1728. case QED_CMDQS_CQS:
  1729. return "CMDQS_CQS";
  1730. case QED_RDMA_STATS_QUEUE:
  1731. return "RDMA_STATS_QUEUE";
  1732. case QED_BDQ:
  1733. return "BDQ";
  1734. case QED_SB:
  1735. return "SB";
  1736. default:
  1737. return "UNKNOWN_RESOURCE";
  1738. }
  1739. }
  1740. static int
  1741. __qed_hw_set_soft_resc_size(struct qed_hwfn *p_hwfn,
  1742. struct qed_ptt *p_ptt,
  1743. enum qed_resources res_id,
  1744. u32 resc_max_val, u32 *p_mcp_resp)
  1745. {
  1746. int rc;
  1747. rc = qed_mcp_set_resc_max_val(p_hwfn, p_ptt, res_id,
  1748. resc_max_val, p_mcp_resp);
  1749. if (rc) {
  1750. DP_NOTICE(p_hwfn,
  1751. "MFW response failure for a max value setting of resource %d [%s]\n",
  1752. res_id, qed_hw_get_resc_name(res_id));
  1753. return rc;
  1754. }
  1755. if (*p_mcp_resp != FW_MSG_CODE_RESOURCE_ALLOC_OK)
  1756. DP_INFO(p_hwfn,
  1757. "Failed to set the max value of resource %d [%s]. mcp_resp = 0x%08x.\n",
  1758. res_id, qed_hw_get_resc_name(res_id), *p_mcp_resp);
  1759. return 0;
  1760. }
  1761. static int
  1762. qed_hw_set_soft_resc_size(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  1763. {
  1764. bool b_ah = QED_IS_AH(p_hwfn->cdev);
  1765. u32 resc_max_val, mcp_resp;
  1766. u8 res_id;
  1767. int rc;
  1768. for (res_id = 0; res_id < QED_MAX_RESC; res_id++) {
  1769. switch (res_id) {
  1770. case QED_LL2_QUEUE:
  1771. resc_max_val = MAX_NUM_LL2_RX_QUEUES;
  1772. break;
  1773. case QED_RDMA_CNQ_RAM:
  1774. /* No need for a case for QED_CMDQS_CQS since
  1775. * CNQ/CMDQS are the same resource.
  1776. */
  1777. resc_max_val = NUM_OF_CMDQS_CQS;
  1778. break;
  1779. case QED_RDMA_STATS_QUEUE:
  1780. resc_max_val = b_ah ? RDMA_NUM_STATISTIC_COUNTERS_K2
  1781. : RDMA_NUM_STATISTIC_COUNTERS_BB;
  1782. break;
  1783. case QED_BDQ:
  1784. resc_max_val = BDQ_NUM_RESOURCES;
  1785. break;
  1786. default:
  1787. continue;
  1788. }
  1789. rc = __qed_hw_set_soft_resc_size(p_hwfn, p_ptt, res_id,
  1790. resc_max_val, &mcp_resp);
  1791. if (rc)
  1792. return rc;
  1793. /* There's no point to continue to the next resource if the
  1794. * command is not supported by the MFW.
  1795. * We do continue if the command is supported but the resource
  1796. * is unknown to the MFW. Such a resource will be later
  1797. * configured with the default allocation values.
  1798. */
  1799. if (mcp_resp == FW_MSG_CODE_UNSUPPORTED)
  1800. return -EINVAL;
  1801. }
  1802. return 0;
  1803. }
  1804. static
  1805. int qed_hw_get_dflt_resc(struct qed_hwfn *p_hwfn,
  1806. enum qed_resources res_id,
  1807. u32 *p_resc_num, u32 *p_resc_start)
  1808. {
  1809. u8 num_funcs = p_hwfn->num_funcs_on_engine;
  1810. bool b_ah = QED_IS_AH(p_hwfn->cdev);
  1811. struct qed_sb_cnt_info sb_cnt_info;
  1812. switch (res_id) {
  1813. case QED_L2_QUEUE:
  1814. *p_resc_num = (b_ah ? MAX_NUM_L2_QUEUES_K2 :
  1815. MAX_NUM_L2_QUEUES_BB) / num_funcs;
  1816. break;
  1817. case QED_VPORT:
  1818. *p_resc_num = (b_ah ? MAX_NUM_VPORTS_K2 :
  1819. MAX_NUM_VPORTS_BB) / num_funcs;
  1820. break;
  1821. case QED_RSS_ENG:
  1822. *p_resc_num = (b_ah ? ETH_RSS_ENGINE_NUM_K2 :
  1823. ETH_RSS_ENGINE_NUM_BB) / num_funcs;
  1824. break;
  1825. case QED_PQ:
  1826. *p_resc_num = (b_ah ? MAX_QM_TX_QUEUES_K2 :
  1827. MAX_QM_TX_QUEUES_BB) / num_funcs;
  1828. *p_resc_num &= ~0x7; /* The granularity of the PQs is 8 */
  1829. break;
  1830. case QED_RL:
  1831. *p_resc_num = MAX_QM_GLOBAL_RLS / num_funcs;
  1832. break;
  1833. case QED_MAC:
  1834. case QED_VLAN:
  1835. /* Each VFC resource can accommodate both a MAC and a VLAN */
  1836. *p_resc_num = ETH_NUM_MAC_FILTERS / num_funcs;
  1837. break;
  1838. case QED_ILT:
  1839. *p_resc_num = (b_ah ? PXP_NUM_ILT_RECORDS_K2 :
  1840. PXP_NUM_ILT_RECORDS_BB) / num_funcs;
  1841. break;
  1842. case QED_LL2_QUEUE:
  1843. *p_resc_num = MAX_NUM_LL2_RX_QUEUES / num_funcs;
  1844. break;
  1845. case QED_RDMA_CNQ_RAM:
  1846. case QED_CMDQS_CQS:
  1847. /* CNQ/CMDQS are the same resource */
  1848. *p_resc_num = NUM_OF_CMDQS_CQS / num_funcs;
  1849. break;
  1850. case QED_RDMA_STATS_QUEUE:
  1851. *p_resc_num = (b_ah ? RDMA_NUM_STATISTIC_COUNTERS_K2 :
  1852. RDMA_NUM_STATISTIC_COUNTERS_BB) / num_funcs;
  1853. break;
  1854. case QED_BDQ:
  1855. if (p_hwfn->hw_info.personality != QED_PCI_ISCSI &&
  1856. p_hwfn->hw_info.personality != QED_PCI_FCOE)
  1857. *p_resc_num = 0;
  1858. else
  1859. *p_resc_num = 1;
  1860. break;
  1861. case QED_SB:
  1862. memset(&sb_cnt_info, 0, sizeof(sb_cnt_info));
  1863. qed_int_get_num_sbs(p_hwfn, &sb_cnt_info);
  1864. *p_resc_num = sb_cnt_info.sb_cnt;
  1865. break;
  1866. default:
  1867. return -EINVAL;
  1868. }
  1869. switch (res_id) {
  1870. case QED_BDQ:
  1871. if (!*p_resc_num)
  1872. *p_resc_start = 0;
  1873. else if (p_hwfn->cdev->num_ports_in_engine == 4)
  1874. *p_resc_start = p_hwfn->port_id;
  1875. else if (p_hwfn->hw_info.personality == QED_PCI_ISCSI)
  1876. *p_resc_start = p_hwfn->port_id;
  1877. else if (p_hwfn->hw_info.personality == QED_PCI_FCOE)
  1878. *p_resc_start = p_hwfn->port_id + 2;
  1879. break;
  1880. default:
  1881. *p_resc_start = *p_resc_num * p_hwfn->enabled_func_idx;
  1882. break;
  1883. }
  1884. return 0;
  1885. }
  1886. static int __qed_hw_set_resc_info(struct qed_hwfn *p_hwfn,
  1887. enum qed_resources res_id)
  1888. {
  1889. u32 dflt_resc_num = 0, dflt_resc_start = 0;
  1890. u32 mcp_resp, *p_resc_num, *p_resc_start;
  1891. int rc;
  1892. p_resc_num = &RESC_NUM(p_hwfn, res_id);
  1893. p_resc_start = &RESC_START(p_hwfn, res_id);
  1894. rc = qed_hw_get_dflt_resc(p_hwfn, res_id, &dflt_resc_num,
  1895. &dflt_resc_start);
  1896. if (rc) {
  1897. DP_ERR(p_hwfn,
  1898. "Failed to get default amount for resource %d [%s]\n",
  1899. res_id, qed_hw_get_resc_name(res_id));
  1900. return rc;
  1901. }
  1902. rc = qed_mcp_get_resc_info(p_hwfn, p_hwfn->p_main_ptt, res_id,
  1903. &mcp_resp, p_resc_num, p_resc_start);
  1904. if (rc) {
  1905. DP_NOTICE(p_hwfn,
  1906. "MFW response failure for an allocation request for resource %d [%s]\n",
  1907. res_id, qed_hw_get_resc_name(res_id));
  1908. return rc;
  1909. }
  1910. /* Default driver values are applied in the following cases:
  1911. * - The resource allocation MB command is not supported by the MFW
  1912. * - There is an internal error in the MFW while processing the request
  1913. * - The resource ID is unknown to the MFW
  1914. */
  1915. if (mcp_resp != FW_MSG_CODE_RESOURCE_ALLOC_OK) {
  1916. DP_INFO(p_hwfn,
  1917. "Failed to receive allocation info for resource %d [%s]. mcp_resp = 0x%x. Applying default values [%d,%d].\n",
  1918. res_id,
  1919. qed_hw_get_resc_name(res_id),
  1920. mcp_resp, dflt_resc_num, dflt_resc_start);
  1921. *p_resc_num = dflt_resc_num;
  1922. *p_resc_start = dflt_resc_start;
  1923. goto out;
  1924. }
  1925. /* Special handling for status blocks; Would be revised in future */
  1926. if (res_id == QED_SB) {
  1927. *p_resc_num -= 1;
  1928. *p_resc_start -= p_hwfn->enabled_func_idx;
  1929. }
  1930. out:
  1931. /* PQs have to divide by 8 [that's the HW granularity].
  1932. * Reduce number so it would fit.
  1933. */
  1934. if ((res_id == QED_PQ) && ((*p_resc_num % 8) || (*p_resc_start % 8))) {
  1935. DP_INFO(p_hwfn,
  1936. "PQs need to align by 8; Number %08x --> %08x, Start %08x --> %08x\n",
  1937. *p_resc_num,
  1938. (*p_resc_num) & ~0x7,
  1939. *p_resc_start, (*p_resc_start) & ~0x7);
  1940. *p_resc_num &= ~0x7;
  1941. *p_resc_start &= ~0x7;
  1942. }
  1943. return 0;
  1944. }
  1945. static int qed_hw_set_resc_info(struct qed_hwfn *p_hwfn)
  1946. {
  1947. int rc;
  1948. u8 res_id;
  1949. for (res_id = 0; res_id < QED_MAX_RESC; res_id++) {
  1950. rc = __qed_hw_set_resc_info(p_hwfn, res_id);
  1951. if (rc)
  1952. return rc;
  1953. }
  1954. return 0;
  1955. }
  1956. static int qed_hw_get_resc(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  1957. {
  1958. struct qed_resc_unlock_params resc_unlock_params;
  1959. struct qed_resc_lock_params resc_lock_params;
  1960. bool b_ah = QED_IS_AH(p_hwfn->cdev);
  1961. u8 res_id;
  1962. int rc;
  1963. /* Setting the max values of the soft resources and the following
  1964. * resources allocation queries should be atomic. Since several PFs can
  1965. * run in parallel - a resource lock is needed.
  1966. * If either the resource lock or resource set value commands are not
  1967. * supported - skip the the max values setting, release the lock if
  1968. * needed, and proceed to the queries. Other failures, including a
  1969. * failure to acquire the lock, will cause this function to fail.
  1970. */
  1971. qed_mcp_resc_lock_default_init(&resc_lock_params, &resc_unlock_params,
  1972. QED_RESC_LOCK_RESC_ALLOC, false);
  1973. rc = qed_mcp_resc_lock(p_hwfn, p_ptt, &resc_lock_params);
  1974. if (rc && rc != -EINVAL) {
  1975. return rc;
  1976. } else if (rc == -EINVAL) {
  1977. DP_INFO(p_hwfn,
  1978. "Skip the max values setting of the soft resources since the resource lock is not supported by the MFW\n");
  1979. } else if (!rc && !resc_lock_params.b_granted) {
  1980. DP_NOTICE(p_hwfn,
  1981. "Failed to acquire the resource lock for the resource allocation commands\n");
  1982. return -EBUSY;
  1983. } else {
  1984. rc = qed_hw_set_soft_resc_size(p_hwfn, p_ptt);
  1985. if (rc && rc != -EINVAL) {
  1986. DP_NOTICE(p_hwfn,
  1987. "Failed to set the max values of the soft resources\n");
  1988. goto unlock_and_exit;
  1989. } else if (rc == -EINVAL) {
  1990. DP_INFO(p_hwfn,
  1991. "Skip the max values setting of the soft resources since it is not supported by the MFW\n");
  1992. rc = qed_mcp_resc_unlock(p_hwfn, p_ptt,
  1993. &resc_unlock_params);
  1994. if (rc)
  1995. DP_INFO(p_hwfn,
  1996. "Failed to release the resource lock for the resource allocation commands\n");
  1997. }
  1998. }
  1999. rc = qed_hw_set_resc_info(p_hwfn);
  2000. if (rc)
  2001. goto unlock_and_exit;
  2002. if (resc_lock_params.b_granted && !resc_unlock_params.b_released) {
  2003. rc = qed_mcp_resc_unlock(p_hwfn, p_ptt, &resc_unlock_params);
  2004. if (rc)
  2005. DP_INFO(p_hwfn,
  2006. "Failed to release the resource lock for the resource allocation commands\n");
  2007. }
  2008. /* Sanity for ILT */
  2009. if ((b_ah && (RESC_END(p_hwfn, QED_ILT) > PXP_NUM_ILT_RECORDS_K2)) ||
  2010. (!b_ah && (RESC_END(p_hwfn, QED_ILT) > PXP_NUM_ILT_RECORDS_BB))) {
  2011. DP_NOTICE(p_hwfn, "Can't assign ILT pages [%08x,...,%08x]\n",
  2012. RESC_START(p_hwfn, QED_ILT),
  2013. RESC_END(p_hwfn, QED_ILT) - 1);
  2014. return -EINVAL;
  2015. }
  2016. qed_hw_set_feat(p_hwfn);
  2017. for (res_id = 0; res_id < QED_MAX_RESC; res_id++)
  2018. DP_VERBOSE(p_hwfn, NETIF_MSG_PROBE, "%s = %d start = %d\n",
  2019. qed_hw_get_resc_name(res_id),
  2020. RESC_NUM(p_hwfn, res_id),
  2021. RESC_START(p_hwfn, res_id));
  2022. return 0;
  2023. unlock_and_exit:
  2024. if (resc_lock_params.b_granted && !resc_unlock_params.b_released)
  2025. qed_mcp_resc_unlock(p_hwfn, p_ptt, &resc_unlock_params);
  2026. return rc;
  2027. }
  2028. static int qed_hw_get_nvm_info(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  2029. {
  2030. u32 port_cfg_addr, link_temp, nvm_cfg_addr, device_capabilities;
  2031. u32 nvm_cfg1_offset, mf_mode, addr, generic_cont0, core_cfg;
  2032. struct qed_mcp_link_params *link;
  2033. /* Read global nvm_cfg address */
  2034. nvm_cfg_addr = qed_rd(p_hwfn, p_ptt, MISC_REG_GEN_PURP_CR0);
  2035. /* Verify MCP has initialized it */
  2036. if (!nvm_cfg_addr) {
  2037. DP_NOTICE(p_hwfn, "Shared memory not initialized\n");
  2038. return -EINVAL;
  2039. }
  2040. /* Read nvm_cfg1 (Notice this is just offset, and not offsize (TBD) */
  2041. nvm_cfg1_offset = qed_rd(p_hwfn, p_ptt, nvm_cfg_addr + 4);
  2042. addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
  2043. offsetof(struct nvm_cfg1, glob) +
  2044. offsetof(struct nvm_cfg1_glob, core_cfg);
  2045. core_cfg = qed_rd(p_hwfn, p_ptt, addr);
  2046. switch ((core_cfg & NVM_CFG1_GLOB_NETWORK_PORT_MODE_MASK) >>
  2047. NVM_CFG1_GLOB_NETWORK_PORT_MODE_OFFSET) {
  2048. case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_2X40G:
  2049. p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X40G;
  2050. break;
  2051. case NVM_CFG1_GLOB_NETWORK_PORT_MODE_2X50G:
  2052. p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X50G;
  2053. break;
  2054. case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_1X100G:
  2055. p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_1X100G;
  2056. break;
  2057. case NVM_CFG1_GLOB_NETWORK_PORT_MODE_4X10G_F:
  2058. p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X10G_F;
  2059. break;
  2060. case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_4X10G_E:
  2061. p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X10G_E;
  2062. break;
  2063. case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_4X20G:
  2064. p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X20G;
  2065. break;
  2066. case NVM_CFG1_GLOB_NETWORK_PORT_MODE_1X40G:
  2067. p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_1X40G;
  2068. break;
  2069. case NVM_CFG1_GLOB_NETWORK_PORT_MODE_2X25G:
  2070. p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X25G;
  2071. break;
  2072. case NVM_CFG1_GLOB_NETWORK_PORT_MODE_2X10G:
  2073. p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X10G;
  2074. break;
  2075. case NVM_CFG1_GLOB_NETWORK_PORT_MODE_1X25G:
  2076. p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_1X25G;
  2077. break;
  2078. case NVM_CFG1_GLOB_NETWORK_PORT_MODE_4X25G:
  2079. p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X25G;
  2080. break;
  2081. default:
  2082. DP_NOTICE(p_hwfn, "Unknown port mode in 0x%08x\n", core_cfg);
  2083. break;
  2084. }
  2085. /* Read default link configuration */
  2086. link = &p_hwfn->mcp_info->link_input;
  2087. port_cfg_addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
  2088. offsetof(struct nvm_cfg1, port[MFW_PORT(p_hwfn)]);
  2089. link_temp = qed_rd(p_hwfn, p_ptt,
  2090. port_cfg_addr +
  2091. offsetof(struct nvm_cfg1_port, speed_cap_mask));
  2092. link_temp &= NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_MASK;
  2093. link->speed.advertised_speeds = link_temp;
  2094. link_temp = link->speed.advertised_speeds;
  2095. p_hwfn->mcp_info->link_capabilities.speed_capabilities = link_temp;
  2096. link_temp = qed_rd(p_hwfn, p_ptt,
  2097. port_cfg_addr +
  2098. offsetof(struct nvm_cfg1_port, link_settings));
  2099. switch ((link_temp & NVM_CFG1_PORT_DRV_LINK_SPEED_MASK) >>
  2100. NVM_CFG1_PORT_DRV_LINK_SPEED_OFFSET) {
  2101. case NVM_CFG1_PORT_DRV_LINK_SPEED_AUTONEG:
  2102. link->speed.autoneg = true;
  2103. break;
  2104. case NVM_CFG1_PORT_DRV_LINK_SPEED_1G:
  2105. link->speed.forced_speed = 1000;
  2106. break;
  2107. case NVM_CFG1_PORT_DRV_LINK_SPEED_10G:
  2108. link->speed.forced_speed = 10000;
  2109. break;
  2110. case NVM_CFG1_PORT_DRV_LINK_SPEED_25G:
  2111. link->speed.forced_speed = 25000;
  2112. break;
  2113. case NVM_CFG1_PORT_DRV_LINK_SPEED_40G:
  2114. link->speed.forced_speed = 40000;
  2115. break;
  2116. case NVM_CFG1_PORT_DRV_LINK_SPEED_50G:
  2117. link->speed.forced_speed = 50000;
  2118. break;
  2119. case NVM_CFG1_PORT_DRV_LINK_SPEED_BB_100G:
  2120. link->speed.forced_speed = 100000;
  2121. break;
  2122. default:
  2123. DP_NOTICE(p_hwfn, "Unknown Speed in 0x%08x\n", link_temp);
  2124. }
  2125. p_hwfn->mcp_info->link_capabilities.default_speed_autoneg =
  2126. link->speed.autoneg;
  2127. link_temp &= NVM_CFG1_PORT_DRV_FLOW_CONTROL_MASK;
  2128. link_temp >>= NVM_CFG1_PORT_DRV_FLOW_CONTROL_OFFSET;
  2129. link->pause.autoneg = !!(link_temp &
  2130. NVM_CFG1_PORT_DRV_FLOW_CONTROL_AUTONEG);
  2131. link->pause.forced_rx = !!(link_temp &
  2132. NVM_CFG1_PORT_DRV_FLOW_CONTROL_RX);
  2133. link->pause.forced_tx = !!(link_temp &
  2134. NVM_CFG1_PORT_DRV_FLOW_CONTROL_TX);
  2135. link->loopback_mode = 0;
  2136. DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
  2137. "Read default link: Speed 0x%08x, Adv. Speed 0x%08x, AN: 0x%02x, PAUSE AN: 0x%02x\n",
  2138. link->speed.forced_speed, link->speed.advertised_speeds,
  2139. link->speed.autoneg, link->pause.autoneg);
  2140. /* Read Multi-function information from shmem */
  2141. addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
  2142. offsetof(struct nvm_cfg1, glob) +
  2143. offsetof(struct nvm_cfg1_glob, generic_cont0);
  2144. generic_cont0 = qed_rd(p_hwfn, p_ptt, addr);
  2145. mf_mode = (generic_cont0 & NVM_CFG1_GLOB_MF_MODE_MASK) >>
  2146. NVM_CFG1_GLOB_MF_MODE_OFFSET;
  2147. switch (mf_mode) {
  2148. case NVM_CFG1_GLOB_MF_MODE_MF_ALLOWED:
  2149. p_hwfn->cdev->mf_mode = QED_MF_OVLAN;
  2150. break;
  2151. case NVM_CFG1_GLOB_MF_MODE_NPAR1_0:
  2152. p_hwfn->cdev->mf_mode = QED_MF_NPAR;
  2153. break;
  2154. case NVM_CFG1_GLOB_MF_MODE_DEFAULT:
  2155. p_hwfn->cdev->mf_mode = QED_MF_DEFAULT;
  2156. break;
  2157. }
  2158. DP_INFO(p_hwfn, "Multi function mode is %08x\n",
  2159. p_hwfn->cdev->mf_mode);
  2160. /* Read Multi-function information from shmem */
  2161. addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
  2162. offsetof(struct nvm_cfg1, glob) +
  2163. offsetof(struct nvm_cfg1_glob, device_capabilities);
  2164. device_capabilities = qed_rd(p_hwfn, p_ptt, addr);
  2165. if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ETHERNET)
  2166. __set_bit(QED_DEV_CAP_ETH,
  2167. &p_hwfn->hw_info.device_capabilities);
  2168. if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_FCOE)
  2169. __set_bit(QED_DEV_CAP_FCOE,
  2170. &p_hwfn->hw_info.device_capabilities);
  2171. if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ISCSI)
  2172. __set_bit(QED_DEV_CAP_ISCSI,
  2173. &p_hwfn->hw_info.device_capabilities);
  2174. if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ROCE)
  2175. __set_bit(QED_DEV_CAP_ROCE,
  2176. &p_hwfn->hw_info.device_capabilities);
  2177. return qed_mcp_fill_shmem_func_info(p_hwfn, p_ptt);
  2178. }
  2179. static void qed_get_num_funcs(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  2180. {
  2181. u8 num_funcs, enabled_func_idx = p_hwfn->rel_pf_id;
  2182. u32 reg_function_hide, tmp, eng_mask, low_pfs_mask;
  2183. struct qed_dev *cdev = p_hwfn->cdev;
  2184. num_funcs = QED_IS_AH(cdev) ? MAX_NUM_PFS_K2 : MAX_NUM_PFS_BB;
  2185. /* Bit 0 of MISCS_REG_FUNCTION_HIDE indicates whether the bypass values
  2186. * in the other bits are selected.
  2187. * Bits 1-15 are for functions 1-15, respectively, and their value is
  2188. * '0' only for enabled functions (function 0 always exists and
  2189. * enabled).
  2190. * In case of CMT, only the "even" functions are enabled, and thus the
  2191. * number of functions for both hwfns is learnt from the same bits.
  2192. */
  2193. reg_function_hide = qed_rd(p_hwfn, p_ptt, MISCS_REG_FUNCTION_HIDE);
  2194. if (reg_function_hide & 0x1) {
  2195. if (QED_IS_BB(cdev)) {
  2196. if (QED_PATH_ID(p_hwfn) && cdev->num_hwfns == 1) {
  2197. num_funcs = 0;
  2198. eng_mask = 0xaaaa;
  2199. } else {
  2200. num_funcs = 1;
  2201. eng_mask = 0x5554;
  2202. }
  2203. } else {
  2204. num_funcs = 1;
  2205. eng_mask = 0xfffe;
  2206. }
  2207. /* Get the number of the enabled functions on the engine */
  2208. tmp = (reg_function_hide ^ 0xffffffff) & eng_mask;
  2209. while (tmp) {
  2210. if (tmp & 0x1)
  2211. num_funcs++;
  2212. tmp >>= 0x1;
  2213. }
  2214. /* Get the PF index within the enabled functions */
  2215. low_pfs_mask = (0x1 << p_hwfn->abs_pf_id) - 1;
  2216. tmp = reg_function_hide & eng_mask & low_pfs_mask;
  2217. while (tmp) {
  2218. if (tmp & 0x1)
  2219. enabled_func_idx--;
  2220. tmp >>= 0x1;
  2221. }
  2222. }
  2223. p_hwfn->num_funcs_on_engine = num_funcs;
  2224. p_hwfn->enabled_func_idx = enabled_func_idx;
  2225. DP_VERBOSE(p_hwfn,
  2226. NETIF_MSG_PROBE,
  2227. "PF [rel_id %d, abs_id %d] occupies index %d within the %d enabled functions on the engine\n",
  2228. p_hwfn->rel_pf_id,
  2229. p_hwfn->abs_pf_id,
  2230. p_hwfn->enabled_func_idx, p_hwfn->num_funcs_on_engine);
  2231. }
  2232. static void qed_hw_info_port_num_bb(struct qed_hwfn *p_hwfn,
  2233. struct qed_ptt *p_ptt)
  2234. {
  2235. u32 port_mode;
  2236. port_mode = qed_rd(p_hwfn, p_ptt, CNIG_REG_NW_PORT_MODE_BB_B0);
  2237. if (port_mode < 3) {
  2238. p_hwfn->cdev->num_ports_in_engine = 1;
  2239. } else if (port_mode <= 5) {
  2240. p_hwfn->cdev->num_ports_in_engine = 2;
  2241. } else {
  2242. DP_NOTICE(p_hwfn, "PORT MODE: %d not supported\n",
  2243. p_hwfn->cdev->num_ports_in_engine);
  2244. /* Default num_ports_in_engine to something */
  2245. p_hwfn->cdev->num_ports_in_engine = 1;
  2246. }
  2247. }
  2248. static void qed_hw_info_port_num_ah(struct qed_hwfn *p_hwfn,
  2249. struct qed_ptt *p_ptt)
  2250. {
  2251. u32 port;
  2252. int i;
  2253. p_hwfn->cdev->num_ports_in_engine = 0;
  2254. for (i = 0; i < MAX_NUM_PORTS_K2; i++) {
  2255. port = qed_rd(p_hwfn, p_ptt,
  2256. CNIG_REG_NIG_PORT0_CONF_K2 + (i * 4));
  2257. if (port & 1)
  2258. p_hwfn->cdev->num_ports_in_engine++;
  2259. }
  2260. if (!p_hwfn->cdev->num_ports_in_engine) {
  2261. DP_NOTICE(p_hwfn, "All NIG ports are inactive\n");
  2262. /* Default num_ports_in_engine to something */
  2263. p_hwfn->cdev->num_ports_in_engine = 1;
  2264. }
  2265. }
  2266. static void qed_hw_info_port_num(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  2267. {
  2268. if (QED_IS_BB(p_hwfn->cdev))
  2269. qed_hw_info_port_num_bb(p_hwfn, p_ptt);
  2270. else
  2271. qed_hw_info_port_num_ah(p_hwfn, p_ptt);
  2272. }
  2273. static int
  2274. qed_get_hw_info(struct qed_hwfn *p_hwfn,
  2275. struct qed_ptt *p_ptt,
  2276. enum qed_pci_personality personality)
  2277. {
  2278. int rc;
  2279. /* Since all information is common, only first hwfns should do this */
  2280. if (IS_LEAD_HWFN(p_hwfn)) {
  2281. rc = qed_iov_hw_info(p_hwfn);
  2282. if (rc)
  2283. return rc;
  2284. }
  2285. qed_hw_info_port_num(p_hwfn, p_ptt);
  2286. qed_hw_get_nvm_info(p_hwfn, p_ptt);
  2287. rc = qed_int_igu_read_cam(p_hwfn, p_ptt);
  2288. if (rc)
  2289. return rc;
  2290. if (qed_mcp_is_init(p_hwfn))
  2291. ether_addr_copy(p_hwfn->hw_info.hw_mac_addr,
  2292. p_hwfn->mcp_info->func_info.mac);
  2293. else
  2294. eth_random_addr(p_hwfn->hw_info.hw_mac_addr);
  2295. if (qed_mcp_is_init(p_hwfn)) {
  2296. if (p_hwfn->mcp_info->func_info.ovlan != QED_MCP_VLAN_UNSET)
  2297. p_hwfn->hw_info.ovlan =
  2298. p_hwfn->mcp_info->func_info.ovlan;
  2299. qed_mcp_cmd_port_init(p_hwfn, p_ptt);
  2300. }
  2301. if (qed_mcp_is_init(p_hwfn)) {
  2302. enum qed_pci_personality protocol;
  2303. protocol = p_hwfn->mcp_info->func_info.protocol;
  2304. p_hwfn->hw_info.personality = protocol;
  2305. }
  2306. p_hwfn->hw_info.num_hw_tc = NUM_PHYS_TCS_4PORT_K2;
  2307. p_hwfn->hw_info.num_active_tc = 1;
  2308. qed_get_num_funcs(p_hwfn, p_ptt);
  2309. if (qed_mcp_is_init(p_hwfn))
  2310. p_hwfn->hw_info.mtu = p_hwfn->mcp_info->func_info.mtu;
  2311. return qed_hw_get_resc(p_hwfn, p_ptt);
  2312. }
  2313. static int qed_get_dev_info(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  2314. {
  2315. struct qed_dev *cdev = p_hwfn->cdev;
  2316. u16 device_id_mask;
  2317. u32 tmp;
  2318. /* Read Vendor Id / Device Id */
  2319. pci_read_config_word(cdev->pdev, PCI_VENDOR_ID, &cdev->vendor_id);
  2320. pci_read_config_word(cdev->pdev, PCI_DEVICE_ID, &cdev->device_id);
  2321. /* Determine type */
  2322. device_id_mask = cdev->device_id & QED_DEV_ID_MASK;
  2323. switch (device_id_mask) {
  2324. case QED_DEV_ID_MASK_BB:
  2325. cdev->type = QED_DEV_TYPE_BB;
  2326. break;
  2327. case QED_DEV_ID_MASK_AH:
  2328. cdev->type = QED_DEV_TYPE_AH;
  2329. break;
  2330. default:
  2331. DP_NOTICE(p_hwfn, "Unknown device id 0x%x\n", cdev->device_id);
  2332. return -EBUSY;
  2333. }
  2334. cdev->chip_num = (u16)qed_rd(p_hwfn, p_ptt, MISCS_REG_CHIP_NUM);
  2335. cdev->chip_rev = (u16)qed_rd(p_hwfn, p_ptt, MISCS_REG_CHIP_REV);
  2336. MASK_FIELD(CHIP_REV, cdev->chip_rev);
  2337. /* Learn number of HW-functions */
  2338. tmp = qed_rd(p_hwfn, p_ptt, MISCS_REG_CMT_ENABLED_FOR_PAIR);
  2339. if (tmp & (1 << p_hwfn->rel_pf_id)) {
  2340. DP_NOTICE(cdev->hwfns, "device in CMT mode\n");
  2341. cdev->num_hwfns = 2;
  2342. } else {
  2343. cdev->num_hwfns = 1;
  2344. }
  2345. cdev->chip_bond_id = qed_rd(p_hwfn, p_ptt,
  2346. MISCS_REG_CHIP_TEST_REG) >> 4;
  2347. MASK_FIELD(CHIP_BOND_ID, cdev->chip_bond_id);
  2348. cdev->chip_metal = (u16)qed_rd(p_hwfn, p_ptt, MISCS_REG_CHIP_METAL);
  2349. MASK_FIELD(CHIP_METAL, cdev->chip_metal);
  2350. DP_INFO(cdev->hwfns,
  2351. "Chip details - %s %c%d, Num: %04x Rev: %04x Bond id: %04x Metal: %04x\n",
  2352. QED_IS_BB(cdev) ? "BB" : "AH",
  2353. 'A' + cdev->chip_rev,
  2354. (int)cdev->chip_metal,
  2355. cdev->chip_num, cdev->chip_rev,
  2356. cdev->chip_bond_id, cdev->chip_metal);
  2357. if (QED_IS_BB(cdev) && CHIP_REV_IS_A0(cdev)) {
  2358. DP_NOTICE(cdev->hwfns,
  2359. "The chip type/rev (BB A0) is not supported!\n");
  2360. return -EINVAL;
  2361. }
  2362. return 0;
  2363. }
  2364. static int qed_hw_prepare_single(struct qed_hwfn *p_hwfn,
  2365. void __iomem *p_regview,
  2366. void __iomem *p_doorbells,
  2367. enum qed_pci_personality personality)
  2368. {
  2369. int rc = 0;
  2370. /* Split PCI bars evenly between hwfns */
  2371. p_hwfn->regview = p_regview;
  2372. p_hwfn->doorbells = p_doorbells;
  2373. if (IS_VF(p_hwfn->cdev))
  2374. return qed_vf_hw_prepare(p_hwfn);
  2375. /* Validate that chip access is feasible */
  2376. if (REG_RD(p_hwfn, PXP_PF_ME_OPAQUE_ADDR) == 0xffffffff) {
  2377. DP_ERR(p_hwfn,
  2378. "Reading the ME register returns all Fs; Preventing further chip access\n");
  2379. return -EINVAL;
  2380. }
  2381. get_function_id(p_hwfn);
  2382. /* Allocate PTT pool */
  2383. rc = qed_ptt_pool_alloc(p_hwfn);
  2384. if (rc)
  2385. goto err0;
  2386. /* Allocate the main PTT */
  2387. p_hwfn->p_main_ptt = qed_get_reserved_ptt(p_hwfn, RESERVED_PTT_MAIN);
  2388. /* First hwfn learns basic information, e.g., number of hwfns */
  2389. if (!p_hwfn->my_id) {
  2390. rc = qed_get_dev_info(p_hwfn, p_hwfn->p_main_ptt);
  2391. if (rc)
  2392. goto err1;
  2393. }
  2394. qed_hw_hwfn_prepare(p_hwfn);
  2395. /* Initialize MCP structure */
  2396. rc = qed_mcp_cmd_init(p_hwfn, p_hwfn->p_main_ptt);
  2397. if (rc) {
  2398. DP_NOTICE(p_hwfn, "Failed initializing mcp command\n");
  2399. goto err1;
  2400. }
  2401. /* Read the device configuration information from the HW and SHMEM */
  2402. rc = qed_get_hw_info(p_hwfn, p_hwfn->p_main_ptt, personality);
  2403. if (rc) {
  2404. DP_NOTICE(p_hwfn, "Failed to get HW information\n");
  2405. goto err2;
  2406. }
  2407. /* Sending a mailbox to the MFW should be done after qed_get_hw_info()
  2408. * is called as it sets the ports number in an engine.
  2409. */
  2410. if (IS_LEAD_HWFN(p_hwfn)) {
  2411. rc = qed_mcp_initiate_pf_flr(p_hwfn, p_hwfn->p_main_ptt);
  2412. if (rc)
  2413. DP_NOTICE(p_hwfn, "Failed to initiate PF FLR\n");
  2414. }
  2415. /* Allocate the init RT array and initialize the init-ops engine */
  2416. rc = qed_init_alloc(p_hwfn);
  2417. if (rc)
  2418. goto err2;
  2419. return rc;
  2420. err2:
  2421. if (IS_LEAD_HWFN(p_hwfn))
  2422. qed_iov_free_hw_info(p_hwfn->cdev);
  2423. qed_mcp_free(p_hwfn);
  2424. err1:
  2425. qed_hw_hwfn_free(p_hwfn);
  2426. err0:
  2427. return rc;
  2428. }
  2429. int qed_hw_prepare(struct qed_dev *cdev,
  2430. int personality)
  2431. {
  2432. struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
  2433. int rc;
  2434. /* Store the precompiled init data ptrs */
  2435. if (IS_PF(cdev))
  2436. qed_init_iro_array(cdev);
  2437. /* Initialize the first hwfn - will learn number of hwfns */
  2438. rc = qed_hw_prepare_single(p_hwfn,
  2439. cdev->regview,
  2440. cdev->doorbells, personality);
  2441. if (rc)
  2442. return rc;
  2443. personality = p_hwfn->hw_info.personality;
  2444. /* Initialize the rest of the hwfns */
  2445. if (cdev->num_hwfns > 1) {
  2446. void __iomem *p_regview, *p_doorbell;
  2447. u8 __iomem *addr;
  2448. /* adjust bar offset for second engine */
  2449. addr = cdev->regview +
  2450. qed_hw_bar_size(p_hwfn, p_hwfn->p_main_ptt,
  2451. BAR_ID_0) / 2;
  2452. p_regview = addr;
  2453. addr = cdev->doorbells +
  2454. qed_hw_bar_size(p_hwfn, p_hwfn->p_main_ptt,
  2455. BAR_ID_1) / 2;
  2456. p_doorbell = addr;
  2457. /* prepare second hw function */
  2458. rc = qed_hw_prepare_single(&cdev->hwfns[1], p_regview,
  2459. p_doorbell, personality);
  2460. /* in case of error, need to free the previously
  2461. * initiliazed hwfn 0.
  2462. */
  2463. if (rc) {
  2464. if (IS_PF(cdev)) {
  2465. qed_init_free(p_hwfn);
  2466. qed_mcp_free(p_hwfn);
  2467. qed_hw_hwfn_free(p_hwfn);
  2468. }
  2469. }
  2470. }
  2471. return rc;
  2472. }
  2473. void qed_hw_remove(struct qed_dev *cdev)
  2474. {
  2475. struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
  2476. int i;
  2477. if (IS_PF(cdev))
  2478. qed_mcp_ov_update_driver_state(p_hwfn, p_hwfn->p_main_ptt,
  2479. QED_OV_DRIVER_STATE_NOT_LOADED);
  2480. for_each_hwfn(cdev, i) {
  2481. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  2482. if (IS_VF(cdev)) {
  2483. qed_vf_pf_release(p_hwfn);
  2484. continue;
  2485. }
  2486. qed_init_free(p_hwfn);
  2487. qed_hw_hwfn_free(p_hwfn);
  2488. qed_mcp_free(p_hwfn);
  2489. }
  2490. qed_iov_free_hw_info(cdev);
  2491. }
  2492. static void qed_chain_free_next_ptr(struct qed_dev *cdev,
  2493. struct qed_chain *p_chain)
  2494. {
  2495. void *p_virt = p_chain->p_virt_addr, *p_virt_next = NULL;
  2496. dma_addr_t p_phys = p_chain->p_phys_addr, p_phys_next = 0;
  2497. struct qed_chain_next *p_next;
  2498. u32 size, i;
  2499. if (!p_virt)
  2500. return;
  2501. size = p_chain->elem_size * p_chain->usable_per_page;
  2502. for (i = 0; i < p_chain->page_cnt; i++) {
  2503. if (!p_virt)
  2504. break;
  2505. p_next = (struct qed_chain_next *)((u8 *)p_virt + size);
  2506. p_virt_next = p_next->next_virt;
  2507. p_phys_next = HILO_DMA_REGPAIR(p_next->next_phys);
  2508. dma_free_coherent(&cdev->pdev->dev,
  2509. QED_CHAIN_PAGE_SIZE, p_virt, p_phys);
  2510. p_virt = p_virt_next;
  2511. p_phys = p_phys_next;
  2512. }
  2513. }
  2514. static void qed_chain_free_single(struct qed_dev *cdev,
  2515. struct qed_chain *p_chain)
  2516. {
  2517. if (!p_chain->p_virt_addr)
  2518. return;
  2519. dma_free_coherent(&cdev->pdev->dev,
  2520. QED_CHAIN_PAGE_SIZE,
  2521. p_chain->p_virt_addr, p_chain->p_phys_addr);
  2522. }
  2523. static void qed_chain_free_pbl(struct qed_dev *cdev, struct qed_chain *p_chain)
  2524. {
  2525. void **pp_virt_addr_tbl = p_chain->pbl.pp_virt_addr_tbl;
  2526. u32 page_cnt = p_chain->page_cnt, i, pbl_size;
  2527. u8 *p_pbl_virt = p_chain->pbl_sp.p_virt_table;
  2528. if (!pp_virt_addr_tbl)
  2529. return;
  2530. if (!p_pbl_virt)
  2531. goto out;
  2532. for (i = 0; i < page_cnt; i++) {
  2533. if (!pp_virt_addr_tbl[i])
  2534. break;
  2535. dma_free_coherent(&cdev->pdev->dev,
  2536. QED_CHAIN_PAGE_SIZE,
  2537. pp_virt_addr_tbl[i],
  2538. *(dma_addr_t *)p_pbl_virt);
  2539. p_pbl_virt += QED_CHAIN_PBL_ENTRY_SIZE;
  2540. }
  2541. pbl_size = page_cnt * QED_CHAIN_PBL_ENTRY_SIZE;
  2542. dma_free_coherent(&cdev->pdev->dev,
  2543. pbl_size,
  2544. p_chain->pbl_sp.p_virt_table,
  2545. p_chain->pbl_sp.p_phys_table);
  2546. out:
  2547. vfree(p_chain->pbl.pp_virt_addr_tbl);
  2548. }
  2549. void qed_chain_free(struct qed_dev *cdev, struct qed_chain *p_chain)
  2550. {
  2551. switch (p_chain->mode) {
  2552. case QED_CHAIN_MODE_NEXT_PTR:
  2553. qed_chain_free_next_ptr(cdev, p_chain);
  2554. break;
  2555. case QED_CHAIN_MODE_SINGLE:
  2556. qed_chain_free_single(cdev, p_chain);
  2557. break;
  2558. case QED_CHAIN_MODE_PBL:
  2559. qed_chain_free_pbl(cdev, p_chain);
  2560. break;
  2561. }
  2562. }
  2563. static int
  2564. qed_chain_alloc_sanity_check(struct qed_dev *cdev,
  2565. enum qed_chain_cnt_type cnt_type,
  2566. size_t elem_size, u32 page_cnt)
  2567. {
  2568. u64 chain_size = ELEMS_PER_PAGE(elem_size) * page_cnt;
  2569. /* The actual chain size can be larger than the maximal possible value
  2570. * after rounding up the requested elements number to pages, and after
  2571. * taking into acount the unusuable elements (next-ptr elements).
  2572. * The size of a "u16" chain can be (U16_MAX + 1) since the chain
  2573. * size/capacity fields are of a u32 type.
  2574. */
  2575. if ((cnt_type == QED_CHAIN_CNT_TYPE_U16 &&
  2576. chain_size > ((u32)U16_MAX + 1)) ||
  2577. (cnt_type == QED_CHAIN_CNT_TYPE_U32 && chain_size > U32_MAX)) {
  2578. DP_NOTICE(cdev,
  2579. "The actual chain size (0x%llx) is larger than the maximal possible value\n",
  2580. chain_size);
  2581. return -EINVAL;
  2582. }
  2583. return 0;
  2584. }
  2585. static int
  2586. qed_chain_alloc_next_ptr(struct qed_dev *cdev, struct qed_chain *p_chain)
  2587. {
  2588. void *p_virt = NULL, *p_virt_prev = NULL;
  2589. dma_addr_t p_phys = 0;
  2590. u32 i;
  2591. for (i = 0; i < p_chain->page_cnt; i++) {
  2592. p_virt = dma_alloc_coherent(&cdev->pdev->dev,
  2593. QED_CHAIN_PAGE_SIZE,
  2594. &p_phys, GFP_KERNEL);
  2595. if (!p_virt)
  2596. return -ENOMEM;
  2597. if (i == 0) {
  2598. qed_chain_init_mem(p_chain, p_virt, p_phys);
  2599. qed_chain_reset(p_chain);
  2600. } else {
  2601. qed_chain_init_next_ptr_elem(p_chain, p_virt_prev,
  2602. p_virt, p_phys);
  2603. }
  2604. p_virt_prev = p_virt;
  2605. }
  2606. /* Last page's next element should point to the beginning of the
  2607. * chain.
  2608. */
  2609. qed_chain_init_next_ptr_elem(p_chain, p_virt_prev,
  2610. p_chain->p_virt_addr,
  2611. p_chain->p_phys_addr);
  2612. return 0;
  2613. }
  2614. static int
  2615. qed_chain_alloc_single(struct qed_dev *cdev, struct qed_chain *p_chain)
  2616. {
  2617. dma_addr_t p_phys = 0;
  2618. void *p_virt = NULL;
  2619. p_virt = dma_alloc_coherent(&cdev->pdev->dev,
  2620. QED_CHAIN_PAGE_SIZE, &p_phys, GFP_KERNEL);
  2621. if (!p_virt)
  2622. return -ENOMEM;
  2623. qed_chain_init_mem(p_chain, p_virt, p_phys);
  2624. qed_chain_reset(p_chain);
  2625. return 0;
  2626. }
  2627. static int qed_chain_alloc_pbl(struct qed_dev *cdev, struct qed_chain *p_chain)
  2628. {
  2629. u32 page_cnt = p_chain->page_cnt, size, i;
  2630. dma_addr_t p_phys = 0, p_pbl_phys = 0;
  2631. void **pp_virt_addr_tbl = NULL;
  2632. u8 *p_pbl_virt = NULL;
  2633. void *p_virt = NULL;
  2634. size = page_cnt * sizeof(*pp_virt_addr_tbl);
  2635. pp_virt_addr_tbl = vzalloc(size);
  2636. if (!pp_virt_addr_tbl)
  2637. return -ENOMEM;
  2638. /* The allocation of the PBL table is done with its full size, since it
  2639. * is expected to be successive.
  2640. * qed_chain_init_pbl_mem() is called even in a case of an allocation
  2641. * failure, since pp_virt_addr_tbl was previously allocated, and it
  2642. * should be saved to allow its freeing during the error flow.
  2643. */
  2644. size = page_cnt * QED_CHAIN_PBL_ENTRY_SIZE;
  2645. p_pbl_virt = dma_alloc_coherent(&cdev->pdev->dev,
  2646. size, &p_pbl_phys, GFP_KERNEL);
  2647. qed_chain_init_pbl_mem(p_chain, p_pbl_virt, p_pbl_phys,
  2648. pp_virt_addr_tbl);
  2649. if (!p_pbl_virt)
  2650. return -ENOMEM;
  2651. for (i = 0; i < page_cnt; i++) {
  2652. p_virt = dma_alloc_coherent(&cdev->pdev->dev,
  2653. QED_CHAIN_PAGE_SIZE,
  2654. &p_phys, GFP_KERNEL);
  2655. if (!p_virt)
  2656. return -ENOMEM;
  2657. if (i == 0) {
  2658. qed_chain_init_mem(p_chain, p_virt, p_phys);
  2659. qed_chain_reset(p_chain);
  2660. }
  2661. /* Fill the PBL table with the physical address of the page */
  2662. *(dma_addr_t *)p_pbl_virt = p_phys;
  2663. /* Keep the virtual address of the page */
  2664. p_chain->pbl.pp_virt_addr_tbl[i] = p_virt;
  2665. p_pbl_virt += QED_CHAIN_PBL_ENTRY_SIZE;
  2666. }
  2667. return 0;
  2668. }
  2669. int qed_chain_alloc(struct qed_dev *cdev,
  2670. enum qed_chain_use_mode intended_use,
  2671. enum qed_chain_mode mode,
  2672. enum qed_chain_cnt_type cnt_type,
  2673. u32 num_elems, size_t elem_size, struct qed_chain *p_chain)
  2674. {
  2675. u32 page_cnt;
  2676. int rc = 0;
  2677. if (mode == QED_CHAIN_MODE_SINGLE)
  2678. page_cnt = 1;
  2679. else
  2680. page_cnt = QED_CHAIN_PAGE_CNT(num_elems, elem_size, mode);
  2681. rc = qed_chain_alloc_sanity_check(cdev, cnt_type, elem_size, page_cnt);
  2682. if (rc) {
  2683. DP_NOTICE(cdev,
  2684. "Cannot allocate a chain with the given arguments:\n");
  2685. DP_NOTICE(cdev,
  2686. "[use_mode %d, mode %d, cnt_type %d, num_elems %d, elem_size %zu]\n",
  2687. intended_use, mode, cnt_type, num_elems, elem_size);
  2688. return rc;
  2689. }
  2690. qed_chain_init_params(p_chain, page_cnt, (u8) elem_size, intended_use,
  2691. mode, cnt_type);
  2692. switch (mode) {
  2693. case QED_CHAIN_MODE_NEXT_PTR:
  2694. rc = qed_chain_alloc_next_ptr(cdev, p_chain);
  2695. break;
  2696. case QED_CHAIN_MODE_SINGLE:
  2697. rc = qed_chain_alloc_single(cdev, p_chain);
  2698. break;
  2699. case QED_CHAIN_MODE_PBL:
  2700. rc = qed_chain_alloc_pbl(cdev, p_chain);
  2701. break;
  2702. }
  2703. if (rc)
  2704. goto nomem;
  2705. return 0;
  2706. nomem:
  2707. qed_chain_free(cdev, p_chain);
  2708. return rc;
  2709. }
  2710. int qed_fw_l2_queue(struct qed_hwfn *p_hwfn, u16 src_id, u16 *dst_id)
  2711. {
  2712. if (src_id >= RESC_NUM(p_hwfn, QED_L2_QUEUE)) {
  2713. u16 min, max;
  2714. min = (u16) RESC_START(p_hwfn, QED_L2_QUEUE);
  2715. max = min + RESC_NUM(p_hwfn, QED_L2_QUEUE);
  2716. DP_NOTICE(p_hwfn,
  2717. "l2_queue id [%d] is not valid, available indices [%d - %d]\n",
  2718. src_id, min, max);
  2719. return -EINVAL;
  2720. }
  2721. *dst_id = RESC_START(p_hwfn, QED_L2_QUEUE) + src_id;
  2722. return 0;
  2723. }
  2724. int qed_fw_vport(struct qed_hwfn *p_hwfn, u8 src_id, u8 *dst_id)
  2725. {
  2726. if (src_id >= RESC_NUM(p_hwfn, QED_VPORT)) {
  2727. u8 min, max;
  2728. min = (u8)RESC_START(p_hwfn, QED_VPORT);
  2729. max = min + RESC_NUM(p_hwfn, QED_VPORT);
  2730. DP_NOTICE(p_hwfn,
  2731. "vport id [%d] is not valid, available indices [%d - %d]\n",
  2732. src_id, min, max);
  2733. return -EINVAL;
  2734. }
  2735. *dst_id = RESC_START(p_hwfn, QED_VPORT) + src_id;
  2736. return 0;
  2737. }
  2738. int qed_fw_rss_eng(struct qed_hwfn *p_hwfn, u8 src_id, u8 *dst_id)
  2739. {
  2740. if (src_id >= RESC_NUM(p_hwfn, QED_RSS_ENG)) {
  2741. u8 min, max;
  2742. min = (u8)RESC_START(p_hwfn, QED_RSS_ENG);
  2743. max = min + RESC_NUM(p_hwfn, QED_RSS_ENG);
  2744. DP_NOTICE(p_hwfn,
  2745. "rss_eng id [%d] is not valid, available indices [%d - %d]\n",
  2746. src_id, min, max);
  2747. return -EINVAL;
  2748. }
  2749. *dst_id = RESC_START(p_hwfn, QED_RSS_ENG) + src_id;
  2750. return 0;
  2751. }
  2752. static void qed_llh_mac_to_filter(u32 *p_high, u32 *p_low,
  2753. u8 *p_filter)
  2754. {
  2755. *p_high = p_filter[1] | (p_filter[0] << 8);
  2756. *p_low = p_filter[5] | (p_filter[4] << 8) |
  2757. (p_filter[3] << 16) | (p_filter[2] << 24);
  2758. }
  2759. int qed_llh_add_mac_filter(struct qed_hwfn *p_hwfn,
  2760. struct qed_ptt *p_ptt, u8 *p_filter)
  2761. {
  2762. u32 high = 0, low = 0, en;
  2763. int i;
  2764. if (!(IS_MF_SI(p_hwfn) || IS_MF_DEFAULT(p_hwfn)))
  2765. return 0;
  2766. qed_llh_mac_to_filter(&high, &low, p_filter);
  2767. /* Find a free entry and utilize it */
  2768. for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
  2769. en = qed_rd(p_hwfn, p_ptt,
  2770. NIG_REG_LLH_FUNC_FILTER_EN + i * sizeof(u32));
  2771. if (en)
  2772. continue;
  2773. qed_wr(p_hwfn, p_ptt,
  2774. NIG_REG_LLH_FUNC_FILTER_VALUE +
  2775. 2 * i * sizeof(u32), low);
  2776. qed_wr(p_hwfn, p_ptt,
  2777. NIG_REG_LLH_FUNC_FILTER_VALUE +
  2778. (2 * i + 1) * sizeof(u32), high);
  2779. qed_wr(p_hwfn, p_ptt,
  2780. NIG_REG_LLH_FUNC_FILTER_MODE + i * sizeof(u32), 0);
  2781. qed_wr(p_hwfn, p_ptt,
  2782. NIG_REG_LLH_FUNC_FILTER_PROTOCOL_TYPE +
  2783. i * sizeof(u32), 0);
  2784. qed_wr(p_hwfn, p_ptt,
  2785. NIG_REG_LLH_FUNC_FILTER_EN + i * sizeof(u32), 1);
  2786. break;
  2787. }
  2788. if (i >= NIG_REG_LLH_FUNC_FILTER_EN_SIZE) {
  2789. DP_NOTICE(p_hwfn,
  2790. "Failed to find an empty LLH filter to utilize\n");
  2791. return -EINVAL;
  2792. }
  2793. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  2794. "mac: %pM is added at %d\n",
  2795. p_filter, i);
  2796. return 0;
  2797. }
  2798. void qed_llh_remove_mac_filter(struct qed_hwfn *p_hwfn,
  2799. struct qed_ptt *p_ptt, u8 *p_filter)
  2800. {
  2801. u32 high = 0, low = 0;
  2802. int i;
  2803. if (!(IS_MF_SI(p_hwfn) || IS_MF_DEFAULT(p_hwfn)))
  2804. return;
  2805. qed_llh_mac_to_filter(&high, &low, p_filter);
  2806. /* Find the entry and clean it */
  2807. for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
  2808. if (qed_rd(p_hwfn, p_ptt,
  2809. NIG_REG_LLH_FUNC_FILTER_VALUE +
  2810. 2 * i * sizeof(u32)) != low)
  2811. continue;
  2812. if (qed_rd(p_hwfn, p_ptt,
  2813. NIG_REG_LLH_FUNC_FILTER_VALUE +
  2814. (2 * i + 1) * sizeof(u32)) != high)
  2815. continue;
  2816. qed_wr(p_hwfn, p_ptt,
  2817. NIG_REG_LLH_FUNC_FILTER_EN + i * sizeof(u32), 0);
  2818. qed_wr(p_hwfn, p_ptt,
  2819. NIG_REG_LLH_FUNC_FILTER_VALUE + 2 * i * sizeof(u32), 0);
  2820. qed_wr(p_hwfn, p_ptt,
  2821. NIG_REG_LLH_FUNC_FILTER_VALUE +
  2822. (2 * i + 1) * sizeof(u32), 0);
  2823. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  2824. "mac: %pM is removed from %d\n",
  2825. p_filter, i);
  2826. break;
  2827. }
  2828. if (i >= NIG_REG_LLH_FUNC_FILTER_EN_SIZE)
  2829. DP_NOTICE(p_hwfn, "Tried to remove a non-configured filter\n");
  2830. }
  2831. int
  2832. qed_llh_add_protocol_filter(struct qed_hwfn *p_hwfn,
  2833. struct qed_ptt *p_ptt,
  2834. u16 source_port_or_eth_type,
  2835. u16 dest_port, enum qed_llh_port_filter_type_t type)
  2836. {
  2837. u32 high = 0, low = 0, en;
  2838. int i;
  2839. if (!(IS_MF_SI(p_hwfn) || IS_MF_DEFAULT(p_hwfn)))
  2840. return 0;
  2841. switch (type) {
  2842. case QED_LLH_FILTER_ETHERTYPE:
  2843. high = source_port_or_eth_type;
  2844. break;
  2845. case QED_LLH_FILTER_TCP_SRC_PORT:
  2846. case QED_LLH_FILTER_UDP_SRC_PORT:
  2847. low = source_port_or_eth_type << 16;
  2848. break;
  2849. case QED_LLH_FILTER_TCP_DEST_PORT:
  2850. case QED_LLH_FILTER_UDP_DEST_PORT:
  2851. low = dest_port;
  2852. break;
  2853. case QED_LLH_FILTER_TCP_SRC_AND_DEST_PORT:
  2854. case QED_LLH_FILTER_UDP_SRC_AND_DEST_PORT:
  2855. low = (source_port_or_eth_type << 16) | dest_port;
  2856. break;
  2857. default:
  2858. DP_NOTICE(p_hwfn,
  2859. "Non valid LLH protocol filter type %d\n", type);
  2860. return -EINVAL;
  2861. }
  2862. /* Find a free entry and utilize it */
  2863. for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
  2864. en = qed_rd(p_hwfn, p_ptt,
  2865. NIG_REG_LLH_FUNC_FILTER_EN + i * sizeof(u32));
  2866. if (en)
  2867. continue;
  2868. qed_wr(p_hwfn, p_ptt,
  2869. NIG_REG_LLH_FUNC_FILTER_VALUE +
  2870. 2 * i * sizeof(u32), low);
  2871. qed_wr(p_hwfn, p_ptt,
  2872. NIG_REG_LLH_FUNC_FILTER_VALUE +
  2873. (2 * i + 1) * sizeof(u32), high);
  2874. qed_wr(p_hwfn, p_ptt,
  2875. NIG_REG_LLH_FUNC_FILTER_MODE + i * sizeof(u32), 1);
  2876. qed_wr(p_hwfn, p_ptt,
  2877. NIG_REG_LLH_FUNC_FILTER_PROTOCOL_TYPE +
  2878. i * sizeof(u32), 1 << type);
  2879. qed_wr(p_hwfn, p_ptt,
  2880. NIG_REG_LLH_FUNC_FILTER_EN + i * sizeof(u32), 1);
  2881. break;
  2882. }
  2883. if (i >= NIG_REG_LLH_FUNC_FILTER_EN_SIZE) {
  2884. DP_NOTICE(p_hwfn,
  2885. "Failed to find an empty LLH filter to utilize\n");
  2886. return -EINVAL;
  2887. }
  2888. switch (type) {
  2889. case QED_LLH_FILTER_ETHERTYPE:
  2890. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  2891. "ETH type %x is added at %d\n",
  2892. source_port_or_eth_type, i);
  2893. break;
  2894. case QED_LLH_FILTER_TCP_SRC_PORT:
  2895. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  2896. "TCP src port %x is added at %d\n",
  2897. source_port_or_eth_type, i);
  2898. break;
  2899. case QED_LLH_FILTER_UDP_SRC_PORT:
  2900. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  2901. "UDP src port %x is added at %d\n",
  2902. source_port_or_eth_type, i);
  2903. break;
  2904. case QED_LLH_FILTER_TCP_DEST_PORT:
  2905. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  2906. "TCP dst port %x is added at %d\n", dest_port, i);
  2907. break;
  2908. case QED_LLH_FILTER_UDP_DEST_PORT:
  2909. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  2910. "UDP dst port %x is added at %d\n", dest_port, i);
  2911. break;
  2912. case QED_LLH_FILTER_TCP_SRC_AND_DEST_PORT:
  2913. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  2914. "TCP src/dst ports %x/%x are added at %d\n",
  2915. source_port_or_eth_type, dest_port, i);
  2916. break;
  2917. case QED_LLH_FILTER_UDP_SRC_AND_DEST_PORT:
  2918. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  2919. "UDP src/dst ports %x/%x are added at %d\n",
  2920. source_port_or_eth_type, dest_port, i);
  2921. break;
  2922. }
  2923. return 0;
  2924. }
  2925. void
  2926. qed_llh_remove_protocol_filter(struct qed_hwfn *p_hwfn,
  2927. struct qed_ptt *p_ptt,
  2928. u16 source_port_or_eth_type,
  2929. u16 dest_port,
  2930. enum qed_llh_port_filter_type_t type)
  2931. {
  2932. u32 high = 0, low = 0;
  2933. int i;
  2934. if (!(IS_MF_SI(p_hwfn) || IS_MF_DEFAULT(p_hwfn)))
  2935. return;
  2936. switch (type) {
  2937. case QED_LLH_FILTER_ETHERTYPE:
  2938. high = source_port_or_eth_type;
  2939. break;
  2940. case QED_LLH_FILTER_TCP_SRC_PORT:
  2941. case QED_LLH_FILTER_UDP_SRC_PORT:
  2942. low = source_port_or_eth_type << 16;
  2943. break;
  2944. case QED_LLH_FILTER_TCP_DEST_PORT:
  2945. case QED_LLH_FILTER_UDP_DEST_PORT:
  2946. low = dest_port;
  2947. break;
  2948. case QED_LLH_FILTER_TCP_SRC_AND_DEST_PORT:
  2949. case QED_LLH_FILTER_UDP_SRC_AND_DEST_PORT:
  2950. low = (source_port_or_eth_type << 16) | dest_port;
  2951. break;
  2952. default:
  2953. DP_NOTICE(p_hwfn,
  2954. "Non valid LLH protocol filter type %d\n", type);
  2955. return;
  2956. }
  2957. for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
  2958. if (!qed_rd(p_hwfn, p_ptt,
  2959. NIG_REG_LLH_FUNC_FILTER_EN + i * sizeof(u32)))
  2960. continue;
  2961. if (!qed_rd(p_hwfn, p_ptt,
  2962. NIG_REG_LLH_FUNC_FILTER_MODE + i * sizeof(u32)))
  2963. continue;
  2964. if (!(qed_rd(p_hwfn, p_ptt,
  2965. NIG_REG_LLH_FUNC_FILTER_PROTOCOL_TYPE +
  2966. i * sizeof(u32)) & BIT(type)))
  2967. continue;
  2968. if (qed_rd(p_hwfn, p_ptt,
  2969. NIG_REG_LLH_FUNC_FILTER_VALUE +
  2970. 2 * i * sizeof(u32)) != low)
  2971. continue;
  2972. if (qed_rd(p_hwfn, p_ptt,
  2973. NIG_REG_LLH_FUNC_FILTER_VALUE +
  2974. (2 * i + 1) * sizeof(u32)) != high)
  2975. continue;
  2976. qed_wr(p_hwfn, p_ptt,
  2977. NIG_REG_LLH_FUNC_FILTER_EN + i * sizeof(u32), 0);
  2978. qed_wr(p_hwfn, p_ptt,
  2979. NIG_REG_LLH_FUNC_FILTER_MODE + i * sizeof(u32), 0);
  2980. qed_wr(p_hwfn, p_ptt,
  2981. NIG_REG_LLH_FUNC_FILTER_PROTOCOL_TYPE +
  2982. i * sizeof(u32), 0);
  2983. qed_wr(p_hwfn, p_ptt,
  2984. NIG_REG_LLH_FUNC_FILTER_VALUE + 2 * i * sizeof(u32), 0);
  2985. qed_wr(p_hwfn, p_ptt,
  2986. NIG_REG_LLH_FUNC_FILTER_VALUE +
  2987. (2 * i + 1) * sizeof(u32), 0);
  2988. break;
  2989. }
  2990. if (i >= NIG_REG_LLH_FUNC_FILTER_EN_SIZE)
  2991. DP_NOTICE(p_hwfn, "Tried to remove a non-configured filter\n");
  2992. }
  2993. static int qed_set_coalesce(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt,
  2994. u32 hw_addr, void *p_eth_qzone,
  2995. size_t eth_qzone_size, u8 timeset)
  2996. {
  2997. struct coalescing_timeset *p_coal_timeset;
  2998. if (p_hwfn->cdev->int_coalescing_mode != QED_COAL_MODE_ENABLE) {
  2999. DP_NOTICE(p_hwfn, "Coalescing configuration not enabled\n");
  3000. return -EINVAL;
  3001. }
  3002. p_coal_timeset = p_eth_qzone;
  3003. memset(p_coal_timeset, 0, eth_qzone_size);
  3004. SET_FIELD(p_coal_timeset->value, COALESCING_TIMESET_TIMESET, timeset);
  3005. SET_FIELD(p_coal_timeset->value, COALESCING_TIMESET_VALID, 1);
  3006. qed_memcpy_to(p_hwfn, p_ptt, hw_addr, p_eth_qzone, eth_qzone_size);
  3007. return 0;
  3008. }
  3009. int qed_set_rxq_coalesce(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt,
  3010. u16 coalesce, u16 qid, u16 sb_id)
  3011. {
  3012. struct ustorm_eth_queue_zone eth_qzone;
  3013. u8 timeset, timer_res;
  3014. u16 fw_qid = 0;
  3015. u32 address;
  3016. int rc;
  3017. /* Coalesce = (timeset << timer-resolution), timeset is 7bit wide */
  3018. if (coalesce <= 0x7F) {
  3019. timer_res = 0;
  3020. } else if (coalesce <= 0xFF) {
  3021. timer_res = 1;
  3022. } else if (coalesce <= 0x1FF) {
  3023. timer_res = 2;
  3024. } else {
  3025. DP_ERR(p_hwfn, "Invalid coalesce value - %d\n", coalesce);
  3026. return -EINVAL;
  3027. }
  3028. timeset = (u8)(coalesce >> timer_res);
  3029. rc = qed_fw_l2_queue(p_hwfn, qid, &fw_qid);
  3030. if (rc)
  3031. return rc;
  3032. rc = qed_int_set_timer_res(p_hwfn, p_ptt, timer_res, sb_id, false);
  3033. if (rc)
  3034. goto out;
  3035. address = BAR0_MAP_REG_USDM_RAM + USTORM_ETH_QUEUE_ZONE_OFFSET(fw_qid);
  3036. rc = qed_set_coalesce(p_hwfn, p_ptt, address, &eth_qzone,
  3037. sizeof(struct ustorm_eth_queue_zone), timeset);
  3038. if (rc)
  3039. goto out;
  3040. p_hwfn->cdev->rx_coalesce_usecs = coalesce;
  3041. out:
  3042. return rc;
  3043. }
  3044. int qed_set_txq_coalesce(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt,
  3045. u16 coalesce, u16 qid, u16 sb_id)
  3046. {
  3047. struct xstorm_eth_queue_zone eth_qzone;
  3048. u8 timeset, timer_res;
  3049. u16 fw_qid = 0;
  3050. u32 address;
  3051. int rc;
  3052. /* Coalesce = (timeset << timer-resolution), timeset is 7bit wide */
  3053. if (coalesce <= 0x7F) {
  3054. timer_res = 0;
  3055. } else if (coalesce <= 0xFF) {
  3056. timer_res = 1;
  3057. } else if (coalesce <= 0x1FF) {
  3058. timer_res = 2;
  3059. } else {
  3060. DP_ERR(p_hwfn, "Invalid coalesce value - %d\n", coalesce);
  3061. return -EINVAL;
  3062. }
  3063. timeset = (u8)(coalesce >> timer_res);
  3064. rc = qed_fw_l2_queue(p_hwfn, qid, &fw_qid);
  3065. if (rc)
  3066. return rc;
  3067. rc = qed_int_set_timer_res(p_hwfn, p_ptt, timer_res, sb_id, true);
  3068. if (rc)
  3069. goto out;
  3070. address = BAR0_MAP_REG_XSDM_RAM + XSTORM_ETH_QUEUE_ZONE_OFFSET(fw_qid);
  3071. rc = qed_set_coalesce(p_hwfn, p_ptt, address, &eth_qzone,
  3072. sizeof(struct xstorm_eth_queue_zone), timeset);
  3073. if (rc)
  3074. goto out;
  3075. p_hwfn->cdev->tx_coalesce_usecs = coalesce;
  3076. out:
  3077. return rc;
  3078. }
  3079. /* Calculate final WFQ values for all vports and configure them.
  3080. * After this configuration each vport will have
  3081. * approx min rate = min_pf_rate * (vport_wfq / QED_WFQ_UNIT)
  3082. */
  3083. static void qed_configure_wfq_for_all_vports(struct qed_hwfn *p_hwfn,
  3084. struct qed_ptt *p_ptt,
  3085. u32 min_pf_rate)
  3086. {
  3087. struct init_qm_vport_params *vport_params;
  3088. int i;
  3089. vport_params = p_hwfn->qm_info.qm_vport_params;
  3090. for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
  3091. u32 wfq_speed = p_hwfn->qm_info.wfq_data[i].min_speed;
  3092. vport_params[i].vport_wfq = (wfq_speed * QED_WFQ_UNIT) /
  3093. min_pf_rate;
  3094. qed_init_vport_wfq(p_hwfn, p_ptt,
  3095. vport_params[i].first_tx_pq_id,
  3096. vport_params[i].vport_wfq);
  3097. }
  3098. }
  3099. static void qed_init_wfq_default_param(struct qed_hwfn *p_hwfn,
  3100. u32 min_pf_rate)
  3101. {
  3102. int i;
  3103. for (i = 0; i < p_hwfn->qm_info.num_vports; i++)
  3104. p_hwfn->qm_info.qm_vport_params[i].vport_wfq = 1;
  3105. }
  3106. static void qed_disable_wfq_for_all_vports(struct qed_hwfn *p_hwfn,
  3107. struct qed_ptt *p_ptt,
  3108. u32 min_pf_rate)
  3109. {
  3110. struct init_qm_vport_params *vport_params;
  3111. int i;
  3112. vport_params = p_hwfn->qm_info.qm_vport_params;
  3113. for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
  3114. qed_init_wfq_default_param(p_hwfn, min_pf_rate);
  3115. qed_init_vport_wfq(p_hwfn, p_ptt,
  3116. vport_params[i].first_tx_pq_id,
  3117. vport_params[i].vport_wfq);
  3118. }
  3119. }
  3120. /* This function performs several validations for WFQ
  3121. * configuration and required min rate for a given vport
  3122. * 1. req_rate must be greater than one percent of min_pf_rate.
  3123. * 2. req_rate should not cause other vports [not configured for WFQ explicitly]
  3124. * rates to get less than one percent of min_pf_rate.
  3125. * 3. total_req_min_rate [all vports min rate sum] shouldn't exceed min_pf_rate.
  3126. */
  3127. static int qed_init_wfq_param(struct qed_hwfn *p_hwfn,
  3128. u16 vport_id, u32 req_rate, u32 min_pf_rate)
  3129. {
  3130. u32 total_req_min_rate = 0, total_left_rate = 0, left_rate_per_vp = 0;
  3131. int non_requested_count = 0, req_count = 0, i, num_vports;
  3132. num_vports = p_hwfn->qm_info.num_vports;
  3133. /* Accounting for the vports which are configured for WFQ explicitly */
  3134. for (i = 0; i < num_vports; i++) {
  3135. u32 tmp_speed;
  3136. if ((i != vport_id) &&
  3137. p_hwfn->qm_info.wfq_data[i].configured) {
  3138. req_count++;
  3139. tmp_speed = p_hwfn->qm_info.wfq_data[i].min_speed;
  3140. total_req_min_rate += tmp_speed;
  3141. }
  3142. }
  3143. /* Include current vport data as well */
  3144. req_count++;
  3145. total_req_min_rate += req_rate;
  3146. non_requested_count = num_vports - req_count;
  3147. if (req_rate < min_pf_rate / QED_WFQ_UNIT) {
  3148. DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
  3149. "Vport [%d] - Requested rate[%d Mbps] is less than one percent of configured PF min rate[%d Mbps]\n",
  3150. vport_id, req_rate, min_pf_rate);
  3151. return -EINVAL;
  3152. }
  3153. if (num_vports > QED_WFQ_UNIT) {
  3154. DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
  3155. "Number of vports is greater than %d\n",
  3156. QED_WFQ_UNIT);
  3157. return -EINVAL;
  3158. }
  3159. if (total_req_min_rate > min_pf_rate) {
  3160. DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
  3161. "Total requested min rate for all vports[%d Mbps] is greater than configured PF min rate[%d Mbps]\n",
  3162. total_req_min_rate, min_pf_rate);
  3163. return -EINVAL;
  3164. }
  3165. total_left_rate = min_pf_rate - total_req_min_rate;
  3166. left_rate_per_vp = total_left_rate / non_requested_count;
  3167. if (left_rate_per_vp < min_pf_rate / QED_WFQ_UNIT) {
  3168. DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
  3169. "Non WFQ configured vports rate [%d Mbps] is less than one percent of configured PF min rate[%d Mbps]\n",
  3170. left_rate_per_vp, min_pf_rate);
  3171. return -EINVAL;
  3172. }
  3173. p_hwfn->qm_info.wfq_data[vport_id].min_speed = req_rate;
  3174. p_hwfn->qm_info.wfq_data[vport_id].configured = true;
  3175. for (i = 0; i < num_vports; i++) {
  3176. if (p_hwfn->qm_info.wfq_data[i].configured)
  3177. continue;
  3178. p_hwfn->qm_info.wfq_data[i].min_speed = left_rate_per_vp;
  3179. }
  3180. return 0;
  3181. }
  3182. static int __qed_configure_vport_wfq(struct qed_hwfn *p_hwfn,
  3183. struct qed_ptt *p_ptt, u16 vp_id, u32 rate)
  3184. {
  3185. struct qed_mcp_link_state *p_link;
  3186. int rc = 0;
  3187. p_link = &p_hwfn->cdev->hwfns[0].mcp_info->link_output;
  3188. if (!p_link->min_pf_rate) {
  3189. p_hwfn->qm_info.wfq_data[vp_id].min_speed = rate;
  3190. p_hwfn->qm_info.wfq_data[vp_id].configured = true;
  3191. return rc;
  3192. }
  3193. rc = qed_init_wfq_param(p_hwfn, vp_id, rate, p_link->min_pf_rate);
  3194. if (!rc)
  3195. qed_configure_wfq_for_all_vports(p_hwfn, p_ptt,
  3196. p_link->min_pf_rate);
  3197. else
  3198. DP_NOTICE(p_hwfn,
  3199. "Validation failed while configuring min rate\n");
  3200. return rc;
  3201. }
  3202. static int __qed_configure_vp_wfq_on_link_change(struct qed_hwfn *p_hwfn,
  3203. struct qed_ptt *p_ptt,
  3204. u32 min_pf_rate)
  3205. {
  3206. bool use_wfq = false;
  3207. int rc = 0;
  3208. u16 i;
  3209. /* Validate all pre configured vports for wfq */
  3210. for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
  3211. u32 rate;
  3212. if (!p_hwfn->qm_info.wfq_data[i].configured)
  3213. continue;
  3214. rate = p_hwfn->qm_info.wfq_data[i].min_speed;
  3215. use_wfq = true;
  3216. rc = qed_init_wfq_param(p_hwfn, i, rate, min_pf_rate);
  3217. if (rc) {
  3218. DP_NOTICE(p_hwfn,
  3219. "WFQ validation failed while configuring min rate\n");
  3220. break;
  3221. }
  3222. }
  3223. if (!rc && use_wfq)
  3224. qed_configure_wfq_for_all_vports(p_hwfn, p_ptt, min_pf_rate);
  3225. else
  3226. qed_disable_wfq_for_all_vports(p_hwfn, p_ptt, min_pf_rate);
  3227. return rc;
  3228. }
  3229. /* Main API for qed clients to configure vport min rate.
  3230. * vp_id - vport id in PF Range[0 - (total_num_vports_per_pf - 1)]
  3231. * rate - Speed in Mbps needs to be assigned to a given vport.
  3232. */
  3233. int qed_configure_vport_wfq(struct qed_dev *cdev, u16 vp_id, u32 rate)
  3234. {
  3235. int i, rc = -EINVAL;
  3236. /* Currently not supported; Might change in future */
  3237. if (cdev->num_hwfns > 1) {
  3238. DP_NOTICE(cdev,
  3239. "WFQ configuration is not supported for this device\n");
  3240. return rc;
  3241. }
  3242. for_each_hwfn(cdev, i) {
  3243. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  3244. struct qed_ptt *p_ptt;
  3245. p_ptt = qed_ptt_acquire(p_hwfn);
  3246. if (!p_ptt)
  3247. return -EBUSY;
  3248. rc = __qed_configure_vport_wfq(p_hwfn, p_ptt, vp_id, rate);
  3249. if (rc) {
  3250. qed_ptt_release(p_hwfn, p_ptt);
  3251. return rc;
  3252. }
  3253. qed_ptt_release(p_hwfn, p_ptt);
  3254. }
  3255. return rc;
  3256. }
  3257. /* API to configure WFQ from mcp link change */
  3258. void qed_configure_vp_wfq_on_link_change(struct qed_dev *cdev,
  3259. struct qed_ptt *p_ptt, u32 min_pf_rate)
  3260. {
  3261. int i;
  3262. if (cdev->num_hwfns > 1) {
  3263. DP_VERBOSE(cdev,
  3264. NETIF_MSG_LINK,
  3265. "WFQ configuration is not supported for this device\n");
  3266. return;
  3267. }
  3268. for_each_hwfn(cdev, i) {
  3269. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  3270. __qed_configure_vp_wfq_on_link_change(p_hwfn, p_ptt,
  3271. min_pf_rate);
  3272. }
  3273. }
  3274. int __qed_configure_pf_max_bandwidth(struct qed_hwfn *p_hwfn,
  3275. struct qed_ptt *p_ptt,
  3276. struct qed_mcp_link_state *p_link,
  3277. u8 max_bw)
  3278. {
  3279. int rc = 0;
  3280. p_hwfn->mcp_info->func_info.bandwidth_max = max_bw;
  3281. if (!p_link->line_speed && (max_bw != 100))
  3282. return rc;
  3283. p_link->speed = (p_link->line_speed * max_bw) / 100;
  3284. p_hwfn->qm_info.pf_rl = p_link->speed;
  3285. /* Since the limiter also affects Tx-switched traffic, we don't want it
  3286. * to limit such traffic in case there's no actual limit.
  3287. * In that case, set limit to imaginary high boundary.
  3288. */
  3289. if (max_bw == 100)
  3290. p_hwfn->qm_info.pf_rl = 100000;
  3291. rc = qed_init_pf_rl(p_hwfn, p_ptt, p_hwfn->rel_pf_id,
  3292. p_hwfn->qm_info.pf_rl);
  3293. DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
  3294. "Configured MAX bandwidth to be %08x Mb/sec\n",
  3295. p_link->speed);
  3296. return rc;
  3297. }
  3298. /* Main API to configure PF max bandwidth where bw range is [1 - 100] */
  3299. int qed_configure_pf_max_bandwidth(struct qed_dev *cdev, u8 max_bw)
  3300. {
  3301. int i, rc = -EINVAL;
  3302. if (max_bw < 1 || max_bw > 100) {
  3303. DP_NOTICE(cdev, "PF max bw valid range is [1-100]\n");
  3304. return rc;
  3305. }
  3306. for_each_hwfn(cdev, i) {
  3307. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  3308. struct qed_hwfn *p_lead = QED_LEADING_HWFN(cdev);
  3309. struct qed_mcp_link_state *p_link;
  3310. struct qed_ptt *p_ptt;
  3311. p_link = &p_lead->mcp_info->link_output;
  3312. p_ptt = qed_ptt_acquire(p_hwfn);
  3313. if (!p_ptt)
  3314. return -EBUSY;
  3315. rc = __qed_configure_pf_max_bandwidth(p_hwfn, p_ptt,
  3316. p_link, max_bw);
  3317. qed_ptt_release(p_hwfn, p_ptt);
  3318. if (rc)
  3319. break;
  3320. }
  3321. return rc;
  3322. }
  3323. int __qed_configure_pf_min_bandwidth(struct qed_hwfn *p_hwfn,
  3324. struct qed_ptt *p_ptt,
  3325. struct qed_mcp_link_state *p_link,
  3326. u8 min_bw)
  3327. {
  3328. int rc = 0;
  3329. p_hwfn->mcp_info->func_info.bandwidth_min = min_bw;
  3330. p_hwfn->qm_info.pf_wfq = min_bw;
  3331. if (!p_link->line_speed)
  3332. return rc;
  3333. p_link->min_pf_rate = (p_link->line_speed * min_bw) / 100;
  3334. rc = qed_init_pf_wfq(p_hwfn, p_ptt, p_hwfn->rel_pf_id, min_bw);
  3335. DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
  3336. "Configured MIN bandwidth to be %d Mb/sec\n",
  3337. p_link->min_pf_rate);
  3338. return rc;
  3339. }
  3340. /* Main API to configure PF min bandwidth where bw range is [1-100] */
  3341. int qed_configure_pf_min_bandwidth(struct qed_dev *cdev, u8 min_bw)
  3342. {
  3343. int i, rc = -EINVAL;
  3344. if (min_bw < 1 || min_bw > 100) {
  3345. DP_NOTICE(cdev, "PF min bw valid range is [1-100]\n");
  3346. return rc;
  3347. }
  3348. for_each_hwfn(cdev, i) {
  3349. struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
  3350. struct qed_hwfn *p_lead = QED_LEADING_HWFN(cdev);
  3351. struct qed_mcp_link_state *p_link;
  3352. struct qed_ptt *p_ptt;
  3353. p_link = &p_lead->mcp_info->link_output;
  3354. p_ptt = qed_ptt_acquire(p_hwfn);
  3355. if (!p_ptt)
  3356. return -EBUSY;
  3357. rc = __qed_configure_pf_min_bandwidth(p_hwfn, p_ptt,
  3358. p_link, min_bw);
  3359. if (rc) {
  3360. qed_ptt_release(p_hwfn, p_ptt);
  3361. return rc;
  3362. }
  3363. if (p_link->min_pf_rate) {
  3364. u32 min_rate = p_link->min_pf_rate;
  3365. rc = __qed_configure_vp_wfq_on_link_change(p_hwfn,
  3366. p_ptt,
  3367. min_rate);
  3368. }
  3369. qed_ptt_release(p_hwfn, p_ptt);
  3370. }
  3371. return rc;
  3372. }
  3373. void qed_clean_wfq_db(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  3374. {
  3375. struct qed_mcp_link_state *p_link;
  3376. p_link = &p_hwfn->mcp_info->link_output;
  3377. if (p_link->min_pf_rate)
  3378. qed_disable_wfq_for_all_vports(p_hwfn, p_ptt,
  3379. p_link->min_pf_rate);
  3380. memset(p_hwfn->qm_info.wfq_data, 0,
  3381. sizeof(*p_hwfn->qm_info.wfq_data) * p_hwfn->qm_info.num_vports);
  3382. }
  3383. int qed_device_num_engines(struct qed_dev *cdev)
  3384. {
  3385. return QED_IS_BB(cdev) ? 2 : 1;
  3386. }
  3387. static int qed_device_num_ports(struct qed_dev *cdev)
  3388. {
  3389. /* in CMT always only one port */
  3390. if (cdev->num_hwfns > 1)
  3391. return 1;
  3392. return cdev->num_ports_in_engine * qed_device_num_engines(cdev);
  3393. }
  3394. int qed_device_get_port_id(struct qed_dev *cdev)
  3395. {
  3396. return (QED_LEADING_HWFN(cdev)->abs_pf_id) % qed_device_num_ports(cdev);
  3397. }