lio_main.c 115 KB

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