lio_main.c 112 KB

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