lio_main.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394
  1. /**********************************************************************
  2. * Author: Cavium, Inc.
  3. *
  4. * Contact: support@cavium.com
  5. * Please include "LiquidIO" in the subject.
  6. *
  7. * Copyright (c) 2003-2016 Cavium, Inc.
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more details.
  17. ***********************************************************************/
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/pci.h>
  21. #include <linux/firmware.h>
  22. #include <net/vxlan.h>
  23. #include <linux/kthread.h>
  24. #include <net/switchdev.h>
  25. #include "liquidio_common.h"
  26. #include "octeon_droq.h"
  27. #include "octeon_iq.h"
  28. #include "response_manager.h"
  29. #include "octeon_device.h"
  30. #include "octeon_nic.h"
  31. #include "octeon_main.h"
  32. #include "octeon_network.h"
  33. #include "cn66xx_regs.h"
  34. #include "cn66xx_device.h"
  35. #include "cn68xx_device.h"
  36. #include "cn23xx_pf_device.h"
  37. #include "liquidio_image.h"
  38. #include "lio_vf_rep.h"
  39. MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
  40. MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
  41. MODULE_LICENSE("GPL");
  42. MODULE_VERSION(LIQUIDIO_VERSION);
  43. MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME
  44. "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
  45. MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME
  46. "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
  47. MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME
  48. "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
  49. MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_23XX_NAME
  50. "_" LIO_FW_NAME_TYPE_NIC LIO_FW_NAME_SUFFIX);
  51. static int ddr_timeout = 10000;
  52. module_param(ddr_timeout, int, 0644);
  53. MODULE_PARM_DESC(ddr_timeout,
  54. "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
  55. #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
  56. static int debug = -1;
  57. module_param(debug, int, 0644);
  58. MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
  59. static char fw_type[LIO_MAX_FW_TYPE_LEN] = LIO_FW_NAME_TYPE_AUTO;
  60. module_param_string(fw_type, fw_type, sizeof(fw_type), 0444);
  61. MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded (default is \"auto\"), which uses firmware in flash, if present, else loads \"nic\".");
  62. static u32 console_bitmask;
  63. module_param(console_bitmask, int, 0644);
  64. MODULE_PARM_DESC(console_bitmask,
  65. "Bitmask indicating which consoles have debug output redirected to syslog.");
  66. /**
  67. * \brief determines if a given console has debug enabled.
  68. * @param console console to check
  69. * @returns 1 = enabled. 0 otherwise
  70. */
  71. static int octeon_console_debug_enabled(u32 console)
  72. {
  73. return (console_bitmask >> (console)) & 0x1;
  74. }
  75. /* Polling interval for determining when NIC application is alive */
  76. #define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
  77. /* runtime link query interval */
  78. #define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
  79. /* update localtime to octeon firmware every 60 seconds.
  80. * make firmware to use same time reference, so that it will be easy to
  81. * correlate firmware logged events/errors with host events, for debugging.
  82. */
  83. #define LIO_SYNC_OCTEON_TIME_INTERVAL_MS 60000
  84. struct lio_trusted_vf_ctx {
  85. struct completion complete;
  86. int status;
  87. };
  88. struct liquidio_rx_ctl_context {
  89. int octeon_id;
  90. wait_queue_head_t wc;
  91. int cond;
  92. };
  93. struct oct_link_status_resp {
  94. u64 rh;
  95. struct oct_link_info link_info;
  96. u64 status;
  97. };
  98. struct oct_timestamp_resp {
  99. u64 rh;
  100. u64 timestamp;
  101. u64 status;
  102. };
  103. #define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
  104. union tx_info {
  105. u64 u64;
  106. struct {
  107. #ifdef __BIG_ENDIAN_BITFIELD
  108. u16 gso_size;
  109. u16 gso_segs;
  110. u32 reserved;
  111. #else
  112. u32 reserved;
  113. u16 gso_segs;
  114. u16 gso_size;
  115. #endif
  116. } s;
  117. };
  118. /** Octeon device properties to be used by the NIC module.
  119. * Each octeon device in the system will be represented
  120. * by this structure in the NIC module.
  121. */
  122. #define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
  123. #define OCTNIC_GSO_MAX_HEADER_SIZE 128
  124. #define OCTNIC_GSO_MAX_SIZE \
  125. (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
  126. /** Structure of a node in list of gather components maintained by
  127. * NIC driver for each network device.
  128. */
  129. struct octnic_gather {
  130. /** List manipulation. Next and prev pointers. */
  131. struct list_head list;
  132. /** Size of the gather component at sg in bytes. */
  133. int sg_size;
  134. /** Number of bytes that sg was adjusted to make it 8B-aligned. */
  135. int adjust;
  136. /** Gather component that can accommodate max sized fragment list
  137. * received from the IP layer.
  138. */
  139. struct octeon_sg_entry *sg;
  140. dma_addr_t sg_dma_ptr;
  141. };
  142. struct handshake {
  143. struct completion init;
  144. struct completion started;
  145. struct pci_dev *pci_dev;
  146. int init_ok;
  147. int started_ok;
  148. };
  149. #ifdef CONFIG_PCI_IOV
  150. static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs);
  151. #endif
  152. static int octeon_dbg_console_print(struct octeon_device *oct, u32 console_num,
  153. char *prefix, char *suffix);
  154. static int octeon_device_init(struct octeon_device *);
  155. static int liquidio_stop(struct net_device *netdev);
  156. static void liquidio_remove(struct pci_dev *pdev);
  157. static int liquidio_probe(struct pci_dev *pdev,
  158. const struct pci_device_id *ent);
  159. static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
  160. int linkstate);
  161. static struct handshake handshake[MAX_OCTEON_DEVICES];
  162. static struct completion first_stage;
  163. static void octeon_droq_bh(unsigned long pdev)
  164. {
  165. int q_no;
  166. int reschedule = 0;
  167. struct octeon_device *oct = (struct octeon_device *)pdev;
  168. struct octeon_device_priv *oct_priv =
  169. (struct octeon_device_priv *)oct->priv;
  170. for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
  171. if (!(oct->io_qmask.oq & BIT_ULL(q_no)))
  172. continue;
  173. reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
  174. MAX_PACKET_BUDGET);
  175. lio_enable_irq(oct->droq[q_no], NULL);
  176. if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
  177. /* set time and cnt interrupt thresholds for this DROQ
  178. * for NAPI
  179. */
  180. int adjusted_q_no = q_no + oct->sriov_info.pf_srn;
  181. octeon_write_csr64(
  182. oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
  183. 0x5700000040ULL);
  184. octeon_write_csr64(
  185. oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
  186. }
  187. }
  188. if (reschedule)
  189. tasklet_schedule(&oct_priv->droq_tasklet);
  190. }
  191. static int lio_wait_for_oq_pkts(struct octeon_device *oct)
  192. {
  193. struct octeon_device_priv *oct_priv =
  194. (struct octeon_device_priv *)oct->priv;
  195. int retry = 100, pkt_cnt = 0, pending_pkts = 0;
  196. int i;
  197. do {
  198. pending_pkts = 0;
  199. for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
  200. if (!(oct->io_qmask.oq & BIT_ULL(i)))
  201. continue;
  202. pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
  203. }
  204. if (pkt_cnt > 0) {
  205. pending_pkts += pkt_cnt;
  206. tasklet_schedule(&oct_priv->droq_tasklet);
  207. }
  208. pkt_cnt = 0;
  209. schedule_timeout_uninterruptible(1);
  210. } while (retry-- && pending_pkts);
  211. return pkt_cnt;
  212. }
  213. /**
  214. * \brief Forces all IO queues off on a given device
  215. * @param oct Pointer to Octeon device
  216. */
  217. static void force_io_queues_off(struct octeon_device *oct)
  218. {
  219. if ((oct->chip_id == OCTEON_CN66XX) ||
  220. (oct->chip_id == OCTEON_CN68XX)) {
  221. /* Reset the Enable bits for Input Queues. */
  222. octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
  223. /* Reset the Enable bits for Output Queues. */
  224. octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
  225. }
  226. }
  227. /**
  228. * \brief Cause device to go quiet so it can be safely removed/reset/etc
  229. * @param oct Pointer to Octeon device
  230. */
  231. static inline void pcierror_quiesce_device(struct octeon_device *oct)
  232. {
  233. int i;
  234. /* Disable the input and output queues now. No more packets will
  235. * arrive from Octeon, but we should wait for all packet processing
  236. * to finish.
  237. */
  238. force_io_queues_off(oct);
  239. /* To allow for in-flight requests */
  240. schedule_timeout_uninterruptible(100);
  241. if (wait_for_pending_requests(oct))
  242. dev_err(&oct->pci_dev->dev, "There were pending requests\n");
  243. /* Force all requests waiting to be fetched by OCTEON to complete. */
  244. for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
  245. struct octeon_instr_queue *iq;
  246. if (!(oct->io_qmask.iq & BIT_ULL(i)))
  247. continue;
  248. iq = oct->instr_queue[i];
  249. if (atomic_read(&iq->instr_pending)) {
  250. spin_lock_bh(&iq->lock);
  251. iq->fill_cnt = 0;
  252. iq->octeon_read_index = iq->host_write_index;
  253. iq->stats.instr_processed +=
  254. atomic_read(&iq->instr_pending);
  255. lio_process_iq_request_list(oct, iq, 0);
  256. spin_unlock_bh(&iq->lock);
  257. }
  258. }
  259. /* Force all pending ordered list requests to time out. */
  260. lio_process_ordered_list(oct, 1);
  261. /* We do not need to wait for output queue packets to be processed. */
  262. }
  263. /**
  264. * \brief Cleanup PCI AER uncorrectable error status
  265. * @param dev Pointer to PCI device
  266. */
  267. static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
  268. {
  269. int pos = 0x100;
  270. u32 status, mask;
  271. pr_info("%s :\n", __func__);
  272. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
  273. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
  274. if (dev->error_state == pci_channel_io_normal)
  275. status &= ~mask; /* Clear corresponding nonfatal bits */
  276. else
  277. status &= mask; /* Clear corresponding fatal bits */
  278. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
  279. }
  280. /**
  281. * \brief Stop all PCI IO to a given device
  282. * @param dev Pointer to Octeon device
  283. */
  284. static void stop_pci_io(struct octeon_device *oct)
  285. {
  286. /* No more instructions will be forwarded. */
  287. atomic_set(&oct->status, OCT_DEV_IN_RESET);
  288. pci_disable_device(oct->pci_dev);
  289. /* Disable interrupts */
  290. oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
  291. pcierror_quiesce_device(oct);
  292. /* Release the interrupt line */
  293. free_irq(oct->pci_dev->irq, oct);
  294. if (oct->flags & LIO_FLAG_MSI_ENABLED)
  295. pci_disable_msi(oct->pci_dev);
  296. dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
  297. lio_get_state_string(&oct->status));
  298. /* making it a common function for all OCTEON models */
  299. cleanup_aer_uncorrect_error_status(oct->pci_dev);
  300. }
  301. /**
  302. * \brief called when PCI error is detected
  303. * @param pdev Pointer to PCI device
  304. * @param state The current pci connection state
  305. *
  306. * This function is called after a PCI bus error affecting
  307. * this device has been detected.
  308. */
  309. static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
  310. pci_channel_state_t state)
  311. {
  312. struct octeon_device *oct = pci_get_drvdata(pdev);
  313. /* Non-correctable Non-fatal errors */
  314. if (state == pci_channel_io_normal) {
  315. dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
  316. cleanup_aer_uncorrect_error_status(oct->pci_dev);
  317. return PCI_ERS_RESULT_CAN_RECOVER;
  318. }
  319. /* Non-correctable Fatal errors */
  320. dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
  321. stop_pci_io(oct);
  322. /* Always return a DISCONNECT. There is no support for recovery but only
  323. * for a clean shutdown.
  324. */
  325. return PCI_ERS_RESULT_DISCONNECT;
  326. }
  327. /**
  328. * \brief mmio handler
  329. * @param pdev Pointer to PCI device
  330. */
  331. static pci_ers_result_t liquidio_pcie_mmio_enabled(
  332. struct pci_dev *pdev __attribute__((unused)))
  333. {
  334. /* We should never hit this since we never ask for a reset for a Fatal
  335. * Error. We always return DISCONNECT in io_error above.
  336. * But play safe and return RECOVERED for now.
  337. */
  338. return PCI_ERS_RESULT_RECOVERED;
  339. }
  340. /**
  341. * \brief called after the pci bus has been reset.
  342. * @param pdev Pointer to PCI device
  343. *
  344. * Restart the card from scratch, as if from a cold-boot. Implementation
  345. * resembles the first-half of the octeon_resume routine.
  346. */
  347. static pci_ers_result_t liquidio_pcie_slot_reset(
  348. struct pci_dev *pdev __attribute__((unused)))
  349. {
  350. /* We should never hit this since we never ask for a reset for a Fatal
  351. * Error. We always return DISCONNECT in io_error above.
  352. * But play safe and return RECOVERED for now.
  353. */
  354. return PCI_ERS_RESULT_RECOVERED;
  355. }
  356. /**
  357. * \brief called when traffic can start flowing again.
  358. * @param pdev Pointer to PCI device
  359. *
  360. * This callback is called when the error recovery driver tells us that
  361. * its OK to resume normal operation. Implementation resembles the
  362. * second-half of the octeon_resume routine.
  363. */
  364. static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
  365. {
  366. /* Nothing to be done here. */
  367. }
  368. #ifdef CONFIG_PM
  369. /**
  370. * \brief called when suspending
  371. * @param pdev Pointer to PCI device
  372. * @param state state to suspend to
  373. */
  374. static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
  375. pm_message_t state __attribute__((unused)))
  376. {
  377. return 0;
  378. }
  379. /**
  380. * \brief called when resuming
  381. * @param pdev Pointer to PCI device
  382. */
  383. static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
  384. {
  385. return 0;
  386. }
  387. #endif
  388. /* For PCI-E Advanced Error Recovery (AER) Interface */
  389. static const struct pci_error_handlers liquidio_err_handler = {
  390. .error_detected = liquidio_pcie_error_detected,
  391. .mmio_enabled = liquidio_pcie_mmio_enabled,
  392. .slot_reset = liquidio_pcie_slot_reset,
  393. .resume = liquidio_pcie_resume,
  394. };
  395. static const struct pci_device_id liquidio_pci_tbl[] = {
  396. { /* 68xx */
  397. PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
  398. },
  399. { /* 66xx */
  400. PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
  401. },
  402. { /* 23xx pf */
  403. PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
  404. },
  405. {
  406. 0, 0, 0, 0, 0, 0, 0
  407. }
  408. };
  409. MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
  410. static struct pci_driver liquidio_pci_driver = {
  411. .name = "LiquidIO",
  412. .id_table = liquidio_pci_tbl,
  413. .probe = liquidio_probe,
  414. .remove = liquidio_remove,
  415. .err_handler = &liquidio_err_handler, /* For AER */
  416. #ifdef CONFIG_PM
  417. .suspend = liquidio_suspend,
  418. .resume = liquidio_resume,
  419. #endif
  420. #ifdef CONFIG_PCI_IOV
  421. .sriov_configure = liquidio_enable_sriov,
  422. #endif
  423. };
  424. /**
  425. * \brief register PCI driver
  426. */
  427. static int liquidio_init_pci(void)
  428. {
  429. return pci_register_driver(&liquidio_pci_driver);
  430. }
  431. /**
  432. * \brief unregister PCI driver
  433. */
  434. static void liquidio_deinit_pci(void)
  435. {
  436. pci_unregister_driver(&liquidio_pci_driver);
  437. }
  438. /**
  439. * \brief Check Tx queue status, and take appropriate action
  440. * @param lio per-network private data
  441. * @returns 0 if full, number of queues woken up otherwise
  442. */
  443. static inline int check_txq_status(struct lio *lio)
  444. {
  445. int numqs = lio->netdev->num_tx_queues;
  446. int ret_val = 0;
  447. int q, iq;
  448. /* check each sub-queue state */
  449. for (q = 0; q < numqs; q++) {
  450. iq = lio->linfo.txpciq[q %
  451. lio->oct_dev->num_iqs].s.q_no;
  452. if (octnet_iq_is_full(lio->oct_dev, iq))
  453. continue;
  454. if (__netif_subqueue_stopped(lio->netdev, q)) {
  455. netif_wake_subqueue(lio->netdev, q);
  456. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
  457. tx_restart, 1);
  458. ret_val++;
  459. }
  460. }
  461. return ret_val;
  462. }
  463. /**
  464. * Remove the node at the head of the list. The list would be empty at
  465. * the end of this call if there are no more nodes in the list.
  466. */
  467. static inline struct list_head *list_delete_head(struct list_head *root)
  468. {
  469. struct list_head *node;
  470. if ((root->prev == root) && (root->next == root))
  471. node = NULL;
  472. else
  473. node = root->next;
  474. if (node)
  475. list_del(node);
  476. return node;
  477. }
  478. /**
  479. * \brief Delete gather lists
  480. * @param lio per-network private data
  481. */
  482. static void delete_glists(struct lio *lio)
  483. {
  484. struct octnic_gather *g;
  485. int i;
  486. kfree(lio->glist_lock);
  487. lio->glist_lock = NULL;
  488. if (!lio->glist)
  489. return;
  490. for (i = 0; i < lio->linfo.num_txpciq; i++) {
  491. do {
  492. g = (struct octnic_gather *)
  493. list_delete_head(&lio->glist[i]);
  494. if (g)
  495. kfree(g);
  496. } while (g);
  497. if (lio->glists_virt_base && lio->glists_virt_base[i] &&
  498. lio->glists_dma_base && lio->glists_dma_base[i]) {
  499. lio_dma_free(lio->oct_dev,
  500. lio->glist_entry_size * lio->tx_qsize,
  501. lio->glists_virt_base[i],
  502. lio->glists_dma_base[i]);
  503. }
  504. }
  505. kfree(lio->glists_virt_base);
  506. lio->glists_virt_base = NULL;
  507. kfree(lio->glists_dma_base);
  508. lio->glists_dma_base = NULL;
  509. kfree(lio->glist);
  510. lio->glist = NULL;
  511. }
  512. /**
  513. * \brief Setup gather lists
  514. * @param lio per-network private data
  515. */
  516. static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
  517. {
  518. int i, j;
  519. struct octnic_gather *g;
  520. lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
  521. GFP_KERNEL);
  522. if (!lio->glist_lock)
  523. return -ENOMEM;
  524. lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
  525. GFP_KERNEL);
  526. if (!lio->glist) {
  527. kfree(lio->glist_lock);
  528. lio->glist_lock = NULL;
  529. return -ENOMEM;
  530. }
  531. lio->glist_entry_size =
  532. ROUNDUP8((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
  533. /* allocate memory to store virtual and dma base address of
  534. * per glist consistent memory
  535. */
  536. lio->glists_virt_base = kcalloc(num_iqs, sizeof(*lio->glists_virt_base),
  537. GFP_KERNEL);
  538. lio->glists_dma_base = kcalloc(num_iqs, sizeof(*lio->glists_dma_base),
  539. GFP_KERNEL);
  540. if (!lio->glists_virt_base || !lio->glists_dma_base) {
  541. delete_glists(lio);
  542. return -ENOMEM;
  543. }
  544. for (i = 0; i < num_iqs; i++) {
  545. int numa_node = dev_to_node(&oct->pci_dev->dev);
  546. spin_lock_init(&lio->glist_lock[i]);
  547. INIT_LIST_HEAD(&lio->glist[i]);
  548. lio->glists_virt_base[i] =
  549. lio_dma_alloc(oct,
  550. lio->glist_entry_size * lio->tx_qsize,
  551. &lio->glists_dma_base[i]);
  552. if (!lio->glists_virt_base[i]) {
  553. delete_glists(lio);
  554. return -ENOMEM;
  555. }
  556. for (j = 0; j < lio->tx_qsize; j++) {
  557. g = kzalloc_node(sizeof(*g), GFP_KERNEL,
  558. numa_node);
  559. if (!g)
  560. g = kzalloc(sizeof(*g), GFP_KERNEL);
  561. if (!g)
  562. break;
  563. g->sg = lio->glists_virt_base[i] +
  564. (j * lio->glist_entry_size);
  565. g->sg_dma_ptr = lio->glists_dma_base[i] +
  566. (j * lio->glist_entry_size);
  567. list_add_tail(&g->list, &lio->glist[i]);
  568. }
  569. if (j != lio->tx_qsize) {
  570. delete_glists(lio);
  571. return -ENOMEM;
  572. }
  573. }
  574. return 0;
  575. }
  576. /**
  577. * \brief Print link information
  578. * @param netdev network device
  579. */
  580. static void print_link_info(struct net_device *netdev)
  581. {
  582. struct lio *lio = GET_LIO(netdev);
  583. if (!ifstate_check(lio, LIO_IFSTATE_RESETTING) &&
  584. ifstate_check(lio, LIO_IFSTATE_REGISTERED)) {
  585. struct oct_link_info *linfo = &lio->linfo;
  586. if (linfo->link.s.link_up) {
  587. netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
  588. linfo->link.s.speed,
  589. (linfo->link.s.duplex) ? "Full" : "Half");
  590. } else {
  591. netif_info(lio, link, lio->netdev, "Link Down\n");
  592. }
  593. }
  594. }
  595. /**
  596. * \brief Routine to notify MTU change
  597. * @param work work_struct data structure
  598. */
  599. static void octnet_link_status_change(struct work_struct *work)
  600. {
  601. struct cavium_wk *wk = (struct cavium_wk *)work;
  602. struct lio *lio = (struct lio *)wk->ctxptr;
  603. /* lio->linfo.link.s.mtu always contains max MTU of the lio interface.
  604. * this API is invoked only when new max-MTU of the interface is
  605. * less than current MTU.
  606. */
  607. rtnl_lock();
  608. dev_set_mtu(lio->netdev, lio->linfo.link.s.mtu);
  609. rtnl_unlock();
  610. }
  611. /**
  612. * \brief Sets up the mtu status change work
  613. * @param netdev network device
  614. */
  615. static inline int setup_link_status_change_wq(struct net_device *netdev)
  616. {
  617. struct lio *lio = GET_LIO(netdev);
  618. struct octeon_device *oct = lio->oct_dev;
  619. lio->link_status_wq.wq = alloc_workqueue("link-status",
  620. WQ_MEM_RECLAIM, 0);
  621. if (!lio->link_status_wq.wq) {
  622. dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
  623. return -1;
  624. }
  625. INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
  626. octnet_link_status_change);
  627. lio->link_status_wq.wk.ctxptr = lio;
  628. return 0;
  629. }
  630. static inline void cleanup_link_status_change_wq(struct net_device *netdev)
  631. {
  632. struct lio *lio = GET_LIO(netdev);
  633. if (lio->link_status_wq.wq) {
  634. cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
  635. destroy_workqueue(lio->link_status_wq.wq);
  636. }
  637. }
  638. /**
  639. * \brief Update link status
  640. * @param netdev network device
  641. * @param ls link status structure
  642. *
  643. * Called on receipt of a link status response from the core application to
  644. * update each interface's link status.
  645. */
  646. static inline void update_link_status(struct net_device *netdev,
  647. union oct_link_status *ls)
  648. {
  649. struct lio *lio = GET_LIO(netdev);
  650. int changed = (lio->linfo.link.u64 != ls->u64);
  651. int current_max_mtu = lio->linfo.link.s.mtu;
  652. struct octeon_device *oct = lio->oct_dev;
  653. dev_dbg(&oct->pci_dev->dev, "%s: lio->linfo.link.u64=%llx, ls->u64=%llx\n",
  654. __func__, lio->linfo.link.u64, ls->u64);
  655. lio->linfo.link.u64 = ls->u64;
  656. if ((lio->intf_open) && (changed)) {
  657. print_link_info(netdev);
  658. lio->link_changes++;
  659. if (lio->linfo.link.s.link_up) {
  660. dev_dbg(&oct->pci_dev->dev, "%s: link_up", __func__);
  661. netif_carrier_on(netdev);
  662. wake_txqs(netdev);
  663. } else {
  664. dev_dbg(&oct->pci_dev->dev, "%s: link_off", __func__);
  665. netif_carrier_off(netdev);
  666. stop_txqs(netdev);
  667. }
  668. if (lio->linfo.link.s.mtu != current_max_mtu) {
  669. netif_info(lio, probe, lio->netdev, "Max MTU changed from %d to %d\n",
  670. current_max_mtu, lio->linfo.link.s.mtu);
  671. netdev->max_mtu = lio->linfo.link.s.mtu;
  672. }
  673. if (lio->linfo.link.s.mtu < netdev->mtu) {
  674. dev_warn(&oct->pci_dev->dev,
  675. "Current MTU is higher than new max MTU; Reducing the current mtu from %d to %d\n",
  676. netdev->mtu, lio->linfo.link.s.mtu);
  677. queue_delayed_work(lio->link_status_wq.wq,
  678. &lio->link_status_wq.wk.work, 0);
  679. }
  680. }
  681. }
  682. /**
  683. * lio_sync_octeon_time_cb - callback that is invoked when soft command
  684. * sent by lio_sync_octeon_time() has completed successfully or failed
  685. *
  686. * @oct - octeon device structure
  687. * @status - indicates success or failure
  688. * @buf - pointer to the command that was sent to firmware
  689. **/
  690. static void lio_sync_octeon_time_cb(struct octeon_device *oct,
  691. u32 status, void *buf)
  692. {
  693. struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
  694. if (status)
  695. dev_err(&oct->pci_dev->dev,
  696. "Failed to sync time to octeon; error=%d\n", status);
  697. octeon_free_soft_command(oct, sc);
  698. }
  699. /**
  700. * lio_sync_octeon_time - send latest localtime to octeon firmware so that
  701. * firmware will correct it's time, in case there is a time skew
  702. *
  703. * @work: work scheduled to send time update to octeon firmware
  704. **/
  705. static void lio_sync_octeon_time(struct work_struct *work)
  706. {
  707. struct cavium_wk *wk = (struct cavium_wk *)work;
  708. struct lio *lio = (struct lio *)wk->ctxptr;
  709. struct octeon_device *oct = lio->oct_dev;
  710. struct octeon_soft_command *sc;
  711. struct timespec64 ts;
  712. struct lio_time *lt;
  713. int ret;
  714. sc = octeon_alloc_soft_command(oct, sizeof(struct lio_time), 0, 0);
  715. if (!sc) {
  716. dev_err(&oct->pci_dev->dev,
  717. "Failed to sync time to octeon: soft command allocation failed\n");
  718. return;
  719. }
  720. lt = (struct lio_time *)sc->virtdptr;
  721. /* Get time of the day */
  722. getnstimeofday64(&ts);
  723. lt->sec = ts.tv_sec;
  724. lt->nsec = ts.tv_nsec;
  725. octeon_swap_8B_data((u64 *)lt, (sizeof(struct lio_time)) / 8);
  726. sc->iq_no = lio->linfo.txpciq[0].s.q_no;
  727. octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
  728. OPCODE_NIC_SYNC_OCTEON_TIME, 0, 0, 0);
  729. sc->callback = lio_sync_octeon_time_cb;
  730. sc->callback_arg = sc;
  731. sc->wait_time = 1000;
  732. ret = octeon_send_soft_command(oct, sc);
  733. if (ret == IQ_SEND_FAILED) {
  734. dev_err(&oct->pci_dev->dev,
  735. "Failed to sync time to octeon: failed to send soft command\n");
  736. octeon_free_soft_command(oct, sc);
  737. }
  738. queue_delayed_work(lio->sync_octeon_time_wq.wq,
  739. &lio->sync_octeon_time_wq.wk.work,
  740. msecs_to_jiffies(LIO_SYNC_OCTEON_TIME_INTERVAL_MS));
  741. }
  742. /**
  743. * setup_sync_octeon_time_wq - Sets up the work to periodically update
  744. * local time to octeon firmware
  745. *
  746. * @netdev - network device which should send time update to firmware
  747. **/
  748. static inline int setup_sync_octeon_time_wq(struct net_device *netdev)
  749. {
  750. struct lio *lio = GET_LIO(netdev);
  751. struct octeon_device *oct = lio->oct_dev;
  752. lio->sync_octeon_time_wq.wq =
  753. alloc_workqueue("update-octeon-time", WQ_MEM_RECLAIM, 0);
  754. if (!lio->sync_octeon_time_wq.wq) {
  755. dev_err(&oct->pci_dev->dev, "Unable to create wq to update octeon time\n");
  756. return -1;
  757. }
  758. INIT_DELAYED_WORK(&lio->sync_octeon_time_wq.wk.work,
  759. lio_sync_octeon_time);
  760. lio->sync_octeon_time_wq.wk.ctxptr = lio;
  761. queue_delayed_work(lio->sync_octeon_time_wq.wq,
  762. &lio->sync_octeon_time_wq.wk.work,
  763. msecs_to_jiffies(LIO_SYNC_OCTEON_TIME_INTERVAL_MS));
  764. return 0;
  765. }
  766. /**
  767. * cleanup_sync_octeon_time_wq - stop scheduling and destroy the work created
  768. * to periodically update local time to octeon firmware
  769. *
  770. * @netdev - network device which should send time update to firmware
  771. **/
  772. static inline void cleanup_sync_octeon_time_wq(struct net_device *netdev)
  773. {
  774. struct lio *lio = GET_LIO(netdev);
  775. struct cavium_wq *time_wq = &lio->sync_octeon_time_wq;
  776. if (time_wq->wq) {
  777. cancel_delayed_work_sync(&time_wq->wk.work);
  778. destroy_workqueue(time_wq->wq);
  779. }
  780. }
  781. static struct octeon_device *get_other_octeon_device(struct octeon_device *oct)
  782. {
  783. struct octeon_device *other_oct;
  784. other_oct = lio_get_device(oct->octeon_id + 1);
  785. if (other_oct && other_oct->pci_dev) {
  786. int oct_busnum, other_oct_busnum;
  787. oct_busnum = oct->pci_dev->bus->number;
  788. other_oct_busnum = other_oct->pci_dev->bus->number;
  789. if (oct_busnum == other_oct_busnum) {
  790. int oct_slot, other_oct_slot;
  791. oct_slot = PCI_SLOT(oct->pci_dev->devfn);
  792. other_oct_slot = PCI_SLOT(other_oct->pci_dev->devfn);
  793. if (oct_slot == other_oct_slot)
  794. return other_oct;
  795. }
  796. }
  797. return NULL;
  798. }
  799. static void disable_all_vf_links(struct octeon_device *oct)
  800. {
  801. struct net_device *netdev;
  802. int max_vfs, vf, i;
  803. if (!oct)
  804. return;
  805. max_vfs = oct->sriov_info.max_vfs;
  806. for (i = 0; i < oct->ifcount; i++) {
  807. netdev = oct->props[i].netdev;
  808. if (!netdev)
  809. continue;
  810. for (vf = 0; vf < max_vfs; vf++)
  811. liquidio_set_vf_link_state(netdev, vf,
  812. IFLA_VF_LINK_STATE_DISABLE);
  813. }
  814. }
  815. static int liquidio_watchdog(void *param)
  816. {
  817. bool err_msg_was_printed[LIO_MAX_CORES];
  818. u16 mask_of_crashed_or_stuck_cores = 0;
  819. bool all_vf_links_are_disabled = false;
  820. struct octeon_device *oct = param;
  821. struct octeon_device *other_oct;
  822. #ifdef CONFIG_MODULE_UNLOAD
  823. long refcount, vfs_referencing_pf;
  824. u64 vfs_mask1, vfs_mask2;
  825. #endif
  826. int core;
  827. memset(err_msg_was_printed, 0, sizeof(err_msg_was_printed));
  828. while (!kthread_should_stop()) {
  829. /* sleep for a couple of seconds so that we don't hog the CPU */
  830. set_current_state(TASK_INTERRUPTIBLE);
  831. schedule_timeout(msecs_to_jiffies(2000));
  832. mask_of_crashed_or_stuck_cores =
  833. (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
  834. if (!mask_of_crashed_or_stuck_cores)
  835. continue;
  836. WRITE_ONCE(oct->cores_crashed, true);
  837. other_oct = get_other_octeon_device(oct);
  838. if (other_oct)
  839. WRITE_ONCE(other_oct->cores_crashed, true);
  840. for (core = 0; core < LIO_MAX_CORES; core++) {
  841. bool core_crashed_or_got_stuck;
  842. core_crashed_or_got_stuck =
  843. (mask_of_crashed_or_stuck_cores
  844. >> core) & 1;
  845. if (core_crashed_or_got_stuck &&
  846. !err_msg_was_printed[core]) {
  847. dev_err(&oct->pci_dev->dev,
  848. "ERROR: Octeon core %d crashed or got stuck! See oct-fwdump for details.\n",
  849. core);
  850. err_msg_was_printed[core] = true;
  851. }
  852. }
  853. if (all_vf_links_are_disabled)
  854. continue;
  855. disable_all_vf_links(oct);
  856. disable_all_vf_links(other_oct);
  857. all_vf_links_are_disabled = true;
  858. #ifdef CONFIG_MODULE_UNLOAD
  859. vfs_mask1 = READ_ONCE(oct->sriov_info.vf_drv_loaded_mask);
  860. vfs_mask2 = READ_ONCE(other_oct->sriov_info.vf_drv_loaded_mask);
  861. vfs_referencing_pf = hweight64(vfs_mask1);
  862. vfs_referencing_pf += hweight64(vfs_mask2);
  863. refcount = module_refcount(THIS_MODULE);
  864. if (refcount >= vfs_referencing_pf) {
  865. while (vfs_referencing_pf) {
  866. module_put(THIS_MODULE);
  867. vfs_referencing_pf--;
  868. }
  869. }
  870. #endif
  871. }
  872. return 0;
  873. }
  874. /**
  875. * \brief PCI probe handler
  876. * @param pdev PCI device structure
  877. * @param ent unused
  878. */
  879. static int
  880. liquidio_probe(struct pci_dev *pdev,
  881. const struct pci_device_id *ent __attribute__((unused)))
  882. {
  883. struct octeon_device *oct_dev = NULL;
  884. struct handshake *hs;
  885. oct_dev = octeon_allocate_device(pdev->device,
  886. sizeof(struct octeon_device_priv));
  887. if (!oct_dev) {
  888. dev_err(&pdev->dev, "Unable to allocate device\n");
  889. return -ENOMEM;
  890. }
  891. if (pdev->device == OCTEON_CN23XX_PF_VID)
  892. oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
  893. /* Enable PTP for 6XXX Device */
  894. if (((pdev->device == OCTEON_CN66XX) ||
  895. (pdev->device == OCTEON_CN68XX)))
  896. oct_dev->ptp_enable = true;
  897. else
  898. oct_dev->ptp_enable = false;
  899. dev_info(&pdev->dev, "Initializing device %x:%x.\n",
  900. (u32)pdev->vendor, (u32)pdev->device);
  901. /* Assign octeon_device for this device to the private data area. */
  902. pci_set_drvdata(pdev, oct_dev);
  903. /* set linux specific device pointer */
  904. oct_dev->pci_dev = (void *)pdev;
  905. hs = &handshake[oct_dev->octeon_id];
  906. init_completion(&hs->init);
  907. init_completion(&hs->started);
  908. hs->pci_dev = pdev;
  909. if (oct_dev->octeon_id == 0)
  910. /* first LiquidIO NIC is detected */
  911. complete(&first_stage);
  912. if (octeon_device_init(oct_dev)) {
  913. complete(&hs->init);
  914. liquidio_remove(pdev);
  915. return -ENOMEM;
  916. }
  917. if (OCTEON_CN23XX_PF(oct_dev)) {
  918. u8 bus, device, function;
  919. if (atomic_read(oct_dev->adapter_refcount) == 1) {
  920. /* Each NIC gets one watchdog kernel thread. The first
  921. * PF (of each NIC) that gets pci_driver->probe()'d
  922. * creates that thread.
  923. */
  924. bus = pdev->bus->number;
  925. device = PCI_SLOT(pdev->devfn);
  926. function = PCI_FUNC(pdev->devfn);
  927. oct_dev->watchdog_task = kthread_create(
  928. liquidio_watchdog, oct_dev,
  929. "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
  930. if (!IS_ERR(oct_dev->watchdog_task)) {
  931. wake_up_process(oct_dev->watchdog_task);
  932. } else {
  933. oct_dev->watchdog_task = NULL;
  934. dev_err(&oct_dev->pci_dev->dev,
  935. "failed to create kernel_thread\n");
  936. liquidio_remove(pdev);
  937. return -1;
  938. }
  939. }
  940. }
  941. oct_dev->rx_pause = 1;
  942. oct_dev->tx_pause = 1;
  943. dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
  944. return 0;
  945. }
  946. static bool fw_type_is_auto(void)
  947. {
  948. return strncmp(fw_type, LIO_FW_NAME_TYPE_AUTO,
  949. sizeof(LIO_FW_NAME_TYPE_AUTO)) == 0;
  950. }
  951. /**
  952. * \brief PCI FLR for each Octeon device.
  953. * @param oct octeon device
  954. */
  955. static void octeon_pci_flr(struct octeon_device *oct)
  956. {
  957. int rc;
  958. pci_save_state(oct->pci_dev);
  959. pci_cfg_access_lock(oct->pci_dev);
  960. /* Quiesce the device completely */
  961. pci_write_config_word(oct->pci_dev, PCI_COMMAND,
  962. PCI_COMMAND_INTX_DISABLE);
  963. rc = __pci_reset_function_locked(oct->pci_dev);
  964. if (rc != 0)
  965. dev_err(&oct->pci_dev->dev, "Error %d resetting PCI function %d\n",
  966. rc, oct->pf_num);
  967. pci_cfg_access_unlock(oct->pci_dev);
  968. pci_restore_state(oct->pci_dev);
  969. }
  970. /**
  971. *\brief Destroy resources associated with octeon device
  972. * @param pdev PCI device structure
  973. * @param ent unused
  974. */
  975. static void octeon_destroy_resources(struct octeon_device *oct)
  976. {
  977. int i, refcount;
  978. struct msix_entry *msix_entries;
  979. struct octeon_device_priv *oct_priv =
  980. (struct octeon_device_priv *)oct->priv;
  981. struct handshake *hs;
  982. switch (atomic_read(&oct->status)) {
  983. case OCT_DEV_RUNNING:
  984. case OCT_DEV_CORE_OK:
  985. /* No more instructions will be forwarded. */
  986. atomic_set(&oct->status, OCT_DEV_IN_RESET);
  987. oct->app_mode = CVM_DRV_INVALID_APP;
  988. dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
  989. lio_get_state_string(&oct->status));
  990. schedule_timeout_uninterruptible(HZ / 10);
  991. /* fallthrough */
  992. case OCT_DEV_HOST_OK:
  993. /* fallthrough */
  994. case OCT_DEV_CONSOLE_INIT_DONE:
  995. /* Remove any consoles */
  996. octeon_remove_consoles(oct);
  997. /* fallthrough */
  998. case OCT_DEV_IO_QUEUES_DONE:
  999. if (wait_for_pending_requests(oct))
  1000. dev_err(&oct->pci_dev->dev, "There were pending requests\n");
  1001. if (lio_wait_for_instr_fetch(oct))
  1002. dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
  1003. /* Disable the input and output queues now. No more packets will
  1004. * arrive from Octeon, but we should wait for all packet
  1005. * processing to finish.
  1006. */
  1007. oct->fn_list.disable_io_queues(oct);
  1008. if (lio_wait_for_oq_pkts(oct))
  1009. dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
  1010. /* fallthrough */
  1011. case OCT_DEV_INTR_SET_DONE:
  1012. /* Disable interrupts */
  1013. oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
  1014. if (oct->msix_on) {
  1015. msix_entries = (struct msix_entry *)oct->msix_entries;
  1016. for (i = 0; i < oct->num_msix_irqs - 1; i++) {
  1017. if (oct->ioq_vector[i].vector) {
  1018. /* clear the affinity_cpumask */
  1019. irq_set_affinity_hint(
  1020. msix_entries[i].vector,
  1021. NULL);
  1022. free_irq(msix_entries[i].vector,
  1023. &oct->ioq_vector[i]);
  1024. oct->ioq_vector[i].vector = 0;
  1025. }
  1026. }
  1027. /* non-iov vector's argument is oct struct */
  1028. free_irq(msix_entries[i].vector, oct);
  1029. pci_disable_msix(oct->pci_dev);
  1030. kfree(oct->msix_entries);
  1031. oct->msix_entries = NULL;
  1032. } else {
  1033. /* Release the interrupt line */
  1034. free_irq(oct->pci_dev->irq, oct);
  1035. if (oct->flags & LIO_FLAG_MSI_ENABLED)
  1036. pci_disable_msi(oct->pci_dev);
  1037. }
  1038. kfree(oct->irq_name_storage);
  1039. oct->irq_name_storage = NULL;
  1040. /* fallthrough */
  1041. case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
  1042. if (OCTEON_CN23XX_PF(oct))
  1043. octeon_free_ioq_vector(oct);
  1044. /* fallthrough */
  1045. case OCT_DEV_MBOX_SETUP_DONE:
  1046. if (OCTEON_CN23XX_PF(oct))
  1047. oct->fn_list.free_mbox(oct);
  1048. /* fallthrough */
  1049. case OCT_DEV_IN_RESET:
  1050. case OCT_DEV_DROQ_INIT_DONE:
  1051. /* Wait for any pending operations */
  1052. mdelay(100);
  1053. for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
  1054. if (!(oct->io_qmask.oq & BIT_ULL(i)))
  1055. continue;
  1056. octeon_delete_droq(oct, i);
  1057. }
  1058. /* Force any pending handshakes to complete */
  1059. for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
  1060. hs = &handshake[i];
  1061. if (hs->pci_dev) {
  1062. handshake[oct->octeon_id].init_ok = 0;
  1063. complete(&handshake[oct->octeon_id].init);
  1064. handshake[oct->octeon_id].started_ok = 0;
  1065. complete(&handshake[oct->octeon_id].started);
  1066. }
  1067. }
  1068. /* fallthrough */
  1069. case OCT_DEV_RESP_LIST_INIT_DONE:
  1070. octeon_delete_response_list(oct);
  1071. /* fallthrough */
  1072. case OCT_DEV_INSTR_QUEUE_INIT_DONE:
  1073. for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
  1074. if (!(oct->io_qmask.iq & BIT_ULL(i)))
  1075. continue;
  1076. octeon_delete_instr_queue(oct, i);
  1077. }
  1078. #ifdef CONFIG_PCI_IOV
  1079. if (oct->sriov_info.sriov_enabled)
  1080. pci_disable_sriov(oct->pci_dev);
  1081. #endif
  1082. /* fallthrough */
  1083. case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
  1084. octeon_free_sc_buffer_pool(oct);
  1085. /* fallthrough */
  1086. case OCT_DEV_DISPATCH_INIT_DONE:
  1087. octeon_delete_dispatch_list(oct);
  1088. cancel_delayed_work_sync(&oct->nic_poll_work.work);
  1089. /* fallthrough */
  1090. case OCT_DEV_PCI_MAP_DONE:
  1091. refcount = octeon_deregister_device(oct);
  1092. /* Soft reset the octeon device before exiting.
  1093. * However, if fw was loaded from card (i.e. autoboot),
  1094. * perform an FLR instead.
  1095. * Implementation note: only soft-reset the device
  1096. * if it is a CN6XXX OR the LAST CN23XX device.
  1097. */
  1098. if (atomic_read(oct->adapter_fw_state) == FW_IS_PRELOADED)
  1099. octeon_pci_flr(oct);
  1100. else if (OCTEON_CN6XXX(oct) || !refcount)
  1101. oct->fn_list.soft_reset(oct);
  1102. octeon_unmap_pci_barx(oct, 0);
  1103. octeon_unmap_pci_barx(oct, 1);
  1104. /* fallthrough */
  1105. case OCT_DEV_PCI_ENABLE_DONE:
  1106. pci_clear_master(oct->pci_dev);
  1107. /* Disable the device, releasing the PCI INT */
  1108. pci_disable_device(oct->pci_dev);
  1109. /* fallthrough */
  1110. case OCT_DEV_BEGIN_STATE:
  1111. /* Nothing to be done here either */
  1112. break;
  1113. } /* end switch (oct->status) */
  1114. tasklet_kill(&oct_priv->droq_tasklet);
  1115. }
  1116. /**
  1117. * \brief Callback for rx ctrl
  1118. * @param status status of request
  1119. * @param buf pointer to resp structure
  1120. */
  1121. static void rx_ctl_callback(struct octeon_device *oct,
  1122. u32 status,
  1123. void *buf)
  1124. {
  1125. struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
  1126. struct liquidio_rx_ctl_context *ctx;
  1127. ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
  1128. oct = lio_get_device(ctx->octeon_id);
  1129. if (status)
  1130. dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
  1131. CVM_CAST64(status));
  1132. WRITE_ONCE(ctx->cond, 1);
  1133. /* This barrier is required to be sure that the response has been
  1134. * written fully before waking up the handler
  1135. */
  1136. wmb();
  1137. wake_up_interruptible(&ctx->wc);
  1138. }
  1139. /**
  1140. * \brief Send Rx control command
  1141. * @param lio per-network private data
  1142. * @param start_stop whether to start or stop
  1143. */
  1144. static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
  1145. {
  1146. struct octeon_soft_command *sc;
  1147. struct liquidio_rx_ctl_context *ctx;
  1148. union octnet_cmd *ncmd;
  1149. int ctx_size = sizeof(struct liquidio_rx_ctl_context);
  1150. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  1151. int retval;
  1152. if (oct->props[lio->ifidx].rx_on == start_stop)
  1153. return;
  1154. sc = (struct octeon_soft_command *)
  1155. octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
  1156. 16, ctx_size);
  1157. ncmd = (union octnet_cmd *)sc->virtdptr;
  1158. ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
  1159. WRITE_ONCE(ctx->cond, 0);
  1160. ctx->octeon_id = lio_get_device_id(oct);
  1161. init_waitqueue_head(&ctx->wc);
  1162. ncmd->u64 = 0;
  1163. ncmd->s.cmd = OCTNET_CMD_RX_CTL;
  1164. ncmd->s.param1 = start_stop;
  1165. octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
  1166. sc->iq_no = lio->linfo.txpciq[0].s.q_no;
  1167. octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
  1168. OPCODE_NIC_CMD, 0, 0, 0);
  1169. sc->callback = rx_ctl_callback;
  1170. sc->callback_arg = sc;
  1171. sc->wait_time = 5000;
  1172. retval = octeon_send_soft_command(oct, sc);
  1173. if (retval == IQ_SEND_FAILED) {
  1174. netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
  1175. } else {
  1176. /* Sleep on a wait queue till the cond flag indicates that the
  1177. * response arrived or timed-out.
  1178. */
  1179. if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
  1180. return;
  1181. oct->props[lio->ifidx].rx_on = start_stop;
  1182. }
  1183. octeon_free_soft_command(oct, sc);
  1184. }
  1185. /**
  1186. * \brief Destroy NIC device interface
  1187. * @param oct octeon device
  1188. * @param ifidx which interface to destroy
  1189. *
  1190. * Cleanup associated with each interface for an Octeon device when NIC
  1191. * module is being unloaded or if initialization fails during load.
  1192. */
  1193. static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
  1194. {
  1195. struct net_device *netdev = oct->props[ifidx].netdev;
  1196. struct lio *lio;
  1197. struct napi_struct *napi, *n;
  1198. if (!netdev) {
  1199. dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
  1200. __func__, ifidx);
  1201. return;
  1202. }
  1203. lio = GET_LIO(netdev);
  1204. dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
  1205. if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
  1206. liquidio_stop(netdev);
  1207. if (oct->props[lio->ifidx].napi_enabled == 1) {
  1208. list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
  1209. napi_disable(napi);
  1210. oct->props[lio->ifidx].napi_enabled = 0;
  1211. if (OCTEON_CN23XX_PF(oct))
  1212. oct->droq[0]->ops.poll_mode = 0;
  1213. }
  1214. /* Delete NAPI */
  1215. list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
  1216. netif_napi_del(napi);
  1217. if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
  1218. unregister_netdev(netdev);
  1219. cleanup_sync_octeon_time_wq(netdev);
  1220. cleanup_link_status_change_wq(netdev);
  1221. cleanup_rx_oom_poll_fn(netdev);
  1222. delete_glists(lio);
  1223. free_netdev(netdev);
  1224. oct->props[ifidx].gmxport = -1;
  1225. oct->props[ifidx].netdev = NULL;
  1226. }
  1227. /**
  1228. * \brief Stop complete NIC functionality
  1229. * @param oct octeon device
  1230. */
  1231. static int liquidio_stop_nic_module(struct octeon_device *oct)
  1232. {
  1233. int i, j;
  1234. struct lio *lio;
  1235. dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
  1236. if (!oct->ifcount) {
  1237. dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
  1238. return 1;
  1239. }
  1240. spin_lock_bh(&oct->cmd_resp_wqlock);
  1241. oct->cmd_resp_state = OCT_DRV_OFFLINE;
  1242. spin_unlock_bh(&oct->cmd_resp_wqlock);
  1243. lio_vf_rep_destroy(oct);
  1244. for (i = 0; i < oct->ifcount; i++) {
  1245. lio = GET_LIO(oct->props[i].netdev);
  1246. for (j = 0; j < oct->num_oqs; j++)
  1247. octeon_unregister_droq_ops(oct,
  1248. lio->linfo.rxpciq[j].s.q_no);
  1249. }
  1250. for (i = 0; i < oct->ifcount; i++)
  1251. liquidio_destroy_nic_device(oct, i);
  1252. if (oct->devlink) {
  1253. devlink_unregister(oct->devlink);
  1254. devlink_free(oct->devlink);
  1255. oct->devlink = NULL;
  1256. }
  1257. dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
  1258. return 0;
  1259. }
  1260. /**
  1261. * \brief Cleans up resources at unload time
  1262. * @param pdev PCI device structure
  1263. */
  1264. static void liquidio_remove(struct pci_dev *pdev)
  1265. {
  1266. struct octeon_device *oct_dev = pci_get_drvdata(pdev);
  1267. dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
  1268. if (oct_dev->watchdog_task)
  1269. kthread_stop(oct_dev->watchdog_task);
  1270. if (!oct_dev->octeon_id &&
  1271. oct_dev->fw_info.app_cap_flags & LIQUIDIO_SWITCHDEV_CAP)
  1272. lio_vf_rep_modexit();
  1273. if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
  1274. liquidio_stop_nic_module(oct_dev);
  1275. /* Reset the octeon device and cleanup all memory allocated for
  1276. * the octeon device by driver.
  1277. */
  1278. octeon_destroy_resources(oct_dev);
  1279. dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
  1280. /* This octeon device has been removed. Update the global
  1281. * data structure to reflect this. Free the device structure.
  1282. */
  1283. octeon_free_device_mem(oct_dev);
  1284. }
  1285. /**
  1286. * \brief Identify the Octeon device and to map the BAR address space
  1287. * @param oct octeon device
  1288. */
  1289. static int octeon_chip_specific_setup(struct octeon_device *oct)
  1290. {
  1291. u32 dev_id, rev_id;
  1292. int ret = 1;
  1293. char *s;
  1294. pci_read_config_dword(oct->pci_dev, 0, &dev_id);
  1295. pci_read_config_dword(oct->pci_dev, 8, &rev_id);
  1296. oct->rev_id = rev_id & 0xff;
  1297. switch (dev_id) {
  1298. case OCTEON_CN68XX_PCIID:
  1299. oct->chip_id = OCTEON_CN68XX;
  1300. ret = lio_setup_cn68xx_octeon_device(oct);
  1301. s = "CN68XX";
  1302. break;
  1303. case OCTEON_CN66XX_PCIID:
  1304. oct->chip_id = OCTEON_CN66XX;
  1305. ret = lio_setup_cn66xx_octeon_device(oct);
  1306. s = "CN66XX";
  1307. break;
  1308. case OCTEON_CN23XX_PCIID_PF:
  1309. oct->chip_id = OCTEON_CN23XX_PF_VID;
  1310. ret = setup_cn23xx_octeon_pf_device(oct);
  1311. if (ret)
  1312. break;
  1313. #ifdef CONFIG_PCI_IOV
  1314. if (!ret)
  1315. pci_sriov_set_totalvfs(oct->pci_dev,
  1316. oct->sriov_info.max_vfs);
  1317. #endif
  1318. s = "CN23XX";
  1319. break;
  1320. default:
  1321. s = "?";
  1322. dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
  1323. dev_id);
  1324. }
  1325. if (!ret)
  1326. dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
  1327. OCTEON_MAJOR_REV(oct),
  1328. OCTEON_MINOR_REV(oct),
  1329. octeon_get_conf(oct)->card_name,
  1330. LIQUIDIO_VERSION);
  1331. return ret;
  1332. }
  1333. /**
  1334. * \brief PCI initialization for each Octeon device.
  1335. * @param oct octeon device
  1336. */
  1337. static int octeon_pci_os_setup(struct octeon_device *oct)
  1338. {
  1339. /* setup PCI stuff first */
  1340. if (pci_enable_device(oct->pci_dev)) {
  1341. dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
  1342. return 1;
  1343. }
  1344. if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
  1345. dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
  1346. pci_disable_device(oct->pci_dev);
  1347. return 1;
  1348. }
  1349. /* Enable PCI DMA Master. */
  1350. pci_set_master(oct->pci_dev);
  1351. return 0;
  1352. }
  1353. /**
  1354. * \brief Unmap and free network buffer
  1355. * @param buf buffer
  1356. */
  1357. static void free_netbuf(void *buf)
  1358. {
  1359. struct sk_buff *skb;
  1360. struct octnet_buf_free_info *finfo;
  1361. struct lio *lio;
  1362. finfo = (struct octnet_buf_free_info *)buf;
  1363. skb = finfo->skb;
  1364. lio = finfo->lio;
  1365. dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
  1366. DMA_TO_DEVICE);
  1367. tx_buffer_free(skb);
  1368. }
  1369. /**
  1370. * \brief Unmap and free gather buffer
  1371. * @param buf buffer
  1372. */
  1373. static void free_netsgbuf(void *buf)
  1374. {
  1375. struct octnet_buf_free_info *finfo;
  1376. struct sk_buff *skb;
  1377. struct lio *lio;
  1378. struct octnic_gather *g;
  1379. int i, frags, iq;
  1380. finfo = (struct octnet_buf_free_info *)buf;
  1381. skb = finfo->skb;
  1382. lio = finfo->lio;
  1383. g = finfo->g;
  1384. frags = skb_shinfo(skb)->nr_frags;
  1385. dma_unmap_single(&lio->oct_dev->pci_dev->dev,
  1386. g->sg[0].ptr[0], (skb->len - skb->data_len),
  1387. DMA_TO_DEVICE);
  1388. i = 1;
  1389. while (frags--) {
  1390. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
  1391. pci_unmap_page((lio->oct_dev)->pci_dev,
  1392. g->sg[(i >> 2)].ptr[(i & 3)],
  1393. frag->size, DMA_TO_DEVICE);
  1394. i++;
  1395. }
  1396. iq = skb_iq(lio, skb);
  1397. spin_lock(&lio->glist_lock[iq]);
  1398. list_add_tail(&g->list, &lio->glist[iq]);
  1399. spin_unlock(&lio->glist_lock[iq]);
  1400. tx_buffer_free(skb);
  1401. }
  1402. /**
  1403. * \brief Unmap and free gather buffer with response
  1404. * @param buf buffer
  1405. */
  1406. static void free_netsgbuf_with_resp(void *buf)
  1407. {
  1408. struct octeon_soft_command *sc;
  1409. struct octnet_buf_free_info *finfo;
  1410. struct sk_buff *skb;
  1411. struct lio *lio;
  1412. struct octnic_gather *g;
  1413. int i, frags, iq;
  1414. sc = (struct octeon_soft_command *)buf;
  1415. skb = (struct sk_buff *)sc->callback_arg;
  1416. finfo = (struct octnet_buf_free_info *)&skb->cb;
  1417. lio = finfo->lio;
  1418. g = finfo->g;
  1419. frags = skb_shinfo(skb)->nr_frags;
  1420. dma_unmap_single(&lio->oct_dev->pci_dev->dev,
  1421. g->sg[0].ptr[0], (skb->len - skb->data_len),
  1422. DMA_TO_DEVICE);
  1423. i = 1;
  1424. while (frags--) {
  1425. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
  1426. pci_unmap_page((lio->oct_dev)->pci_dev,
  1427. g->sg[(i >> 2)].ptr[(i & 3)],
  1428. frag->size, DMA_TO_DEVICE);
  1429. i++;
  1430. }
  1431. iq = skb_iq(lio, skb);
  1432. spin_lock(&lio->glist_lock[iq]);
  1433. list_add_tail(&g->list, &lio->glist[iq]);
  1434. spin_unlock(&lio->glist_lock[iq]);
  1435. /* Don't free the skb yet */
  1436. }
  1437. /**
  1438. * \brief Adjust ptp frequency
  1439. * @param ptp PTP clock info
  1440. * @param ppb how much to adjust by, in parts-per-billion
  1441. */
  1442. static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  1443. {
  1444. struct lio *lio = container_of(ptp, struct lio, ptp_info);
  1445. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  1446. u64 comp, delta;
  1447. unsigned long flags;
  1448. bool neg_adj = false;
  1449. if (ppb < 0) {
  1450. neg_adj = true;
  1451. ppb = -ppb;
  1452. }
  1453. /* The hardware adds the clock compensation value to the
  1454. * PTP clock on every coprocessor clock cycle, so we
  1455. * compute the delta in terms of coprocessor clocks.
  1456. */
  1457. delta = (u64)ppb << 32;
  1458. do_div(delta, oct->coproc_clock_rate);
  1459. spin_lock_irqsave(&lio->ptp_lock, flags);
  1460. comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
  1461. if (neg_adj)
  1462. comp -= delta;
  1463. else
  1464. comp += delta;
  1465. lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
  1466. spin_unlock_irqrestore(&lio->ptp_lock, flags);
  1467. return 0;
  1468. }
  1469. /**
  1470. * \brief Adjust ptp time
  1471. * @param ptp PTP clock info
  1472. * @param delta how much to adjust by, in nanosecs
  1473. */
  1474. static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  1475. {
  1476. unsigned long flags;
  1477. struct lio *lio = container_of(ptp, struct lio, ptp_info);
  1478. spin_lock_irqsave(&lio->ptp_lock, flags);
  1479. lio->ptp_adjust += delta;
  1480. spin_unlock_irqrestore(&lio->ptp_lock, flags);
  1481. return 0;
  1482. }
  1483. /**
  1484. * \brief Get hardware clock time, including any adjustment
  1485. * @param ptp PTP clock info
  1486. * @param ts timespec
  1487. */
  1488. static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
  1489. struct timespec64 *ts)
  1490. {
  1491. u64 ns;
  1492. unsigned long flags;
  1493. struct lio *lio = container_of(ptp, struct lio, ptp_info);
  1494. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  1495. spin_lock_irqsave(&lio->ptp_lock, flags);
  1496. ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
  1497. ns += lio->ptp_adjust;
  1498. spin_unlock_irqrestore(&lio->ptp_lock, flags);
  1499. *ts = ns_to_timespec64(ns);
  1500. return 0;
  1501. }
  1502. /**
  1503. * \brief Set hardware clock time. Reset adjustment
  1504. * @param ptp PTP clock info
  1505. * @param ts timespec
  1506. */
  1507. static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
  1508. const struct timespec64 *ts)
  1509. {
  1510. u64 ns;
  1511. unsigned long flags;
  1512. struct lio *lio = container_of(ptp, struct lio, ptp_info);
  1513. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  1514. ns = timespec64_to_ns(ts);
  1515. spin_lock_irqsave(&lio->ptp_lock, flags);
  1516. lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
  1517. lio->ptp_adjust = 0;
  1518. spin_unlock_irqrestore(&lio->ptp_lock, flags);
  1519. return 0;
  1520. }
  1521. /**
  1522. * \brief Check if PTP is enabled
  1523. * @param ptp PTP clock info
  1524. * @param rq request
  1525. * @param on is it on
  1526. */
  1527. static int
  1528. liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
  1529. struct ptp_clock_request *rq __attribute__((unused)),
  1530. int on __attribute__((unused)))
  1531. {
  1532. return -EOPNOTSUPP;
  1533. }
  1534. /**
  1535. * \brief Open PTP clock source
  1536. * @param netdev network device
  1537. */
  1538. static void oct_ptp_open(struct net_device *netdev)
  1539. {
  1540. struct lio *lio = GET_LIO(netdev);
  1541. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  1542. spin_lock_init(&lio->ptp_lock);
  1543. snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
  1544. lio->ptp_info.owner = THIS_MODULE;
  1545. lio->ptp_info.max_adj = 250000000;
  1546. lio->ptp_info.n_alarm = 0;
  1547. lio->ptp_info.n_ext_ts = 0;
  1548. lio->ptp_info.n_per_out = 0;
  1549. lio->ptp_info.pps = 0;
  1550. lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
  1551. lio->ptp_info.adjtime = liquidio_ptp_adjtime;
  1552. lio->ptp_info.gettime64 = liquidio_ptp_gettime;
  1553. lio->ptp_info.settime64 = liquidio_ptp_settime;
  1554. lio->ptp_info.enable = liquidio_ptp_enable;
  1555. lio->ptp_adjust = 0;
  1556. lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
  1557. &oct->pci_dev->dev);
  1558. if (IS_ERR(lio->ptp_clock))
  1559. lio->ptp_clock = NULL;
  1560. }
  1561. /**
  1562. * \brief Init PTP clock
  1563. * @param oct octeon device
  1564. */
  1565. static void liquidio_ptp_init(struct octeon_device *oct)
  1566. {
  1567. u64 clock_comp, cfg;
  1568. clock_comp = (u64)NSEC_PER_SEC << 32;
  1569. do_div(clock_comp, oct->coproc_clock_rate);
  1570. lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
  1571. /* Enable */
  1572. cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
  1573. lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
  1574. }
  1575. /**
  1576. * \brief Load firmware to device
  1577. * @param oct octeon device
  1578. *
  1579. * Maps device to firmware filename, requests firmware, and downloads it
  1580. */
  1581. static int load_firmware(struct octeon_device *oct)
  1582. {
  1583. int ret = 0;
  1584. const struct firmware *fw;
  1585. char fw_name[LIO_MAX_FW_FILENAME_LEN];
  1586. char *tmp_fw_type;
  1587. if (fw_type_is_auto()) {
  1588. tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
  1589. strncpy(fw_type, tmp_fw_type, sizeof(fw_type));
  1590. } else {
  1591. tmp_fw_type = fw_type;
  1592. }
  1593. sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
  1594. octeon_get_conf(oct)->card_name, tmp_fw_type,
  1595. LIO_FW_NAME_SUFFIX);
  1596. ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
  1597. if (ret) {
  1598. dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
  1599. fw_name);
  1600. release_firmware(fw);
  1601. return ret;
  1602. }
  1603. ret = octeon_download_firmware(oct, fw->data, fw->size);
  1604. release_firmware(fw);
  1605. return ret;
  1606. }
  1607. /**
  1608. * \brief Callback for getting interface configuration
  1609. * @param status status of request
  1610. * @param buf pointer to resp structure
  1611. */
  1612. static void if_cfg_callback(struct octeon_device *oct,
  1613. u32 status __attribute__((unused)),
  1614. void *buf)
  1615. {
  1616. struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
  1617. struct liquidio_if_cfg_resp *resp;
  1618. struct liquidio_if_cfg_context *ctx;
  1619. resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
  1620. ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
  1621. oct = lio_get_device(ctx->octeon_id);
  1622. if (resp->status)
  1623. dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: 0x%llx (0x%08x)\n",
  1624. CVM_CAST64(resp->status), status);
  1625. WRITE_ONCE(ctx->cond, 1);
  1626. snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
  1627. resp->cfg_info.liquidio_firmware_version);
  1628. /* This barrier is required to be sure that the response has been
  1629. * written fully before waking up the handler
  1630. */
  1631. wmb();
  1632. wake_up_interruptible(&ctx->wc);
  1633. }
  1634. /**
  1635. * \brief Poll routine for checking transmit queue status
  1636. * @param work work_struct data structure
  1637. */
  1638. static void octnet_poll_check_txq_status(struct work_struct *work)
  1639. {
  1640. struct cavium_wk *wk = (struct cavium_wk *)work;
  1641. struct lio *lio = (struct lio *)wk->ctxptr;
  1642. if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
  1643. return;
  1644. check_txq_status(lio);
  1645. queue_delayed_work(lio->txq_status_wq.wq,
  1646. &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
  1647. }
  1648. /**
  1649. * \brief Sets up the txq poll check
  1650. * @param netdev network device
  1651. */
  1652. static inline int setup_tx_poll_fn(struct net_device *netdev)
  1653. {
  1654. struct lio *lio = GET_LIO(netdev);
  1655. struct octeon_device *oct = lio->oct_dev;
  1656. lio->txq_status_wq.wq = alloc_workqueue("txq-status",
  1657. WQ_MEM_RECLAIM, 0);
  1658. if (!lio->txq_status_wq.wq) {
  1659. dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
  1660. return -1;
  1661. }
  1662. INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
  1663. octnet_poll_check_txq_status);
  1664. lio->txq_status_wq.wk.ctxptr = lio;
  1665. queue_delayed_work(lio->txq_status_wq.wq,
  1666. &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
  1667. return 0;
  1668. }
  1669. static inline void cleanup_tx_poll_fn(struct net_device *netdev)
  1670. {
  1671. struct lio *lio = GET_LIO(netdev);
  1672. if (lio->txq_status_wq.wq) {
  1673. cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
  1674. destroy_workqueue(lio->txq_status_wq.wq);
  1675. }
  1676. }
  1677. /**
  1678. * \brief Net device open for LiquidIO
  1679. * @param netdev network device
  1680. */
  1681. static int liquidio_open(struct net_device *netdev)
  1682. {
  1683. struct lio *lio = GET_LIO(netdev);
  1684. struct octeon_device *oct = lio->oct_dev;
  1685. struct napi_struct *napi, *n;
  1686. if (oct->props[lio->ifidx].napi_enabled == 0) {
  1687. list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
  1688. napi_enable(napi);
  1689. oct->props[lio->ifidx].napi_enabled = 1;
  1690. if (OCTEON_CN23XX_PF(oct))
  1691. oct->droq[0]->ops.poll_mode = 1;
  1692. }
  1693. if (oct->ptp_enable)
  1694. oct_ptp_open(netdev);
  1695. ifstate_set(lio, LIO_IFSTATE_RUNNING);
  1696. /* Ready for link status updates */
  1697. lio->intf_open = 1;
  1698. netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
  1699. if (OCTEON_CN23XX_PF(oct)) {
  1700. if (!oct->msix_on)
  1701. if (setup_tx_poll_fn(netdev))
  1702. return -1;
  1703. } else {
  1704. if (setup_tx_poll_fn(netdev))
  1705. return -1;
  1706. }
  1707. start_txqs(netdev);
  1708. /* tell Octeon to start forwarding packets to host */
  1709. send_rx_ctrl_cmd(lio, 1);
  1710. dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
  1711. netdev->name);
  1712. return 0;
  1713. }
  1714. /**
  1715. * \brief Net device stop for LiquidIO
  1716. * @param netdev network device
  1717. */
  1718. static int liquidio_stop(struct net_device *netdev)
  1719. {
  1720. struct lio *lio = GET_LIO(netdev);
  1721. struct octeon_device *oct = lio->oct_dev;
  1722. struct napi_struct *napi, *n;
  1723. ifstate_reset(lio, LIO_IFSTATE_RUNNING);
  1724. netif_tx_disable(netdev);
  1725. /* Inform that netif carrier is down */
  1726. netif_carrier_off(netdev);
  1727. lio->intf_open = 0;
  1728. lio->linfo.link.s.link_up = 0;
  1729. lio->link_changes++;
  1730. /* Tell Octeon that nic interface is down. */
  1731. send_rx_ctrl_cmd(lio, 0);
  1732. if (OCTEON_CN23XX_PF(oct)) {
  1733. if (!oct->msix_on)
  1734. cleanup_tx_poll_fn(netdev);
  1735. } else {
  1736. cleanup_tx_poll_fn(netdev);
  1737. }
  1738. if (lio->ptp_clock) {
  1739. ptp_clock_unregister(lio->ptp_clock);
  1740. lio->ptp_clock = NULL;
  1741. }
  1742. /* Wait for any pending Rx descriptors */
  1743. if (lio_wait_for_clean_oq(oct))
  1744. netif_info(lio, rx_err, lio->netdev,
  1745. "Proceeding with stop interface after partial RX desc processing\n");
  1746. if (oct->props[lio->ifidx].napi_enabled == 1) {
  1747. list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
  1748. napi_disable(napi);
  1749. oct->props[lio->ifidx].napi_enabled = 0;
  1750. if (OCTEON_CN23XX_PF(oct))
  1751. oct->droq[0]->ops.poll_mode = 0;
  1752. }
  1753. dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
  1754. return 0;
  1755. }
  1756. /**
  1757. * \brief Converts a mask based on net device flags
  1758. * @param netdev network device
  1759. *
  1760. * This routine generates a octnet_ifflags mask from the net device flags
  1761. * received from the OS.
  1762. */
  1763. static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
  1764. {
  1765. enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
  1766. if (netdev->flags & IFF_PROMISC)
  1767. f |= OCTNET_IFFLAG_PROMISC;
  1768. if (netdev->flags & IFF_ALLMULTI)
  1769. f |= OCTNET_IFFLAG_ALLMULTI;
  1770. if (netdev->flags & IFF_MULTICAST) {
  1771. f |= OCTNET_IFFLAG_MULTICAST;
  1772. /* Accept all multicast addresses if there are more than we
  1773. * can handle
  1774. */
  1775. if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
  1776. f |= OCTNET_IFFLAG_ALLMULTI;
  1777. }
  1778. if (netdev->flags & IFF_BROADCAST)
  1779. f |= OCTNET_IFFLAG_BROADCAST;
  1780. return f;
  1781. }
  1782. /**
  1783. * \brief Net device set_multicast_list
  1784. * @param netdev network device
  1785. */
  1786. static void liquidio_set_mcast_list(struct net_device *netdev)
  1787. {
  1788. struct lio *lio = GET_LIO(netdev);
  1789. struct octeon_device *oct = lio->oct_dev;
  1790. struct octnic_ctrl_pkt nctrl;
  1791. struct netdev_hw_addr *ha;
  1792. u64 *mc;
  1793. int ret;
  1794. int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
  1795. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  1796. /* Create a ctrl pkt command to be sent to core app. */
  1797. nctrl.ncmd.u64 = 0;
  1798. nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
  1799. nctrl.ncmd.s.param1 = get_new_flags(netdev);
  1800. nctrl.ncmd.s.param2 = mc_count;
  1801. nctrl.ncmd.s.more = mc_count;
  1802. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  1803. nctrl.netpndev = (u64)netdev;
  1804. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  1805. /* copy all the addresses into the udd */
  1806. mc = &nctrl.udd[0];
  1807. netdev_for_each_mc_addr(ha, netdev) {
  1808. *mc = 0;
  1809. memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
  1810. /* no need to swap bytes */
  1811. if (++mc > &nctrl.udd[mc_count])
  1812. break;
  1813. }
  1814. /* Apparently, any activity in this call from the kernel has to
  1815. * be atomic. So we won't wait for response.
  1816. */
  1817. nctrl.wait_time = 0;
  1818. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  1819. if (ret < 0) {
  1820. dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
  1821. ret);
  1822. }
  1823. }
  1824. /**
  1825. * \brief Net device set_mac_address
  1826. * @param netdev network device
  1827. */
  1828. static int liquidio_set_mac(struct net_device *netdev, void *p)
  1829. {
  1830. int ret = 0;
  1831. struct lio *lio = GET_LIO(netdev);
  1832. struct octeon_device *oct = lio->oct_dev;
  1833. struct sockaddr *addr = (struct sockaddr *)p;
  1834. struct octnic_ctrl_pkt nctrl;
  1835. if (!is_valid_ether_addr(addr->sa_data))
  1836. return -EADDRNOTAVAIL;
  1837. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  1838. nctrl.ncmd.u64 = 0;
  1839. nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
  1840. nctrl.ncmd.s.param1 = 0;
  1841. nctrl.ncmd.s.more = 1;
  1842. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  1843. nctrl.netpndev = (u64)netdev;
  1844. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  1845. nctrl.wait_time = 100;
  1846. nctrl.udd[0] = 0;
  1847. /* The MAC Address is presented in network byte order. */
  1848. memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
  1849. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  1850. if (ret < 0) {
  1851. dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
  1852. return -ENOMEM;
  1853. }
  1854. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1855. memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
  1856. return 0;
  1857. }
  1858. /**
  1859. * \brief Net device get_stats
  1860. * @param netdev network device
  1861. */
  1862. static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
  1863. {
  1864. struct lio *lio = GET_LIO(netdev);
  1865. struct net_device_stats *stats = &netdev->stats;
  1866. struct octeon_device *oct;
  1867. u64 pkts = 0, drop = 0, bytes = 0;
  1868. struct oct_droq_stats *oq_stats;
  1869. struct oct_iq_stats *iq_stats;
  1870. int i, iq_no, oq_no;
  1871. oct = lio->oct_dev;
  1872. if (ifstate_check(lio, LIO_IFSTATE_RESETTING))
  1873. return stats;
  1874. for (i = 0; i < oct->num_iqs; i++) {
  1875. iq_no = lio->linfo.txpciq[i].s.q_no;
  1876. iq_stats = &oct->instr_queue[iq_no]->stats;
  1877. pkts += iq_stats->tx_done;
  1878. drop += iq_stats->tx_dropped;
  1879. bytes += iq_stats->tx_tot_bytes;
  1880. }
  1881. stats->tx_packets = pkts;
  1882. stats->tx_bytes = bytes;
  1883. stats->tx_dropped = drop;
  1884. pkts = 0;
  1885. drop = 0;
  1886. bytes = 0;
  1887. for (i = 0; i < oct->num_oqs; i++) {
  1888. oq_no = lio->linfo.rxpciq[i].s.q_no;
  1889. oq_stats = &oct->droq[oq_no]->stats;
  1890. pkts += oq_stats->rx_pkts_received;
  1891. drop += (oq_stats->rx_dropped +
  1892. oq_stats->dropped_nodispatch +
  1893. oq_stats->dropped_toomany +
  1894. oq_stats->dropped_nomem);
  1895. bytes += oq_stats->rx_bytes_received;
  1896. }
  1897. stats->rx_bytes = bytes;
  1898. stats->rx_packets = pkts;
  1899. stats->rx_dropped = drop;
  1900. return stats;
  1901. }
  1902. /**
  1903. * \brief Handler for SIOCSHWTSTAMP ioctl
  1904. * @param netdev network device
  1905. * @param ifr interface request
  1906. * @param cmd command
  1907. */
  1908. static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
  1909. {
  1910. struct hwtstamp_config conf;
  1911. struct lio *lio = GET_LIO(netdev);
  1912. if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
  1913. return -EFAULT;
  1914. if (conf.flags)
  1915. return -EINVAL;
  1916. switch (conf.tx_type) {
  1917. case HWTSTAMP_TX_ON:
  1918. case HWTSTAMP_TX_OFF:
  1919. break;
  1920. default:
  1921. return -ERANGE;
  1922. }
  1923. switch (conf.rx_filter) {
  1924. case HWTSTAMP_FILTER_NONE:
  1925. break;
  1926. case HWTSTAMP_FILTER_ALL:
  1927. case HWTSTAMP_FILTER_SOME:
  1928. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  1929. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  1930. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  1931. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1932. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1933. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1934. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1935. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1936. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1937. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1938. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1939. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1940. case HWTSTAMP_FILTER_NTP_ALL:
  1941. conf.rx_filter = HWTSTAMP_FILTER_ALL;
  1942. break;
  1943. default:
  1944. return -ERANGE;
  1945. }
  1946. if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
  1947. ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
  1948. else
  1949. ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
  1950. return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
  1951. }
  1952. /**
  1953. * \brief ioctl handler
  1954. * @param netdev network device
  1955. * @param ifr interface request
  1956. * @param cmd command
  1957. */
  1958. static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1959. {
  1960. struct lio *lio = GET_LIO(netdev);
  1961. switch (cmd) {
  1962. case SIOCSHWTSTAMP:
  1963. if (lio->oct_dev->ptp_enable)
  1964. return hwtstamp_ioctl(netdev, ifr);
  1965. default:
  1966. return -EOPNOTSUPP;
  1967. }
  1968. }
  1969. /**
  1970. * \brief handle a Tx timestamp response
  1971. * @param status response status
  1972. * @param buf pointer to skb
  1973. */
  1974. static void handle_timestamp(struct octeon_device *oct,
  1975. u32 status,
  1976. void *buf)
  1977. {
  1978. struct octnet_buf_free_info *finfo;
  1979. struct octeon_soft_command *sc;
  1980. struct oct_timestamp_resp *resp;
  1981. struct lio *lio;
  1982. struct sk_buff *skb = (struct sk_buff *)buf;
  1983. finfo = (struct octnet_buf_free_info *)skb->cb;
  1984. lio = finfo->lio;
  1985. sc = finfo->sc;
  1986. oct = lio->oct_dev;
  1987. resp = (struct oct_timestamp_resp *)sc->virtrptr;
  1988. if (status != OCTEON_REQUEST_DONE) {
  1989. dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
  1990. CVM_CAST64(status));
  1991. resp->timestamp = 0;
  1992. }
  1993. octeon_swap_8B_data(&resp->timestamp, 1);
  1994. if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
  1995. struct skb_shared_hwtstamps ts;
  1996. u64 ns = resp->timestamp;
  1997. netif_info(lio, tx_done, lio->netdev,
  1998. "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
  1999. skb, (unsigned long long)ns);
  2000. ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
  2001. skb_tstamp_tx(skb, &ts);
  2002. }
  2003. octeon_free_soft_command(oct, sc);
  2004. tx_buffer_free(skb);
  2005. }
  2006. /* \brief Send a data packet that will be timestamped
  2007. * @param oct octeon device
  2008. * @param ndata pointer to network data
  2009. * @param finfo pointer to private network data
  2010. */
  2011. static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
  2012. struct octnic_data_pkt *ndata,
  2013. struct octnet_buf_free_info *finfo,
  2014. int xmit_more)
  2015. {
  2016. int retval;
  2017. struct octeon_soft_command *sc;
  2018. struct lio *lio;
  2019. int ring_doorbell;
  2020. u32 len;
  2021. lio = finfo->lio;
  2022. sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
  2023. sizeof(struct oct_timestamp_resp));
  2024. finfo->sc = sc;
  2025. if (!sc) {
  2026. dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
  2027. return IQ_SEND_FAILED;
  2028. }
  2029. if (ndata->reqtype == REQTYPE_NORESP_NET)
  2030. ndata->reqtype = REQTYPE_RESP_NET;
  2031. else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
  2032. ndata->reqtype = REQTYPE_RESP_NET_SG;
  2033. sc->callback = handle_timestamp;
  2034. sc->callback_arg = finfo->skb;
  2035. sc->iq_no = ndata->q_no;
  2036. if (OCTEON_CN23XX_PF(oct))
  2037. len = (u32)((struct octeon_instr_ih3 *)
  2038. (&sc->cmd.cmd3.ih3))->dlengsz;
  2039. else
  2040. len = (u32)((struct octeon_instr_ih2 *)
  2041. (&sc->cmd.cmd2.ih2))->dlengsz;
  2042. ring_doorbell = !xmit_more;
  2043. retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
  2044. sc, len, ndata->reqtype);
  2045. if (retval == IQ_SEND_FAILED) {
  2046. dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
  2047. retval);
  2048. octeon_free_soft_command(oct, sc);
  2049. } else {
  2050. netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
  2051. }
  2052. return retval;
  2053. }
  2054. /** \brief Transmit networks packets to the Octeon interface
  2055. * @param skbuff skbuff struct to be passed to network layer.
  2056. * @param netdev pointer to network device
  2057. * @returns whether the packet was transmitted to the device okay or not
  2058. * (NETDEV_TX_OK or NETDEV_TX_BUSY)
  2059. */
  2060. static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
  2061. {
  2062. struct lio *lio;
  2063. struct octnet_buf_free_info *finfo;
  2064. union octnic_cmd_setup cmdsetup;
  2065. struct octnic_data_pkt ndata;
  2066. struct octeon_device *oct;
  2067. struct oct_iq_stats *stats;
  2068. struct octeon_instr_irh *irh;
  2069. union tx_info *tx_info;
  2070. int status = 0;
  2071. int q_idx = 0, iq_no = 0;
  2072. int j, xmit_more = 0;
  2073. u64 dptr = 0;
  2074. u32 tag = 0;
  2075. lio = GET_LIO(netdev);
  2076. oct = lio->oct_dev;
  2077. q_idx = skb_iq(lio, skb);
  2078. tag = q_idx;
  2079. iq_no = lio->linfo.txpciq[q_idx].s.q_no;
  2080. stats = &oct->instr_queue[iq_no]->stats;
  2081. /* Check for all conditions in which the current packet cannot be
  2082. * transmitted.
  2083. */
  2084. if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
  2085. (!lio->linfo.link.s.link_up) ||
  2086. (skb->len <= 0)) {
  2087. netif_info(lio, tx_err, lio->netdev,
  2088. "Transmit failed link_status : %d\n",
  2089. lio->linfo.link.s.link_up);
  2090. goto lio_xmit_failed;
  2091. }
  2092. /* Use space in skb->cb to store info used to unmap and
  2093. * free the buffers.
  2094. */
  2095. finfo = (struct octnet_buf_free_info *)skb->cb;
  2096. finfo->lio = lio;
  2097. finfo->skb = skb;
  2098. finfo->sc = NULL;
  2099. /* Prepare the attributes for the data to be passed to OSI. */
  2100. memset(&ndata, 0, sizeof(struct octnic_data_pkt));
  2101. ndata.buf = (void *)finfo;
  2102. ndata.q_no = iq_no;
  2103. if (octnet_iq_is_full(oct, ndata.q_no)) {
  2104. /* defer sending if queue is full */
  2105. netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
  2106. ndata.q_no);
  2107. stats->tx_iq_busy++;
  2108. return NETDEV_TX_BUSY;
  2109. }
  2110. /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
  2111. * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
  2112. */
  2113. ndata.datasize = skb->len;
  2114. cmdsetup.u64 = 0;
  2115. cmdsetup.s.iq_no = iq_no;
  2116. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2117. if (skb->encapsulation) {
  2118. cmdsetup.s.tnl_csum = 1;
  2119. stats->tx_vxlan++;
  2120. } else {
  2121. cmdsetup.s.transport_csum = 1;
  2122. }
  2123. }
  2124. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
  2125. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  2126. cmdsetup.s.timestamp = 1;
  2127. }
  2128. if (skb_shinfo(skb)->nr_frags == 0) {
  2129. cmdsetup.s.u.datasize = skb->len;
  2130. octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
  2131. /* Offload checksum calculation for TCP/UDP packets */
  2132. dptr = dma_map_single(&oct->pci_dev->dev,
  2133. skb->data,
  2134. skb->len,
  2135. DMA_TO_DEVICE);
  2136. if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
  2137. dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
  2138. __func__);
  2139. return NETDEV_TX_BUSY;
  2140. }
  2141. if (OCTEON_CN23XX_PF(oct))
  2142. ndata.cmd.cmd3.dptr = dptr;
  2143. else
  2144. ndata.cmd.cmd2.dptr = dptr;
  2145. finfo->dptr = dptr;
  2146. ndata.reqtype = REQTYPE_NORESP_NET;
  2147. } else {
  2148. int i, frags;
  2149. struct skb_frag_struct *frag;
  2150. struct octnic_gather *g;
  2151. spin_lock(&lio->glist_lock[q_idx]);
  2152. g = (struct octnic_gather *)
  2153. list_delete_head(&lio->glist[q_idx]);
  2154. spin_unlock(&lio->glist_lock[q_idx]);
  2155. if (!g) {
  2156. netif_info(lio, tx_err, lio->netdev,
  2157. "Transmit scatter gather: glist null!\n");
  2158. goto lio_xmit_failed;
  2159. }
  2160. cmdsetup.s.gather = 1;
  2161. cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
  2162. octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
  2163. memset(g->sg, 0, g->sg_size);
  2164. g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
  2165. skb->data,
  2166. (skb->len - skb->data_len),
  2167. DMA_TO_DEVICE);
  2168. if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
  2169. dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
  2170. __func__);
  2171. return NETDEV_TX_BUSY;
  2172. }
  2173. add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
  2174. frags = skb_shinfo(skb)->nr_frags;
  2175. i = 1;
  2176. while (frags--) {
  2177. frag = &skb_shinfo(skb)->frags[i - 1];
  2178. g->sg[(i >> 2)].ptr[(i & 3)] =
  2179. dma_map_page(&oct->pci_dev->dev,
  2180. frag->page.p,
  2181. frag->page_offset,
  2182. frag->size,
  2183. DMA_TO_DEVICE);
  2184. if (dma_mapping_error(&oct->pci_dev->dev,
  2185. g->sg[i >> 2].ptr[i & 3])) {
  2186. dma_unmap_single(&oct->pci_dev->dev,
  2187. g->sg[0].ptr[0],
  2188. skb->len - skb->data_len,
  2189. DMA_TO_DEVICE);
  2190. for (j = 1; j < i; j++) {
  2191. frag = &skb_shinfo(skb)->frags[j - 1];
  2192. dma_unmap_page(&oct->pci_dev->dev,
  2193. g->sg[j >> 2].ptr[j & 3],
  2194. frag->size,
  2195. DMA_TO_DEVICE);
  2196. }
  2197. dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
  2198. __func__);
  2199. return NETDEV_TX_BUSY;
  2200. }
  2201. add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
  2202. i++;
  2203. }
  2204. dptr = g->sg_dma_ptr;
  2205. if (OCTEON_CN23XX_PF(oct))
  2206. ndata.cmd.cmd3.dptr = dptr;
  2207. else
  2208. ndata.cmd.cmd2.dptr = dptr;
  2209. finfo->dptr = dptr;
  2210. finfo->g = g;
  2211. ndata.reqtype = REQTYPE_NORESP_NET_SG;
  2212. }
  2213. if (OCTEON_CN23XX_PF(oct)) {
  2214. irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
  2215. tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
  2216. } else {
  2217. irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
  2218. tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
  2219. }
  2220. if (skb_shinfo(skb)->gso_size) {
  2221. tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
  2222. tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
  2223. stats->tx_gso++;
  2224. }
  2225. /* HW insert VLAN tag */
  2226. if (skb_vlan_tag_present(skb)) {
  2227. irh->priority = skb_vlan_tag_get(skb) >> 13;
  2228. irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
  2229. }
  2230. xmit_more = skb->xmit_more;
  2231. if (unlikely(cmdsetup.s.timestamp))
  2232. status = send_nic_timestamp_pkt(oct, &ndata, finfo, xmit_more);
  2233. else
  2234. status = octnet_send_nic_data_pkt(oct, &ndata, xmit_more);
  2235. if (status == IQ_SEND_FAILED)
  2236. goto lio_xmit_failed;
  2237. netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
  2238. if (status == IQ_SEND_STOP)
  2239. netif_stop_subqueue(netdev, q_idx);
  2240. netif_trans_update(netdev);
  2241. if (tx_info->s.gso_segs)
  2242. stats->tx_done += tx_info->s.gso_segs;
  2243. else
  2244. stats->tx_done++;
  2245. stats->tx_tot_bytes += ndata.datasize;
  2246. return NETDEV_TX_OK;
  2247. lio_xmit_failed:
  2248. stats->tx_dropped++;
  2249. netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
  2250. iq_no, stats->tx_dropped);
  2251. if (dptr)
  2252. dma_unmap_single(&oct->pci_dev->dev, dptr,
  2253. ndata.datasize, DMA_TO_DEVICE);
  2254. octeon_ring_doorbell_locked(oct, iq_no);
  2255. tx_buffer_free(skb);
  2256. return NETDEV_TX_OK;
  2257. }
  2258. /** \brief Network device Tx timeout
  2259. * @param netdev pointer to network device
  2260. */
  2261. static void liquidio_tx_timeout(struct net_device *netdev)
  2262. {
  2263. struct lio *lio;
  2264. lio = GET_LIO(netdev);
  2265. netif_info(lio, tx_err, lio->netdev,
  2266. "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
  2267. netdev->stats.tx_dropped);
  2268. netif_trans_update(netdev);
  2269. wake_txqs(netdev);
  2270. }
  2271. static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
  2272. __be16 proto __attribute__((unused)),
  2273. u16 vid)
  2274. {
  2275. struct lio *lio = GET_LIO(netdev);
  2276. struct octeon_device *oct = lio->oct_dev;
  2277. struct octnic_ctrl_pkt nctrl;
  2278. int ret = 0;
  2279. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2280. nctrl.ncmd.u64 = 0;
  2281. nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
  2282. nctrl.ncmd.s.param1 = vid;
  2283. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2284. nctrl.wait_time = 100;
  2285. nctrl.netpndev = (u64)netdev;
  2286. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2287. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2288. if (ret < 0) {
  2289. dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
  2290. ret);
  2291. }
  2292. return ret;
  2293. }
  2294. static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
  2295. __be16 proto __attribute__((unused)),
  2296. u16 vid)
  2297. {
  2298. struct lio *lio = GET_LIO(netdev);
  2299. struct octeon_device *oct = lio->oct_dev;
  2300. struct octnic_ctrl_pkt nctrl;
  2301. int ret = 0;
  2302. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2303. nctrl.ncmd.u64 = 0;
  2304. nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
  2305. nctrl.ncmd.s.param1 = vid;
  2306. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2307. nctrl.wait_time = 100;
  2308. nctrl.netpndev = (u64)netdev;
  2309. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2310. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2311. if (ret < 0) {
  2312. dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
  2313. ret);
  2314. }
  2315. return ret;
  2316. }
  2317. /** Sending command to enable/disable RX checksum offload
  2318. * @param netdev pointer to network device
  2319. * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
  2320. * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
  2321. * OCTNET_CMD_RXCSUM_DISABLE
  2322. * @returns SUCCESS or FAILURE
  2323. */
  2324. static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
  2325. u8 rx_cmd)
  2326. {
  2327. struct lio *lio = GET_LIO(netdev);
  2328. struct octeon_device *oct = lio->oct_dev;
  2329. struct octnic_ctrl_pkt nctrl;
  2330. int ret = 0;
  2331. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2332. nctrl.ncmd.u64 = 0;
  2333. nctrl.ncmd.s.cmd = command;
  2334. nctrl.ncmd.s.param1 = rx_cmd;
  2335. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2336. nctrl.wait_time = 100;
  2337. nctrl.netpndev = (u64)netdev;
  2338. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2339. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2340. if (ret < 0) {
  2341. dev_err(&oct->pci_dev->dev,
  2342. "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
  2343. ret);
  2344. }
  2345. return ret;
  2346. }
  2347. /** Sending command to add/delete VxLAN UDP port to firmware
  2348. * @param netdev pointer to network device
  2349. * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
  2350. * @param vxlan_port VxLAN port to be added or deleted
  2351. * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
  2352. * OCTNET_CMD_VXLAN_PORT_DEL
  2353. * @returns SUCCESS or FAILURE
  2354. */
  2355. static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
  2356. u16 vxlan_port, u8 vxlan_cmd_bit)
  2357. {
  2358. struct lio *lio = GET_LIO(netdev);
  2359. struct octeon_device *oct = lio->oct_dev;
  2360. struct octnic_ctrl_pkt nctrl;
  2361. int ret = 0;
  2362. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2363. nctrl.ncmd.u64 = 0;
  2364. nctrl.ncmd.s.cmd = command;
  2365. nctrl.ncmd.s.more = vxlan_cmd_bit;
  2366. nctrl.ncmd.s.param1 = vxlan_port;
  2367. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2368. nctrl.wait_time = 100;
  2369. nctrl.netpndev = (u64)netdev;
  2370. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2371. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2372. if (ret < 0) {
  2373. dev_err(&oct->pci_dev->dev,
  2374. "VxLAN port add/delete failed in core (ret:0x%x)\n",
  2375. ret);
  2376. }
  2377. return ret;
  2378. }
  2379. /** \brief Net device fix features
  2380. * @param netdev pointer to network device
  2381. * @param request features requested
  2382. * @returns updated features list
  2383. */
  2384. static netdev_features_t liquidio_fix_features(struct net_device *netdev,
  2385. netdev_features_t request)
  2386. {
  2387. struct lio *lio = netdev_priv(netdev);
  2388. if ((request & NETIF_F_RXCSUM) &&
  2389. !(lio->dev_capability & NETIF_F_RXCSUM))
  2390. request &= ~NETIF_F_RXCSUM;
  2391. if ((request & NETIF_F_HW_CSUM) &&
  2392. !(lio->dev_capability & NETIF_F_HW_CSUM))
  2393. request &= ~NETIF_F_HW_CSUM;
  2394. if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
  2395. request &= ~NETIF_F_TSO;
  2396. if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
  2397. request &= ~NETIF_F_TSO6;
  2398. if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
  2399. request &= ~NETIF_F_LRO;
  2400. /*Disable LRO if RXCSUM is off */
  2401. if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
  2402. (lio->dev_capability & NETIF_F_LRO))
  2403. request &= ~NETIF_F_LRO;
  2404. if ((request & NETIF_F_HW_VLAN_CTAG_FILTER) &&
  2405. !(lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER))
  2406. request &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
  2407. return request;
  2408. }
  2409. /** \brief Net device set features
  2410. * @param netdev pointer to network device
  2411. * @param features features to enable/disable
  2412. */
  2413. static int liquidio_set_features(struct net_device *netdev,
  2414. netdev_features_t features)
  2415. {
  2416. struct lio *lio = netdev_priv(netdev);
  2417. if ((features & NETIF_F_LRO) &&
  2418. (lio->dev_capability & NETIF_F_LRO) &&
  2419. !(netdev->features & NETIF_F_LRO))
  2420. liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
  2421. OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
  2422. else if (!(features & NETIF_F_LRO) &&
  2423. (lio->dev_capability & NETIF_F_LRO) &&
  2424. (netdev->features & NETIF_F_LRO))
  2425. liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
  2426. OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
  2427. /* Sending command to firmware to enable/disable RX checksum
  2428. * offload settings using ethtool
  2429. */
  2430. if (!(netdev->features & NETIF_F_RXCSUM) &&
  2431. (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
  2432. (features & NETIF_F_RXCSUM))
  2433. liquidio_set_rxcsum_command(netdev,
  2434. OCTNET_CMD_TNL_RX_CSUM_CTL,
  2435. OCTNET_CMD_RXCSUM_ENABLE);
  2436. else if ((netdev->features & NETIF_F_RXCSUM) &&
  2437. (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
  2438. !(features & NETIF_F_RXCSUM))
  2439. liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
  2440. OCTNET_CMD_RXCSUM_DISABLE);
  2441. if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
  2442. (lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER) &&
  2443. !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
  2444. liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
  2445. OCTNET_CMD_VLAN_FILTER_ENABLE);
  2446. else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
  2447. (lio->dev_capability & NETIF_F_HW_VLAN_CTAG_FILTER) &&
  2448. (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
  2449. liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
  2450. OCTNET_CMD_VLAN_FILTER_DISABLE);
  2451. return 0;
  2452. }
  2453. static void liquidio_add_vxlan_port(struct net_device *netdev,
  2454. struct udp_tunnel_info *ti)
  2455. {
  2456. if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
  2457. return;
  2458. liquidio_vxlan_port_command(netdev,
  2459. OCTNET_CMD_VXLAN_PORT_CONFIG,
  2460. htons(ti->port),
  2461. OCTNET_CMD_VXLAN_PORT_ADD);
  2462. }
  2463. static void liquidio_del_vxlan_port(struct net_device *netdev,
  2464. struct udp_tunnel_info *ti)
  2465. {
  2466. if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
  2467. return;
  2468. liquidio_vxlan_port_command(netdev,
  2469. OCTNET_CMD_VXLAN_PORT_CONFIG,
  2470. htons(ti->port),
  2471. OCTNET_CMD_VXLAN_PORT_DEL);
  2472. }
  2473. static int __liquidio_set_vf_mac(struct net_device *netdev, int vfidx,
  2474. u8 *mac, bool is_admin_assigned)
  2475. {
  2476. struct lio *lio = GET_LIO(netdev);
  2477. struct octeon_device *oct = lio->oct_dev;
  2478. struct octnic_ctrl_pkt nctrl;
  2479. if (!is_valid_ether_addr(mac))
  2480. return -EINVAL;
  2481. if (vfidx < 0 || vfidx >= oct->sriov_info.max_vfs)
  2482. return -EINVAL;
  2483. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2484. nctrl.ncmd.u64 = 0;
  2485. nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
  2486. /* vfidx is 0 based, but vf_num (param1) is 1 based */
  2487. nctrl.ncmd.s.param1 = vfidx + 1;
  2488. nctrl.ncmd.s.param2 = (is_admin_assigned ? 1 : 0);
  2489. nctrl.ncmd.s.more = 1;
  2490. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2491. nctrl.netpndev = (u64)netdev;
  2492. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2493. nctrl.wait_time = LIO_CMD_WAIT_TM;
  2494. nctrl.udd[0] = 0;
  2495. /* The MAC Address is presented in network byte order. */
  2496. ether_addr_copy((u8 *)&nctrl.udd[0] + 2, mac);
  2497. oct->sriov_info.vf_macaddr[vfidx] = nctrl.udd[0];
  2498. octnet_send_nic_ctrl_pkt(oct, &nctrl);
  2499. return 0;
  2500. }
  2501. static int liquidio_set_vf_mac(struct net_device *netdev, int vfidx, u8 *mac)
  2502. {
  2503. struct lio *lio = GET_LIO(netdev);
  2504. struct octeon_device *oct = lio->oct_dev;
  2505. int retval;
  2506. if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
  2507. return -EINVAL;
  2508. retval = __liquidio_set_vf_mac(netdev, vfidx, mac, true);
  2509. if (!retval)
  2510. cn23xx_tell_vf_its_macaddr_changed(oct, vfidx, mac);
  2511. return retval;
  2512. }
  2513. static int liquidio_set_vf_vlan(struct net_device *netdev, int vfidx,
  2514. u16 vlan, u8 qos, __be16 vlan_proto)
  2515. {
  2516. struct lio *lio = GET_LIO(netdev);
  2517. struct octeon_device *oct = lio->oct_dev;
  2518. struct octnic_ctrl_pkt nctrl;
  2519. u16 vlantci;
  2520. if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
  2521. return -EINVAL;
  2522. if (vlan_proto != htons(ETH_P_8021Q))
  2523. return -EPROTONOSUPPORT;
  2524. if (vlan >= VLAN_N_VID || qos > 7)
  2525. return -EINVAL;
  2526. if (vlan)
  2527. vlantci = vlan | (u16)qos << VLAN_PRIO_SHIFT;
  2528. else
  2529. vlantci = 0;
  2530. if (oct->sriov_info.vf_vlantci[vfidx] == vlantci)
  2531. return 0;
  2532. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2533. if (vlan)
  2534. nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
  2535. else
  2536. nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
  2537. nctrl.ncmd.s.param1 = vlantci;
  2538. nctrl.ncmd.s.param2 =
  2539. vfidx + 1; /* vfidx is 0 based, but vf_num (param2) is 1 based */
  2540. nctrl.ncmd.s.more = 0;
  2541. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2542. nctrl.cb_fn = 0;
  2543. nctrl.wait_time = LIO_CMD_WAIT_TM;
  2544. octnet_send_nic_ctrl_pkt(oct, &nctrl);
  2545. oct->sriov_info.vf_vlantci[vfidx] = vlantci;
  2546. return 0;
  2547. }
  2548. static int liquidio_get_vf_config(struct net_device *netdev, int vfidx,
  2549. struct ifla_vf_info *ivi)
  2550. {
  2551. struct lio *lio = GET_LIO(netdev);
  2552. struct octeon_device *oct = lio->oct_dev;
  2553. u8 *macaddr;
  2554. if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
  2555. return -EINVAL;
  2556. ivi->vf = vfidx;
  2557. macaddr = 2 + (u8 *)&oct->sriov_info.vf_macaddr[vfidx];
  2558. ether_addr_copy(&ivi->mac[0], macaddr);
  2559. ivi->vlan = oct->sriov_info.vf_vlantci[vfidx] & VLAN_VID_MASK;
  2560. ivi->qos = oct->sriov_info.vf_vlantci[vfidx] >> VLAN_PRIO_SHIFT;
  2561. if (oct->sriov_info.trusted_vf.active &&
  2562. oct->sriov_info.trusted_vf.id == vfidx)
  2563. ivi->trusted = true;
  2564. else
  2565. ivi->trusted = false;
  2566. ivi->linkstate = oct->sriov_info.vf_linkstate[vfidx];
  2567. return 0;
  2568. }
  2569. static void trusted_vf_callback(struct octeon_device *oct_dev,
  2570. u32 status, void *ptr)
  2571. {
  2572. struct octeon_soft_command *sc = (struct octeon_soft_command *)ptr;
  2573. struct lio_trusted_vf_ctx *ctx;
  2574. ctx = (struct lio_trusted_vf_ctx *)sc->ctxptr;
  2575. ctx->status = status;
  2576. complete(&ctx->complete);
  2577. }
  2578. static int liquidio_send_vf_trust_cmd(struct lio *lio, int vfidx, bool trusted)
  2579. {
  2580. struct octeon_device *oct = lio->oct_dev;
  2581. struct lio_trusted_vf_ctx *ctx;
  2582. struct octeon_soft_command *sc;
  2583. int ctx_size, retval;
  2584. ctx_size = sizeof(struct lio_trusted_vf_ctx);
  2585. sc = octeon_alloc_soft_command(oct, 0, 0, ctx_size);
  2586. ctx = (struct lio_trusted_vf_ctx *)sc->ctxptr;
  2587. init_completion(&ctx->complete);
  2588. sc->iq_no = lio->linfo.txpciq[0].s.q_no;
  2589. /* vfidx is 0 based, but vf_num (param1) is 1 based */
  2590. octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
  2591. OPCODE_NIC_SET_TRUSTED_VF, 0, vfidx + 1,
  2592. trusted);
  2593. sc->callback = trusted_vf_callback;
  2594. sc->callback_arg = sc;
  2595. sc->wait_time = 1000;
  2596. retval = octeon_send_soft_command(oct, sc);
  2597. if (retval == IQ_SEND_FAILED) {
  2598. retval = -1;
  2599. } else {
  2600. /* Wait for response or timeout */
  2601. if (wait_for_completion_timeout(&ctx->complete,
  2602. msecs_to_jiffies(2000)))
  2603. retval = ctx->status;
  2604. else
  2605. retval = -1;
  2606. }
  2607. octeon_free_soft_command(oct, sc);
  2608. return retval;
  2609. }
  2610. static int liquidio_set_vf_trust(struct net_device *netdev, int vfidx,
  2611. bool setting)
  2612. {
  2613. struct lio *lio = GET_LIO(netdev);
  2614. struct octeon_device *oct = lio->oct_dev;
  2615. if (strcmp(oct->fw_info.liquidio_firmware_version, "1.7.1") < 0) {
  2616. /* trusted vf is not supported by firmware older than 1.7.1 */
  2617. return -EOPNOTSUPP;
  2618. }
  2619. if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced) {
  2620. netif_info(lio, drv, lio->netdev, "Invalid vfidx %d\n", vfidx);
  2621. return -EINVAL;
  2622. }
  2623. if (setting) {
  2624. /* Set */
  2625. if (oct->sriov_info.trusted_vf.active &&
  2626. oct->sriov_info.trusted_vf.id == vfidx)
  2627. return 0;
  2628. if (oct->sriov_info.trusted_vf.active) {
  2629. netif_info(lio, drv, lio->netdev, "More than one trusted VF is not allowed\n");
  2630. return -EPERM;
  2631. }
  2632. } else {
  2633. /* Clear */
  2634. if (!oct->sriov_info.trusted_vf.active)
  2635. return 0;
  2636. }
  2637. if (!liquidio_send_vf_trust_cmd(lio, vfidx, setting)) {
  2638. if (setting) {
  2639. oct->sriov_info.trusted_vf.id = vfidx;
  2640. oct->sriov_info.trusted_vf.active = true;
  2641. } else {
  2642. oct->sriov_info.trusted_vf.active = false;
  2643. }
  2644. netif_info(lio, drv, lio->netdev, "VF %u is %strusted\n", vfidx,
  2645. setting ? "" : "not ");
  2646. } else {
  2647. netif_info(lio, drv, lio->netdev, "Failed to set VF trusted\n");
  2648. return -1;
  2649. }
  2650. return 0;
  2651. }
  2652. static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
  2653. int linkstate)
  2654. {
  2655. struct lio *lio = GET_LIO(netdev);
  2656. struct octeon_device *oct = lio->oct_dev;
  2657. struct octnic_ctrl_pkt nctrl;
  2658. if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
  2659. return -EINVAL;
  2660. if (oct->sriov_info.vf_linkstate[vfidx] == linkstate)
  2661. return 0;
  2662. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2663. nctrl.ncmd.s.cmd = OCTNET_CMD_SET_VF_LINKSTATE;
  2664. nctrl.ncmd.s.param1 =
  2665. vfidx + 1; /* vfidx is 0 based, but vf_num (param1) is 1 based */
  2666. nctrl.ncmd.s.param2 = linkstate;
  2667. nctrl.ncmd.s.more = 0;
  2668. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2669. nctrl.cb_fn = 0;
  2670. nctrl.wait_time = LIO_CMD_WAIT_TM;
  2671. octnet_send_nic_ctrl_pkt(oct, &nctrl);
  2672. oct->sriov_info.vf_linkstate[vfidx] = linkstate;
  2673. return 0;
  2674. }
  2675. static int
  2676. liquidio_eswitch_mode_get(struct devlink *devlink, u16 *mode)
  2677. {
  2678. struct lio_devlink_priv *priv;
  2679. struct octeon_device *oct;
  2680. priv = devlink_priv(devlink);
  2681. oct = priv->oct;
  2682. *mode = oct->eswitch_mode;
  2683. return 0;
  2684. }
  2685. static int
  2686. liquidio_eswitch_mode_set(struct devlink *devlink, u16 mode)
  2687. {
  2688. struct lio_devlink_priv *priv;
  2689. struct octeon_device *oct;
  2690. int ret = 0;
  2691. priv = devlink_priv(devlink);
  2692. oct = priv->oct;
  2693. if (!(oct->fw_info.app_cap_flags & LIQUIDIO_SWITCHDEV_CAP))
  2694. return -EINVAL;
  2695. if (oct->eswitch_mode == mode)
  2696. return 0;
  2697. switch (mode) {
  2698. case DEVLINK_ESWITCH_MODE_SWITCHDEV:
  2699. oct->eswitch_mode = mode;
  2700. ret = lio_vf_rep_create(oct);
  2701. break;
  2702. case DEVLINK_ESWITCH_MODE_LEGACY:
  2703. lio_vf_rep_destroy(oct);
  2704. oct->eswitch_mode = mode;
  2705. break;
  2706. default:
  2707. ret = -EINVAL;
  2708. }
  2709. return ret;
  2710. }
  2711. static const struct devlink_ops liquidio_devlink_ops = {
  2712. .eswitch_mode_get = liquidio_eswitch_mode_get,
  2713. .eswitch_mode_set = liquidio_eswitch_mode_set,
  2714. };
  2715. static int
  2716. lio_pf_switchdev_attr_get(struct net_device *dev, struct switchdev_attr *attr)
  2717. {
  2718. struct lio *lio = GET_LIO(dev);
  2719. struct octeon_device *oct = lio->oct_dev;
  2720. if (oct->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
  2721. return -EOPNOTSUPP;
  2722. switch (attr->id) {
  2723. case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
  2724. attr->u.ppid.id_len = ETH_ALEN;
  2725. ether_addr_copy(attr->u.ppid.id,
  2726. (void *)&lio->linfo.hw_addr + 2);
  2727. break;
  2728. default:
  2729. return -EOPNOTSUPP;
  2730. }
  2731. return 0;
  2732. }
  2733. static const struct switchdev_ops lio_pf_switchdev_ops = {
  2734. .switchdev_port_attr_get = lio_pf_switchdev_attr_get,
  2735. };
  2736. static const struct net_device_ops lionetdevops = {
  2737. .ndo_open = liquidio_open,
  2738. .ndo_stop = liquidio_stop,
  2739. .ndo_start_xmit = liquidio_xmit,
  2740. .ndo_get_stats = liquidio_get_stats,
  2741. .ndo_set_mac_address = liquidio_set_mac,
  2742. .ndo_set_rx_mode = liquidio_set_mcast_list,
  2743. .ndo_tx_timeout = liquidio_tx_timeout,
  2744. .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
  2745. .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
  2746. .ndo_change_mtu = liquidio_change_mtu,
  2747. .ndo_do_ioctl = liquidio_ioctl,
  2748. .ndo_fix_features = liquidio_fix_features,
  2749. .ndo_set_features = liquidio_set_features,
  2750. .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
  2751. .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
  2752. .ndo_set_vf_mac = liquidio_set_vf_mac,
  2753. .ndo_set_vf_vlan = liquidio_set_vf_vlan,
  2754. .ndo_get_vf_config = liquidio_get_vf_config,
  2755. .ndo_set_vf_trust = liquidio_set_vf_trust,
  2756. .ndo_set_vf_link_state = liquidio_set_vf_link_state,
  2757. };
  2758. /** \brief Entry point for the liquidio module
  2759. */
  2760. static int __init liquidio_init(void)
  2761. {
  2762. int i;
  2763. struct handshake *hs;
  2764. init_completion(&first_stage);
  2765. octeon_init_device_list(OCTEON_CONFIG_TYPE_DEFAULT);
  2766. if (liquidio_init_pci())
  2767. return -EINVAL;
  2768. wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
  2769. for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
  2770. hs = &handshake[i];
  2771. if (hs->pci_dev) {
  2772. wait_for_completion(&hs->init);
  2773. if (!hs->init_ok) {
  2774. /* init handshake failed */
  2775. dev_err(&hs->pci_dev->dev,
  2776. "Failed to init device\n");
  2777. liquidio_deinit_pci();
  2778. return -EIO;
  2779. }
  2780. }
  2781. }
  2782. for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
  2783. hs = &handshake[i];
  2784. if (hs->pci_dev) {
  2785. wait_for_completion_timeout(&hs->started,
  2786. msecs_to_jiffies(30000));
  2787. if (!hs->started_ok) {
  2788. /* starter handshake failed */
  2789. dev_err(&hs->pci_dev->dev,
  2790. "Firmware failed to start\n");
  2791. liquidio_deinit_pci();
  2792. return -EIO;
  2793. }
  2794. }
  2795. }
  2796. return 0;
  2797. }
  2798. static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
  2799. {
  2800. struct octeon_device *oct = (struct octeon_device *)buf;
  2801. struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
  2802. int gmxport = 0;
  2803. union oct_link_status *ls;
  2804. int i;
  2805. if (recv_pkt->buffer_size[0] != (sizeof(*ls) + OCT_DROQ_INFO_SIZE)) {
  2806. dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
  2807. recv_pkt->buffer_size[0],
  2808. recv_pkt->rh.r_nic_info.gmxport);
  2809. goto nic_info_err;
  2810. }
  2811. gmxport = recv_pkt->rh.r_nic_info.gmxport;
  2812. ls = (union oct_link_status *)(get_rbd(recv_pkt->buffer_ptr[0]) +
  2813. OCT_DROQ_INFO_SIZE);
  2814. octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
  2815. for (i = 0; i < oct->ifcount; i++) {
  2816. if (oct->props[i].gmxport == gmxport) {
  2817. update_link_status(oct->props[i].netdev, ls);
  2818. break;
  2819. }
  2820. }
  2821. nic_info_err:
  2822. for (i = 0; i < recv_pkt->buffer_count; i++)
  2823. recv_buffer_free(recv_pkt->buffer_ptr[i]);
  2824. octeon_free_recv_info(recv_info);
  2825. return 0;
  2826. }
  2827. /**
  2828. * \brief Setup network interfaces
  2829. * @param octeon_dev octeon device
  2830. *
  2831. * Called during init time for each device. It assumes the NIC
  2832. * is already up and running. The link information for each
  2833. * interface is passed in link_info.
  2834. */
  2835. static int setup_nic_devices(struct octeon_device *octeon_dev)
  2836. {
  2837. struct lio *lio = NULL;
  2838. struct net_device *netdev;
  2839. u8 mac[6], i, j, *fw_ver;
  2840. struct octeon_soft_command *sc;
  2841. struct liquidio_if_cfg_context *ctx;
  2842. struct liquidio_if_cfg_resp *resp;
  2843. struct octdev_props *props;
  2844. int retval, num_iqueues, num_oqueues;
  2845. union oct_nic_if_cfg if_cfg;
  2846. unsigned int base_queue;
  2847. unsigned int gmx_port_id;
  2848. u32 resp_size, ctx_size, data_size;
  2849. u32 ifidx_or_pfnum;
  2850. struct lio_version *vdata;
  2851. struct devlink *devlink;
  2852. struct lio_devlink_priv *lio_devlink;
  2853. /* This is to handle link status changes */
  2854. octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
  2855. OPCODE_NIC_INFO,
  2856. lio_nic_info, octeon_dev);
  2857. /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
  2858. * They are handled directly.
  2859. */
  2860. octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
  2861. free_netbuf);
  2862. octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
  2863. free_netsgbuf);
  2864. octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
  2865. free_netsgbuf_with_resp);
  2866. for (i = 0; i < octeon_dev->ifcount; i++) {
  2867. resp_size = sizeof(struct liquidio_if_cfg_resp);
  2868. ctx_size = sizeof(struct liquidio_if_cfg_context);
  2869. data_size = sizeof(struct lio_version);
  2870. sc = (struct octeon_soft_command *)
  2871. octeon_alloc_soft_command(octeon_dev, data_size,
  2872. resp_size, ctx_size);
  2873. resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
  2874. ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
  2875. vdata = (struct lio_version *)sc->virtdptr;
  2876. *((u64 *)vdata) = 0;
  2877. vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
  2878. vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
  2879. vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
  2880. if (OCTEON_CN23XX_PF(octeon_dev)) {
  2881. num_iqueues = octeon_dev->sriov_info.num_pf_rings;
  2882. num_oqueues = octeon_dev->sriov_info.num_pf_rings;
  2883. base_queue = octeon_dev->sriov_info.pf_srn;
  2884. gmx_port_id = octeon_dev->pf_num;
  2885. ifidx_or_pfnum = octeon_dev->pf_num;
  2886. } else {
  2887. num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
  2888. octeon_get_conf(octeon_dev), i);
  2889. num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
  2890. octeon_get_conf(octeon_dev), i);
  2891. base_queue = CFG_GET_BASE_QUE_NIC_IF(
  2892. octeon_get_conf(octeon_dev), i);
  2893. gmx_port_id = CFG_GET_GMXID_NIC_IF(
  2894. octeon_get_conf(octeon_dev), i);
  2895. ifidx_or_pfnum = i;
  2896. }
  2897. dev_dbg(&octeon_dev->pci_dev->dev,
  2898. "requesting config for interface %d, iqs %d, oqs %d\n",
  2899. ifidx_or_pfnum, num_iqueues, num_oqueues);
  2900. WRITE_ONCE(ctx->cond, 0);
  2901. ctx->octeon_id = lio_get_device_id(octeon_dev);
  2902. init_waitqueue_head(&ctx->wc);
  2903. if_cfg.u64 = 0;
  2904. if_cfg.s.num_iqueues = num_iqueues;
  2905. if_cfg.s.num_oqueues = num_oqueues;
  2906. if_cfg.s.base_queue = base_queue;
  2907. if_cfg.s.gmx_port_id = gmx_port_id;
  2908. sc->iq_no = 0;
  2909. octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
  2910. OPCODE_NIC_IF_CFG, 0,
  2911. if_cfg.u64, 0);
  2912. sc->callback = if_cfg_callback;
  2913. sc->callback_arg = sc;
  2914. sc->wait_time = 3000;
  2915. retval = octeon_send_soft_command(octeon_dev, sc);
  2916. if (retval == IQ_SEND_FAILED) {
  2917. dev_err(&octeon_dev->pci_dev->dev,
  2918. "iq/oq config failed status: %x\n",
  2919. retval);
  2920. /* Soft instr is freed by driver in case of failure. */
  2921. goto setup_nic_dev_fail;
  2922. }
  2923. /* Sleep on a wait queue till the cond flag indicates that the
  2924. * response arrived or timed-out.
  2925. */
  2926. if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
  2927. dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
  2928. goto setup_nic_wait_intr;
  2929. }
  2930. retval = resp->status;
  2931. if (retval) {
  2932. dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
  2933. goto setup_nic_dev_fail;
  2934. }
  2935. /* Verify f/w version (in case of 'auto' loading from flash) */
  2936. fw_ver = octeon_dev->fw_info.liquidio_firmware_version;
  2937. if (memcmp(LIQUIDIO_BASE_VERSION,
  2938. fw_ver,
  2939. strlen(LIQUIDIO_BASE_VERSION))) {
  2940. dev_err(&octeon_dev->pci_dev->dev,
  2941. "Unmatched firmware version. Expected %s.x, got %s.\n",
  2942. LIQUIDIO_BASE_VERSION, fw_ver);
  2943. goto setup_nic_dev_fail;
  2944. } else if (atomic_read(octeon_dev->adapter_fw_state) ==
  2945. FW_IS_PRELOADED) {
  2946. dev_info(&octeon_dev->pci_dev->dev,
  2947. "Using auto-loaded firmware version %s.\n",
  2948. fw_ver);
  2949. }
  2950. octeon_swap_8B_data((u64 *)(&resp->cfg_info),
  2951. (sizeof(struct liquidio_if_cfg_info)) >> 3);
  2952. num_iqueues = hweight64(resp->cfg_info.iqmask);
  2953. num_oqueues = hweight64(resp->cfg_info.oqmask);
  2954. if (!(num_iqueues) || !(num_oqueues)) {
  2955. dev_err(&octeon_dev->pci_dev->dev,
  2956. "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
  2957. resp->cfg_info.iqmask,
  2958. resp->cfg_info.oqmask);
  2959. goto setup_nic_dev_fail;
  2960. }
  2961. dev_dbg(&octeon_dev->pci_dev->dev,
  2962. "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
  2963. i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
  2964. num_iqueues, num_oqueues);
  2965. netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
  2966. if (!netdev) {
  2967. dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
  2968. goto setup_nic_dev_fail;
  2969. }
  2970. SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
  2971. /* Associate the routines that will handle different
  2972. * netdev tasks.
  2973. */
  2974. netdev->netdev_ops = &lionetdevops;
  2975. SWITCHDEV_SET_OPS(netdev, &lio_pf_switchdev_ops);
  2976. lio = GET_LIO(netdev);
  2977. memset(lio, 0, sizeof(struct lio));
  2978. lio->ifidx = ifidx_or_pfnum;
  2979. props = &octeon_dev->props[i];
  2980. props->gmxport = resp->cfg_info.linfo.gmxport;
  2981. props->netdev = netdev;
  2982. lio->linfo.num_rxpciq = num_oqueues;
  2983. lio->linfo.num_txpciq = num_iqueues;
  2984. for (j = 0; j < num_oqueues; j++) {
  2985. lio->linfo.rxpciq[j].u64 =
  2986. resp->cfg_info.linfo.rxpciq[j].u64;
  2987. }
  2988. for (j = 0; j < num_iqueues; j++) {
  2989. lio->linfo.txpciq[j].u64 =
  2990. resp->cfg_info.linfo.txpciq[j].u64;
  2991. }
  2992. lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
  2993. lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
  2994. lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
  2995. lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  2996. if (OCTEON_CN23XX_PF(octeon_dev) ||
  2997. OCTEON_CN6XXX(octeon_dev)) {
  2998. lio->dev_capability = NETIF_F_HIGHDMA
  2999. | NETIF_F_IP_CSUM
  3000. | NETIF_F_IPV6_CSUM
  3001. | NETIF_F_SG | NETIF_F_RXCSUM
  3002. | NETIF_F_GRO
  3003. | NETIF_F_TSO | NETIF_F_TSO6
  3004. | NETIF_F_LRO;
  3005. }
  3006. netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
  3007. /* Copy of transmit encapsulation capabilities:
  3008. * TSO, TSO6, Checksums for this device
  3009. */
  3010. lio->enc_dev_capability = NETIF_F_IP_CSUM
  3011. | NETIF_F_IPV6_CSUM
  3012. | NETIF_F_GSO_UDP_TUNNEL
  3013. | NETIF_F_HW_CSUM | NETIF_F_SG
  3014. | NETIF_F_RXCSUM
  3015. | NETIF_F_TSO | NETIF_F_TSO6
  3016. | NETIF_F_LRO;
  3017. netdev->hw_enc_features = (lio->enc_dev_capability &
  3018. ~NETIF_F_LRO);
  3019. lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
  3020. netdev->vlan_features = lio->dev_capability;
  3021. /* Add any unchangeable hw features */
  3022. lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
  3023. NETIF_F_HW_VLAN_CTAG_RX |
  3024. NETIF_F_HW_VLAN_CTAG_TX;
  3025. netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
  3026. netdev->hw_features = lio->dev_capability;
  3027. /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
  3028. netdev->hw_features = netdev->hw_features &
  3029. ~NETIF_F_HW_VLAN_CTAG_RX;
  3030. /* MTU range: 68 - 16000 */
  3031. netdev->min_mtu = LIO_MIN_MTU_SIZE;
  3032. netdev->max_mtu = LIO_MAX_MTU_SIZE;
  3033. /* Point to the properties for octeon device to which this
  3034. * interface belongs.
  3035. */
  3036. lio->oct_dev = octeon_dev;
  3037. lio->octprops = props;
  3038. lio->netdev = netdev;
  3039. dev_dbg(&octeon_dev->pci_dev->dev,
  3040. "if%d gmx: %d hw_addr: 0x%llx\n", i,
  3041. lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
  3042. for (j = 0; j < octeon_dev->sriov_info.max_vfs; j++) {
  3043. u8 vfmac[ETH_ALEN];
  3044. random_ether_addr(&vfmac[0]);
  3045. if (__liquidio_set_vf_mac(netdev, j,
  3046. &vfmac[0], false)) {
  3047. dev_err(&octeon_dev->pci_dev->dev,
  3048. "Error setting VF%d MAC address\n",
  3049. j);
  3050. goto setup_nic_dev_fail;
  3051. }
  3052. }
  3053. /* 64-bit swap required on LE machines */
  3054. octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
  3055. for (j = 0; j < 6; j++)
  3056. mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
  3057. /* Copy MAC Address to OS network device structure */
  3058. ether_addr_copy(netdev->dev_addr, mac);
  3059. /* By default all interfaces on a single Octeon uses the same
  3060. * tx and rx queues
  3061. */
  3062. lio->txq = lio->linfo.txpciq[0].s.q_no;
  3063. lio->rxq = lio->linfo.rxpciq[0].s.q_no;
  3064. if (liquidio_setup_io_queues(octeon_dev, i,
  3065. lio->linfo.num_txpciq,
  3066. lio->linfo.num_rxpciq)) {
  3067. dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
  3068. goto setup_nic_dev_fail;
  3069. }
  3070. ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
  3071. lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
  3072. lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
  3073. if (setup_glists(octeon_dev, lio, num_iqueues)) {
  3074. dev_err(&octeon_dev->pci_dev->dev,
  3075. "Gather list allocation failed\n");
  3076. goto setup_nic_dev_fail;
  3077. }
  3078. /* Register ethtool support */
  3079. liquidio_set_ethtool_ops(netdev);
  3080. if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
  3081. octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
  3082. else
  3083. octeon_dev->priv_flags = 0x0;
  3084. if (netdev->features & NETIF_F_LRO)
  3085. liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
  3086. OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
  3087. liquidio_set_feature(netdev, OCTNET_CMD_VLAN_FILTER_CTL,
  3088. OCTNET_CMD_VLAN_FILTER_ENABLE);
  3089. if ((debug != -1) && (debug & NETIF_MSG_HW))
  3090. liquidio_set_feature(netdev,
  3091. OCTNET_CMD_VERBOSE_ENABLE, 0);
  3092. if (setup_link_status_change_wq(netdev))
  3093. goto setup_nic_dev_fail;
  3094. if ((octeon_dev->fw_info.app_cap_flags &
  3095. LIQUIDIO_TIME_SYNC_CAP) &&
  3096. setup_sync_octeon_time_wq(netdev))
  3097. goto setup_nic_dev_fail;
  3098. if (setup_rx_oom_poll_fn(netdev))
  3099. goto setup_nic_dev_fail;
  3100. /* Register the network device with the OS */
  3101. if (register_netdev(netdev)) {
  3102. dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
  3103. goto setup_nic_dev_fail;
  3104. }
  3105. dev_dbg(&octeon_dev->pci_dev->dev,
  3106. "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
  3107. i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  3108. netif_carrier_off(netdev);
  3109. lio->link_changes++;
  3110. ifstate_set(lio, LIO_IFSTATE_REGISTERED);
  3111. /* Sending command to firmware to enable Rx checksum offload
  3112. * by default at the time of setup of Liquidio driver for
  3113. * this device
  3114. */
  3115. liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
  3116. OCTNET_CMD_RXCSUM_ENABLE);
  3117. liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
  3118. OCTNET_CMD_TXCSUM_ENABLE);
  3119. dev_dbg(&octeon_dev->pci_dev->dev,
  3120. "NIC ifidx:%d Setup successful\n", i);
  3121. octeon_free_soft_command(octeon_dev, sc);
  3122. }
  3123. devlink = devlink_alloc(&liquidio_devlink_ops,
  3124. sizeof(struct lio_devlink_priv));
  3125. if (!devlink) {
  3126. dev_err(&octeon_dev->pci_dev->dev, "devlink alloc failed\n");
  3127. goto setup_nic_wait_intr;
  3128. }
  3129. lio_devlink = devlink_priv(devlink);
  3130. lio_devlink->oct = octeon_dev;
  3131. if (devlink_register(devlink, &octeon_dev->pci_dev->dev)) {
  3132. devlink_free(devlink);
  3133. dev_err(&octeon_dev->pci_dev->dev,
  3134. "devlink registration failed\n");
  3135. goto setup_nic_wait_intr;
  3136. }
  3137. octeon_dev->devlink = devlink;
  3138. octeon_dev->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
  3139. return 0;
  3140. setup_nic_dev_fail:
  3141. octeon_free_soft_command(octeon_dev, sc);
  3142. setup_nic_wait_intr:
  3143. while (i--) {
  3144. dev_err(&octeon_dev->pci_dev->dev,
  3145. "NIC ifidx:%d Setup failed\n", i);
  3146. liquidio_destroy_nic_device(octeon_dev, i);
  3147. }
  3148. return -ENODEV;
  3149. }
  3150. #ifdef CONFIG_PCI_IOV
  3151. static int octeon_enable_sriov(struct octeon_device *oct)
  3152. {
  3153. unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
  3154. struct pci_dev *vfdev;
  3155. int err;
  3156. u32 u;
  3157. if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
  3158. err = pci_enable_sriov(oct->pci_dev,
  3159. oct->sriov_info.num_vfs_alloced);
  3160. if (err) {
  3161. dev_err(&oct->pci_dev->dev,
  3162. "OCTEON: Failed to enable PCI sriov: %d\n",
  3163. err);
  3164. oct->sriov_info.num_vfs_alloced = 0;
  3165. return err;
  3166. }
  3167. oct->sriov_info.sriov_enabled = 1;
  3168. /* init lookup table that maps DPI ring number to VF pci_dev
  3169. * struct pointer
  3170. */
  3171. u = 0;
  3172. vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
  3173. OCTEON_CN23XX_VF_VID, NULL);
  3174. while (vfdev) {
  3175. if (vfdev->is_virtfn &&
  3176. (vfdev->physfn == oct->pci_dev)) {
  3177. oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
  3178. vfdev;
  3179. u += oct->sriov_info.rings_per_vf;
  3180. }
  3181. vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
  3182. OCTEON_CN23XX_VF_VID, vfdev);
  3183. }
  3184. }
  3185. return num_vfs_alloced;
  3186. }
  3187. static int lio_pci_sriov_disable(struct octeon_device *oct)
  3188. {
  3189. int u;
  3190. if (pci_vfs_assigned(oct->pci_dev)) {
  3191. dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
  3192. return -EPERM;
  3193. }
  3194. pci_disable_sriov(oct->pci_dev);
  3195. u = 0;
  3196. while (u < MAX_POSSIBLE_VFS) {
  3197. oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
  3198. u += oct->sriov_info.rings_per_vf;
  3199. }
  3200. oct->sriov_info.num_vfs_alloced = 0;
  3201. dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
  3202. oct->pf_num);
  3203. return 0;
  3204. }
  3205. static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
  3206. {
  3207. struct octeon_device *oct = pci_get_drvdata(dev);
  3208. int ret = 0;
  3209. if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
  3210. (oct->sriov_info.sriov_enabled)) {
  3211. dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
  3212. oct->pf_num, num_vfs);
  3213. return 0;
  3214. }
  3215. if (!num_vfs) {
  3216. lio_vf_rep_destroy(oct);
  3217. ret = lio_pci_sriov_disable(oct);
  3218. } else if (num_vfs > oct->sriov_info.max_vfs) {
  3219. dev_err(&oct->pci_dev->dev,
  3220. "OCTEON: Max allowed VFs:%d user requested:%d",
  3221. oct->sriov_info.max_vfs, num_vfs);
  3222. ret = -EPERM;
  3223. } else {
  3224. oct->sriov_info.num_vfs_alloced = num_vfs;
  3225. ret = octeon_enable_sriov(oct);
  3226. dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
  3227. oct->pf_num, num_vfs);
  3228. ret = lio_vf_rep_create(oct);
  3229. if (ret)
  3230. dev_info(&oct->pci_dev->dev,
  3231. "vf representor create failed");
  3232. }
  3233. return ret;
  3234. }
  3235. #endif
  3236. /**
  3237. * \brief initialize the NIC
  3238. * @param oct octeon device
  3239. *
  3240. * This initialization routine is called once the Octeon device application is
  3241. * up and running
  3242. */
  3243. static int liquidio_init_nic_module(struct octeon_device *oct)
  3244. {
  3245. int i, retval = 0;
  3246. int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
  3247. dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
  3248. /* only default iq and oq were initialized
  3249. * initialize the rest as well
  3250. */
  3251. /* run port_config command for each port */
  3252. oct->ifcount = num_nic_ports;
  3253. memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
  3254. for (i = 0; i < MAX_OCTEON_LINKS; i++)
  3255. oct->props[i].gmxport = -1;
  3256. retval = setup_nic_devices(oct);
  3257. if (retval) {
  3258. dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
  3259. goto octnet_init_failure;
  3260. }
  3261. /* Call vf_rep_modinit if the firmware is switchdev capable
  3262. * and do it from the first liquidio function probed.
  3263. */
  3264. if (!oct->octeon_id &&
  3265. oct->fw_info.app_cap_flags & LIQUIDIO_SWITCHDEV_CAP) {
  3266. retval = lio_vf_rep_modinit();
  3267. if (retval) {
  3268. liquidio_stop_nic_module(oct);
  3269. goto octnet_init_failure;
  3270. }
  3271. }
  3272. liquidio_ptp_init(oct);
  3273. dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
  3274. return retval;
  3275. octnet_init_failure:
  3276. oct->ifcount = 0;
  3277. return retval;
  3278. }
  3279. /**
  3280. * \brief starter callback that invokes the remaining initialization work after
  3281. * the NIC is up and running.
  3282. * @param octptr work struct work_struct
  3283. */
  3284. static void nic_starter(struct work_struct *work)
  3285. {
  3286. struct octeon_device *oct;
  3287. struct cavium_wk *wk = (struct cavium_wk *)work;
  3288. oct = (struct octeon_device *)wk->ctxptr;
  3289. if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
  3290. return;
  3291. /* If the status of the device is CORE_OK, the core
  3292. * application has reported its application type. Call
  3293. * any registered handlers now and move to the RUNNING
  3294. * state.
  3295. */
  3296. if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
  3297. schedule_delayed_work(&oct->nic_poll_work.work,
  3298. LIQUIDIO_STARTER_POLL_INTERVAL_MS);
  3299. return;
  3300. }
  3301. atomic_set(&oct->status, OCT_DEV_RUNNING);
  3302. if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
  3303. dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
  3304. if (liquidio_init_nic_module(oct))
  3305. dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
  3306. else
  3307. handshake[oct->octeon_id].started_ok = 1;
  3308. } else {
  3309. dev_err(&oct->pci_dev->dev,
  3310. "Unexpected application running on NIC (%d). Check firmware.\n",
  3311. oct->app_mode);
  3312. }
  3313. complete(&handshake[oct->octeon_id].started);
  3314. }
  3315. static int
  3316. octeon_recv_vf_drv_notice(struct octeon_recv_info *recv_info, void *buf)
  3317. {
  3318. struct octeon_device *oct = (struct octeon_device *)buf;
  3319. struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
  3320. int i, notice, vf_idx;
  3321. bool cores_crashed;
  3322. u64 *data, vf_num;
  3323. notice = recv_pkt->rh.r.ossp;
  3324. data = (u64 *)(get_rbd(recv_pkt->buffer_ptr[0]) + OCT_DROQ_INFO_SIZE);
  3325. /* the first 64-bit word of data is the vf_num */
  3326. vf_num = data[0];
  3327. octeon_swap_8B_data(&vf_num, 1);
  3328. vf_idx = (int)vf_num - 1;
  3329. cores_crashed = READ_ONCE(oct->cores_crashed);
  3330. if (notice == VF_DRV_LOADED) {
  3331. if (!(oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx))) {
  3332. oct->sriov_info.vf_drv_loaded_mask |= BIT_ULL(vf_idx);
  3333. dev_info(&oct->pci_dev->dev,
  3334. "driver for VF%d was loaded\n", vf_idx);
  3335. if (!cores_crashed)
  3336. try_module_get(THIS_MODULE);
  3337. }
  3338. } else if (notice == VF_DRV_REMOVED) {
  3339. if (oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx)) {
  3340. oct->sriov_info.vf_drv_loaded_mask &= ~BIT_ULL(vf_idx);
  3341. dev_info(&oct->pci_dev->dev,
  3342. "driver for VF%d was removed\n", vf_idx);
  3343. if (!cores_crashed)
  3344. module_put(THIS_MODULE);
  3345. }
  3346. } else if (notice == VF_DRV_MACADDR_CHANGED) {
  3347. u8 *b = (u8 *)&data[1];
  3348. oct->sriov_info.vf_macaddr[vf_idx] = data[1];
  3349. dev_info(&oct->pci_dev->dev,
  3350. "VF driver changed VF%d's MAC address to %pM\n",
  3351. vf_idx, b + 2);
  3352. }
  3353. for (i = 0; i < recv_pkt->buffer_count; i++)
  3354. recv_buffer_free(recv_pkt->buffer_ptr[i]);
  3355. octeon_free_recv_info(recv_info);
  3356. return 0;
  3357. }
  3358. /**
  3359. * \brief Device initialization for each Octeon device that is probed
  3360. * @param octeon_dev octeon device
  3361. */
  3362. static int octeon_device_init(struct octeon_device *octeon_dev)
  3363. {
  3364. int j, ret;
  3365. char bootcmd[] = "\n";
  3366. char *dbg_enb = NULL;
  3367. enum lio_fw_state fw_state;
  3368. struct octeon_device_priv *oct_priv =
  3369. (struct octeon_device_priv *)octeon_dev->priv;
  3370. atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
  3371. /* Enable access to the octeon device and make its DMA capability
  3372. * known to the OS.
  3373. */
  3374. if (octeon_pci_os_setup(octeon_dev))
  3375. return 1;
  3376. atomic_set(&octeon_dev->status, OCT_DEV_PCI_ENABLE_DONE);
  3377. /* Identify the Octeon type and map the BAR address space. */
  3378. if (octeon_chip_specific_setup(octeon_dev)) {
  3379. dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
  3380. return 1;
  3381. }
  3382. atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
  3383. /* Only add a reference after setting status 'OCT_DEV_PCI_MAP_DONE',
  3384. * since that is what is required for the reference to be removed
  3385. * during de-initialization (see 'octeon_destroy_resources').
  3386. */
  3387. octeon_register_device(octeon_dev, octeon_dev->pci_dev->bus->number,
  3388. PCI_SLOT(octeon_dev->pci_dev->devfn),
  3389. PCI_FUNC(octeon_dev->pci_dev->devfn),
  3390. true);
  3391. octeon_dev->app_mode = CVM_DRV_INVALID_APP;
  3392. /* CN23XX supports preloaded firmware if the following is true:
  3393. *
  3394. * The adapter indicates that firmware is currently running AND
  3395. * 'fw_type' is 'auto'.
  3396. *
  3397. * (default state is NEEDS_TO_BE_LOADED, override it if appropriate).
  3398. */
  3399. if (OCTEON_CN23XX_PF(octeon_dev) &&
  3400. cn23xx_fw_loaded(octeon_dev) && fw_type_is_auto()) {
  3401. atomic_cmpxchg(octeon_dev->adapter_fw_state,
  3402. FW_NEEDS_TO_BE_LOADED, FW_IS_PRELOADED);
  3403. }
  3404. /* If loading firmware, only first device of adapter needs to do so. */
  3405. fw_state = atomic_cmpxchg(octeon_dev->adapter_fw_state,
  3406. FW_NEEDS_TO_BE_LOADED,
  3407. FW_IS_BEING_LOADED);
  3408. /* Here, [local variable] 'fw_state' is set to one of:
  3409. *
  3410. * FW_IS_PRELOADED: No firmware is to be loaded (see above)
  3411. * FW_NEEDS_TO_BE_LOADED: The driver's first instance will load
  3412. * firmware to the adapter.
  3413. * FW_IS_BEING_LOADED: The driver's second instance will not load
  3414. * firmware to the adapter.
  3415. */
  3416. /* Prior to f/w load, perform a soft reset of the Octeon device;
  3417. * if error resetting, return w/error.
  3418. */
  3419. if (fw_state == FW_NEEDS_TO_BE_LOADED)
  3420. if (octeon_dev->fn_list.soft_reset(octeon_dev))
  3421. return 1;
  3422. /* Initialize the dispatch mechanism used to push packets arriving on
  3423. * Octeon Output queues.
  3424. */
  3425. if (octeon_init_dispatch_list(octeon_dev))
  3426. return 1;
  3427. octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
  3428. OPCODE_NIC_CORE_DRV_ACTIVE,
  3429. octeon_core_drv_init,
  3430. octeon_dev);
  3431. octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
  3432. OPCODE_NIC_VF_DRV_NOTICE,
  3433. octeon_recv_vf_drv_notice, octeon_dev);
  3434. INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
  3435. octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
  3436. schedule_delayed_work(&octeon_dev->nic_poll_work.work,
  3437. LIQUIDIO_STARTER_POLL_INTERVAL_MS);
  3438. atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
  3439. if (octeon_set_io_queues_off(octeon_dev)) {
  3440. dev_err(&octeon_dev->pci_dev->dev, "setting io queues off failed\n");
  3441. return 1;
  3442. }
  3443. if (OCTEON_CN23XX_PF(octeon_dev)) {
  3444. ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
  3445. if (ret) {
  3446. dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
  3447. return ret;
  3448. }
  3449. }
  3450. /* Initialize soft command buffer pool
  3451. */
  3452. if (octeon_setup_sc_buffer_pool(octeon_dev)) {
  3453. dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
  3454. return 1;
  3455. }
  3456. atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
  3457. /* Setup the data structures that manage this Octeon's Input queues. */
  3458. if (octeon_setup_instr_queues(octeon_dev)) {
  3459. dev_err(&octeon_dev->pci_dev->dev,
  3460. "instruction queue initialization failed\n");
  3461. return 1;
  3462. }
  3463. atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
  3464. /* Initialize lists to manage the requests of different types that
  3465. * arrive from user & kernel applications for this octeon device.
  3466. */
  3467. if (octeon_setup_response_list(octeon_dev)) {
  3468. dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
  3469. return 1;
  3470. }
  3471. atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
  3472. if (octeon_setup_output_queues(octeon_dev)) {
  3473. dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
  3474. return 1;
  3475. }
  3476. atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
  3477. if (OCTEON_CN23XX_PF(octeon_dev)) {
  3478. if (octeon_dev->fn_list.setup_mbox(octeon_dev)) {
  3479. dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Mailbox setup failed\n");
  3480. return 1;
  3481. }
  3482. atomic_set(&octeon_dev->status, OCT_DEV_MBOX_SETUP_DONE);
  3483. if (octeon_allocate_ioq_vector(octeon_dev)) {
  3484. dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
  3485. return 1;
  3486. }
  3487. atomic_set(&octeon_dev->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
  3488. } else {
  3489. /* The input and output queue registers were setup earlier (the
  3490. * queues were not enabled). Any additional registers
  3491. * that need to be programmed should be done now.
  3492. */
  3493. ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
  3494. if (ret) {
  3495. dev_err(&octeon_dev->pci_dev->dev,
  3496. "Failed to configure device registers\n");
  3497. return ret;
  3498. }
  3499. }
  3500. /* Initialize the tasklet that handles output queue packet processing.*/
  3501. dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
  3502. tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
  3503. (unsigned long)octeon_dev);
  3504. /* Setup the interrupt handler and record the INT SUM register address
  3505. */
  3506. if (octeon_setup_interrupt(octeon_dev,
  3507. octeon_dev->sriov_info.num_pf_rings))
  3508. return 1;
  3509. /* Enable Octeon device interrupts */
  3510. octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
  3511. atomic_set(&octeon_dev->status, OCT_DEV_INTR_SET_DONE);
  3512. /* Send Credit for Octeon Output queues. Credits are always sent BEFORE
  3513. * the output queue is enabled.
  3514. * This ensures that we'll receive the f/w CORE DRV_ACTIVE message in
  3515. * case we've configured CN23XX_SLI_GBL_CONTROL[NOPTR_D] = 0.
  3516. * Otherwise, it is possible that the DRV_ACTIVE message will be sent
  3517. * before any credits have been issued, causing the ring to be reset
  3518. * (and the f/w appear to never have started).
  3519. */
  3520. for (j = 0; j < octeon_dev->num_oqs; j++)
  3521. writel(octeon_dev->droq[j]->max_count,
  3522. octeon_dev->droq[j]->pkts_credit_reg);
  3523. /* Enable the input and output queues for this Octeon device */
  3524. ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
  3525. if (ret) {
  3526. dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
  3527. return ret;
  3528. }
  3529. atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
  3530. if (fw_state == FW_NEEDS_TO_BE_LOADED) {
  3531. dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
  3532. if (!ddr_timeout) {
  3533. dev_info(&octeon_dev->pci_dev->dev,
  3534. "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
  3535. }
  3536. schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
  3537. /* Wait for the octeon to initialize DDR after the soft-reset.*/
  3538. while (!ddr_timeout) {
  3539. set_current_state(TASK_INTERRUPTIBLE);
  3540. if (schedule_timeout(HZ / 10)) {
  3541. /* user probably pressed Control-C */
  3542. return 1;
  3543. }
  3544. }
  3545. ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
  3546. if (ret) {
  3547. dev_err(&octeon_dev->pci_dev->dev,
  3548. "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
  3549. ret);
  3550. return 1;
  3551. }
  3552. if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
  3553. dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
  3554. return 1;
  3555. }
  3556. /* Divert uboot to take commands from host instead. */
  3557. ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
  3558. dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
  3559. ret = octeon_init_consoles(octeon_dev);
  3560. if (ret) {
  3561. dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
  3562. return 1;
  3563. }
  3564. /* If console debug enabled, specify empty string to use default
  3565. * enablement ELSE specify NULL string for 'disabled'.
  3566. */
  3567. dbg_enb = octeon_console_debug_enabled(0) ? "" : NULL;
  3568. ret = octeon_add_console(octeon_dev, 0, dbg_enb);
  3569. if (ret) {
  3570. dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
  3571. return 1;
  3572. } else if (octeon_console_debug_enabled(0)) {
  3573. /* If console was added AND we're logging console output
  3574. * then set our console print function.
  3575. */
  3576. octeon_dev->console[0].print = octeon_dbg_console_print;
  3577. }
  3578. atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
  3579. dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
  3580. ret = load_firmware(octeon_dev);
  3581. if (ret) {
  3582. dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
  3583. return 1;
  3584. }
  3585. atomic_set(octeon_dev->adapter_fw_state, FW_HAS_BEEN_LOADED);
  3586. }
  3587. handshake[octeon_dev->octeon_id].init_ok = 1;
  3588. complete(&handshake[octeon_dev->octeon_id].init);
  3589. atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
  3590. return 0;
  3591. }
  3592. /**
  3593. * \brief Debug console print function
  3594. * @param octeon_dev octeon device
  3595. * @param console_num console number
  3596. * @param prefix first portion of line to display
  3597. * @param suffix second portion of line to display
  3598. *
  3599. * The OCTEON debug console outputs entire lines (excluding '\n').
  3600. * Normally, the line will be passed in the 'prefix' parameter.
  3601. * However, due to buffering, it is possible for a line to be split into two
  3602. * parts, in which case they will be passed as the 'prefix' parameter and
  3603. * 'suffix' parameter.
  3604. */
  3605. static int octeon_dbg_console_print(struct octeon_device *oct, u32 console_num,
  3606. char *prefix, char *suffix)
  3607. {
  3608. if (prefix && suffix)
  3609. dev_info(&oct->pci_dev->dev, "%u: %s%s\n", console_num, prefix,
  3610. suffix);
  3611. else if (prefix)
  3612. dev_info(&oct->pci_dev->dev, "%u: %s\n", console_num, prefix);
  3613. else if (suffix)
  3614. dev_info(&oct->pci_dev->dev, "%u: %s\n", console_num, suffix);
  3615. return 0;
  3616. }
  3617. /**
  3618. * \brief Exits the module
  3619. */
  3620. static void __exit liquidio_exit(void)
  3621. {
  3622. liquidio_deinit_pci();
  3623. pr_info("LiquidIO network module is now unloaded\n");
  3624. }
  3625. module_init(liquidio_init);
  3626. module_exit(liquidio_exit);