lio_main.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665
  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/module.h>
  24. #include <linux/crc32.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/pci.h>
  27. #include <linux/pci_ids.h>
  28. #include <linux/ip.h>
  29. #include <net/ip.h>
  30. #include <linux/ipv6.h>
  31. #include <linux/net_tstamp.h>
  32. #include <linux/if_vlan.h>
  33. #include <linux/firmware.h>
  34. #include <linux/ethtool.h>
  35. #include <linux/ptp_clock_kernel.h>
  36. #include <linux/types.h>
  37. #include <linux/list.h>
  38. #include <linux/workqueue.h>
  39. #include <linux/interrupt.h>
  40. #include "octeon_config.h"
  41. #include "liquidio_common.h"
  42. #include "octeon_droq.h"
  43. #include "octeon_iq.h"
  44. #include "response_manager.h"
  45. #include "octeon_device.h"
  46. #include "octeon_nic.h"
  47. #include "octeon_main.h"
  48. #include "octeon_network.h"
  49. #include "cn66xx_regs.h"
  50. #include "cn66xx_device.h"
  51. #include "cn68xx_regs.h"
  52. #include "cn68xx_device.h"
  53. #include "liquidio_image.h"
  54. MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
  55. MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
  56. MODULE_LICENSE("GPL");
  57. MODULE_VERSION(LIQUIDIO_VERSION);
  58. MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME LIO_FW_NAME_SUFFIX);
  59. MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME LIO_FW_NAME_SUFFIX);
  60. MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME LIO_FW_NAME_SUFFIX);
  61. static int ddr_timeout = 10000;
  62. module_param(ddr_timeout, int, 0644);
  63. MODULE_PARM_DESC(ddr_timeout,
  64. "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
  65. static u32 console_bitmask;
  66. module_param(console_bitmask, int, 0644);
  67. MODULE_PARM_DESC(console_bitmask,
  68. "Bitmask indicating which consoles have debug output redirected to syslog.");
  69. #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
  70. static int debug = -1;
  71. module_param(debug, int, 0644);
  72. MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
  73. static char fw_type[LIO_MAX_FW_TYPE_LEN];
  74. module_param_string(fw_type, fw_type, sizeof(fw_type), 0000);
  75. MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded. Default \"nic\"");
  76. static int conf_type;
  77. module_param(conf_type, int, 0);
  78. MODULE_PARM_DESC(conf_type, "select octeon configuration 0 default 1 ovs");
  79. /* Bit mask values for lio->ifstate */
  80. #define LIO_IFSTATE_DROQ_OPS 0x01
  81. #define LIO_IFSTATE_REGISTERED 0x02
  82. #define LIO_IFSTATE_RUNNING 0x04
  83. #define LIO_IFSTATE_RX_TIMESTAMP_ENABLED 0x08
  84. /* Polling interval for determining when NIC application is alive */
  85. #define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
  86. /* runtime link query interval */
  87. #define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
  88. struct liquidio_if_cfg_context {
  89. int octeon_id;
  90. wait_queue_head_t wc;
  91. int cond;
  92. };
  93. struct liquidio_if_cfg_resp {
  94. u64 rh;
  95. struct liquidio_if_cfg_info cfg_info;
  96. u64 status;
  97. };
  98. struct oct_link_status_resp {
  99. u64 rh;
  100. struct oct_link_info link_info;
  101. u64 status;
  102. };
  103. struct oct_timestamp_resp {
  104. u64 rh;
  105. u64 timestamp;
  106. u64 status;
  107. };
  108. #define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
  109. union tx_info {
  110. u64 u64;
  111. struct {
  112. #ifdef __BIG_ENDIAN_BITFIELD
  113. u16 gso_size;
  114. u16 gso_segs;
  115. u32 reserved;
  116. #else
  117. u32 reserved;
  118. u16 gso_segs;
  119. u16 gso_size;
  120. #endif
  121. } s;
  122. };
  123. /** Octeon device properties to be used by the NIC module.
  124. * Each octeon device in the system will be represented
  125. * by this structure in the NIC module.
  126. */
  127. #define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
  128. #define OCTNIC_GSO_MAX_HEADER_SIZE 128
  129. #define OCTNIC_GSO_MAX_SIZE (GSO_MAX_SIZE - OCTNIC_GSO_MAX_HEADER_SIZE)
  130. /** Structure of a node in list of gather components maintained by
  131. * NIC driver for each network device.
  132. */
  133. struct octnic_gather {
  134. /** List manipulation. Next and prev pointers. */
  135. struct list_head list;
  136. /** Size of the gather component at sg in bytes. */
  137. int sg_size;
  138. /** Number of bytes that sg was adjusted to make it 8B-aligned. */
  139. int adjust;
  140. /** Gather component that can accommodate max sized fragment list
  141. * received from the IP layer.
  142. */
  143. struct octeon_sg_entry *sg;
  144. };
  145. /** This structure is used by NIC driver to store information required
  146. * to free the sk_buff when the packet has been fetched by Octeon.
  147. * Bytes offset below assume worst-case of a 64-bit system.
  148. */
  149. struct octnet_buf_free_info {
  150. /** Bytes 1-8. Pointer to network device private structure. */
  151. struct lio *lio;
  152. /** Bytes 9-16. Pointer to sk_buff. */
  153. struct sk_buff *skb;
  154. /** Bytes 17-24. Pointer to gather list. */
  155. struct octnic_gather *g;
  156. /** Bytes 25-32. Physical address of skb->data or gather list. */
  157. u64 dptr;
  158. /** Bytes 33-47. Piggybacked soft command, if any */
  159. struct octeon_soft_command *sc;
  160. };
  161. struct handshake {
  162. struct completion init;
  163. struct completion started;
  164. struct pci_dev *pci_dev;
  165. int init_ok;
  166. int started_ok;
  167. };
  168. struct octeon_device_priv {
  169. /** Tasklet structures for this device. */
  170. struct tasklet_struct droq_tasklet;
  171. unsigned long napi_mask;
  172. };
  173. static int octeon_device_init(struct octeon_device *);
  174. static void liquidio_remove(struct pci_dev *pdev);
  175. static int liquidio_probe(struct pci_dev *pdev,
  176. const struct pci_device_id *ent);
  177. static struct handshake handshake[MAX_OCTEON_DEVICES];
  178. static struct completion first_stage;
  179. static void octeon_droq_bh(unsigned long pdev)
  180. {
  181. int q_no;
  182. int reschedule = 0;
  183. struct octeon_device *oct = (struct octeon_device *)pdev;
  184. struct octeon_device_priv *oct_priv =
  185. (struct octeon_device_priv *)oct->priv;
  186. /* for (q_no = 0; q_no < oct->num_oqs; q_no++) { */
  187. for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES; q_no++) {
  188. if (!(oct->io_qmask.oq & (1UL << q_no)))
  189. continue;
  190. reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
  191. MAX_PACKET_BUDGET);
  192. }
  193. if (reschedule)
  194. tasklet_schedule(&oct_priv->droq_tasklet);
  195. }
  196. static int lio_wait_for_oq_pkts(struct octeon_device *oct)
  197. {
  198. struct octeon_device_priv *oct_priv =
  199. (struct octeon_device_priv *)oct->priv;
  200. int retry = 100, pkt_cnt = 0, pending_pkts = 0;
  201. int i;
  202. do {
  203. pending_pkts = 0;
  204. for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES; i++) {
  205. if (!(oct->io_qmask.oq & (1UL << i)))
  206. continue;
  207. pkt_cnt += octeon_droq_check_hw_for_pkts(oct,
  208. oct->droq[i]);
  209. }
  210. if (pkt_cnt > 0) {
  211. pending_pkts += pkt_cnt;
  212. tasklet_schedule(&oct_priv->droq_tasklet);
  213. }
  214. pkt_cnt = 0;
  215. schedule_timeout_uninterruptible(1);
  216. } while (retry-- && pending_pkts);
  217. return pkt_cnt;
  218. }
  219. void octeon_report_tx_completion_to_bql(void *txq, unsigned int pkts_compl,
  220. unsigned int bytes_compl)
  221. {
  222. struct netdev_queue *netdev_queue = txq;
  223. netdev_tx_completed_queue(netdev_queue, pkts_compl, bytes_compl);
  224. }
  225. void octeon_update_tx_completion_counters(void *buf, int reqtype,
  226. unsigned int *pkts_compl,
  227. unsigned int *bytes_compl)
  228. {
  229. struct octnet_buf_free_info *finfo;
  230. struct sk_buff *skb = NULL;
  231. struct octeon_soft_command *sc;
  232. switch (reqtype) {
  233. case REQTYPE_NORESP_NET:
  234. case REQTYPE_NORESP_NET_SG:
  235. finfo = buf;
  236. skb = finfo->skb;
  237. break;
  238. case REQTYPE_RESP_NET_SG:
  239. case REQTYPE_RESP_NET:
  240. sc = buf;
  241. skb = sc->callback_arg;
  242. break;
  243. default:
  244. return;
  245. }
  246. (*pkts_compl)++;
  247. *bytes_compl += skb->len;
  248. }
  249. void octeon_report_sent_bytes_to_bql(void *buf, int reqtype)
  250. {
  251. struct octnet_buf_free_info *finfo;
  252. struct sk_buff *skb;
  253. struct octeon_soft_command *sc;
  254. struct netdev_queue *txq;
  255. switch (reqtype) {
  256. case REQTYPE_NORESP_NET:
  257. case REQTYPE_NORESP_NET_SG:
  258. finfo = buf;
  259. skb = finfo->skb;
  260. break;
  261. case REQTYPE_RESP_NET_SG:
  262. case REQTYPE_RESP_NET:
  263. sc = buf;
  264. skb = sc->callback_arg;
  265. break;
  266. default:
  267. return;
  268. }
  269. txq = netdev_get_tx_queue(skb->dev, skb_get_queue_mapping(skb));
  270. netdev_tx_sent_queue(txq, skb->len);
  271. }
  272. int octeon_console_debug_enabled(u32 console)
  273. {
  274. return (console_bitmask >> (console)) & 0x1;
  275. }
  276. /**
  277. * \brief Forces all IO queues off on a given device
  278. * @param oct Pointer to Octeon device
  279. */
  280. static void force_io_queues_off(struct octeon_device *oct)
  281. {
  282. if ((oct->chip_id == OCTEON_CN66XX) ||
  283. (oct->chip_id == OCTEON_CN68XX)) {
  284. /* Reset the Enable bits for Input Queues. */
  285. octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
  286. /* Reset the Enable bits for Output Queues. */
  287. octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
  288. }
  289. }
  290. /**
  291. * \brief wait for all pending requests to complete
  292. * @param oct Pointer to Octeon device
  293. *
  294. * Called during shutdown sequence
  295. */
  296. static int wait_for_pending_requests(struct octeon_device *oct)
  297. {
  298. int i, pcount = 0;
  299. for (i = 0; i < 100; i++) {
  300. pcount =
  301. atomic_read(&oct->response_list
  302. [OCTEON_ORDERED_SC_LIST].pending_req_count);
  303. if (pcount)
  304. schedule_timeout_uninterruptible(HZ / 10);
  305. else
  306. break;
  307. }
  308. if (pcount)
  309. return 1;
  310. return 0;
  311. }
  312. /**
  313. * \brief Cause device to go quiet so it can be safely removed/reset/etc
  314. * @param oct Pointer to Octeon device
  315. */
  316. static inline void pcierror_quiesce_device(struct octeon_device *oct)
  317. {
  318. int i;
  319. /* Disable the input and output queues now. No more packets will
  320. * arrive from Octeon, but we should wait for all packet processing
  321. * to finish.
  322. */
  323. force_io_queues_off(oct);
  324. /* To allow for in-flight requests */
  325. schedule_timeout_uninterruptible(100);
  326. if (wait_for_pending_requests(oct))
  327. dev_err(&oct->pci_dev->dev, "There were pending requests\n");
  328. /* Force all requests waiting to be fetched by OCTEON to complete. */
  329. for (i = 0; i < MAX_OCTEON_INSTR_QUEUES; i++) {
  330. struct octeon_instr_queue *iq;
  331. if (!(oct->io_qmask.iq & (1UL << i)))
  332. continue;
  333. iq = oct->instr_queue[i];
  334. if (atomic_read(&iq->instr_pending)) {
  335. spin_lock_bh(&iq->lock);
  336. iq->fill_cnt = 0;
  337. iq->octeon_read_index = iq->host_write_index;
  338. iq->stats.instr_processed +=
  339. atomic_read(&iq->instr_pending);
  340. lio_process_iq_request_list(oct, iq);
  341. spin_unlock_bh(&iq->lock);
  342. }
  343. }
  344. /* Force all pending ordered list requests to time out. */
  345. lio_process_ordered_list(oct, 1);
  346. /* We do not need to wait for output queue packets to be processed. */
  347. }
  348. /**
  349. * \brief Cleanup PCI AER uncorrectable error status
  350. * @param dev Pointer to PCI device
  351. */
  352. static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
  353. {
  354. int pos = 0x100;
  355. u32 status, mask;
  356. pr_info("%s :\n", __func__);
  357. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
  358. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
  359. if (dev->error_state == pci_channel_io_normal)
  360. status &= ~mask; /* Clear corresponding nonfatal bits */
  361. else
  362. status &= mask; /* Clear corresponding fatal bits */
  363. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
  364. }
  365. /**
  366. * \brief Stop all PCI IO to a given device
  367. * @param dev Pointer to Octeon device
  368. */
  369. static void stop_pci_io(struct octeon_device *oct)
  370. {
  371. /* No more instructions will be forwarded. */
  372. atomic_set(&oct->status, OCT_DEV_IN_RESET);
  373. pci_disable_device(oct->pci_dev);
  374. /* Disable interrupts */
  375. oct->fn_list.disable_interrupt(oct->chip);
  376. pcierror_quiesce_device(oct);
  377. /* Release the interrupt line */
  378. free_irq(oct->pci_dev->irq, oct);
  379. if (oct->flags & LIO_FLAG_MSI_ENABLED)
  380. pci_disable_msi(oct->pci_dev);
  381. dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
  382. lio_get_state_string(&oct->status));
  383. /* cn63xx_cleanup_aer_uncorrect_error_status(oct->pci_dev); */
  384. /* making it a common function for all OCTEON models */
  385. cleanup_aer_uncorrect_error_status(oct->pci_dev);
  386. }
  387. /**
  388. * \brief called when PCI error is detected
  389. * @param pdev Pointer to PCI device
  390. * @param state The current pci connection state
  391. *
  392. * This function is called after a PCI bus error affecting
  393. * this device has been detected.
  394. */
  395. static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
  396. pci_channel_state_t state)
  397. {
  398. struct octeon_device *oct = pci_get_drvdata(pdev);
  399. /* Non-correctable Non-fatal errors */
  400. if (state == pci_channel_io_normal) {
  401. dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
  402. cleanup_aer_uncorrect_error_status(oct->pci_dev);
  403. return PCI_ERS_RESULT_CAN_RECOVER;
  404. }
  405. /* Non-correctable Fatal errors */
  406. dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
  407. stop_pci_io(oct);
  408. /* Always return a DISCONNECT. There is no support for recovery but only
  409. * for a clean shutdown.
  410. */
  411. return PCI_ERS_RESULT_DISCONNECT;
  412. }
  413. /**
  414. * \brief mmio handler
  415. * @param pdev Pointer to PCI device
  416. */
  417. static pci_ers_result_t liquidio_pcie_mmio_enabled(struct pci_dev *pdev)
  418. {
  419. /* We should never hit this since we never ask for a reset for a Fatal
  420. * Error. We always return DISCONNECT in io_error above.
  421. * But play safe and return RECOVERED for now.
  422. */
  423. return PCI_ERS_RESULT_RECOVERED;
  424. }
  425. /**
  426. * \brief called after the pci bus has been reset.
  427. * @param pdev Pointer to PCI device
  428. *
  429. * Restart the card from scratch, as if from a cold-boot. Implementation
  430. * resembles the first-half of the octeon_resume routine.
  431. */
  432. static pci_ers_result_t liquidio_pcie_slot_reset(struct pci_dev *pdev)
  433. {
  434. /* We should never hit this since we never ask for a reset for a Fatal
  435. * Error. We always return DISCONNECT in io_error above.
  436. * But play safe and return RECOVERED for now.
  437. */
  438. return PCI_ERS_RESULT_RECOVERED;
  439. }
  440. /**
  441. * \brief called when traffic can start flowing again.
  442. * @param pdev Pointer to PCI device
  443. *
  444. * This callback is called when the error recovery driver tells us that
  445. * its OK to resume normal operation. Implementation resembles the
  446. * second-half of the octeon_resume routine.
  447. */
  448. static void liquidio_pcie_resume(struct pci_dev *pdev)
  449. {
  450. /* Nothing to be done here. */
  451. }
  452. #ifdef CONFIG_PM
  453. /**
  454. * \brief called when suspending
  455. * @param pdev Pointer to PCI device
  456. * @param state state to suspend to
  457. */
  458. static int liquidio_suspend(struct pci_dev *pdev, pm_message_t state)
  459. {
  460. return 0;
  461. }
  462. /**
  463. * \brief called when resuming
  464. * @param pdev Pointer to PCI device
  465. */
  466. static int liquidio_resume(struct pci_dev *pdev)
  467. {
  468. return 0;
  469. }
  470. #endif
  471. /* For PCI-E Advanced Error Recovery (AER) Interface */
  472. static const struct pci_error_handlers liquidio_err_handler = {
  473. .error_detected = liquidio_pcie_error_detected,
  474. .mmio_enabled = liquidio_pcie_mmio_enabled,
  475. .slot_reset = liquidio_pcie_slot_reset,
  476. .resume = liquidio_pcie_resume,
  477. };
  478. static const struct pci_device_id liquidio_pci_tbl[] = {
  479. { /* 68xx */
  480. PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
  481. },
  482. { /* 66xx */
  483. PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
  484. },
  485. {
  486. 0, 0, 0, 0, 0, 0, 0
  487. }
  488. };
  489. MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
  490. static struct pci_driver liquidio_pci_driver = {
  491. .name = "LiquidIO",
  492. .id_table = liquidio_pci_tbl,
  493. .probe = liquidio_probe,
  494. .remove = liquidio_remove,
  495. .err_handler = &liquidio_err_handler, /* For AER */
  496. #ifdef CONFIG_PM
  497. .suspend = liquidio_suspend,
  498. .resume = liquidio_resume,
  499. #endif
  500. };
  501. /**
  502. * \brief register PCI driver
  503. */
  504. static int liquidio_init_pci(void)
  505. {
  506. return pci_register_driver(&liquidio_pci_driver);
  507. }
  508. /**
  509. * \brief unregister PCI driver
  510. */
  511. static void liquidio_deinit_pci(void)
  512. {
  513. pci_unregister_driver(&liquidio_pci_driver);
  514. }
  515. /**
  516. * \brief check interface state
  517. * @param lio per-network private data
  518. * @param state_flag flag state to check
  519. */
  520. static inline int ifstate_check(struct lio *lio, int state_flag)
  521. {
  522. return atomic_read(&lio->ifstate) & state_flag;
  523. }
  524. /**
  525. * \brief set interface state
  526. * @param lio per-network private data
  527. * @param state_flag flag state to set
  528. */
  529. static inline void ifstate_set(struct lio *lio, int state_flag)
  530. {
  531. atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) | state_flag));
  532. }
  533. /**
  534. * \brief clear interface state
  535. * @param lio per-network private data
  536. * @param state_flag flag state to clear
  537. */
  538. static inline void ifstate_reset(struct lio *lio, int state_flag)
  539. {
  540. atomic_set(&lio->ifstate, (atomic_read(&lio->ifstate) & ~(state_flag)));
  541. }
  542. /**
  543. * \brief Stop Tx queues
  544. * @param netdev network device
  545. */
  546. static inline void txqs_stop(struct net_device *netdev)
  547. {
  548. if (netif_is_multiqueue(netdev)) {
  549. int i;
  550. for (i = 0; i < netdev->num_tx_queues; i++)
  551. netif_stop_subqueue(netdev, i);
  552. } else {
  553. netif_stop_queue(netdev);
  554. }
  555. }
  556. /**
  557. * \brief Start Tx queues
  558. * @param netdev network device
  559. */
  560. static inline void txqs_start(struct net_device *netdev)
  561. {
  562. if (netif_is_multiqueue(netdev)) {
  563. int i;
  564. for (i = 0; i < netdev->num_tx_queues; i++)
  565. netif_start_subqueue(netdev, i);
  566. } else {
  567. netif_start_queue(netdev);
  568. }
  569. }
  570. /**
  571. * \brief Wake Tx queues
  572. * @param netdev network device
  573. */
  574. static inline void txqs_wake(struct net_device *netdev)
  575. {
  576. if (netif_is_multiqueue(netdev)) {
  577. int i;
  578. for (i = 0; i < netdev->num_tx_queues; i++)
  579. netif_wake_subqueue(netdev, i);
  580. } else {
  581. netif_wake_queue(netdev);
  582. }
  583. }
  584. /**
  585. * \brief Stop Tx queue
  586. * @param netdev network device
  587. */
  588. static void stop_txq(struct net_device *netdev)
  589. {
  590. txqs_stop(netdev);
  591. }
  592. /**
  593. * \brief Start Tx queue
  594. * @param netdev network device
  595. */
  596. static void start_txq(struct net_device *netdev)
  597. {
  598. struct lio *lio = GET_LIO(netdev);
  599. if (lio->linfo.link.s.status) {
  600. txqs_start(netdev);
  601. return;
  602. }
  603. }
  604. /**
  605. * \brief Wake a queue
  606. * @param netdev network device
  607. * @param q which queue to wake
  608. */
  609. static inline void wake_q(struct net_device *netdev, int q)
  610. {
  611. if (netif_is_multiqueue(netdev))
  612. netif_wake_subqueue(netdev, q);
  613. else
  614. netif_wake_queue(netdev);
  615. }
  616. /**
  617. * \brief Stop a queue
  618. * @param netdev network device
  619. * @param q which queue to stop
  620. */
  621. static inline void stop_q(struct net_device *netdev, int q)
  622. {
  623. if (netif_is_multiqueue(netdev))
  624. netif_stop_subqueue(netdev, q);
  625. else
  626. netif_stop_queue(netdev);
  627. }
  628. /**
  629. * \brief Check Tx queue status, and take appropriate action
  630. * @param lio per-network private data
  631. * @returns 0 if full, number of queues woken up otherwise
  632. */
  633. static inline int check_txq_status(struct lio *lio)
  634. {
  635. int ret_val = 0;
  636. if (netif_is_multiqueue(lio->netdev)) {
  637. int numqs = lio->netdev->num_tx_queues;
  638. int q, iq = 0;
  639. /* check each sub-queue state */
  640. for (q = 0; q < numqs; q++) {
  641. iq = lio->linfo.txpciq[q & (lio->linfo.num_txpciq - 1)];
  642. if (octnet_iq_is_full(lio->oct_dev, iq))
  643. continue;
  644. wake_q(lio->netdev, q);
  645. ret_val++;
  646. }
  647. } else {
  648. if (octnet_iq_is_full(lio->oct_dev, lio->txq))
  649. return 0;
  650. wake_q(lio->netdev, lio->txq);
  651. ret_val = 1;
  652. }
  653. return ret_val;
  654. }
  655. /**
  656. * Remove the node at the head of the list. The list would be empty at
  657. * the end of this call if there are no more nodes in the list.
  658. */
  659. static inline struct list_head *list_delete_head(struct list_head *root)
  660. {
  661. struct list_head *node;
  662. if ((root->prev == root) && (root->next == root))
  663. node = NULL;
  664. else
  665. node = root->next;
  666. if (node)
  667. list_del(node);
  668. return node;
  669. }
  670. /**
  671. * \brief Delete gather list
  672. * @param lio per-network private data
  673. */
  674. static void delete_glist(struct lio *lio)
  675. {
  676. struct octnic_gather *g;
  677. do {
  678. g = (struct octnic_gather *)
  679. list_delete_head(&lio->glist);
  680. if (g) {
  681. if (g->sg)
  682. kfree((void *)((unsigned long)g->sg -
  683. g->adjust));
  684. kfree(g);
  685. }
  686. } while (g);
  687. }
  688. /**
  689. * \brief Setup gather list
  690. * @param lio per-network private data
  691. */
  692. static int setup_glist(struct lio *lio)
  693. {
  694. int i;
  695. struct octnic_gather *g;
  696. INIT_LIST_HEAD(&lio->glist);
  697. for (i = 0; i < lio->tx_qsize; i++) {
  698. g = kzalloc(sizeof(*g), GFP_KERNEL);
  699. if (!g)
  700. break;
  701. g->sg_size =
  702. ((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
  703. g->sg = kmalloc(g->sg_size + 8, GFP_KERNEL);
  704. if (!g->sg) {
  705. kfree(g);
  706. break;
  707. }
  708. /* The gather component should be aligned on 64-bit boundary */
  709. if (((unsigned long)g->sg) & 7) {
  710. g->adjust = 8 - (((unsigned long)g->sg) & 7);
  711. g->sg = (struct octeon_sg_entry *)
  712. ((unsigned long)g->sg + g->adjust);
  713. }
  714. list_add_tail(&g->list, &lio->glist);
  715. }
  716. if (i == lio->tx_qsize)
  717. return 0;
  718. delete_glist(lio);
  719. return 1;
  720. }
  721. /**
  722. * \brief Print link information
  723. * @param netdev network device
  724. */
  725. static void print_link_info(struct net_device *netdev)
  726. {
  727. struct lio *lio = GET_LIO(netdev);
  728. if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
  729. struct oct_link_info *linfo = &lio->linfo;
  730. if (linfo->link.s.status) {
  731. netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
  732. linfo->link.s.speed,
  733. (linfo->link.s.duplex) ? "Full" : "Half");
  734. } else {
  735. netif_info(lio, link, lio->netdev, "Link Down\n");
  736. }
  737. }
  738. }
  739. /**
  740. * \brief Update link status
  741. * @param netdev network device
  742. * @param ls link status structure
  743. *
  744. * Called on receipt of a link status response from the core application to
  745. * update each interface's link status.
  746. */
  747. static inline void update_link_status(struct net_device *netdev,
  748. union oct_link_status *ls)
  749. {
  750. struct lio *lio = GET_LIO(netdev);
  751. if ((lio->intf_open) && (lio->linfo.link.u64 != ls->u64)) {
  752. lio->linfo.link.u64 = ls->u64;
  753. print_link_info(netdev);
  754. if (lio->linfo.link.s.status) {
  755. netif_carrier_on(netdev);
  756. /* start_txq(netdev); */
  757. txqs_wake(netdev);
  758. } else {
  759. netif_carrier_off(netdev);
  760. stop_txq(netdev);
  761. }
  762. }
  763. }
  764. /**
  765. * \brief Droq packet processor sceduler
  766. * @param oct octeon device
  767. */
  768. static
  769. void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
  770. {
  771. struct octeon_device_priv *oct_priv =
  772. (struct octeon_device_priv *)oct->priv;
  773. u64 oq_no;
  774. struct octeon_droq *droq;
  775. if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
  776. for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES; oq_no++) {
  777. if (!(oct->droq_intr & (1 << oq_no)))
  778. continue;
  779. droq = oct->droq[oq_no];
  780. if (droq->ops.poll_mode) {
  781. droq->ops.napi_fn(droq);
  782. oct_priv->napi_mask |= (1 << oq_no);
  783. } else {
  784. tasklet_schedule(&oct_priv->droq_tasklet);
  785. }
  786. }
  787. }
  788. }
  789. /**
  790. * \brief Interrupt handler for octeon
  791. * @param irq unused
  792. * @param dev octeon device
  793. */
  794. static
  795. irqreturn_t liquidio_intr_handler(int irq __attribute__((unused)), void *dev)
  796. {
  797. struct octeon_device *oct = (struct octeon_device *)dev;
  798. irqreturn_t ret;
  799. /* Disable our interrupts for the duration of ISR */
  800. oct->fn_list.disable_interrupt(oct->chip);
  801. ret = oct->fn_list.process_interrupt_regs(oct);
  802. if (ret == IRQ_HANDLED)
  803. liquidio_schedule_droq_pkt_handlers(oct);
  804. /* Re-enable our interrupts */
  805. if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
  806. oct->fn_list.enable_interrupt(oct->chip);
  807. return ret;
  808. }
  809. /**
  810. * \brief Setup interrupt for octeon device
  811. * @param oct octeon device
  812. *
  813. * Enable interrupt in Octeon device as given in the PCI interrupt mask.
  814. */
  815. static int octeon_setup_interrupt(struct octeon_device *oct)
  816. {
  817. int irqret, err;
  818. err = pci_enable_msi(oct->pci_dev);
  819. if (err)
  820. dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
  821. err);
  822. else
  823. oct->flags |= LIO_FLAG_MSI_ENABLED;
  824. irqret = request_irq(oct->pci_dev->irq, liquidio_intr_handler,
  825. IRQF_SHARED, "octeon", oct);
  826. if (irqret) {
  827. if (oct->flags & LIO_FLAG_MSI_ENABLED)
  828. pci_disable_msi(oct->pci_dev);
  829. dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
  830. irqret);
  831. return 1;
  832. }
  833. return 0;
  834. }
  835. /**
  836. * \brief PCI probe handler
  837. * @param pdev PCI device structure
  838. * @param ent unused
  839. */
  840. static int liquidio_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  841. {
  842. struct octeon_device *oct_dev = NULL;
  843. struct handshake *hs;
  844. oct_dev = octeon_allocate_device(pdev->device,
  845. sizeof(struct octeon_device_priv));
  846. if (!oct_dev) {
  847. dev_err(&pdev->dev, "Unable to allocate device\n");
  848. return -ENOMEM;
  849. }
  850. dev_info(&pdev->dev, "Initializing device %x:%x.\n",
  851. (u32)pdev->vendor, (u32)pdev->device);
  852. /* Assign octeon_device for this device to the private data area. */
  853. pci_set_drvdata(pdev, oct_dev);
  854. /* set linux specific device pointer */
  855. oct_dev->pci_dev = (void *)pdev;
  856. hs = &handshake[oct_dev->octeon_id];
  857. init_completion(&hs->init);
  858. init_completion(&hs->started);
  859. hs->pci_dev = pdev;
  860. if (oct_dev->octeon_id == 0)
  861. /* first LiquidIO NIC is detected */
  862. complete(&first_stage);
  863. if (octeon_device_init(oct_dev)) {
  864. liquidio_remove(pdev);
  865. return -ENOMEM;
  866. }
  867. dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
  868. return 0;
  869. }
  870. /**
  871. *\brief Destroy resources associated with octeon device
  872. * @param pdev PCI device structure
  873. * @param ent unused
  874. */
  875. static void octeon_destroy_resources(struct octeon_device *oct)
  876. {
  877. int i;
  878. struct octeon_device_priv *oct_priv =
  879. (struct octeon_device_priv *)oct->priv;
  880. struct handshake *hs;
  881. switch (atomic_read(&oct->status)) {
  882. case OCT_DEV_RUNNING:
  883. case OCT_DEV_CORE_OK:
  884. /* No more instructions will be forwarded. */
  885. atomic_set(&oct->status, OCT_DEV_IN_RESET);
  886. oct->app_mode = CVM_DRV_INVALID_APP;
  887. dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
  888. lio_get_state_string(&oct->status));
  889. schedule_timeout_uninterruptible(HZ / 10);
  890. /* fallthrough */
  891. case OCT_DEV_HOST_OK:
  892. /* fallthrough */
  893. case OCT_DEV_CONSOLE_INIT_DONE:
  894. /* Remove any consoles */
  895. octeon_remove_consoles(oct);
  896. /* fallthrough */
  897. case OCT_DEV_IO_QUEUES_DONE:
  898. if (wait_for_pending_requests(oct))
  899. dev_err(&oct->pci_dev->dev, "There were pending requests\n");
  900. if (lio_wait_for_instr_fetch(oct))
  901. dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
  902. /* Disable the input and output queues now. No more packets will
  903. * arrive from Octeon, but we should wait for all packet
  904. * processing to finish.
  905. */
  906. oct->fn_list.disable_io_queues(oct);
  907. if (lio_wait_for_oq_pkts(oct))
  908. dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
  909. /* Disable interrupts */
  910. oct->fn_list.disable_interrupt(oct->chip);
  911. /* Release the interrupt line */
  912. free_irq(oct->pci_dev->irq, oct);
  913. if (oct->flags & LIO_FLAG_MSI_ENABLED)
  914. pci_disable_msi(oct->pci_dev);
  915. /* Soft reset the octeon device before exiting */
  916. oct->fn_list.soft_reset(oct);
  917. /* Disable the device, releasing the PCI INT */
  918. pci_disable_device(oct->pci_dev);
  919. /* fallthrough */
  920. case OCT_DEV_IN_RESET:
  921. case OCT_DEV_DROQ_INIT_DONE:
  922. /*atomic_set(&oct->status, OCT_DEV_DROQ_INIT_DONE);*/
  923. mdelay(100);
  924. for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES; i++) {
  925. if (!(oct->io_qmask.oq & (1UL << i)))
  926. continue;
  927. octeon_delete_droq(oct, i);
  928. }
  929. /* Force any pending handshakes to complete */
  930. for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
  931. hs = &handshake[i];
  932. if (hs->pci_dev) {
  933. handshake[oct->octeon_id].init_ok = 0;
  934. complete(&handshake[oct->octeon_id].init);
  935. handshake[oct->octeon_id].started_ok = 0;
  936. complete(&handshake[oct->octeon_id].started);
  937. }
  938. }
  939. /* fallthrough */
  940. case OCT_DEV_RESP_LIST_INIT_DONE:
  941. octeon_delete_response_list(oct);
  942. /* fallthrough */
  943. case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
  944. octeon_free_sc_buffer_pool(oct);
  945. /* fallthrough */
  946. case OCT_DEV_INSTR_QUEUE_INIT_DONE:
  947. for (i = 0; i < MAX_OCTEON_INSTR_QUEUES; i++) {
  948. if (!(oct->io_qmask.iq & (1UL << i)))
  949. continue;
  950. octeon_delete_instr_queue(oct, i);
  951. }
  952. /* fallthrough */
  953. case OCT_DEV_DISPATCH_INIT_DONE:
  954. octeon_delete_dispatch_list(oct);
  955. cancel_delayed_work_sync(&oct->nic_poll_work.work);
  956. /* fallthrough */
  957. case OCT_DEV_PCI_MAP_DONE:
  958. octeon_unmap_pci_barx(oct, 0);
  959. octeon_unmap_pci_barx(oct, 1);
  960. /* fallthrough */
  961. case OCT_DEV_BEGIN_STATE:
  962. /* Nothing to be done here either */
  963. break;
  964. } /* end switch(oct->status) */
  965. tasklet_kill(&oct_priv->droq_tasklet);
  966. }
  967. /**
  968. * \brief Send Rx control command
  969. * @param lio per-network private data
  970. * @param start_stop whether to start or stop
  971. */
  972. static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
  973. {
  974. struct octnic_ctrl_pkt nctrl;
  975. struct octnic_ctrl_params nparams;
  976. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  977. nctrl.ncmd.s.cmd = OCTNET_CMD_RX_CTL;
  978. nctrl.ncmd.s.param1 = lio->linfo.ifidx;
  979. nctrl.ncmd.s.param2 = start_stop;
  980. nctrl.netpndev = (u64)lio->netdev;
  981. nparams.resp_order = OCTEON_RESP_NORESPONSE;
  982. if (octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl, nparams) < 0)
  983. netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
  984. }
  985. /**
  986. * \brief Destroy NIC device interface
  987. * @param oct octeon device
  988. * @param ifidx which interface to destroy
  989. *
  990. * Cleanup associated with each interface for an Octeon device when NIC
  991. * module is being unloaded or if initialization fails during load.
  992. */
  993. static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
  994. {
  995. struct net_device *netdev = oct->props[ifidx].netdev;
  996. struct lio *lio;
  997. if (!netdev) {
  998. dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
  999. __func__, ifidx);
  1000. return;
  1001. }
  1002. lio = GET_LIO(netdev);
  1003. dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
  1004. send_rx_ctrl_cmd(lio, 0);
  1005. if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
  1006. txqs_stop(netdev);
  1007. if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
  1008. unregister_netdev(netdev);
  1009. delete_glist(lio);
  1010. free_netdev(netdev);
  1011. oct->props[ifidx].netdev = NULL;
  1012. }
  1013. /**
  1014. * \brief Stop complete NIC functionality
  1015. * @param oct octeon device
  1016. */
  1017. static int liquidio_stop_nic_module(struct octeon_device *oct)
  1018. {
  1019. int i, j;
  1020. struct lio *lio;
  1021. dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
  1022. if (!oct->ifcount) {
  1023. dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
  1024. return 1;
  1025. }
  1026. for (i = 0; i < oct->ifcount; i++) {
  1027. lio = GET_LIO(oct->props[i].netdev);
  1028. for (j = 0; j < lio->linfo.num_rxpciq; j++)
  1029. octeon_unregister_droq_ops(oct, lio->linfo.rxpciq[j]);
  1030. }
  1031. for (i = 0; i < oct->ifcount; i++)
  1032. liquidio_destroy_nic_device(oct, i);
  1033. dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
  1034. return 0;
  1035. }
  1036. /**
  1037. * \brief Cleans up resources at unload time
  1038. * @param pdev PCI device structure
  1039. */
  1040. static void liquidio_remove(struct pci_dev *pdev)
  1041. {
  1042. struct octeon_device *oct_dev = pci_get_drvdata(pdev);
  1043. dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
  1044. if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
  1045. liquidio_stop_nic_module(oct_dev);
  1046. /* Reset the octeon device and cleanup all memory allocated for
  1047. * the octeon device by driver.
  1048. */
  1049. octeon_destroy_resources(oct_dev);
  1050. dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
  1051. /* This octeon device has been removed. Update the global
  1052. * data structure to reflect this. Free the device structure.
  1053. */
  1054. octeon_free_device_mem(oct_dev);
  1055. }
  1056. /**
  1057. * \brief Identify the Octeon device and to map the BAR address space
  1058. * @param oct octeon device
  1059. */
  1060. static int octeon_chip_specific_setup(struct octeon_device *oct)
  1061. {
  1062. u32 dev_id, rev_id;
  1063. int ret = 1;
  1064. pci_read_config_dword(oct->pci_dev, 0, &dev_id);
  1065. pci_read_config_dword(oct->pci_dev, 8, &rev_id);
  1066. oct->rev_id = rev_id & 0xff;
  1067. switch (dev_id) {
  1068. case OCTEON_CN68XX_PCIID:
  1069. oct->chip_id = OCTEON_CN68XX;
  1070. ret = lio_setup_cn68xx_octeon_device(oct);
  1071. break;
  1072. case OCTEON_CN66XX_PCIID:
  1073. oct->chip_id = OCTEON_CN66XX;
  1074. ret = lio_setup_cn66xx_octeon_device(oct);
  1075. break;
  1076. default:
  1077. dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
  1078. dev_id);
  1079. }
  1080. if (!ret)
  1081. dev_info(&oct->pci_dev->dev, "CN68XX PASS%d.%d %s\n",
  1082. OCTEON_MAJOR_REV(oct),
  1083. OCTEON_MINOR_REV(oct),
  1084. octeon_get_conf(oct)->card_name);
  1085. return ret;
  1086. }
  1087. /**
  1088. * \brief PCI initialization for each Octeon device.
  1089. * @param oct octeon device
  1090. */
  1091. static int octeon_pci_os_setup(struct octeon_device *oct)
  1092. {
  1093. /* setup PCI stuff first */
  1094. if (pci_enable_device(oct->pci_dev)) {
  1095. dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
  1096. return 1;
  1097. }
  1098. if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
  1099. dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
  1100. return 1;
  1101. }
  1102. /* Enable PCI DMA Master. */
  1103. pci_set_master(oct->pci_dev);
  1104. return 0;
  1105. }
  1106. /**
  1107. * \brief Check Tx queue state for a given network buffer
  1108. * @param lio per-network private data
  1109. * @param skb network buffer
  1110. */
  1111. static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
  1112. {
  1113. int q = 0, iq = 0;
  1114. if (netif_is_multiqueue(lio->netdev)) {
  1115. q = skb->queue_mapping;
  1116. iq = lio->linfo.txpciq[(q & (lio->linfo.num_txpciq - 1))];
  1117. } else {
  1118. iq = lio->txq;
  1119. }
  1120. if (octnet_iq_is_full(lio->oct_dev, iq))
  1121. return 0;
  1122. wake_q(lio->netdev, q);
  1123. return 1;
  1124. }
  1125. /**
  1126. * \brief Unmap and free network buffer
  1127. * @param buf buffer
  1128. */
  1129. static void free_netbuf(void *buf)
  1130. {
  1131. struct sk_buff *skb;
  1132. struct octnet_buf_free_info *finfo;
  1133. struct lio *lio;
  1134. finfo = (struct octnet_buf_free_info *)buf;
  1135. skb = finfo->skb;
  1136. lio = finfo->lio;
  1137. dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
  1138. DMA_TO_DEVICE);
  1139. check_txq_state(lio, skb);
  1140. recv_buffer_free((struct sk_buff *)skb);
  1141. }
  1142. /**
  1143. * \brief Unmap and free gather buffer
  1144. * @param buf buffer
  1145. */
  1146. static void free_netsgbuf(void *buf)
  1147. {
  1148. struct octnet_buf_free_info *finfo;
  1149. struct sk_buff *skb;
  1150. struct lio *lio;
  1151. struct octnic_gather *g;
  1152. int i, frags;
  1153. finfo = (struct octnet_buf_free_info *)buf;
  1154. skb = finfo->skb;
  1155. lio = finfo->lio;
  1156. g = finfo->g;
  1157. frags = skb_shinfo(skb)->nr_frags;
  1158. dma_unmap_single(&lio->oct_dev->pci_dev->dev,
  1159. g->sg[0].ptr[0], (skb->len - skb->data_len),
  1160. DMA_TO_DEVICE);
  1161. i = 1;
  1162. while (frags--) {
  1163. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
  1164. pci_unmap_page((lio->oct_dev)->pci_dev,
  1165. g->sg[(i >> 2)].ptr[(i & 3)],
  1166. frag->size, DMA_TO_DEVICE);
  1167. i++;
  1168. }
  1169. dma_unmap_single(&lio->oct_dev->pci_dev->dev,
  1170. finfo->dptr, g->sg_size,
  1171. DMA_TO_DEVICE);
  1172. spin_lock(&lio->lock);
  1173. list_add_tail(&g->list, &lio->glist);
  1174. spin_unlock(&lio->lock);
  1175. check_txq_state(lio, skb); /* mq support: sub-queue state check */
  1176. recv_buffer_free((struct sk_buff *)skb);
  1177. }
  1178. /**
  1179. * \brief Unmap and free gather buffer with response
  1180. * @param buf buffer
  1181. */
  1182. static void free_netsgbuf_with_resp(void *buf)
  1183. {
  1184. struct octeon_soft_command *sc;
  1185. struct octnet_buf_free_info *finfo;
  1186. struct sk_buff *skb;
  1187. struct lio *lio;
  1188. struct octnic_gather *g;
  1189. int i, frags;
  1190. sc = (struct octeon_soft_command *)buf;
  1191. skb = (struct sk_buff *)sc->callback_arg;
  1192. finfo = (struct octnet_buf_free_info *)&skb->cb;
  1193. lio = finfo->lio;
  1194. g = finfo->g;
  1195. frags = skb_shinfo(skb)->nr_frags;
  1196. dma_unmap_single(&lio->oct_dev->pci_dev->dev,
  1197. g->sg[0].ptr[0], (skb->len - skb->data_len),
  1198. DMA_TO_DEVICE);
  1199. i = 1;
  1200. while (frags--) {
  1201. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
  1202. pci_unmap_page((lio->oct_dev)->pci_dev,
  1203. g->sg[(i >> 2)].ptr[(i & 3)],
  1204. frag->size, DMA_TO_DEVICE);
  1205. i++;
  1206. }
  1207. dma_unmap_single(&lio->oct_dev->pci_dev->dev,
  1208. finfo->dptr, g->sg_size,
  1209. DMA_TO_DEVICE);
  1210. spin_lock(&lio->lock);
  1211. list_add_tail(&g->list, &lio->glist);
  1212. spin_unlock(&lio->lock);
  1213. /* Don't free the skb yet */
  1214. check_txq_state(lio, skb);
  1215. }
  1216. /**
  1217. * \brief Adjust ptp frequency
  1218. * @param ptp PTP clock info
  1219. * @param ppb how much to adjust by, in parts-per-billion
  1220. */
  1221. static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  1222. {
  1223. struct lio *lio = container_of(ptp, struct lio, ptp_info);
  1224. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  1225. u64 comp, delta;
  1226. unsigned long flags;
  1227. bool neg_adj = false;
  1228. if (ppb < 0) {
  1229. neg_adj = true;
  1230. ppb = -ppb;
  1231. }
  1232. /* The hardware adds the clock compensation value to the
  1233. * PTP clock on every coprocessor clock cycle, so we
  1234. * compute the delta in terms of coprocessor clocks.
  1235. */
  1236. delta = (u64)ppb << 32;
  1237. do_div(delta, oct->coproc_clock_rate);
  1238. spin_lock_irqsave(&lio->ptp_lock, flags);
  1239. comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
  1240. if (neg_adj)
  1241. comp -= delta;
  1242. else
  1243. comp += delta;
  1244. lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
  1245. spin_unlock_irqrestore(&lio->ptp_lock, flags);
  1246. return 0;
  1247. }
  1248. /**
  1249. * \brief Adjust ptp time
  1250. * @param ptp PTP clock info
  1251. * @param delta how much to adjust by, in nanosecs
  1252. */
  1253. static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  1254. {
  1255. unsigned long flags;
  1256. struct lio *lio = container_of(ptp, struct lio, ptp_info);
  1257. spin_lock_irqsave(&lio->ptp_lock, flags);
  1258. lio->ptp_adjust += delta;
  1259. spin_unlock_irqrestore(&lio->ptp_lock, flags);
  1260. return 0;
  1261. }
  1262. /**
  1263. * \brief Get hardware clock time, including any adjustment
  1264. * @param ptp PTP clock info
  1265. * @param ts timespec
  1266. */
  1267. static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
  1268. struct timespec64 *ts)
  1269. {
  1270. u64 ns;
  1271. unsigned long flags;
  1272. struct lio *lio = container_of(ptp, struct lio, ptp_info);
  1273. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  1274. spin_lock_irqsave(&lio->ptp_lock, flags);
  1275. ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
  1276. ns += lio->ptp_adjust;
  1277. spin_unlock_irqrestore(&lio->ptp_lock, flags);
  1278. *ts = ns_to_timespec64(ns);
  1279. return 0;
  1280. }
  1281. /**
  1282. * \brief Set hardware clock time. Reset adjustment
  1283. * @param ptp PTP clock info
  1284. * @param ts timespec
  1285. */
  1286. static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
  1287. const struct timespec64 *ts)
  1288. {
  1289. u64 ns;
  1290. unsigned long flags;
  1291. struct lio *lio = container_of(ptp, struct lio, ptp_info);
  1292. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  1293. ns = timespec_to_ns(ts);
  1294. spin_lock_irqsave(&lio->ptp_lock, flags);
  1295. lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
  1296. lio->ptp_adjust = 0;
  1297. spin_unlock_irqrestore(&lio->ptp_lock, flags);
  1298. return 0;
  1299. }
  1300. /**
  1301. * \brief Check if PTP is enabled
  1302. * @param ptp PTP clock info
  1303. * @param rq request
  1304. * @param on is it on
  1305. */
  1306. static int liquidio_ptp_enable(struct ptp_clock_info *ptp,
  1307. struct ptp_clock_request *rq, int on)
  1308. {
  1309. return -EOPNOTSUPP;
  1310. }
  1311. /**
  1312. * \brief Open PTP clock source
  1313. * @param netdev network device
  1314. */
  1315. static void oct_ptp_open(struct net_device *netdev)
  1316. {
  1317. struct lio *lio = GET_LIO(netdev);
  1318. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  1319. spin_lock_init(&lio->ptp_lock);
  1320. snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
  1321. lio->ptp_info.owner = THIS_MODULE;
  1322. lio->ptp_info.max_adj = 250000000;
  1323. lio->ptp_info.n_alarm = 0;
  1324. lio->ptp_info.n_ext_ts = 0;
  1325. lio->ptp_info.n_per_out = 0;
  1326. lio->ptp_info.pps = 0;
  1327. lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
  1328. lio->ptp_info.adjtime = liquidio_ptp_adjtime;
  1329. lio->ptp_info.gettime64 = liquidio_ptp_gettime;
  1330. lio->ptp_info.settime64 = liquidio_ptp_settime;
  1331. lio->ptp_info.enable = liquidio_ptp_enable;
  1332. lio->ptp_adjust = 0;
  1333. lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
  1334. &oct->pci_dev->dev);
  1335. if (IS_ERR(lio->ptp_clock))
  1336. lio->ptp_clock = NULL;
  1337. }
  1338. /**
  1339. * \brief Init PTP clock
  1340. * @param oct octeon device
  1341. */
  1342. static void liquidio_ptp_init(struct octeon_device *oct)
  1343. {
  1344. u64 clock_comp, cfg;
  1345. clock_comp = (u64)NSEC_PER_SEC << 32;
  1346. do_div(clock_comp, oct->coproc_clock_rate);
  1347. lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
  1348. /* Enable */
  1349. cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
  1350. lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
  1351. }
  1352. /**
  1353. * \brief Load firmware to device
  1354. * @param oct octeon device
  1355. *
  1356. * Maps device to firmware filename, requests firmware, and downloads it
  1357. */
  1358. static int load_firmware(struct octeon_device *oct)
  1359. {
  1360. int ret = 0;
  1361. const struct firmware *fw;
  1362. char fw_name[LIO_MAX_FW_FILENAME_LEN];
  1363. char *tmp_fw_type;
  1364. if (strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
  1365. sizeof(LIO_FW_NAME_TYPE_NONE)) == 0) {
  1366. dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
  1367. return ret;
  1368. }
  1369. if (fw_type[0] == '\0')
  1370. tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
  1371. else
  1372. tmp_fw_type = fw_type;
  1373. sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
  1374. octeon_get_conf(oct)->card_name, tmp_fw_type,
  1375. LIO_FW_NAME_SUFFIX);
  1376. ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
  1377. if (ret) {
  1378. dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
  1379. fw_name);
  1380. return ret;
  1381. }
  1382. ret = octeon_download_firmware(oct, fw->data, fw->size);
  1383. release_firmware(fw);
  1384. return ret;
  1385. }
  1386. /**
  1387. * \brief Setup output queue
  1388. * @param oct octeon device
  1389. * @param q_no which queue
  1390. * @param num_descs how many descriptors
  1391. * @param desc_size size of each descriptor
  1392. * @param app_ctx application context
  1393. */
  1394. static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
  1395. int desc_size, void *app_ctx)
  1396. {
  1397. int ret_val = 0;
  1398. dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
  1399. /* droq creation and local register settings. */
  1400. ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
  1401. if (ret_val < 0)
  1402. return ret_val;
  1403. if (ret_val == 1) {
  1404. dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
  1405. return 0;
  1406. }
  1407. /* tasklet creation for the droq */
  1408. /* Enable the droq queues */
  1409. octeon_set_droq_pkt_op(oct, q_no, 1);
  1410. /* Send Credit for Octeon Output queues. Credits are always
  1411. * sent after the output queue is enabled.
  1412. */
  1413. writel(oct->droq[q_no]->max_count,
  1414. oct->droq[q_no]->pkts_credit_reg);
  1415. return ret_val;
  1416. }
  1417. /**
  1418. * \brief Callback for getting interface configuration
  1419. * @param status status of request
  1420. * @param buf pointer to resp structure
  1421. */
  1422. static void if_cfg_callback(struct octeon_device *oct,
  1423. u32 status,
  1424. void *buf)
  1425. {
  1426. struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
  1427. struct liquidio_if_cfg_resp *resp;
  1428. struct liquidio_if_cfg_context *ctx;
  1429. resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
  1430. ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
  1431. oct = lio_get_device(ctx->octeon_id);
  1432. if (resp->status)
  1433. dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: %llx\n",
  1434. CVM_CAST64(resp->status));
  1435. ACCESS_ONCE(ctx->cond) = 1;
  1436. /* This barrier is required to be sure that the response has been
  1437. * written fully before waking up the handler
  1438. */
  1439. wmb();
  1440. wake_up_interruptible(&ctx->wc);
  1441. }
  1442. /**
  1443. * \brief Select queue based on hash
  1444. * @param dev Net device
  1445. * @param skb sk_buff structure
  1446. * @returns selected queue number
  1447. */
  1448. static u16 select_q(struct net_device *dev, struct sk_buff *skb,
  1449. void *accel_priv, select_queue_fallback_t fallback)
  1450. {
  1451. int qindex;
  1452. struct lio *lio;
  1453. lio = GET_LIO(dev);
  1454. /* select queue on chosen queue_mapping or core */
  1455. qindex = skb_rx_queue_recorded(skb) ?
  1456. skb_get_rx_queue(skb) : smp_processor_id();
  1457. return (u16)(qindex & (lio->linfo.num_txpciq - 1));
  1458. }
  1459. /** Routine to push packets arriving on Octeon interface upto network layer.
  1460. * @param oct_id - octeon device id.
  1461. * @param skbuff - skbuff struct to be passed to network layer.
  1462. * @param len - size of total data received.
  1463. * @param rh - Control header associated with the packet
  1464. * @param param - additional control data with the packet
  1465. */
  1466. static void
  1467. liquidio_push_packet(u32 octeon_id,
  1468. void *skbuff,
  1469. u32 len,
  1470. union octeon_rh *rh,
  1471. void *param)
  1472. {
  1473. struct napi_struct *napi = param;
  1474. struct octeon_device *oct = lio_get_device(octeon_id);
  1475. struct sk_buff *skb = (struct sk_buff *)skbuff;
  1476. struct skb_shared_hwtstamps *shhwtstamps;
  1477. u64 ns;
  1478. struct net_device *netdev =
  1479. (struct net_device *)oct->props[rh->r_dh.link].netdev;
  1480. struct octeon_droq *droq = container_of(param, struct octeon_droq,
  1481. napi);
  1482. if (netdev) {
  1483. int packet_was_received;
  1484. struct lio *lio = GET_LIO(netdev);
  1485. /* Do not proceed if the interface is not in RUNNING state. */
  1486. if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
  1487. recv_buffer_free(skb);
  1488. droq->stats.rx_dropped++;
  1489. return;
  1490. }
  1491. skb->dev = netdev;
  1492. if (rh->r_dh.has_hwtstamp) {
  1493. /* timestamp is included from the hardware at the
  1494. * beginning of the packet.
  1495. */
  1496. if (ifstate_check(lio,
  1497. LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
  1498. /* Nanoseconds are in the first 64-bits
  1499. * of the packet.
  1500. */
  1501. memcpy(&ns, (skb->data), sizeof(ns));
  1502. shhwtstamps = skb_hwtstamps(skb);
  1503. shhwtstamps->hwtstamp =
  1504. ns_to_ktime(ns + lio->ptp_adjust);
  1505. }
  1506. skb_pull(skb, sizeof(ns));
  1507. }
  1508. skb->protocol = eth_type_trans(skb, skb->dev);
  1509. if ((netdev->features & NETIF_F_RXCSUM) &&
  1510. (rh->r_dh.csum_verified == CNNIC_CSUM_VERIFIED))
  1511. /* checksum has already been verified */
  1512. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1513. else
  1514. skb->ip_summed = CHECKSUM_NONE;
  1515. packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
  1516. if (packet_was_received) {
  1517. droq->stats.rx_bytes_received += len;
  1518. droq->stats.rx_pkts_received++;
  1519. netdev->last_rx = jiffies;
  1520. } else {
  1521. droq->stats.rx_dropped++;
  1522. netif_info(lio, rx_err, lio->netdev,
  1523. "droq:%d error rx_dropped:%llu\n",
  1524. droq->q_no, droq->stats.rx_dropped);
  1525. }
  1526. } else {
  1527. recv_buffer_free(skb);
  1528. }
  1529. }
  1530. /**
  1531. * \brief wrapper for calling napi_schedule
  1532. * @param param parameters to pass to napi_schedule
  1533. *
  1534. * Used when scheduling on different CPUs
  1535. */
  1536. static void napi_schedule_wrapper(void *param)
  1537. {
  1538. struct napi_struct *napi = param;
  1539. napi_schedule(napi);
  1540. }
  1541. /**
  1542. * \brief callback when receive interrupt occurs and we are in NAPI mode
  1543. * @param arg pointer to octeon output queue
  1544. */
  1545. static void liquidio_napi_drv_callback(void *arg)
  1546. {
  1547. struct octeon_droq *droq = arg;
  1548. int this_cpu = smp_processor_id();
  1549. if (droq->cpu_id == this_cpu) {
  1550. napi_schedule(&droq->napi);
  1551. } else {
  1552. struct call_single_data *csd = &droq->csd;
  1553. csd->func = napi_schedule_wrapper;
  1554. csd->info = &droq->napi;
  1555. csd->flags = 0;
  1556. smp_call_function_single_async(droq->cpu_id, csd);
  1557. }
  1558. }
  1559. /**
  1560. * \brief Main NAPI poll function
  1561. * @param droq octeon output queue
  1562. * @param budget maximum number of items to process
  1563. */
  1564. static int liquidio_napi_do_rx(struct octeon_droq *droq, int budget)
  1565. {
  1566. int work_done;
  1567. struct lio *lio = GET_LIO(droq->napi.dev);
  1568. struct octeon_device *oct = lio->oct_dev;
  1569. work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
  1570. POLL_EVENT_PROCESS_PKTS,
  1571. budget);
  1572. if (work_done < 0) {
  1573. netif_info(lio, rx_err, lio->netdev,
  1574. "Receive work_done < 0, rxq:%d\n", droq->q_no);
  1575. goto octnet_napi_finish;
  1576. }
  1577. if (work_done > budget)
  1578. dev_err(&oct->pci_dev->dev, ">>>> %s work_done: %d budget: %d\n",
  1579. __func__, work_done, budget);
  1580. return work_done;
  1581. octnet_napi_finish:
  1582. napi_complete(&droq->napi);
  1583. octeon_process_droq_poll_cmd(oct, droq->q_no, POLL_EVENT_ENABLE_INTR,
  1584. 0);
  1585. return 0;
  1586. }
  1587. /**
  1588. * \brief Entry point for NAPI polling
  1589. * @param napi NAPI structure
  1590. * @param budget maximum number of items to process
  1591. */
  1592. static int liquidio_napi_poll(struct napi_struct *napi, int budget)
  1593. {
  1594. struct octeon_droq *droq;
  1595. int work_done;
  1596. droq = container_of(napi, struct octeon_droq, napi);
  1597. work_done = liquidio_napi_do_rx(droq, budget);
  1598. if (work_done < budget) {
  1599. napi_complete(napi);
  1600. octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
  1601. POLL_EVENT_ENABLE_INTR, 0);
  1602. return 0;
  1603. }
  1604. return work_done;
  1605. }
  1606. /**
  1607. * \brief Setup input and output queues
  1608. * @param octeon_dev octeon device
  1609. * @param net_device Net device
  1610. *
  1611. * Note: Queues are with respect to the octeon device. Thus
  1612. * an input queue is for egress packets, and output queues
  1613. * are for ingress packets.
  1614. */
  1615. static inline int setup_io_queues(struct octeon_device *octeon_dev,
  1616. struct net_device *net_device)
  1617. {
  1618. static int first_time = 1;
  1619. static struct octeon_droq_ops droq_ops;
  1620. static int cpu_id;
  1621. static int cpu_id_modulus;
  1622. struct octeon_droq *droq;
  1623. struct napi_struct *napi;
  1624. int q, q_no, retval = 0;
  1625. struct lio *lio;
  1626. int num_tx_descs;
  1627. lio = GET_LIO(net_device);
  1628. if (first_time) {
  1629. first_time = 0;
  1630. memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
  1631. droq_ops.fptr = liquidio_push_packet;
  1632. droq_ops.poll_mode = 1;
  1633. droq_ops.napi_fn = liquidio_napi_drv_callback;
  1634. cpu_id = 0;
  1635. cpu_id_modulus = num_present_cpus();
  1636. }
  1637. /* set up DROQs. */
  1638. for (q = 0; q < lio->linfo.num_rxpciq; q++) {
  1639. q_no = lio->linfo.rxpciq[q];
  1640. retval = octeon_setup_droq(octeon_dev, q_no,
  1641. CFG_GET_NUM_RX_DESCS_NIC_IF
  1642. (octeon_get_conf(octeon_dev),
  1643. lio->ifidx),
  1644. CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
  1645. (octeon_get_conf(octeon_dev),
  1646. lio->ifidx), NULL);
  1647. if (retval) {
  1648. dev_err(&octeon_dev->pci_dev->dev,
  1649. " %s : Runtime DROQ(RxQ) creation failed.\n",
  1650. __func__);
  1651. return 1;
  1652. }
  1653. droq = octeon_dev->droq[q_no];
  1654. napi = &droq->napi;
  1655. netif_napi_add(net_device, napi, liquidio_napi_poll, 64);
  1656. /* designate a CPU for this droq */
  1657. droq->cpu_id = cpu_id;
  1658. cpu_id++;
  1659. if (cpu_id >= cpu_id_modulus)
  1660. cpu_id = 0;
  1661. octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
  1662. }
  1663. /* set up IQs. */
  1664. for (q = 0; q < lio->linfo.num_txpciq; q++) {
  1665. num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
  1666. (octeon_dev),
  1667. lio->ifidx);
  1668. retval = octeon_setup_iq(octeon_dev, lio->linfo.txpciq[q],
  1669. num_tx_descs,
  1670. netdev_get_tx_queue(net_device, q));
  1671. if (retval) {
  1672. dev_err(&octeon_dev->pci_dev->dev,
  1673. " %s : Runtime IQ(TxQ) creation failed.\n",
  1674. __func__);
  1675. return 1;
  1676. }
  1677. }
  1678. return 0;
  1679. }
  1680. /**
  1681. * \brief Poll routine for checking transmit queue status
  1682. * @param work work_struct data structure
  1683. */
  1684. static void octnet_poll_check_txq_status(struct work_struct *work)
  1685. {
  1686. struct cavium_wk *wk = (struct cavium_wk *)work;
  1687. struct lio *lio = (struct lio *)wk->ctxptr;
  1688. if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
  1689. return;
  1690. check_txq_status(lio);
  1691. queue_delayed_work(lio->txq_status_wq.wq,
  1692. &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
  1693. }
  1694. /**
  1695. * \brief Sets up the txq poll check
  1696. * @param netdev network device
  1697. */
  1698. static inline void setup_tx_poll_fn(struct net_device *netdev)
  1699. {
  1700. struct lio *lio = GET_LIO(netdev);
  1701. struct octeon_device *oct = lio->oct_dev;
  1702. lio->txq_status_wq.wq = create_workqueue("txq-status");
  1703. if (!lio->txq_status_wq.wq) {
  1704. dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
  1705. return;
  1706. }
  1707. INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
  1708. octnet_poll_check_txq_status);
  1709. lio->txq_status_wq.wk.ctxptr = lio;
  1710. queue_delayed_work(lio->txq_status_wq.wq,
  1711. &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
  1712. }
  1713. /**
  1714. * \brief Net device open for LiquidIO
  1715. * @param netdev network device
  1716. */
  1717. static int liquidio_open(struct net_device *netdev)
  1718. {
  1719. struct lio *lio = GET_LIO(netdev);
  1720. struct octeon_device *oct = lio->oct_dev;
  1721. struct napi_struct *napi, *n;
  1722. list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
  1723. napi_enable(napi);
  1724. oct_ptp_open(netdev);
  1725. ifstate_set(lio, LIO_IFSTATE_RUNNING);
  1726. setup_tx_poll_fn(netdev);
  1727. start_txq(netdev);
  1728. netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
  1729. try_module_get(THIS_MODULE);
  1730. /* tell Octeon to start forwarding packets to host */
  1731. send_rx_ctrl_cmd(lio, 1);
  1732. /* Ready for link status updates */
  1733. lio->intf_open = 1;
  1734. dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
  1735. netdev->name);
  1736. return 0;
  1737. }
  1738. /**
  1739. * \brief Net device stop for LiquidIO
  1740. * @param netdev network device
  1741. */
  1742. static int liquidio_stop(struct net_device *netdev)
  1743. {
  1744. struct napi_struct *napi, *n;
  1745. struct lio *lio = GET_LIO(netdev);
  1746. struct octeon_device *oct = lio->oct_dev;
  1747. netif_info(lio, ifdown, lio->netdev, "Stopping interface!\n");
  1748. /* Inform that netif carrier is down */
  1749. lio->intf_open = 0;
  1750. lio->linfo.link.s.status = 0;
  1751. netif_carrier_off(netdev);
  1752. /* tell Octeon to stop forwarding packets to host */
  1753. send_rx_ctrl_cmd(lio, 0);
  1754. cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
  1755. flush_workqueue(lio->txq_status_wq.wq);
  1756. destroy_workqueue(lio->txq_status_wq.wq);
  1757. if (lio->ptp_clock) {
  1758. ptp_clock_unregister(lio->ptp_clock);
  1759. lio->ptp_clock = NULL;
  1760. }
  1761. ifstate_reset(lio, LIO_IFSTATE_RUNNING);
  1762. /* This is a hack that allows DHCP to continue working. */
  1763. set_bit(__LINK_STATE_START, &lio->netdev->state);
  1764. list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
  1765. napi_disable(napi);
  1766. txqs_stop(netdev);
  1767. dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
  1768. module_put(THIS_MODULE);
  1769. return 0;
  1770. }
  1771. void liquidio_link_ctrl_cmd_completion(void *nctrl_ptr)
  1772. {
  1773. struct octnic_ctrl_pkt *nctrl = (struct octnic_ctrl_pkt *)nctrl_ptr;
  1774. struct net_device *netdev = (struct net_device *)nctrl->netpndev;
  1775. struct lio *lio = GET_LIO(netdev);
  1776. struct octeon_device *oct = lio->oct_dev;
  1777. switch (nctrl->ncmd.s.cmd) {
  1778. case OCTNET_CMD_CHANGE_DEVFLAGS:
  1779. case OCTNET_CMD_SET_MULTI_LIST:
  1780. break;
  1781. case OCTNET_CMD_CHANGE_MACADDR:
  1782. /* If command is successful, change the MACADDR. */
  1783. netif_info(lio, probe, lio->netdev, " MACAddr changed to 0x%llx\n",
  1784. CVM_CAST64(nctrl->udd[0]));
  1785. dev_info(&oct->pci_dev->dev, "%s MACAddr changed to 0x%llx\n",
  1786. netdev->name, CVM_CAST64(nctrl->udd[0]));
  1787. memcpy(netdev->dev_addr, ((u8 *)&nctrl->udd[0]) + 2, ETH_ALEN);
  1788. break;
  1789. case OCTNET_CMD_CHANGE_MTU:
  1790. /* If command is successful, change the MTU. */
  1791. netif_info(lio, probe, lio->netdev, " MTU Changed from %d to %d\n",
  1792. netdev->mtu, nctrl->ncmd.s.param2);
  1793. dev_info(&oct->pci_dev->dev, "%s MTU Changed from %d to %d\n",
  1794. netdev->name, netdev->mtu,
  1795. nctrl->ncmd.s.param2);
  1796. netdev->mtu = nctrl->ncmd.s.param2;
  1797. break;
  1798. case OCTNET_CMD_GPIO_ACCESS:
  1799. netif_info(lio, probe, lio->netdev, "LED Flashing visual identification\n");
  1800. break;
  1801. case OCTNET_CMD_LRO_ENABLE:
  1802. dev_info(&oct->pci_dev->dev, "%s LRO Enabled\n", netdev->name);
  1803. break;
  1804. case OCTNET_CMD_LRO_DISABLE:
  1805. dev_info(&oct->pci_dev->dev, "%s LRO Disabled\n",
  1806. netdev->name);
  1807. break;
  1808. case OCTNET_CMD_VERBOSE_ENABLE:
  1809. dev_info(&oct->pci_dev->dev, "%s LRO Enabled\n", netdev->name);
  1810. break;
  1811. case OCTNET_CMD_VERBOSE_DISABLE:
  1812. dev_info(&oct->pci_dev->dev, "%s LRO Disabled\n",
  1813. netdev->name);
  1814. break;
  1815. case OCTNET_CMD_SET_SETTINGS:
  1816. dev_info(&oct->pci_dev->dev, "%s settings changed\n",
  1817. netdev->name);
  1818. break;
  1819. default:
  1820. dev_err(&oct->pci_dev->dev, "%s Unknown cmd %d\n", __func__,
  1821. nctrl->ncmd.s.cmd);
  1822. }
  1823. }
  1824. /**
  1825. * \brief Converts a mask based on net device flags
  1826. * @param netdev network device
  1827. *
  1828. * This routine generates a octnet_ifflags mask from the net device flags
  1829. * received from the OS.
  1830. */
  1831. static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
  1832. {
  1833. enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
  1834. if (netdev->flags & IFF_PROMISC)
  1835. f |= OCTNET_IFFLAG_PROMISC;
  1836. if (netdev->flags & IFF_ALLMULTI)
  1837. f |= OCTNET_IFFLAG_ALLMULTI;
  1838. if (netdev->flags & IFF_MULTICAST) {
  1839. f |= OCTNET_IFFLAG_MULTICAST;
  1840. /* Accept all multicast addresses if there are more than we
  1841. * can handle
  1842. */
  1843. if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
  1844. f |= OCTNET_IFFLAG_ALLMULTI;
  1845. }
  1846. if (netdev->flags & IFF_BROADCAST)
  1847. f |= OCTNET_IFFLAG_BROADCAST;
  1848. return f;
  1849. }
  1850. /**
  1851. * \brief Net device set_multicast_list
  1852. * @param netdev network device
  1853. */
  1854. static void liquidio_set_mcast_list(struct net_device *netdev)
  1855. {
  1856. struct lio *lio = GET_LIO(netdev);
  1857. struct octeon_device *oct = lio->oct_dev;
  1858. struct octnic_ctrl_pkt nctrl;
  1859. struct octnic_ctrl_params nparams;
  1860. struct netdev_hw_addr *ha;
  1861. u64 *mc;
  1862. int ret, i;
  1863. int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
  1864. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  1865. /* Create a ctrl pkt command to be sent to core app. */
  1866. nctrl.ncmd.u64 = 0;
  1867. nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
  1868. nctrl.ncmd.s.param1 = lio->linfo.ifidx;
  1869. nctrl.ncmd.s.param2 = get_new_flags(netdev);
  1870. nctrl.ncmd.s.param3 = mc_count;
  1871. nctrl.ncmd.s.more = mc_count;
  1872. nctrl.netpndev = (u64)netdev;
  1873. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  1874. /* copy all the addresses into the udd */
  1875. i = 0;
  1876. mc = &nctrl.udd[0];
  1877. netdev_for_each_mc_addr(ha, netdev) {
  1878. *mc = 0;
  1879. memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
  1880. /* no need to swap bytes */
  1881. if (++mc > &nctrl.udd[mc_count])
  1882. break;
  1883. }
  1884. /* Apparently, any activity in this call from the kernel has to
  1885. * be atomic. So we won't wait for response.
  1886. */
  1887. nctrl.wait_time = 0;
  1888. nparams.resp_order = OCTEON_RESP_NORESPONSE;
  1889. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl, nparams);
  1890. if (ret < 0) {
  1891. dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
  1892. ret);
  1893. }
  1894. }
  1895. /**
  1896. * \brief Net device set_mac_address
  1897. * @param netdev network device
  1898. */
  1899. static int liquidio_set_mac(struct net_device *netdev, void *p)
  1900. {
  1901. int ret = 0;
  1902. struct lio *lio = GET_LIO(netdev);
  1903. struct octeon_device *oct = lio->oct_dev;
  1904. struct sockaddr *addr = (struct sockaddr *)p;
  1905. struct octnic_ctrl_pkt nctrl;
  1906. struct octnic_ctrl_params nparams;
  1907. if ((!is_valid_ether_addr(addr->sa_data)) ||
  1908. (ifstate_check(lio, LIO_IFSTATE_RUNNING)))
  1909. return -EADDRNOTAVAIL;
  1910. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  1911. nctrl.ncmd.u64 = 0;
  1912. nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
  1913. nctrl.ncmd.s.param1 = lio->linfo.ifidx;
  1914. nctrl.ncmd.s.param2 = 0;
  1915. nctrl.ncmd.s.more = 1;
  1916. nctrl.netpndev = (u64)netdev;
  1917. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  1918. nctrl.wait_time = 100;
  1919. nctrl.udd[0] = 0;
  1920. /* The MAC Address is presented in network byte order. */
  1921. memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
  1922. nparams.resp_order = OCTEON_RESP_ORDERED;
  1923. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl, nparams);
  1924. if (ret < 0) {
  1925. dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
  1926. return -ENOMEM;
  1927. }
  1928. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1929. memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
  1930. return 0;
  1931. }
  1932. /**
  1933. * \brief Net device get_stats
  1934. * @param netdev network device
  1935. */
  1936. static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
  1937. {
  1938. struct lio *lio = GET_LIO(netdev);
  1939. struct net_device_stats *stats = &netdev->stats;
  1940. struct octeon_device *oct;
  1941. u64 pkts = 0, drop = 0, bytes = 0;
  1942. struct oct_droq_stats *oq_stats;
  1943. struct oct_iq_stats *iq_stats;
  1944. int i, iq_no, oq_no;
  1945. oct = lio->oct_dev;
  1946. for (i = 0; i < lio->linfo.num_txpciq; i++) {
  1947. iq_no = lio->linfo.txpciq[i];
  1948. iq_stats = &oct->instr_queue[iq_no]->stats;
  1949. pkts += iq_stats->tx_done;
  1950. drop += iq_stats->tx_dropped;
  1951. bytes += iq_stats->tx_tot_bytes;
  1952. }
  1953. stats->tx_packets = pkts;
  1954. stats->tx_bytes = bytes;
  1955. stats->tx_dropped = drop;
  1956. pkts = 0;
  1957. drop = 0;
  1958. bytes = 0;
  1959. for (i = 0; i < lio->linfo.num_rxpciq; i++) {
  1960. oq_no = lio->linfo.rxpciq[i];
  1961. oq_stats = &oct->droq[oq_no]->stats;
  1962. pkts += oq_stats->rx_pkts_received;
  1963. drop += (oq_stats->rx_dropped +
  1964. oq_stats->dropped_nodispatch +
  1965. oq_stats->dropped_toomany +
  1966. oq_stats->dropped_nomem);
  1967. bytes += oq_stats->rx_bytes_received;
  1968. }
  1969. stats->rx_bytes = bytes;
  1970. stats->rx_packets = pkts;
  1971. stats->rx_dropped = drop;
  1972. return stats;
  1973. }
  1974. /**
  1975. * \brief Net device change_mtu
  1976. * @param netdev network device
  1977. */
  1978. static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
  1979. {
  1980. struct lio *lio = GET_LIO(netdev);
  1981. struct octeon_device *oct = lio->oct_dev;
  1982. struct octnic_ctrl_pkt nctrl;
  1983. struct octnic_ctrl_params nparams;
  1984. int max_frm_size = new_mtu + OCTNET_FRM_HEADER_SIZE;
  1985. int ret = 0;
  1986. /* Limit the MTU to make sure the ethernet packets are between 64 bytes
  1987. * and 65535 bytes
  1988. */
  1989. if ((max_frm_size < OCTNET_MIN_FRM_SIZE) ||
  1990. (max_frm_size > OCTNET_MAX_FRM_SIZE)) {
  1991. dev_err(&oct->pci_dev->dev, "Invalid MTU: %d\n", new_mtu);
  1992. dev_err(&oct->pci_dev->dev, "Valid range %d and %d\n",
  1993. (OCTNET_MIN_FRM_SIZE - OCTNET_FRM_HEADER_SIZE),
  1994. (OCTNET_MAX_FRM_SIZE - OCTNET_FRM_HEADER_SIZE));
  1995. return -EINVAL;
  1996. }
  1997. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  1998. nctrl.ncmd.u64 = 0;
  1999. nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
  2000. nctrl.ncmd.s.param1 = lio->linfo.ifidx;
  2001. nctrl.ncmd.s.param2 = new_mtu;
  2002. nctrl.wait_time = 100;
  2003. nctrl.netpndev = (u64)netdev;
  2004. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2005. nparams.resp_order = OCTEON_RESP_ORDERED;
  2006. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl, nparams);
  2007. if (ret < 0) {
  2008. dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
  2009. return -1;
  2010. }
  2011. lio->mtu = new_mtu;
  2012. return 0;
  2013. }
  2014. /**
  2015. * \brief Handler for SIOCSHWTSTAMP ioctl
  2016. * @param netdev network device
  2017. * @param ifr interface request
  2018. * @param cmd command
  2019. */
  2020. static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2021. {
  2022. struct hwtstamp_config conf;
  2023. struct lio *lio = GET_LIO(netdev);
  2024. if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
  2025. return -EFAULT;
  2026. if (conf.flags)
  2027. return -EINVAL;
  2028. switch (conf.tx_type) {
  2029. case HWTSTAMP_TX_ON:
  2030. case HWTSTAMP_TX_OFF:
  2031. break;
  2032. default:
  2033. return -ERANGE;
  2034. }
  2035. switch (conf.rx_filter) {
  2036. case HWTSTAMP_FILTER_NONE:
  2037. break;
  2038. case HWTSTAMP_FILTER_ALL:
  2039. case HWTSTAMP_FILTER_SOME:
  2040. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  2041. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  2042. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  2043. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  2044. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  2045. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  2046. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  2047. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  2048. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  2049. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  2050. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  2051. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  2052. conf.rx_filter = HWTSTAMP_FILTER_ALL;
  2053. break;
  2054. default:
  2055. return -ERANGE;
  2056. }
  2057. if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
  2058. ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
  2059. else
  2060. ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
  2061. return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
  2062. }
  2063. /**
  2064. * \brief ioctl handler
  2065. * @param netdev network device
  2066. * @param ifr interface request
  2067. * @param cmd command
  2068. */
  2069. static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2070. {
  2071. switch (cmd) {
  2072. case SIOCSHWTSTAMP:
  2073. return hwtstamp_ioctl(netdev, ifr, cmd);
  2074. default:
  2075. return -EOPNOTSUPP;
  2076. }
  2077. }
  2078. /**
  2079. * \brief handle a Tx timestamp response
  2080. * @param status response status
  2081. * @param buf pointer to skb
  2082. */
  2083. static void handle_timestamp(struct octeon_device *oct,
  2084. u32 status,
  2085. void *buf)
  2086. {
  2087. struct octnet_buf_free_info *finfo;
  2088. struct octeon_soft_command *sc;
  2089. struct oct_timestamp_resp *resp;
  2090. struct lio *lio;
  2091. struct sk_buff *skb = (struct sk_buff *)buf;
  2092. finfo = (struct octnet_buf_free_info *)skb->cb;
  2093. lio = finfo->lio;
  2094. sc = finfo->sc;
  2095. oct = lio->oct_dev;
  2096. resp = (struct oct_timestamp_resp *)sc->virtrptr;
  2097. if (status != OCTEON_REQUEST_DONE) {
  2098. dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
  2099. CVM_CAST64(status));
  2100. resp->timestamp = 0;
  2101. }
  2102. octeon_swap_8B_data(&resp->timestamp, 1);
  2103. if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
  2104. struct skb_shared_hwtstamps ts;
  2105. u64 ns = resp->timestamp;
  2106. netif_info(lio, tx_done, lio->netdev,
  2107. "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
  2108. skb, (unsigned long long)ns);
  2109. ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
  2110. skb_tstamp_tx(skb, &ts);
  2111. }
  2112. octeon_free_soft_command(oct, sc);
  2113. recv_buffer_free(skb);
  2114. }
  2115. /* \brief Send a data packet that will be timestamped
  2116. * @param oct octeon device
  2117. * @param ndata pointer to network data
  2118. * @param finfo pointer to private network data
  2119. */
  2120. static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
  2121. struct octnic_data_pkt *ndata,
  2122. struct octnet_buf_free_info *finfo,
  2123. int xmit_more)
  2124. {
  2125. int retval;
  2126. struct octeon_soft_command *sc;
  2127. struct octeon_instr_ih *ih;
  2128. struct octeon_instr_rdp *rdp;
  2129. struct lio *lio;
  2130. int ring_doorbell;
  2131. lio = finfo->lio;
  2132. sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
  2133. sizeof(struct oct_timestamp_resp));
  2134. finfo->sc = sc;
  2135. if (!sc) {
  2136. dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
  2137. return IQ_SEND_FAILED;
  2138. }
  2139. if (ndata->reqtype == REQTYPE_NORESP_NET)
  2140. ndata->reqtype = REQTYPE_RESP_NET;
  2141. else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
  2142. ndata->reqtype = REQTYPE_RESP_NET_SG;
  2143. sc->callback = handle_timestamp;
  2144. sc->callback_arg = finfo->skb;
  2145. sc->iq_no = ndata->q_no;
  2146. ih = (struct octeon_instr_ih *)&sc->cmd.ih;
  2147. rdp = (struct octeon_instr_rdp *)&sc->cmd.rdp;
  2148. ring_doorbell = !xmit_more;
  2149. retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
  2150. sc, ih->dlengsz, ndata->reqtype);
  2151. if (retval) {
  2152. dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
  2153. retval);
  2154. octeon_free_soft_command(oct, sc);
  2155. } else {
  2156. netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
  2157. }
  2158. return retval;
  2159. }
  2160. static inline int is_ipv4(struct sk_buff *skb)
  2161. {
  2162. return (skb->protocol == htons(ETH_P_IP)) &&
  2163. (ip_hdr(skb)->version == 4);
  2164. }
  2165. static inline int is_vlan(struct sk_buff *skb)
  2166. {
  2167. return skb->protocol == htons(ETH_P_8021Q);
  2168. }
  2169. static inline int is_ip_fragmented(struct sk_buff *skb)
  2170. {
  2171. /* The Don't fragment and Reserved flag fields are ignored.
  2172. * IP is fragmented if
  2173. * - the More fragments bit is set (indicating this IP is a fragment
  2174. * with more to follow; the current offset could be 0 ).
  2175. * - ths offset field is non-zero.
  2176. */
  2177. return (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) ? 1 : 0;
  2178. }
  2179. static inline int is_ipv6(struct sk_buff *skb)
  2180. {
  2181. return (skb->protocol == htons(ETH_P_IPV6)) &&
  2182. (ipv6_hdr(skb)->version == 6);
  2183. }
  2184. static inline int is_with_extn_hdr(struct sk_buff *skb)
  2185. {
  2186. return (ipv6_hdr(skb)->nexthdr != IPPROTO_TCP) &&
  2187. (ipv6_hdr(skb)->nexthdr != IPPROTO_UDP);
  2188. }
  2189. static inline int is_tcpudp(struct sk_buff *skb)
  2190. {
  2191. return (ip_hdr(skb)->protocol == IPPROTO_TCP) ||
  2192. (ip_hdr(skb)->protocol == IPPROTO_UDP);
  2193. }
  2194. static inline u32 get_ipv4_5tuple_tag(struct sk_buff *skb)
  2195. {
  2196. u32 tag;
  2197. struct iphdr *iphdr = ip_hdr(skb);
  2198. tag = crc32(0, &iphdr->protocol, 1);
  2199. tag = crc32(tag, (u8 *)&iphdr->saddr, 8);
  2200. tag = crc32(tag, skb_transport_header(skb), 4);
  2201. return tag;
  2202. }
  2203. static inline u32 get_ipv6_5tuple_tag(struct sk_buff *skb)
  2204. {
  2205. u32 tag;
  2206. struct ipv6hdr *ipv6hdr = ipv6_hdr(skb);
  2207. tag = crc32(0, &ipv6hdr->nexthdr, 1);
  2208. tag = crc32(tag, (u8 *)&ipv6hdr->saddr, 32);
  2209. tag = crc32(tag, skb_transport_header(skb), 4);
  2210. return tag;
  2211. }
  2212. /** \brief Transmit networks packets to the Octeon interface
  2213. * @param skbuff skbuff struct to be passed to network layer.
  2214. * @param netdev pointer to network device
  2215. * @returns whether the packet was transmitted to the device okay or not
  2216. * (NETDEV_TX_OK or NETDEV_TX_BUSY)
  2217. */
  2218. static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
  2219. {
  2220. struct lio *lio;
  2221. struct octnet_buf_free_info *finfo;
  2222. union octnic_cmd_setup cmdsetup;
  2223. struct octnic_data_pkt ndata;
  2224. struct octeon_device *oct;
  2225. struct oct_iq_stats *stats;
  2226. int cpu = 0, status = 0;
  2227. int q_idx = 0, iq_no = 0;
  2228. int xmit_more;
  2229. u32 tag = 0;
  2230. lio = GET_LIO(netdev);
  2231. oct = lio->oct_dev;
  2232. if (netif_is_multiqueue(netdev)) {
  2233. cpu = skb->queue_mapping;
  2234. q_idx = (cpu & (lio->linfo.num_txpciq - 1));
  2235. iq_no = lio->linfo.txpciq[q_idx];
  2236. } else {
  2237. iq_no = lio->txq;
  2238. }
  2239. stats = &oct->instr_queue[iq_no]->stats;
  2240. /* Check for all conditions in which the current packet cannot be
  2241. * transmitted.
  2242. */
  2243. if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
  2244. (!lio->linfo.link.s.status) ||
  2245. (skb->len <= 0)) {
  2246. netif_info(lio, tx_err, lio->netdev,
  2247. "Transmit failed link_status : %d\n",
  2248. lio->linfo.link.s.status);
  2249. goto lio_xmit_failed;
  2250. }
  2251. /* Use space in skb->cb to store info used to unmap and
  2252. * free the buffers.
  2253. */
  2254. finfo = (struct octnet_buf_free_info *)skb->cb;
  2255. finfo->lio = lio;
  2256. finfo->skb = skb;
  2257. finfo->sc = NULL;
  2258. /* Prepare the attributes for the data to be passed to OSI. */
  2259. memset(&ndata, 0, sizeof(struct octnic_data_pkt));
  2260. ndata.buf = (void *)finfo;
  2261. ndata.q_no = iq_no;
  2262. if (netif_is_multiqueue(netdev)) {
  2263. if (octnet_iq_is_full(oct, ndata.q_no)) {
  2264. /* defer sending if queue is full */
  2265. netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
  2266. ndata.q_no);
  2267. stats->tx_iq_busy++;
  2268. return NETDEV_TX_BUSY;
  2269. }
  2270. } else {
  2271. if (octnet_iq_is_full(oct, lio->txq)) {
  2272. /* defer sending if queue is full */
  2273. stats->tx_iq_busy++;
  2274. netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
  2275. ndata.q_no);
  2276. return NETDEV_TX_BUSY;
  2277. }
  2278. }
  2279. /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
  2280. * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no );
  2281. */
  2282. ndata.datasize = skb->len;
  2283. cmdsetup.u64 = 0;
  2284. cmdsetup.s.ifidx = lio->linfo.ifidx;
  2285. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2286. if (is_ipv4(skb) && !is_ip_fragmented(skb) && is_tcpudp(skb)) {
  2287. tag = get_ipv4_5tuple_tag(skb);
  2288. cmdsetup.s.cksum_offset = sizeof(struct ethhdr) + 1;
  2289. if (ip_hdr(skb)->ihl > 5)
  2290. cmdsetup.s.ipv4opts_ipv6exthdr =
  2291. OCT_PKT_PARAM_IPV4OPTS;
  2292. } else if (is_ipv6(skb)) {
  2293. tag = get_ipv6_5tuple_tag(skb);
  2294. cmdsetup.s.cksum_offset = sizeof(struct ethhdr) + 1;
  2295. if (is_with_extn_hdr(skb))
  2296. cmdsetup.s.ipv4opts_ipv6exthdr =
  2297. OCT_PKT_PARAM_IPV6EXTHDR;
  2298. } else if (is_vlan(skb)) {
  2299. if (vlan_eth_hdr(skb)->h_vlan_encapsulated_proto
  2300. == htons(ETH_P_IP) &&
  2301. !is_ip_fragmented(skb) && is_tcpudp(skb)) {
  2302. tag = get_ipv4_5tuple_tag(skb);
  2303. cmdsetup.s.cksum_offset =
  2304. sizeof(struct vlan_ethhdr) + 1;
  2305. if (ip_hdr(skb)->ihl > 5)
  2306. cmdsetup.s.ipv4opts_ipv6exthdr =
  2307. OCT_PKT_PARAM_IPV4OPTS;
  2308. } else if (vlan_eth_hdr(skb)->h_vlan_encapsulated_proto
  2309. == htons(ETH_P_IPV6)) {
  2310. tag = get_ipv6_5tuple_tag(skb);
  2311. cmdsetup.s.cksum_offset =
  2312. sizeof(struct vlan_ethhdr) + 1;
  2313. if (is_with_extn_hdr(skb))
  2314. cmdsetup.s.ipv4opts_ipv6exthdr =
  2315. OCT_PKT_PARAM_IPV6EXTHDR;
  2316. }
  2317. }
  2318. }
  2319. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
  2320. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  2321. cmdsetup.s.timestamp = 1;
  2322. }
  2323. if (skb_shinfo(skb)->nr_frags == 0) {
  2324. cmdsetup.s.u.datasize = skb->len;
  2325. octnet_prepare_pci_cmd(&ndata.cmd, &cmdsetup, tag);
  2326. /* Offload checksum calculation for TCP/UDP packets */
  2327. ndata.cmd.dptr = dma_map_single(&oct->pci_dev->dev,
  2328. skb->data,
  2329. skb->len,
  2330. DMA_TO_DEVICE);
  2331. if (dma_mapping_error(&oct->pci_dev->dev, ndata.cmd.dptr)) {
  2332. dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
  2333. __func__);
  2334. return NETDEV_TX_BUSY;
  2335. }
  2336. finfo->dptr = ndata.cmd.dptr;
  2337. ndata.reqtype = REQTYPE_NORESP_NET;
  2338. } else {
  2339. int i, frags;
  2340. struct skb_frag_struct *frag;
  2341. struct octnic_gather *g;
  2342. spin_lock(&lio->lock);
  2343. g = (struct octnic_gather *)list_delete_head(&lio->glist);
  2344. spin_unlock(&lio->lock);
  2345. if (!g) {
  2346. netif_info(lio, tx_err, lio->netdev,
  2347. "Transmit scatter gather: glist null!\n");
  2348. goto lio_xmit_failed;
  2349. }
  2350. cmdsetup.s.gather = 1;
  2351. cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
  2352. octnet_prepare_pci_cmd(&ndata.cmd, &cmdsetup, tag);
  2353. memset(g->sg, 0, g->sg_size);
  2354. g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
  2355. skb->data,
  2356. (skb->len - skb->data_len),
  2357. DMA_TO_DEVICE);
  2358. if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
  2359. dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
  2360. __func__);
  2361. return NETDEV_TX_BUSY;
  2362. }
  2363. add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
  2364. frags = skb_shinfo(skb)->nr_frags;
  2365. i = 1;
  2366. while (frags--) {
  2367. frag = &skb_shinfo(skb)->frags[i - 1];
  2368. g->sg[(i >> 2)].ptr[(i & 3)] =
  2369. dma_map_page(&oct->pci_dev->dev,
  2370. frag->page.p,
  2371. frag->page_offset,
  2372. frag->size,
  2373. DMA_TO_DEVICE);
  2374. add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
  2375. i++;
  2376. }
  2377. ndata.cmd.dptr = dma_map_single(&oct->pci_dev->dev,
  2378. g->sg, g->sg_size,
  2379. DMA_TO_DEVICE);
  2380. if (dma_mapping_error(&oct->pci_dev->dev, ndata.cmd.dptr)) {
  2381. dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
  2382. __func__);
  2383. dma_unmap_single(&oct->pci_dev->dev, g->sg[0].ptr[0],
  2384. skb->len - skb->data_len,
  2385. DMA_TO_DEVICE);
  2386. return NETDEV_TX_BUSY;
  2387. }
  2388. finfo->dptr = ndata.cmd.dptr;
  2389. finfo->g = g;
  2390. ndata.reqtype = REQTYPE_NORESP_NET_SG;
  2391. }
  2392. if (skb_shinfo(skb)->gso_size) {
  2393. struct octeon_instr_irh *irh =
  2394. (struct octeon_instr_irh *)&ndata.cmd.irh;
  2395. union tx_info *tx_info = (union tx_info *)&ndata.cmd.ossp[0];
  2396. irh->len = 1; /* to indicate that ossp[0] contains tx_info */
  2397. tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
  2398. tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
  2399. }
  2400. xmit_more = skb->xmit_more;
  2401. if (unlikely(cmdsetup.s.timestamp))
  2402. status = send_nic_timestamp_pkt(oct, &ndata, finfo, xmit_more);
  2403. else
  2404. status = octnet_send_nic_data_pkt(oct, &ndata, xmit_more);
  2405. if (status == IQ_SEND_FAILED)
  2406. goto lio_xmit_failed;
  2407. netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
  2408. if (status == IQ_SEND_STOP)
  2409. stop_q(lio->netdev, q_idx);
  2410. netif_trans_update(netdev);
  2411. stats->tx_done++;
  2412. stats->tx_tot_bytes += skb->len;
  2413. return NETDEV_TX_OK;
  2414. lio_xmit_failed:
  2415. stats->tx_dropped++;
  2416. netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
  2417. iq_no, stats->tx_dropped);
  2418. dma_unmap_single(&oct->pci_dev->dev, ndata.cmd.dptr,
  2419. ndata.datasize, DMA_TO_DEVICE);
  2420. recv_buffer_free(skb);
  2421. return NETDEV_TX_OK;
  2422. }
  2423. /** \brief Network device Tx timeout
  2424. * @param netdev pointer to network device
  2425. */
  2426. static void liquidio_tx_timeout(struct net_device *netdev)
  2427. {
  2428. struct lio *lio;
  2429. lio = GET_LIO(netdev);
  2430. netif_info(lio, tx_err, lio->netdev,
  2431. "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
  2432. netdev->stats.tx_dropped);
  2433. netif_trans_update(netdev);
  2434. txqs_wake(netdev);
  2435. }
  2436. int liquidio_set_feature(struct net_device *netdev, int cmd)
  2437. {
  2438. struct lio *lio = GET_LIO(netdev);
  2439. struct octeon_device *oct = lio->oct_dev;
  2440. struct octnic_ctrl_pkt nctrl;
  2441. struct octnic_ctrl_params nparams;
  2442. int ret = 0;
  2443. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2444. nctrl.ncmd.u64 = 0;
  2445. nctrl.ncmd.s.cmd = cmd;
  2446. nctrl.ncmd.s.param1 = lio->linfo.ifidx;
  2447. nctrl.ncmd.s.param2 = OCTNIC_LROIPV4 | OCTNIC_LROIPV6;
  2448. nctrl.wait_time = 100;
  2449. nctrl.netpndev = (u64)netdev;
  2450. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2451. nparams.resp_order = OCTEON_RESP_NORESPONSE;
  2452. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl, nparams);
  2453. if (ret < 0) {
  2454. dev_err(&oct->pci_dev->dev, "Feature change failed in core (ret: 0x%x)\n",
  2455. ret);
  2456. }
  2457. return ret;
  2458. }
  2459. /** \brief Net device fix features
  2460. * @param netdev pointer to network device
  2461. * @param request features requested
  2462. * @returns updated features list
  2463. */
  2464. static netdev_features_t liquidio_fix_features(struct net_device *netdev,
  2465. netdev_features_t request)
  2466. {
  2467. struct lio *lio = netdev_priv(netdev);
  2468. if ((request & NETIF_F_RXCSUM) &&
  2469. !(lio->dev_capability & NETIF_F_RXCSUM))
  2470. request &= ~NETIF_F_RXCSUM;
  2471. if ((request & NETIF_F_HW_CSUM) &&
  2472. !(lio->dev_capability & NETIF_F_HW_CSUM))
  2473. request &= ~NETIF_F_HW_CSUM;
  2474. if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
  2475. request &= ~NETIF_F_TSO;
  2476. if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
  2477. request &= ~NETIF_F_TSO6;
  2478. if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
  2479. request &= ~NETIF_F_LRO;
  2480. /*Disable LRO if RXCSUM is off */
  2481. if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
  2482. (lio->dev_capability & NETIF_F_LRO))
  2483. request &= ~NETIF_F_LRO;
  2484. return request;
  2485. }
  2486. /** \brief Net device set features
  2487. * @param netdev pointer to network device
  2488. * @param features features to enable/disable
  2489. */
  2490. static int liquidio_set_features(struct net_device *netdev,
  2491. netdev_features_t features)
  2492. {
  2493. struct lio *lio = netdev_priv(netdev);
  2494. if (!((netdev->features ^ features) & NETIF_F_LRO))
  2495. return 0;
  2496. if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
  2497. liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE);
  2498. else if (!(features & NETIF_F_LRO) &&
  2499. (lio->dev_capability & NETIF_F_LRO))
  2500. liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE);
  2501. return 0;
  2502. }
  2503. static struct net_device_ops lionetdevops = {
  2504. .ndo_open = liquidio_open,
  2505. .ndo_stop = liquidio_stop,
  2506. .ndo_start_xmit = liquidio_xmit,
  2507. .ndo_get_stats = liquidio_get_stats,
  2508. .ndo_set_mac_address = liquidio_set_mac,
  2509. .ndo_set_rx_mode = liquidio_set_mcast_list,
  2510. .ndo_tx_timeout = liquidio_tx_timeout,
  2511. .ndo_change_mtu = liquidio_change_mtu,
  2512. .ndo_do_ioctl = liquidio_ioctl,
  2513. .ndo_fix_features = liquidio_fix_features,
  2514. .ndo_set_features = liquidio_set_features,
  2515. };
  2516. /** \brief Entry point for the liquidio module
  2517. */
  2518. static int __init liquidio_init(void)
  2519. {
  2520. int i;
  2521. struct handshake *hs;
  2522. init_completion(&first_stage);
  2523. octeon_init_device_list(conf_type);
  2524. if (liquidio_init_pci())
  2525. return -EINVAL;
  2526. wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
  2527. for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
  2528. hs = &handshake[i];
  2529. if (hs->pci_dev) {
  2530. wait_for_completion(&hs->init);
  2531. if (!hs->init_ok) {
  2532. /* init handshake failed */
  2533. dev_err(&hs->pci_dev->dev,
  2534. "Failed to init device\n");
  2535. liquidio_deinit_pci();
  2536. return -EIO;
  2537. }
  2538. }
  2539. }
  2540. for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
  2541. hs = &handshake[i];
  2542. if (hs->pci_dev) {
  2543. wait_for_completion_timeout(&hs->started,
  2544. msecs_to_jiffies(30000));
  2545. if (!hs->started_ok) {
  2546. /* starter handshake failed */
  2547. dev_err(&hs->pci_dev->dev,
  2548. "Firmware failed to start\n");
  2549. liquidio_deinit_pci();
  2550. return -EIO;
  2551. }
  2552. }
  2553. }
  2554. return 0;
  2555. }
  2556. static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
  2557. {
  2558. struct octeon_device *oct = (struct octeon_device *)buf;
  2559. struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
  2560. int ifidx = 0;
  2561. union oct_link_status *ls;
  2562. int i;
  2563. if ((recv_pkt->buffer_size[0] != sizeof(*ls)) ||
  2564. (recv_pkt->rh.r_nic_info.ifidx > oct->ifcount)) {
  2565. dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
  2566. recv_pkt->buffer_size[0],
  2567. recv_pkt->rh.r_nic_info.ifidx);
  2568. goto nic_info_err;
  2569. }
  2570. ifidx = recv_pkt->rh.r_nic_info.ifidx;
  2571. ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
  2572. octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
  2573. update_link_status(oct->props[ifidx].netdev, ls);
  2574. nic_info_err:
  2575. for (i = 0; i < recv_pkt->buffer_count; i++)
  2576. recv_buffer_free(recv_pkt->buffer_ptr[i]);
  2577. octeon_free_recv_info(recv_info);
  2578. return 0;
  2579. }
  2580. /**
  2581. * \brief Setup network interfaces
  2582. * @param octeon_dev octeon device
  2583. *
  2584. * Called during init time for each device. It assumes the NIC
  2585. * is already up and running. The link information for each
  2586. * interface is passed in link_info.
  2587. */
  2588. static int setup_nic_devices(struct octeon_device *octeon_dev)
  2589. {
  2590. struct lio *lio = NULL;
  2591. struct net_device *netdev;
  2592. u8 mac[6], i, j;
  2593. struct octeon_soft_command *sc;
  2594. struct liquidio_if_cfg_context *ctx;
  2595. struct liquidio_if_cfg_resp *resp;
  2596. struct octdev_props *props;
  2597. int retval, num_iqueues, num_oqueues, q_no;
  2598. u64 q_mask;
  2599. int num_cpus = num_online_cpus();
  2600. union oct_nic_if_cfg if_cfg;
  2601. unsigned int base_queue;
  2602. unsigned int gmx_port_id;
  2603. u32 resp_size, ctx_size;
  2604. /* This is to handle link status changes */
  2605. octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
  2606. OPCODE_NIC_INFO,
  2607. lio_nic_info, octeon_dev);
  2608. /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
  2609. * They are handled directly.
  2610. */
  2611. octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
  2612. free_netbuf);
  2613. octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
  2614. free_netsgbuf);
  2615. octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
  2616. free_netsgbuf_with_resp);
  2617. for (i = 0; i < octeon_dev->ifcount; i++) {
  2618. resp_size = sizeof(struct liquidio_if_cfg_resp);
  2619. ctx_size = sizeof(struct liquidio_if_cfg_context);
  2620. sc = (struct octeon_soft_command *)
  2621. octeon_alloc_soft_command(octeon_dev, 0,
  2622. resp_size, ctx_size);
  2623. resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
  2624. ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
  2625. num_iqueues =
  2626. CFG_GET_NUM_TXQS_NIC_IF(octeon_get_conf(octeon_dev), i);
  2627. num_oqueues =
  2628. CFG_GET_NUM_RXQS_NIC_IF(octeon_get_conf(octeon_dev), i);
  2629. base_queue =
  2630. CFG_GET_BASE_QUE_NIC_IF(octeon_get_conf(octeon_dev), i);
  2631. gmx_port_id =
  2632. CFG_GET_GMXID_NIC_IF(octeon_get_conf(octeon_dev), i);
  2633. if (num_iqueues > num_cpus)
  2634. num_iqueues = num_cpus;
  2635. if (num_oqueues > num_cpus)
  2636. num_oqueues = num_cpus;
  2637. dev_dbg(&octeon_dev->pci_dev->dev,
  2638. "requesting config for interface %d, iqs %d, oqs %d\n",
  2639. i, num_iqueues, num_oqueues);
  2640. ACCESS_ONCE(ctx->cond) = 0;
  2641. ctx->octeon_id = lio_get_device_id(octeon_dev);
  2642. init_waitqueue_head(&ctx->wc);
  2643. if_cfg.u64 = 0;
  2644. if_cfg.s.num_iqueues = num_iqueues;
  2645. if_cfg.s.num_oqueues = num_oqueues;
  2646. if_cfg.s.base_queue = base_queue;
  2647. if_cfg.s.gmx_port_id = gmx_port_id;
  2648. octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
  2649. OPCODE_NIC_IF_CFG, i,
  2650. if_cfg.u64, 0);
  2651. sc->callback = if_cfg_callback;
  2652. sc->callback_arg = sc;
  2653. sc->wait_time = 1000;
  2654. retval = octeon_send_soft_command(octeon_dev, sc);
  2655. if (retval) {
  2656. dev_err(&octeon_dev->pci_dev->dev,
  2657. "iq/oq config failed status: %x\n",
  2658. retval);
  2659. /* Soft instr is freed by driver in case of failure. */
  2660. goto setup_nic_dev_fail;
  2661. }
  2662. /* Sleep on a wait queue till the cond flag indicates that the
  2663. * response arrived or timed-out.
  2664. */
  2665. sleep_cond(&ctx->wc, &ctx->cond);
  2666. retval = resp->status;
  2667. if (retval) {
  2668. dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
  2669. goto setup_nic_dev_fail;
  2670. }
  2671. octeon_swap_8B_data((u64 *)(&resp->cfg_info),
  2672. (sizeof(struct liquidio_if_cfg_info)) >> 3);
  2673. num_iqueues = hweight64(resp->cfg_info.iqmask);
  2674. num_oqueues = hweight64(resp->cfg_info.oqmask);
  2675. if (!(num_iqueues) || !(num_oqueues)) {
  2676. dev_err(&octeon_dev->pci_dev->dev,
  2677. "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
  2678. resp->cfg_info.iqmask,
  2679. resp->cfg_info.oqmask);
  2680. goto setup_nic_dev_fail;
  2681. }
  2682. dev_dbg(&octeon_dev->pci_dev->dev,
  2683. "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
  2684. i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
  2685. num_iqueues, num_oqueues);
  2686. netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
  2687. if (!netdev) {
  2688. dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
  2689. goto setup_nic_dev_fail;
  2690. }
  2691. props = &octeon_dev->props[i];
  2692. props->netdev = netdev;
  2693. if (num_iqueues > 1)
  2694. lionetdevops.ndo_select_queue = select_q;
  2695. /* Associate the routines that will handle different
  2696. * netdev tasks.
  2697. */
  2698. netdev->netdev_ops = &lionetdevops;
  2699. lio = GET_LIO(netdev);
  2700. memset(lio, 0, sizeof(struct lio));
  2701. lio->linfo.ifidx = resp->cfg_info.ifidx;
  2702. lio->ifidx = resp->cfg_info.ifidx;
  2703. lio->linfo.num_rxpciq = num_oqueues;
  2704. lio->linfo.num_txpciq = num_iqueues;
  2705. q_mask = resp->cfg_info.oqmask;
  2706. /* q_mask is 0-based and already verified mask is nonzero */
  2707. for (j = 0; j < num_oqueues; j++) {
  2708. q_no = __ffs64(q_mask);
  2709. q_mask &= (~(1UL << q_no));
  2710. lio->linfo.rxpciq[j] = q_no;
  2711. }
  2712. q_mask = resp->cfg_info.iqmask;
  2713. for (j = 0; j < num_iqueues; j++) {
  2714. q_no = __ffs64(q_mask);
  2715. q_mask &= (~(1UL << q_no));
  2716. lio->linfo.txpciq[j] = q_no;
  2717. }
  2718. lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
  2719. lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
  2720. lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
  2721. lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  2722. lio->dev_capability = NETIF_F_HIGHDMA
  2723. | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
  2724. | NETIF_F_SG | NETIF_F_RXCSUM
  2725. | NETIF_F_TSO | NETIF_F_TSO6
  2726. | NETIF_F_LRO;
  2727. netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
  2728. netdev->features = lio->dev_capability;
  2729. netdev->vlan_features = lio->dev_capability;
  2730. netdev->hw_features = lio->dev_capability;
  2731. /* Point to the properties for octeon device to which this
  2732. * interface belongs.
  2733. */
  2734. lio->oct_dev = octeon_dev;
  2735. lio->octprops = props;
  2736. lio->netdev = netdev;
  2737. spin_lock_init(&lio->lock);
  2738. dev_dbg(&octeon_dev->pci_dev->dev,
  2739. "if%d gmx: %d hw_addr: 0x%llx\n", i,
  2740. lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
  2741. /* 64-bit swap required on LE machines */
  2742. octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
  2743. for (j = 0; j < 6; j++)
  2744. mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
  2745. /* Copy MAC Address to OS network device structure */
  2746. ether_addr_copy(netdev->dev_addr, mac);
  2747. if (setup_io_queues(octeon_dev, netdev)) {
  2748. dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
  2749. goto setup_nic_dev_fail;
  2750. }
  2751. ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
  2752. /* By default all interfaces on a single Octeon uses the same
  2753. * tx and rx queues
  2754. */
  2755. lio->txq = lio->linfo.txpciq[0];
  2756. lio->rxq = lio->linfo.rxpciq[0];
  2757. lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
  2758. lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
  2759. if (setup_glist(lio)) {
  2760. dev_err(&octeon_dev->pci_dev->dev,
  2761. "Gather list allocation failed\n");
  2762. goto setup_nic_dev_fail;
  2763. }
  2764. /* Register ethtool support */
  2765. liquidio_set_ethtool_ops(netdev);
  2766. liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE);
  2767. if ((debug != -1) && (debug & NETIF_MSG_HW))
  2768. liquidio_set_feature(netdev, OCTNET_CMD_VERBOSE_ENABLE);
  2769. /* Register the network device with the OS */
  2770. if (register_netdev(netdev)) {
  2771. dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
  2772. goto setup_nic_dev_fail;
  2773. }
  2774. dev_dbg(&octeon_dev->pci_dev->dev,
  2775. "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
  2776. i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  2777. netif_carrier_off(netdev);
  2778. if (lio->linfo.link.s.status) {
  2779. netif_carrier_on(netdev);
  2780. start_txq(netdev);
  2781. } else {
  2782. netif_carrier_off(netdev);
  2783. }
  2784. ifstate_set(lio, LIO_IFSTATE_REGISTERED);
  2785. dev_dbg(&octeon_dev->pci_dev->dev,
  2786. "NIC ifidx:%d Setup successful\n", i);
  2787. octeon_free_soft_command(octeon_dev, sc);
  2788. }
  2789. return 0;
  2790. setup_nic_dev_fail:
  2791. octeon_free_soft_command(octeon_dev, sc);
  2792. while (i--) {
  2793. dev_err(&octeon_dev->pci_dev->dev,
  2794. "NIC ifidx:%d Setup failed\n", i);
  2795. liquidio_destroy_nic_device(octeon_dev, i);
  2796. }
  2797. return -ENODEV;
  2798. }
  2799. /**
  2800. * \brief initialize the NIC
  2801. * @param oct octeon device
  2802. *
  2803. * This initialization routine is called once the Octeon device application is
  2804. * up and running
  2805. */
  2806. static int liquidio_init_nic_module(struct octeon_device *oct)
  2807. {
  2808. struct oct_intrmod_cfg *intrmod_cfg;
  2809. int retval = 0;
  2810. int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
  2811. dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
  2812. /* only default iq and oq were initialized
  2813. * initialize the rest as well
  2814. */
  2815. /* run port_config command for each port */
  2816. oct->ifcount = num_nic_ports;
  2817. memset(oct->props, 0,
  2818. sizeof(struct octdev_props) * num_nic_ports);
  2819. retval = setup_nic_devices(oct);
  2820. if (retval) {
  2821. dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
  2822. goto octnet_init_failure;
  2823. }
  2824. liquidio_ptp_init(oct);
  2825. /* Initialize interrupt moderation params */
  2826. intrmod_cfg = &((struct octeon_device *)oct)->intrmod;
  2827. intrmod_cfg->intrmod_enable = 1;
  2828. intrmod_cfg->intrmod_check_intrvl = LIO_INTRMOD_CHECK_INTERVAL;
  2829. intrmod_cfg->intrmod_maxpkt_ratethr = LIO_INTRMOD_MAXPKT_RATETHR;
  2830. intrmod_cfg->intrmod_minpkt_ratethr = LIO_INTRMOD_MINPKT_RATETHR;
  2831. intrmod_cfg->intrmod_maxcnt_trigger = LIO_INTRMOD_MAXCNT_TRIGGER;
  2832. intrmod_cfg->intrmod_maxtmr_trigger = LIO_INTRMOD_MAXTMR_TRIGGER;
  2833. intrmod_cfg->intrmod_mintmr_trigger = LIO_INTRMOD_MINTMR_TRIGGER;
  2834. intrmod_cfg->intrmod_mincnt_trigger = LIO_INTRMOD_MINCNT_TRIGGER;
  2835. dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
  2836. return retval;
  2837. octnet_init_failure:
  2838. oct->ifcount = 0;
  2839. return retval;
  2840. }
  2841. /**
  2842. * \brief starter callback that invokes the remaining initialization work after
  2843. * the NIC is up and running.
  2844. * @param octptr work struct work_struct
  2845. */
  2846. static void nic_starter(struct work_struct *work)
  2847. {
  2848. struct octeon_device *oct;
  2849. struct cavium_wk *wk = (struct cavium_wk *)work;
  2850. oct = (struct octeon_device *)wk->ctxptr;
  2851. if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
  2852. return;
  2853. /* If the status of the device is CORE_OK, the core
  2854. * application has reported its application type. Call
  2855. * any registered handlers now and move to the RUNNING
  2856. * state.
  2857. */
  2858. if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
  2859. schedule_delayed_work(&oct->nic_poll_work.work,
  2860. LIQUIDIO_STARTER_POLL_INTERVAL_MS);
  2861. return;
  2862. }
  2863. atomic_set(&oct->status, OCT_DEV_RUNNING);
  2864. if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
  2865. dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
  2866. if (liquidio_init_nic_module(oct))
  2867. dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
  2868. else
  2869. handshake[oct->octeon_id].started_ok = 1;
  2870. } else {
  2871. dev_err(&oct->pci_dev->dev,
  2872. "Unexpected application running on NIC (%d). Check firmware.\n",
  2873. oct->app_mode);
  2874. }
  2875. complete(&handshake[oct->octeon_id].started);
  2876. }
  2877. /**
  2878. * \brief Device initialization for each Octeon device that is probed
  2879. * @param octeon_dev octeon device
  2880. */
  2881. static int octeon_device_init(struct octeon_device *octeon_dev)
  2882. {
  2883. int j, ret;
  2884. struct octeon_device_priv *oct_priv =
  2885. (struct octeon_device_priv *)octeon_dev->priv;
  2886. atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
  2887. /* Enable access to the octeon device and make its DMA capability
  2888. * known to the OS.
  2889. */
  2890. if (octeon_pci_os_setup(octeon_dev))
  2891. return 1;
  2892. /* Identify the Octeon type and map the BAR address space. */
  2893. if (octeon_chip_specific_setup(octeon_dev)) {
  2894. dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
  2895. return 1;
  2896. }
  2897. atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
  2898. octeon_dev->app_mode = CVM_DRV_INVALID_APP;
  2899. /* Do a soft reset of the Octeon device. */
  2900. if (octeon_dev->fn_list.soft_reset(octeon_dev))
  2901. return 1;
  2902. /* Initialize the dispatch mechanism used to push packets arriving on
  2903. * Octeon Output queues.
  2904. */
  2905. if (octeon_init_dispatch_list(octeon_dev))
  2906. return 1;
  2907. octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
  2908. OPCODE_NIC_CORE_DRV_ACTIVE,
  2909. octeon_core_drv_init,
  2910. octeon_dev);
  2911. INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
  2912. octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
  2913. schedule_delayed_work(&octeon_dev->nic_poll_work.work,
  2914. LIQUIDIO_STARTER_POLL_INTERVAL_MS);
  2915. atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
  2916. octeon_set_io_queues_off(octeon_dev);
  2917. /* Setup the data structures that manage this Octeon's Input queues. */
  2918. if (octeon_setup_instr_queues(octeon_dev)) {
  2919. dev_err(&octeon_dev->pci_dev->dev,
  2920. "instruction queue initialization failed\n");
  2921. /* On error, release any previously allocated queues */
  2922. for (j = 0; j < octeon_dev->num_iqs; j++)
  2923. octeon_delete_instr_queue(octeon_dev, j);
  2924. return 1;
  2925. }
  2926. atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
  2927. /* Initialize soft command buffer pool
  2928. */
  2929. if (octeon_setup_sc_buffer_pool(octeon_dev)) {
  2930. dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
  2931. return 1;
  2932. }
  2933. atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
  2934. /* Initialize lists to manage the requests of different types that
  2935. * arrive from user & kernel applications for this octeon device.
  2936. */
  2937. if (octeon_setup_response_list(octeon_dev)) {
  2938. dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
  2939. return 1;
  2940. }
  2941. atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
  2942. if (octeon_setup_output_queues(octeon_dev)) {
  2943. dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
  2944. /* Release any previously allocated queues */
  2945. for (j = 0; j < octeon_dev->num_oqs; j++)
  2946. octeon_delete_droq(octeon_dev, j);
  2947. }
  2948. atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
  2949. /* The input and output queue registers were setup earlier (the queues
  2950. * were not enabled). Any additional registers that need to be
  2951. * programmed should be done now.
  2952. */
  2953. ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
  2954. if (ret) {
  2955. dev_err(&octeon_dev->pci_dev->dev,
  2956. "Failed to configure device registers\n");
  2957. return ret;
  2958. }
  2959. /* Initialize the tasklet that handles output queue packet processing.*/
  2960. dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
  2961. tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
  2962. (unsigned long)octeon_dev);
  2963. /* Setup the interrupt handler and record the INT SUM register address
  2964. */
  2965. octeon_setup_interrupt(octeon_dev);
  2966. /* Enable Octeon device interrupts */
  2967. octeon_dev->fn_list.enable_interrupt(octeon_dev->chip);
  2968. /* Enable the input and output queues for this Octeon device */
  2969. octeon_dev->fn_list.enable_io_queues(octeon_dev);
  2970. atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
  2971. dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
  2972. if (ddr_timeout == 0) {
  2973. dev_info(&octeon_dev->pci_dev->dev,
  2974. "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
  2975. }
  2976. schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
  2977. /* Wait for the octeon to initialize DDR after the soft-reset. */
  2978. ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
  2979. if (ret) {
  2980. dev_err(&octeon_dev->pci_dev->dev,
  2981. "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
  2982. ret);
  2983. return 1;
  2984. }
  2985. if (octeon_wait_for_bootloader(octeon_dev, 1000) != 0) {
  2986. dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
  2987. return 1;
  2988. }
  2989. dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
  2990. ret = octeon_init_consoles(octeon_dev);
  2991. if (ret) {
  2992. dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
  2993. return 1;
  2994. }
  2995. ret = octeon_add_console(octeon_dev, 0);
  2996. if (ret) {
  2997. dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
  2998. return 1;
  2999. }
  3000. atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
  3001. dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
  3002. ret = load_firmware(octeon_dev);
  3003. if (ret) {
  3004. dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
  3005. return 1;
  3006. }
  3007. handshake[octeon_dev->octeon_id].init_ok = 1;
  3008. complete(&handshake[octeon_dev->octeon_id].init);
  3009. atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
  3010. /* Send Credit for Octeon Output queues. Credits are always sent after
  3011. * the output queue is enabled.
  3012. */
  3013. for (j = 0; j < octeon_dev->num_oqs; j++)
  3014. writel(octeon_dev->droq[j]->max_count,
  3015. octeon_dev->droq[j]->pkts_credit_reg);
  3016. /* Packets can start arriving on the output queues from this point. */
  3017. return 0;
  3018. }
  3019. /**
  3020. * \brief Exits the module
  3021. */
  3022. static void __exit liquidio_exit(void)
  3023. {
  3024. liquidio_deinit_pci();
  3025. pr_info("LiquidIO network module is now unloaded\n");
  3026. }
  3027. module_init(liquidio_init);
  3028. module_exit(liquidio_exit);